4e83785d16662a3c550603cd80990ca32f743658
[mirror/winof/.git] / core / bus / kernel / bus_iou_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 "al_ca.h"\r
39 #include "al_mgr.h"\r
40 #include "bus_pnp.h"\r
41 #include "bus_iou_mgr.h"\r
42 #include <initguid.h>\r
43 #include <wdmguid.h>\r
44 #include "iba/iou_ifc.h"\r
45 \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  * Size of device descriptions, in the format:\r
54  *      IBA\VxxxxxxPxxxxxxxxvxxxxxxxx\r
55  */\r
56 #define IOU_DEV_ID_SIZE         sizeof(L"IBA\\VxxxxxxPxxxxvxxxxxxxx")\r
57 #define IOU_DEV_ID_STRING1      L"IBA\\V%06xP%04hxv%08x"\r
58 #define IOU_DEV_ID_STRING2      L"IBA\\V%06xP%04hx"\r
59 #define IOU_HW_ID_SIZE          \\r
60         sizeof(L"IBA\\VxxxxxxPxxxxvxxxxxxxx\0IBA\\VxxxxxxPxxxx\0\0")\r
61 #define IOU_COMPAT_ID           L"IBA\\IB_IOU\0\0"\r
62 #define IOU_LOCATION_SIZE       \\r
63         sizeof(L"Chassis 0xxxxxxxxxxxxxxxxx, Slot xx")\r
64 \r
65 /*\r
66  * Device extension for IOU PDOs.\r
67  */\r
68 typedef struct _bus_iou_ext\r
69 {\r
70         bus_pdo_ext_t                   pdo;\r
71 \r
72         net64_t                                 chassis_guid;\r
73         uint8_t                                 slot;\r
74         net64_t                                 guid;\r
75         net32_t                                 vend_id;\r
76         net16_t                                 dev_id;\r
77         net32_t                                 revision;\r
78         char                                    desc[IB_NODE_DESCRIPTION_SIZE + 1];\r
79 \r
80         /* Number of references on the upper interface. */\r
81         atomic32_t                              n_ifc_ref;\r
82 \r
83 }       bus_iou_ext_t;\r
84 \r
85 \r
86 typedef struct _iou_pnp_context\r
87 {\r
88         bus_filter_t    *p_bus_filter;\r
89         void                    *p_pdo_ext;\r
90 \r
91 }       iou_pnp_ctx_t;\r
92 \r
93 \r
94 /*\r
95  * Function prototypes.\r
96  */\r
97 void\r
98 destroying_iou_mgr(\r
99         IN                              cl_obj_t*                                       p_obj );\r
100 \r
101 void\r
102 free_iou_mgr(\r
103         IN                              cl_obj_t*                                       p_obj );\r
104 \r
105 ib_api_status_t\r
106 bus_reg_iou_pnp(\r
107         IN                              bus_filter_t*                           p_bfi );\r
108 \r
109 ib_api_status_t\r
110 iou_mgr_pnp_cb(\r
111         IN                              ib_pnp_rec_t*                           p_pnp_rec );\r
112 \r
113 ib_api_status_t\r
114 iou_mgr_iou_add(\r
115         IN                              ib_pnp_iou_rec_t*                       p_pnp_rec );\r
116 \r
117 void\r
118 iou_mgr_iou_remove(\r
119         IN                              ib_pnp_iou_rec_t*                       p_pnp_rec );\r
120 \r
121 static NTSTATUS\r
122 iou_start(\r
123         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
124         IN                              IRP* const                                      p_irp, \r
125                 OUT                     cl_irp_action_t* const          p_action );\r
126 \r
127 static NTSTATUS\r
128 iou_query_remove(\r
129         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
130         IN                              IRP* const                                      p_irp, \r
131                 OUT                     cl_irp_action_t* const          p_action );\r
132 \r
133 static void\r
134 iou_release_resources(\r
135         IN                              DEVICE_OBJECT* const            p_dev_obj );\r
136 \r
137 static NTSTATUS\r
138 iou_remove(\r
139         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
140         IN                              IRP* const                                      p_irp, \r
141                 OUT                     cl_irp_action_t* const          p_action );\r
142 \r
143 static NTSTATUS\r
144 iou_surprise_remove(\r
145         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
146         IN                              IRP* const                                      p_irp, \r
147                 OUT                     cl_irp_action_t* const          p_action );\r
148 \r
149 static NTSTATUS\r
150 iou_query_capabilities(\r
151         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
152         IN                              IRP* const                                      p_irp, \r
153                 OUT                     cl_irp_action_t* const          p_action );\r
154 \r
155 static NTSTATUS\r
156 iou_query_target_relations(\r
157         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
158         IN                              IRP* const                                      p_irp, \r
159                 OUT                     cl_irp_action_t* const          p_action );\r
160 \r
161 static NTSTATUS\r
162 iou_query_device_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 iou_query_hardware_ids(\r
168         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
169         IN                              IRP* const                                      p_irp );\r
170 \r
171 static NTSTATUS\r
172 iou_query_compatible_ids(\r
173         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
174         IN                              IRP* const                                      p_irp );\r
175 \r
176 static NTSTATUS\r
177 iou_query_unique_id(\r
178         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
179         IN                              IRP* const                                      p_irp );\r
180 \r
181 static NTSTATUS\r
182 iou_query_description(\r
183         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
184         IN                              IRP* const                                      p_irp );\r
185 \r
186 static NTSTATUS\r
187 iou_query_location(\r
188         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
189         IN                              IRP* const                                      p_irp );\r
190 \r
191 static NTSTATUS\r
192 iou_query_bus_info(\r
193         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
194         IN                              IRP* const                                      p_irp, \r
195                 OUT                     cl_irp_action_t* const          p_action );\r
196 \r
197 static NTSTATUS\r
198 iou_query_interface(\r
199         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
200         IN                              IRP* const                                      p_irp, \r
201                 OUT                     cl_irp_action_t* const          p_action );\r
202 \r
203 static NTSTATUS\r
204 iou_set_power(\r
205         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
206         IN                              IRP* const                                      p_irp,\r
207                 OUT                     cl_irp_action_t* const          p_action );\r
208 \r
209 \r
210 \r
211 /*\r
212  * Global virtual function pointer tables shared between all \r
213  * instances of Port PDOs.\r
214  */\r
215 static const cl_vfptr_pnp_po_t          vfptr_iou_pnp = {\r
216         "IB IOU",\r
217         iou_start,\r
218         cl_irp_succeed,\r
219         cl_irp_succeed,\r
220         cl_irp_succeed,\r
221         iou_query_remove,\r
222         iou_release_resources,\r
223         iou_remove,\r
224         cl_irp_succeed,\r
225         iou_surprise_remove,\r
226         iou_query_capabilities,\r
227         cl_irp_complete,\r
228         cl_irp_complete,\r
229         cl_irp_succeed,\r
230         cl_irp_complete,\r
231         cl_irp_complete,\r
232         cl_irp_complete,\r
233         iou_query_target_relations,\r
234         cl_irp_complete,\r
235         cl_irp_complete,\r
236         cl_irp_complete,\r
237         iou_query_bus_info,\r
238         iou_query_interface,\r
239         cl_irp_complete,\r
240         cl_irp_complete,\r
241         cl_irp_complete,\r
242         cl_irp_complete,\r
243         cl_irp_succeed,                         // QueryPower\r
244         iou_set_power,                          // SetPower\r
245         cl_irp_unsupported,                     // PowerSequence\r
246         cl_irp_unsupported                      // WaitWake\r
247 };\r
248 \r
249 \r
250 static const cl_vfptr_query_txt_t               vfptr_iou_query_txt = {\r
251         iou_query_device_id,\r
252         iou_query_hardware_ids,\r
253         iou_query_compatible_ids,\r
254         iou_query_unique_id,\r
255         iou_query_description,\r
256         iou_query_location\r
257 };\r
258 \r
259 \r
260 /*\r
261  * Create the AL load service.\r
262  */\r
263 ib_api_status_t\r
264 create_iou_mgr(\r
265         IN                              bus_filter_t*                           p_bfi,\r
266                 OUT                     iou_mgr_t** const                       pp_iou_mgr )\r
267 {\r
268         ib_api_status_t         status;\r
269         cl_status_t                     cl_status;\r
270         iou_mgr_t                       *gp_iou_mgr;\r
271 \r
272         BUS_ENTER( BUS_DBG_PNP );\r
273 \r
274         CL_ASSERT( p_bfi->p_iou_mgr == NULL );\r
275 \r
276         gp_iou_mgr = cl_zalloc( sizeof(iou_mgr_t) );\r
277         if( !gp_iou_mgr )\r
278         {\r
279                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
280                         ("Failed to allocate IOU manager.\n") );\r
281                 return IB_INSUFFICIENT_MEMORY;\r
282         }\r
283         p_bfi->p_iou_mgr = gp_iou_mgr;\r
284 \r
285         /* Construct the load service. */\r
286         cl_obj_construct( &gp_iou_mgr->obj, AL_OBJ_TYPE_LOADER );\r
287         p_bfi->p_iou_mgr_obj = &p_bfi->p_iou_mgr->obj; // save for destroy & free\r
288         cl_mutex_construct( &gp_iou_mgr->pdo_mutex );\r
289         cl_qlist_init( &gp_iou_mgr->iou_list );\r
290 \r
291         cl_status = cl_mutex_init( &gp_iou_mgr->pdo_mutex );\r
292         if( cl_status != CL_SUCCESS )\r
293         {\r
294                 free_iou_mgr( &gp_iou_mgr->obj );\r
295                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
296                         ("cl_mutex_init returned %#x.\n", cl_status) );\r
297                 return ib_convert_cl_status( cl_status );\r
298         }\r
299 \r
300         /* Initialize the load service object. */\r
301         cl_status = cl_obj_init( &gp_iou_mgr->obj, CL_DESTROY_SYNC,\r
302                                                          destroying_iou_mgr, NULL, free_iou_mgr );\r
303         if( cl_status != CL_SUCCESS )\r
304         {\r
305                 free_iou_mgr( &gp_iou_mgr->obj );\r
306                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
307                         ("cl_obj_init returned %#x.\n", cl_status) );\r
308                 return ib_convert_cl_status( cl_status );\r
309         }\r
310 \r
311         /* Register for IOU PnP events. */\r
312         status = bus_reg_iou_pnp( p_bfi );\r
313         if( status != IB_SUCCESS )\r
314         {\r
315                 free_iou_mgr( &gp_iou_mgr->obj );\r
316                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
317                         ("bus_reg_iou_pnp returned %s.\n", ib_get_err_str(status)) );\r
318                 return status;\r
319         }\r
320 \r
321         *pp_iou_mgr = gp_iou_mgr;\r
322 \r
323         BUS_EXIT( BUS_DBG_PNP );\r
324         return IB_SUCCESS;\r
325 }\r
326 \r
327 \r
328 /*\r
329  * Pre-destroy the load service.\r
330  */\r
331 void\r
332 destroying_iou_mgr(\r
333         IN                              cl_obj_t*                                       p_obj )\r
334 {\r
335         ib_api_status_t                 status;\r
336         bus_filter_t                    *p_bfi;\r
337         iou_mgr_t                               *gp_iou_mgr;\r
338 \r
339         BUS_ENTER( BUS_DBG_PNP );\r
340 \r
341         CL_ASSERT( p_obj );\r
342         p_bfi = get_bfi_by_obj( BFI_IOU_MGR_OBJ, p_obj );\r
343         if (p_bfi == NULL) {\r
344                 BUS_PRINT(BUS_DBG_PNP, ("Failed to find p_bfi by obj %p?\n", p_obj));\r
345                 return;\r
346         }\r
347         gp_iou_mgr = p_bfi->p_iou_mgr;\r
348 \r
349         BUS_PRINT(BUS_DBG_PNP, ("%s obj %p port_mgr %p\n",\r
350                                                         p_bfi->whoami, p_obj, gp_iou_mgr));\r
351 \r
352         CL_ASSERT( gp_iou_mgr == PARENT_STRUCT( p_obj, iou_mgr_t, obj ) );\r
353 \r
354         /* Deregister for iou PnP events. */\r
355         if( get_bfi_count() == 1 && bus_globals.h_pnp_iou )\r
356         {\r
357                 status = ib_dereg_pnp( bus_globals.h_pnp_iou, NULL );\r
358                 bus_globals.h_pnp_iou = NULL;\r
359                 CL_ASSERT( status == IB_SUCCESS );\r
360         }\r
361         cl_obj_deref( p_bfi->p_iou_mgr_obj );\r
362 \r
363         BUS_EXIT( BUS_DBG_PNP );\r
364 }\r
365 \r
366 \r
367 /*\r
368  * Free the load service.\r
369  */\r
370 void\r
371 free_iou_mgr(\r
372         IN                              cl_obj_t*                                       p_obj )\r
373 {\r
374         bus_pdo_ext_t   *p_ext;\r
375         cl_list_item_t  *p_list_item;\r
376         bus_filter_t    *p_bfi;\r
377         iou_mgr_t               *gp_iou_mgr;\r
378 \r
379         BUS_ENTER( BUS_DBG_PNP );\r
380 \r
381         CL_ASSERT( p_obj );\r
382         p_bfi = get_bfi_by_obj( BFI_IOU_MGR_OBJ, p_obj );\r
383         if ( p_bfi == NULL ) {\r
384                 BUS_PRINT( BUS_DBG_PNP, ("Unable to get p_bfi iou_obj %p?\n", p_obj) );\r
385                 return;\r
386         }\r
387         gp_iou_mgr = p_bfi->p_iou_mgr;\r
388         if ( !gp_iou_mgr ) {\r
389                 // if create fails & then free is called, p_bfi->p_iou_mgr == NULL\r
390                 return;\r
391         }\r
392 \r
393         CL_ASSERT( gp_iou_mgr == PARENT_STRUCT( p_obj, iou_mgr_t, obj ) );\r
394 \r
395         BUS_PRINT( BUS_DBG_PNP, ("%s Mark all IOU PDOs as no longer present\n",\r
396                                                                 p_bfi->whoami));\r
397         /*\r
398          * Mark all IOU PDOs as no longer present.  This will cause them\r
399          * to be removed when they process the IRP_MN_REMOVE_DEVICE.\r
400          */\r
401         p_list_item = cl_qlist_remove_head( &gp_iou_mgr->iou_list );\r
402         while( p_list_item != cl_qlist_end( &gp_iou_mgr->iou_list ) )\r
403         {\r
404                 p_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
405                 p_list_item = cl_qlist_remove_head( &gp_iou_mgr->iou_list );\r
406                 if( p_ext->cl_ext.pnp_state == SurpriseRemoved )\r
407                 {\r
408                         CL_ASSERT( !p_ext->b_present );\r
409                         p_ext->b_reported_missing = TRUE;\r
410                         BUS_TRACE( BUS_DBG_PNP, ("%s %s: ext %p, present %d, missing %d\n",\r
411                                                 p_bfi->whoami,\r
412                                                 p_ext->cl_ext.vfptr_pnp_po->identity, p_ext,\r
413                                                 p_ext->b_present, p_ext->b_reported_missing ) );\r
414                         continue;\r
415                 }\r
416                 if( p_ext->h_ca )\r
417                 {\r
418                         /* Invalidate bus relations for the HCA. */\r
419                         IoInvalidateDeviceRelations(\r
420                                 p_ext->h_ca->obj.p_ci_ca->verbs.p_hca_dev, BusRelations );\r
421 \r
422                         /* Release the reference on the CA object. */\r
423                         deref_al_obj( &p_ext->h_ca->obj );\r
424                 }\r
425                 BUS_TRACE( BUS_DBG_PNP, ("Deleted device %s %s: PDO %p, ext %p\n",\r
426                                         p_bfi->whoami, p_ext->cl_ext.vfptr_pnp_po->identity,\r
427                                         p_ext->cl_ext.p_self_do, p_ext ) );\r
428 \r
429                 BUS_TRACE( BUS_DBG_PNP,("%s p_ext->h_ca->obj.state %d ref_cnt %d\n",\r
430                                         p_bfi->whoami,\r
431                                         p_ext->h_ca->obj.state,\r
432                                         p_ext->h_ca->obj.ref_cnt));\r
433 \r
434                 IoDeleteDevice( p_ext->cl_ext.p_self_do );\r
435         }\r
436 \r
437         cl_mutex_destroy( &gp_iou_mgr->pdo_mutex );\r
438         cl_obj_deinit( p_obj );\r
439         cl_free( gp_iou_mgr );\r
440 \r
441         p_bfi->p_iou_mgr = NULL;\r
442         p_bfi->p_iou_mgr_obj = NULL;\r
443 \r
444         BUS_EXIT( BUS_DBG_PNP );\r
445 }\r
446 \r
447 \r
448 /*\r
449  * Register the load service for the given PnP class events.\r
450  */\r
451 ib_api_status_t\r
452 bus_reg_iou_pnp( IN bus_filter_t *p_bfi )\r
453 {\r
454         ib_pnp_req_t                    pnp_req;\r
455         ib_api_status_t                 status = IB_SUCCESS;\r
456         boolean_t                               need_pnp_reg = FALSE;\r
457 \r
458         /* only need to register for IOU PNP events once.\r
459          * do not hold mutex over pnp_reg() call as callback which needs mutex\r
460          * could occur.\r
461          */\r
462         if ( !bus_globals.h_pnp_iou )\r
463         {\r
464                 lock_control_event();\r
465                 if ( !bus_globals.h_pnp_iou )\r
466                 {\r
467                         bus_globals.h_pnp_iou = (ib_pnp_handle_t)1; /* block others */ \r
468                         need_pnp_reg = TRUE;\r
469                 }\r
470                 unlock_control_event();\r
471 \r
472                 if ( need_pnp_reg )\r
473                 {\r
474                         cl_memclr( &pnp_req, sizeof( ib_pnp_req_t ) );\r
475                         pnp_req.pnp_class       = IB_PNP_IOU | IB_PNP_FLAG_REG_SYNC;\r
476                         pnp_req.pnp_context = NULL;\r
477                         pnp_req.pfn_pnp_cb      = iou_mgr_pnp_cb;\r
478 \r
479                         status = ib_reg_pnp( gh_al, &pnp_req, &bus_globals.h_pnp_iou );\r
480                 }\r
481         }\r
482 \r
483         if ( status == IB_SUCCESS )\r
484         {\r
485                 /* Reference the load service on behalf of the ib_reg_pnp call. */\r
486                 cl_obj_ref( &p_bfi->p_iou_mgr->obj );\r
487         }\r
488 \r
489         return status;\r
490 }\r
491 \r
492 \r
493 /*\r
494  * Load service PnP event callback.\r
495  */\r
496 ib_api_status_t\r
497 iou_mgr_pnp_cb(\r
498         IN                              ib_pnp_rec_t*                           p_pnp_rec )\r
499 {\r
500         ib_api_status_t         status=IB_SUCCESS;\r
501 \r
502         BUS_ENTER( BUS_DBG_PNP );\r
503 \r
504         CL_ASSERT( p_pnp_rec );\r
505 \r
506         switch( p_pnp_rec->pnp_event )\r
507         {\r
508         case IB_PNP_IOU_ADD:\r
509                 status = iou_mgr_iou_add( (ib_pnp_iou_rec_t*)p_pnp_rec );\r
510                 break;\r
511 \r
512         case IB_PNP_IOU_REMOVE:\r
513                 iou_mgr_iou_remove( (ib_pnp_iou_rec_t*)p_pnp_rec );\r
514                 break;\r
515 \r
516         default:\r
517                 BUS_PRINT( BUS_DBG_PNP, ("Unhandled PNP Event %s\n",\r
518                                         ib_get_pnp_event_str(p_pnp_rec->pnp_event) ));\r
519                 break;\r
520         }\r
521         BUS_EXIT( BUS_DBG_PNP );\r
522         return status;\r
523 }\r
524 \r
525 \r
526 /*\r
527  * Called to get child relations for the bus root.\r
528  */\r
529 NTSTATUS\r
530 iou_mgr_get_bus_relations(\r
531         IN              const   net64_t                                         ca_guid,\r
532         IN                              IRP* const                                      p_irp )\r
533 {\r
534         NTSTATUS                        status;\r
535         bus_filter_t            *p_bfi;\r
536         iou_mgr_t                       *gp_iou_mgr;\r
537         DEVICE_RELATIONS        *p_rel;\r
538 \r
539         BUS_ENTER( BUS_DBG_PNP );\r
540 \r
541         BUS_PRINT(BUS_DBG_PNP, ("CA_guid %I64x\n",ca_guid));\r
542 \r
543         /* special case guid == 0 - walk all bus filter instances */\r
544         if ( ca_guid == 0ULL ) {\r
545                 for(p_bfi=g_bus_filters; p_bfi < &g_bus_filters[MAX_BUS_FILTERS]; p_bfi++) {\r
546                         gp_iou_mgr = p_bfi->p_iou_mgr;\r
547                         if ( !gp_iou_mgr )\r
548                                 continue;\r
549                         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
550                         status = bus_get_relations( &gp_iou_mgr->iou_list, ca_guid, p_irp );\r
551                         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
552                 }\r
553                 p_rel = (DEVICE_RELATIONS*)p_irp->IoStatus.Information;\r
554                 if ( p_rel ) {\r
555                         BUS_PRINT(BUS_DBG_PNP, ("CA_guid 0 Reports %d\n", p_rel->Count));\r
556                 }\r
557                 BUS_EXIT( BUS_DBG_PNP );\r
558                 return STATUS_SUCCESS;\r
559         }\r
560 \r
561         p_bfi = get_bfi_by_ca_guid(ca_guid);\r
562         if (p_bfi == NULL) {\r
563                 BUS_TRACE_EXIT(BUS_DBG_PNP,\r
564                                                                 ("NULL p_bfi from ca_guid %I64x ?\n",ca_guid));\r
565                 return STATUS_UNSUCCESSFUL;\r
566         }\r
567         gp_iou_mgr = p_bfi->p_iou_mgr;\r
568 \r
569         BUS_PRINT(BUS_DBG_PNP, ("%s for ca_guid %I64x iou_mgr %p\n",\r
570                                                         p_bfi->whoami, ca_guid, gp_iou_mgr) );\r
571         if (!gp_iou_mgr)\r
572                 return STATUS_NO_SUCH_DEVICE;\r
573 \r
574         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
575         status = bus_get_relations( &gp_iou_mgr->iou_list, ca_guid, p_irp );\r
576         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
577 \r
578         BUS_EXIT( BUS_DBG_PNP );\r
579         return status;\r
580 }\r
581 \r
582 \r
583 static ib_api_status_t\r
584 __iou_was_hibernated(\r
585         IN                              ib_pnp_iou_rec_t*                       p_pnp_rec,\r
586         IN                              bus_filter_t*                           p_bfi )\r
587 {\r
588         NTSTATUS                status;\r
589         cl_list_item_t  *p_list_item;\r
590         bus_iou_ext_t   *p_iou_ext;\r
591         bus_pdo_ext_t   *p_pdo_ext = NULL;\r
592         size_t                  n_devs = 0;\r
593         iou_mgr_t               *gp_iou_mgr = p_bfi->p_iou_mgr;\r
594         cl_qlist_t              *p_pdo_list = &gp_iou_mgr->iou_list;\r
595         iou_pnp_ctx_t   *p_ctx = p_pnp_rec->pnp_rec.context;\r
596 \r
597         BUS_ENTER( BUS_DBG_PNP );\r
598         \r
599         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
600         \r
601         /* Count the number of child devices. */\r
602         for( p_list_item = cl_qlist_head( p_pdo_list );\r
603                 p_list_item != cl_qlist_end( p_pdo_list );\r
604                 p_list_item = cl_qlist_next( p_list_item ) )\r
605         {\r
606                 p_pdo_ext = PARENT_STRUCT( p_list_item, bus_pdo_ext_t, list_item );\r
607                 p_iou_ext = (bus_iou_ext_t*)p_pdo_ext;\r
608 \r
609                 /* TODO: maybe we need more search patterns like vend_id, dev_id ... */\r
610                 if( p_pdo_ext->b_present && p_pdo_ext->b_hibernating &&\r
611                         (p_iou_ext->guid == p_pnp_rec->pnp_rec.guid) )\r
612                 {\r
613                         n_devs++;\r
614                         break;\r
615                 }\r
616 \r
617                 BUS_TRACE( BUS_DBG_PNP, ("%s Skipped PDO for %s: PDO %p, ext %p, "\r
618                         "present %d, missing %d, hibernating %d, port_guid %I64x.\n",\r
619                         p_bfi->whoami,\r
620                         p_pdo_ext->cl_ext.vfptr_pnp_po->identity,\r
621                         p_pdo_ext->cl_ext.p_self_do, \r
622                         p_pdo_ext, p_pdo_ext->b_present, p_pdo_ext->b_reported_missing, \r
623                         p_pdo_ext->b_hibernating, p_iou_ext->guid ) );\r
624         }\r
625 \r
626         if (n_devs)\r
627         {\r
628                 /* Take a reference on the parent HCA. */\r
629                 p_pdo_ext->h_ca = acquire_ca( p_pnp_rec->ca_guid );\r
630                 if( !p_pdo_ext->h_ca )\r
631                 {\r
632                         BUS_TRACE( BUS_DBG_ERROR,\r
633                                 ("acquire_ca failed to find CA by guid %I64x\n",\r
634                                 p_pnp_rec->ca_guid ) );\r
635                         status = IB_INVALID_GUID;\r
636                 }\r
637                 else \r
638                 {\r
639                         p_pdo_ext->b_hibernating = FALSE;\r
640                         p_ctx->p_pdo_ext = p_pdo_ext; // for iou_mgr_iou_remove()\r
641 \r
642                         status = IB_SUCCESS;\r
643                         p_iou_ext = (bus_iou_ext_t*)p_pdo_ext;\r
644                         BUS_TRACE( BUS_DBG_PNP, ("%s Found PDO for %s: PDO %p, ext %p, "\r
645                                 "present %d, missing %d, hibernating %d, port_guid %I64x.\n",\r
646                                 p_bfi->whoami,\r
647                                 p_pdo_ext->cl_ext.vfptr_pnp_po->identity,\r
648                                 p_pdo_ext->cl_ext.p_self_do, \r
649                                 p_pdo_ext, p_pdo_ext->b_present, p_pdo_ext->b_reported_missing, \r
650                                 p_pdo_ext->b_hibernating, p_iou_ext->guid ) );\r
651                 }\r
652         }\r
653         else \r
654         {\r
655                 BUS_TRACE( BUS_DBG_PNP, ("%s Failed to find PDO for guid  %I64x .\n",\r
656                                         p_bfi->whoami, p_pnp_rec->pnp_rec.guid ) );\r
657                 status = IB_NOT_FOUND;\r
658         }\r
659 \r
660         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
661 \r
662         BUS_EXIT( BUS_DBG_PNP );\r
663         return status;\r
664 }\r
665 \r
666 ib_api_status_t\r
667 iou_mgr_iou_add(\r
668         IN                              ib_pnp_iou_rec_t*                       p_pnp_rec )\r
669 {\r
670         NTSTATUS                status;\r
671         DEVICE_OBJECT   *p_pdo;\r
672         bus_iou_ext_t   *p_iou_ext;\r
673         bus_filter_t    *p_bfi;\r
674         iou_mgr_t               *gp_iou_mgr;\r
675         iou_pnp_ctx_t   *p_ctx = p_pnp_rec->pnp_rec.context;\r
676 \r
677         BUS_ENTER( BUS_DBG_PNP );\r
678 \r
679         p_bfi = get_bfi_by_ca_guid( p_pnp_rec->ca_guid );\r
680         if ( !p_bfi ) {\r
681                 BUS_TRACE_EXIT( BUS_DBG_PNP,("NULL p_bfi? ca_guid 0x%I64x\n",\r
682                                                                         p_pnp_rec->ca_guid ) );\r
683                 return IB_ERROR;\r
684         }\r
685 \r
686         if ( !p_ctx ) {\r
687                 /*\r
688                  * Allocate a PNP context for this object. pnp_rec.context is object\r
689                  * unique.\r
690                  */\r
691                 p_ctx = cl_zalloc( sizeof(*p_ctx) );\r
692                 if( !p_ctx )\r
693                 {\r
694                         BUS_TRACE_EXIT(BUS_DBG_PNP, ("%s ca_guid %I64x iou_guid(%I64x) "\r
695                                         "BAD alloc for PNP context\n", p_bfi->whoami,\r
696                                         p_bfi->ca_guid, p_pnp_rec->guid ));\r
697 \r
698                         return IB_ERROR;\r
699                 }\r
700                 p_ctx->p_bus_filter = p_bfi;\r
701                 p_pnp_rec->pnp_rec.context = p_ctx;\r
702 \r
703                 BUS_PRINT(BUS_DBG_PNP,\r
704                                         ("%s ca_guid %I64x iou_guid(%I64x) ALLOC p_ctx @ %p\n",\r
705                                         p_bfi->whoami, p_bfi->ca_guid, p_pnp_rec->guid,p_ctx));\r
706         }\r
707         gp_iou_mgr = p_bfi->p_iou_mgr;\r
708 \r
709         /* Upon hibernating the computer IB_BUS driver doesn't remove PDO, but\r
710            marks with a flag. So we first try to find an existing PDO for this port,\r
711            marked with this flag. If it was found, we turn off the flag and use\r
712            this PDO */\r
713         status = __iou_was_hibernated( p_pnp_rec, p_bfi );\r
714         if( status != IB_NOT_FOUND )\r
715         {\r
716                 BUS_EXIT( BUS_DBG_PNP );\r
717                 return status;\r
718         }\r
719 \r
720         /* Create the PDO for the new port device. */\r
721         status = IoCreateDevice( bus_globals.p_driver_obj, sizeof(bus_iou_ext_t),\r
722                                                          NULL, FILE_DEVICE_CONTROLLER, \r
723                                                          FILE_DEVICE_SECURE_OPEN |\r
724                                                                                         FILE_AUTOGENERATED_DEVICE_NAME,\r
725                                                          FALSE, &p_pdo );\r
726 \r
727         if( !NT_SUCCESS( status ) )\r
728         {\r
729                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
730                         ("IoCreateDevice returned %08x.\n", status) );\r
731                 return IB_ERROR;\r
732         }\r
733 \r
734         /* Initialize the device extension. */\r
735         cl_init_pnp_po_ext( p_pdo, NULL, p_pdo, bus_globals.dbg_lvl,\r
736                                                 &vfptr_iou_pnp, &vfptr_iou_query_txt );\r
737 \r
738         /* Set the DO_BUS_ENUMERATED_DEVICE flag to mark it as a PDO. */\r
739         p_pdo->Flags |= DO_BUS_ENUMERATED_DEVICE;\r
740         \r
741         p_iou_ext = p_pdo->DeviceExtension;\r
742         p_iou_ext->pdo.dev_po_state.DeviceState = PowerDeviceD0;\r
743         p_iou_ext->pdo.p_parent_ext = p_bfi->p_bus_ext;\r
744         p_iou_ext->pdo.b_present = TRUE;\r
745         p_iou_ext->pdo.b_reported_missing = FALSE;\r
746         BUS_TRACE( BUS_DBG_PNP, ("%s: ext %p, present %d, missing %d .\n",\r
747                 p_iou_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_iou_ext,\r
748                 p_iou_ext->pdo.b_present, p_iou_ext->pdo.b_reported_missing ) );\r
749 \r
750         p_iou_ext->guid = p_pnp_rec->guid;\r
751         p_iou_ext->chassis_guid = p_pnp_rec->chassis_guid;\r
752         p_iou_ext->slot = p_pnp_rec->slot;\r
753         p_iou_ext->vend_id = cl_ntoh32( p_pnp_rec->vend_id );\r
754         if( p_iou_ext->vend_id == 0x00066a )\r
755                 p_iou_ext->dev_id = (net16_t)(p_pnp_rec->pnp_rec.guid >> 32) & 0x00FF;\r
756         else\r
757                 p_iou_ext->dev_id = cl_ntoh16( p_pnp_rec->dev_id );\r
758         p_iou_ext->revision = cl_ntoh32( p_pnp_rec->revision );\r
759         cl_memcpy( p_iou_ext->desc, p_pnp_rec->desc,\r
760                 IB_NODE_DESCRIPTION_SIZE + 1 );\r
761         p_iou_ext->n_ifc_ref = 0;\r
762 \r
763         /* Cache the CA GUID. */\r
764         p_iou_ext->pdo.ca_guid = p_pnp_rec->ca_guid;\r
765 \r
766         /* Take a reference on the parent HCA. */\r
767         p_iou_ext->pdo.h_ca = acquire_ca( p_pnp_rec->ca_guid );\r
768         if( !p_iou_ext->pdo.h_ca )\r
769         {\r
770                 IoDeleteDevice( p_pdo );\r
771                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("acquire_ca failed to find CA.\n") );\r
772                 return IB_INVALID_GUID;\r
773         }\r
774 \r
775         /* Store the device extension in the PDO list for future queries. */\r
776         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
777         cl_qlist_insert_tail( &gp_iou_mgr->iou_list, &p_iou_ext->pdo.list_item );\r
778         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
779 \r
780         /*\r
781          * Set the context of the PNP event.  The context is passed in for future\r
782          * events on the same port.\r
783          */\r
784         /* if not set in iou_was_hibernated(), set now */\r
785         if ( !p_ctx->p_pdo_ext )\r
786                 p_ctx->p_pdo_ext = p_iou_ext;\r
787 \r
788         /* Tell the PnP Manager to rescan for the HCA's bus relations. */\r
789         IoInvalidateDeviceRelations(\r
790                         p_iou_ext->pdo.h_ca->obj.p_ci_ca->verbs.p_hca_dev, BusRelations );\r
791 \r
792         /* Invalidate removal relations for the bus driver. */\r
793         IoInvalidateDeviceRelations(\r
794                 p_bfi->p_bus_ext->cl_ext.p_pdo, RemovalRelations );\r
795 \r
796         BUS_EXIT( BUS_DBG_PNP );\r
797 \r
798         return IB_SUCCESS;\r
799 }\r
800 \r
801 \r
802 void\r
803 iou_mgr_iou_remove(\r
804         IN                              ib_pnp_iou_rec_t*                       p_pnp_rec )\r
805 {\r
806         bus_pdo_ext_t   *p_ext;\r
807         iou_mgr_t               *gp_iou_mgr;\r
808         bus_filter_t    *p_bfi;\r
809         iou_pnp_ctx_t   *p_ctx = p_pnp_rec->pnp_rec.context;\r
810 \r
811         BUS_ENTER( BUS_DBG_PNP );\r
812 \r
813         if ( !p_ctx ) {\r
814                 BUS_EXIT( BUS_DBG_PNP );\r
815                 return;\r
816         }\r
817 \r
818         CL_ASSERT( p_ctx->p_bus_filter->magic == BFI_MAGIC );\r
819         p_bfi = p_ctx->p_bus_filter;\r
820         CL_ASSERT( p_bfi );\r
821 \r
822         BUS_PRINT(BUS_DBG_PNP,("%s ca_guid 0x%I64x iou_mgr %p\n",\r
823                                 p_bfi->whoami, p_bfi->ca_guid, p_bfi->p_iou_mgr));\r
824 \r
825         /* fdo_release_resources() has destroyed the IOU mgr, all that needs to be\r
826          * done is cleanup the PNP IOU context; one per port.\r
827          */\r
828         if ( p_bfi->ca_guid == 0ULL || !p_bfi->p_iou_mgr ) {\r
829                 cl_free( p_ctx );\r
830                 p_pnp_rec->pnp_rec.context = NULL;\r
831                 BUS_EXIT( BUS_DBG_PNP );\r
832                 return;\r
833         }\r
834 \r
835         gp_iou_mgr = p_bfi->p_iou_mgr;\r
836 \r
837         /* Within the PNP record's context is the IOU extension; see\r
838          * was_hibernated().\r
839          */\r
840         p_ext = p_ctx->p_pdo_ext;\r
841         CL_ASSERT( p_ext );\r
842 \r
843         if (p_bfi != p_ext->p_parent_ext->bus_filter) {\r
844                 BUS_PRINT(BUS_DBG_PNP,\r
845                         ("p_bfi(%p) != p_ext->bus_filter(%p) line %d file %s\n",\r
846                         p_bfi,p_ext->p_parent_ext->bus_filter, __LINE__,__FILE__));\r
847                 CL_ASSERT (p_bfi == p_ext->p_parent_ext->bus_filter);\r
848         }\r
849 \r
850         /*\r
851          * Flag the port PDO as no longer being present.  We have to wait until\r
852          * the PnP manager removes it to clean up.  However, we do release the\r
853          * reference on the CA object in order to allow the removal of the HCA\r
854          * to proceed should it occur before the port's PDO is cleaned up.\r
855          */\r
856         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
857         if ( !p_ext->h_ca )\r
858         {\r
859                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("NULL h_ca? p_ext %p\n", p_ext ) );\r
860                 return;\r
861         }\r
862 \r
863         if( p_ext->b_hibernating )\r
864         {\r
865                 BUS_TRACE( BUS_DBG_PNP, ("%s Skip port removing for %s: PDO %p ext %p "\r
866                         "present %d missing %d hibernating %d\n",\r
867                         p_bfi->whoami,\r
868                         p_ext->cl_ext.vfptr_pnp_po->identity, p_ext->cl_ext.p_self_do,\r
869                         p_ext, p_ext->b_present, \r
870                         p_ext->b_reported_missing, p_ext->b_hibernating ) );\r
871                 goto hca_deref;\r
872         }\r
873 \r
874         p_ext->b_present = FALSE;\r
875         p_ext->b_reported_missing = TRUE;\r
876 \r
877         BUS_TRACE( BUS_DBG_PNP, ("%s %s: ext %p, present %d, missing %d .\n",\r
878                                 p_bfi->whoami,\r
879                                 p_ext->cl_ext.vfptr_pnp_po->identity, p_ext, p_ext->b_present,\r
880                                 p_ext->b_reported_missing ) );\r
881 \r
882         /* Invalidate removal relations for the bus driver. */\r
883         IoInvalidateDeviceRelations(\r
884                 p_bfi->p_bus_ext->cl_ext.p_pdo, RemovalRelations );\r
885 \r
886         /* Invalidate bus relations for the HCA. */\r
887         IoInvalidateDeviceRelations(\r
888                 p_ext->h_ca->obj.p_ci_ca->verbs.p_hca_dev, BusRelations );\r
889 \r
890         /* free PNP context */\r
891         cl_free( p_ctx );\r
892         p_pnp_rec->pnp_rec.context = NULL;\r
893 \r
894 hca_deref:\r
895         deref_al_obj( &p_ext->h_ca->obj );\r
896         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
897 \r
898         BUS_EXIT( BUS_DBG_PNP );\r
899 }\r
900 \r
901 \r
902 static NTSTATUS\r
903 iou_start(\r
904         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
905         IN                                      IRP* const                              p_irp, \r
906                 OUT                             cl_irp_action_t* const  p_action )\r
907 {\r
908         bus_pdo_ext_t   *p_ext;\r
909 \r
910         BUS_ENTER( BUS_DBG_PNP );\r
911 \r
912         UNUSED_PARAM( p_irp );\r
913 \r
914         p_ext = p_dev_obj->DeviceExtension;\r
915 \r
916         /* Notify the Power Manager that the device is started. */\r
917         PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
918 \r
919         *p_action = IrpComplete;\r
920         BUS_EXIT( BUS_DBG_PNP );\r
921         return STATUS_SUCCESS;\r
922 }\r
923 \r
924 \r
925 static NTSTATUS\r
926 iou_query_remove(\r
927         IN                              DEVICE_OBJECT* const    p_dev_obj,\r
928         IN                              IRP* const                              p_irp, \r
929                 OUT                     cl_irp_action_t* const  p_action )\r
930 {\r
931         bus_iou_ext_t   *p_ext;\r
932 \r
933         BUS_ENTER( BUS_DBG_PNP );\r
934 \r
935         UNUSED_PARAM( p_irp );\r
936 \r
937         p_ext = p_dev_obj->DeviceExtension;\r
938 \r
939         *p_action = IrpComplete;\r
940         if( p_ext->n_ifc_ref )\r
941         {\r
942                 /*\r
943                  * Our interface is still being held by someone.\r
944                  * Rollback the PnP state that was changed in the complib handler.\r
945                  */\r
946                 cl_rollback_pnp_state( &p_ext->pdo.cl_ext );\r
947 \r
948                 /* Fail the query. */\r
949                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Failing IRP_MN_QUERY_REMOVE_DEVICE:\n"\r
950                         "\tInterface has %d reference\n", p_ext->n_ifc_ref ) );\r
951                 return STATUS_UNSUCCESSFUL;\r
952         }\r
953 \r
954         BUS_EXIT( BUS_DBG_PNP );\r
955         return STATUS_SUCCESS;\r
956 }\r
957 \r
958 \r
959 static void\r
960 iou_release_resources(\r
961         IN                                      DEVICE_OBJECT* const    p_dev_obj )\r
962 {\r
963         bus_iou_ext_t   *p_ext;\r
964         POWER_STATE             po_state;\r
965         iou_mgr_t               *gp_iou_mgr;\r
966 \r
967         BUS_ENTER( BUS_DBG_PNP );\r
968 \r
969         p_ext = p_dev_obj->DeviceExtension;\r
970         gp_iou_mgr = p_ext->pdo.p_parent_ext->bus_filter->p_iou_mgr;\r
971 \r
972         /* Remove this PDO from its list. */\r
973         cl_mutex_acquire( &gp_iou_mgr->pdo_mutex );\r
974         BUS_TRACE( BUS_DBG_PNP, ("Removing IOU from list.\n") );\r
975         cl_qlist_remove_item( &gp_iou_mgr->iou_list, &p_ext->pdo.list_item );\r
976         cl_mutex_release( &gp_iou_mgr->pdo_mutex );\r
977         po_state.DeviceState = PowerDeviceD3;\r
978         PoSetPowerState( p_ext->pdo.cl_ext.p_pdo, DevicePowerState, po_state );\r
979 \r
980         BUS_EXIT( BUS_DBG_PNP );\r
981 }\r
982 \r
983 \r
984 static NTSTATUS\r
985 iou_remove(\r
986         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
987         IN                                      IRP* const                              p_irp, \r
988                 OUT                             cl_irp_action_t* const  p_action )\r
989 {\r
990         bus_iou_ext_t   *p_ext;\r
991 \r
992         BUS_ENTER( BUS_DBG_PNP );\r
993 \r
994         p_ext = p_dev_obj->DeviceExtension;\r
995 \r
996         if( p_ext->pdo.b_present )\r
997         {\r
998                 CL_ASSERT( p_ext->pdo.cl_ext.pnp_state != NotStarted );\r
999                 CL_ASSERT( !p_ext->pdo.b_reported_missing );\r
1000                 /* Reset the state to NotStarted.  CompLib set it to Deleted. */\r
1001                 cl_set_pnp_state( &p_ext->pdo.cl_ext, NotStarted );\r
1002                 /* Don't delete the device.  It may simply be disabled. */\r
1003                 *p_action = IrpComplete;\r
1004                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Device still present.\n") );\r
1005                 return STATUS_SUCCESS;\r
1006         }\r
1007 \r
1008         if( !p_ext->pdo.b_reported_missing )\r
1009         {\r
1010                 /* Reset the state to RemovePending.  Complib set it to Deleted. */\r
1011                 cl_rollback_pnp_state( &p_ext->pdo.cl_ext );\r
1012                 *p_action = IrpComplete;\r
1013                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Device not reported missing yet.\n") );\r
1014                 return STATUS_SUCCESS;\r
1015         }\r
1016 \r
1017         /* Wait for all I/O operations to complete. */\r
1018         IoReleaseRemoveLockAndWait( &p_ext->pdo.cl_ext.remove_lock, p_irp );\r
1019 \r
1020         /* Release resources if it was not done yet. */\r
1021         if( p_ext->pdo.cl_ext.last_pnp_state != SurpriseRemoved )\r
1022                 p_ext->pdo.cl_ext.vfptr_pnp_po->pfn_release_resources( p_dev_obj );\r
1023 \r
1024         p_irp->IoStatus.Status = STATUS_SUCCESS;\r
1025         IoCompleteRequest( p_irp, IO_NO_INCREMENT );\r
1026 \r
1027         IoDeleteDevice( p_dev_obj );\r
1028 \r
1029         *p_action = IrpDoNothing;\r
1030         BUS_EXIT( BUS_DBG_PNP );\r
1031         return STATUS_SUCCESS;\r
1032 }\r
1033 \r
1034 \r
1035 static NTSTATUS\r
1036 iou_surprise_remove(\r
1037         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
1038         IN                              IRP* const                                      p_irp, \r
1039                 OUT                     cl_irp_action_t* const          p_action )\r
1040 {\r
1041         bus_iou_ext_t   *p_ext;\r
1042 \r
1043         BUS_ENTER( BUS_DBG_PNP );\r
1044 \r
1045         UNUSED_PARAM( p_irp );\r
1046 \r
1047         p_ext = p_dev_obj->DeviceExtension;\r
1048         p_ext->pdo.b_present = FALSE;\r
1049         p_ext->pdo.b_reported_missing = TRUE;\r
1050         BUS_TRACE( BUS_DBG_PNP, ("%s: ext %p, present %d, missing %d .\n",\r
1051                 p_ext->pdo.cl_ext.vfptr_pnp_po->identity, p_ext, p_ext->pdo.b_present, p_ext->pdo.b_reported_missing ) );\r
1052 \r
1053         *p_action = IrpComplete;\r
1054 \r
1055         BUS_EXIT( BUS_DBG_PNP );\r
1056         return STATUS_SUCCESS;\r
1057 }\r
1058 \r
1059 \r
1060 static NTSTATUS\r
1061 iou_query_capabilities(\r
1062         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1063         IN                                      IRP* const                              p_irp, \r
1064                 OUT                             cl_irp_action_t* const  p_action )\r
1065 {\r
1066         DEVICE_CAPABILITIES             *p_caps;\r
1067         IO_STACK_LOCATION               *p_io_stack;\r
1068 \r
1069         BUS_ENTER( BUS_DBG_PNP );\r
1070 \r
1071         UNUSED_PARAM( p_dev_obj );\r
1072 \r
1073         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
1074         p_caps = p_io_stack->Parameters.DeviceCapabilities.Capabilities;\r
1075 \r
1076         p_caps->DeviceD1 = FALSE;\r
1077         p_caps->DeviceD2 = FALSE;\r
1078         p_caps->LockSupported = FALSE;\r
1079         p_caps->EjectSupported = FALSE;\r
1080         p_caps->Removable = TRUE;\r
1081         p_caps->DockDevice = FALSE;\r
1082         p_caps->UniqueID = TRUE;\r
1083         p_caps->SilentInstall = TRUE;\r
1084         p_caps->RawDeviceOK = FALSE;\r
1085         p_caps->SurpriseRemovalOK = FALSE;\r
1086         p_caps->WakeFromD0 = FALSE;\r
1087         p_caps->WakeFromD1 = FALSE;\r
1088         p_caps->WakeFromD2 = FALSE;\r
1089         p_caps->WakeFromD3 = FALSE;\r
1090         p_caps->HardwareDisabled = FALSE;\r
1091         p_caps->DeviceState[PowerSystemWorking] = PowerDeviceD0;\r
1092         p_caps->DeviceState[PowerSystemSleeping1] = PowerDeviceD3;\r
1093         p_caps->DeviceState[PowerSystemSleeping2] = PowerDeviceD3;\r
1094         p_caps->DeviceState[PowerSystemSleeping3] = PowerDeviceD3;\r
1095         p_caps->DeviceState[PowerSystemHibernate] = PowerDeviceD3;\r
1096         p_caps->DeviceState[PowerSystemShutdown] = PowerDeviceD3;\r
1097         p_caps->SystemWake = PowerSystemUnspecified;\r
1098         p_caps->DeviceWake = PowerDeviceUnspecified;\r
1099         p_caps->D1Latency = 0;\r
1100         p_caps->D2Latency = 0;\r
1101         p_caps->D3Latency = 0;\r
1102 \r
1103         *p_action = IrpComplete;\r
1104         BUS_EXIT( BUS_DBG_PNP );\r
1105         return STATUS_SUCCESS;\r
1106 }\r
1107 \r
1108 \r
1109 static NTSTATUS\r
1110 iou_query_target_relations(\r
1111         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1112         IN                                      IRP* const                              p_irp, \r
1113                 OUT                             cl_irp_action_t* const  p_action )\r
1114 {\r
1115         NTSTATUS                        status;\r
1116         DEVICE_RELATIONS        *p_rel;\r
1117 \r
1118         BUS_ENTER( BUS_DBG_PNP );\r
1119 \r
1120         *p_action = IrpComplete;\r
1121 \r
1122         status = cl_alloc_relations( p_irp, 1 );\r
1123         if( !NT_SUCCESS( status ) )\r
1124         {\r
1125                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1126                         ("cl_alloc_relations returned 0x%08x.\n", status) );\r
1127                 return status;\r
1128         }\r
1129 \r
1130         p_rel = (DEVICE_RELATIONS*)p_irp->IoStatus.Information;\r
1131         p_rel->Count = 1;\r
1132         p_rel->Objects[0] = p_dev_obj;\r
1133 \r
1134         ObReferenceObject( p_dev_obj );\r
1135 \r
1136         BUS_EXIT( BUS_DBG_PNP );\r
1137         return status;\r
1138 }\r
1139 \r
1140 \r
1141 static NTSTATUS\r
1142 iou_query_device_id(\r
1143         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1144                 OUT                             IRP* const                              p_irp )\r
1145 {\r
1146         NTSTATUS                        status;\r
1147         bus_iou_ext_t           *p_ext;\r
1148         WCHAR                           *p_string;\r
1149 \r
1150         BUS_ENTER( BUS_DBG_PNP );\r
1151 \r
1152         p_ext = (bus_iou_ext_t*)p_dev_obj->DeviceExtension;\r
1153         if( !p_ext->pdo.b_present )\r
1154         {\r
1155                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1156                 return STATUS_NO_SUCH_DEVICE;\r
1157         }\r
1158 \r
1159         /* Device ID is "IBA\SID_<sid> where <sid> is the IPoIB Service ID. */\r
1160         p_string = ExAllocatePoolWithTag( PagedPool, IOU_DEV_ID_SIZE, 'didq' );\r
1161         if( !p_string )\r
1162         {\r
1163                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1164                         ("Failed to allocate device ID buffer (%d bytes).\n",\r
1165                         IOU_DEV_ID_SIZE) );\r
1166                 return STATUS_INSUFFICIENT_RESOURCES;\r
1167         }\r
1168 \r
1169         status =\r
1170                 RtlStringCbPrintfW( p_string, IOU_DEV_ID_SIZE, IOU_DEV_ID_STRING1,\r
1171                         p_ext->vend_id, p_ext->dev_id, p_ext->revision );\r
1172         if( !NT_SUCCESS( status ) )\r
1173         {\r
1174                 ExFreePool( p_string );\r
1175                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1176                         ("Failed to format device ID string.\n") );\r
1177                 return status;\r
1178         }\r
1179         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1180 \r
1181         BUS_EXIT( BUS_DBG_PNP );\r
1182         return STATUS_SUCCESS;\r
1183 }\r
1184 \r
1185 \r
1186 static NTSTATUS\r
1187 iou_query_hardware_ids(\r
1188         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1189                 OUT                             IRP* const                              p_irp )\r
1190 {\r
1191         NTSTATUS                        status;\r
1192         bus_iou_ext_t           *p_ext;\r
1193         WCHAR                           *p_string, *p_start;\r
1194         size_t                          size;\r
1195 \r
1196         BUS_ENTER( BUS_DBG_PNP );\r
1197 \r
1198         p_ext = (bus_iou_ext_t*)p_dev_obj->DeviceExtension;\r
1199         if( !p_ext->pdo.b_present )\r
1200         {\r
1201                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1202                 return STATUS_NO_SUCH_DEVICE;\r
1203         }\r
1204 \r
1205         p_string = ExAllocatePoolWithTag( PagedPool, IOU_HW_ID_SIZE, 'dihq' );\r
1206         if( !p_string )\r
1207         {\r
1208                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1209                         ("Failed to allocate hardware ID buffer (%d bytes).\n",\r
1210                         IOU_HW_ID_SIZE) );\r
1211                 return STATUS_INSUFFICIENT_RESOURCES;\r
1212         }\r
1213 \r
1214         p_start = p_string;\r
1215         size = IOU_HW_ID_SIZE;\r
1216         /* Fill in the first HW ID. */\r
1217         status = RtlStringCbPrintfExW( p_start, size, &p_start, &size,\r
1218                 STRSAFE_FILL_BEHIND_NULL | STRSAFE_NO_TRUNCATION, IOU_DEV_ID_STRING1,\r
1219                 p_ext->vend_id, p_ext->dev_id, p_ext->revision );\r
1220         if( !NT_SUCCESS( status ) )\r
1221         {\r
1222                 ExFreePool( p_string );\r
1223                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1224                         ("Failed to format device ID string.\n") );\r
1225                 return status;\r
1226         }\r
1227         /* Fill in the second HW ID. */\r
1228         CL_ASSERT( *p_start == L'\0' );\r
1229         p_start++;\r
1230         size -= sizeof(WCHAR);\r
1231         status = RtlStringCbPrintfExW( p_start, size, NULL, NULL,\r
1232                 STRSAFE_FILL_BEHIND_NULL | STRSAFE_NO_TRUNCATION, IOU_DEV_ID_STRING2,\r
1233                 p_ext->vend_id, p_ext->dev_id );\r
1234         if( !NT_SUCCESS( status ) )\r
1235         {\r
1236                 ExFreePool( p_string );\r
1237                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1238                         ("Failed to format device ID string.\n") );\r
1239                 return status;\r
1240         }\r
1241         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1242 \r
1243         BUS_EXIT( BUS_DBG_PNP );\r
1244         return STATUS_SUCCESS;\r
1245 }\r
1246 \r
1247 \r
1248 static NTSTATUS\r
1249 iou_query_compatible_ids(\r
1250         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1251                 OUT                             IRP* const                              p_irp )\r
1252 {\r
1253         WCHAR                           *p_string;\r
1254 \r
1255         BUS_ENTER( BUS_DBG_PNP );\r
1256 \r
1257         UNUSED_PARAM( p_dev_obj );\r
1258 \r
1259         p_string = ExAllocatePoolWithTag( PagedPool, sizeof(IOU_COMPAT_ID), 'dicq' );\r
1260         if( !p_string )\r
1261         {\r
1262                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1263                         ("Failed to allocate compatible ID buffer (%d bytes).\n",\r
1264                         IOU_HW_ID_SIZE) );\r
1265                 return STATUS_INSUFFICIENT_RESOURCES;\r
1266         }\r
1267 \r
1268         cl_memcpy( p_string, IOU_COMPAT_ID, sizeof(IOU_COMPAT_ID) );\r
1269         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1270 \r
1271         BUS_EXIT( BUS_DBG_PNP );\r
1272         return STATUS_SUCCESS;\r
1273 }\r
1274 \r
1275 \r
1276 static NTSTATUS\r
1277 iou_query_unique_id(\r
1278         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1279                 OUT                             IRP* const                              p_irp )\r
1280 {\r
1281         NTSTATUS                        status;\r
1282         WCHAR                           *p_string;\r
1283         bus_iou_ext_t           *p_ext;\r
1284 \r
1285         BUS_ENTER( BUS_DBG_PNP );\r
1286         \r
1287         p_ext = p_dev_obj->DeviceExtension;\r
1288         if( !p_ext->pdo.b_present )\r
1289         {\r
1290                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1291                 return STATUS_NO_SUCH_DEVICE;\r
1292         }\r
1293 \r
1294         /* The instance ID is the port GUID. */\r
1295         p_string = ExAllocatePoolWithTag( PagedPool, sizeof(WCHAR) * 33, 'diuq' );\r
1296         if( !p_string )\r
1297         {\r
1298                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1299                         ("Failed to allocate instance ID buffer (%d bytes).\n",\r
1300                         sizeof(WCHAR) * 17) );\r
1301                 return STATUS_NO_MEMORY;\r
1302         }\r
1303 \r
1304         status = RtlStringCchPrintfW( p_string, 33, L"%016I64x%016I64x",\r
1305                 p_ext->guid, p_ext->pdo.ca_guid );\r
1306         if( !NT_SUCCESS( status ) )\r
1307         {\r
1308                 CL_ASSERT( NT_SUCCESS( status ) );\r
1309                 ExFreePool( p_string );\r
1310                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1311                         ("RtlStringCchPrintfW returned %08x.\n", status) );\r
1312                 return status;\r
1313         }\r
1314 \r
1315         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1316 \r
1317         BUS_EXIT( BUS_DBG_PNP );\r
1318         return STATUS_SUCCESS;\r
1319 }\r
1320 \r
1321 \r
1322 static NTSTATUS\r
1323 iou_query_description(\r
1324         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1325                 OUT                             IRP* const                              p_irp )\r
1326 {\r
1327         NTSTATUS                        status;\r
1328         WCHAR                           *p_string;\r
1329         bus_iou_ext_t           *p_ext;\r
1330 \r
1331         BUS_ENTER( BUS_DBG_PNP );\r
1332         \r
1333         p_ext = p_dev_obj->DeviceExtension;\r
1334         if( !p_ext->pdo.b_present )\r
1335         {\r
1336                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1337                 return STATUS_NO_SUCH_DEVICE;\r
1338         }\r
1339 \r
1340         /* The instance ID is the port GUID. */\r
1341         p_string = ExAllocatePoolWithTag( PagedPool,\r
1342                                                                           sizeof(WCHAR) * sizeof(p_ext->desc),\r
1343                                                                           'sedq' );\r
1344         if( !p_string )\r
1345         {\r
1346                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1347                         ("Failed to allocate device description buffer (%d bytes).\n",\r
1348                         sizeof(WCHAR) * sizeof(p_ext->desc)) );\r
1349                 return STATUS_INSUFFICIENT_RESOURCES;\r
1350         }\r
1351 \r
1352         status = RtlStringCchPrintfW( p_string, sizeof(p_ext->desc),\r
1353                 L"%S", p_ext->desc );\r
1354         if( !NT_SUCCESS( status ) )\r
1355         {\r
1356                 CL_ASSERT( NT_SUCCESS( status ) );\r
1357                 ExFreePool( p_string );\r
1358                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1359                         ("RtlStringCchPrintfW returned %08x.\n", status) );\r
1360                 return status;\r
1361         }\r
1362         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1363 \r
1364         BUS_EXIT( BUS_DBG_PNP );\r
1365         return STATUS_SUCCESS;\r
1366 }\r
1367 \r
1368 \r
1369 static NTSTATUS\r
1370 iou_query_location(\r
1371         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1372                 OUT                             IRP* const                              p_irp )\r
1373 {\r
1374         NTSTATUS                        status;\r
1375         bus_iou_ext_t           *p_ext;\r
1376         WCHAR                           *p_string;\r
1377 \r
1378         BUS_ENTER( BUS_DBG_PNP );\r
1379 \r
1380         p_ext = (bus_iou_ext_t*)p_dev_obj->DeviceExtension;\r
1381         if( !p_ext->pdo.b_present )\r
1382         {\r
1383                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("Device not present.\n") );\r
1384                 return STATUS_NO_SUCH_DEVICE;\r
1385         }\r
1386 \r
1387         p_string = ExAllocatePoolWithTag( PagedPool, IOU_LOCATION_SIZE, 'colq' );\r
1388         if( !p_string )\r
1389         {\r
1390                 BUS_TRACE_EXIT( BUS_DBG_ERROR, \r
1391                         ("Failed to allocate location buffer (%d bytes).\n",\r
1392                         IOU_LOCATION_SIZE) );\r
1393                 return STATUS_INSUFFICIENT_RESOURCES;\r
1394         }\r
1395 \r
1396         status = RtlStringCbPrintfW( p_string, IOU_LOCATION_SIZE,\r
1397                 L"Chassis 0x%016I64x, Slot %d", p_ext->chassis_guid, p_ext->slot );\r
1398         if( !NT_SUCCESS( status ) )\r
1399         {\r
1400                 ExFreePool( p_string );\r
1401                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1402                         ("Failed to format device ID string.\n") );\r
1403                 return status;\r
1404         }\r
1405         p_irp->IoStatus.Information = (ULONG_PTR)p_string;\r
1406 \r
1407         BUS_EXIT( BUS_DBG_PNP );\r
1408         return STATUS_SUCCESS;\r
1409 }\r
1410 \r
1411 \r
1412 static NTSTATUS\r
1413 iou_query_bus_info(\r
1414         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1415         IN                                      IRP* const                              p_irp, \r
1416                 OUT                             cl_irp_action_t* const  p_action )\r
1417 {\r
1418         PNP_BUS_INFORMATION     *p_bus_info;\r
1419 \r
1420         BUS_ENTER( BUS_DBG_PNP );\r
1421 \r
1422         UNUSED_PARAM( p_dev_obj );\r
1423 \r
1424         *p_action = IrpComplete;\r
1425 \r
1426         p_bus_info = ExAllocatePoolWithTag( PagedPool, sizeof(PNP_BUS_INFORMATION), 'subq' );\r
1427         if( !p_bus_info )\r
1428         {\r
1429                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1430                         ("Failed to allocate PNP_BUS_INFORMATION (%d bytes).\n",\r
1431                         sizeof(PNP_BUS_INFORMATION)) );\r
1432                 return STATUS_INSUFFICIENT_RESOURCES;\r
1433         }\r
1434 \r
1435         p_bus_info->BusTypeGuid = GUID_BUS_TYPE_IBA;\r
1436         //TODO: Memory from Intel - storage miniport would not stay loaded unless\r
1437         //TODO: bus type was PCI.  Look here if SRP is having problems staying\r
1438         //TODO: loaded.\r
1439         p_bus_info->LegacyBusType = PNPBus;\r
1440         p_bus_info->BusNumber = 0;\r
1441 \r
1442         p_irp->IoStatus.Information = (ULONG_PTR)p_bus_info;\r
1443         BUS_EXIT( BUS_DBG_PNP );\r
1444         return STATUS_SUCCESS;\r
1445 }\r
1446 \r
1447 \r
1448 static NTSTATUS\r
1449 iou_query_iou_ifc(\r
1450         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
1451         IN                              IO_STACK_LOCATION* const        p_io_stack )\r
1452 {\r
1453         NTSTATUS                                status;\r
1454         ib_al_ifc_t                             *p_ifc;\r
1455         ib_al_ifc_data_t                *p_ifc_data;\r
1456         iou_ifc_data_t                  *p_iou_data;\r
1457         bus_iou_ext_t                   *p_ext;\r
1458         const GUID                              *p_guid;\r
1459 \r
1460         BUS_ENTER( BUS_DBG_PNP );\r
1461 \r
1462         CL_ASSERT( KeGetCurrentIrql() < DISPATCH_LEVEL );\r
1463 \r
1464         p_ext = p_dev_obj->DeviceExtension;\r
1465 \r
1466         /* Get the interface. */\r
1467         status = cl_fwd_query_ifc(\r
1468                 p_ext->pdo.p_parent_ext->cl_ext.p_self_do, p_io_stack );\r
1469         if( !NT_SUCCESS( status ) )\r
1470         {\r
1471                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1472                         ("Failed to forward interface query: %08X\n", status) );\r
1473                 return status;\r
1474         }\r
1475 \r
1476         if( !p_io_stack->Parameters.QueryInterface.InterfaceSpecificData )\r
1477         {\r
1478                 BUS_TRACE_EXIT( BUS_DBG_ERROR, ("No interface specific data!\n") );\r
1479                 return status;\r
1480         }\r
1481 \r
1482         p_ifc = (ib_al_ifc_t*)p_io_stack->Parameters.QueryInterface.Interface;\r
1483 \r
1484         p_ifc_data = (ib_al_ifc_data_t*)\r
1485                 p_io_stack->Parameters.QueryInterface.InterfaceSpecificData;\r
1486         p_guid = p_ifc_data->type;\r
1487         if( !IsEqualGUID( p_guid, &GUID_IOU_INTERFACE_DATA ) )\r
1488         {\r
1489                 BUS_TRACE_EXIT( BUS_DBG_PNP, ("Unsupported interface data: \n\t"\r
1490                         "0x%08x, 0x%04x, 0x%04x, 0x%02x, 0x%02x, 0x%02x,"\r
1491                         "0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x.\n",\r
1492                         p_guid->Data1, p_guid->Data2, p_guid->Data3,\r
1493                         p_guid->Data4[0], p_guid->Data4[1], p_guid->Data4[2],\r
1494                         p_guid->Data4[3], p_guid->Data4[4], p_guid->Data4[5],\r
1495                         p_guid->Data4[6], p_guid->Data4[7]) );\r
1496                 return status;\r
1497         }\r
1498 \r
1499         if( p_ifc_data->version != IOU_INTERFACE_DATA_VERSION )\r
1500         {\r
1501                 p_ifc->wdm.InterfaceDereference( p_ifc->wdm.Context );\r
1502                 BUS_TRACE_EXIT( BUS_DBG_ERROR,\r
1503                         ("Unsupported version %d, expected %d\n",\r
1504                         p_ifc_data->version, IOU_INTERFACE_DATA_VERSION) );\r
1505                 return STATUS_NOT_SUPPORTED;\r
1506         }\r
1507 \r
1508         ASSERT( p_ifc_data->p_data );\r
1509 \r
1510         if( p_ifc_data->size != sizeof(iou_ifc_data_t) )\r
1511         {\r
1512                 p_ifc->wdm.InterfaceDereference( p_ifc->wdm.Context );\r
1513                 BUS_TRACE_EXIT( BUS_DBG_PNP,\r
1514                         ("Buffer too small (%d given, %d required).\n",\r
1515                         p_ifc_data->size,\r
1516                         sizeof(iou_ifc_data_t)) );\r
1517                 return STATUS_BUFFER_TOO_SMALL;\r
1518         }\r
1519 \r
1520         /* Set the interface data. */\r
1521         p_iou_data = (iou_ifc_data_t*)p_ifc_data->p_data;\r
1522 \r
1523         p_iou_data->ca_guid = p_ext->pdo.ca_guid;\r
1524         p_iou_data->chassis_guid = p_ext->chassis_guid;\r
1525         p_iou_data->slot = p_ext->slot;\r
1526         p_iou_data->guid = p_ext->guid;\r
1527 \r
1528         BUS_EXIT( BUS_DBG_PNP );\r
1529         return STATUS_SUCCESS;\r
1530 }\r
1531 \r
1532 \r
1533 static NTSTATUS\r
1534 iou_query_interface(\r
1535         IN                                      DEVICE_OBJECT* const    p_dev_obj,\r
1536         IN                                      IRP* const                              p_irp, \r
1537                 OUT                             cl_irp_action_t* const  p_action )\r
1538 {\r
1539         bus_pdo_ext_t           *p_ext;\r
1540         NTSTATUS                        status;\r
1541         IO_STACK_LOCATION       *p_io_stack;\r
1542 \r
1543         BUS_ENTER( BUS_DBG_PNP );\r
1544 \r
1545         CL_ASSERT( KeGetCurrentIrql() < DISPATCH_LEVEL );\r
1546 \r
1547         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
1548         \r
1549         /* Bottom of the stack - IRP must be completed. */\r
1550         *p_action = IrpComplete;\r
1551 \r
1552         /* Compare requested GUID with our supported interface GUIDs. */\r
1553         if( IsEqualGUID( p_io_stack->Parameters.QueryInterface.InterfaceType,\r
1554                 &GUID_IB_AL_INTERFACE ) )\r
1555         {\r
1556                 status = iou_query_iou_ifc( p_dev_obj, p_io_stack );\r
1557         }\r
1558         else if( IsEqualGUID( p_io_stack->Parameters.QueryInterface.InterfaceType,\r
1559                 &GUID_BUS_INTERFACE_STANDARD ) )\r
1560         {\r
1561                 p_ext = p_dev_obj->DeviceExtension;\r
1562                 if( !p_ext->h_ca ||\r
1563                         !p_ext->b_present ||\r
1564                         !p_ext->h_ca->obj.p_ci_ca->verbs.p_hca_dev ||\r
1565                         p_ext->b_reported_missing )\r
1566                 {\r
1567                         return STATUS_NO_SUCH_DEVICE;\r
1568                 }\r
1569 \r
1570 \r
1571                 status = cl_fwd_query_ifc(\r
1572                         p_ext->h_ca->obj.p_ci_ca->verbs.p_hca_dev, p_io_stack );\r
1573         }\r
1574         else\r
1575         {\r
1576                 status = p_irp->IoStatus.Status;\r
1577         }\r
1578 \r
1579         BUS_EXIT( BUS_DBG_PNP );\r
1580         return status;\r
1581 }\r
1582 \r
1583 \r
1584 \r
1585 /* Work item callback to handle DevicePowerD3 IRPs at passive level. */\r
1586 static void\r
1587 __HibernateUpWorkItem(\r
1588         IN                              DEVICE_OBJECT*                          p_dev_obj,\r
1589         IN                              void*                                           context )\r
1590 {\r
1591         IO_STACK_LOCATION       *p_io_stack;\r
1592         bus_pdo_ext_t           *p_ext;\r
1593         IRP                                     *p_irp;\r
1594         POWER_STATE powerState;\r
1595 \r
1596         BUS_ENTER( BUS_DBG_POWER );\r
1597 \r
1598         p_ext = (bus_pdo_ext_t*)p_dev_obj->DeviceExtension;\r
1599         p_irp = (IRP*)context;\r
1600         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
1601 \r
1602         IoFreeWorkItem( p_ext->p_po_work_item );\r
1603         p_ext->p_po_work_item = NULL;\r
1604 \r
1605         while (!p_ext->h_ca) {\r
1606                 BUS_TRACE( BUS_DBG_PNP, ("Waiting for the end of HCA registration ... \n"));\r
1607                 cl_thread_suspend( 200 );       /* suspend for 200 ms */\r
1608         }\r
1609 \r
1610         p_ext->dev_po_state = p_io_stack->Parameters.Power.State;\r
1611         powerState = PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
1612 \r
1613         BUS_TRACE( BUS_DBG_POWER, \r
1614                 ("PoSetPowerState: old state %d, new state to %d\n", \r
1615                 powerState.DeviceState, p_ext->dev_po_state ));\r
1616 \r
1617         p_irp->IoStatus.Status = STATUS_SUCCESS;\r
1618         PoStartNextPowerIrp( p_irp );\r
1619         IoCompleteRequest( p_irp, IO_NO_INCREMENT );\r
1620         IoReleaseRemoveLock( &p_ext->cl_ext.remove_lock, p_irp );\r
1621 \r
1622         BUS_EXIT( BUS_DBG_POWER );\r
1623 }\r
1624 \r
1625 /*\r
1626  * The PDOs created by the IB Bus driver are software devices.  As such,\r
1627  * all power states are supported.  It is left to the HCA power policy \r
1628  * owner to handle which states can be supported by the HCA.\r
1629  */\r
1630 static NTSTATUS\r
1631 iou_set_power(\r
1632         IN                              DEVICE_OBJECT* const            p_dev_obj,\r
1633         IN                              IRP* const                                      p_irp,\r
1634                 OUT                     cl_irp_action_t* const          p_action )\r
1635 {\r
1636         NTSTATUS                        status = STATUS_SUCCESS;\r
1637         IO_STACK_LOCATION       *p_io_stack;\r
1638         bus_pdo_ext_t           *p_ext;\r
1639 \r
1640         BUS_ENTER( BUS_DBG_POWER );\r
1641 \r
1642         p_ext = p_dev_obj->DeviceExtension;\r
1643         p_io_stack = IoGetCurrentIrpStackLocation( p_irp );\r
1644 \r
1645         BUS_TRACE( BUS_DBG_POWER, \r
1646                 ("SET_POWER for PDO %p (ext %p): type %s, state %d, action %d \n",\r
1647                 p_dev_obj, p_ext,\r
1648                 (p_io_stack->Parameters.Power.Type) ? "DevicePowerState" : "SystemPowerState",\r
1649                 p_io_stack->Parameters.Power.State.DeviceState, \r
1650                 p_io_stack->Parameters.Power.ShutdownType ));\r
1651 \r
1652         if ((p_io_stack->Parameters.Power.Type == SystemPowerState) &&\r
1653                 (p_io_stack->Parameters.Power.State.SystemState ==PowerSystemHibernate ||\r
1654                 p_io_stack->Parameters.Power.State.SystemState ==PowerSystemSleeping1 ))\r
1655         {\r
1656                 BUS_TRACE( BUS_DBG_POWER, ("Setting b_hibernating flag for PDO %p \n", p_dev_obj));\r
1657                 p_ext->b_hibernating = TRUE;\r
1658         }\r
1659 \r
1660         if( p_io_stack->Parameters.Power.Type == DevicePowerState )\r
1661         {\r
1662                 /* after hibernation PDO is not ready for work. we need to wait for finishing of the HCA registration */\r
1663                 if( p_io_stack->Parameters.Power.State.DeviceState == PowerDeviceD0 && p_ext->b_hibernating)\r
1664                 {\r
1665                         /* Process in a work item - deregister_ca and HcaDeinit block. */\r
1666                         ASSERT( !p_ext->p_po_work_item );\r
1667                         p_ext->p_po_work_item = IoAllocateWorkItem( p_dev_obj );\r
1668                         if( !p_ext->p_po_work_item )\r
1669                                 status = STATUS_INSUFFICIENT_RESOURCES;\r
1670                         else {\r
1671                                 /* Process in work item callback. */\r
1672                                 IoMarkIrpPending( p_irp );\r
1673                                 IoQueueWorkItem(\r
1674                                         p_ext->p_po_work_item, __HibernateUpWorkItem, DelayedWorkQueue, p_irp );\r
1675                                 *p_action = IrpDoNothing;\r
1676                                 BUS_EXIT( BUS_DBG_POWER );\r
1677                                 return STATUS_PENDING;\r
1678                         }\r
1679                 }\r
1680 \r
1681                 /* Notify the power manager. */\r
1682                 p_ext->dev_po_state = p_io_stack->Parameters.Power.State;\r
1683                 PoSetPowerState( p_dev_obj, DevicePowerState, p_ext->dev_po_state );\r
1684         }\r
1685 \r
1686         *p_action = IrpComplete;\r
1687         BUS_EXIT( BUS_DBG_POWER );\r
1688         return STATUS_SUCCESS;\r
1689 }\r