c87ef53b7c851fe8e8c6cb075e72606e8352cdf3
[mirror/winof/.git] / ulp / ipoib_NDIS6_CM / kernel / ipoib_adapter.cpp
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.\r
4  * Portions Copyright (c) 2008 Microsoft Corporation.  All rights reserved.\r
5  *\r
6  * This software is available to you under the OpenIB.org BSD license\r
7  * below:\r
8  *\r
9  *     Redistribution and use in source and binary forms, with or\r
10  *     without modification, are permitted provided that the following\r
11  *     conditions are met:\r
12  *\r
13  *      - Redistributions of source code must retain the above\r
14  *        copyright notice, this list of conditions and the following\r
15  *        disclaimer.\r
16  *\r
17  *      - Redistributions in binary form must reproduce the above\r
18  *        copyright notice, this list of conditions and the following\r
19  *        disclaimer in the documentation and/or other materials\r
20  *        provided with the distribution.\r
21  *\r
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
29  * SOFTWARE.\r
30  *\r
31  * $Id: ipoib_adapter.c 4506 2009-06-23 14:40:54Z xalex $\r
32  */\r
33 \r
34 #include <precompile.h>\r
35 \r
36 \r
37 #if defined(EVENT_TRACING)\r
38 #ifdef offsetof\r
39 #undef offsetof\r
40 #endif\r
41 #include "ipoib_adapter.tmh"\r
42 #endif\r
43 \r
44 \r
45 #define ITEM_POOL_START         16\r
46 #define ITEM_POOL_GROW          16\r
47 \r
48 \r
49 /* IB Link speeds in 100bps */\r
50 #define ONE_X_IN_100BPS         25000000\r
51 #define FOUR_X_IN_100BPS        100000000\r
52 #define TWELVE_X_IN_100BPS      300000000\r
53 \r
54 \r
55 /* Declarations */\r
56 static void\r
57 adapter_construct(\r
58         IN                              ipoib_adapter_t* const          p_adapter );\r
59 \r
60 \r
61 static ib_api_status_t\r
62 adapter_init(\r
63         IN                              ipoib_adapter_t* const          p_adapter );\r
64 \r
65 \r
66 static void\r
67 __adapter_destroying(\r
68         IN                              cl_obj_t* const                         p_obj );\r
69 \r
70 \r
71 static void\r
72 __adapter_free(\r
73         IN                              cl_obj_t* const                         p_obj );\r
74 \r
75 \r
76 static ib_api_status_t\r
77 __ipoib_pnp_reg(\r
78         IN                              ipoib_adapter_t* const          p_adapter,\r
79         IN                              ib_pnp_class_t                          flags );\r
80 \r
81 \r
82 static void\r
83 __ipoib_pnp_dereg(\r
84         IN                              void*                                           context );\r
85 \r
86 \r
87 static void\r
88 __ipoib_adapter_reset(\r
89         IN                              void*   context);\r
90 \r
91 \r
92 static ib_api_status_t\r
93 __ipoib_pnp_cb(\r
94         IN                              ib_pnp_rec_t                            *p_pnp_rec );\r
95 \r
96 \r
97 /* Leaves all mcast groups when port goes down. */\r
98 static void\r
99 ipoib_clear_mcast(\r
100         IN                              ipoib_port_t* const                     p_port );\r
101 \r
102 NDIS_STATUS\r
103 ipoib_get_adapter_guids(\r
104         IN                              NDIS_HANDLE* const                      h_adapter,\r
105         IN      OUT                     ipoib_adapter_t                         *p_adapter );\r
106 \r
107 NDIS_STATUS\r
108 ipoib_get_adapter_params(\r
109         IN      OUT                     ipoib_adapter_t                         *p_adapter,\r
110         OUT                             PUCHAR                                          *p_mac,\r
111         OUT                             UINT                                            *p_len);\r
112 \r
113 \r
114 /* Implementation */\r
115 ib_api_status_t\r
116 ipoib_create_adapter(\r
117         IN                              void* const                                     h_adapter,\r
118                 OUT                     ipoib_adapter_t** const         pp_adapter )\r
119 {\r
120         ipoib_adapter_t         *p_adapter;\r
121         NDIS_STATUS                     status;\r
122         ib_api_status_t         ib_status;\r
123         cl_status_t                     cl_status;\r
124         PUCHAR                          mac;\r
125         UINT                            len;\r
126 \r
127         IPOIB_ENTER( IPOIB_DBG_INIT );\r
128 \r
129         p_adapter = (ipoib_adapter_t *) cl_zalloc( sizeof(ipoib_adapter_t) );\r
130         if( !p_adapter )\r
131         {\r
132                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
133                         ("Failed to allocate ipoib_adapter_t (%d bytes)",\r
134                         sizeof(ipoib_adapter_t)) );\r
135                 return IB_INSUFFICIENT_MEMORY;\r
136         }\r
137 \r
138         adapter_construct( p_adapter );\r
139 \r
140         p_adapter->h_adapter = h_adapter;\r
141 \r
142         p_adapter->p_ifc = (ib_al_ifc_t *) cl_zalloc( sizeof(ib_al_ifc_t) );\r
143         if( !p_adapter->p_ifc )\r
144         {\r
145                 __adapter_free( &p_adapter->obj );\r
146                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
147                         ("ipoib_create_adapter failed to alloc ipoib_ifc_t %d bytes\n",\r
148                         sizeof(ib_al_ifc_t)) );\r
149                 return IB_INSUFFICIENT_MEMORY;\r
150         }\r
151 \r
152         /* Get the CA and port GUID from the bus driver. */\r
153         status = ipoib_get_adapter_guids( (NDIS_HANDLE *const) h_adapter,  p_adapter );\r
154         if( status != NDIS_STATUS_SUCCESS )\r
155         {\r
156                 //ASSERT(FALSE);\r
157                 __adapter_free( &p_adapter->obj );\r
158                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
159                         ("ipoib_get_adapter_guids returned 0x%.8X.\n", status) );\r
160                 return IB_ERROR;\r
161         }\r
162 \r
163         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
164                         ("Port %016I64x (CA %016I64x port %d) initializing\n",\r
165                         p_adapter->guids.port_guid.guid, p_adapter->guids.ca_guid,\r
166                         p_adapter->guids.port_num) );\r
167 \r
168         cl_status = cl_obj_init( &p_adapter->obj, CL_DESTROY_SYNC,\r
169                 __adapter_destroying, NULL, __adapter_free );\r
170         if( cl_status != CL_SUCCESS )\r
171         {\r
172                 __adapter_free( &p_adapter->obj );\r
173                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
174                         ("cl_obj_init returned %#x\n", cl_status) );\r
175                 return IB_ERROR;\r
176         }\r
177 \r
178         /* Read configuration parameters. */\r
179         status = ipoib_get_adapter_params( p_adapter , &mac, &len );\r
180         if( status != NDIS_STATUS_SUCCESS )\r
181         {\r
182                 cl_obj_destroy( &p_adapter->obj );\r
183                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
184                         ("ipoib_get_adapter_params returned 0x%.8x.\n", status) );\r
185                 return IB_ERROR;\r
186         }\r
187                 \r
188         ib_status = adapter_init( p_adapter );\r
189         if( ib_status != IB_SUCCESS )\r
190         {\r
191                 cl_obj_destroy( &p_adapter->obj );\r
192                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
193                         ("adapter_init returned %s.\n", \r
194                         p_adapter->p_ifc->get_err_str( ib_status )) );\r
195                 return ib_status;\r
196         }\r
197 \r
198         ETH_COPY_NETWORK_ADDRESS( p_adapter->params.conf_mac.addr, p_adapter->mac.addr );\r
199         /* If there is a NetworkAddress override in registry, use it */\r
200         if( (len == HW_ADDR_LEN) && (mac != NULL) )\r
201         {\r
202                 if( ETH_IS_MULTICAST(mac) || ETH_IS_BROADCAST(mac) ||\r
203                         !ETH_IS_LOCALLY_ADMINISTERED(mac) )\r
204                 {\r
205                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_INIT,\r
206                                 ("Overriding NetworkAddress is invalid - "\r
207                                 "%02x-%02x-%02x-%02x-%02x-%02x\n",\r
208                                 mac[0], mac[1], mac[2],\r
209                                 mac[3], mac[4], mac[5]) );\r
210                 }\r
211                 else\r
212         {\r
213                         ETH_COPY_NETWORK_ADDRESS( p_adapter->params.conf_mac.addr, mac );\r
214                 }\r
215         }\r
216 \r
217         *pp_adapter = p_adapter;\r
218 \r
219         IPOIB_EXIT( IPOIB_DBG_INIT );\r
220         return IB_SUCCESS;\r
221 }\r
222 \r
223 \r
224 ib_api_status_t\r
225 ipoib_start_adapter(\r
226         IN                              ipoib_adapter_t* const          p_adapter )\r
227 {\r
228         ib_api_status_t status;\r
229 \r
230         IPOIB_ENTER( IPOIB_DBG_INIT );\r
231 \r
232         status = __ipoib_pnp_reg( p_adapter,\r
233                 IB_PNP_FLAG_REG_SYNC | IB_PNP_FLAG_REG_COMPLETE );\r
234 \r
235         IPOIB_EXIT( IPOIB_DBG_INIT );\r
236         return status;\r
237 }\r
238 \r
239 \r
240 void\r
241 ipoib_destroy_adapter(\r
242         IN                              ipoib_adapter_t* const          p_adapter )\r
243 {\r
244         IPOIB_ENTER( IPOIB_DBG_INIT );\r
245 \r
246         CL_ASSERT( p_adapter );\r
247 \r
248         /*\r
249          * Flag the adapter as being removed.  We use the IB_PNP_PORT_REMOVE state\r
250          * for this purpose.  Note that we protect this state change with both the\r
251          * mutex and the lock.  The mutex provides synchronization as a whole\r
252          * between destruction and AL callbacks (PnP, Query, Destruction).\r
253          * The lock provides protection\r
254          */\r
255         KeWaitForMutexObject(\r
256                 &p_adapter->mutex, Executive, KernelMode, FALSE, NULL );\r
257         cl_obj_lock( &p_adapter->obj );\r
258         p_adapter->state = IB_PNP_PORT_REMOVE;\r
259 \r
260         /*\r
261          * Clear the pointer to the port object since the object destruction\r
262          * will cascade to child objects.  This prevents potential duplicate\r
263          * destruction (or worse, stale pointer usage).\r
264          */\r
265         p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
266         p_adapter->p_port = NULL;\r
267 \r
268         cl_obj_unlock( &p_adapter->obj );\r
269 \r
270         KeReleaseMutex( &p_adapter->mutex, FALSE );\r
271 \r
272         cl_obj_destroy( &p_adapter->obj );\r
273 \r
274         IPOIB_EXIT( IPOIB_DBG_INIT );\r
275 }\r
276 \r
277 \r
278 static void\r
279 adapter_construct(\r
280         IN                              ipoib_adapter_t* const          p_adapter )\r
281 {\r
282         cl_obj_construct( &p_adapter->obj, IPOIB_OBJ_INSTANCE );\r
283         cl_spinlock_construct( &p_adapter->send_stat_lock );\r
284         cl_spinlock_construct( &p_adapter->recv_stat_lock );\r
285         cl_qpool_construct( &p_adapter->item_pool );\r
286         KeInitializeMutex( &p_adapter->mutex, 0 );\r
287 \r
288         cl_thread_construct(&p_adapter->destroy_thread);\r
289         \r
290         cl_vector_construct( &p_adapter->ip_vector );\r
291 \r
292         cl_perf_construct( &p_adapter->perf );\r
293 \r
294         p_adapter->state = IB_PNP_PORT_ADD;\r
295         p_adapter->port_rate = FOUR_X_IN_100BPS;\r
296 }\r
297 \r
298 \r
299 static ib_api_status_t\r
300 adapter_init(\r
301         IN                              ipoib_adapter_t* const          p_adapter )\r
302 {\r
303         cl_status_t                     cl_status;\r
304         ib_api_status_t         status;\r
305 \r
306         IPOIB_ENTER( IPOIB_DBG_INIT );\r
307 \r
308         cl_status = cl_perf_init( &p_adapter->perf, MaxPerf );\r
309         if( cl_status != CL_SUCCESS )\r
310         {\r
311                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
312                         ("cl_perf_init returned %#x\n", cl_status) );\r
313                 return IB_ERROR;\r
314         }\r
315 \r
316         cl_status = cl_spinlock_init( &p_adapter->send_stat_lock );\r
317         if( cl_status != CL_SUCCESS )\r
318         {\r
319                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
320                         ("cl_spinlock_init returned %#x\n", cl_status) );\r
321                 return IB_ERROR;\r
322         }\r
323 \r
324         cl_status = cl_spinlock_init( &p_adapter->recv_stat_lock );\r
325         if( cl_status != CL_SUCCESS )\r
326         {\r
327                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
328                         ("cl_spinlock_init returned %#x\n", cl_status) );\r
329                 return IB_ERROR;\r
330         }\r
331 \r
332         cl_status = cl_qpool_init( &p_adapter->item_pool, ITEM_POOL_START, 0,\r
333                 ITEM_POOL_GROW, sizeof(cl_pool_obj_t), NULL, NULL, NULL );\r
334         if( cl_status != CL_SUCCESS )\r
335         {\r
336                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
337                         ("cl_qpool_init returned %#x\n", cl_status) );\r
338                 return IB_ERROR;\r
339         }\r
340 \r
341 \r
342         /* We manually manage the size and capacity of the vector. */\r
343         cl_status = cl_vector_init( &p_adapter->ip_vector, 0,\r
344                 0, sizeof(net_address_item_t), NULL, NULL, p_adapter );\r
345         if( cl_status != CL_SUCCESS )\r
346         {\r
347                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
348                         ("cl_vector_init for ip_vector returned %#x\n",\r
349                         cl_status) );\r
350                 return IB_ERROR;\r
351         }\r
352 \r
353         /* Validate the port GUID and generate the MAC address. */\r
354         status =\r
355                 ipoib_mac_from_guid( p_adapter->guids.port_guid.guid, p_adapter->params.guid_mask, &p_adapter->mac);\r
356         if( status != IB_SUCCESS )\r
357         {\r
358                 if( status == IB_INVALID_GUID_MASK )\r
359                 {\r
360                         IPOIB_PRINT( TRACE_LEVEL_WARNING, IPOIB_DBG_ERROR,\r
361                                 ("Invalid GUID mask received, rejecting it") );\r
362                         ipoib_create_log(p_adapter->h_adapter, GUID_MASK_LOG_INDEX, EVENT_IPOIB_WRONG_PARAMETER_WRN);\r
363                 }\r
364 \r
365                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
366                         ("ipoib_mac_from_guid returned %s\n", \r
367                         p_adapter->p_ifc->get_err_str( status )) );\r
368                 return status;\r
369         }\r
370 \r
371         /* Open AL. */\r
372         status = p_adapter->p_ifc->open_al( &p_adapter->h_al );\r
373         if( status != IB_SUCCESS )\r
374         {\r
375                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
376                         ("ib_open_al returned %s\n", \r
377                         p_adapter->p_ifc->get_err_str( status )) );\r
378                 return status;\r
379         }\r
380 \r
381         IPOIB_EXIT( IPOIB_DBG_INIT );\r
382         return status;\r
383 }\r
384 \r
385 \r
386 static ib_api_status_t\r
387 __ipoib_pnp_reg(\r
388         IN                              ipoib_adapter_t* const          p_adapter,\r
389         IN                              ib_pnp_class_t                          flags )\r
390 {\r
391         ib_api_status_t         status;\r
392         ib_pnp_req_t            pnp_req;\r
393         \r
394         IPOIB_ENTER( IPOIB_DBG_INIT );\r
395 \r
396         CL_ASSERT( !p_adapter->h_pnp );\r
397         CL_ASSERT( !p_adapter->registering );\r
398 \r
399         p_adapter->registering = TRUE;\r
400         \r
401         /* Register for PNP events. */\r
402         cl_memclr( &pnp_req, sizeof(pnp_req) );\r
403         pnp_req.pnp_class = IB_PNP_PORT | flags;\r
404         /*\r
405          * Context is the cl_obj of the adapter to allow passing cl_obj_deref\r
406          * to ib_dereg_pnp.\r
407          */\r
408         pnp_req.pnp_context = &p_adapter->obj;\r
409         pnp_req.pfn_pnp_cb = __ipoib_pnp_cb;\r
410         status = p_adapter->p_ifc->reg_pnp( p_adapter->h_al, &pnp_req, &p_adapter->h_pnp );\r
411         if( status != IB_SUCCESS )\r
412         {\r
413                 //ASSERT(FALSE);\r
414                 p_adapter->registering = FALSE;\r
415                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
416                         ("ib_reg_pnp returned %s\n", \r
417                         p_adapter->p_ifc->get_err_str( status )) );\r
418                 return status;\r
419         }\r
420         /*\r
421          * Reference the adapter on behalf of the PNP registration.\r
422          * This allows the destruction to block until the PNP deregistration\r
423          * completes.\r
424          */\r
425         cl_obj_ref( &p_adapter->obj );\r
426 \r
427         IPOIB_EXIT( IPOIB_DBG_INIT );\r
428         return status;\r
429 }\r
430 \r
431 \r
432 static void\r
433 __adapter_destroying(\r
434         IN                              cl_obj_t* const                         p_obj )\r
435 {\r
436         ipoib_adapter_t         *p_adapter;\r
437         KLOCK_QUEUE_HANDLE      hdl;\r
438 \r
439         IPOIB_ENTER( IPOIB_DBG_INIT );\r
440 \r
441         p_adapter = PARENT_STRUCT( p_obj, ipoib_adapter_t, obj );\r
442 \r
443         /*\r
444          * The adapter's object will be dereferenced when the deregistration\r
445          * completes.  No need to lock here since all PnP related API calls\r
446          * are driven by NDIS (via the Init/Reset/Destroy paths).\r
447          */\r
448         if( p_adapter->h_pnp )\r
449         {\r
450                 p_adapter->p_ifc->dereg_pnp( p_adapter->h_pnp, (ib_pfn_destroy_cb_t) cl_obj_deref );\r
451                 p_adapter->h_pnp = NULL;\r
452         }\r
453 \r
454         if( p_adapter->packet_filter )\r
455         {\r
456                 KeAcquireInStackQueuedSpinLock( &g_ipoib.lock, &hdl );\r
457                 cl_obj_lock( &p_adapter->obj );\r
458 \r
459                 ASSERT( cl_qlist_count( &g_ipoib.adapter_list ) );\r
460                 cl_qlist_remove_item( &g_ipoib.adapter_list, &p_adapter->entry );\r
461 \r
462                 p_adapter->packet_filter = 0;\r
463 \r
464                 cl_obj_unlock( &p_adapter->obj );\r
465                 KeReleaseInStackQueuedSpinLock( &hdl );\r
466         }\r
467 \r
468         IPOIB_EXIT( IPOIB_DBG_INIT );\r
469 }\r
470 \r
471 \r
472 static void\r
473 __adapter_free(\r
474         IN                              cl_obj_t* const                         p_obj )\r
475 {\r
476         ipoib_adapter_t *p_adapter;\r
477 \r
478         IPOIB_ENTER( IPOIB_DBG_INIT );\r
479 \r
480         p_adapter = PARENT_STRUCT( p_obj, ipoib_adapter_t, obj );\r
481 \r
482         if( p_adapter->p_ifc )\r
483         {\r
484                 if( p_adapter->h_al )\r
485                         p_adapter->p_ifc->close_al( p_adapter->h_al );\r
486 \r
487                 cl_free( p_adapter->p_ifc );\r
488                 p_adapter->p_ifc = NULL;\r
489         }\r
490 \r
491         cl_vector_destroy( &p_adapter->ip_vector );\r
492         cl_qpool_destroy( &p_adapter->item_pool );\r
493         cl_spinlock_destroy( &p_adapter->recv_stat_lock );\r
494         cl_spinlock_destroy( &p_adapter->send_stat_lock );\r
495         cl_obj_deinit( p_obj );\r
496 \r
497         cl_perf_destroy( &p_adapter->perf, TRUE );\r
498 \r
499         cl_free( p_adapter );\r
500 \r
501         IPOIB_EXIT( IPOIB_DBG_INIT );\r
502 }\r
503 \r
504 \r
505 static ib_api_status_t\r
506 ipoib_query_pkey_index(ipoib_adapter_t  *p_adapter)\r
507 {\r
508         ib_api_status_t                 status;\r
509         ib_ca_attr_t                *ca_attr;\r
510         uint32_t                            ca_size;\r
511         uint16_t index = 0;\r
512 \r
513         /* Query the CA for Pkey table */\r
514         status = p_adapter->p_ifc->query_ca(p_adapter->p_port->ib_mgr.h_ca, NULL, &ca_size);\r
515         if(status != IB_INSUFFICIENT_MEMORY)\r
516         {\r
517                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
518                                                 ("ib_query_ca failed\n"));\r
519                 return status;\r
520         }\r
521 \r
522         ca_attr = (ib_ca_attr_t*)cl_zalloc(ca_size);\r
523         if      (!ca_attr)\r
524         {\r
525                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
526                                                 ("cl_zalloc can't allocate %d\n",ca_size));\r
527                 return IB_INSUFFICIENT_MEMORY;\r
528         }\r
529 \r
530         status = p_adapter->p_ifc->query_ca(p_adapter->p_port->ib_mgr.h_ca, ca_attr,&ca_size);  \r
531         if( status != IB_SUCCESS )\r
532         {\r
533                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
534                                                 ("ib_query_ca returned %s\n", \r
535                                                  p_adapter->p_ifc->get_err_str( status )) );\r
536                 goto pkey_end;\r
537         }\r
538         CL_ASSERT(ca_attr->p_port_attr[p_adapter->p_port->port_num -1].p_pkey_table[0] == IB_DEFAULT_PKEY);\r
539         for(index = 0; index < ca_attr->p_port_attr[p_adapter->p_port->port_num -1].num_pkeys; index++)\r
540         {\r
541                 if(cl_hton16(p_adapter->guids.port_guid.pkey) == ca_attr->p_port_attr[p_adapter->p_port->port_num -1].p_pkey_table[index])\r
542                         break;\r
543         }\r
544         if(index >= ca_attr->p_port_attr[p_adapter->p_port->port_num -1].num_pkeys)\r
545         {\r
546                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
547                                                 ("Pkey table is invalid, index not found\n"));\r
548                 NdisWriteErrorLogEntry( p_adapter->h_adapter,\r
549                         EVENT_IPOIB_PARTITION_ERR, 1, p_adapter->guids.port_guid.pkey );\r
550                 status = IB_NOT_FOUND;\r
551                 p_adapter->p_port->pkey_index = PKEY_INVALID_INDEX;\r
552                 goto pkey_end;\r
553         }\r
554 \r
555         p_adapter->p_port->pkey_index = index;\r
556         IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_IB,\r
557                                         ("for PKEY = 0x%04X got index = %d\n",p_adapter->guids.port_guid.pkey,index));\r
558 \r
559 pkey_end:\r
560         if(ca_attr)\r
561                 cl_free(ca_attr);\r
562         return status;\r
563 }\r
564 \r
565 static inline void __ipoib_get_down(\r
566         IN ipoib_adapter_t              *p_adapter) \r
567 {\r
568 \r
569         NDIS_LINK_STATE         link_state;\r
570         NDIS_STATUS_INDICATION  status_indication;\r
571         \r
572         link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
573         link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
574         link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
575         link_state.MediaConnectState = MediaConnectStateDisconnected;\r
576         link_state.MediaDuplexState = MediaDuplexStateFull;\r
577         link_state.XmitLinkSpeed = \r
578         link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
579         link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
580 \r
581         IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
582                            p_adapter->h_adapter,\r
583                            NDIS_STATUS_LINK_STATE,\r
584                            (PVOID)&link_state,\r
585                            sizeof(link_state));\r
586 \r
587         NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);\r
588         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
589                 ("Link DOWN!\n") );\r
590 \r
591         ipoib_port_down( p_adapter->p_port );\r
592 }\r
593 \r
594 static ib_api_status_t\r
595 __ipoib_pnp_cb(\r
596         IN                              ib_pnp_rec_t                            *p_pnp_rec )\r
597 {\r
598         ipoib_adapter_t         *p_adapter;\r
599         ipoib_port_t            *p_port;\r
600         ib_pnp_event_t          old_state;\r
601         ib_pnp_port_rec_t       *p_port_rec;\r
602         ib_api_status_t         status = IB_SUCCESS;\r
603         NDIS_LINK_STATE         link_state;\r
604         NDIS_STATUS_INDICATION  status_indication;\r
605 \r
606         IPOIB_ENTER( IPOIB_DBG_PNP );\r
607 \r
608         CL_ASSERT( p_pnp_rec );\r
609         NdisZeroMemory(&link_state, sizeof(NDIS_LINK_STATE));\r
610         p_adapter =\r
611                 PARENT_STRUCT( p_pnp_rec->pnp_context, ipoib_adapter_t, obj );\r
612 \r
613         CL_ASSERT( p_adapter );\r
614 \r
615         /* Synchronize with destruction */\r
616         KeWaitForMutexObject(\r
617                 &p_adapter->mutex, Executive, KernelMode, FALSE, NULL );\r
618         cl_obj_lock( &p_adapter->obj );\r
619         old_state = p_adapter->state;\r
620         cl_obj_unlock( &p_adapter->obj );\r
621         if( old_state == IB_PNP_PORT_REMOVE )\r
622         {\r
623                 KeReleaseMutex( &p_adapter->mutex, FALSE );\r
624                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_PNP,\r
625                         ("Aborting - Adapter destroying.\n") );\r
626                 return IB_NOT_DONE;\r
627         }\r
628 \r
629         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_PNP,\r
630                 ("p_pnp_rec->pnp_event = 0x%x (%s)\n",\r
631                 p_pnp_rec->pnp_event, ib_get_pnp_event_str( p_pnp_rec->pnp_event )) );\r
632 \r
633         p_port_rec = (ib_pnp_port_rec_t*)p_pnp_rec;\r
634 \r
635         switch( p_pnp_rec->pnp_event )\r
636         {\r
637         case IB_PNP_PORT_ADD:\r
638                 CL_ASSERT( !p_pnp_rec->context );\r
639                 /* Only process our port GUID. */\r
640                 if( p_pnp_rec->guid != p_adapter->guids.port_guid.guid )\r
641                 {\r
642                         status = IB_NOT_DONE;\r
643                         break;\r
644                 }\r
645 \r
646                 /* Don't process if we're destroying. */\r
647                 if( p_adapter->obj.state == CL_DESTROYING )\r
648                 {\r
649                         status = IB_NOT_DONE;\r
650                         break;\r
651                 }\r
652 \r
653                 CL_ASSERT( !p_adapter->p_port );\r
654                 /* Allocate all IB resources. */\r
655                 cl_obj_lock( &p_adapter->obj );\r
656                 p_adapter->state = IB_PNP_PORT_ADD;\r
657                 cl_obj_unlock( &p_adapter->obj );\r
658                 status = ipoib_create_port( p_adapter, p_port_rec, &p_port );\r
659                 cl_obj_lock( &p_adapter->obj );\r
660                 if( status != IB_SUCCESS )\r
661                 {\r
662                         p_adapter->state = old_state;\r
663                         cl_obj_unlock( &p_adapter->obj );\r
664                         p_adapter->hung = TRUE;\r
665                         break;\r
666                 }\r
667 \r
668                 p_pnp_rec->context = p_port;\r
669                 if ( p_adapter->p_stat )\r
670                         p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
671                 p_adapter->p_port = p_port;\r
672                 cl_obj_unlock( &p_adapter->obj );\r
673                 break;\r
674 \r
675         case IB_PNP_PORT_REMOVE:\r
676 \r
677                 if (p_adapter->state != IB_PNP_PORT_DOWN) {\r
678                         IPOIB_PRINT( TRACE_LEVEL_WARNING, IPOIB_DBG_ALL, \r
679                                 (" Port got IB_PNP_PORT_REMOVE before IB_PNP_PORT_DOWN\n"));\r
680                         __ipoib_get_down( p_adapter);\r
681                 }\r
682 \r
683                 /* Release all IB resources. */\r
684                 CL_ASSERT( p_pnp_rec->context );\r
685 \r
686                 cl_obj_lock( &p_adapter->obj );\r
687                 p_adapter->state = IB_PNP_PORT_REMOVE;\r
688                 if ( p_adapter->p_stat )\r
689                         p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
690                 p_port = p_adapter->p_port;\r
691                 p_adapter->p_port = NULL;\r
692                 cl_obj_unlock( &p_adapter->obj );\r
693                 ipoib_port_destroy( p_port );\r
694                 p_pnp_rec->context = NULL;\r
695                 status = IB_SUCCESS;\r
696                 break;\r
697 \r
698         case IB_PNP_PORT_ACTIVE:\r
699                 /* Join multicast groups and put QP in RTS. */\r
700                 CL_ASSERT( p_pnp_rec->context );\r
701 \r
702                 cl_obj_lock( &p_adapter->obj );\r
703                 p_adapter->state = IB_PNP_PORT_INIT;\r
704                 cl_obj_unlock( &p_adapter->obj );\r
705                 ipoib_port_up( p_adapter->p_port, p_port_rec );\r
706 \r
707                 status = IB_SUCCESS;\r
708                 break;\r
709 \r
710         case IB_PNP_PORT_ARMED:\r
711                 status = IB_SUCCESS;\r
712                 break;\r
713 \r
714         case IB_PNP_PORT_INIT:\r
715                 /*\r
716                  * Init could happen if the SM brings the port down\r
717                  * without changing the physical link.\r
718                  */\r
719         case IB_PNP_PORT_DOWN:\r
720                 CL_ASSERT( p_pnp_rec->context );\r
721 \r
722                 cl_obj_lock( &p_adapter->obj );\r
723                 old_state = p_adapter->state;\r
724                 p_adapter->state = IB_PNP_PORT_DOWN;\r
725                 cl_obj_unlock( &p_adapter->obj );\r
726                 status = IB_SUCCESS;\r
727 \r
728                 if( !p_adapter->registering && old_state != IB_PNP_PORT_DOWN )\r
729                 {\r
730                         __ipoib_get_down( p_adapter );\r
731                 }\r
732                 break;\r
733 \r
734         case IB_PNP_REG_COMPLETE:\r
735                 if( p_adapter->registering )\r
736                 {\r
737                         p_adapter->registering = FALSE;\r
738                         cl_obj_lock( &p_adapter->obj );\r
739                         old_state = p_adapter->state;\r
740                         cl_obj_unlock( &p_adapter->obj );\r
741 \r
742                         if( old_state == IB_PNP_PORT_DOWN )\r
743                         {\r
744                                 /* If we were initializing, we might have pended some OIDs. */\r
745                                 ipoib_resume_oids( p_adapter );\r
746 \r
747                                 link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
748                                 link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
749                                 link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
750                                 link_state.MediaConnectState = MediaConnectStateDisconnected;\r
751                                 link_state.MediaDuplexState = MediaDuplexStateFull;\r
752                                 link_state.XmitLinkSpeed = \r
753                                 link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
754                                 link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
755 \r
756                                 IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
757                                                                            p_adapter->h_adapter,\r
758                                                                            NDIS_STATUS_LINK_STATE,\r
759                                                                            (PVOID)&link_state,\r
760                                                                            sizeof(link_state));\r
761                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_INIT, ("Indicate DISCONNECT\n") );\r
762                                 NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);                 \r
763                         }\r
764                 }\r
765 \r
766                 if( p_adapter->reset && p_adapter->state != IB_PNP_PORT_INIT )\r
767                 {\r
768                         p_adapter->reset = FALSE;\r
769                         NdisMResetComplete(\r
770                                 p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
771                 }\r
772                 status = IB_SUCCESS;\r
773                 break;\r
774 \r
775         default:\r
776                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
777                         ("IPOIB: Received unhandled PnP event 0x%x (%s)\n",\r
778                         p_pnp_rec->pnp_event, ib_get_pnp_event_str( p_pnp_rec->pnp_event )) );\r
779                 /* Fall through. */\r
780 \r
781                 status = IB_SUCCESS;\r
782 \r
783                 /* We ignore events below if the link is not active. */\r
784                 if( p_port_rec->p_port_attr->link_state != IB_LINK_ACTIVE )\r
785                         break;\r
786 \r
787                 case IB_PNP_PKEY_CHANGE:\r
788                         if(p_pnp_rec->pnp_event == IB_PNP_PKEY_CHANGE && \r
789                            p_adapter->guids.port_guid.pkey != IB_DEFAULT_PKEY)\r
790                         {\r
791                                 status = ipoib_query_pkey_index(p_adapter);\r
792                                 if(status != IB_SUCCESS)\r
793                                 {\r
794                                    cl_obj_lock( &p_adapter->obj );\r
795                                    p_adapter->state = IB_PNP_PORT_INIT;\r
796                                    cl_obj_unlock( &p_adapter->obj );\r
797                                 }\r
798                         }\r
799 \r
800                 case IB_PNP_SM_CHANGE:\r
801                 case IB_PNP_GID_CHANGE:\r
802                 case IB_PNP_LID_CHANGE:\r
803 \r
804                 cl_obj_lock( &p_adapter->obj );\r
805                 old_state = p_adapter->state;\r
806                 switch( old_state )\r
807                 {\r
808                 case IB_PNP_PORT_DOWN:\r
809                         p_adapter->state = IB_PNP_PORT_INIT;\r
810                         break;\r
811 \r
812                 default:\r
813                         p_adapter->state = IB_PNP_PORT_DOWN;\r
814                 }\r
815                 cl_obj_unlock( &p_adapter->obj );\r
816                 \r
817                 if( p_adapter->registering )\r
818                         break;\r
819 \r
820                 switch( old_state )\r
821                 {\r
822                 case IB_PNP_PORT_ACTIVE:\r
823                 case IB_PNP_PORT_INIT:\r
824                         link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
825                         link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
826                         link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
827                         link_state.MediaConnectState = MediaConnectStateDisconnected;\r
828                         link_state.MediaDuplexState = MediaDuplexStateFull;\r
829                         link_state.XmitLinkSpeed = \r
830                         link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
831                         link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
832 \r
833                         IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
834                                                                    p_adapter->h_adapter,\r
835                                                                    NDIS_STATUS_LINK_STATE,\r
836                                                                    (PVOID)&link_state,\r
837                                                                    sizeof(link_state));\r
838 \r
839                         NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);         \r
840 \r
841                         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
842                                 ("Link DOWN!\n") );\r
843                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_INIT,\r
844                         ("Indicate DISCONNECT\n") );\r
845 \r
846                         ipoib_port_down( p_adapter->p_port );\r
847                         /* Fall through. */\r
848 \r
849                 case IB_PNP_PORT_DOWN:\r
850                         cl_obj_lock( &p_adapter->obj );\r
851                         p_adapter->state = IB_PNP_PORT_INIT;\r
852                         cl_obj_unlock( &p_adapter->obj );\r
853                         ipoib_port_up( p_adapter->p_port, (ib_pnp_port_rec_t*)p_pnp_rec );\r
854                 }\r
855                 break;\r
856         }\r
857 \r
858         KeReleaseMutex( &p_adapter->mutex, FALSE );\r
859 \r
860         IPOIB_EXIT( IPOIB_DBG_PNP );\r
861         return status;\r
862 }\r
863 \r
864 \r
865 /* Joins/leaves mcast groups based on currently programmed mcast MACs. */\r
866 void\r
867 ipoib_refresh_mcast(\r
868         IN                              ipoib_adapter_t* const          p_adapter,\r
869         IN                              mac_addr_t* const                       p_mac_array,\r
870         IN              const   uint8_t                                         num_macs )\r
871 {\r
872         uint8_t                         i, j;\r
873         ipoib_port_t            *p_port = NULL;\r
874 \r
875         IPOIB_ENTER( IPOIB_DBG_MCAST );\r
876         cl_obj_lock( &p_adapter->obj );\r
877         if( p_adapter->state == IB_PNP_PORT_ACTIVE )\r
878         {\r
879                 p_port = p_adapter->p_port;\r
880                 ipoib_port_ref( p_port, ref_refresh_mcast );\r
881         }\r
882         cl_obj_unlock( &p_adapter->obj );\r
883 \r
884         if( p_port )\r
885         {\r
886                 /* Purge old entries. */\r
887                 for( i = 0; i < p_adapter->mcast_array_size; i++ )\r
888                 {\r
889                         for( j = 0; j < num_macs; j++ )\r
890                         {\r
891                                 if( !cl_memcmp( &p_adapter->mcast_array[i], &p_mac_array[j],\r
892                                         sizeof(mac_addr_t) ) )\r
893                                 {\r
894                                         break;\r
895                                 }\r
896                         }\r
897                         if( j != num_macs )\r
898                                 continue;\r
899 \r
900                         ipoib_port_remove_endpt( p_port, p_adapter->mcast_array[i] );\r
901                 }\r
902 \r
903                 /* Add new entries */\r
904                 for( i = 0; i < num_macs; i++ )\r
905                 {\r
906                         for( j = 0; j < p_adapter->mcast_array_size; j++ )\r
907                         {\r
908                                 if( !cl_memcmp( &p_adapter->mcast_array[j], &p_mac_array[i],\r
909                                         sizeof(mac_addr_t) ) )\r
910                                 {\r
911                                         break;\r
912                                 }\r
913                         }\r
914 \r
915                         if( j != p_adapter->mcast_array_size )\r
916                                 continue;\r
917                         if ( ( p_mac_array[i].addr[0] == 1 && p_mac_array[i].addr[1] == 0 && p_mac_array[i].addr[2] == 0x5e &&\r
918                                    p_mac_array[i].addr[3] == 0 && p_mac_array[i].addr[4] == 0 && p_mac_array[i].addr[5] == 1 ) ||\r
919                                   !( p_mac_array[i].addr[0] == 1 && p_mac_array[i].addr[1] == 0 && p_mac_array[i].addr[2] == 0x5e )\r
920                                 )\r
921                         {\r
922                                 \r
923                                 ipoib_port_join_mcast( p_port, p_mac_array[i], IB_MC_REC_STATE_FULL_MEMBER );\r
924                         }\r
925                 }\r
926         }\r
927 \r
928         /* Copy the MAC array. */\r
929         NdisMoveMemory( p_adapter->mcast_array, p_mac_array,\r
930                 num_macs * sizeof(mac_addr_t) );\r
931         p_adapter->mcast_array_size = num_macs;\r
932 \r
933         if( p_port )\r
934                 ipoib_port_deref( p_port, ref_refresh_mcast );\r
935 \r
936         IPOIB_EXIT( IPOIB_DBG_MCAST );\r
937 }\r
938 \r
939 \r
940 ib_api_status_t\r
941 ipoib_reset_adapter(\r
942         IN                              ipoib_adapter_t* const          p_adapter )\r
943 {\r
944         ib_api_status_t         status;\r
945         ib_pnp_handle_t         h_pnp;\r
946 \r
947         IPOIB_ENTER( IPOIB_DBG_INIT );\r
948 \r
949         if( p_adapter->reset )\r
950                 return IB_INVALID_STATE;\r
951 \r
952         p_adapter->hung = FALSE;\r
953         p_adapter->reset = TRUE;\r
954 \r
955         if( p_adapter->h_pnp )\r
956         {\r
957                 h_pnp = p_adapter->h_pnp;\r
958                 p_adapter->h_pnp  = NULL;\r
959                 status = p_adapter->p_ifc->dereg_pnp( h_pnp, __ipoib_pnp_dereg );\r
960                 if( status == IB_SUCCESS )\r
961                         status = IB_NOT_DONE;\r
962         }\r
963         else\r
964         {\r
965                 status = __ipoib_pnp_reg( p_adapter, IB_PNP_FLAG_REG_COMPLETE );\r
966                 if( status == IB_SUCCESS )\r
967                         p_adapter->hung = FALSE;\r
968         }\r
969         if (status == IB_NOT_DONE) {\r
970                 p_adapter->reset = TRUE;\r
971         }\r
972         else {\r
973                 p_adapter->reset = FALSE;\r
974         }\r
975         IPOIB_EXIT( IPOIB_DBG_INIT );\r
976         return status;\r
977 }\r
978 \r
979 \r
980 static void\r
981 __ipoib_pnp_dereg(\r
982         IN                              void*                                           context )\r
983 {\r
984         ipoib_adapter_t*        p_adapter;\r
985 \r
986         IPOIB_ENTER( IPOIB_DBG_INIT );\r
987 \r
988         p_adapter = PARENT_STRUCT( context, ipoib_adapter_t, obj );\r
989 \r
990         cl_thread_init(&p_adapter->destroy_thread, __ipoib_adapter_reset, (void*)p_adapter, "destroy_thread");\r
991         \r
992         IPOIB_ENTER( IPOIB_DBG_INIT );\r
993 \r
994 }\r
995 \r
996 static void\r
997 __ipoib_adapter_reset(\r
998         IN                              void*   context)\r
999 {\r
1000 \r
1001         ipoib_adapter_t *p_adapter;\r
1002         ipoib_port_t            *p_port;\r
1003         ib_api_status_t         status;\r
1004         ib_pnp_event_t          state;\r
1005         \r
1006         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1007         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1008                                 ("Got RESET\n") );\r
1009 \r
1010         p_adapter = (ipoib_adapter_t*)context;\r
1011         \r
1012         /* Synchronize with destruction */\r
1013         KeWaitForMutexObject(\r
1014                 &p_adapter->mutex, Executive, KernelMode, FALSE, NULL );\r
1015 \r
1016         cl_obj_lock( &p_adapter->obj );\r
1017 \r
1018         CL_ASSERT( !p_adapter->h_pnp );\r
1019 \r
1020         if( p_adapter->state != IB_PNP_PORT_REMOVE )\r
1021                 p_adapter->state = IB_PNP_PORT_ADD;\r
1022 \r
1023         state = p_adapter->state;\r
1024 \r
1025         /* Destroy the current port instance if it still exists. */\r
1026         if ( p_adapter->p_stat )\r
1027                 p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
1028         p_port = p_adapter->p_port;\r
1029         p_adapter->p_port = NULL;\r
1030         cl_obj_unlock( &p_adapter->obj );\r
1031 \r
1032         if( p_port )\r
1033                 ipoib_port_destroy( p_port );\r
1034         ASSERT(p_adapter->reset == TRUE);\r
1035         if( state != IB_PNP_PORT_REMOVE )\r
1036         {\r
1037                 status = __ipoib_pnp_reg( p_adapter, IB_PNP_FLAG_REG_COMPLETE );\r
1038                 if( status != IB_SUCCESS )\r
1039                 {\r
1040                         p_adapter->reset = FALSE;\r
1041                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1042                                 ("__ipoib_pnp_reg returned %s\n",\r
1043                                 p_adapter->p_ifc->get_err_str( status )) );\r
1044                         NdisMResetComplete( \r
1045                                 p_adapter->h_adapter, NDIS_STATUS_HARD_ERRORS, TRUE );\r
1046                 }\r
1047         }\r
1048         else\r
1049         {\r
1050                 p_adapter->reset = FALSE;\r
1051                 NdisMResetComplete(\r
1052                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1053                 status = IB_SUCCESS;\r
1054         }\r
1055 \r
1056         /* Dereference the adapter since the previous registration is now gone. */\r
1057         cl_obj_deref( &p_adapter->obj );\r
1058 \r
1059         KeReleaseMutex( &p_adapter->mutex, FALSE );\r
1060 \r
1061         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1062 }\r
1063 \r
1064 \r
1065 void\r
1066 ipoib_set_rate(\r
1067         IN                              ipoib_adapter_t* const          p_adapter,\r
1068         IN              const   uint8_t                                         link_width, \r
1069         IN              const   uint8_t                                         link_speed )\r
1070 {\r
1071         uint32_t        rate;\r
1072 \r
1073         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1074 \r
1075         /* Set the link speed based on the IB link speed (1x vs 4x, etc). */\r
1076         switch( link_speed )\r
1077         {\r
1078         case IB_LINK_SPEED_ACTIVE_2_5:\r
1079                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1080                         ("Link speed is 2.5Gs\n") );\r
1081                 rate = IB_LINK_SPEED_ACTIVE_2_5;\r
1082                 break;\r
1083 \r
1084         case IB_LINK_SPEED_ACTIVE_5:\r
1085                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1086                         ("Link speed is 5G\n") );\r
1087                 rate = IB_LINK_SPEED_ACTIVE_5;\r
1088                 break;\r
1089 \r
1090         case IB_LINK_SPEED_ACTIVE_10:\r
1091                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1092                         ("Link speed is 10G\n") );\r
1093                 rate = IB_LINK_SPEED_ACTIVE_10;\r
1094                 break;\r
1095 \r
1096         default:\r
1097                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1098                         ("Invalid link speed %d.\n", link_speed) );\r
1099                 rate = 0;\r
1100         }\r
1101 \r
1102         switch( link_width )\r
1103         {\r
1104         case IB_LINK_WIDTH_ACTIVE_1X:\r
1105                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1106                         ("Link width is 1X\n") );\r
1107                 rate *= ONE_X_IN_100BPS;\r
1108                 break;\r
1109 \r
1110         case IB_LINK_WIDTH_ACTIVE_4X:\r
1111                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1112                         ("Link width is 4X\n") );\r
1113                 rate *= FOUR_X_IN_100BPS;\r
1114                 break;\r
1115 \r
1116         case IB_LINK_WIDTH_ACTIVE_12X:\r
1117                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1118                         ("Link width is 12X\n") );\r
1119                 rate *= TWELVE_X_IN_100BPS;\r
1120                 break;\r
1121 \r
1122         default:\r
1123                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1124                         ("Invalid link rate (%d).\n", link_width) );\r
1125                 rate = 0;\r
1126         }\r
1127 \r
1128         p_adapter->port_rate = rate;\r
1129         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1130 }\r
1131 \r
1132 \r
1133 ib_api_status_t\r
1134 ipoib_set_active(\r
1135         IN                              ipoib_adapter_t* const          p_adapter )\r
1136 {\r
1137         ib_pnp_event_t  old_state;\r
1138         uint8_t                 i;\r
1139         ib_api_status_t status = IB_SUCCESS;\r
1140         NDIS_LINK_STATE         link_state;\r
1141         NDIS_STATUS_INDICATION  status_indication;\r
1142         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1143 \r
1144         NdisZeroMemory(&link_state, sizeof(NDIS_LINK_STATE));\r
1145         cl_obj_lock( &p_adapter->obj );\r
1146         old_state = p_adapter->state;\r
1147 \r
1148         /* Change the state to indicate that we are now connected and live. */\r
1149         if( old_state == IB_PNP_PORT_INIT )\r
1150                 p_adapter->state = IB_PNP_PORT_ACTIVE;\r
1151 \r
1152         cl_obj_unlock( &p_adapter->obj );\r
1153 \r
1154         /*\r
1155          * If we had a pending OID request for OID_GEN_LINK_SPEED,\r
1156          * complete it now.\r
1157          */\r
1158         switch( old_state )\r
1159         {\r
1160         case IB_PNP_PORT_ADD:\r
1161                 ipoib_reg_addrs( p_adapter );\r
1162                 /* Fall through. */\r
1163 \r
1164         case IB_PNP_PORT_REMOVE:\r
1165                 ipoib_resume_oids( p_adapter );\r
1166                 break;\r
1167 \r
1168         default:\r
1169                 if (p_adapter->guids.port_guid.pkey != IB_DEFAULT_PKEY)\r
1170                 {\r
1171                         status = ipoib_query_pkey_index(p_adapter);\r
1172                         if( IB_SUCCESS != status)\r
1173                         {\r
1174                                 break;\r
1175                         }\r
1176                 }\r
1177                 /* Join all programmed multicast groups. */\r
1178                 for( i = 0; i < p_adapter->mcast_array_size; i++ )\r
1179                 {\r
1180                         IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_MCAST,("Calling join mcast from ipoib_set_active\n"));\r
1181                         ipoib_port_join_mcast(\r
1182                                 p_adapter->p_port, p_adapter->mcast_array[i] ,IB_MC_REC_STATE_FULL_MEMBER);\r
1183                 }\r
1184 \r
1185                 /* Register all existing addresses. */\r
1186                 ipoib_reg_addrs( p_adapter );\r
1187 \r
1188                 ipoib_resume_oids( p_adapter );\r
1189 \r
1190                 /*\r
1191                  * Now that we're in the broadcast group, notify that\r
1192                  * we have a link.\r
1193                  */\r
1194                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("Link UP!\n") );\r
1195                 NdisWriteErrorLogEntry( p_adapter->h_adapter,\r
1196                         EVENT_IPOIB_PORT_UP + (p_adapter->port_rate/ONE_X_IN_100BPS),\r
1197                         1, p_adapter->port_rate );\r
1198 \r
1199                 if( !p_adapter->reset )\r
1200                 {\r
1201                         link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
1202                         link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1203                         link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
1204                         link_state.MediaConnectState = MediaConnectStateConnected;\r
1205                         link_state.MediaDuplexState = MediaDuplexStateFull;\r
1206                         link_state.XmitLinkSpeed = \r
1207                         link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
1208                         link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
1209                         IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
1210                                                                    p_adapter->h_adapter,\r
1211                                                                    NDIS_STATUS_LINK_STATE,\r
1212                                                                    (PVOID)&link_state,\r
1213                                                                    sizeof(link_state));\r
1214                         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("***************Indicate connect!\n") );\r
1215                         NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);\r
1216                 }\r
1217         }\r
1218 \r
1219         if( p_adapter->reset )\r
1220         {\r
1221                 //ASSERT(FALSE);\r
1222                 p_adapter->reset = FALSE;\r
1223                 NdisMResetComplete(\r
1224                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1225         }\r
1226 \r
1227         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1228         return  status;\r
1229 }\r
1230 \r
1231 /*\r
1232  * If something goes wrong after the port goes active, e.g.\r
1233  *      - PortInfo query failure\r
1234  *      - MC Join timeout\r
1235  *      - etc\r
1236  * Mark the port state as down, resume any pended OIDS, etc.\r
1237  */\r
1238 void\r
1239 ipoib_set_inactive(\r
1240         IN                              ipoib_adapter_t* const          p_adapter )\r
1241 {\r
1242         ib_pnp_event_t  old_state;\r
1243         NDIS_LINK_STATE         link_state;\r
1244         NDIS_STATUS_INDICATION  status_indication;\r
1245 \r
1246         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1247 \r
1248         NdisZeroMemory(&link_state, sizeof(NDIS_LINK_STATE));\r
1249         cl_obj_lock( &p_adapter->obj );\r
1250         old_state = p_adapter->state;\r
1251         if( old_state != IB_PNP_PORT_REMOVE )\r
1252                 p_adapter->state = IB_PNP_PORT_DOWN;\r
1253         cl_obj_unlock( &p_adapter->obj );\r
1254 \r
1255         /*\r
1256          * If we had a pending OID request for OID_GEN_LINK_SPEED,\r
1257          * complete it now.\r
1258          */\r
1259         if( old_state == IB_PNP_PORT_INIT )\r
1260         {\r
1261                 link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
1262                 link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1263                 link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
1264                 link_state.MediaConnectState = MediaConnectStateDisconnected;\r
1265                 link_state.MediaDuplexState = MediaDuplexStateFull;\r
1266                 link_state.XmitLinkSpeed = \r
1267                 link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
1268                 link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
1269 \r
1270                 IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
1271                                                            p_adapter->h_adapter,\r
1272                                                            NDIS_STATUS_LINK_STATE,\r
1273                                                            (PVOID)&link_state,\r
1274                                                            sizeof(link_state));\r
1275                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("Indicate Disconnect!\n") );\r
1276                 NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);\r
1277 \r
1278                 ipoib_resume_oids( p_adapter );\r
1279         }\r
1280 \r
1281         if( p_adapter->reset )\r
1282         {\r
1283                 p_adapter->reset = FALSE;\r
1284                 NdisMResetComplete(\r
1285                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1286         }\r
1287 \r
1288         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1289 }\r
1290 \r
1291 NDIS_STATUS\r
1292 ipoib_get_gen_stat(\r
1293         IN                              ipoib_adapter_t* const          p_adapter,\r
1294         OUT             pending_oid_t* const            p_oid_info)\r
1295 {\r
1296         PNDIS_STATISTICS_INFO StatisticsInfo;\r
1297         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1298 \r
1299         if (p_oid_info->buf_len < sizeof(StatisticsInfo))\r
1300         {\r
1301                 *p_oid_info->p_bytes_needed = sizeof(NDIS_STATISTICS_INFO);\r
1302                 return NDIS_STATUS_INVALID_LENGTH;\r
1303         }\r
1304 \r
1305         StatisticsInfo = (PNDIS_STATISTICS_INFO)p_oid_info->p_buf;\r
1306     StatisticsInfo->Header.Revision = NDIS_OBJECT_REVISION_1;\r
1307     StatisticsInfo->Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1308     StatisticsInfo->Header.Size = sizeof(NDIS_STATISTICS_INFO);\r
1309     /*StatisticsInfo->SupportedStatistics = NDIS_STATISTICS_FLAGS_VALID_RCV_DISCARDS          |\r
1310                                           NDIS_STATISTICS_FLAGS_VALID_RCV_ERROR             |\r
1311                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_RCV   |\r
1312                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_RCV  |\r
1313                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_RCV  |\r
1314                                           NDIS_STATISTICS_FLAGS_VALID_BYTES_RCV             | \r
1315                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_RCV   |\r
1316                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_RCV       |\r
1317                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_RCV        |\r
1318 \r
1319                                                                                   NDIS_STATISTICS_FLAGS_VALID_XMIT_DISCARDS                     |\r
1320                                                                                   NDIS_STATISTICS_FLAGS_VALID_XMIT_ERROR            |\r
1321                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_XMIT  | \r
1322                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_XMIT |\r
1323                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_XMIT |\r
1324                                                                                   NDIS_STATISTICS_FLAGS_VALID_BYTES_XMIT                        |\r
1325                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_XMIT       |\r
1326                                                                                   NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_XMIT      |\r
1327                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_XMIT; */\r
1328                                                                                   \r
1329                                                                                   \r
1330         StatisticsInfo->SupportedStatistics =  NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_RCV |\r
1331                                                                                         //The data in the ifHCInUcastPkts member is valid. \r
1332                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_RCV |\r
1333                                                                                         //The data in the ifHCInMulticastPkts member is valid. \r
1334                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_RCV |\r
1335                                                                                         //The data in the ifHCInBroadcastPkts member is valid. \r
1336                                                                                         NDIS_STATISTICS_FLAGS_VALID_BYTES_RCV |\r
1337                                                                                         //The data in the ifHCInOctets member is valid. \r
1338                                                                                         NDIS_STATISTICS_FLAGS_VALID_RCV_DISCARDS |\r
1339                                                                                         //The data in the ifInDiscards member is valid. \r
1340                                                                                         NDIS_STATISTICS_FLAGS_VALID_RCV_ERROR |\r
1341                                                                                         //The data in the ifInErrors member is valid. \r
1342                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_XMIT |\r
1343                                                                                         //The data in the ifHCOutUcastPkts member is valid. \r
1344                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_XMIT |\r
1345                                                                                         //The data in the ifHCOutMulticastPkts member is valid. \r
1346                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_XMIT |\r
1347                                                                                         //The data in the ifHCOutBroadcastPkts member is valid. \r
1348                                                                                         NDIS_STATISTICS_FLAGS_VALID_BYTES_XMIT |\r
1349                                                                                         //The data in the ifHCOutOctets member is valid. \r
1350                                                                                         NDIS_STATISTICS_FLAGS_VALID_XMIT_ERROR |\r
1351                                                                                         //The data in the ifOutErrors member is valid. \r
1352                                                                                         NDIS_STATISTICS_FLAGS_VALID_XMIT_DISCARDS |\r
1353                                                                                         //The data in the ifOutDiscards member is valid. \r
1354                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_RCV |\r
1355                                                                                         //The data in the ifHCInUcastOctets member is valid. \r
1356                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_RCV |\r
1357                                                                                         //The data in the ifHCInMulticastOctets member is valid. \r
1358                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_RCV |\r
1359                                                                                         //The data in the ifHCInBroadcastOctets member is valid. \r
1360                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_XMIT |\r
1361                                                                                         //The data in the ifHCOutUcastOctets member is valid. \r
1362                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_XMIT |\r
1363                                                                                         //The data in the ifHCOutMulticastOctets member is valid. \r
1364                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_XMIT ;\r
1365                                                                                         //The data in the ifHCOutBroadcastOctets member is valid\r
1366 \r
1367     cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1368     StatisticsInfo->ifInDiscards = p_adapter->recv_stats.comp.dropped +\r
1369                                    p_adapter->recv_stats.comp.error;            \r
1370     StatisticsInfo->ifInErrors = p_adapter->recv_stats.comp.error;\r
1371     StatisticsInfo->ifHCInOctets = p_adapter->recv_stats.ucast.bytes + \r
1372                                                                    p_adapter->recv_stats.bcast.bytes + \r
1373                                                                    p_adapter->recv_stats.mcast.bytes;       \r
1374     StatisticsInfo->ifHCInUcastPkts       = p_adapter->recv_stats.ucast.frames;\r
1375     StatisticsInfo->ifHCInMulticastPkts   = p_adapter->recv_stats.mcast.frames;  \r
1376     StatisticsInfo->ifHCInBroadcastPkts   = p_adapter->recv_stats.bcast.frames;  \r
1377         StatisticsInfo->ifHCInMulticastOctets = p_adapter->recv_stats.mcast.bytes;\r
1378         StatisticsInfo->ifHCInBroadcastOctets = p_adapter->recv_stats.bcast.bytes;\r
1379         StatisticsInfo->ifHCInUcastOctets     = p_adapter->recv_stats.ucast.bytes;\r
1380         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1381 \r
1382     cl_spinlock_acquire( &p_adapter->send_stat_lock ); \r
1383     StatisticsInfo->ifHCOutOctets = p_adapter->send_stats.ucast.bytes + \r
1384                                                                         p_adapter->send_stats.mcast.bytes + \r
1385                                                                         p_adapter->send_stats.bcast.bytes;        \r
1386     StatisticsInfo->ifHCOutUcastPkts       = p_adapter->send_stats.ucast.frames;     \r
1387     StatisticsInfo->ifHCOutMulticastPkts   = p_adapter->send_stats.mcast.frames; \r
1388     StatisticsInfo->ifHCOutBroadcastPkts   = p_adapter->send_stats.bcast.frames; \r
1389     StatisticsInfo->ifOutErrors            = p_adapter->send_stats.comp.error;                                      \r
1390     StatisticsInfo->ifOutDiscards          = p_adapter->send_stats.comp.dropped;\r
1391         StatisticsInfo->ifHCOutUcastOctets     = p_adapter->send_stats.ucast.bytes;\r
1392         StatisticsInfo->ifHCOutMulticastOctets = p_adapter->send_stats.mcast.bytes;\r
1393         StatisticsInfo->ifHCOutBroadcastOctets = p_adapter->send_stats.bcast.bytes;\r
1394     cl_spinlock_release( &p_adapter->send_stat_lock );\r
1395 \r
1396         *p_oid_info->p_bytes_used = sizeof(NDIS_STATISTICS_INFO);\r
1397 \r
1398         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1399         return NDIS_STATUS_SUCCESS;\r
1400 }\r
1401 \r
1402 NDIS_STATUS\r
1403 ipoib_get_recv_stat(\r
1404         IN                              ipoib_adapter_t* const          p_adapter,\r
1405         IN              const   ip_stat_sel_t                           stat_sel,\r
1406         IN                              pending_oid_t* const            p_oid_info )\r
1407 {\r
1408         uint64_t        stat;\r
1409 \r
1410         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1411 \r
1412         CL_ASSERT( p_adapter );\r
1413 \r
1414         cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1415         switch( stat_sel )\r
1416         {\r
1417         case IP_STAT_SUCCESS:\r
1418                 stat = p_adapter->recv_stats.comp.success;\r
1419                 break;\r
1420 \r
1421         case IP_STAT_ERROR:\r
1422                 stat = p_adapter->recv_stats.comp.error;\r
1423                 break;\r
1424 \r
1425         case IP_STAT_DROPPED:\r
1426                 stat = p_adapter->recv_stats.comp.dropped;\r
1427                 break;\r
1428 \r
1429         case IP_STAT_UCAST_BYTES:\r
1430                 stat = p_adapter->recv_stats.ucast.bytes;\r
1431                 break;\r
1432 \r
1433         case IP_STAT_UCAST_FRAMES:\r
1434                 stat = p_adapter->recv_stats.ucast.frames;\r
1435                 break;\r
1436 \r
1437         case IP_STAT_BCAST_BYTES:\r
1438                 stat = p_adapter->recv_stats.bcast.bytes;\r
1439                 break;\r
1440 \r
1441         case IP_STAT_BCAST_FRAMES:\r
1442                 stat = p_adapter->recv_stats.bcast.frames;\r
1443                 break;\r
1444 \r
1445         case IP_STAT_MCAST_BYTES:\r
1446                 stat = p_adapter->recv_stats.mcast.bytes;\r
1447                 break;\r
1448 \r
1449         case IP_STAT_MCAST_FRAMES:\r
1450                 stat = p_adapter->recv_stats.mcast.frames;\r
1451                 break;\r
1452 \r
1453         default:\r
1454                 stat = 0;\r
1455         }\r
1456         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1457 \r
1458         *p_oid_info->p_bytes_needed = sizeof(uint64_t);\r
1459 \r
1460         if( p_oid_info->buf_len >= sizeof(uint64_t) )\r
1461         {\r
1462                 *((uint64_t*)p_oid_info->p_buf) = stat;\r
1463                 *p_oid_info->p_bytes_used = sizeof(uint64_t);\r
1464         }\r
1465         else if( p_oid_info->buf_len >= sizeof(uint32_t) )\r
1466         {\r
1467                 *((uint32_t*)p_oid_info->p_buf) = (uint32_t)stat;\r
1468                 *p_oid_info->p_bytes_used = sizeof(uint32_t);\r
1469         }\r
1470         else\r
1471         {\r
1472                 *p_oid_info->p_bytes_used = 0;\r
1473                 IPOIB_EXIT( IPOIB_DBG_STAT );\r
1474                 return NDIS_STATUS_INVALID_LENGTH;\r
1475         }\r
1476 \r
1477         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1478         return NDIS_STATUS_SUCCESS;\r
1479 }\r
1480 \r
1481 \r
1482 void\r
1483 ipoib_inc_recv_stat(\r
1484         IN                              ipoib_adapter_t* const          p_adapter,\r
1485         IN              const   ip_stat_sel_t                           stat_sel,\r
1486         IN              const   size_t                                          bytes OPTIONAL,\r
1487         IN              const   size_t                                          packets OPTIONAL )\r
1488 {\r
1489         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1490 \r
1491         cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1492         switch( stat_sel )\r
1493         {\r
1494         case IP_STAT_ERROR:\r
1495                 p_adapter->recv_stats.comp.error++;\r
1496                 break;\r
1497 \r
1498         case IP_STAT_DROPPED:\r
1499                 p_adapter->recv_stats.comp.dropped++;\r
1500                 break;\r
1501 \r
1502         case IP_STAT_UCAST_BYTES:\r
1503         case IP_STAT_UCAST_FRAMES:\r
1504                 p_adapter->recv_stats.comp.success++;\r
1505                 p_adapter->recv_stats.ucast.frames += packets;\r
1506                 p_adapter->recv_stats.ucast.bytes += bytes;\r
1507                 break;\r
1508 \r
1509         case IP_STAT_BCAST_BYTES:\r
1510         case IP_STAT_BCAST_FRAMES:\r
1511                 p_adapter->recv_stats.comp.success++;\r
1512                 p_adapter->recv_stats.bcast.frames += packets;\r
1513                 p_adapter->recv_stats.bcast.bytes += bytes;\r
1514                 break;\r
1515 \r
1516         case IP_STAT_MCAST_BYTES:\r
1517         case IP_STAT_MCAST_FRAMES:\r
1518                 p_adapter->recv_stats.comp.success++;\r
1519                 p_adapter->recv_stats.mcast.frames += packets;\r
1520                 p_adapter->recv_stats.mcast.bytes += bytes;\r
1521                 break;\r
1522 \r
1523         default:\r
1524                 break;\r
1525         }\r
1526         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1527 \r
1528         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1529 }\r
1530 \r
1531 NDIS_STATUS\r
1532 ipoib_get_send_stat(\r
1533         IN                              ipoib_adapter_t* const          p_adapter,\r
1534         IN              const   ip_stat_sel_t                           stat_sel,\r
1535         IN                              pending_oid_t* const            p_oid_info )\r
1536 {\r
1537         uint64_t        stat;\r
1538 \r
1539         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1540 \r
1541         CL_ASSERT( p_adapter );\r
1542 \r
1543         cl_spinlock_acquire( &p_adapter->send_stat_lock );\r
1544         switch( stat_sel )\r
1545         {\r
1546         case IP_STAT_SUCCESS:\r
1547                 stat = p_adapter->send_stats.comp.success;\r
1548                 break;\r
1549 \r
1550         case IP_STAT_ERROR:\r
1551                 stat = p_adapter->send_stats.comp.error;\r
1552                 break;\r
1553 \r
1554         case IP_STAT_DROPPED:\r
1555                 stat = p_adapter->send_stats.comp.dropped;\r
1556                 break;\r
1557 \r
1558         case IP_STAT_UCAST_BYTES:\r
1559                 stat = p_adapter->send_stats.ucast.bytes;\r
1560                 break;\r
1561 \r
1562         case IP_STAT_UCAST_FRAMES:\r
1563                 stat = p_adapter->send_stats.ucast.frames;\r
1564                 break;\r
1565 \r
1566         case IP_STAT_BCAST_BYTES:\r
1567                 stat = p_adapter->send_stats.bcast.bytes;\r
1568                 break;\r
1569 \r
1570         case IP_STAT_BCAST_FRAMES:\r
1571                 stat = p_adapter->send_stats.bcast.frames;\r
1572                 break;\r
1573 \r
1574         case IP_STAT_MCAST_BYTES:\r
1575                 stat = p_adapter->send_stats.mcast.bytes;\r
1576                 break;\r
1577 \r
1578         case IP_STAT_MCAST_FRAMES:\r
1579                 stat = p_adapter->send_stats.mcast.frames;\r
1580                 break;\r
1581 \r
1582         default:\r
1583                 stat = 0;\r
1584         }\r
1585         cl_spinlock_release( &p_adapter->send_stat_lock );\r
1586 \r
1587         *p_oid_info->p_bytes_needed = sizeof(uint64_t);\r
1588 \r
1589         if( p_oid_info->buf_len >= sizeof(uint64_t) )\r
1590         {\r
1591                 *((uint64_t*)p_oid_info->p_buf) = stat;\r
1592                 *p_oid_info->p_bytes_used = sizeof(uint64_t);\r
1593         }\r
1594         else if( p_oid_info->buf_len >= sizeof(uint32_t) )\r
1595         {\r
1596                 *((uint32_t*)p_oid_info->p_buf) = (uint32_t)stat;\r
1597                 *p_oid_info->p_bytes_used = sizeof(uint32_t);\r
1598         }\r
1599         else\r
1600         {\r
1601                 *p_oid_info->p_bytes_used = 0;\r
1602                 IPOIB_EXIT( IPOIB_DBG_STAT );\r
1603                 return NDIS_STATUS_INVALID_LENGTH;\r
1604         }\r
1605 \r
1606         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1607         return NDIS_STATUS_SUCCESS;\r
1608 }\r
1609 \r
1610 \r
1611 void\r
1612 ipoib_inc_send_stat(\r
1613         IN                              ipoib_adapter_t* const          p_adapter,\r
1614         IN              const   ip_stat_sel_t                           stat_sel,\r
1615         IN              const   size_t                                          bytes OPTIONAL )\r
1616 {\r
1617         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1618 \r
1619         cl_spinlock_acquire( &p_adapter->send_stat_lock );\r
1620         switch( stat_sel )\r
1621         {\r
1622         case IP_STAT_ERROR:\r
1623                 p_adapter->send_stats.comp.error++;\r
1624                 break;\r
1625 \r
1626         case IP_STAT_DROPPED:\r
1627                 p_adapter->send_stats.comp.dropped++;\r
1628                 break;\r
1629 \r
1630         case IP_STAT_UCAST_BYTES:\r
1631         case IP_STAT_UCAST_FRAMES:\r
1632                 p_adapter->send_stats.comp.success++;\r
1633                 p_adapter->send_stats.ucast.frames++;\r
1634                 p_adapter->send_stats.ucast.bytes += bytes;\r
1635                 break;\r
1636 \r
1637         case IP_STAT_BCAST_BYTES:\r
1638         case IP_STAT_BCAST_FRAMES:\r
1639                 p_adapter->send_stats.comp.success++;\r
1640                 p_adapter->send_stats.bcast.frames++;\r
1641                 p_adapter->send_stats.bcast.bytes += bytes;\r
1642                 break;\r
1643 \r
1644         case IP_STAT_MCAST_BYTES:\r
1645         case IP_STAT_MCAST_FRAMES:\r
1646                 p_adapter->send_stats.comp.success++;\r
1647                 p_adapter->send_stats.mcast.frames++;\r
1648                 p_adapter->send_stats.mcast.bytes += bytes;\r
1649                 break;\r
1650 \r
1651         default:\r
1652                 break;\r
1653         }\r
1654         cl_spinlock_release( &p_adapter->send_stat_lock );\r
1655 \r
1656         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1657 }\r