9663f55e9f1b6b5ea4bb604d1cace8ec425fdee6
[mirror/winof/.git] / core / bus / kernel / bus_port_mgr.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.\r
4  *\r
5  * This software is available to you under the OpenIB.org BSD license\r
6  * below:\r
7  *\r
8  *     Redistribution and use in source and binary forms, with or\r
9  *     without modification, are permitted provided that the following\r
10  *     conditions are met:\r
11  *\r
12  *      - Redistributions of source code must retain the above\r
13  *        copyright notice, this list of conditions and the following\r
14  *        disclaimer.\r
15  *\r
16  *      - Redistributions in binary form must reproduce the above\r
17  *        copyright notice, this list of conditions and the following\r
18  *        disclaimer in the documentation and/or other materials\r
19  *        provided with the distribution.\r
20  *\r
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
25  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
26  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
28  * SOFTWARE.\r
29  *\r
30  * $Id$\r
31  */\r
32 \r
33 \r
34 #include <iba/ib_types.h>\r
35 #include <complib/cl_async_proc.h>\r
36 #include <complib/cl_bus_ifc.h>\r
37 #include "ib_common.h"\r
38 #include "bus_pnp.h"\r
39 #include "bus_port_mgr.h"\r
40 #include "al_ca.h"\r
41 #include "al_mgr.h"\r
42 #include <initguid.h>\r
43 #include <wdmguid.h>\r
44 #include "iba/ipoib_ifc.h"\r
45 #include "al_dev.h"\r
46 \r
47 /* {5A9649F4-0101-4a7c-8337-796C48082DA2} */\r
48 DEFINE_GUID(GUID_BUS_TYPE_IBA,\r
49 0x5a9649f4, 0x101, 0x4a7c, 0x83, 0x37, 0x79, 0x6c, 0x48, 0x8, 0x2d, 0xa2);\r
50 \r
51 \r
52 /*\r
53  * Device extension for IPoIB port PDOs.\r
54  */\r
55 typedef struct _bus_port_ext\r
56 {\r
57         bus_pdo_ext_t                   pdo;\r
58 \r
59         port_guid_pkey_t                port_guid;\r
60         uint32_t                                n_port;\r
61 \r
62         /* Number of references on the upper interface. */\r
63         atomic32_t                              n_ifc_ref;\r
64 \r
65 }       bus_port_ext_t;\r
66 \r
67 \r
68 typedef struct _port_pnp_context\r
69 {\r
70         bus_filter_t    *p_bus_filter;\r
71         void                    *p_pdo_ext;\r
72         int                             port_num;\r
73 \r
74 }       port_pnp_ctx_t;\r
75 \r
76 \r
77 extern pkey_array_t  g_pkeys;\r
78 \r
79 /*\r
80  * Function prototypes.\r
81  */\r
82 void\r
83 destroying_port_mgr(\r
84         IN                              cl_obj_t*                                       p_obj );\r
85 \r
86 void\r
87 free_port_mgr(\r
88         IN                              cl_obj_t*                                       p_obj );\r
89 \r
90 ib_api_status_t\r
91 bus_reg_port_pnp(\r
92         IN                              bus_filter_t*                           p_bfi );\r
93 \r
94 ib_api_status_t\r
95 port_mgr_pnp_cb(\r
96         IN                              ib_pnp_rec_t*                           p_pnp_rec );\r
97 \r
98 ib_api_status_t\r
99 port_mgr_port_add(\r
100         IN                              ib_pnp_port_rec_t*                      p_pnp_rec );\r
101 \r
102 void\r
103 port_mgr_port_remove(\r
104         IN                              ib_pnp_port_rec_t*                      p_pnp_rec );\r
105 \r
106 static NTSTATUS\r
107 port_start(\r
108         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
109         IN                              IRP* const                                      p_irp,\r
110                 OUT                     cl_irp_action_t* const          p_action );\r
111 \r
112 static NTSTATUS\r
113 port_query_remove(\r
114         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
115         IN                              IRP* const                                      p_irp,\r
116                 OUT                     cl_irp_action_t* const          p_action );\r
117 \r
118 static void\r
119 port_release_resources(\r
120         IN                              DEVICE_OBJECT* const            p_dev_obj );\r
121 \r
122 static NTSTATUS\r
123 port_remove(\r
124         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
125         IN                              IRP* const                                      p_irp,\r
126                 OUT                     cl_irp_action_t* const          p_action );\r
127 \r
128 static NTSTATUS\r
129 port_surprise_remove(\r
130         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
131         IN                              IRP* const                                      p_irp,\r
132                 OUT                     cl_irp_action_t* const          p_action );\r
133 \r
134 static NTSTATUS\r
135 port_query_capabilities(\r
136         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
137         IN                              IRP* const                                      p_irp,\r
138                 OUT                     cl_irp_action_t* const          p_action );\r
139 \r
140 static NTSTATUS\r
141 port_query_target_relations(\r
142         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
143         IN                              IRP* const                                      p_irp,\r
144                 OUT                     cl_irp_action_t* const          p_action );\r
145 \r
146 static NTSTATUS\r
147 port_query_device_id(\r
148         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
149         IN                              IRP* const                                      p_irp );\r
150 \r
151 static NTSTATUS\r
152 port_query_hardware_ids(\r
153         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
154         IN                              IRP* const                                      p_irp );\r
155 \r
156 static NTSTATUS\r
157 port_query_compatible_ids(\r
158         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
159         IN                              IRP* const                                      p_irp );\r
160 \r
161 static NTSTATUS\r
162 port_query_unique_id(\r
163         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
164         IN                              IRP* const                                      p_irp );\r
165 \r
166 static NTSTATUS\r
167 port_query_description(\r
168         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
169         IN                              IRP* const                                      p_irp );\r
170 \r
171 \r
172 static NTSTATUS\r
173 port_query_location(\r
174         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
175         IN                              IRP* const                                      p_irp );\r
176 \r
177 static NTSTATUS\r
178 port_query_bus_info(\r
179         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
180         IN                              IRP* const                                      p_irp,\r
181                 OUT                     cl_irp_action_t* const          p_action );\r
182 \r
183 static NTSTATUS\r
184 port_query_ipoib_ifc(\r
185         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
186         IN                              IO_STACK_LOCATION* const        p_io_stack );\r
187 \r
188 static NTSTATUS\r
189 port_query_interface(\r
190         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
191         IN                              IRP* const                                      p_irp,\r
192                 OUT                     cl_irp_action_t* const          p_action );\r
193 \r
194 static NTSTATUS\r
195 port_set_power(\r
196         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
197         IN                              IRP* const                                      p_irp,\r
198                 OUT                     cl_irp_action_t* const          p_action );\r
199 \r
200 \r
201 \r
202 /*\r
203  * Global virtual function pointer tables shared between all\r
204  * instances of Port PDOs.\r
205  */\r
206 static const cl_vfptr_pnp_po_t          vfptr_port_pnp = {\r
207         "IODEVICE",\r
208         port_start,\r
209         cl_irp_succeed,\r
210         cl_irp_succeed,\r
211         cl_irp_succeed,\r
212         port_query_remove,\r
213         port_release_resources,\r
214         port_remove,\r
215         cl_irp_succeed,\r
216         port_surprise_remove,\r
217         port_query_capabilities,\r
218         cl_irp_complete,\r
219         cl_irp_complete,\r
220         cl_irp_succeed,\r
221         cl_irp_complete,\r
222         cl_irp_complete,\r
223         cl_irp_complete,\r
224         port_query_target_relations,\r
225         cl_irp_complete,\r
226         cl_irp_complete,\r
227         cl_irp_complete,\r
228         port_query_bus_info,\r
229         port_query_interface,\r
230         cl_irp_complete,\r
231         cl_irp_complete,\r
232         cl_irp_complete,\r
233         cl_irp_complete,\r
234         cl_irp_succeed,                         // QueryPower\r
235         port_set_power,                         // SetPower\r
236         cl_irp_unsupported,                     // PowerSequence\r
237         cl_irp_unsupported                      // WaitWake\r
238 };\r
239 \r
240 \r
241 static const cl_vfptr_query_txt_t               vfptr_port_query_txt = {\r
242         port_query_device_id,\r
243         port_query_hardware_ids,\r
244         port_query_compatible_ids,\r
245         port_query_unique_id,\r
246         port_query_description,\r
247         port_query_location\r
248 };\r
249 \r
250 \r
251 \r
252 /*\r
253  * Create the AL load service.\r
254  */\r
255 ib_api_status_t\r
256 create_port_mgr(\r
257                 IN                      bus_filter_t*                           p_bfi,\r
258                 OUT                     port_mgr_t** const                      pp_port_mgr )\r
259 {\r
260         ib_api_status_t         status;\r
261         cl_status_t                     cl_status;\r
262         port_mgr_t                      *p_port_mgr;\r
263 \r
264         BUS_ENTER( BUS_DBG_PNP );\r
265 \r
266         CL_ASSERT( p_bfi->p_port_mgr == NULL );\r
267 \r
268         p_port_mgr = cl_zalloc( sizeof( port_mgr_t ) );\r
269         if( !p_port_mgr )\r
270         {\r
271                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
272                         ("Failed to allocate port manager.\n") );\r
273                 return IB_INSUFFICIENT_MEMORY;\r
274         }\r
275         p_bfi->p_port_mgr = p_port_mgr;\r
276 \r
277         /* Construct the load service. */\r
278         cl_obj_construct( &p_port_mgr->obj, AL_OBJ_TYPE_LOADER );\r
279         p_bfi->p_port_mgr_obj = &p_port_mgr->obj;\r
280         cl_mutex_construct( &p_port_mgr->pdo_mutex );\r
281         cl_qlist_init( &p_port_mgr->port_list );\r
282 \r
283         cl_status = cl_mutex_init( &p_port_mgr->pdo_mutex );\r
284         if( cl_status != CL_SUCCESS )\r
285         {\r
286                 free_port_mgr( &p_port_mgr->obj );\r
287                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
288                         ("cl_mutex_init returned %#x.\n", cl_status) );\r
289                 return ib_convert_cl_status( cl_status );\r
290         }\r
291 \r
292         /* Initialize the load service object. */\r
293         cl_status = cl_obj_init( &p_port_mgr->obj, CL_DESTROY_SYNC,\r
294                                                          destroying_port_mgr, NULL, free_port_mgr );\r
295 \r
296         if( cl_status != CL_SUCCESS )\r
297         {\r
298                 free_port_mgr( &p_port_mgr->obj );\r
299                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
300                         ("cl_obj_init returned %#x.\n", cl_status) );\r
301                 return ib_convert_cl_status( cl_status );\r
302         }\r
303 \r
304         /* Register for port PnP events */\r
305         status = bus_reg_port_pnp(p_bfi);\r
306         if( status != IB_SUCCESS )\r
307         {\r
308                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
309                         ("bus_reg_port_pnp returned %s.\n", ib_get_err_str(status)) );\r
310                 free_port_mgr( &p_port_mgr->obj );\r
311                 return status;\r
312         }\r
313 \r
314         *pp_port_mgr = p_port_mgr;\r
315 \r
316         BUS_EXIT( BUS_DBG_PNP );\r
317         return IB_SUCCESS;\r
318 }\r
319 \r
320 \r
321 /*\r
322  * Pre-destroy the load service.\r
323  */\r
324 void\r
325 destroying_port_mgr(\r
326         IN                              cl_obj_t*                                       p_obj )\r
327 {\r
328         ib_api_status_t                 status;\r
329         bus_filter_t                    *p_bfi;\r
330         port_mgr_t                              *p_port_mgr;\r
331 \r
332         BUS_ENTER( BUS_DBG_PNP );\r
333 \r
334         CL_ASSERT( p_obj );\r
335         p_bfi = get_bfi_by_obj(BFI_PORT_MGR_OBJ, p_obj);\r
336         if (p_bfi == NULL) {\r
337                 BUS_PRINT(BUS_DBG_PNP, ("Failed to find p_bfi by obj %p?\n", p_obj));\r
338                 return;\r
339         }\r
340         p_port_mgr = p_bfi->p_port_mgr;\r
341 \r
342         BUS_PRINT(BUS_DBG_PNP, ("%s obj %p port_mgr %p port_mgr_obj %p\n",\r
343                         p_bfi->whoami,p_obj,p_port_mgr, p_bfi->p_port_mgr_obj) );\r
344 \r
345         CL_ASSERT( (void*)p_bfi->p_port_mgr == (void*)p_bfi->p_port_mgr_obj );\r
346         CL_ASSERT( p_port_mgr == PARENT_STRUCT( p_obj, port_mgr_t, obj ) );\r
347 \r
348         /* Deregister for port PnP events if this is the last Port manager. */\r
349         if ( get_bfi_count() == 1 && bus_globals.h_pnp_port ) {\r
350                 status = ib_dereg_pnp( bus_globals.h_pnp_port, NULL );\r
351                 bus_globals.h_pnp_port = NULL;\r
352                 CL_ASSERT( status == IB_SUCCESS );\r
353         }\r
354         cl_obj_deref( p_bfi->p_port_mgr_obj );\r
355 \r
356         BUS_EXIT( BUS_DBG_PNP );\r
357 }\r
358 \r
359 \r
360 /*\r
361  * Free the load service.\r
362  */\r
363 void\r
364 free_port_mgr(\r
365         IN                              cl_obj_t*                                       p_obj )\r
366 {\r
367         bus_pdo_ext_t   *p_ext;\r
368         cl_list_item_t  *p_list_item;\r
369         bus_filter_t    *p_bfi;\r
370         port_mgr_t              *p_port_mgr;\r
371 \r
372         BUS_ENTER( BUS_DBG_PNP );\r
373 \r
374         CL_ASSERT( p_obj );\r
375         p_bfi = get_bfi_by_obj(BFI_PORT_MGR_OBJ, p_obj);\r
376         if (p_bfi == NULL) {\r
377                 BUS_PRINT(BUS_DBG_PNP, ("No p_bfi for port obj %p?\n", p_obj));\r
378                 return;\r
379         }\r
380         p_port_mgr = p_bfi->p_port_mgr;\r
381         if ( !p_port_mgr ) {\r
382                 // if create fails & then free is called, p_bfi->p_port_mgr == NULL\r
383                 return;\r
384         }\r
385         CL_ASSERT( p_port_mgr == PARENT_STRUCT( p_obj, port_mgr_t, obj ) );\r
386 \r
387         BUS_PRINT(BUS_DBG_PNP, ("%s obj %p port_mgr %p port_mgr_obj %p\n",\r
388                         p_bfi->whoami, p_obj,p_port_mgr,\r
389                         p_bfi->p_port_mgr_obj) );\r
390 \r
391         BUS_PRINT( BUS_DBG_PNP,\r
392                                 ("%s Mark all IPoIB PDOs no longer present\n", p_bfi->whoami));\r
393         /*\r
394          * Mark all IPoIB PDOs as no longer present.  This will cause them\r
395          * to be removed when they process the IRP_MN_REMOVE_DEVICE.\r
396          */\r
397         p_list_item = cl_qlist_remove_head( &p_port_mgr->port_list );\r
398         while( p_list_item != cl_qlist_end( &p_port_mgr->port_list ) )\r
399         {\r
400                 p_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
401                 p_list_item = cl_qlist_remove_head( &p_port_mgr->port_list );\r
402                 if( p_ext->cl_ext.pnp_state == SurpriseRemoved )\r
403                 {\r
404                         CL_ASSERT( !p_ext->b_present );\r
405                         p_ext->b_reported_missing = TRUE;\r
406                         BUS_TRACE( BUS_DBG_PNP,\r
407                                 ("%s %s: PDO %p, ext %p, present %d, missing %d .\n",\r
408                                 p_bfi->whoami,\r
409                                 p_ext->cl_ext.vfptr_pnp_po->identity, p_ext->cl_ext.p_self_do,\r
410                                 p_ext, p_ext->b_present, p_ext->b_reported_missing ) );\r
411                         continue;\r
412                 }\r
413 \r
414                 if( p_ext->h_ca && p_ext->hca_acquired )\r
415                 {\r
416                         /* Invalidate bus relations for the HCA. */\r
417                         IoInvalidateDeviceRelations(\r
418                                 p_ext->h_ca->p_hca_dev, BusRelations );\r
419 \r
420                         /* Release the reference on the CA object. */\r
421                         deref_al_obj( &p_ext->h_ca->obj );\r
422                 }\r
423 \r
424                 BUS_TRACE( BUS_DBG_PNP, ("%s Deleted device %s: PDO %p, ext %p\n",\r
425                                                 p_bfi->whoami, p_ext->cl_ext.vfptr_pnp_po->identity,\r
426                                                 p_ext->cl_ext.p_self_do, p_ext ) );\r
427 \r
428                 IoDeleteDevice( p_ext->cl_ext.p_self_do );\r
429         }\r
430 \r
431         cl_mutex_destroy( &p_port_mgr->pdo_mutex );\r
432         cl_obj_deinit( p_obj );\r
433         cl_free( p_port_mgr );\r
434 \r
435         p_bfi->p_port_mgr = NULL;\r
436         p_bfi->p_port_mgr_obj = NULL;\r
437 \r
438         BUS_EXIT( BUS_DBG_PNP );\r
439 }\r
440 \r
441 \r
442 /*\r
443  * Register the load service for the given PnP class events.\r
444  */\r
445 ib_api_status_t\r
446 bus_reg_port_pnp( IN bus_filter_t *p_bfi )\r
447 {\r
448         ib_pnp_req_t                    pnp_req;\r
449         ib_api_status_t                 status = IB_SUCCESS;\r
450         boolean_t                               need_pnp_reg = FALSE;\r
451 \r
452         /* only need to register for port PNP events once.\r
453          * Do not hold mutex over pnp_reg() call as callback which needs mutex\r
454          * could occur.\r
455          */\r
456         if ( !bus_globals.h_pnp_port )\r
457         {\r
458                 lock_control_event();\r
459                 if ( !bus_globals.h_pnp_port ) {\r
460                         bus_globals.h_pnp_port = (ib_pnp_handle_t)1; /* block others */\r
461                         need_pnp_reg = TRUE;\r
462                 }\r
463                 unlock_control_event();\r
464 \r
465                 if ( need_pnp_reg )\r
466                 {\r
467                         cl_memclr( &pnp_req, sizeof( ib_pnp_req_t ) );\r
468                         pnp_req.pnp_class       = IB_PNP_PORT | IB_PNP_FLAG_REG_SYNC;\r
469                         pnp_req.pnp_context = NULL;\r
470                         pnp_req.pfn_pnp_cb      = port_mgr_pnp_cb;\r
471 \r
472                         status = ib_reg_pnp( gh_al, &pnp_req, &bus_globals.h_pnp_port );\r
473                 }\r
474         }\r
475 \r
476         if ( status == IB_SUCCESS )\r
477         {\r
478                 /* Reference this bus filter's port load service */\r
479                 cl_obj_ref( &p_bfi->p_port_mgr->obj );\r
480         }\r
481 \r
482         return status;\r
483 }\r
484 \r
485 \r
486 /*\r
487  * Load service PnP event callback.\r
488  */\r
489 ib_api_status_t\r
490 port_mgr_pnp_cb(\r
491         IN                              ib_pnp_rec_t*                           p_pnp_rec )\r
492 {\r
493         ib_api_status_t         status=IB_SUCCESS;\r
494 \r
495         BUS_ENTER( BUS_DBG_PNP );\r
496 \r
497         CL_ASSERT( p_pnp_rec );\r
498 \r
499         switch( p_pnp_rec->pnp_event )\r
500         {\r
501         case IB_PNP_PORT_ADD:\r
502                 status = port_mgr_port_add( (ib_pnp_port_rec_t*)p_pnp_rec );\r
503                 break;\r
504 \r
505         case IB_PNP_PORT_REMOVE:\r
506                 port_mgr_port_remove( (ib_pnp_port_rec_t*)p_pnp_rec );\r
507                 break;\r
508 \r
509         default:\r
510                 XBUS_PRINT( BUS_DBG_PNP, ("Unhandled PNP Event %s\n",\r
511                                         ib_get_pnp_event_str(p_pnp_rec->pnp_event) ));\r
512                 break;\r
513         }\r
514         BUS_EXIT( BUS_DBG_PNP );\r
515 \r
516         return status;\r
517 }\r
518 \r
519 \r
520 /*\r
521  * Called to get bus relations for an HCA.\r
522  */\r
523 NTSTATUS\r
524 port_mgr_get_bus_relations(\r
525         IN              const   net64_t                                         ca_guid,\r
526         IN                              IRP* const                                      p_irp )\r
527 {\r
528         NTSTATUS                        status;\r
529         bus_filter_t            *p_bfi;\r
530         port_mgr_t                      *p_port_mgr;\r
531         DEVICE_RELATIONS        *p_rel;\r
532 \r
533         BUS_ENTER( BUS_DBG_PNP );\r
534 \r
535         BUS_PRINT(BUS_DBG_PNP, ("CA_guid %I64x\n",ca_guid));\r
536 \r
537         /* special case guid == 0 - walk all bus filter instances */\r
538         if ( ca_guid == 0ULL ) {\r
539                 BUS_PRINT(BUS_DBG_PNP, ("CA_guid 0\n"));\r
540                 for(p_bfi=g_bus_filters; p_bfi < &g_bus_filters[MAX_BUS_FILTERS]; p_bfi++) {\r
541                         p_port_mgr = p_bfi->p_port_mgr;\r
542                         if ( !p_port_mgr )\r
543                                 continue;\r
544                         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
545                         status = bus_get_relations( &p_port_mgr->port_list,\r
546                                                                                 p_bfi->ca_guid,\r
547                                                                                 p_irp );\r
548                         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
549                 }\r
550 \r
551                 p_rel = (DEVICE_RELATIONS*)p_irp->IoStatus.Information;\r
552                 if ( p_rel ) {\r
553                         BUS_PRINT(BUS_DBG_PNP, ("CA_guid 0 Reports %d\n", p_rel->Count));\r
554                 }\r
555                 BUS_EXIT( BUS_DBG_PNP );\r
556                 return STATUS_SUCCESS;\r
557         }\r
558 \r
559         p_bfi = get_bfi_by_ca_guid(ca_guid);\r
560         if (p_bfi == NULL) {\r
561                 BUS_PRINT(BUS_DBG_PNP,\r
562                         ("Null *p_bfi from ca_guid %I64x\n",ca_guid));\r
563                 BUS_EXIT( BUS_DBG_PNP );\r
564                 return STATUS_NO_SUCH_DEVICE;\r
565         }\r
566         p_port_mgr = p_bfi->p_port_mgr;\r
567 \r
568         BUS_PRINT(BUS_DBG_PNP, ("%s for ca_guid %I64x port_mgr %p\n",\r
569                                                         p_bfi->whoami, ca_guid, p_port_mgr) );\r
570         if (!p_port_mgr)\r
571                 return STATUS_NO_SUCH_DEVICE;\r
572 \r
573         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
574         status = bus_get_relations( &p_port_mgr->port_list, ca_guid, p_irp );\r
575         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
576 \r
577         BUS_EXIT( BUS_DBG_PNP );\r
578         return STATUS_SUCCESS;\r
579 }\r
580 \r
581 static ib_api_status_t\r
582 __port_was_hibernated(\r
583         IN                              ib_pnp_port_rec_t*                      p_pnp_rec,\r
584         IN                              bus_filter_t*                           p_bfi )\r
585 {\r
586         ib_api_status_t status;\r
587         cl_list_item_t  *p_list_item;\r
588         bus_port_ext_t  *p_port_ext;\r
589         bus_pdo_ext_t   *p_shadow_pdo_ext, *p_pdo_ext = NULL;\r
590         size_t                  n_devs = 0;\r
591         port_mgr_t              *p_port_mgr = p_bfi->p_port_mgr;\r
592         cl_qlist_t              *p_pdo_list = &p_port_mgr->port_list;\r
593         port_pnp_ctx_t  *p_ctx = p_pnp_rec->pnp_rec.context;\r
594 \r
595         BUS_ENTER( BUS_DBG_PNP );\r
596 \r
597         if ( !p_port_mgr ) {\r
598                 // if free_port_mgr has been called , p_bfi->p_port_mgr == NULL \r
599                 // this will cause crash on cl_mutex_acquire\r
600                 // (leo) i'm not sure when it happens, but i saw it happened\r
601                 status = IB_NOT_FOUND;\r
602                 goto end;\r
603         }\r
604 \r
605         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
606         \r
607         /* Count the number of child devices. */\r
608         for( p_list_item = cl_qlist_head( p_pdo_list );\r
609                 p_list_item != cl_qlist_end( p_pdo_list );\r
610                 p_list_item = cl_qlist_next( p_list_item ) )\r
611         {\r
612                 p_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
613                 p_port_ext = (bus_port_ext_t*)p_pdo_ext;\r
614         \r
615                 if( p_pdo_ext->b_present && p_pdo_ext->b_hibernating && p_pdo_ext->hca_acquired &&\r
616                         (p_port_ext->port_guid.guid == p_pnp_rec->p_port_attr->port_guid) )\r
617                 {\r
618                         n_devs++;\r
619                         break;\r
620                 }\r
621 \r
622                 BUS_TRACE( BUS_DBG_PNP, ("%s Skipped acquire hca on PDO for %s: PDO %p, ext %p, "\r
623                         "present %d, missing %d, hibernating %d, port_guid %I64x.\n",\r
624                         p_bfi->whoami,\r
625                         p_pdo_ext->cl_ext.vfptr_pnp_po->identity,\r
626                         p_pdo_ext->cl_ext.p_self_do, \r
627                         p_pdo_ext, p_pdo_ext->b_present, p_pdo_ext->b_reported_missing, \r
628                         p_pdo_ext->b_hibernating, p_port_ext->port_guid.guid  ) );\r
629         }\r
630 \r
631         if (n_devs)\r
632         {\r
633                 /* Take a reference on the parent HCA. */\r
634                 p_pdo_ext->h_ca = acquire_ca( p_pnp_rec->p_ca_attr->ca_guid );\r
635                 if( !p_pdo_ext->h_ca )\r
636                 {\r
637                         BUS_TRACE( BUS_DBG_ERROR,\r
638                                 ("%s acquire_ca failed to find CA by guid %I64x\n",\r
639                                 p_bfi->whoami, p_pnp_rec->p_ca_attr->ca_guid ) );\r
640                         status = IB_INVALID_GUID;\r
641                 }\r
642                 else \r
643                 {\r
644                         p_pdo_ext->b_hibernating = FALSE;\r
645 \r
646                         CL_ASSERT( p_ctx );\r
647                         p_ctx->p_pdo_ext = p_pdo_ext; // save for port_mgr_port_remove\r
648 \r
649                         status = IB_SUCCESS;\r
650                         p_port_ext = (bus_port_ext_t*)p_pdo_ext;\r
651                         BUS_TRACE( BUS_DBG_PNP, ("%s Found PDO for %s: PDO %p, ext %p, "\r
652                                 "present %d, missing %d, hibernating %d, port_guid %I64x.\n",\r
653                                 p_bfi->whoami,\r
654                                 p_pdo_ext->cl_ext.vfptr_pnp_po->identity,\r
655                                 p_pdo_ext->cl_ext.p_self_do, \r
656                                 p_pdo_ext, p_pdo_ext->b_present, p_pdo_ext->b_reported_missing, \r
657                                 p_pdo_ext->b_hibernating, p_port_ext->port_guid.guid ) );\r
658 \r
659                         for( p_list_item = cl_qlist_head( p_pdo_list );\r
660                                 p_list_item != cl_qlist_end( p_pdo_list );\r
661                                 p_list_item = cl_qlist_next( p_list_item ) )\r
662                         {\r
663                                 p_shadow_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
664                                 p_port_ext = (bus_port_ext_t*)p_shadow_pdo_ext;\r
665 \r
666                                 if( p_shadow_pdo_ext->b_present && p_shadow_pdo_ext->b_hibernating &&\r
667                                         (p_port_ext->port_guid.guid == p_pnp_rec->p_port_attr->port_guid) )\r
668                                 {\r
669                                         p_shadow_pdo_ext->b_hibernating = FALSE;\r
670                                         p_shadow_pdo_ext->h_ca = p_pdo_ext->h_ca;\r
671 \r
672 \r
673                                         BUS_TRACE( BUS_DBG_PNP, ("%s set shadow h_ca PDO for %s: PDO %p, ext %p, "\r
674                                                                                          "present %d, missing %d, hibernating %d, port_guid %I64x.\n",\r
675                                                                                          p_bfi->whoami,\r
676                                                                                          p_shadow_pdo_ext->cl_ext.vfptr_pnp_po->identity,\r
677                                                                                          p_shadow_pdo_ext->cl_ext.p_self_do, \r
678                                                                                          p_shadow_pdo_ext, p_shadow_pdo_ext->b_present, p_shadow_pdo_ext->b_reported_missing, \r
679                                                                                          p_shadow_pdo_ext->b_hibernating, p_port_ext->port_guid.guid  ) );\r
680                                 }\r
681                         }\r
682 \r
683                 }\r
684         }\r
685         else \r
686         {\r
687                 BUS_TRACE( BUS_DBG_PNP, ("%s Failed to find PDO for guid  %I64x .\n",\r
688                                         p_bfi->whoami, p_pnp_rec->p_ca_attr->ca_guid ) );\r
689                 status = IB_NOT_FOUND;\r
690         }\r
691 \r
692         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
693 \r
694 end:\r
695         BUS_EXIT( BUS_DBG_PNP );\r
696         return status;\r
697 }\r
698 \r
699 #if DBG\r
700 \r
701 void\r
702 dump_pnp_port_rec( ib_pnp_port_rec_t*   pr )\r
703 {\r
704         BUS_TRACE( BUS_DBG_PNP, ("ib_pnp_port_rec_t* @ %p\nib_pnp_rec_t*:\n", pr));\r
705 \r
706 \r
707         BUS_PRINT( BUS_DBG_PNP, ("  Event %s\n",\r
708                                 ib_get_pnp_event_str(pr->pnp_rec.pnp_event) ));\r
709 \r
710         BUS_PRINT( BUS_DBG_PNP, ("  pnp_context %p\n",pr->pnp_rec.pnp_context));\r
711         BUS_PRINT( BUS_DBG_PNP, ("  context %p\n",pr->pnp_rec.context));\r
712         BUS_PRINT( BUS_DBG_PNP, ("  guid %I64x\n",pr->pnp_rec.guid));\r
713         BUS_PRINT( BUS_DBG_PNP, ("  ca_guid %I64x\n",pr->pnp_rec.ca_guid));\r
714 \r
715         if ( !pr->p_ca_attr ) {\r
716                 BUS_PRINT( BUS_DBG_PNP, ("  NULL *p_ca_attr ?\n"));\r
717         }\r
718         else {\r
719                 BUS_PRINT( BUS_DBG_PNP, ("*p_ca_attr\n"));\r
720                 BUS_PRINT( BUS_DBG_PNP, ("  ca_guid 0x%I64x\n",\r
721                                                                 pr->p_ca_attr->ca_guid ) );\r
722         }\r
723         if ( !pr->p_port_attr ) {\r
724                 BUS_PRINT( BUS_DBG_PNP, ("  NULL *p_port_attr?\n"));\r
725         }\r
726         else {\r
727                 BUS_PRINT( BUS_DBG_PNP, ("*p_port_attr:\n"));\r
728                 BUS_PRINT( BUS_DBG_PNP, ("  port_guid 0x%I64x port_num %d\n",\r
729                                                                 pr->p_port_attr->port_guid,\r
730                                                                 pr->p_port_attr->port_num ));\r
731         }\r
732 }\r
733 \r
734 void\r
735 dump_pnp_iou_rec( ib_pnp_iou_rec_t*     pr )\r
736 {\r
737         BUS_TRACE( BUS_DBG_PNP, ("ib_pnp_iou_rec_t* @ %p\nib_pnp_rec_t*:\n", pr));\r
738 \r
739 \r
740         BUS_PRINT( BUS_DBG_PNP, ("  Event %s\n",\r
741                                 ib_get_pnp_event_str(pr->pnp_rec.pnp_event) ));\r
742 \r
743         BUS_PRINT( BUS_DBG_PNP, ("  pnp_context %p\n",pr->pnp_rec.pnp_context));\r
744         BUS_PRINT( BUS_DBG_PNP, ("  context %p\n",pr->pnp_rec.context));\r
745         BUS_PRINT( BUS_DBG_PNP, ("  guid %I64x\n",pr->pnp_rec.guid));\r
746         BUS_PRINT( BUS_DBG_PNP, ("  ca_guid %I64x\n",pr->pnp_rec.ca_guid));\r
747 \r
748         BUS_PRINT( BUS_DBG_PNP, ("pnp_iou_rec_t:\n" ));\r
749         BUS_PRINT( BUS_DBG_PNP,\r
750                                 ("  guid 0x%I64x\n  ca_guid %I64x\n  chassis_guid %I64x\n",\r
751                                 pr->guid, pr->ca_guid, pr->chassis_guid ));\r
752         BUS_PRINT( BUS_DBG_PNP,\r
753                                 ("  slot 0x%x\n  vend_id 0x%x\n  dev_id 0x%x  revision 0x%x\n",\r
754                                 pr->slot, pr->vend_id, pr->dev_id, pr->revision ));\r
755         if ( pr->desc[0] ) {\r
756                 BUS_PRINT( BUS_DBG_PNP, ("  Desc %s\n",pr->desc ));\r
757         }\r
758 }\r
759 #endif\r
760 \r
761 \r
762 ib_api_status_t\r
763 port_mgr_port_add(\r
764         IN                              ib_pnp_port_rec_t*                      p_pnp_rec )\r
765 {\r
766         NTSTATUS                status;\r
767         DEVICE_OBJECT   *p_pdo; \r
768         bus_port_ext_t  *p_port_ext;\r
769         bus_filter_t    *p_bfi;\r
770         port_mgr_t              *p_port_mgr;\r
771         port_pnp_ctx_t  *p_ctx = p_pnp_rec->pnp_rec.context;\r
772         child_device_info_list_t *pCurList;\r
773         ib_ca_handle_t  h_ca = NULL;\r
774         ULONG                   pKey;\r
775         UNICODE_STRING  uniKey;\r
776 \r
777 \r
778         BUS_ENTER( BUS_DBG_PNP );\r
779 \r
780         CL_ASSERT( p_pnp_rec->p_ca_attr->ca_guid );\r
781 \r
782         p_bfi = get_bfi_by_ca_guid( p_pnp_rec->p_ca_attr->ca_guid );\r
783         if ( !p_bfi ) {\r
784                 BUS_TRACE_EXIT( BUS_DBG_PNP,("NULL p_bfi? ca_guid 0x%I64x\n",\r
785                                                                 p_pnp_rec->p_ca_attr->ca_guid ) );\r
786                 return IB_ERROR;\r
787         }\r
788 \r
789         /*\r
790          * Allocate a PNP context for this object. pnp_rec.context is obj unique.\r
791          */\r
792         if ( !p_ctx ) {\r
793                 p_ctx = cl_zalloc( sizeof(*p_ctx) );\r
794                 if( !p_ctx )\r
795                 {\r
796                         BUS_TRACE_EXIT(BUS_DBG_PNP,\r
797                                         ("%s ca_guid %I64x port(%d) BAD alloc PNP context\n",\r
798                                         p_bfi->whoami, p_bfi->ca_guid, \r
799                                         p_pnp_rec->p_port_attr->port_num));\r
800                         return IB_ERROR;\r
801                 }\r
802                 p_ctx->p_bus_filter = p_bfi;\r
803                 p_ctx->port_num = p_pnp_rec->p_port_attr->port_num;\r
804                 p_pnp_rec->pnp_rec.context = p_ctx;\r
805 \r
806                 BUS_PRINT(BUS_DBG_PNP,\r
807                                         ("%s ca_guid %I64x port %d ALLOC p_ctx @ %p\n",\r
808                                         p_bfi->whoami, p_bfi->ca_guid, \r
809                                         p_pnp_rec->p_port_attr->port_num,p_ctx));\r
810         }\r
811 \r
812         p_port_mgr = p_bfi->p_port_mgr;\r
813 \r
814         if( !bus_globals.b_report_port_nic )\r
815         {\r
816                 BUS_EXIT( BUS_DBG_PNP );\r
817                 return IB_NOT_DONE;\r
818         }\r
819 \r
820         /* Upon hibernating the computer IB_BUS driver doesn't remove PDO, but\r
821            marks with a flag. So we first try to find an existing PDO for this port,\r
822            marked with this flag. If it was found, we turn off the flag and use\r
823            this PDO */\r
824         status = __port_was_hibernated( p_pnp_rec, p_bfi );\r
825         if( status != IB_NOT_FOUND )\r
826         {\r
827                 BUS_EXIT( BUS_DBG_PNP );\r
828                 return status;\r
829         }\r
830 \r
831         pCurList = bus_globals.p_device_list;\r
832 \r
833         while(pCurList)\r
834         {\r
835                 /* Create the PDO for the new port device. */\r
836                 status = IoCreateDevice( bus_globals.p_driver_obj,\r
837                                                                  sizeof(bus_port_ext_t),\r
838                                                                  NULL, FILE_DEVICE_CONTROLLER,\r
839                                                                  FILE_DEVICE_SECURE_OPEN |\r
840                                                                  FILE_AUTOGENERATED_DEVICE_NAME,\r
841                                                                  FALSE, &p_pdo );\r
842                 if( !NT_SUCCESS( status ) )\r
843                 {\r
844                         BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
845                                 ("IoCreateDevice returned %08x.\n", status) );\r
846                         return IB_ERROR;\r
847                 }\r
848 \r
849                 /* clean the extension (must be before initializing) */\r
850                 p_port_ext = p_pdo->DeviceExtension;\r
851                 memset( p_port_ext, 0, sizeof(bus_port_ext_t) );\r
852 \r
853                 /* Initialize the device extension. */\r
854                 cl_init_pnp_po_ext( p_pdo, NULL, p_pdo,\r
855                                                         bus_globals.dbg_lvl, &vfptr_port_pnp,\r
856                                                         &vfptr_port_query_txt );\r
857 \r
858                 /* Set the DO_BUS_ENUMERATED_DEVICE flag to mark it as a PDO. */\r
859                 p_pdo->Flags |= DO_BUS_ENUMERATED_DEVICE;\r
860 \r
861                 p_port_ext->pdo.dev_po_state.DeviceState = PowerDeviceD0;\r
862                 p_port_ext->pdo.p_parent_ext = p_bfi->p_bus_ext;\r
863                 p_port_ext->pdo.b_present = TRUE;\r
864                 p_port_ext->pdo.b_reported_missing = FALSE;\r
865                 p_port_ext->pdo.b_hibernating = FALSE;\r
866                 p_port_ext->pdo.p_po_work_item = NULL;\r
867                 p_port_ext->pdo.p_pdo_device_info = &pCurList->io_device_info;\r
868                 BUS_TRACE( BUS_DBG_PNP,\r
869                         ("Created %s %s: PDO %p,ext %p, present %d, missing %d .\n",\r
870                         p_bfi->whoami,\r
871                         p_port_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_pdo,\r
872                         p_port_ext, p_port_ext->pdo.b_present,\r
873                         p_port_ext->pdo.b_reported_missing ) );\r
874 \r
875                 /* Cache the CA GUID. */\r
876                 p_port_ext->pdo.ca_guid = p_pnp_rec->p_ca_attr->ca_guid;\r
877 \r
878                 /*Only acquire one hca for each port*/\r
879                 if(h_ca)\r
880                 {\r
881                         p_port_ext->pdo.h_ca = h_ca;\r
882                         p_port_ext->pdo.hca_acquired = FALSE;\r
883                 }else\r
884                 {\r
885                         /* Acquire CA for the first child pdo*/\r
886                         h_ca = p_port_ext->pdo.h_ca = acquire_ca( p_pnp_rec->p_ca_attr->ca_guid );\r
887                         p_port_ext->pdo.hca_acquired = TRUE;\r
888                 }\r
889 \r
890                 if( !p_port_ext->pdo.h_ca )\r
891                 {\r
892                         BUS_TRACE( BUS_DBG_PNP, ("Deleted device: PDO %p\n", p_pdo));\r
893                         IoDeleteDevice( p_pdo);\r
894                         BUS_TRACE_EXIT( BUS_DBG_ERROR, ("acquire_ca failed to find CA.\n") );\r
895                         return IB_INVALID_GUID;\r
896                 }\r
897 \r
898                 p_port_ext->port_guid.guid = p_pnp_rec->p_port_attr->port_guid;\r
899                 p_port_ext->n_port = p_pnp_rec->p_port_attr->port_num;\r
900 \r
901                 RtlInitUnicodeString(&uniKey, pCurList->io_device_info.pkey);\r
902                 RtlUnicodeStringToInteger(&uniKey,16,&pKey);\r
903                 p_port_ext->port_guid.pkey = (ib_net16_t)pKey;\r
904 \r
905                 p_port_ext->n_ifc_ref = 0;\r
906 \r
907 \r
908                 /* Store the device extension in the port vector for future queries. */\r
909                 cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
910                 cl_qlist_insert_tail( &p_port_mgr->port_list,\r
911                                                           &p_port_ext->pdo.list_item );\r
912                 cl_mutex_release( &p_port_mgr->pdo_mutex );\r
913 \r
914                 /*\r
915                  * Set the context of the PNP event. The context is passed in for future\r
916                  * events on the same port.\r
917                  */\r
918                 if(p_port_ext->pdo.hca_acquired)\r
919                 {\r
920                         p_ctx->p_pdo_ext = p_port_ext;\r
921                 }\r
922 \r
923                 pCurList = pCurList->next_device_info;\r
924 \r
925                 /* Tell the PnP Manager to rescan for the HCA's bus relations. */\r
926                 IoInvalidateDeviceRelations(\r
927                         p_port_ext->pdo.h_ca->p_hca_dev, BusRelations );\r
928         }\r
929 \r
930         /* Invalidate removal relations for the bus driver. */\r
931         IoInvalidateDeviceRelations(\r
932                 p_bfi->p_bus_ext->cl_ext.p_pdo, RemovalRelations );\r
933 \r
934         BUS_EXIT( BUS_DBG_PNP );\r
935         return IB_SUCCESS;\r
936 }\r
937 \r
938 \r
939 \r
940 /************************************************************************************\r
941 * name  :       port_mgr_pkey_rem\r
942 *           removes pdo for each pkey value in pkey_array \r
943 * input :       g_pkeys\r
944 * output:       none\r
945 * return:       cl_status\r
946 *************************************************************************************/\r
947 cl_status_t _port_mgr_pkey_rem( IN      pkey_array_t    *pkeys,\r
948                                                                 IN      port_mgr_t              *p_port_mgr )\r
949 {\r
950 \r
951         uint16_t                        cnt;\r
952         cl_list_item_t          *p_list_item;\r
953         bus_port_ext_t          *p_port_ext;\r
954         bus_pdo_ext_t           *p_pdo_ext = NULL;\r
955         cl_qlist_t*                     p_pdo_list = &p_port_mgr->port_list;\r
956 \r
957         BUS_ENTER( BUS_DBG_PNP );\r
958 \r
959         p_port_ext = NULL;\r
960         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
961         \r
962         /* Count the number of child devices. */\r
963         for( p_list_item = cl_qlist_head( p_pdo_list );\r
964                 p_list_item != cl_qlist_end( p_pdo_list );\r
965                 p_list_item = cl_qlist_next( p_list_item ) )\r
966         {\r
967                 p_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
968                 p_port_ext = (bus_port_ext_t*)p_pdo_ext;\r
969 \r
970                 if(p_port_ext->port_guid.guid == pkeys->port_guid)\r
971                 {\r
972                         for(cnt = 0; cnt < pkeys->pkey_num; cnt++)\r
973                         {\r
974                                 if( (p_port_ext->port_guid.pkey == pkeys->pkey_array[cnt]) &&\r
975                                         (p_port_ext->port_guid.pkey != IB_DEFAULT_PKEY))\r
976                                 {\r
977                                         p_port_ext->pdo.b_present = FALSE;\r
978                                         break;\r
979                                 }\r
980                         }\r
981                 }\r
982         }\r
983         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
984 \r
985         /* Tell the PnP Manager to rescan for the HCA's bus relations. */\r
986         IoInvalidateDeviceRelations(\r
987                 p_port_ext->pdo.h_ca->p_hca_dev, BusRelations );\r
988 \r
989         BUS_EXIT( BUS_DBG_PNP );\r
990         return CL_SUCCESS;\r
991 }\r
992 \r
993 \r
994 cl_status_t port_mgr_pkey_rem( IN pkey_array_t *pkeys )\r
995 {\r
996         bus_filter_t    *p_bfi;\r
997         cl_status_t             status;\r
998         boolean_t               GO;\r
999         int                             success_cnt=0;\r
1000 \r
1001         for(p_bfi=&g_bus_filters[0]; p_bfi < &g_bus_filters[MAX_BUS_FILTERS]; p_bfi++)\r
1002         {\r
1003                 if ( !p_bfi->p_bus_ext )\r
1004                         continue;\r
1005                 GO = FALSE;\r
1006                 lock_control_event();\r
1007                 if ( p_bfi->ca_guid && p_bfi->p_port_mgr )\r
1008                         GO = TRUE;\r
1009                 unlock_control_event();\r
1010                 if ( GO == FALSE )\r
1011                         continue;\r
1012                 status = _port_mgr_pkey_rem( pkeys, p_bfi->p_port_mgr );\r
1013                 if ( status == CL_SUCCESS )\r
1014                         success_cnt++;\r
1015         }\r
1016         return ( success_cnt ? CL_SUCCESS : CL_ERROR );\r
1017 }\r
1018 \r
1019 \r
1020 /************************************************************************************\r
1021 * name  :       port_mgr_pkey_add\r
1022 *           creates pdo for each pkey value in pkey_array \r
1023 * input :       g_pkeys\r
1024 * output:       none\r
1025 * return:       cl_status\r
1026 *************************************************************************************/\r
1027 cl_status_t _port_mgr_pkey_add( IN      pkey_array_t    *req_pkeys,\r
1028                                                                 IN      bus_filter_t    *p_bfi,\r
1029                                                                 IN      port_mgr_t              *p_port_mgr )\r
1030 {\r
1031         uint16_t                        cnt;\r
1032         NTSTATUS            status;\r
1033         cl_list_item_t          *p_list_item;\r
1034         bus_port_ext_t          *p_port_ext, *pkey_port_ext, *pmatched_guid_ext;\r
1035         DEVICE_OBJECT       *p_pdo[MAX_NUM_PKEY];\r
1036         cl_qlist_t*                     p_pdo_list = &p_port_mgr->port_list;\r
1037 \r
1038         BUS_ENTER( BUS_DBG_PNP );\r
1039 \r
1040         pmatched_guid_ext = NULL;\r
1041         p_port_ext = NULL;\r
1042         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
1043         \r
1044         /* Count the number of child devices. */\r
1045         for( p_list_item = cl_qlist_head( p_pdo_list );\r
1046                 p_list_item != cl_qlist_end( p_pdo_list );\r
1047                 p_list_item = cl_qlist_next( p_list_item ) )\r
1048         {\r
1049                 p_port_ext = (bus_port_ext_t*)PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
1050 \r
1051                 if(p_port_ext->port_guid.guid == req_pkeys->port_guid)\r
1052                 {\r
1053                         uint16_t i;\r
1054                         for(i = 0; i < req_pkeys->pkey_num; i++)\r
1055                         {\r
1056                                 if(p_port_ext->port_guid.pkey == req_pkeys->pkey_array[i])\r
1057                                 {\r
1058                                         /* was removed previously */\r
1059                                         p_port_ext->pdo.b_present = TRUE;\r
1060                                         p_port_ext->pdo.b_reported_missing = FALSE;\r
1061                                         req_pkeys->pkey_array[i] = 0;  \r
1062                                 }\r
1063                         }\r
1064                         if(!pmatched_guid_ext)\r
1065                                 pmatched_guid_ext = p_port_ext;\r
1066                 }\r
1067         }\r
1068         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
1069 \r
1070         if (!pmatched_guid_ext)\r
1071         {\r
1072                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1073                         ("No existed pdo found.\n") );\r
1074                 return CL_NOT_FOUND;\r
1075         }\r
1076 \r
1077     for (cnt = 0; cnt < req_pkeys->pkey_num; cnt++)\r
1078     {\r
1079                 if(! (cl_hton16(req_pkeys->pkey_array[cnt]) & IB_PKEY_BASE_MASK) )\r
1080                         continue;\r
1081 \r
1082                 /* Create the PDO for the new port device. */\r
1083                 status = IoCreateDevice( bus_globals.p_driver_obj, sizeof(bus_port_ext_t),\r
1084                         NULL, FILE_DEVICE_CONTROLLER,\r
1085                         FILE_DEVICE_SECURE_OPEN | FILE_AUTOGENERATED_DEVICE_NAME,\r
1086                         FALSE, &p_pdo[cnt] );\r
1087                 if( !NT_SUCCESS( status ) )\r
1088                 {\r
1089                         BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1090                                 ("IoCreateDevice returned %08x.\n", status) );\r
1091                         return CL_ERROR;\r
1092                 }\r
1093         \r
1094                 /* Initialize the device extension. */\r
1095                 cl_init_pnp_po_ext( p_pdo[cnt], NULL, p_pdo[cnt], bus_globals.dbg_lvl,\r
1096                         &vfptr_port_pnp, &vfptr_port_query_txt );\r
1097         \r
1098                 /* Set the DO_BUS_ENUMERATED_DEVICE flag to mark it as a PDO. */\r
1099                 p_pdo[cnt]->Flags |= DO_BUS_ENUMERATED_DEVICE;\r
1100         \r
1101                 pkey_port_ext = p_pdo[cnt]->DeviceExtension;\r
1102                 pkey_port_ext->pdo.dev_po_state.DeviceState = PowerDeviceD0;\r
1103                 pkey_port_ext->pdo.p_parent_ext = p_bfi->p_bus_ext;\r
1104                 pkey_port_ext->pdo.b_present = TRUE;\r
1105                 pkey_port_ext->pdo.b_reported_missing = FALSE;\r
1106                 pkey_port_ext->pdo.b_hibernating = FALSE;\r
1107                 pkey_port_ext->pdo.p_po_work_item = NULL;\r
1108                 BUS_TRACE( BUS_DBG_PNP, ("Created device for %s: PDO %p,ext %p, present %d, missing %d .\n",\r
1109                         pkey_port_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_pdo[cnt], pkey_port_ext, pkey_port_ext->pdo.b_present, \r
1110                         pkey_port_ext->pdo.b_reported_missing ) );\r
1111         \r
1112                 /* Cache the CA GUID. */\r
1113                 pkey_port_ext->pdo.ca_guid = pmatched_guid_ext->pdo.ca_guid;\r
1114                 pkey_port_ext->pdo.h_ca = pmatched_guid_ext->pdo.h_ca;\r
1115                 pkey_port_ext->port_guid.guid = pmatched_guid_ext->port_guid.guid;\r
1116                 pkey_port_ext->n_port = pmatched_guid_ext->n_port;\r
1117                 pkey_port_ext->port_guid.pkey = req_pkeys->pkey_array[cnt];\r
1118                 /* Store the device extension in the port vector for future queries. */\r
1119                 cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
1120                 cl_qlist_insert_tail( &p_port_mgr->port_list,\r
1121                         &pkey_port_ext->pdo.list_item );\r
1122                 cl_mutex_release( &p_port_mgr->pdo_mutex );\r
1123         }\r
1124 \r
1125         /* Tell the PnP Manager to rescan for the HCA's bus relations. */\r
1126         IoInvalidateDeviceRelations(\r
1127                 pmatched_guid_ext->pdo.h_ca->p_hca_dev, BusRelations );\r
1128 \r
1129         /* Invalidate removal relations for the bus driver. */\r
1130         IoInvalidateDeviceRelations(\r
1131                 p_bfi->p_bus_ext->cl_ext.p_pdo, RemovalRelations );\r
1132 \r
1133         BUS_EXIT( BUS_DBG_PNP );\r
1134         return CL_SUCCESS;\r
1135 }\r
1136 \r
1137 cl_status_t port_mgr_pkey_add(pkey_array_t *pkeys)\r
1138 {\r
1139         bus_filter_t    *p_bfi;\r
1140         cl_status_t             status;\r
1141         boolean_t               GO;\r
1142         int                             success_cnt=0;\r
1143 \r
1144         for(p_bfi=&g_bus_filters[0]; p_bfi < &g_bus_filters[MAX_BUS_FILTERS]; p_bfi++)\r
1145         {\r
1146                 if ( !p_bfi->p_bus_ext )\r
1147                         continue;\r
1148                 GO = FALSE;\r
1149                 lock_control_event();\r
1150                 if ( p_bfi->ca_guid && p_bfi->p_port_mgr )\r
1151                         GO = TRUE;\r
1152                 unlock_control_event();\r
1153                 if ( GO == FALSE )\r
1154                         continue;\r
1155                 status = _port_mgr_pkey_add( pkeys, p_bfi, p_bfi->p_port_mgr );\r
1156                 if ( status == CL_SUCCESS )\r
1157                         success_cnt++;\r
1158         }\r
1159         return ( success_cnt ? CL_SUCCESS : CL_ERROR );\r
1160 }\r
1161 \r
1162 \r
1163 void\r
1164 port_mgr_port_remove(\r
1165         IN                              ib_pnp_port_rec_t*              p_pnp_rec )\r
1166 {\r
1167         bus_pdo_ext_t   *p_ext;\r
1168         port_mgr_t              *p_port_mgr;\r
1169         bus_filter_t    *p_bfi;\r
1170         port_pnp_ctx_t  *p_ctx = p_pnp_rec->pnp_rec.context;\r
1171         cl_list_item_t          *p_list_item;\r
1172         bus_port_ext_t          *p_port_ext;\r
1173         bus_pdo_ext_t           *p_pdo_ext;\r
1174         cl_qlist_t*                     p_pdo_list;\r
1175 \r
1176         BUS_ENTER( BUS_DBG_PNP );\r
1177 \r
1178         if ( !p_ctx ) {\r
1179                 BUS_EXIT( BUS_DBG_PNP );\r
1180                 return;\r
1181         }\r
1182 \r
1183         CL_ASSERT( p_ctx->p_bus_filter->magic == BFI_MAGIC );\r
1184         p_bfi = p_ctx->p_bus_filter;\r
1185         CL_ASSERT( p_bfi );\r
1186 \r
1187         BUS_PRINT(BUS_DBG_PNP,("%s ca_guid 0x%I64x port_num %d port_mgr %p\n",\r
1188                                                 p_bfi->whoami, p_bfi->ca_guid,\r
1189                                                 p_ctx->port_num, p_bfi->p_port_mgr));\r
1190 \r
1191         /* in the process of device remove, port_mgr has been destroyed.\r
1192          * cleanup allocated PNP port context; one per port.\r
1193          * The issue is the PNP PORT_REMOVE event occurs after\r
1194          * fdo_release_resources() completes.\r
1195          */\r
1196         if ( p_bfi->ca_guid == 0ULL || !p_bfi->p_port_mgr ) {\r
1197                 cl_free( p_ctx );\r
1198                 p_pnp_rec->pnp_rec.context = NULL;\r
1199                 BUS_EXIT( BUS_DBG_PNP );\r
1200                 return;\r
1201         }\r
1202 \r
1203         p_port_mgr = p_bfi->p_port_mgr;\r
1204 \r
1205         /* Within the PNP record's context is the port extension ptr;\r
1206          * see port_was_hibernated().\r
1207          */\r
1208         p_ext = p_ctx->p_pdo_ext;\r
1209         CL_ASSERT( p_ext );\r
1210 \r
1211         /*\r
1212          * Flag the port PDO as no longer being present.  We have to wait until\r
1213          * the PnP manager removes it to clean up.  However, we do release the\r
1214          * reference on the CA object in order to allow the removal of the HCA\r
1215          * to proceed should it occur before the port's PDO is cleaned up.\r
1216          */\r
1217         if ( !p_ext->h_ca )\r
1218         {\r
1219                 BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1220                                                 ("%s NULL h_ca? p_ext %p\n", p_bfi->whoami, p_ext ) );\r
1221                 return;\r
1222         }\r
1223 \r
1224         // Don't crash if p_ext->p_parent_ext is NULL\r
1225         CL_ASSERT((p_ext->p_parent_ext == NULL) || p_bfi == p_ext->p_parent_ext->bus_filter);\r
1226         \r
1227         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
1228         CL_ASSERT( p_ext->h_ca );\r
1229 \r
1230         if( p_ext->b_hibernating )\r
1231         {\r
1232                 BUS_TRACE( BUS_DBG_PNP, ("Skip port removing for %s: PDO %p, ext %p, "\r
1233                         "present %d, missing %d, hibernating %d .\n",\r
1234                         p_ext->cl_ext.vfptr_pnp_po->identity, p_ext->cl_ext.p_self_do,\r
1235                         p_ext, p_ext->b_present, p_ext->b_reported_missing,\r
1236                         p_ext->b_hibernating ) );\r
1237                 goto hca_deref;\r
1238         }\r
1239 \r
1240         p_ext->b_present = FALSE;\r
1241 \r
1242         p_pdo_list = &p_port_mgr->port_list;\r
1243 \r
1244         for( p_list_item = cl_qlist_head( p_pdo_list );\r
1245                  p_list_item != cl_qlist_end( p_pdo_list );\r
1246                  p_list_item = cl_qlist_next( p_list_item ) )\r
1247                 {\r
1248                 p_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
1249                 p_port_ext =  (bus_port_ext_t*) p_pdo_ext;\r
1250 \r
1251                 if( (p_port_ext->port_guid.guid == ((bus_port_ext_t*)p_ext)->port_guid.guid) )\r
1252                         {\r
1253                         p_pdo_ext->b_present = FALSE;\r
1254                 }                       \r
1255         }\r
1256 \r
1257 \r
1258         BUS_TRACE( BUS_DBG_PNP,\r
1259                 ("Mark removing %s: PDO %p, ext %p, present %d, missing %d .\n",\r
1260                 p_ext->cl_ext.vfptr_pnp_po->identity, p_ext->cl_ext.p_self_do, p_ext,\r
1261                 p_ext->b_present, p_ext->b_reported_missing ) );\r
1262 \r
1263         /* Invalidate removal relations for the bus driver. */\r
1264         IoInvalidateDeviceRelations(\r
1265                 p_bfi->p_bus_ext->cl_ext.p_pdo, RemovalRelations );\r
1266 \r
1267         /* Invalidate bus relations for the HCA. */\r
1268         IoInvalidateDeviceRelations(\r
1269                 p_ext->h_ca->p_hca_dev, BusRelations );\r
1270 \r
1271 hca_deref:\r
1272         /* Free PNP context memory */\r
1273         cl_free( p_ctx );\r
1274         p_pnp_rec->pnp_rec.context = NULL;\r
1275 \r
1276         deref_al_obj( &p_ext->h_ca->obj );\r
1277         \r
1278         /* Setting h_ca to be NULL forces IPoIB to start only after re-acquiring\r
1279          * new CA object. The latter happens in __port_was_hibernated or\r
1280          * port_mgr_port_add functions  after arriving IB_PNP_PORT_ADD event\r
1281          * from IBAL.\r
1282          */\r
1283         p_ext->h_ca = NULL;\r
1284 \r
1285         p_pdo_list = &p_port_mgr->port_list;\r
1286 \r
1287         for( p_list_item = cl_qlist_head( p_pdo_list );\r
1288                  p_list_item != cl_qlist_end( p_pdo_list );\r
1289                  p_list_item = cl_qlist_next( p_list_item ) )\r
1290                 {\r
1291                 p_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
1292                 p_port_ext =  (bus_port_ext_t*) p_pdo_ext;\r
1293 \r
1294                 if( p_port_ext->port_guid.guid == ((bus_port_ext_t*)p_ext)->port_guid.guid )\r
1295                 {\r
1296                         p_pdo_ext->h_ca = NULL;\r
1297                 }                       \r
1298         }\r
1299 \r
1300         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
1301 \r
1302         BUS_EXIT( BUS_DBG_PNP );\r
1303 }\r
1304 \r
1305 \r
1306 static NTSTATUS\r
1307 port_start(\r
1308         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1309         IN                                      IRP* const                              p_irp,\r
1310                 OUT                             cl_irp_action_t* const  p_action )\r
1311 {\r
1312         bus_pdo_ext_t   *p_ext;\r
1313 \r
1314         BUS_ENTER( BUS_DBG_PNP );\r
1315 \r
1316         UNUSED_PARAM( p_irp );\r
1317 \r
1318         p_ext = p_dev_obj->DeviceExtension;\r
1319 \r
1320         /* Notify the Power Manager that the device is started. */\r
1321         PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
1322         *p_action = IrpComplete;\r
1323         BUS_EXIT( BUS_DBG_PNP );\r
1324 \r
1325         return STATUS_SUCCESS;\r
1326 }\r
1327 \r
1328 \r
1329 static NTSTATUS\r
1330 port_query_remove(\r
1331         IN                              DEVICE_OBJECT* const    p_dev_obj,\r
1332         IN                              IRP* const                              p_irp,\r
1333                 OUT                     cl_irp_action_t* const  p_action )\r
1334 {\r
1335         bus_port_ext_t  *p_ext;\r
1336 \r
1337         BUS_ENTER( BUS_DBG_PNP );\r
1338 \r
1339         UNUSED_PARAM( p_irp );\r
1340 \r
1341         p_ext = p_dev_obj->DeviceExtension;\r
1342 \r
1343         *p_action = IrpComplete;\r
1344 \r
1345         if( p_ext->n_ifc_ref )\r
1346         {\r
1347                 /*\r
1348                  * Our interface is still being held by someone.\r
1349                  * Rollback the PnP state that was changed in the complib handler.\r
1350                  */\r
1351                 cl_rollback_pnp_state( &p_ext->pdo.cl_ext );\r
1352 \r
1353                 /* Fail the query. */\r
1354                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Failing IRP_MN_QUERY_REMOVE_DEVICE:\n"\r
1355                         "\tInterface has %d reference\n", p_ext->n_ifc_ref ) );\r
1356                 return STATUS_UNSUCCESSFUL;\r
1357         }\r
1358 \r
1359         CL_ASSERT(p_ext->pdo.p_parent_ext->bus_filter);\r
1360         BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1361                         ("OK IRP_MN_QUERY_REMOVE_DEVICE:\n  %s HCA guid %I64x port %d\n",\r
1362                         p_ext->pdo.p_parent_ext->bus_filter->whoami,\r
1363                         p_ext->pdo.ca_guid, p_ext->n_port ) );\r
1364 \r
1365         return STATUS_SUCCESS;\r
1366 }\r
1367 \r
1368 \r
1369 static void\r
1370 port_release_resources(\r
1371         IN                                      DEVICE_OBJECT* const    p_dev_obj )\r
1372 {\r
1373         bus_port_ext_t  *p_ext;\r
1374         POWER_STATE             po_state;\r
1375         port_mgr_t              *p_port_mgr;\r
1376 \r
1377         BUS_ENTER( BUS_DBG_PNP );\r
1378 \r
1379         p_ext = p_dev_obj->DeviceExtension;\r
1380         p_port_mgr = p_ext->pdo.p_parent_ext->bus_filter->p_port_mgr;\r
1381 \r
1382         /* skip releasing resources if PDO has not been yet reported missing */\r
1383         if (!p_ext->pdo.b_reported_missing) {\r
1384                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("PDO is not yet reported missing - skip the removing port from vector: PDO %p, ext %p\n",\r
1385                         p_dev_obj, p_ext) );\r
1386                 return;\r
1387         }\r
1388 \r
1389         /* Remove this PDO from its list. */\r
1390         cl_mutex_acquire( &p_port_mgr->pdo_mutex );\r
1391         BUS_TRACE( BUS_DBG_PNP, ("Removing port from vector: PDO %p, ext %p\n",\r
1392                                 p_dev_obj, p_ext) );\r
1393         cl_qlist_remove_item( &p_port_mgr->port_list, &p_ext->pdo.list_item );\r
1394         cl_mutex_release( &p_port_mgr->pdo_mutex );\r
1395         po_state.DeviceState = PowerDeviceD3;\r
1396         PoSetPowerState( p_ext->pdo.cl_ext.p_pdo, DevicePowerState, po_state );\r
1397 \r
1398         BUS_EXIT( BUS_DBG_PNP );\r
1399 }\r
1400 \r
1401 \r
1402 static NTSTATUS\r
1403 port_remove(\r
1404         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1405         IN                                      IRP* const                              p_irp,\r
1406                 OUT                             cl_irp_action_t* const  p_action )\r
1407 {\r
1408         bus_port_ext_t  *p_ext;\r
1409 \r
1410         BUS_ENTER( BUS_DBG_PNP );\r
1411 \r
1412         p_ext = p_dev_obj->DeviceExtension;\r
1413 \r
1414         if( p_ext->pdo.b_present )\r
1415         {\r
1416                 CL_ASSERT( p_ext->pdo.cl_ext.pnp_state != NotStarted );\r
1417                 CL_ASSERT( !p_ext->pdo.b_reported_missing );\r
1418                 /* Reset the state to NotStarted.  CompLib set it to Deleted. */\r
1419                 cl_set_pnp_state( &p_ext->pdo.cl_ext, NotStarted );\r
1420                 /* Don't delete the device.  It may simply be disabled. */\r
1421                 *p_action = IrpComplete;\r
1422                 BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1423                                 ("Device %s still present: PDO %p, ext %p\n",\r
1424                                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_dev_obj, p_ext) );\r
1425                 return STATUS_SUCCESS;\r
1426         }\r
1427 \r
1428         if( !p_ext->pdo.b_reported_missing )\r
1429         {\r
1430                 /* Reset the state to RemovePending.  Complib set it to Deleted. */\r
1431                 cl_rollback_pnp_state( &p_ext->pdo.cl_ext );\r
1432                 *p_action = IrpComplete;\r
1433                 BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1434                                                 ("Device %s not reported missing yet: PDO %p, ext %p\n",\r
1435                                                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity,\r
1436                                                 p_dev_obj, p_ext) );\r
1437                 return STATUS_SUCCESS;\r
1438         }\r
1439 \r
1440         /* Wait for all I/O operations to complete. */\r
1441         IoReleaseRemoveLockAndWait( &p_ext->pdo.cl_ext.remove_lock, p_irp );\r
1442 \r
1443         /* Release resources if it was not done yet. */\r
1444         if( p_ext->pdo.cl_ext.last_pnp_state != SurpriseRemoved )\r
1445                 p_ext->pdo.cl_ext.vfptr_pnp_po->pfn_release_resources( p_dev_obj );\r
1446 \r
1447         p_irp->IoStatus.Status = STATUS_SUCCESS;\r
1448         IoCompleteRequest( p_irp, IO_NO_INCREMENT );\r
1449 \r
1450         BUS_TRACE( BUS_DBG_PNP, ("Deleted device %s: PDO %p(=%p), ext %p\n",\r
1451                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_ext->pdo.cl_ext.p_self_do,\r
1452                 p_dev_obj, p_ext ) );\r
1453 \r
1454         IoDeleteDevice( p_dev_obj );\r
1455 \r
1456         *p_action = IrpDoNothing;\r
1457         BUS_EXIT( BUS_DBG_PNP );\r
1458         return STATUS_SUCCESS;\r
1459 }\r
1460 \r
1461 \r
1462 static NTSTATUS\r
1463 port_surprise_remove(\r
1464         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
1465         IN                              IRP* const                                      p_irp,\r
1466                 OUT                     cl_irp_action_t* const          p_action )\r
1467 {\r
1468         bus_port_ext_t  *p_ext;\r
1469 \r
1470         BUS_ENTER( BUS_DBG_PNP );\r
1471 \r
1472         UNUSED_PARAM( p_irp );\r
1473 \r
1474         p_ext = p_dev_obj->DeviceExtension;\r
1475         //\r
1476         // Setting 2 folloeing flags seems like the right behaviour\r
1477         // according to DDK, but it causes \r
1478         // WHQL PnP SurpriseRemoval test to fail\r
1479         // So, as a work around, they are disabled for now.\r
1480         // The best solution is to rewrite all the drivers\r
1481         // to WDF model, hoping it will handle right all PnP/Power issues\r
1482         //\r
1483 //      p_ext->pdo.b_present = FALSE;\r
1484 //      p_ext->pdo.b_reported_missing = TRUE;\r
1485         if (!p_ext->pdo.b_reported_missing) {\r
1486                 // we have not yet reported the device absence \r
1487                 cl_rollback_pnp_state( &p_ext->pdo.cl_ext );\r
1488         }\r
1489         BUS_TRACE( BUS_DBG_PNP, ("%s: PDO %p, ext %p, present %d, missing %d .\n",\r
1490                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_ext->pdo.cl_ext.p_self_do, \r
1491                 p_ext, p_ext->pdo.b_present, p_ext->pdo.b_reported_missing ) );\r
1492 \r
1493         *p_action = IrpComplete;\r
1494 \r
1495         BUS_EXIT( BUS_DBG_PNP );\r
1496         return STATUS_SUCCESS;\r
1497 }\r
1498 \r
1499 \r
1500 static NTSTATUS\r
1501 port_query_capabilities(\r
1502         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1503         IN                                      IRP* const                              p_irp,\r
1504                 OUT                             cl_irp_action_t* const  p_action )\r
1505 {\r
1506         DEVICE_CAPABILITIES             *p_caps;\r
1507         IO_STACK_LOCATION               *p_io_stack;\r
1508 \r
1509         BUS_ENTER( BUS_DBG_PNP );\r
1510 \r
1511         UNUSED_PARAM( p_dev_obj );\r
1512 \r
1513         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
1514         p_caps = p_io_stack->Parameters.DeviceCapabilities.Capabilities;\r
1515 \r
1516         p_caps->DeviceD1 = FALSE;\r
1517         p_caps->DeviceD2 = FALSE;\r
1518         p_caps->LockSupported = FALSE;\r
1519         p_caps->EjectSupported = FALSE;\r
1520         p_caps->Removable = FALSE;\r
1521         p_caps->DockDevice = FALSE;\r
1522         p_caps->UniqueID = TRUE;\r
1523         p_caps->SilentInstall = TRUE;\r
1524         p_caps->RawDeviceOK = FALSE;\r
1525         p_caps->SurpriseRemovalOK = FALSE;\r
1526         p_caps->WakeFromD0 = FALSE;\r
1527         p_caps->WakeFromD1 = FALSE;\r
1528         p_caps->WakeFromD2 = FALSE;\r
1529         p_caps->WakeFromD3 = FALSE;\r
1530         p_caps->HardwareDisabled = FALSE;\r
1531         p_caps->DeviceState[PowerSystemWorking] = PowerDeviceD0;\r
1532         p_caps->DeviceState[PowerSystemSleeping1] = PowerDeviceD3;\r
1533         p_caps->DeviceState[PowerSystemSleeping2] = PowerDeviceD3;\r
1534         p_caps->DeviceState[PowerSystemSleeping3] = PowerDeviceD3;\r
1535         p_caps->DeviceState[PowerSystemHibernate] = PowerDeviceD3;\r
1536         p_caps->DeviceState[PowerSystemShutdown] = PowerDeviceD3;\r
1537         p_caps->SystemWake = PowerSystemUnspecified;\r
1538         p_caps->DeviceWake = PowerDeviceUnspecified;\r
1539         p_caps->D1Latency = 0;\r
1540         p_caps->D2Latency = 0;\r
1541         p_caps->D3Latency = 0;\r
1542 \r
1543         *p_action = IrpComplete;\r
1544         BUS_EXIT( BUS_DBG_PNP );\r
1545         return STATUS_SUCCESS;\r
1546 }\r
1547 \r
1548 \r
1549 static NTSTATUS\r
1550 port_query_target_relations(\r
1551         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1552         IN                                      IRP* const                              p_irp,\r
1553                 OUT                             cl_irp_action_t* const  p_action )\r
1554 {\r
1555         NTSTATUS                        status;\r
1556         DEVICE_RELATIONS        *p_rel;\r
1557 \r
1558         BUS_ENTER( BUS_DBG_PNP );\r
1559 \r
1560         *p_action = IrpComplete;\r
1561 \r
1562         status = cl_alloc_relations( p_irp, 1 );\r
1563         if( !NT_SUCCESS( status ) )\r
1564         {\r
1565                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1566                         ("cl_alloc_relations returned 0x%08x.\n", status) );\r
1567                 return status;\r
1568         }\r
1569 \r
1570         p_rel = (DEVICE_RELATIONS*)p_irp->IoStatus.Information;\r
1571         p_rel->Count = 1;\r
1572         p_rel->Objects[0] = p_dev_obj;\r
1573 \r
1574         ObReferenceObject( p_dev_obj );\r
1575 \r
1576         BUS_EXIT( BUS_DBG_PNP );\r
1577         return status;\r
1578 }\r
1579 \r
1580 \r
1581 static NTSTATUS\r
1582 port_query_device_id(\r
1583         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1584                 OUT                             IRP* const                              p_irp )\r
1585 {\r
1586         WCHAR                           *p_string;\r
1587         bus_port_ext_t          *p_ext;\r
1588         size_t                          dev_id_size;\r
1589         \r
1590         BUS_ENTER( BUS_DBG_PNP );\r
1591 \r
1592         \r
1593         p_ext = (bus_port_ext_t*)p_dev_obj->DeviceExtension;\r
1594         if( !p_ext->pdo.b_present )\r
1595         {\r
1596                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1597                 return STATUS_NO_SUCH_DEVICE;\r
1598         }\r
1599 \r
1600         dev_id_size = p_ext->pdo.p_pdo_device_info->device_id_size;\r
1601 \r
1602         /* Device ID is "IBA\SID_<sid> where <sid> is the IO device Service ID. */\r
1603         p_string = ExAllocatePoolWithTag( PagedPool, dev_id_size, 'vedq' );\r
1604         if( !p_string )\r
1605         {\r
1606                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1607                         ("Failed to allocate device ID buffer (%d bytes).\n",\r
1608                         dev_id_size) );\r
1609                 return STATUS_INSUFFICIENT_RESOURCES;\r
1610         }\r
1611 \r
1612         RtlZeroMemory(p_string, dev_id_size);\r
1613 \r
1614         cl_memcpy( p_string, p_ext->pdo.p_pdo_device_info->device_id, dev_id_size );\r
1615 \r
1616         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1617 \r
1618         BUS_EXIT( BUS_DBG_PNP );\r
1619         return STATUS_SUCCESS;\r
1620 }\r
1621 \r
1622 \r
1623 static NTSTATUS\r
1624 port_query_hardware_ids(\r
1625         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1626                 OUT                             IRP* const                              p_irp )\r
1627 {\r
1628         WCHAR                           *p_string;\r
1629         bus_port_ext_t          *p_ext;\r
1630         size_t                          dev_id_size;\r
1631 \r
1632         BUS_ENTER( BUS_DBG_PNP );\r
1633 \r
1634 \r
1635         p_ext = (bus_port_ext_t*)p_dev_obj->DeviceExtension;\r
1636 \r
1637         dev_id_size = p_ext->pdo.p_pdo_device_info->hardware_id_size;\r
1638 \r
1639         p_string = ExAllocatePoolWithTag( PagedPool, dev_id_size, 'ihqp' );\r
1640         if( !p_string )\r
1641         {\r
1642                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1643                         ("Failed to allocate hardware ID buffer (%d bytes).\n",\r
1644                         dev_id_size) );\r
1645                 return STATUS_INSUFFICIENT_RESOURCES;\r
1646         }\r
1647 \r
1648         RtlZeroMemory(p_string, dev_id_size);\r
1649 \r
1650         cl_memcpy( p_string, p_ext->pdo.p_pdo_device_info->hardware_id, dev_id_size );\r
1651 \r
1652         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1653 \r
1654         BUS_EXIT( BUS_DBG_PNP );\r
1655         return STATUS_SUCCESS;\r
1656 }\r
1657 \r
1658 \r
1659 static NTSTATUS\r
1660 port_query_compatible_ids(\r
1661         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1662                 OUT                             IRP* const                              p_irp )\r
1663 {\r
1664         WCHAR                           *p_string;\r
1665         bus_port_ext_t          *p_ext;\r
1666         size_t                          dev_id_size;\r
1667 \r
1668         BUS_ENTER( BUS_DBG_PNP );\r
1669 \r
1670         p_ext = (bus_port_ext_t*)p_dev_obj->DeviceExtension;\r
1671 \r
1672         dev_id_size = p_ext->pdo.p_pdo_device_info->compatible_id_size;\r
1673 \r
1674         p_string = ExAllocatePoolWithTag( PagedPool, dev_id_size, 'ihqp' );\r
1675         if( !p_string )\r
1676         {\r
1677                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1678                         ("Failed to allocate hardware ID buffer (%d bytes).\n",\r
1679                         dev_id_size) );\r
1680                 return STATUS_INSUFFICIENT_RESOURCES;\r
1681         }\r
1682 \r
1683         RtlZeroMemory(p_string, dev_id_size);\r
1684 \r
1685         cl_memcpy( p_string, p_ext->pdo.p_pdo_device_info->compatible_id, dev_id_size );\r
1686 \r
1687         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1688 \r
1689         BUS_EXIT( BUS_DBG_PNP );\r
1690         return STATUS_SUCCESS;\r
1691 }\r
1692 \r
1693 \r
1694 static NTSTATUS\r
1695 port_query_unique_id(\r
1696         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1697                 OUT                             IRP* const                              p_irp )\r
1698 {\r
1699         NTSTATUS                        status;\r
1700         WCHAR                           *p_string;\r
1701         bus_port_ext_t          *p_ext;\r
1702 \r
1703         BUS_ENTER( BUS_DBG_PNP );\r
1704 \r
1705         p_ext = p_dev_obj->DeviceExtension;\r
1706 \r
1707         if( !p_ext->pdo.b_present )\r
1708         {\r
1709                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1710                 return STATUS_NO_SUCH_DEVICE;\r
1711         }\r
1712 \r
1713         /* The instance ID is the port GUID. */\r
1714         p_string = ExAllocatePoolWithTag( PagedPool, sizeof(WCHAR) * 21, 'iuqp' );\r
1715         if( !p_string )\r
1716         {\r
1717                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1718                         ("Failed to allocate instance ID buffer (%d bytes).\n",\r
1719                         sizeof(WCHAR) * 21) );\r
1720                 return STATUS_NO_MEMORY;\r
1721         }\r
1722 \r
1723         status = RtlStringCchPrintfW( p_string, 21, L"%016I64x%04x",p_ext->port_guid.guid,p_ext->port_guid.pkey );\r
1724         if( !NT_SUCCESS( status ) )\r
1725         {\r
1726                 CL_ASSERT( NT_SUCCESS( status ) );\r
1727                 ExFreePool( p_string );\r
1728                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1729                         ("RtlStringCchPrintfW returned %08x.\n", status) );\r
1730                 return status;\r
1731         }\r
1732 \r
1733         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1734 \r
1735         BUS_EXIT( BUS_DBG_PNP );\r
1736         return STATUS_SUCCESS;\r
1737 }\r
1738 \r
1739 \r
1740 static NTSTATUS\r
1741 port_query_description(\r
1742         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1743                 OUT                             IRP* const                              p_irp )\r
1744 {\r
1745         WCHAR                           *p_string;\r
1746         bus_port_ext_t          *p_ext;\r
1747         \r
1748         BUS_ENTER( BUS_DBG_PNP );\r
1749 \r
1750         p_ext = p_dev_obj->DeviceExtension;\r
1751         if( !p_ext->pdo.b_present )\r
1752         {\r
1753                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1754                 return STATUS_NO_SUCH_DEVICE;\r
1755         }\r
1756 \r
1757 \r
1758         p_string = ExAllocatePoolWithTag( PagedPool, p_ext->pdo.p_pdo_device_info->description_size, 'edqp' );\r
1759 \r
1760         if( !p_string )\r
1761         {\r
1762                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1763                         ("Failed to allocate device description buffer (%d bytes).\n",\r
1764                         p_ext->pdo.p_pdo_device_info->description_size) );\r
1765                 return STATUS_INSUFFICIENT_RESOURCES;\r
1766         }\r
1767 \r
1768         RtlZeroMemory(p_string,p_ext->pdo.p_pdo_device_info->description_size);\r
1769 \r
1770         cl_memcpy( p_string, p_ext->pdo.p_pdo_device_info->description, p_ext->pdo.p_pdo_device_info->description_size );\r
1771 \r
1772         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1773 \r
1774         BUS_EXIT( BUS_DBG_PNP );\r
1775         return STATUS_SUCCESS;\r
1776 }\r
1777 \r
1778 \r
1779 static NTSTATUS\r
1780 port_query_location(\r
1781         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1782                 OUT                             IRP* const                              p_irp )\r
1783 {\r
1784         WCHAR                           *p_string;\r
1785         bus_port_ext_t          *p_ext;\r
1786         size_t                          size;\r
1787         ULONG                           len;\r
1788         NTSTATUS                        status;\r
1789         DEVICE_OBJECT           *p_hca_dev;\r
1790 \r
1791         BUS_ENTER( BUS_DBG_PNP );\r
1792 \r
1793         p_ext = p_dev_obj->DeviceExtension;\r
1794         if( !p_ext->pdo.b_present )\r
1795         {\r
1796                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1797                 return STATUS_NO_SUCH_DEVICE;\r
1798         }\r
1799 \r
1800         p_hca_dev = p_ext->pdo.h_ca->p_hca_dev;\r
1801 \r
1802         /* Get the length of the HCA's location. */\r
1803         status = IoGetDeviceProperty( p_hca_dev,\r
1804                 DevicePropertyLocationInformation, 0, NULL, &len );\r
1805         if( status != STATUS_BUFFER_TOO_SMALL )\r
1806         {\r
1807                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1808                         ("IoGetDeviceProperty for device location size returned %08x.\n",\r
1809                         status) );\r
1810                 return status;\r
1811         }\r
1812 \r
1813         /*\r
1814          * Allocate the string buffer to hold the HCA's location along with the\r
1815          * port number.  The port number is 32-bits, so in decimal it can be at\r
1816          * most 10 characters.\r
1817          */\r
1818         size = len + sizeof(L", port ") + (sizeof(WCHAR) * 10);\r
1819         if( size > (USHORT)-1 )\r
1820         {\r
1821                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1822                         ("Length beyond limits.\n") );\r
1823                 return STATUS_INSUFFICIENT_RESOURCES;\r
1824         }\r
1825 \r
1826         p_string = ExAllocatePoolWithTag( PagedPool, size, 'olqp' );\r
1827         if( !p_string )\r
1828         {\r
1829                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1830                         ("Failed to allocate device location buffer (%d bytes).\n", len) );\r
1831                 return STATUS_NO_MEMORY;\r
1832         }\r
1833 \r
1834         /* Get the HCA's location information. */\r
1835         status = IoGetDeviceProperty( p_hca_dev,\r
1836                 DevicePropertyLocationInformation, len, p_string, &len );\r
1837         if( !NT_SUCCESS( status ) )\r
1838         {\r
1839                 ExFreePool( p_string );\r
1840                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1841                         ("IoGetDeviceProperty for device location returned %08x.\n",\r
1842                         status) );\r
1843                 return status;\r
1844         }\r
1845 \r
1846         /* Append the port number to the HCA's location. */\r
1847         status = RtlStringCbPrintfW( p_string + (len/2) - 1, size - len + 1,\r
1848                 L", port %d", p_ext->n_port );\r
1849         if( !NT_SUCCESS( status ) )\r
1850         {\r
1851                 CL_ASSERT( NT_SUCCESS( status ) );\r
1852                 ExFreePool( p_string );\r
1853                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1854                         ("RtlStringCbPrintfW returned %08x.\n", status) );\r
1855                 return status;\r
1856         }\r
1857 \r
1858         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1859 \r
1860         BUS_EXIT( BUS_DBG_PNP );\r
1861         return STATUS_SUCCESS;\r
1862 }\r
1863 \r
1864 \r
1865 static NTSTATUS\r
1866 port_query_bus_info(\r
1867         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1868         IN                                      IRP* const                              p_irp,\r
1869                 OUT                             cl_irp_action_t* const  p_action )\r
1870 {\r
1871         PNP_BUS_INFORMATION     *p_bus_info;\r
1872 \r
1873         BUS_ENTER( BUS_DBG_PNP );\r
1874 \r
1875         UNUSED_PARAM( p_dev_obj );\r
1876 \r
1877         *p_action = IrpComplete;\r
1878 \r
1879         p_bus_info = ExAllocatePoolWithTag( PagedPool, sizeof(PNP_BUS_INFORMATION), 'ibqp' );\r
1880         if( !p_bus_info )\r
1881         {\r
1882                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1883                         ("Failed to allocate PNP_BUS_INFORMATION (%d bytes).\n",\r
1884                         sizeof(PNP_BUS_INFORMATION)) );\r
1885                 return STATUS_INSUFFICIENT_RESOURCES;\r
1886         }\r
1887 \r
1888         p_bus_info->BusTypeGuid = GUID_BUS_TYPE_IBA;\r
1889         //TODO: Memory from Intel - storage miniport would not stay loaded unless\r
1890         //TODO: bus type was PCI.  Look here if SRP is having problems staying\r
1891         //TODO: loaded.\r
1892         p_bus_info->LegacyBusType = PNPBus;\r
1893         p_bus_info->BusNumber = 0;\r
1894 \r
1895         p_irp->IoStatus.Information = (ULONG_PTR)p_bus_info;\r
1896         BUS_EXIT( BUS_DBG_PNP );\r
1897         return STATUS_SUCCESS;\r
1898 }\r
1899 \r
1900 \r
1901 static NTSTATUS\r
1902 port_query_ipoib_ifc(\r
1903         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
1904         IN                              IO_STACK_LOCATION* const        p_io_stack )\r
1905 {\r
1906         NTSTATUS                                status;\r
1907         ib_al_ifc_t                             *p_ifc;\r
1908         ib_al_ifc_data_t                *p_ifc_data;\r
1909         ipoib_ifc_data_t                *p_ipoib_data;\r
1910         bus_port_ext_t                  *p_ext;\r
1911         const GUID                              *p_guid;\r
1912 \r
1913         BUS_ENTER( BUS_DBG_PNP );\r
1914 \r
1915         CL_ASSERT( KeGetCurrentIrql() < DISPATCH_LEVEL );\r
1916 \r
1917         p_ext = p_dev_obj->DeviceExtension;\r
1918 \r
1919         BUS_TRACE( BUS_DBG_PNP, ("Query i/f for %s: PDO %p (=%p),ext %p, present %d, missing %d, hibernated %d .\n",\r
1920                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_ext->pdo.cl_ext.p_self_do, \r
1921                 p_dev_obj, p_ext, p_ext->pdo.b_present, p_ext->pdo.b_reported_missing,\r
1922                 p_ext->pdo.b_hibernating ) );\r
1923 \r
1924         /* Get the interface. */\r
1925         status = cl_fwd_query_ifc(\r
1926                 p_ext->pdo.p_parent_ext->cl_ext.p_self_do, p_io_stack );\r
1927         if( !NT_SUCCESS( status ) )\r
1928         {\r
1929                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1930                         ("Failed to forward interface query: %08X\n", status) );\r
1931                 return status;\r
1932         }\r
1933 \r
1934         if( !p_io_stack->Parameters.QueryInterface.InterfaceSpecificData )\r
1935         {\r
1936                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("No interface specific data!\n") );\r
1937                 return status;\r
1938         }\r
1939 \r
1940         p_ifc = (ib_al_ifc_t*)p_io_stack->Parameters.QueryInterface.Interface;\r
1941 \r
1942         p_ifc_data = (ib_al_ifc_data_t*)\r
1943                 p_io_stack->Parameters.QueryInterface.InterfaceSpecificData;\r
1944         p_guid = p_ifc_data->type;\r
1945         if( !IsEqualGUID( p_guid, &GUID_IPOIB_INTERFACE_DATA ) )\r
1946         {\r
1947                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Unsupported interface data: \n\t"\r
1948                         "0x%08x, 0x%04x, 0x%04x, 0x%02x, 0x%02x, 0x%02x,"\r
1949                         "0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x.\n",\r
1950                         p_guid->Data1, p_guid->Data2, p_guid->Data3,\r
1951                         p_guid->Data4[0], p_guid->Data4[1], p_guid->Data4[2],\r
1952                         p_guid->Data4[3], p_guid->Data4[4], p_guid->Data4[5],\r
1953                         p_guid->Data4[6], p_guid->Data4[7]) );\r
1954                 return status;\r
1955         }\r
1956 \r
1957         if( p_ifc_data->version != IPOIB_INTERFACE_DATA_VERSION )\r
1958         {\r
1959                 p_ifc->wdm.InterfaceDereference( p_ifc->wdm.Context );\r
1960                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1961                         ("Unsupported version %d, expected %d\n",\r
1962                         p_ifc_data->version, IPOIB_INTERFACE_DATA_VERSION) );\r
1963                 return STATUS_NOT_SUPPORTED;\r
1964         }\r
1965 \r
1966         ASSERT( p_ifc_data->p_data );\r
1967 \r
1968         if( p_ifc_data->size != sizeof(ipoib_ifc_data_t) )\r
1969         {\r
1970                 p_ifc->wdm.InterfaceDereference( p_ifc->wdm.Context );\r
1971                 BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1972                         ("Buffer too small (%d given, %d required).\n",\r
1973                         p_ifc_data->size,\r
1974                         sizeof(ipoib_ifc_data_t)) );\r
1975                 return STATUS_BUFFER_TOO_SMALL;\r
1976         }\r
1977 \r
1978         /* Set the interface data. */\r
1979         p_ipoib_data = (ipoib_ifc_data_t*)p_ifc_data->p_data;\r
1980 \r
1981         p_ipoib_data->ca_guid = p_ext->pdo.h_ca->obj.p_ci_ca->verbs.guid;\r
1982         p_ipoib_data->port_guid = p_ext->port_guid;\r
1983         p_ipoib_data->port_num = (uint8_t)p_ext->n_port;\r
1984 \r
1985         BUS_EXIT( BUS_DBG_PNP );\r
1986         return STATUS_SUCCESS;\r
1987 }\r
1988 \r
1989 \r
1990 static NTSTATUS\r
1991 port_query_interface(\r
1992         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1993         IN                                      IRP* const                              p_irp,\r
1994                 OUT                             cl_irp_action_t* const  p_action )\r
1995 {\r
1996         bus_pdo_ext_t           *p_ext;\r
1997         NTSTATUS                        status;\r
1998         IO_STACK_LOCATION       *p_io_stack;\r
1999 \r
2000         BUS_ENTER( BUS_DBG_PNP );\r
2001 \r
2002         PAGED_CODE();\r
2003 \r
2004         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
2005 \r
2006         /* Bottom of the stack - IRP must be completed. */\r
2007         *p_action = IrpComplete;\r
2008 \r
2009         /* Compare requested GUID with our supported interface GUIDs. */\r
2010         if( IsEqualGUID( p_io_stack->Parameters.QueryInterface.InterfaceType,\r
2011                 &GUID_IB_AL_INTERFACE ) )\r
2012         {\r
2013                 status = port_query_ipoib_ifc( p_dev_obj, p_io_stack );\r
2014         }\r
2015         else if( IsEqualGUID( p_io_stack->Parameters.QueryInterface.InterfaceType,\r
2016                 &GUID_BUS_INTERFACE_STANDARD ) )\r
2017         {\r
2018                 p_ext = p_dev_obj->DeviceExtension;\r
2019                 if( !p_ext->h_ca ||\r
2020                         !p_ext->b_present ||\r
2021                         !p_ext->h_ca->p_hca_dev ||\r
2022                         p_ext->b_reported_missing )\r
2023                 {\r
2024                         return STATUS_NO_SUCH_DEVICE;\r
2025                 }\r
2026 \r
2027                 status = cl_fwd_query_ifc(\r
2028                         p_ext->h_ca->p_hca_dev, p_io_stack );\r
2029         }\r
2030         else\r
2031         {\r
2032                 status = p_irp->IoStatus.Status;\r
2033         }\r
2034         \r
2035         BUS_EXIT( BUS_DBG_PNP );\r
2036         return status;\r
2037 }\r
2038 \r
2039 \r
2040 /* Work item callback to handle DevicePowerD3 IRPs at passive level. */\r
2041 static void\r
2042 __HibernateUpWorkItem(\r
2043         IN                              DEVICE_OBJECT*                          p_dev_obj,\r
2044         IN                              void*                                           context )\r
2045 {\r
2046         IO_STACK_LOCATION       *p_io_stack;\r
2047         bus_pdo_ext_t           *p_ext;\r
2048         IRP                                     *p_irp;\r
2049         POWER_STATE powerState;\r
2050 \r
2051         BUS_ENTER( BUS_DBG_POWER );\r
2052 \r
2053         p_ext = (bus_pdo_ext_t*)p_dev_obj->DeviceExtension;\r
2054         p_irp = (IRP*)context;\r
2055         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
2056 \r
2057         IoFreeWorkItem( p_ext->p_po_work_item );\r
2058         p_ext->p_po_work_item = NULL;\r
2059 \r
2060         while (!p_ext->h_ca) {\r
2061                 BUS_TRACE( BUS_DBG_PNP, ("Waiting for the end of HCA registration ... \n"));\r
2062                 cl_thread_suspend( 200 );       /* suspend for 200 ms */\r
2063         }\r
2064 \r
2065         p_ext->dev_po_state = p_io_stack->Parameters.Power.State;\r
2066         powerState = PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
2067 \r
2068         BUS_TRACE( BUS_DBG_POWER, \r
2069                 ("PoSetPowerState: old state %d, new state to %d\n", \r
2070                 powerState.DeviceState, p_ext->dev_po_state ));\r
2071 \r
2072         p_irp->IoStatus.Status = STATUS_SUCCESS;\r
2073         PoStartNextPowerIrp( p_irp );\r
2074         IoCompleteRequest( p_irp, IO_NO_INCREMENT );\r
2075         IoReleaseRemoveLock( &p_ext->cl_ext.remove_lock, p_irp );\r
2076 \r
2077         BUS_EXIT( BUS_DBG_POWER );\r
2078 }\r
2079 \r
2080 \r
2081 /*\r
2082  * The PDOs created by the IB Bus driver are software devices.  As such,\r
2083  * all power states are supported.  It is left to the HCA power policy\r
2084  * owner to handle which states can be supported by the HCA.\r
2085  */\r
2086 static NTSTATUS\r
2087 port_set_power(\r
2088         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
2089         IN                              IRP* const                                      p_irp,\r
2090                 OUT                     cl_irp_action_t* const          p_action )\r
2091 {\r
2092         NTSTATUS                        status = STATUS_SUCCESS;\r
2093         IO_STACK_LOCATION       *p_io_stack;\r
2094         bus_pdo_ext_t           *p_ext;\r
2095 \r
2096         BUS_ENTER( BUS_DBG_POWER );\r
2097 \r
2098         p_ext = p_dev_obj->DeviceExtension;\r
2099         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
2100 \r
2101         BUS_TRACE( BUS_DBG_POWER, \r
2102                 ("SET_POWER for PDO %p (ext %p): type %s, state %d, action %d \n",\r
2103                 p_dev_obj, p_ext,\r
2104                 (p_io_stack->Parameters.Power.Type) ? "DevicePowerState" : "SystemPowerState",\r
2105                 p_io_stack->Parameters.Power.State.DeviceState, \r
2106                 p_io_stack->Parameters.Power.ShutdownType ));\r
2107 \r
2108         if ((p_io_stack->Parameters.Power.Type == SystemPowerState) &&\r
2109                 (p_io_stack->Parameters.Power.State.SystemState ==PowerSystemHibernate ||\r
2110                 p_io_stack->Parameters.Power.State.SystemState ==PowerSystemSleeping1 ))\r
2111         {\r
2112                 BUS_TRACE( BUS_DBG_POWER, ("Setting b_hibernating flag for PDO %p \n", p_dev_obj));\r
2113                 p_ext->b_hibernating = TRUE;\r
2114         }\r
2115 \r
2116         if( p_io_stack->Parameters.Power.Type == DevicePowerState )\r
2117         {\r
2118                 /* after hibernation PDO is not ready for work. we need to wait for finishing of the HCA registration */\r
2119                 if( p_io_stack->Parameters.Power.State.DeviceState == PowerDeviceD0 && p_ext->b_hibernating)\r
2120                 {\r
2121                         /* Process in a work item - deregister_ca and HcaDeinit block. */\r
2122                         ASSERT( !p_ext->p_po_work_item );\r
2123                         p_ext->p_po_work_item = IoAllocateWorkItem( p_dev_obj );\r
2124                         if( !p_ext->p_po_work_item )\r
2125                                 status = STATUS_INSUFFICIENT_RESOURCES;\r
2126                         else {\r
2127                                 /* Process in work item callback. */\r
2128                                 IoMarkIrpPending( p_irp );\r
2129                                 IoQueueWorkItem(\r
2130                                         p_ext->p_po_work_item, __HibernateUpWorkItem, DelayedWorkQueue, p_irp );\r
2131                                 *p_action = IrpDoNothing;\r
2132                                 BUS_EXIT( BUS_DBG_POWER );\r
2133                                 return STATUS_PENDING;\r
2134                         }\r
2135                 }\r
2136 \r
2137                 /* Notify the power manager. */\r
2138                 p_ext->dev_po_state = p_io_stack->Parameters.Power.State;\r
2139                 PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
2140         }\r
2141 \r
2142         *p_action = IrpComplete;\r
2143         BUS_EXIT( BUS_DBG_POWER );\r
2144         return status;\r
2145 }\r
2146 \r
2147 \r
2148 \r