[IPOIB] g_stat: added more fields: prev_port and halt thread.
[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( (status == NDIS_STATUS_SUCCESS) && (len == HW_ADDR_LEN) )\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 \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                 p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
689                 p_port = p_adapter->p_port;\r
690                 p_adapter->p_port = NULL;\r
691                 cl_obj_unlock( &p_adapter->obj );\r
692                 ipoib_port_destroy( p_port );\r
693                 p_pnp_rec->context = NULL;\r
694                 status = IB_SUCCESS;\r
695                 break;\r
696 \r
697         case IB_PNP_PORT_ACTIVE:\r
698                 /* Join multicast groups and put QP in RTS. */\r
699                 CL_ASSERT( p_pnp_rec->context );\r
700 \r
701                 cl_obj_lock( &p_adapter->obj );\r
702                 p_adapter->state = IB_PNP_PORT_INIT;\r
703                 cl_obj_unlock( &p_adapter->obj );\r
704                 ipoib_port_up( p_adapter->p_port, p_port_rec );\r
705 \r
706                 status = IB_SUCCESS;\r
707                 break;\r
708 \r
709         case IB_PNP_PORT_ARMED:\r
710                 status = IB_SUCCESS;\r
711                 break;\r
712 \r
713         case IB_PNP_PORT_INIT:\r
714                 /*\r
715                  * Init could happen if the SM brings the port down\r
716                  * without changing the physical link.\r
717                  */\r
718         case IB_PNP_PORT_DOWN:\r
719                 CL_ASSERT( p_pnp_rec->context );\r
720 \r
721                 cl_obj_lock( &p_adapter->obj );\r
722                 old_state = p_adapter->state;\r
723                 p_adapter->state = IB_PNP_PORT_DOWN;\r
724                 cl_obj_unlock( &p_adapter->obj );\r
725                 status = IB_SUCCESS;\r
726 \r
727                 if( !p_adapter->registering && old_state != IB_PNP_PORT_DOWN )\r
728                 {\r
729                         __ipoib_get_down( p_adapter );\r
730                 }\r
731                 break;\r
732 \r
733         case IB_PNP_REG_COMPLETE:\r
734                 if( p_adapter->registering )\r
735                 {\r
736                         p_adapter->registering = FALSE;\r
737                         cl_obj_lock( &p_adapter->obj );\r
738                         old_state = p_adapter->state;\r
739                         cl_obj_unlock( &p_adapter->obj );\r
740 \r
741                         if( old_state == IB_PNP_PORT_DOWN )\r
742                         {\r
743                                 /* If we were initializing, we might have pended some OIDs. */\r
744                                 ipoib_resume_oids( p_adapter );\r
745 \r
746                                 link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
747                                 link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
748                                 link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
749                                 link_state.MediaConnectState = MediaConnectStateDisconnected;\r
750                                 link_state.MediaDuplexState = MediaDuplexStateFull;\r
751                                 link_state.XmitLinkSpeed = \r
752                                 link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
753                                 link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
754 \r
755                                 IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
756                                                                            p_adapter->h_adapter,\r
757                                                                            NDIS_STATUS_LINK_STATE,\r
758                                                                            (PVOID)&link_state,\r
759                                                                            sizeof(link_state));\r
760                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_INIT, ("Indicate DISCONNECT\n") );\r
761                                 NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);                 \r
762                         }\r
763                 }\r
764 \r
765                 if( p_adapter->reset && p_adapter->state != IB_PNP_PORT_INIT )\r
766                 {\r
767                         p_adapter->reset = FALSE;\r
768                         NdisMResetComplete(\r
769                                 p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
770                 }\r
771                 status = IB_SUCCESS;\r
772                 break;\r
773 \r
774         default:\r
775                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
776                         ("IPOIB: Received unhandled PnP event 0x%x (%s)\n",\r
777                         p_pnp_rec->pnp_event, ib_get_pnp_event_str( p_pnp_rec->pnp_event )) );\r
778                 /* Fall through. */\r
779 \r
780                 status = IB_SUCCESS;\r
781 \r
782                 /* We ignore events below if the link is not active. */\r
783                 if( p_port_rec->p_port_attr->link_state != IB_LINK_ACTIVE )\r
784                         break;\r
785 \r
786                 case IB_PNP_PKEY_CHANGE:\r
787                         if(p_pnp_rec->pnp_event == IB_PNP_PKEY_CHANGE && \r
788                            p_adapter->guids.port_guid.pkey != IB_DEFAULT_PKEY)\r
789                         {\r
790                                 status = ipoib_query_pkey_index(p_adapter);\r
791                                 if(status != IB_SUCCESS)\r
792                                 {\r
793                                    cl_obj_lock( &p_adapter->obj );\r
794                                    p_adapter->state = IB_PNP_PORT_INIT;\r
795                                    cl_obj_unlock( &p_adapter->obj );\r
796                                 }\r
797                         }\r
798 \r
799                 case IB_PNP_SM_CHANGE:\r
800                 case IB_PNP_GID_CHANGE:\r
801                 case IB_PNP_LID_CHANGE:\r
802 \r
803                 cl_obj_lock( &p_adapter->obj );\r
804                 old_state = p_adapter->state;\r
805                 switch( old_state )\r
806                 {\r
807                 case IB_PNP_PORT_DOWN:\r
808                         p_adapter->state = IB_PNP_PORT_INIT;\r
809                         break;\r
810 \r
811                 default:\r
812                         p_adapter->state = IB_PNP_PORT_DOWN;\r
813                 }\r
814                 cl_obj_unlock( &p_adapter->obj );\r
815                 \r
816                 if( p_adapter->registering )\r
817                         break;\r
818 \r
819                 switch( old_state )\r
820                 {\r
821                 case IB_PNP_PORT_ACTIVE:\r
822                 case IB_PNP_PORT_INIT:\r
823                         link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
824                         link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
825                         link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
826                         link_state.MediaConnectState = MediaConnectStateDisconnected;\r
827                         link_state.MediaDuplexState = MediaDuplexStateFull;\r
828                         link_state.XmitLinkSpeed = \r
829                         link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
830                         link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
831 \r
832                         IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
833                                                                    p_adapter->h_adapter,\r
834                                                                    NDIS_STATUS_LINK_STATE,\r
835                                                                    (PVOID)&link_state,\r
836                                                                    sizeof(link_state));\r
837 \r
838                         NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);         \r
839 \r
840                         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
841                                 ("Link DOWN!\n") );\r
842                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_INIT,\r
843                         ("Indicate DISCONNECT\n") );\r
844 \r
845                         ipoib_port_down( p_adapter->p_port );\r
846                         /* Fall through. */\r
847 \r
848                 case IB_PNP_PORT_DOWN:\r
849                         cl_obj_lock( &p_adapter->obj );\r
850                         p_adapter->state = IB_PNP_PORT_INIT;\r
851                         cl_obj_unlock( &p_adapter->obj );\r
852                         ipoib_port_up( p_adapter->p_port, (ib_pnp_port_rec_t*)p_pnp_rec );\r
853                 }\r
854                 break;\r
855         }\r
856 \r
857         KeReleaseMutex( &p_adapter->mutex, FALSE );\r
858 \r
859         IPOIB_EXIT( IPOIB_DBG_PNP );\r
860         return status;\r
861 }\r
862 \r
863 \r
864 /* Joins/leaves mcast groups based on currently programmed mcast MACs. */\r
865 void\r
866 ipoib_refresh_mcast(\r
867         IN                              ipoib_adapter_t* const          p_adapter,\r
868         IN                              mac_addr_t* const                       p_mac_array,\r
869         IN              const   uint8_t                                         num_macs )\r
870 {\r
871         uint8_t                         i, j;\r
872         ipoib_port_t            *p_port = NULL;\r
873 \r
874         IPOIB_ENTER( IPOIB_DBG_MCAST );\r
875         cl_obj_lock( &p_adapter->obj );\r
876         if( p_adapter->state == IB_PNP_PORT_ACTIVE )\r
877         {\r
878                 p_port = p_adapter->p_port;\r
879                 ipoib_port_ref( p_port, ref_refresh_mcast );\r
880         }\r
881         cl_obj_unlock( &p_adapter->obj );\r
882 \r
883         if( p_port )\r
884         {\r
885                 /* Purge old entries. */\r
886                 for( i = 0; i < p_adapter->mcast_array_size; i++ )\r
887                 {\r
888                         for( j = 0; j < num_macs; j++ )\r
889                         {\r
890                                 if( !cl_memcmp( &p_adapter->mcast_array[i], &p_mac_array[j],\r
891                                         sizeof(mac_addr_t) ) )\r
892                                 {\r
893                                         break;\r
894                                 }\r
895                         }\r
896                         if( j != num_macs )\r
897                                 continue;\r
898 \r
899                         ipoib_port_remove_endpt( p_port, p_adapter->mcast_array[i] );\r
900                 }\r
901 \r
902                 /* Add new entries */\r
903                 for( i = 0; i < num_macs; i++ )\r
904                 {\r
905                         for( j = 0; j < p_adapter->mcast_array_size; j++ )\r
906                         {\r
907                                 if( !cl_memcmp( &p_adapter->mcast_array[j], &p_mac_array[i],\r
908                                         sizeof(mac_addr_t) ) )\r
909                                 {\r
910                                         break;\r
911                                 }\r
912                         }\r
913 \r
914                         if( j != p_adapter->mcast_array_size )\r
915                                 continue;\r
916                         if ( ( p_mac_array[i].addr[0] == 1 && p_mac_array[i].addr[1] == 0 && p_mac_array[i].addr[2] == 0x5e &&\r
917                                    p_mac_array[i].addr[3] == 0 && p_mac_array[i].addr[4] == 0 && p_mac_array[i].addr[5] == 1 ) ||\r
918                                   !( p_mac_array[i].addr[0] == 1 && p_mac_array[i].addr[1] == 0 && p_mac_array[i].addr[2] == 0x5e )\r
919                                 )\r
920                         {\r
921                                 \r
922                                 ipoib_port_join_mcast( p_port, p_mac_array[i], IB_MC_REC_STATE_FULL_MEMBER );\r
923                         }\r
924                 }\r
925         }\r
926 \r
927         /* Copy the MAC array. */\r
928         NdisMoveMemory( p_adapter->mcast_array, p_mac_array,\r
929                 num_macs * sizeof(mac_addr_t) );\r
930         p_adapter->mcast_array_size = num_macs;\r
931 \r
932         if( p_port )\r
933                 ipoib_port_deref( p_port, ref_refresh_mcast );\r
934 \r
935         IPOIB_EXIT( IPOIB_DBG_MCAST );\r
936 }\r
937 \r
938 \r
939 ib_api_status_t\r
940 ipoib_reset_adapter(\r
941         IN                              ipoib_adapter_t* const          p_adapter )\r
942 {\r
943         ib_api_status_t         status;\r
944         ib_pnp_handle_t         h_pnp;\r
945 \r
946         IPOIB_ENTER( IPOIB_DBG_INIT );\r
947 \r
948         if( p_adapter->reset )\r
949                 return IB_INVALID_STATE;\r
950 \r
951         p_adapter->hung = FALSE;\r
952         p_adapter->reset = TRUE;\r
953 \r
954         if( p_adapter->h_pnp )\r
955         {\r
956                 h_pnp = p_adapter->h_pnp;\r
957                 p_adapter->h_pnp  = NULL;\r
958                 status = p_adapter->p_ifc->dereg_pnp( h_pnp, __ipoib_pnp_dereg );\r
959                 if( status == IB_SUCCESS )\r
960                         status = IB_NOT_DONE;\r
961         }\r
962         else\r
963         {\r
964                 status = __ipoib_pnp_reg( p_adapter, IB_PNP_FLAG_REG_COMPLETE );\r
965                 if( status == IB_SUCCESS )\r
966                         p_adapter->hung = FALSE;\r
967         }\r
968         if (status == IB_NOT_DONE) {\r
969                 p_adapter->reset = TRUE;\r
970         }\r
971         else {\r
972                 p_adapter->reset = FALSE;\r
973         }\r
974         IPOIB_EXIT( IPOIB_DBG_INIT );\r
975         return status;\r
976 }\r
977 \r
978 \r
979 static void\r
980 __ipoib_pnp_dereg(\r
981         IN                              void*                                           context )\r
982 {\r
983         ipoib_adapter_t*        p_adapter;\r
984 \r
985         IPOIB_ENTER( IPOIB_DBG_INIT );\r
986 \r
987         p_adapter = PARENT_STRUCT( context, ipoib_adapter_t, obj );\r
988 \r
989         cl_thread_init(&p_adapter->destroy_thread, __ipoib_adapter_reset, (void*)p_adapter, "destroy_thread");\r
990         \r
991         IPOIB_ENTER( IPOIB_DBG_INIT );\r
992 \r
993 }\r
994 \r
995 static void\r
996 __ipoib_adapter_reset(\r
997         IN                              void*   context)\r
998 {\r
999 \r
1000         ipoib_adapter_t *p_adapter;\r
1001         ipoib_port_t            *p_port;\r
1002         ib_api_status_t         status;\r
1003         ib_pnp_event_t          state;\r
1004         \r
1005         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1006         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1007                                 ("Got RESET\n") );\r
1008 \r
1009         p_adapter = (ipoib_adapter_t*)context;\r
1010         \r
1011         /* Synchronize with destruction */\r
1012         KeWaitForMutexObject(\r
1013                 &p_adapter->mutex, Executive, KernelMode, FALSE, NULL );\r
1014 \r
1015         cl_obj_lock( &p_adapter->obj );\r
1016 \r
1017         CL_ASSERT( !p_adapter->h_pnp );\r
1018 \r
1019         if( p_adapter->state != IB_PNP_PORT_REMOVE )\r
1020                 p_adapter->state = IB_PNP_PORT_ADD;\r
1021 \r
1022         state = p_adapter->state;\r
1023 \r
1024         /* Destroy the current port instance if it still exists. */\r
1025         p_adapter->p_stat->p_prev_port = p_adapter->p_port;\r
1026         p_port = p_adapter->p_port;\r
1027         p_adapter->p_port = NULL;\r
1028         cl_obj_unlock( &p_adapter->obj );\r
1029 \r
1030         if( p_port )\r
1031                 ipoib_port_destroy( p_port );\r
1032         ASSERT(p_adapter->reset == TRUE);\r
1033         if( state != IB_PNP_PORT_REMOVE )\r
1034         {\r
1035                 status = __ipoib_pnp_reg( p_adapter, IB_PNP_FLAG_REG_COMPLETE );\r
1036                 if( status != IB_SUCCESS )\r
1037                 {\r
1038                         p_adapter->reset = FALSE;\r
1039                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1040                                 ("__ipoib_pnp_reg returned %s\n",\r
1041                                 p_adapter->p_ifc->get_err_str( status )) );\r
1042                         NdisMResetComplete( \r
1043                                 p_adapter->h_adapter, NDIS_STATUS_HARD_ERRORS, TRUE );\r
1044                 }\r
1045         }\r
1046         else\r
1047         {\r
1048                 p_adapter->reset = FALSE;\r
1049                 NdisMResetComplete(\r
1050                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1051                 status = IB_SUCCESS;\r
1052         }\r
1053 \r
1054         /* Dereference the adapter since the previous registration is now gone. */\r
1055         cl_obj_deref( &p_adapter->obj );\r
1056 \r
1057         KeReleaseMutex( &p_adapter->mutex, FALSE );\r
1058 \r
1059         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1060 }\r
1061 \r
1062 \r
1063 void\r
1064 ipoib_set_rate(\r
1065         IN                              ipoib_adapter_t* const          p_adapter,\r
1066         IN              const   uint8_t                                         link_width, \r
1067         IN              const   uint8_t                                         link_speed )\r
1068 {\r
1069         uint32_t        rate;\r
1070 \r
1071         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1072 \r
1073         /* Set the link speed based on the IB link speed (1x vs 4x, etc). */\r
1074         switch( link_speed )\r
1075         {\r
1076         case IB_LINK_SPEED_ACTIVE_2_5:\r
1077                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1078                         ("Link speed is 2.5Gs\n") );\r
1079                 rate = IB_LINK_SPEED_ACTIVE_2_5;\r
1080                 break;\r
1081 \r
1082         case IB_LINK_SPEED_ACTIVE_5:\r
1083                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1084                         ("Link speed is 5G\n") );\r
1085                 rate = IB_LINK_SPEED_ACTIVE_5;\r
1086                 break;\r
1087 \r
1088         case IB_LINK_SPEED_ACTIVE_10:\r
1089                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1090                         ("Link speed is 10G\n") );\r
1091                 rate = IB_LINK_SPEED_ACTIVE_10;\r
1092                 break;\r
1093 \r
1094         default:\r
1095                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1096                         ("Invalid link speed %d.\n", link_speed) );\r
1097                 rate = 0;\r
1098         }\r
1099 \r
1100         switch( link_width )\r
1101         {\r
1102         case IB_LINK_WIDTH_ACTIVE_1X:\r
1103                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1104                         ("Link width is 1X\n") );\r
1105                 rate *= ONE_X_IN_100BPS;\r
1106                 break;\r
1107 \r
1108         case IB_LINK_WIDTH_ACTIVE_4X:\r
1109                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1110                         ("Link width is 4X\n") );\r
1111                 rate *= FOUR_X_IN_100BPS;\r
1112                 break;\r
1113 \r
1114         case IB_LINK_WIDTH_ACTIVE_12X:\r
1115                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
1116                         ("Link width is 12X\n") );\r
1117                 rate *= TWELVE_X_IN_100BPS;\r
1118                 break;\r
1119 \r
1120         default:\r
1121                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1122                         ("Invalid link rate (%d).\n", link_width) );\r
1123                 rate = 0;\r
1124         }\r
1125 \r
1126         p_adapter->port_rate = rate;\r
1127         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1128 }\r
1129 \r
1130 \r
1131 ib_api_status_t\r
1132 ipoib_set_active(\r
1133         IN                              ipoib_adapter_t* const          p_adapter )\r
1134 {\r
1135         ib_pnp_event_t  old_state;\r
1136         uint8_t                 i;\r
1137         ib_api_status_t status = IB_SUCCESS;\r
1138         NDIS_LINK_STATE         link_state;\r
1139         NDIS_STATUS_INDICATION  status_indication;\r
1140         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1141 \r
1142         NdisZeroMemory(&link_state, sizeof(NDIS_LINK_STATE));\r
1143         cl_obj_lock( &p_adapter->obj );\r
1144         old_state = p_adapter->state;\r
1145 \r
1146         /* Change the state to indicate that we are now connected and live. */\r
1147         if( old_state == IB_PNP_PORT_INIT )\r
1148                 p_adapter->state = IB_PNP_PORT_ACTIVE;\r
1149 \r
1150         cl_obj_unlock( &p_adapter->obj );\r
1151 \r
1152         /*\r
1153          * If we had a pending OID request for OID_GEN_LINK_SPEED,\r
1154          * complete it now.\r
1155          */\r
1156         switch( old_state )\r
1157         {\r
1158         case IB_PNP_PORT_ADD:\r
1159                 ipoib_reg_addrs( p_adapter );\r
1160                 /* Fall through. */\r
1161 \r
1162         case IB_PNP_PORT_REMOVE:\r
1163                 ipoib_resume_oids( p_adapter );\r
1164                 break;\r
1165 \r
1166         default:\r
1167                 if (p_adapter->guids.port_guid.pkey != IB_DEFAULT_PKEY)\r
1168                 {\r
1169                         status = ipoib_query_pkey_index(p_adapter);\r
1170                         if( IB_SUCCESS != status)\r
1171                         {\r
1172                                 break;\r
1173                         }\r
1174                 }\r
1175                 /* Join all programmed multicast groups. */\r
1176                 for( i = 0; i < p_adapter->mcast_array_size; i++ )\r
1177                 {\r
1178                         IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_MCAST,("Calling join mcast from ipoib_set_active\n"));\r
1179                         ipoib_port_join_mcast(\r
1180                                 p_adapter->p_port, p_adapter->mcast_array[i] ,IB_MC_REC_STATE_FULL_MEMBER);\r
1181                 }\r
1182 \r
1183                 /* Register all existing addresses. */\r
1184                 ipoib_reg_addrs( p_adapter );\r
1185 \r
1186                 ipoib_resume_oids( p_adapter );\r
1187 \r
1188                 /*\r
1189                  * Now that we're in the broadcast group, notify that\r
1190                  * we have a link.\r
1191                  */\r
1192                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("Link UP!\n") );\r
1193                 NdisWriteErrorLogEntry( p_adapter->h_adapter,\r
1194                         EVENT_IPOIB_PORT_UP + (p_adapter->port_rate/ONE_X_IN_100BPS),\r
1195                         1, p_adapter->port_rate );\r
1196 \r
1197                 if( !p_adapter->reset )\r
1198                 {\r
1199                         link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
1200                         link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1201                         link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
1202                         link_state.MediaConnectState = MediaConnectStateConnected;\r
1203                         link_state.MediaDuplexState = MediaDuplexStateFull;\r
1204                         link_state.XmitLinkSpeed = \r
1205                         link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
1206                         link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
1207                         IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
1208                                                                    p_adapter->h_adapter,\r
1209                                                                    NDIS_STATUS_LINK_STATE,\r
1210                                                                    (PVOID)&link_state,\r
1211                                                                    sizeof(link_state));\r
1212                         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("***************Indicate connect!\n") );\r
1213                         NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);\r
1214                 }\r
1215         }\r
1216 \r
1217         if( p_adapter->reset )\r
1218         {\r
1219                 //ASSERT(FALSE);\r
1220                 p_adapter->reset = FALSE;\r
1221                 NdisMResetComplete(\r
1222                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1223         }\r
1224 \r
1225         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1226         return  status;\r
1227 }\r
1228 \r
1229 /*\r
1230  * If something goes wrong after the port goes active, e.g.\r
1231  *      - PortInfo query failure\r
1232  *      - MC Join timeout\r
1233  *      - etc\r
1234  * Mark the port state as down, resume any pended OIDS, etc.\r
1235  */\r
1236 void\r
1237 ipoib_set_inactive(\r
1238         IN                              ipoib_adapter_t* const          p_adapter )\r
1239 {\r
1240         ib_pnp_event_t  old_state;\r
1241         NDIS_LINK_STATE         link_state;\r
1242         NDIS_STATUS_INDICATION  status_indication;\r
1243 \r
1244         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1245 \r
1246         NdisZeroMemory(&link_state, sizeof(NDIS_LINK_STATE));\r
1247         cl_obj_lock( &p_adapter->obj );\r
1248         old_state = p_adapter->state;\r
1249         if( old_state != IB_PNP_PORT_REMOVE )\r
1250                 p_adapter->state = IB_PNP_PORT_DOWN;\r
1251         cl_obj_unlock( &p_adapter->obj );\r
1252 \r
1253         /*\r
1254          * If we had a pending OID request for OID_GEN_LINK_SPEED,\r
1255          * complete it now.\r
1256          */\r
1257         if( old_state == IB_PNP_PORT_INIT )\r
1258         {\r
1259                 link_state.Header.Revision = NDIS_LINK_STATE_REVISION_1;\r
1260                 link_state.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1261                 link_state.Header.Size = sizeof(NDIS_LINK_STATE);\r
1262                 link_state.MediaConnectState = MediaConnectStateDisconnected;\r
1263                 link_state.MediaDuplexState = MediaDuplexStateFull;\r
1264                 link_state.XmitLinkSpeed = \r
1265                 link_state.RcvLinkSpeed = IPOIB_MEDIA_MAX_SPEED;\r
1266                 link_state.PauseFunctions = NdisPauseFunctionsSendAndReceive;\r
1267 \r
1268                 IPOIB_INIT_NDIS_STATUS_INDICATION(&status_indication,\r
1269                                                            p_adapter->h_adapter,\r
1270                                                            NDIS_STATUS_LINK_STATE,\r
1271                                                            (PVOID)&link_state,\r
1272                                                            sizeof(link_state));\r
1273                 IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT, ("Indicate Disconnect!\n") );\r
1274                 NdisMIndicateStatusEx(p_adapter->h_adapter,&status_indication);\r
1275 \r
1276                 ipoib_resume_oids( p_adapter );\r
1277         }\r
1278 \r
1279         if( p_adapter->reset )\r
1280         {\r
1281                 p_adapter->reset = FALSE;\r
1282                 NdisMResetComplete(\r
1283                         p_adapter->h_adapter, NDIS_STATUS_SUCCESS, TRUE );\r
1284         }\r
1285 \r
1286         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1287 }\r
1288 \r
1289 NDIS_STATUS\r
1290 ipoib_get_gen_stat(\r
1291         IN                              ipoib_adapter_t* const          p_adapter,\r
1292         OUT             pending_oid_t* const            p_oid_info)\r
1293 {\r
1294         PNDIS_STATISTICS_INFO StatisticsInfo;\r
1295         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1296 \r
1297         if (p_oid_info->buf_len < sizeof(StatisticsInfo))\r
1298         {\r
1299                 *p_oid_info->p_bytes_needed = sizeof(NDIS_STATISTICS_INFO);\r
1300                 return NDIS_STATUS_INVALID_LENGTH;\r
1301         }\r
1302 \r
1303         StatisticsInfo = (PNDIS_STATISTICS_INFO)p_oid_info->p_buf;\r
1304     StatisticsInfo->Header.Revision = NDIS_OBJECT_REVISION_1;\r
1305     StatisticsInfo->Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1306     StatisticsInfo->Header.Size = sizeof(NDIS_STATISTICS_INFO);\r
1307     /*StatisticsInfo->SupportedStatistics = NDIS_STATISTICS_FLAGS_VALID_RCV_DISCARDS          |\r
1308                                           NDIS_STATISTICS_FLAGS_VALID_RCV_ERROR             |\r
1309                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_RCV   |\r
1310                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_RCV  |\r
1311                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_RCV  |\r
1312                                           NDIS_STATISTICS_FLAGS_VALID_BYTES_RCV             | \r
1313                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_RCV   |\r
1314                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_RCV       |\r
1315                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_RCV        |\r
1316 \r
1317                                                                                   NDIS_STATISTICS_FLAGS_VALID_XMIT_DISCARDS                     |\r
1318                                                                                   NDIS_STATISTICS_FLAGS_VALID_XMIT_ERROR            |\r
1319                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_XMIT  | \r
1320                                           NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_XMIT |\r
1321                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_XMIT |\r
1322                                                                                   NDIS_STATISTICS_FLAGS_VALID_BYTES_XMIT                        |\r
1323                                                                                   NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_XMIT       |\r
1324                                                                                   NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_XMIT      |\r
1325                                                                                   NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_XMIT; */\r
1326                                                                                   \r
1327                                                                                   \r
1328         StatisticsInfo->SupportedStatistics =  NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_RCV |\r
1329                                                                                         //The data in the ifHCInUcastPkts member is valid. \r
1330                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_RCV |\r
1331                                                                                         //The data in the ifHCInMulticastPkts member is valid. \r
1332                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_RCV |\r
1333                                                                                         //The data in the ifHCInBroadcastPkts member is valid. \r
1334                                                                                         NDIS_STATISTICS_FLAGS_VALID_BYTES_RCV |\r
1335                                                                                         //The data in the ifHCInOctets member is valid. \r
1336                                                                                         NDIS_STATISTICS_FLAGS_VALID_RCV_DISCARDS |\r
1337                                                                                         //The data in the ifInDiscards member is valid. \r
1338                                                                                         NDIS_STATISTICS_FLAGS_VALID_RCV_ERROR |\r
1339                                                                                         //The data in the ifInErrors member is valid. \r
1340                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_FRAMES_XMIT |\r
1341                                                                                         //The data in the ifHCOutUcastPkts member is valid. \r
1342                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_FRAMES_XMIT |\r
1343                                                                                         //The data in the ifHCOutMulticastPkts member is valid. \r
1344                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_FRAMES_XMIT |\r
1345                                                                                         //The data in the ifHCOutBroadcastPkts member is valid. \r
1346                                                                                         NDIS_STATISTICS_FLAGS_VALID_BYTES_XMIT |\r
1347                                                                                         //The data in the ifHCOutOctets member is valid. \r
1348                                                                                         NDIS_STATISTICS_FLAGS_VALID_XMIT_ERROR |\r
1349                                                                                         //The data in the ifOutErrors member is valid. \r
1350                                                                                         NDIS_STATISTICS_FLAGS_VALID_XMIT_DISCARDS |\r
1351                                                                                         //The data in the ifOutDiscards member is valid. \r
1352                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_RCV |\r
1353                                                                                         //The data in the ifHCInUcastOctets member is valid. \r
1354                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_RCV |\r
1355                                                                                         //The data in the ifHCInMulticastOctets member is valid. \r
1356                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_RCV |\r
1357                                                                                         //The data in the ifHCInBroadcastOctets member is valid. \r
1358                                                                                         NDIS_STATISTICS_FLAGS_VALID_DIRECTED_BYTES_XMIT |\r
1359                                                                                         //The data in the ifHCOutUcastOctets member is valid. \r
1360                                                                                         NDIS_STATISTICS_FLAGS_VALID_MULTICAST_BYTES_XMIT |\r
1361                                                                                         //The data in the ifHCOutMulticastOctets member is valid. \r
1362                                                                                         NDIS_STATISTICS_FLAGS_VALID_BROADCAST_BYTES_XMIT ;\r
1363                                                                                         //The data in the ifHCOutBroadcastOctets member is valid\r
1364 \r
1365     cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1366     StatisticsInfo->ifInDiscards = p_adapter->recv_stats.comp.dropped +\r
1367                                    p_adapter->recv_stats.comp.error;            \r
1368     StatisticsInfo->ifInErrors = p_adapter->recv_stats.comp.error;\r
1369     StatisticsInfo->ifHCInOctets = p_adapter->recv_stats.ucast.bytes + \r
1370                                                                    p_adapter->recv_stats.bcast.bytes + \r
1371                                                                    p_adapter->recv_stats.mcast.bytes;       \r
1372     StatisticsInfo->ifHCInUcastPkts       = p_adapter->recv_stats.ucast.frames;\r
1373     StatisticsInfo->ifHCInMulticastPkts   = p_adapter->recv_stats.mcast.frames;  \r
1374     StatisticsInfo->ifHCInBroadcastPkts   = p_adapter->recv_stats.bcast.frames;  \r
1375         StatisticsInfo->ifHCInMulticastOctets = p_adapter->recv_stats.mcast.bytes;\r
1376         StatisticsInfo->ifHCInBroadcastOctets = p_adapter->recv_stats.bcast.bytes;\r
1377         StatisticsInfo->ifHCInUcastOctets     = p_adapter->recv_stats.ucast.bytes;\r
1378         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1379 \r
1380     cl_spinlock_acquire( &p_adapter->send_stat_lock ); \r
1381     StatisticsInfo->ifHCOutOctets = p_adapter->send_stats.ucast.bytes + \r
1382                                                                         p_adapter->send_stats.mcast.bytes + \r
1383                                                                         p_adapter->send_stats.bcast.bytes;        \r
1384     StatisticsInfo->ifHCOutUcastPkts       = p_adapter->send_stats.ucast.frames;     \r
1385     StatisticsInfo->ifHCOutMulticastPkts   = p_adapter->send_stats.mcast.frames; \r
1386     StatisticsInfo->ifHCOutBroadcastPkts   = p_adapter->send_stats.bcast.frames; \r
1387     StatisticsInfo->ifOutErrors            = p_adapter->send_stats.comp.error;                                      \r
1388     StatisticsInfo->ifOutDiscards          = p_adapter->send_stats.comp.dropped;\r
1389         StatisticsInfo->ifHCOutUcastOctets     = p_adapter->send_stats.ucast.bytes;\r
1390         StatisticsInfo->ifHCOutMulticastOctets = p_adapter->send_stats.mcast.bytes;\r
1391         StatisticsInfo->ifHCOutBroadcastOctets = p_adapter->send_stats.bcast.bytes;\r
1392     cl_spinlock_release( &p_adapter->send_stat_lock );\r
1393 \r
1394         *p_oid_info->p_bytes_used = sizeof(NDIS_STATISTICS_INFO);\r
1395 \r
1396         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1397         return NDIS_STATUS_SUCCESS;\r
1398 }\r
1399 \r
1400 NDIS_STATUS\r
1401 ipoib_get_recv_stat(\r
1402         IN                              ipoib_adapter_t* const          p_adapter,\r
1403         IN              const   ip_stat_sel_t                           stat_sel,\r
1404         IN                              pending_oid_t* const            p_oid_info )\r
1405 {\r
1406         uint64_t        stat;\r
1407 \r
1408         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1409 \r
1410         CL_ASSERT( p_adapter );\r
1411 \r
1412         cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1413         switch( stat_sel )\r
1414         {\r
1415         case IP_STAT_SUCCESS:\r
1416                 stat = p_adapter->recv_stats.comp.success;\r
1417                 break;\r
1418 \r
1419         case IP_STAT_ERROR:\r
1420                 stat = p_adapter->recv_stats.comp.error;\r
1421                 break;\r
1422 \r
1423         case IP_STAT_DROPPED:\r
1424                 stat = p_adapter->recv_stats.comp.dropped;\r
1425                 break;\r
1426 \r
1427         case IP_STAT_UCAST_BYTES:\r
1428                 stat = p_adapter->recv_stats.ucast.bytes;\r
1429                 break;\r
1430 \r
1431         case IP_STAT_UCAST_FRAMES:\r
1432                 stat = p_adapter->recv_stats.ucast.frames;\r
1433                 break;\r
1434 \r
1435         case IP_STAT_BCAST_BYTES:\r
1436                 stat = p_adapter->recv_stats.bcast.bytes;\r
1437                 break;\r
1438 \r
1439         case IP_STAT_BCAST_FRAMES:\r
1440                 stat = p_adapter->recv_stats.bcast.frames;\r
1441                 break;\r
1442 \r
1443         case IP_STAT_MCAST_BYTES:\r
1444                 stat = p_adapter->recv_stats.mcast.bytes;\r
1445                 break;\r
1446 \r
1447         case IP_STAT_MCAST_FRAMES:\r
1448                 stat = p_adapter->recv_stats.mcast.frames;\r
1449                 break;\r
1450 \r
1451         default:\r
1452                 stat = 0;\r
1453         }\r
1454         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1455 \r
1456         *p_oid_info->p_bytes_needed = sizeof(uint64_t);\r
1457 \r
1458         if( p_oid_info->buf_len >= sizeof(uint64_t) )\r
1459         {\r
1460                 *((uint64_t*)p_oid_info->p_buf) = stat;\r
1461                 *p_oid_info->p_bytes_used = sizeof(uint64_t);\r
1462         }\r
1463         else if( p_oid_info->buf_len >= sizeof(uint32_t) )\r
1464         {\r
1465                 *((uint32_t*)p_oid_info->p_buf) = (uint32_t)stat;\r
1466                 *p_oid_info->p_bytes_used = sizeof(uint32_t);\r
1467         }\r
1468         else\r
1469         {\r
1470                 *p_oid_info->p_bytes_used = 0;\r
1471                 IPOIB_EXIT( IPOIB_DBG_STAT );\r
1472                 return NDIS_STATUS_INVALID_LENGTH;\r
1473         }\r
1474 \r
1475         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1476         return NDIS_STATUS_SUCCESS;\r
1477 }\r
1478 \r
1479 \r
1480 void\r
1481 ipoib_inc_recv_stat(\r
1482         IN                              ipoib_adapter_t* const          p_adapter,\r
1483         IN              const   ip_stat_sel_t                           stat_sel,\r
1484         IN              const   size_t                                          bytes OPTIONAL,\r
1485         IN              const   size_t                                          packets OPTIONAL )\r
1486 {\r
1487         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1488 \r
1489         cl_spinlock_acquire( &p_adapter->recv_stat_lock );\r
1490         switch( stat_sel )\r
1491         {\r
1492         case IP_STAT_ERROR:\r
1493                 p_adapter->recv_stats.comp.error++;\r
1494                 break;\r
1495 \r
1496         case IP_STAT_DROPPED:\r
1497                 p_adapter->recv_stats.comp.dropped++;\r
1498                 break;\r
1499 \r
1500         case IP_STAT_UCAST_BYTES:\r
1501         case IP_STAT_UCAST_FRAMES:\r
1502                 p_adapter->recv_stats.comp.success++;\r
1503                 p_adapter->recv_stats.ucast.frames += packets;\r
1504                 p_adapter->recv_stats.ucast.bytes += bytes;\r
1505                 break;\r
1506 \r
1507         case IP_STAT_BCAST_BYTES:\r
1508         case IP_STAT_BCAST_FRAMES:\r
1509                 p_adapter->recv_stats.comp.success++;\r
1510                 p_adapter->recv_stats.bcast.frames += packets;\r
1511                 p_adapter->recv_stats.bcast.bytes += bytes;\r
1512                 break;\r
1513 \r
1514         case IP_STAT_MCAST_BYTES:\r
1515         case IP_STAT_MCAST_FRAMES:\r
1516                 p_adapter->recv_stats.comp.success++;\r
1517                 p_adapter->recv_stats.mcast.frames += packets;\r
1518                 p_adapter->recv_stats.mcast.bytes += bytes;\r
1519                 break;\r
1520 \r
1521         default:\r
1522                 break;\r
1523         }\r
1524         cl_spinlock_release( &p_adapter->recv_stat_lock );\r
1525 \r
1526         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1527 }\r
1528 \r
1529 NDIS_STATUS\r
1530 ipoib_get_send_stat(\r
1531         IN                              ipoib_adapter_t* const          p_adapter,\r
1532         IN              const   ip_stat_sel_t                           stat_sel,\r
1533         IN                              pending_oid_t* const            p_oid_info )\r
1534 {\r
1535         uint64_t        stat;\r
1536 \r
1537         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1538 \r
1539         CL_ASSERT( p_adapter );\r
1540 \r
1541         cl_spinlock_acquire( &p_adapter->send_stat_lock );\r
1542         switch( stat_sel )\r
1543         {\r
1544         case IP_STAT_SUCCESS:\r
1545                 stat = p_adapter->send_stats.comp.success;\r
1546                 break;\r
1547 \r
1548         case IP_STAT_ERROR:\r
1549                 stat = p_adapter->send_stats.comp.error;\r
1550                 break;\r
1551 \r
1552         case IP_STAT_DROPPED:\r
1553                 stat = p_adapter->send_stats.comp.dropped;\r
1554                 break;\r
1555 \r
1556         case IP_STAT_UCAST_BYTES:\r
1557                 stat = p_adapter->send_stats.ucast.bytes;\r
1558                 break;\r
1559 \r
1560         case IP_STAT_UCAST_FRAMES:\r
1561                 stat = p_adapter->send_stats.ucast.frames;\r
1562                 break;\r
1563 \r
1564         case IP_STAT_BCAST_BYTES:\r
1565                 stat = p_adapter->send_stats.bcast.bytes;\r
1566                 break;\r
1567 \r
1568         case IP_STAT_BCAST_FRAMES:\r
1569                 stat = p_adapter->send_stats.bcast.frames;\r
1570                 break;\r
1571 \r
1572         case IP_STAT_MCAST_BYTES:\r
1573                 stat = p_adapter->send_stats.mcast.bytes;\r
1574                 break;\r
1575 \r
1576         case IP_STAT_MCAST_FRAMES:\r
1577                 stat = p_adapter->send_stats.mcast.frames;\r
1578                 break;\r
1579 \r
1580         default:\r
1581                 stat = 0;\r
1582         }\r
1583         cl_spinlock_release( &p_adapter->send_stat_lock );\r
1584 \r
1585         *p_oid_info->p_bytes_needed = sizeof(uint64_t);\r
1586 \r
1587         if( p_oid_info->buf_len >= sizeof(uint64_t) )\r
1588         {\r
1589                 *((uint64_t*)p_oid_info->p_buf) = stat;\r
1590                 *p_oid_info->p_bytes_used = sizeof(uint64_t);\r
1591         }\r
1592         else if( p_oid_info->buf_len >= sizeof(uint32_t) )\r
1593         {\r
1594                 *((uint32_t*)p_oid_info->p_buf) = (uint32_t)stat;\r
1595                 *p_oid_info->p_bytes_used = sizeof(uint32_t);\r
1596         }\r
1597         else\r
1598         {\r
1599                 *p_oid_info->p_bytes_used = 0;\r
1600                 IPOIB_EXIT( IPOIB_DBG_STAT );\r
1601                 return NDIS_STATUS_INVALID_LENGTH;\r
1602         }\r
1603 \r
1604         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1605         return NDIS_STATUS_SUCCESS;\r
1606 }\r
1607 \r
1608 \r
1609 void\r
1610 ipoib_inc_send_stat(\r
1611         IN                              ipoib_adapter_t* const          p_adapter,\r
1612         IN              const   ip_stat_sel_t                           stat_sel,\r
1613         IN              const   size_t                                          bytes OPTIONAL )\r
1614 {\r
1615         IPOIB_ENTER( IPOIB_DBG_STAT );\r
1616 \r
1617         cl_spinlock_acquire( &p_adapter->send_stat_lock );\r
1618         switch( stat_sel )\r
1619         {\r
1620         case IP_STAT_ERROR:\r
1621                 p_adapter->send_stats.comp.error++;\r
1622                 break;\r
1623 \r
1624         case IP_STAT_DROPPED:\r
1625                 p_adapter->send_stats.comp.dropped++;\r
1626                 break;\r
1627 \r
1628         case IP_STAT_UCAST_BYTES:\r
1629         case IP_STAT_UCAST_FRAMES:\r
1630                 p_adapter->send_stats.comp.success++;\r
1631                 p_adapter->send_stats.ucast.frames++;\r
1632                 p_adapter->send_stats.ucast.bytes += bytes;\r
1633                 break;\r
1634 \r
1635         case IP_STAT_BCAST_BYTES:\r
1636         case IP_STAT_BCAST_FRAMES:\r
1637                 p_adapter->send_stats.comp.success++;\r
1638                 p_adapter->send_stats.bcast.frames++;\r
1639                 p_adapter->send_stats.bcast.bytes += bytes;\r
1640                 break;\r
1641 \r
1642         case IP_STAT_MCAST_BYTES:\r
1643         case IP_STAT_MCAST_FRAMES:\r
1644                 p_adapter->send_stats.comp.success++;\r
1645                 p_adapter->send_stats.mcast.frames++;\r
1646                 p_adapter->send_stats.mcast.bytes += bytes;\r
1647                 break;\r
1648 \r
1649         default:\r
1650                 break;\r
1651         }\r
1652         cl_spinlock_release( &p_adapter->send_stat_lock );\r
1653 \r
1654         IPOIB_EXIT( IPOIB_DBG_STAT );\r
1655 }\r