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