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