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