[IBAL] Fix overflow in loop counters when iterating over service entries.
[mirror/winof/.git] / core / al / kernel / al_ioc_pnp.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  *\r
4  * This software is available to you under the OpenIB.org BSD license\r
5  * below:\r
6  *\r
7  *     Redistribution and use in source and binary forms, with or\r
8  *     without modification, are permitted provided that the following\r
9  *     conditions are met:\r
10  *\r
11  *      - Redistributions of source code must retain the above\r
12  *        copyright notice, this list of conditions and the following\r
13  *        disclaimer.\r
14  *\r
15  *      - Redistributions in binary form must reproduce the above\r
16  *        copyright notice, this list of conditions and the following\r
17  *        disclaimer in the documentation and/or other materials\r
18  *        provided with the distribution.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
27  * SOFTWARE.\r
28  *\r
29  * $Id$\r
30  */\r
31 \r
32 \r
33 #include <iba/ib_al.h>\r
34 #include "al_pnp.h"\r
35 #include "al_ioc_pnp.h"\r
36 #include "al_debug.h"\r
37 #if defined(EVENT_TRACING)\r
38 #ifdef offsetof\r
39 #undef offsetof\r
40 #endif\r
41 #include "al_ioc_pnp.tmh"\r
42 #endif\r
43 #include "ib_common.h"\r
44 #include "al_mgr.h"\r
45 #include "al_ca.h"\r
46 #include <complib/cl_timer.h>\r
47 #include <complib/cl_qpool.h>\r
48 #include <complib/cl_qmap.h>\r
49 #include <complib/cl_fleximap.h>\r
50 #include <complib/cl_math.h>\r
51 \r
52 \r
53 /* Basic sweep operation flow:\r
54  *\r
55  * NOTE: Empty lines indicate asynchronous decoupling.\r
56  *      1. Timer expires\r
57  *      2. Issue SA query for all CA nodes\r
58  *      3. Issue SA query for all paths\r
59  *\r
60  *      4. Query callback for first query - store results.\r
61  *      5. Query callback for second query - process results.\r
62  *      6. Associate paths to nodes.\r
63  *      7. For each node, use the first path to send a IOU Info query.\r
64  *\r
65  *      8a. Recv callback (success) - record IOU info, decrement ref count.\r
66  *      8b. Recv callback (failure) - decrement ref count.\r
67  *      8c. Send failure - decrement ref count.\r
68  *      8d. Send timeout - pick next path and repeate IOU info query.\r
69  *      9. Queue results to async proc thread once ref count hits zero\r
70  *\r
71  *      10. Discard any nodes that failed IOU info query, or reported no IOCs.\r
72  *      11. For each node scanned that is already known, compare change ID\r
73  *      12a. Change ID identical - report any path changes.\r
74  *      12b. Change ID different - for each active IOC slot, query IOC profile.\r
75  *\r
76  *      13a. Recv callback (success) - associate IOC with IOU, decrement ref count.\r
77  *      13b. Recv callback (failure) - decrement ref count.\r
78  *      13c. Send failure - decrement ref count.\r
79  *      14. Queue results to async proc thread once ref count hits zero.\r
80  *\r
81  *      15. Discard any nodes that have no IOCs.\r
82  *      16. For each IOC of each node, query all service entries.\r
83  *\r
84  *      17a. Recv callback (success) - copy service entries, decrement ref count.\r
85  *      17b. Recv callback (failure) - Remove IOC from IOU, decrement ref count.\r
86  *      17c. Send failure - Remove IOC from IOU, decrement ref count.\r
87  *      18. Queue results to async proc thread once ref count hits zero.\r
88  *\r
89  *      19. Discard any nodes that have no IOCs.\r
90  *      20. Compare new node map to known nodes and report changes.\r
91  *      21. Compare IOCs for any duplicates and report changes.\r
92  *      22. Compare paths for any duplicates and report changes.\r
93  *      23. Reset sweep timer.\r
94  *\r
95  * Note: the sweep timer is reset at any point where there can be no further\r
96  * progress towards.\r
97  */\r
98 \r
99 \r
100 /* Number of entries in the various pools to grow by. */\r
101 #define IOC_PNP_POOL_GROW       (10)\r
102 \r
103 \r
104 /* IOC PnP Manager structure. */\r
105 typedef struct _ioc_pnp_mgr\r
106 {\r
107         al_obj_t                                obj;\r
108 \r
109         cl_qlist_t                              iou_reg_list;\r
110         cl_qlist_t                              ioc_reg_list;\r
111 \r
112         ib_pnp_handle_t                 h_pnp;\r
113 \r
114         cl_async_proc_item_t    async_item;\r
115         boolean_t                               async_item_is_busy;\r
116 \r
117         cl_spinlock_t                   iou_pool_lock;\r
118         cl_qpool_t                              iou_pool;\r
119         cl_spinlock_t                   ioc_pool_lock;\r
120         cl_qpool_t                              ioc_pool;\r
121         cl_spinlock_t                   path_pool_lock;\r
122         cl_qpool_t                              path_pool;\r
123 \r
124         cl_fmap_t                               iou_map;        /* Map of currently known IOUs */\r
125         cl_fmap_t                               sweep_map;      /* Map of IOUs from sweep results. */\r
126         cl_timer_t                              sweep_timer;/* Timer to trigger sweep. */\r
127         atomic32_t                              query_cnt;      /* Number of sweep results outstanding. */\r
128 \r
129 }       ioc_pnp_mgr_t;\r
130 \r
131 \r
132 /* Per-port IOC PnP agent. */\r
133 typedef struct _ioc_pnp_svc\r
134 {\r
135         al_obj_t                                obj;\r
136 \r
137         net64_t                                 ca_guid;\r
138         net64_t                                 port_guid;\r
139 \r
140         ib_qp_handle_t                  h_qp;\r
141         ib_pool_key_t                   pool_key;\r
142         ib_mad_svc_handle_t             h_mad_svc;\r
143 \r
144         atomic32_t                              query_cnt;\r
145         ib_query_handle_t               h_node_query;\r
146         ib_query_handle_t               h_path_query;\r
147         ib_mad_element_t                *p_node_element;\r
148         ib_mad_element_t                *p_path_element;\r
149         uint32_t                                num_nodes;\r
150         uint32_t                                num_paths;\r
151 \r
152 }       ioc_pnp_svc_t;\r
153 \r
154 \r
155 /****d* Access Layer:IOC PnP/iou_path_t\r
156 * NAME\r
157 *       iou_path_t\r
158 *\r
159 * DESCRIPTION\r
160 *       Describes a path to an IOU node.\r
161 *\r
162 * SYNOPSIS\r
163 */\r
164 typedef struct _iou_path\r
165 {\r
166         cl_fmap_item_t                  map_item;\r
167         net64_t                                 ca_guid;\r
168         net64_t                                 port_guid;\r
169         ib_path_rec_t                   rec;\r
170 \r
171 }       iou_path_t;\r
172 /*\r
173 * FIELDS\r
174 *       map_item\r
175 *               Map item for storing paths in a map.\r
176 *\r
177 *       path_rec\r
178 *               Path record.\r
179 *\r
180 * SEE ALSO\r
181 *       IOC PnP\r
182 *********/\r
183 \r
184 \r
185 /****d* Access Layer:IOC PnP/iou_node_t\r
186 * NAME\r
187 *       iou_node_t\r
188 *\r
189 * DESCRIPTION\r
190 *       Describes an IOU node on the fabric.\r
191 *\r
192 * SYNOPSIS\r
193 */\r
194 typedef struct _iou_node\r
195 {\r
196         cl_fmap_item_t                  map_item;\r
197         cl_fmap_t                               path_map;\r
198         cl_qmap_t                               ioc_map;\r
199         cl_spinlock_t                   lock;\r
200 \r
201         iou_path_t                              *p_config_path;\r
202 \r
203         net64_t                                 ca_guid;\r
204         net64_t                                 guid;\r
205         net64_t                                 chassis_guid;\r
206         uint8_t                                 slot;\r
207         net32_t                                 vend_id;\r
208         net16_t                                 dev_id;\r
209         net32_t                                 revision;\r
210         ib_iou_info_t                   info;\r
211 \r
212         char                                    desc[IB_NODE_DESCRIPTION_SIZE + 1];\r
213 \r
214 }       iou_node_t;\r
215 /*\r
216 * FIELDS\r
217 *       map_item\r
218 *               Map item for storing IOUs in a map.\r
219 *\r
220 *       path_map\r
221 *               Map of paths to the IOU.\r
222 *\r
223 *       ioc_map\r
224 *               Map of IOCs on the IOU.\r
225 *\r
226 *       p_config_path\r
227 *               Path used to get configuration information from the IOU.\r
228 *\r
229 *       ca_guid\r
230 *               CA GUID through which the IOU is accessible.\r
231 *\r
232 *       guid\r
233 *               Node GUID used as key when storing IOUs in the map.\r
234 *\r
235 *       chassis_guid\r
236 *               GUID of the chassis in which the IOU is installed.\r
237 *\r
238 *       slot\r
239 *               Slot number in the chassis in which the IOU is installed.\r
240 *\r
241 *       vend_id\r
242 *               Vendor ID of the IOU.\r
243 *\r
244 *       dev_id\r
245 *               Device ID of the IOU.\r
246 *\r
247 *       revision\r
248 *               Device revision of the IOU.\r
249 *\r
250 *       info\r
251 *               I/O unit info structure.\r
252 *\r
253 *       desc\r
254 *               Node description as provided in ib_node_record_t, along with space for\r
255 *               terminating NULL.\r
256 *\r
257 * NOTES\r
258 *       The guid member must follow the ca_guid member to allow both guids to\r
259 *       be compared in single call to cl_memcmp.\r
260 *\r
261 * SEE ALSO\r
262 *       IOC PnP\r
263 *********/\r
264 \r
265 \r
266 #pragma warning(disable:4324)\r
267 typedef struct _iou_ioc\r
268 {\r
269         cl_map_item_t                   map_item;\r
270         iou_node_t                              *p_iou;\r
271         uint8_t                                 slot;\r
272         ib_ioc_profile_t                profile;\r
273         uint8_t                                 num_valid_entries;\r
274         ib_svc_entry_t                  *p_svc_entries;\r
275 \r
276 }       iou_ioc_t;\r
277 #pragma warning(default:4324)\r
278 \r
279 \r
280 typedef enum _sweep_state\r
281 {\r
282         SWEEP_IOU_INFO,\r
283         SWEEP_IOC_PROFILE,\r
284         SWEEP_SVC_ENTRIES,\r
285         SWEEP_COMPLETE\r
286 \r
287 }       sweep_state_t;\r
288 \r
289 \r
290 typedef struct _ioc_sweep_results\r
291 {\r
292         cl_async_proc_item_t    async_item;\r
293         sweep_state_t                   state;\r
294         ioc_pnp_svc_t                   *p_svc;\r
295         atomic32_t                              query_cnt;\r
296         cl_fmap_t                               iou_map;\r
297 \r
298 }       ioc_sweep_results_t;\r
299 \r
300 \r
301 typedef struct _al_pnp_ioc_event\r
302 {\r
303         size_t                                  rec_size;\r
304         ib_pnp_rec_t                    *p_rec;\r
305         ib_pnp_rec_t                    *p_user_rec;\r
306 \r
307 }       al_pnp_ioc_event_t;\r
308 \r
309 \r
310 /* Global instance of the IOC PnP manager. */\r
311 ioc_pnp_mgr_t   *gp_ioc_pnp = NULL;\r
312 uint32_t                g_ioc_query_timeout = 250;\r
313 uint32_t                g_ioc_query_retries = 4;\r
314 uint32_t                g_ioc_poll_interval = 30000;\r
315 \r
316 \r
317 \r
318 /******************************************************************************\r
319 *\r
320 * IOC PnP Manager functions - global object.\r
321 *\r
322 ******************************************************************************/\r
323 static void\r
324 __construct_ioc_pnp(\r
325         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr );\r
326 \r
327 static ib_api_status_t\r
328 __init_ioc_pnp(\r
329         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr );\r
330 \r
331 static void\r
332 __destroying_ioc_pnp(\r
333         IN                              al_obj_t                                        *p_obj );\r
334 \r
335 static void\r
336 __free_ioc_pnp(\r
337         IN                              al_obj_t                                        *p_obj );\r
338 \r
339 static ib_api_status_t\r
340 __ioc_pnp_cb(\r
341         IN                              ib_pnp_rec_t                            *p_pnp_rec );\r
342 \r
343 static cl_status_t\r
344 __init_iou(\r
345         IN                              void* const                                     p_obj,\r
346         IN                              void*                                           context,\r
347                 OUT                     cl_pool_item_t** const          pp_pool_item );\r
348 \r
349 /******************************************************************************\r
350 *\r
351 * IOC PnP manager sweep-related functions.\r
352 *\r
353 ******************************************************************************/\r
354 static iou_node_t*\r
355 __get_iou(\r
356         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
357         IN              const   net64_t                                         ca_guid,\r
358         IN              const   ib_node_record_t* const         p_node_rec );\r
359 \r
360 static void\r
361 __put_iou(\r
362         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
363         IN                              iou_node_t* const                       p_iou );\r
364 \r
365 static void\r
366 __put_iou_map(\r
367         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
368         IN                              cl_fmap_t* const                        p_iou_map );\r
369 \r
370 static iou_path_t*\r
371 __get_path(\r
372         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
373         IN              const   net64_t                                         ca_guid,\r
374         IN              const   net64_t                                         port_guid,\r
375         IN              const   ib_path_rec_t* const            p_path_rec );\r
376 \r
377 static void\r
378 __put_path(\r
379         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
380         IN                              iou_path_t* const                       p_path );\r
381 \r
382 static void\r
383 __put_path_map(\r
384         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
385         IN                              cl_fmap_t* const                        p_path_map );\r
386 \r
387 static iou_ioc_t*\r
388 __get_ioc(\r
389         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
390         IN              const   uint32_t                                        ioc_slot,\r
391         IN              const   ib_ioc_profile_t* const         p_profile );\r
392 \r
393 static void\r
394 __put_ioc(\r
395         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
396         IN                              iou_ioc_t* const                        p_ioc );\r
397 \r
398 static void\r
399 __put_ioc_map(\r
400         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
401         IN                              cl_qmap_t* const                        p_ioc_map );\r
402 \r
403 static intn_t\r
404 __iou_cmp(\r
405         IN              const   void* const                                     p_key1,\r
406         IN              const   void* const                                     p_key2 );\r
407 \r
408 static intn_t\r
409 __path_cmp(\r
410         IN              const   void* const                                     p_key1,\r
411         IN              const   void* const                                     p_key2 );\r
412 \r
413 static void\r
414 __ioc_pnp_timer_cb(\r
415         IN                              void                                            *context );\r
416 \r
417 static void\r
418 __ioc_async_cb(\r
419         IN                              cl_async_proc_item_t            *p_async_item );\r
420 \r
421 /******************************************************************************\r
422 *\r
423 * IOC PnP service - per local port child of IOC PnP manager.\r
424 *\r
425 ******************************************************************************/\r
426 static ib_api_status_t\r
427 __create_ioc_pnp_svc(\r
428         IN                              ib_pnp_rec_t                            *p_pnp_rec );\r
429 \r
430 static ib_api_status_t\r
431 __init_ioc_pnp_svc(\r
432         IN                              ioc_pnp_svc_t* const            p_ioc_pnp_svc,\r
433         IN              const   ib_pnp_rec_t* const                     p_pnp_rec );\r
434 \r
435 static void\r
436 __destroying_ioc_pnp_svc(\r
437         IN                              al_obj_t                                        *p_obj );\r
438 \r
439 static void\r
440 __free_ioc_pnp_svc(\r
441         IN                              al_obj_t                                        *p_obj );\r
442 \r
443 /******************************************************************************\r
444 *\r
445 * IOC PnP service sweep functions.\r
446 *\r
447 ******************************************************************************/\r
448 static void\r
449 __ioc_pnp_recv_cb(\r
450         IN              const   ib_mad_svc_handle_t                     h_mad_svc,\r
451         IN                              void                                            *mad_svc_context,\r
452         IN                              ib_mad_element_t                        *p_request_mad );\r
453 \r
454 static void\r
455 __ioc_pnp_send_cb(\r
456         IN              const   ib_mad_svc_handle_t                     h_mad_svc,\r
457         IN                              void                                            *mad_svc_context,\r
458         IN                              ib_mad_element_t                        *p_mad_response );\r
459 \r
460 static void\r
461 __node_rec_cb(\r
462         IN                              ib_query_rec_t                          *p_query_rec );\r
463 \r
464 static void\r
465 __path_rec_cb(\r
466         IN                              ib_query_rec_t                          *p_query_rec );\r
467 \r
468 static void\r
469 __process_sweep(\r
470         IN                              cl_async_proc_item_t            *p_async_item );\r
471 \r
472 static void\r
473 __process_query(\r
474         IN                              ioc_pnp_svc_t* const            p_svc );\r
475 \r
476 static void\r
477 __process_nodes(\r
478         IN                              ioc_pnp_svc_t* const            p_svc,\r
479         IN                              cl_qmap_t* const                        p_iou_map );\r
480 \r
481 static void\r
482 __process_paths(\r
483         IN                              ioc_pnp_svc_t* const            p_svc,\r
484         IN                              cl_qmap_t* const                        p_iou_map );\r
485 \r
486 static void\r
487 __build_iou_map(\r
488         IN                              cl_qmap_t* const                        p_port_map,\r
489         IN      OUT                     cl_fmap_t* const                        p_iou_map );\r
490 \r
491 static void\r
492 __format_dm_get(\r
493         IN              const   void* const                                     context1,\r
494         IN              const   void* const                                     context2,\r
495         IN              const   iou_path_t* const                       p_path,\r
496         IN              const   net16_t                                         attr_id,\r
497         IN              const   net32_t                                         attr_mod,\r
498         IN      OUT                     ib_mad_element_t* const         p_mad_element );\r
499 \r
500 static ib_api_status_t\r
501 __ioc_query_sa(\r
502         IN                              ioc_pnp_svc_t* const            p_svc );\r
503 \r
504 static ib_api_status_t\r
505 __query_ious(\r
506         IN                              ioc_sweep_results_t* const      p_results );\r
507 \r
508 static ib_api_status_t\r
509 __query_ioc_profiles(\r
510         IN                              ioc_sweep_results_t* const      p_results );\r
511 \r
512 static ib_api_status_t\r
513 __query_svc_entries(\r
514         IN                              ioc_sweep_results_t* const      p_results );\r
515 \r
516 static void\r
517 __update_results(\r
518         IN                              ioc_sweep_results_t* const      p_results );\r
519 \r
520 static void\r
521 __iou_info_resp(\r
522         IN      OUT                     iou_node_t* const                       p_iou,\r
523         IN              const   ib_dm_mad_t* const                      p_mad );\r
524 \r
525 static void\r
526 __ioc_profile_resp(\r
527         IN      OUT                     iou_node_t* const                       p_iou,\r
528         IN              const   ib_dm_mad_t* const                      p_mad );\r
529 \r
530 static void\r
531 __svc_entry_resp(\r
532         IN      OUT                     iou_ioc_t* const                        p_ioc,\r
533         IN              const   ib_dm_mad_t* const                      p_mad );\r
534 \r
535 /******************************************************************************\r
536 *\r
537 * Client registration and notification management\r
538 *\r
539 ******************************************************************************/\r
540 static void\r
541 __change_ious(\r
542         IN                              cl_fmap_t* const                        p_cur_ious,\r
543         IN                              cl_fmap_t* const                        p_dup_ious );\r
544 \r
545 static void\r
546 __add_ious(\r
547         IN                              cl_fmap_t* const                        p_cur_ious,\r
548         IN                              cl_fmap_t* const                        p_new_ious,\r
549         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
550 \r
551 static void\r
552 __remove_ious(\r
553         IN                              cl_fmap_t* const                        p_old_ious );\r
554 \r
555 static void\r
556 __add_iocs(\r
557         IN                              iou_node_t* const                       p_iou,\r
558         IN                              cl_qmap_t* const                        p_new_iocs,\r
559         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
560 \r
561 static void\r
562 __remove_iocs(\r
563         IN                              iou_node_t* const                       p_iou,\r
564         IN                              cl_qmap_t* const                        p_old_iocs );\r
565 \r
566 static void\r
567 __add_paths(\r
568         IN                              iou_node_t* const                       p_iou,\r
569         IN                              cl_qmap_t* const                        p_ioc_map,\r
570         IN                              cl_fmap_t* const                        p_new_paths,\r
571         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
572 \r
573 static void\r
574 __add_ioc_paths(\r
575         IN                              iou_ioc_t* const                        p_ioc,\r
576         IN                              cl_fmap_t* const                        p_new_paths,\r
577         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
578 \r
579 static void\r
580 __remove_paths(\r
581         IN                              cl_qmap_t* const                        p_ioc_map,\r
582         IN                              cl_fmap_t* const                        p_old_paths );\r
583 \r
584 static void\r
585 __report_iou_add(\r
586         IN                              iou_node_t* const                       p_iou,\r
587         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
588 \r
589 static void\r
590 __report_iou_remove(\r
591         IN                              iou_node_t* const                       p_iou );\r
592 \r
593 static void\r
594 __report_ioc_add(\r
595         IN                              iou_node_t* const                       p_iou,\r
596         IN                              iou_ioc_t* const                        p_ioc,\r
597         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
598 \r
599 static void\r
600 __report_ioc_remove(\r
601         IN                              iou_node_t* const                       p_iou,\r
602         IN                              iou_ioc_t* const                        p_ioc );\r
603 \r
604 static void\r
605 __report_path(\r
606         IN                              iou_ioc_t* const                        p_ioc,\r
607         IN                              iou_path_t* const                       p_path,\r
608         IN                              ib_pnp_event_t                          pnp_event,\r
609         IN                              al_pnp_t* const                         p_reg OPTIONAL );\r
610 \r
611 \r
612 /******************************************************************************\r
613 *\r
614 * Implementation\r
615 *\r
616 ******************************************************************************/\r
617 ib_api_status_t\r
618 create_ioc_pnp(\r
619         IN                              al_obj_t* const                         p_parent_obj )\r
620 {\r
621         ib_api_status_t         status;\r
622         ib_pnp_req_t            pnp_req;\r
623 \r
624         AL_ENTER( AL_DBG_PNP );\r
625 \r
626         CL_ASSERT( !gp_ioc_pnp );\r
627 \r
628         gp_ioc_pnp = (ioc_pnp_mgr_t*)cl_zalloc( sizeof(ioc_pnp_mgr_t) );\r
629         if( !gp_ioc_pnp )\r
630         {\r
631                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
632                         ("Failed to allocate IOC PnP manager.\n") );\r
633                 return IB_INSUFFICIENT_MEMORY;\r
634         }\r
635 \r
636         __construct_ioc_pnp( gp_ioc_pnp );\r
637 \r
638         status = __init_ioc_pnp( gp_ioc_pnp );\r
639         if( status != IB_SUCCESS )\r
640         {\r
641                 __free_ioc_pnp( &gp_ioc_pnp->obj );\r
642                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
643                         ("__construct_ioc_pnp returned %s\n", ib_get_err_str( status )) );\r
644                 return status;\r
645         }\r
646 \r
647         /* Attach to the parent object. */\r
648         status = attach_al_obj( p_parent_obj, &gp_ioc_pnp->obj );\r
649         if( status != IB_SUCCESS )\r
650         {\r
651                 gp_ioc_pnp->obj.pfn_destroy( &gp_ioc_pnp->obj, NULL );\r
652                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
653                         ("attach_al_obj returned %s.\n", ib_get_err_str(status)) );\r
654                 return status;\r
655         }\r
656 \r
657         /* Register for port PnP notifications. */\r
658         cl_memclr( &pnp_req, sizeof(pnp_req) );\r
659         pnp_req.pnp_class = IB_PNP_PORT;\r
660         pnp_req.pnp_context = gp_ioc_pnp;\r
661         pnp_req.pfn_pnp_cb = __ioc_pnp_cb;\r
662         status = ib_reg_pnp( gh_al, &pnp_req, &gp_ioc_pnp->h_pnp );\r
663         if( status != IB_SUCCESS )\r
664         {\r
665                 gp_ioc_pnp->obj.pfn_destroy( &gp_ioc_pnp->obj, NULL );\r
666                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
667                         ("ib_reg_pnp failed with status %s.\n",\r
668                         ib_get_err_str( status )) );\r
669                 return status;\r
670         }\r
671         /*\r
672          * We don't release the reference taken in init_al_obj\r
673          * since PnP deregistration is asynchronous.\r
674          */\r
675         \r
676         AL_EXIT( AL_DBG_PNP );\r
677         return IB_SUCCESS;\r
678 }\r
679 \r
680 \r
681 static void\r
682 __construct_ioc_pnp(\r
683         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr )\r
684 {\r
685         AL_ENTER( AL_DBG_PNP );\r
686 \r
687         cl_qlist_init( &p_ioc_mgr->iou_reg_list );\r
688         cl_qlist_init( &p_ioc_mgr->ioc_reg_list );\r
689         cl_fmap_init( &p_ioc_mgr->iou_map, __iou_cmp );\r
690         construct_al_obj( &p_ioc_mgr->obj, AL_OBJ_TYPE_IOC_PNP_MGR );\r
691         cl_spinlock_construct( &p_ioc_mgr->iou_pool_lock );\r
692         cl_spinlock_construct( &p_ioc_mgr->path_pool_lock );\r
693         cl_spinlock_construct( &p_ioc_mgr->ioc_pool_lock );\r
694         cl_qpool_construct( &p_ioc_mgr->iou_pool );\r
695         cl_qpool_construct( &p_ioc_mgr->path_pool );\r
696         cl_qpool_construct( &p_ioc_mgr->ioc_pool );\r
697         cl_fmap_init( &p_ioc_mgr->sweep_map, __iou_cmp );\r
698         cl_timer_construct( &p_ioc_mgr->sweep_timer );\r
699         p_ioc_mgr->async_item.pfn_callback = __ioc_async_cb;\r
700 \r
701         AL_EXIT( AL_DBG_PNP );\r
702 }\r
703 \r
704 \r
705 static ib_api_status_t\r
706 __init_ioc_pnp(\r
707         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr )\r
708 {\r
709         ib_api_status_t         status;\r
710         cl_status_t                     cl_status;\r
711 \r
712         AL_ENTER( AL_DBG_PNP );\r
713 \r
714         /* Initialize the pool locks. */\r
715         cl_status = cl_spinlock_init( &p_ioc_mgr->iou_pool_lock );\r
716         if( cl_status != CL_SUCCESS )\r
717         {\r
718                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
719                         ("cl_spinlock_init returned %s\n", cl_status_text[cl_status]) );\r
720                 return ib_convert_cl_status( cl_status );\r
721         }\r
722 \r
723         cl_status = cl_spinlock_init( &p_ioc_mgr->path_pool_lock );\r
724         if( cl_status != CL_SUCCESS )\r
725         {\r
726                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
727                         ("cl_spinlock_init returned %s\n", cl_status_text[cl_status]) );\r
728                 return ib_convert_cl_status( cl_status );\r
729         }\r
730 \r
731         cl_status = cl_spinlock_init( &p_ioc_mgr->ioc_pool_lock );\r
732         if( cl_status != CL_SUCCESS )\r
733         {\r
734                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
735                         ("cl_spinlock_init returned %s\n", cl_status_text[cl_status]) );\r
736                 return ib_convert_cl_status( cl_status );\r
737         }\r
738 \r
739         /* Initialize the pools */\r
740         cl_status = cl_qpool_init( &p_ioc_mgr->iou_pool, 0, 0, IOC_PNP_POOL_GROW,\r
741                 sizeof(iou_node_t), __init_iou, NULL, p_ioc_mgr );\r
742         if( cl_status != CL_SUCCESS )\r
743         {\r
744                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
745                         ("cl_qpool_init returned %s\n", cl_status_text[cl_status]) );\r
746                 return ib_convert_cl_status( cl_status );\r
747         }\r
748 \r
749         cl_status = cl_qpool_init( &p_ioc_mgr->path_pool, 0, 0, IOC_PNP_POOL_GROW,\r
750                 sizeof(iou_path_t), NULL, NULL, NULL );\r
751         if( cl_status != CL_SUCCESS )\r
752         {\r
753                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
754                         ("cl_qpool_init returned %s\n", cl_status_text[cl_status]) );\r
755                 return ib_convert_cl_status( cl_status );\r
756         }\r
757 \r
758         cl_status = cl_qpool_init( &p_ioc_mgr->ioc_pool, 0, 0, IOC_PNP_POOL_GROW,\r
759                 sizeof(iou_ioc_t), NULL, NULL, NULL );\r
760         if( cl_status != CL_SUCCESS )\r
761         {\r
762                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
763                         ("cl_qpool_init returned %s\n", cl_status_text[cl_status]) );\r
764                 return ib_convert_cl_status( cl_status );\r
765         }\r
766 \r
767         /* Initialize the sweep timer. */\r
768         cl_status = cl_timer_init( &p_ioc_mgr->sweep_timer,\r
769                 __ioc_pnp_timer_cb, p_ioc_mgr );\r
770         if( cl_status != CL_SUCCESS )\r
771         {\r
772                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
773                         ("cl_timer_init failed with %s\n", cl_status_text[cl_status]) );\r
774                 return ib_convert_cl_status( cl_status );\r
775         }\r
776 \r
777         status = init_al_obj( &p_ioc_mgr->obj, p_ioc_mgr, TRUE,\r
778                 __destroying_ioc_pnp, NULL, __free_ioc_pnp );\r
779         if( status != IB_SUCCESS )\r
780         {\r
781                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
782                         ("init_al_obj returned %s\n", ib_get_err_str( status )) );\r
783                 return status;\r
784         }\r
785 \r
786         AL_EXIT( AL_DBG_PNP );\r
787         return IB_SUCCESS;\r
788 }\r
789 \r
790 \r
791 static void\r
792 __destroying_ioc_pnp(\r
793         IN                              al_obj_t                                        *p_obj )\r
794 {\r
795         ib_api_status_t         status;\r
796 \r
797         AL_ENTER( AL_DBG_PNP );\r
798 \r
799         UNUSED_PARAM( p_obj );\r
800         CL_ASSERT( &gp_ioc_pnp->obj == p_obj );\r
801 \r
802         /* Stop the timer. */\r
803         cl_timer_stop( &gp_ioc_pnp->sweep_timer );\r
804 \r
805         if( gp_ioc_pnp->h_pnp )\r
806         {\r
807                 status = ib_dereg_pnp( gp_ioc_pnp->h_pnp,\r
808                         (ib_pfn_destroy_cb_t)deref_al_obj );\r
809                 CL_ASSERT( status == IB_SUCCESS );\r
810         }\r
811 \r
812         AL_EXIT( AL_DBG_PNP );\r
813 }\r
814 \r
815 \r
816 static void\r
817 __free_ioc_pnp(\r
818         IN                              al_obj_t                                        *p_obj )\r
819 {\r
820         AL_ENTER( AL_DBG_PNP );\r
821 \r
822         CL_ASSERT( &gp_ioc_pnp->obj == p_obj );\r
823 \r
824         /*\r
825          * Return all items from the maps to their pools before\r
826          * destroying the pools\r
827          */\r
828         __put_iou_map( gp_ioc_pnp, &gp_ioc_pnp->iou_map );\r
829         cl_timer_destroy( &gp_ioc_pnp->sweep_timer );\r
830         cl_qpool_destroy( &gp_ioc_pnp->ioc_pool );\r
831         cl_qpool_destroy( &gp_ioc_pnp->path_pool );\r
832         cl_qpool_destroy( &gp_ioc_pnp->iou_pool );\r
833         cl_spinlock_destroy( &gp_ioc_pnp->ioc_pool_lock );\r
834         cl_spinlock_destroy( &gp_ioc_pnp->path_pool_lock );\r
835         cl_spinlock_destroy( &gp_ioc_pnp->iou_pool_lock );\r
836         destroy_al_obj( p_obj );\r
837         cl_free( gp_ioc_pnp );\r
838         gp_ioc_pnp = NULL;\r
839 \r
840         AL_EXIT( AL_DBG_PNP );\r
841 }\r
842 \r
843 \r
844 static cl_status_t\r
845 __init_iou(\r
846         IN                              void* const                                     p_obj,\r
847         IN                              void*                                           context,\r
848                 OUT                     cl_pool_item_t** const          pp_pool_item )\r
849 {\r
850         iou_node_t      *p_iou;\r
851 \r
852         UNUSED_PARAM( context );\r
853 \r
854         p_iou = (iou_node_t*)p_obj;\r
855         \r
856         cl_spinlock_construct( &p_iou->lock );\r
857         cl_qmap_init( &p_iou->ioc_map );\r
858         cl_fmap_init( &p_iou->path_map, __path_cmp );\r
859 \r
860         *pp_pool_item = &p_iou->map_item.pool_item;\r
861         return cl_spinlock_init( &p_iou->lock );\r
862 }\r
863 \r
864 \r
865 static iou_node_t*\r
866 __get_iou(\r
867         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
868         IN              const   net64_t                                         ca_guid,\r
869         IN              const   ib_node_record_t* const         p_node_rec )\r
870 {\r
871         iou_node_t              *p_iou;\r
872         cl_pool_item_t  *p_item;\r
873 \r
874         cl_spinlock_acquire( &p_ioc_mgr->iou_pool_lock );\r
875         p_item = cl_qpool_get( &p_ioc_mgr->iou_pool );\r
876         cl_spinlock_release( &p_ioc_mgr->iou_pool_lock );\r
877         if( !p_item )\r
878                 return NULL;\r
879 \r
880         p_iou = PARENT_STRUCT( PARENT_STRUCT( p_item, cl_map_item_t, pool_item ),\r
881                 iou_node_t, map_item );\r
882 \r
883         p_iou->ca_guid = ca_guid;\r
884         p_iou->guid = p_node_rec->node_info.node_guid;\r
885         p_iou->vend_id = ib_node_info_get_vendor_id( &p_node_rec->node_info );\r
886         p_iou->dev_id = p_node_rec->node_info.device_id;\r
887         p_iou->revision = p_node_rec->node_info.revision;\r
888 \r
889         cl_memclr( &p_iou->info, sizeof(ib_iou_info_t) );\r
890 \r
891         cl_memcpy( p_iou->desc, p_node_rec->node_desc.description,\r
892                 IB_NODE_DESCRIPTION_SIZE );\r
893 \r
894         /* The terminating NULL should never get overwritten. */\r
895         CL_ASSERT( p_iou->desc[IB_NODE_DESCRIPTION_SIZE] == '\0' );\r
896 \r
897         return p_iou;\r
898 }\r
899 \r
900 \r
901 static void\r
902 __put_iou(\r
903         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
904         IN                              iou_node_t* const                       p_iou )\r
905 {\r
906         __put_path_map( p_ioc_mgr, &p_iou->path_map );\r
907         __put_ioc_map( p_ioc_mgr, &p_iou->ioc_map );\r
908 \r
909         cl_spinlock_acquire( &p_ioc_mgr->iou_pool_lock );\r
910         cl_qpool_put( &p_ioc_mgr->iou_pool, &p_iou->map_item.pool_item );\r
911         cl_spinlock_release( &p_ioc_mgr->iou_pool_lock );\r
912 }\r
913 \r
914 \r
915 static void\r
916 __put_iou_map(\r
917         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
918         IN                              cl_fmap_t* const                        p_iou_map )\r
919 {\r
920         cl_qlist_t              list;\r
921         cl_fmap_item_t  *p_item;\r
922         iou_node_t              *p_iou;\r
923 \r
924         cl_qlist_init( &list );\r
925 \r
926         p_item = cl_fmap_head( p_iou_map );\r
927         while( p_item != cl_fmap_end( p_iou_map ) )\r
928         {\r
929                 cl_fmap_remove_item( p_iou_map, p_item );\r
930 \r
931                 p_iou = PARENT_STRUCT(\r
932                         PARENT_STRUCT( p_item, cl_map_item_t, pool_item ),\r
933                         iou_node_t, map_item );\r
934 \r
935                 __put_path_map( p_ioc_mgr, &p_iou->path_map );\r
936                 __put_ioc_map( p_ioc_mgr, &p_iou->ioc_map );\r
937                 cl_qlist_insert_head( &list, &p_item->pool_item.list_item );\r
938                 p_item = cl_fmap_head( p_iou_map );\r
939         }\r
940         cl_spinlock_acquire( &p_ioc_mgr->iou_pool_lock );\r
941         cl_qpool_put_list( &p_ioc_mgr->iou_pool, &list );\r
942         cl_spinlock_release( &p_ioc_mgr->iou_pool_lock );\r
943 }\r
944 \r
945 \r
946 static iou_path_t*\r
947 __get_path(\r
948         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
949         IN              const   net64_t                                         ca_guid,\r
950         IN              const   net64_t                                         port_guid,\r
951         IN              const   ib_path_rec_t* const            p_path_rec )\r
952 {\r
953         cl_pool_item_t  *p_item;\r
954         iou_path_t              *p_path;\r
955 \r
956         cl_spinlock_acquire( &p_ioc_mgr->path_pool_lock );\r
957         p_item = cl_qpool_get( &p_ioc_mgr->path_pool );\r
958         cl_spinlock_release( &p_ioc_mgr->path_pool_lock );\r
959         if( !p_item )\r
960                 return NULL;\r
961 \r
962         p_path = PARENT_STRUCT( PARENT_STRUCT( p_item, cl_fmap_item_t, pool_item ),\r
963                 iou_path_t, map_item );\r
964 \r
965         /*\r
966          * Store the local CA and port GUID for this path to let recipients\r
967          * of a PATH_ADD event avoid a CA lookup based on GID.\r
968          */\r
969         p_path->ca_guid = ca_guid;\r
970         p_path->port_guid = port_guid;\r
971 \r
972         p_path->rec = *p_path_rec;\r
973         /* Clear the num_path field since it is just "undefined". */\r
974         p_path->rec.num_path = 0;\r
975         /*\r
976          * Clear reserved fields in case they were set to prevent undue path\r
977          * thrashing.\r
978          */\r
979         p_path->rec.resv0 = 0;\r
980         p_path->rec.resv1 = 0;\r
981         p_path->rec.resv2 = 0;\r
982 \r
983         return p_path;\r
984 }\r
985 \r
986 \r
987 static void\r
988 __put_path(\r
989         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
990         IN                              iou_path_t* const                       p_path )\r
991 {\r
992         cl_spinlock_acquire( &p_ioc_mgr->path_pool_lock );\r
993         cl_qpool_put( &p_ioc_mgr->path_pool, &p_path->map_item.pool_item );\r
994         cl_spinlock_release( &p_ioc_mgr->path_pool_lock );\r
995 }\r
996 \r
997 \r
998 static void\r
999 __put_path_map(\r
1000         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
1001         IN                              cl_fmap_t* const                        p_path_map )\r
1002 {\r
1003         cl_qlist_t              list;\r
1004         cl_fmap_item_t  *p_item;\r
1005         iou_path_t              *p_path;\r
1006 \r
1007         cl_qlist_init( &list );\r
1008 \r
1009         p_item = cl_fmap_head( p_path_map );\r
1010         while( p_item != cl_fmap_end( p_path_map ) )\r
1011         {\r
1012                 cl_fmap_remove_item( p_path_map, p_item );\r
1013 \r
1014                 p_path = PARENT_STRUCT( PARENT_STRUCT( p_item, cl_fmap_item_t, pool_item ),\r
1015                         iou_path_t, map_item );\r
1016 \r
1017                 cl_qlist_insert_head( &list, &p_item->pool_item.list_item );\r
1018                 p_item = cl_fmap_head( p_path_map );\r
1019         }\r
1020         cl_spinlock_acquire( &p_ioc_mgr->path_pool_lock );\r
1021         cl_qpool_put_list( &p_ioc_mgr->path_pool, &list );\r
1022         cl_spinlock_release( &p_ioc_mgr->path_pool_lock );\r
1023 }\r
1024 \r
1025 \r
1026 static iou_ioc_t*\r
1027 __get_ioc(\r
1028         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
1029         IN              const   uint32_t                                        ioc_slot,\r
1030         IN              const   ib_ioc_profile_t* const         p_profile )\r
1031 {\r
1032         cl_pool_item_t          *p_item;\r
1033         iou_ioc_t                       *p_ioc;\r
1034         ib_svc_entry_t          *p_svc_entries;\r
1035 \r
1036         if( !p_profile->num_svc_entries )\r
1037                 return NULL;\r
1038 \r
1039         p_svc_entries =\r
1040                 cl_zalloc( sizeof(ib_svc_entry_t) * p_profile->num_svc_entries );\r
1041         if( !p_svc_entries )\r
1042                 return NULL;\r
1043 \r
1044         cl_spinlock_acquire( &p_ioc_mgr->ioc_pool_lock );\r
1045         p_item = cl_qpool_get( &p_ioc_mgr->ioc_pool );\r
1046         cl_spinlock_release( &p_ioc_mgr->ioc_pool_lock );\r
1047         if( !p_item )\r
1048         {\r
1049                 cl_free( p_svc_entries );\r
1050                 return NULL;\r
1051         }\r
1052 \r
1053         p_ioc = PARENT_STRUCT( PARENT_STRUCT( p_item, cl_map_item_t, pool_item ),\r
1054                 iou_ioc_t, map_item );\r
1055 \r
1056         CL_ASSERT( !(ioc_slot >> 8) );\r
1057         p_ioc->slot = (uint8_t)ioc_slot;\r
1058         p_ioc->profile = *p_profile;\r
1059         p_ioc->num_valid_entries = 0;\r
1060         p_ioc->p_svc_entries = p_svc_entries;\r
1061 \r
1062         return p_ioc;\r
1063 }\r
1064 \r
1065 \r
1066 static void\r
1067 __put_ioc(\r
1068         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
1069         IN                              iou_ioc_t* const                        p_ioc )\r
1070 {\r
1071         CL_ASSERT( p_ioc->p_svc_entries );\r
1072         cl_free( p_ioc->p_svc_entries );\r
1073 \r
1074         cl_spinlock_acquire( &p_ioc_mgr->ioc_pool_lock );\r
1075         cl_qpool_put( &p_ioc_mgr->ioc_pool, &p_ioc->map_item.pool_item );\r
1076         cl_spinlock_release( &p_ioc_mgr->ioc_pool_lock );\r
1077 }\r
1078 \r
1079 \r
1080 static void\r
1081 __put_ioc_map(\r
1082         IN                              ioc_pnp_mgr_t* const            p_ioc_mgr,\r
1083         IN                              cl_qmap_t* const                        p_ioc_map )\r
1084 {\r
1085         cl_qlist_t              list;\r
1086         cl_map_item_t   *p_item;\r
1087         iou_ioc_t               *p_ioc;\r
1088 \r
1089         cl_qlist_init( &list );\r
1090 \r
1091         p_item = cl_qmap_head( p_ioc_map );\r
1092         while( p_item != cl_qmap_end( p_ioc_map ) )\r
1093         {\r
1094                 cl_qmap_remove_item( p_ioc_map, p_item );\r
1095 \r
1096                 p_ioc = PARENT_STRUCT(\r
1097                         PARENT_STRUCT( p_item, cl_map_item_t, pool_item ),\r
1098                         iou_ioc_t, map_item );\r
1099 \r
1100                 cl_free( p_ioc->p_svc_entries );\r
1101                 cl_qlist_insert_head( &list, &p_item->pool_item.list_item );\r
1102                 p_item = cl_qmap_head( p_ioc_map );\r
1103         }\r
1104         cl_spinlock_acquire( &p_ioc_mgr->ioc_pool_lock );\r
1105         cl_qpool_put_list( &p_ioc_mgr->ioc_pool, &list );\r
1106         cl_spinlock_release( &p_ioc_mgr->ioc_pool_lock );\r
1107 }\r
1108 \r
1109 \r
1110 /*\r
1111  * Compares two IOUs for inserts/lookups in a flexi map.  Keys are the\r
1112  * address of the ca_guid, which is adjacent to the node GUID of the IOU.\r
1113  * This allows for a single call to cl_memcmp.\r
1114  */\r
1115 static intn_t\r
1116 __iou_cmp(\r
1117         IN              const   void* const                                     p_key1,\r
1118         IN              const   void* const                                     p_key2 )\r
1119 {\r
1120         return cl_memcmp( p_key1, p_key2, sizeof(uint64_t) * 2 );\r
1121 }\r
1122 \r
1123 \r
1124 /*\r
1125  * Compares two paths for inserts/lookups in a flexi map.\r
1126  */\r
1127 static intn_t\r
1128 __path_cmp(\r
1129         IN              const   void* const                                     p_key1,\r
1130         IN              const   void* const                                     p_key2 )\r
1131 {\r
1132         return cl_memcmp( p_key1, p_key2, sizeof(ib_path_rec_t) );\r
1133 }\r
1134 \r
1135 \r
1136 /*\r
1137  * Removes all paths and orphaned IOC/IOUs upon a port DOWN event.\r
1138  */\r
1139 static void\r
1140 __process_port_down(\r
1141         IN              const   net64_t                                         port_guid )\r
1142 {\r
1143         cl_fmap_item_t          *p_path_item;\r
1144         cl_fmap_item_t          *p_iou_item;\r
1145         iou_node_t                      *p_iou;\r
1146         iou_path_t                      *p_path;\r
1147         cl_fmap_t                       old_paths;\r
1148         cl_fmap_t                       old_ious;\r
1149 \r
1150         AL_ENTER( AL_DBG_PNP );\r
1151 \r
1152         cl_fmap_init( &old_paths, __path_cmp );\r
1153         cl_fmap_init( &old_ious, __iou_cmp );\r
1154 \r
1155         p_iou_item = cl_fmap_head( &gp_ioc_pnp->iou_map );\r
1156         while( p_iou_item != cl_fmap_end( &gp_ioc_pnp->iou_map ) )\r
1157         {\r
1158                 p_iou = PARENT_STRUCT( p_iou_item, iou_node_t, map_item );\r
1159                 /*\r
1160                  * Note that it is safe to move to the next item even if we remove\r
1161                  * the IOU from the map since the map effectively maintains an ordered\r
1162                  * list of its contents.\r
1163                  */\r
1164                 p_iou_item = cl_fmap_next( p_iou_item );\r
1165 \r
1166                 p_path_item = cl_fmap_head( &p_iou->path_map );\r
1167                 while( p_path_item != cl_fmap_end( &p_iou->path_map ) )\r
1168                 {\r
1169                         p_path = PARENT_STRUCT( p_path_item, iou_path_t, map_item );\r
1170                         p_path_item = cl_fmap_next( p_path_item );\r
1171                         if( p_path->rec.sgid.unicast.interface_id == port_guid )\r
1172                         {\r
1173                                 cl_fmap_remove_item( &p_iou->path_map, &p_path->map_item );\r
1174                                 cl_fmap_insert( &old_paths, &p_path->rec, &p_path->map_item );\r
1175                         }\r
1176                 }\r
1177 \r
1178                 if( !cl_fmap_count( &p_iou->path_map ) )\r
1179                 {\r
1180                         /* Move the paths back to the IOU so that they get freed. */\r
1181                         cl_fmap_merge( &p_iou->path_map, &old_paths );\r
1182                         cl_fmap_remove_item( &gp_ioc_pnp->iou_map, &p_iou->map_item );\r
1183                         cl_fmap_insert( &old_ious, &p_iou->ca_guid, &p_iou->map_item );\r
1184                 }\r
1185                 else\r
1186                 {\r
1187                         /* Report the removed paths. */\r
1188                         __remove_paths( &p_iou->ioc_map, &old_paths );\r
1189                 }\r
1190         }\r
1191 \r
1192         /* Report any removed IOUs. */\r
1193         __remove_ious( &old_ious );\r
1194 \r
1195         AL_EXIT( AL_DBG_PNP );\r
1196 }\r
1197 \r
1198 \r
1199 /*\r
1200  * PnP callback for port event notifications.\r
1201  */\r
1202 static ib_api_status_t\r
1203 __ioc_pnp_cb(\r
1204         IN                              ib_pnp_rec_t                            *p_pnp_rec )\r
1205 {\r
1206         ib_api_status_t status = IB_SUCCESS;\r
1207         cl_status_t             cl_status;\r
1208 \r
1209         AL_ENTER( AL_DBG_PNP );\r
1210 \r
1211         AL_PRINT( TRACE_LEVEL_INFORMATION, AL_DBG_PNP,\r
1212                 ("p_pnp_rec->pnp_event = 0x%x (%s)\n",\r
1213                 p_pnp_rec->pnp_event, ib_get_pnp_event_str( p_pnp_rec->pnp_event )) );\r
1214 \r
1215         switch( p_pnp_rec->pnp_event )\r
1216         {\r
1217         case IB_PNP_PORT_ADD:\r
1218                 /* Create the port service. */\r
1219                 CL_ASSERT( !p_pnp_rec->context );\r
1220                 status = __create_ioc_pnp_svc( p_pnp_rec );\r
1221                 break;\r
1222 \r
1223         case IB_PNP_SM_CHANGE:\r
1224         case IB_PNP_PORT_ACTIVE:\r
1225                 /* Initiate a sweep - delay a bit to allow the ports to come up. */\r
1226                 if( g_ioc_poll_interval )\r
1227                 {\r
1228                         cl_status = cl_timer_start( &gp_ioc_pnp->sweep_timer, 250 );\r
1229                         CL_ASSERT( cl_status == CL_SUCCESS );\r
1230                 }\r
1231                 break;\r
1232 \r
1233         case IB_PNP_PORT_DOWN:\r
1234         case IB_PNP_PORT_INIT:\r
1235         case IB_PNP_PORT_ARMED:\r
1236                 CL_ASSERT( p_pnp_rec->context );\r
1237 \r
1238                 /*\r
1239                  * Report IOC and IOU remove events for any IOU/IOCs that only have\r
1240                  * paths through this port.  Note, no need to synchronize with a\r
1241                  * sweep since synchronization is provided by the PnP thread.\r
1242                  */\r
1243                 __process_port_down( p_pnp_rec->guid );\r
1244                 break;\r
1245 \r
1246         case IB_PNP_PORT_REMOVE:\r
1247                 /* Destroy the port service. */\r
1248                 ref_al_obj( &((ioc_pnp_svc_t* __ptr64)p_pnp_rec->context)->obj );\r
1249                 ((ioc_pnp_svc_t* __ptr64)p_pnp_rec->context)->obj.pfn_destroy(\r
1250                         &((ioc_pnp_svc_t* __ptr64)p_pnp_rec->context)->obj, NULL );\r
1251                 p_pnp_rec->context = NULL;\r
1252 \r
1253         default:\r
1254                 break;  /* Ignore other PNP events. */\r
1255         }\r
1256 \r
1257         AL_EXIT( AL_DBG_PNP );\r
1258         return status;\r
1259 }\r
1260 \r
1261 \r
1262 static ib_api_status_t\r
1263 __init_ioc_pnp_svc(\r
1264         IN                              ioc_pnp_svc_t* const            p_ioc_pnp_svc,\r
1265         IN              const   ib_pnp_rec_t* const                     p_pnp_rec )\r
1266 {\r
1267         ib_api_status_t         status;\r
1268         ib_ca_handle_t          h_ca;\r
1269         ib_qp_create_t          qp_create;\r
1270         ib_mad_svc_t            mad_svc;\r
1271         ib_pnp_port_rec_t       *p_pnp_port_rec;\r
1272 \r
1273         AL_ENTER( AL_DBG_PNP );\r
1274 \r
1275         p_pnp_port_rec = PARENT_STRUCT( p_pnp_rec, ib_pnp_port_rec_t, pnp_rec );\r
1276 \r
1277         /* Store the CA and port GUID so we can issue SA queries. */\r
1278         p_ioc_pnp_svc->ca_guid = p_pnp_port_rec->p_ca_attr->ca_guid;\r
1279         p_ioc_pnp_svc->port_guid = p_pnp_rec->guid;\r
1280 \r
1281         /* Acquire the correct CI CA for this port. */\r
1282         h_ca = acquire_ca( p_pnp_port_rec->p_ca_attr->ca_guid );\r
1283         if( !h_ca )\r
1284         {\r
1285                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR, ("acquire_ca failed.\n") );\r
1286                 return IB_INVALID_GUID;\r
1287         }\r
1288         p_ioc_pnp_svc->obj.p_ci_ca = h_ca->obj.p_ci_ca;\r
1289 \r
1290         /* Create the MAD QP. */\r
1291         cl_memclr( &qp_create, sizeof( ib_qp_create_t ) );\r
1292         qp_create.qp_type = IB_QPT_QP1_ALIAS;\r
1293         qp_create.sq_depth = p_pnp_port_rec->p_ca_attr->max_wrs;\r
1294         qp_create.sq_sge = 1;\r
1295         qp_create.sq_signaled = TRUE;\r
1296         /*\r
1297          * We use the IOC PnP service's al_obj_t as the context to allow using\r
1298          * deref_al_obj as the destroy callback.\r
1299          */\r
1300         status = ib_get_spl_qp( h_ca->obj.p_ci_ca->h_pd_alias,\r
1301                 p_pnp_port_rec->p_port_attr->port_guid, &qp_create,\r
1302                 &p_ioc_pnp_svc->obj, NULL, &p_ioc_pnp_svc->pool_key,\r
1303                 &p_ioc_pnp_svc->h_qp );\r
1304 \r
1305         /*\r
1306          * Release the CI CA once we've allocated the QP.  The CI CA will not\r
1307          * go away while we hold the QP.\r
1308          */\r
1309         deref_al_obj( &h_ca->obj );\r
1310 \r
1311         /* Check for failure allocating the QP. */\r
1312         if( status != IB_SUCCESS )\r
1313         {\r
1314                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1315                         ("ib_get_spl_qp failed with status %s\n",\r
1316                         ib_get_err_str( status )) );\r
1317                 return status;\r
1318         }\r
1319         /* Reference the port object on behalf of the QP. */\r
1320         ref_al_obj( &p_ioc_pnp_svc->obj );\r
1321 \r
1322         /* Create the MAD service. */\r
1323         cl_memclr( &mad_svc, sizeof(ib_mad_svc_t) );\r
1324         mad_svc.mad_svc_context = p_ioc_pnp_svc;\r
1325         mad_svc.pfn_mad_recv_cb = __ioc_pnp_recv_cb;\r
1326         mad_svc.pfn_mad_send_cb = __ioc_pnp_send_cb;\r
1327         status =\r
1328                 ib_reg_mad_svc( p_ioc_pnp_svc->h_qp, &mad_svc,\r
1329                 &p_ioc_pnp_svc->h_mad_svc );\r
1330         if( status != IB_SUCCESS )\r
1331         {\r
1332                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1333                         ("ib_reg_mad_svc failed with status %s\n",\r
1334                         ib_get_err_str( status )) );\r
1335                 return status;\r
1336         }\r
1337 \r
1338         AL_EXIT( AL_DBG_PNP );\r
1339         return IB_SUCCESS;\r
1340 }\r
1341 \r
1342 \r
1343 /*\r
1344  * Create a port agent for a given port.\r
1345  */\r
1346 static ib_api_status_t\r
1347 __create_ioc_pnp_svc(\r
1348         IN                              ib_pnp_rec_t                            *p_pnp_rec )\r
1349 {\r
1350         ioc_pnp_svc_t           *p_ioc_pnp_svc;\r
1351         ib_api_status_t         status;\r
1352 \r
1353         AL_ENTER( AL_DBG_PNP );\r
1354 \r
1355         /* calculate size of port_cm struct */\r
1356         p_ioc_pnp_svc = (ioc_pnp_svc_t*)cl_zalloc( sizeof(ioc_pnp_svc_t) );\r
1357         if( !p_ioc_pnp_svc )\r
1358         {\r
1359                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1360                         ("Failed to cl_zalloc port CM agent.\n") );\r
1361                 return IB_INSUFFICIENT_MEMORY;\r
1362         }\r
1363 \r
1364         construct_al_obj( &p_ioc_pnp_svc->obj, AL_OBJ_TYPE_IOC_PNP_SVC );\r
1365 \r
1366         status = init_al_obj( &p_ioc_pnp_svc->obj, p_ioc_pnp_svc, TRUE,\r
1367                 __destroying_ioc_pnp_svc, NULL, __free_ioc_pnp_svc );\r
1368         if( status != IB_SUCCESS )\r
1369         {\r
1370                 __free_ioc_pnp_svc( &p_ioc_pnp_svc->obj );\r
1371                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1372                         ("init_al_obj failed with status %s.\n",\r
1373                         ib_get_err_str( status )) );\r
1374                 return status;\r
1375         }\r
1376 \r
1377         /* Attach to the global CM object. */\r
1378         status = attach_al_obj( &gp_ioc_pnp->obj, &p_ioc_pnp_svc->obj );\r
1379         if( status != IB_SUCCESS )\r
1380         {\r
1381                 p_ioc_pnp_svc->obj.pfn_destroy( &p_ioc_pnp_svc->obj, NULL );\r
1382                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1383                         ("attach_al_obj returned %s.\n", ib_get_err_str(status)) );\r
1384                 return status;\r
1385         }\r
1386 \r
1387         status = __init_ioc_pnp_svc( p_ioc_pnp_svc, p_pnp_rec );\r
1388         if( status != IB_SUCCESS )\r
1389         {\r
1390                 p_ioc_pnp_svc->obj.pfn_destroy( &p_ioc_pnp_svc->obj, NULL );\r
1391                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1392                         ("__init_data_svc failed with status %s.\n",\r
1393                         ib_get_err_str( status )) );\r
1394                 return status;\r
1395         }\r
1396 \r
1397         /* Set the PnP context to reference this service. */\r
1398         p_pnp_rec->context = p_ioc_pnp_svc;\r
1399 \r
1400         /* Release the reference taken in init_al_obj. */\r
1401         deref_al_obj( &p_ioc_pnp_svc->obj );\r
1402 \r
1403         AL_EXIT( AL_DBG_PNP );\r
1404         return status;\r
1405 }\r
1406 \r
1407 \r
1408 static void\r
1409 __destroying_ioc_pnp_svc(\r
1410         IN                              al_obj_t                                        *p_obj )\r
1411 {\r
1412         ib_api_status_t         status;\r
1413         ioc_pnp_svc_t           *p_svc;\r
1414 \r
1415         CL_ASSERT( p_obj );\r
1416         p_svc = PARENT_STRUCT( p_obj, ioc_pnp_svc_t, obj );\r
1417 \r
1418         if( p_svc->h_node_query )\r
1419                 ib_cancel_query( gh_al, p_svc->h_node_query );\r
1420 \r
1421         if( p_svc->h_path_query )\r
1422                 ib_cancel_query( gh_al, p_svc->h_path_query );\r
1423 \r
1424         /* Destroy the QP. */\r
1425         if( p_svc->h_qp )\r
1426         {\r
1427                 status =\r
1428                         ib_destroy_qp( p_svc->h_qp, (ib_pfn_destroy_cb_t)deref_al_obj );\r
1429                 CL_ASSERT( status == IB_SUCCESS );\r
1430         }\r
1431 }\r
1432 \r
1433 \r
1434 static void\r
1435 __free_ioc_pnp_svc(\r
1436         IN                              al_obj_t                                        *p_obj )\r
1437 {\r
1438         ioc_pnp_svc_t*          p_svc;\r
1439 \r
1440         CL_ASSERT( p_obj );\r
1441         p_svc = PARENT_STRUCT( p_obj, ioc_pnp_svc_t, obj );\r
1442 \r
1443         CL_ASSERT( !p_svc->query_cnt );\r
1444 \r
1445         destroy_al_obj( p_obj );\r
1446         cl_free( p_svc );\r
1447 }\r
1448 \r
1449 \r
1450 static void\r
1451 __ioc_pnp_timer_cb(\r
1452         IN                              void                                            *context )\r
1453 {\r
1454         ib_api_status_t         status;\r
1455         ioc_pnp_mgr_t           *p_mgr;\r
1456         cl_list_item_t          *p_item;\r
1457         ioc_pnp_svc_t           *p_svc;\r
1458 \r
1459         AL_ENTER( AL_DBG_PNP );\r
1460 \r
1461         p_mgr = (ioc_pnp_mgr_t*)context;\r
1462 \r
1463         cl_spinlock_acquire( &p_mgr->obj.lock );\r
1464         if( p_mgr->obj.state == CL_DESTROYING )\r
1465         {\r
1466                 AL_PRINT_EXIT( TRACE_LEVEL_INFORMATION, AL_DBG_PNP,\r
1467                         ("Destroying - not resetting timer.\n") );\r
1468                 cl_spinlock_release( &p_mgr->obj.lock );\r
1469                 return;\r
1470         }\r
1471 \r
1472         CL_ASSERT( !cl_fmap_count( &p_mgr->sweep_map ) );\r
1473 \r
1474         /* Pre-charge the ref count so that we don't toggle between 0 and 1. */\r
1475         cl_atomic_inc( &p_mgr->query_cnt );\r
1476         /* Take a reference on the object for the duration of the sweep process. */\r
1477         ref_al_obj( &p_mgr->obj );\r
1478         for( p_item = cl_qlist_head( &p_mgr->obj.obj_list );\r
1479                 p_item != cl_qlist_end( &p_mgr->obj.obj_list );\r
1480                 p_item = cl_qlist_next( p_item ) )\r
1481         {\r
1482                 p_svc = PARENT_STRUCT( PARENT_STRUCT( p_item, al_obj_t, pool_item ),\r
1483                         ioc_pnp_svc_t, obj );\r
1484                 cl_atomic_inc( &p_mgr->query_cnt );\r
1485                 status = __ioc_query_sa( p_svc );\r
1486                 if( status != IB_SUCCESS )\r
1487                         cl_atomic_dec( &p_mgr->query_cnt );\r
1488         }\r
1489         /* Release the reference we took and see if we're done sweeping. */\r
1490         if( !cl_atomic_dec( &p_mgr->query_cnt ) )\r
1491                 cl_async_proc_queue( gp_async_pnp_mgr, &p_mgr->async_item );\r
1492 \r
1493         cl_spinlock_release( &p_mgr->obj.lock );\r
1494 \r
1495         AL_EXIT( AL_DBG_PNP );\r
1496 }\r
1497 \r
1498 \r
1499 static ib_api_status_t\r
1500 __ioc_query_sa(\r
1501         IN                              ioc_pnp_svc_t* const            p_svc )\r
1502 {\r
1503         ib_api_status_t         status = IB_NOT_DONE;\r
1504         ib_query_req_t          query;\r
1505         ib_user_query_t         info;\r
1506         union _ioc_pnp_timer_cb_u\r
1507         {\r
1508                 ib_node_record_t        node_rec;\r
1509                 ib_path_rec_t           path_rec;\r
1510 \r
1511         }       u;\r
1512 \r
1513         AL_ENTER( AL_DBG_PNP );\r
1514 \r
1515         if( p_svc->h_node_query )\r
1516                 return IB_NOT_DONE;\r
1517         if( p_svc->h_path_query )\r
1518                 return IB_NOT_DONE;\r
1519 \r
1520         if( p_svc->obj.state == CL_DESTROYING )\r
1521         {\r
1522                 AL_PRINT_EXIT( TRACE_LEVEL_INFORMATION, AL_DBG_PNP,\r
1523                         ("Destroying - not resetting timer.\n") );\r
1524                 return IB_NOT_DONE;\r
1525         }\r
1526 \r
1527         info.method = IB_MAD_METHOD_GETTABLE;\r
1528         info.attr_id = IB_MAD_ATTR_NODE_RECORD;\r
1529         info.attr_size = sizeof(ib_node_record_t);\r
1530         info.comp_mask = IB_NR_COMPMASK_NODETYPE;\r
1531         info.p_attr = &u.node_rec;\r
1532 \r
1533         cl_memclr( &u.node_rec, sizeof(ib_node_record_t) );\r
1534         u.node_rec.node_info.node_type = IB_NODE_TYPE_CA;\r
1535 \r
1536         cl_memclr( &query, sizeof(ib_query_req_t) );\r
1537         query.query_type = IB_QUERY_USER_DEFINED;\r
1538         query.p_query_input = &info;\r
1539         query.port_guid = p_svc->port_guid;\r
1540         query.timeout_ms = g_ioc_query_timeout;\r
1541         query.retry_cnt = g_ioc_query_retries;\r
1542         query.query_context = p_svc;\r
1543         query.pfn_query_cb = __node_rec_cb;\r
1544 \r
1545         /* Reference the service for the node record query. */\r
1546         ref_al_obj( &p_svc->obj );\r
1547         cl_atomic_inc( &p_svc->query_cnt );\r
1548 \r
1549         status = ib_query( gh_al, &query, &p_svc->h_node_query );\r
1550         if( status != IB_SUCCESS )\r
1551         {\r
1552                 cl_atomic_dec( &p_svc->query_cnt );\r
1553                 deref_al_obj( &p_svc->obj );\r
1554                 AL_PRINT_EXIT( TRACE_LEVEL_WARNING, AL_DBG_PNP,\r
1555                         ("ib_query returned %s\n", ib_get_err_str( status )) );\r
1556                 return status;\r
1557         }\r
1558 \r
1559         /* Setup the path query. */\r
1560         info.method = IB_MAD_METHOD_GETTABLE;\r
1561         info.attr_id = IB_MAD_ATTR_PATH_RECORD;\r
1562         info.attr_size = sizeof(ib_path_rec_t);\r
1563         info.comp_mask = IB_PR_COMPMASK_SGID | IB_PR_COMPMASK_NUM_PATH;\r
1564         info.p_attr = &u.path_rec;\r
1565 \r
1566         cl_memclr( &u.path_rec, sizeof(ib_path_rec_t) );\r
1567         ib_gid_set_default( &u.path_rec.sgid, p_svc->port_guid );\r
1568         /* Request all the paths available, setting the reversible bit. */\r
1569         u.path_rec.num_path = 0xFF;\r
1570 \r
1571         query.pfn_query_cb = __path_rec_cb;\r
1572 \r
1573         /* Reference the service for the node record query. */\r
1574         ref_al_obj( &p_svc->obj );\r
1575         cl_atomic_inc( &p_svc->query_cnt );\r
1576 \r
1577         status = ib_query( gh_al, &query, &p_svc->h_path_query );\r
1578         if( status != IB_SUCCESS )\r
1579         {\r
1580                 cl_atomic_dec( &p_svc->query_cnt );\r
1581                 deref_al_obj( &p_svc->obj );\r
1582                 AL_PRINT_EXIT( TRACE_LEVEL_WARNING, AL_DBG_PNP,\r
1583                         ("ib_query returned %s\n", ib_get_err_str( status )) );\r
1584         }\r
1585 \r
1586         AL_EXIT( AL_DBG_PNP );\r
1587         return IB_SUCCESS;\r
1588 }\r
1589 \r
1590 \r
1591 static void\r
1592 __node_rec_cb(\r
1593         IN                              ib_query_rec_t                          *p_query_rec )\r
1594 {\r
1595         ioc_pnp_svc_t           *p_svc;\r
1596 \r
1597         AL_ENTER( AL_DBG_PNP );\r
1598 \r
1599         p_svc = (ioc_pnp_svc_t* __ptr64)p_query_rec->query_context;\r
1600 \r
1601         if( p_svc->obj.state != CL_DESTROYING &&\r
1602                 p_query_rec->status == IB_SUCCESS && p_query_rec->result_cnt )\r
1603         {\r
1604                 CL_ASSERT( p_query_rec->p_result_mad );\r
1605                 CL_ASSERT( !p_svc->p_node_element );\r
1606                 CL_ASSERT( p_query_rec->p_result_mad->p_next == NULL );\r
1607                 p_svc->p_node_element = p_query_rec->p_result_mad;\r
1608                 p_svc->num_nodes = p_query_rec->result_cnt;\r
1609         }\r
1610         else if( p_query_rec->p_result_mad )\r
1611         {\r
1612                 ib_put_mad( p_query_rec->p_result_mad );\r
1613         }\r
1614 \r
1615         p_svc->h_node_query = NULL;\r
1616         if( !cl_atomic_dec( &p_svc->query_cnt ) )\r
1617         {\r
1618                 /* The path query has already completed.  Process the results. */\r
1619                 __process_query( p_svc );\r
1620         }\r
1621 \r
1622         /* Release the reference taken for the query. */\r
1623         deref_al_obj( &p_svc->obj );\r
1624 \r
1625         AL_EXIT( AL_DBG_PNP );\r
1626 }\r
1627 \r
1628 \r
1629 static void\r
1630 __path_rec_cb(\r
1631         IN                              ib_query_rec_t                          *p_query_rec )\r
1632 {\r
1633         ioc_pnp_svc_t           *p_svc;\r
1634 \r
1635         AL_ENTER( AL_DBG_PNP );\r
1636 \r
1637         p_svc = (ioc_pnp_svc_t* __ptr64)p_query_rec->query_context;\r
1638 \r
1639         if( p_svc->obj.state != CL_DESTROYING &&\r
1640                 p_query_rec->status == IB_SUCCESS && p_query_rec->result_cnt )\r
1641         {\r
1642                 CL_ASSERT( p_query_rec->p_result_mad );\r
1643                 CL_ASSERT( !p_svc->p_path_element );\r
1644                 CL_ASSERT( p_query_rec->p_result_mad->p_next == NULL );\r
1645                 p_svc->p_path_element = p_query_rec->p_result_mad;\r
1646                 p_svc->num_paths = p_query_rec->result_cnt;\r
1647         }\r
1648         else if( p_query_rec->p_result_mad )\r
1649         {\r
1650                 ib_put_mad( p_query_rec->p_result_mad );\r
1651         }\r
1652 \r
1653         p_svc->h_path_query = NULL;\r
1654         if( !cl_atomic_dec( &p_svc->query_cnt ) )\r
1655         {\r
1656                 /* The node query has already completed.  Process the results. */\r
1657                 __process_query( p_svc );\r
1658         }\r
1659 \r
1660         /* Release the reference taken for the query. */\r
1661         deref_al_obj( &p_svc->obj );\r
1662 \r
1663         AL_EXIT( AL_DBG_PNP );\r
1664 }\r
1665 \r
1666 static void\r
1667 __process_query(\r
1668         IN                              ioc_pnp_svc_t* const            p_svc )\r
1669 {\r
1670         ib_api_status_t                 status;\r
1671         ioc_sweep_results_t             *p_results;\r
1672         cl_qmap_t                               port_map;\r
1673 \r
1674         AL_ENTER( AL_DBG_PNP );\r
1675 \r
1676         cl_qmap_init( &port_map );\r
1677 \r
1678         if( !p_svc->p_node_element || !p_svc->p_path_element )\r
1679         {\r
1680                 /* One of the queries failed.  Release the MADs and reset the timer. */\r
1681                 if( p_svc->p_node_element )\r
1682                 {\r
1683                         ib_put_mad( p_svc->p_node_element );\r
1684                         p_svc->p_node_element = NULL;\r
1685                 }\r
1686 \r
1687                 if( p_svc->p_path_element )\r
1688                 {\r
1689                         ib_put_mad( p_svc->p_path_element );\r
1690                         p_svc->p_path_element = NULL;\r
1691                 }\r
1692 \r
1693                 /* Decrement the IOC PnP manager's query count. */\r
1694                 if( !cl_atomic_dec( &gp_ioc_pnp->query_cnt ) )\r
1695                         cl_async_proc_queue( gp_async_pnp_mgr, &gp_ioc_pnp->async_item );\r
1696                 return;\r
1697         }\r
1698 \r
1699         /*\r
1700          * Allocate the sweep results structure to allow processing\r
1701          * asynchronously.\r
1702          */\r
1703         p_results = cl_zalloc( sizeof(ioc_sweep_results_t) );\r
1704         if( p_results )\r
1705         {\r
1706                 p_results->async_item.pfn_callback = __process_sweep;\r
1707                 p_results->p_svc = p_svc;\r
1708                 cl_fmap_init( &p_results->iou_map, __iou_cmp );\r
1709 \r
1710                 /* Build the map of nodes by port GUID. */\r
1711                 __process_nodes( p_svc, &port_map );\r
1712 \r
1713                 /* Build the map of paths for each node. */\r
1714                 __process_paths( p_svc, &port_map );\r
1715 \r
1716                 /* Collapse the map of nodes to be keyed by node GUID. */\r
1717                 __build_iou_map( &port_map, &p_results->iou_map );\r
1718 \r
1719                 /* Send the IOU Info queries to the nodes. */\r
1720                 status = __query_ious( p_results );\r
1721         }\r
1722         else\r
1723         {\r
1724                 status = IB_INSUFFICIENT_MEMORY;\r
1725         }\r
1726 \r
1727         /* Release the query result MADs now that we're done with them. */\r
1728         ib_put_mad( p_svc->p_node_element );\r
1729         ib_put_mad( p_svc->p_path_element );\r
1730         p_svc->p_node_element = NULL;\r
1731         p_svc->p_path_element = NULL;\r
1732 \r
1733         switch( status )\r
1734         {\r
1735         case IB_SUCCESS:\r
1736                 break;\r
1737         default:\r
1738                 CL_ASSERT( p_results );\r
1739                 cl_free( p_results );\r
1740                 /* Fall through */\r
1741         case IB_INSUFFICIENT_MEMORY:\r
1742                 /* Decrement the IOC PnP manager's query count. */\r
1743                 if( !cl_atomic_dec( &gp_ioc_pnp->query_cnt ) )\r
1744                         cl_async_proc_queue( gp_async_pnp_mgr, &gp_ioc_pnp->async_item );\r
1745         }\r
1746         AL_EXIT( AL_DBG_PNP );\r
1747 }\r
1748 \r
1749 \r
1750 static void\r
1751 __process_nodes(\r
1752         IN                              ioc_pnp_svc_t* const            p_svc,\r
1753         IN                              cl_qmap_t* const                        p_port_map )\r
1754 {\r
1755         iou_node_t                      *p_iou;\r
1756         ib_node_record_t        *p_node_rec;\r
1757         uint32_t                        i;\r
1758         void                            *p_item;\r
1759 \r
1760         AL_ENTER( AL_DBG_PNP );\r
1761 \r
1762         CL_ASSERT( p_svc );\r
1763         CL_ASSERT( p_svc->p_node_element );\r
1764         CL_ASSERT( p_port_map );\r
1765 \r
1766         for( i = 0; i < p_svc->num_nodes; i++ )\r
1767         {\r
1768                 p_node_rec = ib_get_query_node_rec( p_svc->p_node_element, i );\r
1769 \r
1770                 p_iou = __get_iou( gp_ioc_pnp, p_svc->ca_guid, p_node_rec );\r
1771                 if( !p_iou )\r
1772                         break;\r
1773 \r
1774                 /*\r
1775                  * We insert by port GUID, not node GUID so that we can match\r
1776                  * to paths using DGID.  Note that it is safe to cast between\r
1777                  * a flexi-map item and a map item since the pointer to the key\r
1778                  * in a flexi-map item is always a 64-bit pointer.\r
1779                  */\r
1780                 p_item = cl_qmap_insert(\r
1781                         p_port_map, p_node_rec->node_info.port_guid,\r
1782                         (cl_map_item_t*)&p_iou->map_item );\r
1783                 if( p_item != &p_iou->map_item )\r
1784                 {\r
1785                         /* Duplicate node - discard. */\r
1786                         __put_iou( gp_ioc_pnp, p_iou );\r
1787                 }\r
1788         }\r
1789 \r
1790         AL_EXIT( AL_DBG_PNP );\r
1791 }\r
1792 \r
1793 \r
1794 static void\r
1795 __process_paths(\r
1796         IN                              ioc_pnp_svc_t* const            p_svc,\r
1797         IN                              cl_qmap_t* const                        p_port_map )\r
1798 {\r
1799         iou_node_t                      *p_iou;\r
1800         iou_path_t                      *p_path;\r
1801         ib_path_rec_t           *p_path_rec;\r
1802         uint32_t                        i;\r
1803         cl_map_item_t           *p_iou_item;\r
1804         cl_fmap_item_t          *p_item;\r
1805 \r
1806         AL_ENTER( AL_DBG_PNP );\r
1807 \r
1808         CL_ASSERT( p_svc );\r
1809         CL_ASSERT( p_svc->p_node_element );\r
1810         CL_ASSERT( p_port_map );\r
1811 \r
1812         for( i = 0; i < p_svc->num_paths; i++ )\r
1813         {\r
1814                 p_path_rec = ib_get_query_path_rec( p_svc->p_path_element, i );\r
1815 \r
1816                 p_iou_item =\r
1817                         cl_qmap_get( p_port_map, p_path_rec->dgid.unicast.interface_id );\r
1818                 if( p_iou_item == cl_qmap_end( p_port_map ) )\r
1819                         continue;\r
1820 \r
1821                 p_iou = PARENT_STRUCT( p_iou_item, iou_node_t, map_item );\r
1822 \r
1823                 p_path = __get_path( gp_ioc_pnp, p_svc->ca_guid,\r
1824                         p_svc->port_guid, p_path_rec );\r
1825                 if( !p_path )\r
1826                         break;\r
1827 \r
1828                 p_item = cl_fmap_insert( &p_iou->path_map, &p_path->rec,\r
1829                         &p_path->map_item );\r
1830                 if( p_item != &p_path->map_item )\r
1831                 {\r
1832                         /* Duplicate path - discard. */\r
1833                         __put_path( gp_ioc_pnp, p_path );\r
1834                 }\r
1835         }\r
1836 \r
1837         AL_EXIT( AL_DBG_PNP );\r
1838 }\r
1839 \r
1840 \r
1841 static void\r
1842 __build_iou_map(\r
1843         IN                              cl_qmap_t* const                        p_port_map,\r
1844         IN      OUT                     cl_fmap_t* const                        p_iou_map )\r
1845 {\r
1846         cl_fmap_t               map1, map2;\r
1847         void                    *p_item;\r
1848         iou_node_t              *p_iou, *p_dup;\r
1849 \r
1850         AL_ENTER( AL_DBG_PNP );\r
1851 \r
1852         CL_ASSERT( !cl_fmap_count( p_iou_map ) );\r
1853 \r
1854         cl_fmap_init( &map1, __path_cmp );\r
1855         cl_fmap_init( &map2, __path_cmp );\r
1856 \r
1857         /*\r
1858          * Now collapse the map so that IOUs aren't repeated.\r
1859          * This is needed because the IOU map is keyed by port GUID, and thus\r
1860          * a multi-port IOU could be listed twice.\r
1861          */\r
1862         /* Merge the port map into a map of IOUs. */\r
1863         for( p_item = cl_qmap_head( p_port_map );\r
1864                 p_item != cl_qmap_end( p_port_map );\r
1865                 p_item = cl_qmap_head( p_port_map ) )\r
1866         {\r
1867                 cl_qmap_remove_item( p_port_map, (cl_map_item_t*)p_item );\r
1868                 p_iou = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
1869 \r
1870                 p_item = cl_fmap_insert( p_iou_map, &p_iou->ca_guid, p_item );\r
1871                 if( p_item != &p_iou->map_item )\r
1872                 {\r
1873                         /* Duplicate IOU information - merge the paths. */\r
1874                         p_dup = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
1875                         CL_ASSERT( p_dup != p_iou );\r
1876                         cl_fmap_delta( &p_dup->path_map, &p_iou->path_map, &map1, &map2 );\r
1877                         /*\r
1878                          * The path map in p_iou->path_map is duplicate paths.\r
1879                          * map1 contains paths unique to p_iou->path_map, map2 contains\r
1880                          * paths unique to p_dup->path_map.  Add the unique paths back to\r
1881                          * p_dup->path_map since that IOU is already in the IOU map.\r
1882                          * Note that we are keeping the p_dup IOU node.\r
1883                          */\r
1884                         cl_fmap_merge( &p_dup->path_map, &map1 );\r
1885                         cl_fmap_merge( &p_dup->path_map, &map2 );\r
1886                         /* All unique items should have merged without duplicates. */\r
1887                         CL_ASSERT( !cl_fmap_count( &map1 ) );\r
1888                         CL_ASSERT( !cl_fmap_count( &map2 ) );\r
1889 \r
1890                         __put_iou( gp_ioc_pnp, p_iou );\r
1891                 }\r
1892         }\r
1893 \r
1894         AL_EXIT( AL_DBG_PNP );\r
1895 }\r
1896 \r
1897 \r
1898 static void\r
1899 __format_dm_get(\r
1900         IN              const   void* const                                     context1,\r
1901         IN              const   void* const                                     context2,\r
1902         IN              const   iou_path_t* const                       p_path,\r
1903         IN              const   net16_t                                         attr_id,\r
1904         IN              const   net32_t                                         attr_mod,\r
1905         IN      OUT                     ib_mad_element_t* const         p_mad_element )\r
1906 {\r
1907         static uint64_t         tid = 0;\r
1908 \r
1909         AL_ENTER( AL_DBG_PNP );\r
1910 \r
1911         /*\r
1912          * Context information so that we can continue processing when\r
1913          * the query completes.\r
1914          */\r
1915         p_mad_element->context1 = context1;\r
1916         p_mad_element->context2 = context2;\r
1917 \r
1918         /*\r
1919          * Set the addressing bits necessary for the mad service to\r
1920          * create the address vector\r
1921          */\r
1922         p_mad_element->h_av = NULL;\r
1923         p_mad_element->remote_sl = ib_path_rec_sl( &p_path->rec );\r
1924         p_mad_element->remote_lid = p_path->rec.dlid;\r
1925         p_mad_element->grh_valid = FALSE;\r
1926         p_mad_element->path_bits = p_path->rec.num_path;\r
1927 \r
1928         /* Request response processing. */\r
1929         p_mad_element->resp_expected = TRUE;\r
1930         p_mad_element->retry_cnt = g_ioc_query_retries;\r
1931         p_mad_element->timeout_ms = g_ioc_query_timeout;\r
1932 \r
1933         /* Set the destination information for the send. */\r
1934         p_mad_element->remote_qp = IB_QP1;\r
1935         p_mad_element->remote_qkey = IB_QP1_WELL_KNOWN_Q_KEY;\r
1936 \r
1937         /* Format the MAD payload. */\r
1938         cl_memclr( p_mad_element->p_mad_buf, sizeof(ib_dm_mad_t) );\r
1939         ib_mad_init_new( p_mad_element->p_mad_buf, IB_MCLASS_DEV_MGMT, 1,\r
1940                 IB_MAD_METHOD_GET, cl_ntoh64( tid++ ), attr_id, attr_mod );\r
1941 \r
1942         AL_EXIT( AL_DBG_PNP );\r
1943 }\r
1944 \r
1945 \r
1946 static ib_api_status_t\r
1947 __query_ious(\r
1948         IN                              ioc_sweep_results_t* const      p_results )\r
1949 {\r
1950         ib_api_status_t         status;\r
1951         iou_node_t                      *p_iou;\r
1952         iou_path_t                      *p_path;\r
1953         cl_fmap_item_t          *p_iou_item;\r
1954         cl_fmap_item_t          *p_path_item;\r
1955         ib_mad_element_t        *p_mad, *p_mad_list = NULL;\r
1956 \r
1957         AL_ENTER( AL_DBG_PNP );\r
1958 \r
1959         p_results->state = SWEEP_IOU_INFO;\r
1960 \r
1961         /* Send a IOU Info query on the first path to every IOU. */\r
1962         p_iou_item = cl_fmap_head( &p_results->iou_map );\r
1963         while( p_iou_item != cl_fmap_end( &p_results->iou_map ) )\r
1964         {\r
1965                 p_iou = PARENT_STRUCT( p_iou_item, iou_node_t, map_item );\r
1966                 p_iou_item = cl_fmap_next( p_iou_item );\r
1967                 if( !cl_fmap_count( &p_iou->path_map ) )\r
1968                 {\r
1969                         /* No paths for this node.  Discard it. */\r
1970                         cl_fmap_remove_item( &p_results->iou_map, &p_iou->map_item );\r
1971                         __put_iou( gp_ioc_pnp, p_iou );\r
1972                         continue;\r
1973                 }\r
1974 \r
1975                 p_path_item = cl_fmap_head( &p_iou->path_map );\r
1976 \r
1977                 p_path = PARENT_STRUCT( p_path_item, iou_path_t, map_item );\r
1978 \r
1979                 status = ib_get_mad( p_results->p_svc->pool_key,\r
1980                         MAD_BLOCK_SIZE, &p_mad );\r
1981                 if( status != IB_SUCCESS )\r
1982                 {\r
1983                         AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
1984                                 ("ib_get_mad for IOU Info query returned %s.\n",\r
1985                                 ib_get_err_str( status )) );\r
1986                         break;\r
1987                 }\r
1988 \r
1989                 p_iou->p_config_path = p_path;\r
1990                 __format_dm_get( p_results, p_iou, p_path,\r
1991                         IB_MAD_ATTR_IO_UNIT_INFO, 0, p_mad );\r
1992 \r
1993                 /* Link the elements together. */\r
1994                 p_mad->p_next = p_mad_list;\r
1995                 p_mad_list = p_mad;\r
1996 \r
1997                 cl_atomic_inc( &p_results->p_svc->query_cnt );\r
1998         }\r
1999 \r
2000         if( !p_mad_list )\r
2001         {\r
2002                 AL_EXIT( AL_DBG_PNP );\r
2003                 return IB_ERROR;\r
2004         }\r
2005 \r
2006         status = ib_send_mad( p_results->p_svc->h_mad_svc, p_mad_list, &p_mad );\r
2007         if( status != IB_SUCCESS )\r
2008         {\r
2009                 AL_PRINT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
2010                         ("ib_send_mad returned %s\n", ib_get_err_str( status )) );\r
2011 \r
2012                 /* If some sends succeeded, change the status. */\r
2013                 if( p_mad_list != p_mad )\r
2014                         status = IB_SUCCESS;\r
2015 \r
2016                 while( p_mad )\r
2017                 {\r
2018                         p_mad_list = p_mad->p_next;\r
2019                         p_mad->p_next = NULL;\r
2020                         ib_put_mad( p_mad );\r
2021                         if( !cl_atomic_dec( &p_results->p_svc->query_cnt ) &&\r
2022                                 status == IB_SUCCESS )\r
2023                         {\r
2024                                 cl_async_proc_queue( gp_async_pnp_mgr,\r
2025                                         &p_results->async_item );\r
2026                         }\r
2027                         p_mad = p_mad_list;\r
2028                 }\r
2029         }\r
2030         AL_EXIT( AL_DBG_PNP );\r
2031         return status;\r
2032 }\r
2033 \r
2034 \r
2035 static void\r
2036 __ioc_pnp_recv_cb(\r
2037         IN              const   ib_mad_svc_handle_t                     h_mad_svc,\r
2038         IN                              void                                            *mad_svc_context,\r
2039         IN                              ib_mad_element_t                        *p_mad_response )\r
2040 {\r
2041         ioc_sweep_results_t     *p_results;\r
2042         iou_node_t                      *p_iou;\r
2043         iou_ioc_t                       *p_ioc;\r
2044 \r
2045         AL_ENTER( AL_DBG_PNP );\r
2046 \r
2047         UNUSED_PARAM( h_mad_svc );\r
2048         UNUSED_PARAM( mad_svc_context );\r
2049         CL_ASSERT( !p_mad_response->p_next );\r
2050 \r
2051         p_results = (ioc_sweep_results_t* __ptr64)p_mad_response->send_context1;\r
2052         if( !p_mad_response->p_mad_buf->status )\r
2053         {\r
2054                 /* Query was successful */\r
2055                 switch( p_mad_response->p_mad_buf->attr_id )\r
2056                 {\r
2057                 case IB_MAD_ATTR_IO_UNIT_INFO:\r
2058                         p_iou = (iou_node_t* __ptr64)p_mad_response->send_context2;\r
2059                         __iou_info_resp( p_iou,\r
2060                                 (ib_dm_mad_t*)p_mad_response->p_mad_buf );\r
2061                         break;\r
2062 \r
2063                 case IB_MAD_ATTR_IOC_PROFILE:\r
2064                         p_iou = (iou_node_t* __ptr64)p_mad_response->send_context2;\r
2065                         __ioc_profile_resp( p_iou,\r
2066                                 (ib_dm_mad_t*)p_mad_response->p_mad_buf );\r
2067                         break;\r
2068 \r
2069                 case IB_MAD_ATTR_SERVICE_ENTRIES:\r
2070                         p_ioc = (iou_ioc_t* __ptr64)p_mad_response->send_context2;\r
2071                         __svc_entry_resp( p_ioc,\r
2072                                 (ib_dm_mad_t*)p_mad_response->p_mad_buf );\r
2073                         break;\r
2074 \r
2075                 default:\r
2076                         break;\r
2077                 }\r
2078         }\r
2079 \r
2080         ib_put_mad( p_mad_response );\r
2081         AL_EXIT( AL_DBG_PNP );\r
2082 }\r
2083 \r
2084 \r
2085 static void\r
2086 __iou_info_resp(\r
2087         IN      OUT                     iou_node_t* const                       p_iou,\r
2088         IN              const   ib_dm_mad_t* const                      p_mad )\r
2089 {\r
2090         AL_ENTER( AL_DBG_PNP );\r
2091         /* Copy the IOU info for post-processing. */\r
2092         p_iou->info = *((ib_iou_info_t*)p_mad->data);\r
2093         AL_EXIT( AL_DBG_PNP );\r
2094 }\r
2095 \r
2096 \r
2097 static void\r
2098 __ioc_profile_resp(\r
2099         IN      OUT                     iou_node_t* const                       p_iou,\r
2100         IN              const   ib_dm_mad_t* const                      p_mad )\r
2101 {\r
2102         iou_ioc_t               *p_ioc;\r
2103         cl_map_item_t   *p_item;\r
2104 \r
2105         AL_ENTER( AL_DBG_PNP );\r
2106         p_ioc = __get_ioc( gp_ioc_pnp, cl_ntoh32(p_mad->hdr.attr_mod),\r
2107                 (ib_ioc_profile_t*)p_mad->data );\r
2108         if( p_ioc )\r
2109         {\r
2110                 /* Need back link to process service entry failures. */\r
2111                 p_ioc->p_iou = p_iou;\r
2112                 cl_spinlock_acquire( &p_iou->lock );\r
2113                 p_item = cl_qmap_insert( &p_iou->ioc_map,\r
2114                         p_ioc->profile.ioc_guid, &p_ioc->map_item );\r
2115                 cl_spinlock_release( &p_iou->lock );\r
2116                 /* Return the IOC if it's a duplicate. */\r
2117                 if( p_item != &p_ioc->map_item )\r
2118                         __put_ioc( gp_ioc_pnp, p_ioc );\r
2119         }\r
2120         AL_EXIT( AL_DBG_PNP );\r
2121 }\r
2122 \r
2123 \r
2124 static void\r
2125 __svc_entry_resp(\r
2126         IN      OUT                     iou_ioc_t* const                        p_ioc,\r
2127         IN              const   ib_dm_mad_t* const                      p_mad )\r
2128 {\r
2129         uint16_t                        idx;\r
2130         uint8_t                         lo, hi;\r
2131         ib_svc_entries_t        *p_svc_entries;\r
2132 \r
2133         AL_ENTER( AL_DBG_PNP );\r
2134 \r
2135         ib_dm_get_slot_lo_hi( p_mad->hdr.attr_mod, NULL, &lo, &hi );\r
2136         CL_ASSERT( (hi - lo) < SVC_ENTRY_COUNT );\r
2137         p_svc_entries = (ib_svc_entries_t*)p_mad->data;\r
2138 \r
2139         /* Copy the entries. */\r
2140         for( idx = lo; idx <= hi; idx++ )\r
2141                 p_ioc->p_svc_entries[idx] = p_svc_entries->service_entry[idx - lo];\r
2142 \r
2143         /* Update the number of entries received so far. */\r
2144         p_ioc->num_valid_entries += (hi - lo) + 1;\r
2145 \r
2146         AL_EXIT( AL_DBG_PNP );\r
2147 }\r
2148 \r
2149 \r
2150 static void\r
2151 __ioc_pnp_send_cb(\r
2152         IN              const   ib_mad_svc_handle_t                     h_mad_svc,\r
2153         IN                              void                                            *mad_svc_context,\r
2154         IN                              ib_mad_element_t                        *p_request_mad )\r
2155 {\r
2156         ib_api_status_t         status;\r
2157         ioc_sweep_results_t     *p_results;\r
2158         iou_node_t                      *p_iou;\r
2159         iou_ioc_t                       *p_ioc;\r
2160         cl_fmap_item_t          *p_item;\r
2161 \r
2162         AL_ENTER( AL_DBG_PNP );\r
2163 \r
2164         UNUSED_PARAM( h_mad_svc );\r
2165         UNUSED_PARAM( mad_svc_context );\r
2166 \r
2167         CL_ASSERT( p_request_mad->p_next == NULL );\r
2168 \r
2169         p_results = (ioc_sweep_results_t* __ptr64)p_request_mad->context1;\r
2170 \r
2171         if( p_request_mad->status != IB_WCS_SUCCESS )\r
2172         {\r
2173                 switch( p_request_mad->p_mad_buf->attr_id )\r
2174                 {\r
2175                 case IB_MAD_ATTR_IO_UNIT_INFO:\r
2176                         p_iou = (iou_node_t* __ptr64)p_request_mad->context2;\r
2177                         if( p_request_mad->status == IB_WCS_TIMEOUT_RETRY_ERR )\r
2178                         {\r
2179                                 /* Move to the next path for the node and try the query again. */\r
2180                                 p_item = cl_fmap_next( &p_iou->p_config_path->map_item );\r
2181                                 if( p_item != cl_fmap_end( &p_iou->path_map ) )\r
2182                                 {\r
2183                                         p_iou->p_config_path =\r
2184                                                 PARENT_STRUCT( p_item, iou_path_t, map_item );\r
2185                                         __format_dm_get( p_results, p_iou, p_iou->p_config_path,\r
2186                                                 IB_MAD_ATTR_IO_UNIT_INFO, 0, p_request_mad );\r
2187 \r
2188                                         status = ib_send_mad( p_results->p_svc->h_mad_svc,\r
2189                                                 p_request_mad, &p_request_mad );\r
2190                                         if( status == IB_SUCCESS )\r
2191                                         {\r
2192                                                 AL_EXIT( AL_DBG_PNP );\r
2193                                                 return;\r
2194                                         }\r
2195                                 }\r
2196                         }\r
2197                         break;\r
2198 \r
2199                 case IB_MAD_ATTR_SERVICE_ENTRIES:\r
2200                         p_ioc = (iou_ioc_t* __ptr64)p_request_mad->context2;\r
2201                         cl_spinlock_acquire( &p_ioc->p_iou->lock );\r
2202                         cl_qmap_remove_item( &p_ioc->p_iou->ioc_map, &p_ioc->map_item );\r
2203                         cl_spinlock_release( &p_ioc->p_iou->lock );\r
2204                         __put_ioc( gp_ioc_pnp, p_ioc );\r
2205                         break;\r
2206 \r
2207                 default:\r
2208                         break;\r
2209                 }\r
2210         }\r
2211 \r
2212         /* Cleanup. */\r
2213         ib_put_mad( p_request_mad );\r
2214 \r
2215         /*\r
2216          * If this is the last MAD, finish processing the IOU queries\r
2217          * in the PnP thread.\r
2218          */\r
2219         if( !cl_atomic_dec( &p_results->p_svc->query_cnt ) )\r
2220                 cl_async_proc_queue( gp_async_pnp_mgr, &p_results->async_item );\r
2221 \r
2222         AL_EXIT( AL_DBG_PNP );\r
2223 }\r
2224 \r
2225 \r
2226 static void\r
2227 __flush_duds(\r
2228         IN      OUT                     ioc_sweep_results_t                     *p_results )\r
2229 {\r
2230         cl_fmap_item_t                  *p_item;\r
2231         cl_map_item_t                   *p_ioc_item;\r
2232         iou_node_t                              *p_iou;\r
2233         iou_ioc_t                               *p_ioc;\r
2234 \r
2235         AL_ENTER( AL_DBG_PNP );\r
2236 \r
2237         /* Walk the map of IOUs and discard any that didn't respond to IOU info. */\r
2238         p_item = cl_fmap_head( &p_results->iou_map );\r
2239         /*\r
2240          * No locking required since we're protected by the serialization of the\r
2241          * PnP thread.\r
2242          */\r
2243         while( p_item != cl_fmap_end( &p_results->iou_map ) )\r
2244         {\r
2245                 p_iou = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
2246 \r
2247                 p_item = cl_fmap_next( p_item );\r
2248                 switch( p_results->state )\r
2249                 {\r
2250                 case SWEEP_IOU_INFO:\r
2251                         if( p_iou->info.max_controllers )\r
2252                                 continue;\r
2253                         break;\r
2254 \r
2255                 case SWEEP_SVC_ENTRIES:\r
2256                         CL_ASSERT( cl_qmap_count( &p_iou->ioc_map ) );\r
2257                         p_ioc_item = cl_qmap_head( &p_iou->ioc_map );\r
2258                         while( p_ioc_item != cl_qmap_end( &p_iou->ioc_map ) )\r
2259                         {\r
2260                                 p_ioc = PARENT_STRUCT( p_ioc_item, iou_ioc_t, map_item );\r
2261                                 p_ioc_item = cl_qmap_next( p_ioc_item );\r
2262 \r
2263                                 if( !p_ioc->num_valid_entries ||\r
2264                                         p_ioc->num_valid_entries != p_ioc->profile.num_svc_entries )\r
2265                                 {\r
2266                                         cl_qmap_remove_item( &p_iou->ioc_map, &p_ioc->map_item );\r
2267                                         __put_ioc( gp_ioc_pnp, p_ioc );\r
2268                                 }\r
2269                         }\r
2270                         /* Fall through. */\r
2271                 case SWEEP_IOC_PROFILE:\r
2272                         if( cl_qmap_count( &p_iou->ioc_map ) )\r
2273                                 continue;\r
2274                         break;\r
2275 \r
2276                 default:\r
2277                         CL_ASSERT( p_results->state != SWEEP_COMPLETE );\r
2278                         break;\r
2279                 }\r
2280 \r
2281                 cl_fmap_remove_item( &p_results->iou_map, &p_iou->map_item );\r
2282                 __put_iou( gp_ioc_pnp, p_iou );\r
2283         }\r
2284 \r
2285         AL_EXIT( AL_DBG_PNP );\r
2286 }\r
2287 \r
2288 static void\r
2289 __process_sweep(\r
2290         IN                              cl_async_proc_item_t            *p_async_item )\r
2291 {\r
2292         ib_api_status_t                 status;\r
2293         ioc_sweep_results_t             *p_results;\r
2294 \r
2295         AL_ENTER( AL_DBG_PNP );\r
2296 \r
2297         p_results = PARENT_STRUCT( p_async_item, ioc_sweep_results_t, async_item );\r
2298         CL_ASSERT( !p_results->p_svc->query_cnt );\r
2299 \r
2300         if( p_results->p_svc->obj.state == CL_DESTROYING )\r
2301         {\r
2302                 __put_iou_map( gp_ioc_pnp, &p_results->iou_map );\r
2303                 goto err;\r
2304         }\r
2305 \r
2306         /* Walk the map of IOUs and discard any that didn't respond to IOU info. */\r
2307         __flush_duds( p_results );\r
2308         switch( p_results->state )\r
2309         {\r
2310         case SWEEP_IOU_INFO:\r
2311                 /* Next step, query IOC profiles for all IOUs. */\r
2312                 p_results->state = SWEEP_IOC_PROFILE;\r
2313                 status = __query_ioc_profiles( p_results );\r
2314                 break;\r
2315 \r
2316         case SWEEP_IOC_PROFILE:\r
2317                 /* Next step: query service entries for all IOCs. */\r
2318                 p_results->state = SWEEP_SVC_ENTRIES;\r
2319                 status = __query_svc_entries( p_results );\r
2320                 break;\r
2321 \r
2322         case SWEEP_SVC_ENTRIES:\r
2323                 /* Filter results and report changes. */\r
2324                 p_results->state = SWEEP_COMPLETE;\r
2325                 __update_results( p_results );\r
2326                 status = IB_SUCCESS;\r
2327                 break;\r
2328 \r
2329         default:\r
2330                 CL_ASSERT( p_results->state == SWEEP_IOU_INFO ||\r
2331                         p_results->state == SWEEP_IOC_PROFILE ||\r
2332                         p_results->state == SWEEP_SVC_ENTRIES );\r
2333                 status = IB_ERROR;\r
2334         }\r
2335 \r
2336         if( p_results->state == SWEEP_COMPLETE || status != IB_SUCCESS )\r
2337         {\r
2338 err:\r
2339                 if( !cl_atomic_dec( &gp_ioc_pnp->query_cnt ) )\r
2340                         cl_async_proc_queue( gp_async_pnp_mgr, &gp_ioc_pnp->async_item );\r
2341                 cl_free( p_results );\r
2342         }\r
2343 \r
2344         AL_EXIT( AL_DBG_PNP );\r
2345 }\r
2346 \r
2347 \r
2348 static ib_api_status_t\r
2349 __query_ioc_profiles(\r
2350         IN                              ioc_sweep_results_t* const      p_results )\r
2351 {\r
2352         ib_api_status_t         status;\r
2353         cl_fmap_item_t          *p_item;\r
2354         iou_node_t                      *p_iou;\r
2355         uint8_t                         slot;\r
2356         ib_mad_element_t        *p_mad, *p_mad_list = NULL;\r
2357 \r
2358         AL_ENTER( AL_DBG_PNP );\r
2359 \r
2360         p_item = cl_fmap_head( &p_results->iou_map );\r
2361         while( p_item != cl_fmap_end( &p_results->iou_map ) )\r
2362         {\r
2363                 p_iou = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
2364                 CL_ASSERT( p_iou->info.max_controllers );\r
2365                 CL_ASSERT( cl_fmap_count( &p_iou->path_map ) );\r
2366                 CL_ASSERT( p_iou->p_config_path );\r
2367                 p_item = cl_fmap_next( p_item );\r
2368 \r
2369                 p_mad = NULL;\r
2370                 for( slot = 1; slot <= p_iou->info.max_controllers; slot++ )\r
2371                 {\r
2372                         if( ioc_at_slot( &p_iou->info, slot ) == IOC_INSTALLED )\r
2373                         {\r
2374                                 status = ib_get_mad( p_results->p_svc->pool_key,\r
2375                                         MAD_BLOCK_SIZE, &p_mad );\r
2376                                 if( status != IB_SUCCESS )\r
2377                                         break;\r
2378 \r
2379                                 __format_dm_get( p_results, p_iou, p_iou->p_config_path,\r
2380                                         IB_MAD_ATTR_IOC_PROFILE, cl_hton32( slot ), p_mad );\r
2381 \r
2382                                 /* Chain the MAD up. */\r
2383                                 p_mad->p_next = p_mad_list;\r
2384                                 p_mad_list = p_mad;\r
2385 \r
2386                                 cl_atomic_inc( &p_results->p_svc->query_cnt );\r
2387                         }\r
2388                 }\r
2389                 if( !p_mad )\r
2390                 {\r
2391                         /* No IOCs installed in this IOU, or failed to get MAD. */\r
2392                         cl_fmap_remove_item( &p_results->iou_map, &p_iou->map_item );\r
2393                         __put_iou( gp_ioc_pnp, p_iou );\r
2394                 }\r
2395         }\r
2396 \r
2397         /* Trap the case where there are no queries to send. */\r
2398         if( !p_mad_list )\r
2399         {\r
2400                 AL_EXIT( AL_DBG_PNP );\r
2401                 return IB_NOT_DONE;\r
2402         }\r
2403 \r
2404         status = ib_send_mad( p_results->p_svc->h_mad_svc, p_mad_list, &p_mad );\r
2405         if( status != IB_SUCCESS )\r
2406         {\r
2407                 /* If some of the MADs were sent wait for their completion. */\r
2408                 if( p_mad_list != p_mad )\r
2409                         status = IB_SUCCESS;\r
2410 \r
2411                 while( p_mad )\r
2412                 {\r
2413                         p_mad_list = p_mad->p_next;\r
2414                         p_mad->p_next = NULL;\r
2415                         ib_put_mad( p_mad );\r
2416                         if( !cl_atomic_dec( &p_results->p_svc->query_cnt ) &&\r
2417                                 status == IB_SUCCESS )\r
2418                         {\r
2419                                 cl_async_proc_queue( gp_async_pnp_mgr,\r
2420                                         &p_results->async_item );\r
2421                         }\r
2422                         p_mad = p_mad_list;\r
2423                 }\r
2424         }\r
2425         AL_EXIT( AL_DBG_PNP );\r
2426         return status;\r
2427 }\r
2428 \r
2429 \r
2430 static ib_api_status_t\r
2431 __query_svc_entries(\r
2432         IN                              ioc_sweep_results_t* const      p_results )\r
2433 {\r
2434         ib_api_status_t         status;\r
2435         cl_fmap_item_t          *p_iou_item;\r
2436         cl_map_item_t           *p_ioc_item;\r
2437         iou_node_t                      *p_iou;\r
2438         iou_ioc_t                       *p_ioc;\r
2439         uint8_t                         i;\r
2440         uint32_t                        attr_mod;\r
2441         ib_mad_element_t        *p_mad, *p_mad_list = NULL;\r
2442 \r
2443         AL_ENTER( AL_DBG_PNP );\r
2444 \r
2445         for( p_iou_item = cl_fmap_head( &p_results->iou_map );\r
2446                 p_iou_item != cl_fmap_end( &p_results->iou_map );\r
2447                 p_iou_item = cl_fmap_next( p_iou_item ) )\r
2448         {\r
2449                 p_iou = PARENT_STRUCT( p_iou_item, iou_node_t, map_item );\r
2450                 CL_ASSERT( cl_qmap_count( &p_iou->ioc_map ) );\r
2451                 CL_ASSERT( cl_fmap_count( &p_iou->path_map ) );\r
2452                 CL_ASSERT( p_iou->p_config_path );\r
2453 \r
2454                 for( p_ioc_item = cl_qmap_head( &p_iou->ioc_map );\r
2455                         p_ioc_item != cl_qmap_end( &p_iou->ioc_map );\r
2456                         p_ioc_item = cl_qmap_next( p_ioc_item ) )\r
2457                 {\r
2458                         p_ioc = PARENT_STRUCT( p_ioc_item, iou_ioc_t, map_item );\r
2459                         CL_ASSERT( p_ioc->p_iou == p_iou );\r
2460 \r
2461                         for( i = 0; i < p_ioc->profile.num_svc_entries; i += 4 )\r
2462                         {\r
2463                                 status = ib_get_mad( p_results->p_svc->pool_key,\r
2464                                         MAD_BLOCK_SIZE, &p_mad );\r
2465                                 if( status != IB_SUCCESS )\r
2466                                         break;\r
2467 \r
2468                                 attr_mod = (((uint32_t)p_ioc->slot) << 16) | i;\r
2469                                 if( (i + 3) > p_ioc->profile.num_svc_entries )\r
2470                                         attr_mod |= ((p_ioc->profile.num_svc_entries - 1) << 8);\r
2471                                 else\r
2472                                         attr_mod |= ((i + 3) << 8);\r
2473 \r
2474                                 __format_dm_get( p_results, p_ioc, p_iou->p_config_path,\r
2475                                         IB_MAD_ATTR_SERVICE_ENTRIES, cl_hton32( attr_mod ),\r
2476                                         p_mad );\r
2477 \r
2478                                 /* Chain the MAD up. */\r
2479                                 p_mad->p_next = p_mad_list;\r
2480                                 p_mad_list = p_mad;\r
2481 \r
2482                                 cl_atomic_inc( &p_results->p_svc->query_cnt );\r
2483                         }\r
2484                 }\r
2485         }\r
2486 \r
2487         /* Trap the case where there are no queries to send. */\r
2488         if( !p_mad_list )\r
2489         {\r
2490                 AL_EXIT( AL_DBG_PNP );\r
2491                 return IB_NOT_DONE;\r
2492         }\r
2493 \r
2494         status = ib_send_mad( p_results->p_svc->h_mad_svc, p_mad_list, &p_mad );\r
2495         if( status != IB_SUCCESS )\r
2496         {\r
2497                 /* If some of the MADs were sent wait for their completion. */\r
2498                 if( p_mad_list != p_mad )\r
2499                         status = IB_SUCCESS;\r
2500 \r
2501                 while( p_mad )\r
2502                 {\r
2503                         p_mad_list = p_mad->p_next;\r
2504                         p_mad->p_next = NULL;\r
2505                         ib_put_mad( p_mad );\r
2506                         if( !cl_atomic_dec( &p_results->p_svc->query_cnt ) &&\r
2507                                 status == IB_SUCCESS )\r
2508                         {\r
2509                                 cl_async_proc_queue( gp_async_pnp_mgr,\r
2510                                         &p_results->async_item );\r
2511                         }\r
2512                         p_mad = p_mad_list;\r
2513                 }\r
2514         }\r
2515         AL_EXIT( AL_DBG_PNP );\r
2516         return status;\r
2517 }\r
2518 \r
2519 \r
2520 static void\r
2521 __update_results(\r
2522         IN                              ioc_sweep_results_t* const      p_results )\r
2523 {\r
2524         cl_fmap_t                       iou_map1, iou_map2;\r
2525         cl_fmap_item_t          *p_item1, *p_item2;\r
2526         iou_node_t                      *p_iou1, *p_iou2;\r
2527 \r
2528         AL_ENTER( AL_DBG_PNP );\r
2529 \r
2530         cl_fmap_init( &iou_map1, __iou_cmp );\r
2531         cl_fmap_init( &iou_map2, __iou_cmp );\r
2532 \r
2533         /*\r
2534          * No need to lock on the sweep map since all accesses are serialized\r
2535          * by the PnP thread.\r
2536          */\r
2537         cl_fmap_delta( &gp_ioc_pnp->sweep_map, &p_results->iou_map,\r
2538                 &iou_map1, &iou_map2 );\r
2539         /* sweep_map and iou_map now contain exactly the same items. */\r
2540         p_item1 = cl_fmap_head( &gp_ioc_pnp->sweep_map );\r
2541         p_item2 = cl_fmap_head( &p_results->iou_map );\r
2542         while( p_item1 != cl_fmap_end( &gp_ioc_pnp->sweep_map ) )\r
2543         {\r
2544                 CL_ASSERT( p_item2 != cl_fmap_end( &p_results->iou_map ) );\r
2545                 p_iou1 = PARENT_STRUCT( p_item1, iou_node_t, map_item );\r
2546                 p_iou2 = PARENT_STRUCT( p_item2, iou_node_t, map_item );\r
2547                 CL_ASSERT( p_iou1->guid == p_iou2->guid );\r
2548 \r
2549                 /*\r
2550                  * Merge the IOC maps - this leaves all duplicates in\r
2551                  * p_iou2->ioc_map.\r
2552                  */\r
2553                 cl_qmap_merge( &p_iou1->ioc_map, &p_iou2->ioc_map );\r
2554 \r
2555                 /*\r
2556                  * Merge the path maps - this leaves all duplicates in\r
2557                  * p_iou2->path_map\r
2558                  */\r
2559                 cl_fmap_merge( &p_iou1->path_map, &p_iou2->path_map );\r
2560 \r
2561                 /* Return the duplicate IOU (and whatever duplicate paths and IOCs) */\r
2562                 cl_fmap_remove_item( &p_results->iou_map, p_item2 );\r
2563                 __put_iou( gp_ioc_pnp, p_iou2 );\r
2564 \r
2565                 p_item1 = cl_fmap_next( p_item1 );\r
2566                 p_item2 = cl_fmap_head( &p_results->iou_map );\r
2567         }\r
2568         CL_ASSERT( !cl_fmap_count( &p_results->iou_map ) );\r
2569 \r
2570         /* Merge in the unique items. */\r
2571         cl_fmap_merge( &gp_ioc_pnp->sweep_map, &iou_map1 );\r
2572         CL_ASSERT( !cl_fmap_count( &iou_map1 ) );\r
2573         cl_fmap_merge( &gp_ioc_pnp->sweep_map, &iou_map2 );\r
2574         CL_ASSERT( !cl_fmap_count( &iou_map2 ) );\r
2575 \r
2576         AL_EXIT( AL_DBG_PNP );\r
2577         return;\r
2578 }\r
2579 \r
2580 \r
2581 static void\r
2582 __ioc_async_cb(\r
2583         IN                              cl_async_proc_item_t            *p_item )\r
2584 {\r
2585         cl_status_t             status;\r
2586         cl_fmap_t               old_ious, new_ious;\r
2587 \r
2588         AL_ENTER( AL_DBG_PNP );\r
2589 \r
2590         CL_ASSERT( p_item == &gp_ioc_pnp->async_item );\r
2591         UNUSED_PARAM( p_item );\r
2592 \r
2593         CL_ASSERT( !gp_ioc_pnp->query_cnt );\r
2594 \r
2595         cl_fmap_init( &old_ious, __iou_cmp );\r
2596         cl_fmap_init( &new_ious, __iou_cmp );\r
2597         cl_fmap_delta(\r
2598                 &gp_ioc_pnp->iou_map, &gp_ioc_pnp->sweep_map, &new_ious, &old_ious );\r
2599 \r
2600         /* For each duplicate IOU, report changes in IOCs or paths. */\r
2601         __change_ious( &gp_ioc_pnp->iou_map, &gp_ioc_pnp->sweep_map );\r
2602 \r
2603         /* Report all new IOUs. */\r
2604         __add_ious( &gp_ioc_pnp->iou_map, &new_ious, NULL );\r
2605         CL_ASSERT( !cl_fmap_count( &new_ious ) );\r
2606 \r
2607         /* Report all removed IOUs. */\r
2608         __remove_ious( &old_ious );\r
2609         CL_ASSERT( !cl_fmap_count( &old_ious ) );\r
2610 \r
2611         /* Reset the sweep timer. */\r
2612         if( g_ioc_poll_interval )\r
2613         {\r
2614                 status = cl_timer_start(\r
2615                         &gp_ioc_pnp->sweep_timer, g_ioc_poll_interval );\r
2616                 CL_ASSERT( status == CL_SUCCESS );\r
2617         }\r
2618 \r
2619         /* Release the reference we took in the timer callback. */\r
2620         deref_al_obj( &gp_ioc_pnp->obj );\r
2621 \r
2622         AL_EXIT( AL_DBG_PNP );\r
2623 }\r
2624 \r
2625 \r
2626 static void\r
2627 __change_ious(\r
2628         IN                              cl_fmap_t* const                        p_cur_ious,\r
2629         IN                              cl_fmap_t* const                        p_dup_ious )\r
2630 {\r
2631         cl_fmap_t               new_paths, old_paths;\r
2632         cl_qmap_t               new_iocs, old_iocs;\r
2633         cl_fmap_item_t  *p_item1, *p_item2;\r
2634         iou_node_t              *p_iou1, *p_iou2;\r
2635 \r
2636         AL_ENTER( AL_DBG_PNP );\r
2637 \r
2638         cl_fmap_init( &new_paths, __path_cmp );\r
2639         cl_fmap_init( &old_paths, __path_cmp );\r
2640         cl_qmap_init( &new_iocs );\r
2641         cl_qmap_init( &old_iocs );\r
2642 \r
2643         p_item1 = cl_fmap_head( p_cur_ious );\r
2644         p_item2 = cl_fmap_head( p_dup_ious );\r
2645         while( p_item1 != cl_fmap_end( p_cur_ious ) )\r
2646         {\r
2647                 p_iou1 = PARENT_STRUCT( p_item1, iou_node_t, map_item );\r
2648                 p_iou2 = PARENT_STRUCT( p_item2, iou_node_t, map_item );\r
2649                 CL_ASSERT( p_iou1->guid == p_iou2->guid );\r
2650 \r
2651                 /* Figure out what changed. */\r
2652                 cl_fmap_delta(\r
2653                         &p_iou1->path_map, &p_iou2->path_map, &new_paths, &old_paths );\r
2654                 cl_qmap_delta(\r
2655                         &p_iou1->ioc_map, &p_iou2->ioc_map, &new_iocs, &old_iocs );\r
2656 \r
2657                 /*\r
2658                  * Report path changes before IOC changes so that new IOCs\r
2659                  * report up-to-date paths.  Report new paths before removing\r
2660                  * old ones to minimize the chance of disruption of service - \r
2661                  * i.e. the last path being removed before an alternate is available.\r
2662                  */\r
2663                 __add_paths( p_iou1, &p_iou1->ioc_map, &new_paths, NULL );\r
2664                 CL_ASSERT( !cl_fmap_count( &new_paths ) );\r
2665 \r
2666                 __remove_paths( &p_iou1->ioc_map, &old_paths );\r
2667                 CL_ASSERT( !cl_fmap_count( &old_paths ) );\r
2668 \r
2669                 /* Report IOCs. */\r
2670                 __add_iocs( p_iou1, &new_iocs, NULL );\r
2671                 CL_ASSERT( !cl_qmap_count( &new_iocs ) );\r
2672 \r
2673                 __remove_iocs( p_iou1, &old_iocs );\r
2674                 CL_ASSERT( !cl_qmap_count( &old_iocs ) );\r
2675 \r
2676                 /* Done with the duplicate IOU.  Return it to the pool */\r
2677                 cl_fmap_remove_item( p_dup_ious, p_item2 );\r
2678                 __put_iou( gp_ioc_pnp, p_iou2 );\r
2679 \r
2680                 p_item1 = cl_fmap_next( p_item1 );\r
2681                 p_item2 = cl_fmap_head( p_dup_ious );\r
2682         }\r
2683         CL_ASSERT( !cl_fmap_count( p_dup_ious ) );\r
2684 \r
2685         AL_EXIT( AL_DBG_PNP );\r
2686 }\r
2687 \r
2688 \r
2689 static void\r
2690 __add_ious(\r
2691         IN                              cl_fmap_t* const                        p_cur_ious,\r
2692         IN                              cl_fmap_t* const                        p_new_ious,\r
2693         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
2694 {\r
2695         cl_fmap_item_t  *p_item;\r
2696         iou_node_t              *p_iou;\r
2697 \r
2698         AL_ENTER( AL_DBG_PNP );\r
2699 \r
2700         p_item = cl_fmap_head( p_new_ious );\r
2701         while( p_item != cl_fmap_end( p_new_ious ) )\r
2702         {\r
2703                 p_iou = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
2704 \r
2705                 /* Report the IOU addition. */\r
2706                 __report_iou_add( p_iou, p_reg );\r
2707 \r
2708                 p_item = cl_fmap_next( p_item );\r
2709         }\r
2710 \r
2711         if( p_cur_ious != p_new_ious )\r
2712         {\r
2713                 cl_fmap_merge( p_cur_ious, p_new_ious );\r
2714                 CL_ASSERT( !cl_fmap_count( p_new_ious ) );\r
2715         }\r
2716 \r
2717         AL_EXIT( AL_DBG_PNP );\r
2718 }\r
2719 \r
2720 \r
2721 static void\r
2722 __remove_ious(\r
2723         IN                              cl_fmap_t* const                        p_old_ious )\r
2724 {\r
2725         cl_fmap_item_t  *p_item;\r
2726         iou_node_t              *p_iou;\r
2727 \r
2728         AL_ENTER( AL_DBG_PNP );\r
2729 \r
2730         p_item = cl_fmap_head( p_old_ious );\r
2731         while( p_item != cl_fmap_end( p_old_ious ) )\r
2732         {\r
2733                 p_iou = PARENT_STRUCT( p_item, iou_node_t, map_item );\r
2734 \r
2735                 /* Report the IOU removal. */\r
2736                 __report_iou_remove( p_iou );\r
2737 \r
2738                 cl_fmap_remove_item( p_old_ious, p_item );\r
2739                 __put_iou( gp_ioc_pnp, p_iou );\r
2740                 p_item = cl_fmap_head( p_old_ious );\r
2741         }\r
2742         CL_ASSERT( !cl_fmap_count( p_old_ious ) );\r
2743 \r
2744         AL_EXIT( AL_DBG_PNP );\r
2745 }\r
2746 \r
2747 \r
2748 static void\r
2749 __add_iocs(\r
2750         IN                              iou_node_t* const                       p_iou,\r
2751         IN                              cl_qmap_t* const                        p_new_iocs,\r
2752         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
2753 {\r
2754         cl_map_item_t   *p_item;\r
2755         iou_ioc_t               *p_ioc;\r
2756 \r
2757         AL_ENTER( AL_DBG_PNP );\r
2758 \r
2759         p_item = cl_qmap_head( p_new_iocs );\r
2760         while( p_item != cl_qmap_end( p_new_iocs ) )\r
2761         {\r
2762                 p_ioc = PARENT_STRUCT( p_item, iou_ioc_t, map_item );\r
2763 \r
2764                 /* Report the IOU addition. */\r
2765                 __report_ioc_add( p_iou, p_ioc, p_reg );\r
2766 \r
2767                 p_item = cl_qmap_next( p_item );\r
2768         }\r
2769 \r
2770         if( p_new_iocs != &p_iou->ioc_map )\r
2771         {\r
2772                 cl_qmap_merge( &p_iou->ioc_map, p_new_iocs );\r
2773                 CL_ASSERT( !cl_qmap_count( p_new_iocs ) );\r
2774         }\r
2775         AL_EXIT( AL_DBG_PNP );\r
2776 }\r
2777 \r
2778 \r
2779 static void\r
2780 __remove_iocs(\r
2781         IN                              iou_node_t* const                       p_iou,\r
2782         IN                              cl_qmap_t* const                        p_old_iocs )\r
2783 {\r
2784         cl_map_item_t   *p_item;\r
2785         iou_ioc_t               *p_ioc;\r
2786 \r
2787         AL_ENTER( AL_DBG_PNP );\r
2788 \r
2789         p_item = cl_qmap_tail( p_old_iocs );\r
2790         while( p_item != cl_qmap_end( p_old_iocs ) )\r
2791         {\r
2792                 p_ioc = PARENT_STRUCT( p_item, iou_ioc_t, map_item );\r
2793 \r
2794                 /* Report the IOC removal. */\r
2795                 __report_ioc_remove( p_iou, p_ioc );\r
2796 \r
2797                 cl_qmap_remove_item( p_old_iocs, p_item );\r
2798                 __put_ioc( gp_ioc_pnp, p_ioc );\r
2799                 p_item = cl_qmap_tail( p_old_iocs );\r
2800         }\r
2801         CL_ASSERT( !cl_qmap_count( p_old_iocs ) );\r
2802 \r
2803         AL_EXIT( AL_DBG_PNP );\r
2804 }\r
2805 \r
2806 \r
2807 static void\r
2808 __add_paths(\r
2809         IN                              iou_node_t* const                       p_iou,\r
2810         IN                              cl_qmap_t* const                        p_ioc_map,\r
2811         IN                              cl_fmap_t* const                        p_new_paths,\r
2812         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
2813 {\r
2814         cl_map_item_t   *p_ioc_item;\r
2815         cl_fmap_item_t  *p_item;\r
2816         iou_ioc_t               *p_ioc;\r
2817         iou_path_t              *p_path;\r
2818 \r
2819         AL_ENTER( AL_DBG_PNP );\r
2820 \r
2821         p_item = cl_fmap_head( p_new_paths );\r
2822         while( p_item != cl_fmap_end( p_new_paths ) )\r
2823         {\r
2824                 p_path = PARENT_STRUCT( p_item, iou_path_t, map_item );\r
2825 \r
2826                 /* Report the path to all IOCs. */\r
2827                 for( p_ioc_item = cl_qmap_head( p_ioc_map );\r
2828                         p_ioc_item != cl_qmap_end( p_ioc_map );\r
2829                         p_ioc_item = cl_qmap_next( p_ioc_item ) )\r
2830                 {\r
2831                         p_ioc = PARENT_STRUCT( p_ioc_item, iou_ioc_t, map_item );\r
2832                         __report_path( p_ioc, p_path, IB_PNP_IOC_PATH_ADD, p_reg );\r
2833                 }\r
2834 \r
2835                 p_item = cl_fmap_next( p_item );\r
2836         }\r
2837 \r
2838         ASSERT( &p_iou->path_map != p_new_paths );\r
2839 \r
2840         cl_fmap_merge( &p_iou->path_map, p_new_paths );\r
2841         CL_ASSERT( !cl_fmap_count( p_new_paths ) );\r
2842 \r
2843         AL_EXIT( AL_DBG_PNP );\r
2844 }\r
2845 \r
2846 \r
2847 static void\r
2848 __add_ioc_paths(\r
2849         IN                              iou_ioc_t* const                        p_ioc,\r
2850         IN                              cl_fmap_t* const                        p_new_paths,\r
2851         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
2852 {\r
2853         cl_fmap_item_t  *p_item;\r
2854         iou_path_t              *p_path;\r
2855 \r
2856         AL_ENTER( AL_DBG_PNP );\r
2857 \r
2858         p_item = cl_fmap_head( p_new_paths );\r
2859         while( p_item != cl_fmap_end( p_new_paths ) )\r
2860         {\r
2861                 p_path = PARENT_STRUCT( p_item, iou_path_t, map_item );\r
2862 \r
2863                 __report_path( p_ioc, p_path, IB_PNP_IOC_PATH_ADD, p_reg );\r
2864 \r
2865                 p_item = cl_fmap_next( p_item );\r
2866         }\r
2867 \r
2868         AL_EXIT( AL_DBG_PNP );\r
2869 }\r
2870 \r
2871 \r
2872 static void\r
2873 __remove_paths(\r
2874         IN                              cl_qmap_t* const                        p_ioc_map,\r
2875         IN                              cl_fmap_t* const                        p_old_paths )\r
2876 {\r
2877         cl_map_item_t   *p_ioc_item;\r
2878         cl_fmap_item_t  *p_item;\r
2879         iou_ioc_t               *p_ioc;\r
2880         iou_path_t              *p_path;\r
2881 \r
2882         AL_ENTER( AL_DBG_PNP );\r
2883 \r
2884         p_item = cl_fmap_tail( p_old_paths );\r
2885         while( p_item != cl_fmap_end( p_old_paths ) )\r
2886         {\r
2887                 p_path = PARENT_STRUCT( p_item, iou_path_t, map_item );\r
2888 \r
2889                 for( p_ioc_item = cl_qmap_tail( p_ioc_map );\r
2890                         p_ioc_item != cl_qmap_end( p_ioc_map );\r
2891                         p_ioc_item = cl_qmap_prev( p_ioc_item ) )\r
2892                 {\r
2893                         p_ioc = PARENT_STRUCT( p_ioc_item, iou_ioc_t, map_item );\r
2894                         __report_path( p_ioc, p_path, IB_PNP_IOC_PATH_REMOVE, NULL );\r
2895                 }\r
2896 \r
2897                 cl_fmap_remove_item( p_old_paths, p_item );\r
2898                 __put_path( gp_ioc_pnp, p_path );\r
2899                 p_item = cl_fmap_tail( p_old_paths );\r
2900         }\r
2901         CL_ASSERT( !cl_fmap_count( p_old_paths ) );\r
2902 \r
2903         AL_EXIT( AL_DBG_PNP );\r
2904 }\r
2905 \r
2906 \r
2907 static cl_status_t\r
2908 __notify_users(\r
2909         IN              const   cl_list_item_t* const           p_item,\r
2910         IN                              al_pnp_ioc_event_t* const       p_event )\r
2911 {\r
2912         ib_api_status_t                 status;\r
2913         al_pnp_t                                *p_reg;\r
2914         al_pnp_context_t                *p_context;\r
2915 \r
2916         AL_ENTER( AL_DBG_PNP );\r
2917 \r
2918         p_reg = PARENT_STRUCT( p_item, al_pnp_t, list_item );\r
2919 \r
2920         /* Copy the source record into the user's record. */\r
2921         cl_memcpy( p_event->p_user_rec, p_event->p_rec, p_event->rec_size );\r
2922         p_event->p_user_rec->h_pnp = p_reg;\r
2923         p_event->p_user_rec->pnp_context = (void*)p_reg->obj.context;\r
2924 \r
2925         switch( p_event->p_rec->pnp_event )\r
2926         {\r
2927         case IB_PNP_IOU_ADD:\r
2928                 CL_ASSERT( pnp_get_class( p_reg->pnp_class ) == IB_PNP_IOU );\r
2929                 p_context = pnp_create_context( p_reg, p_event->p_rec->guid );\r
2930                 break;\r
2931 \r
2932         case IB_PNP_IOU_REMOVE:\r
2933                 CL_ASSERT( pnp_get_class( p_reg->pnp_class ) == IB_PNP_IOU );\r
2934                 /* Lookup the context for this IOU. */\r
2935                 p_context = pnp_get_context( p_reg, p_event->p_rec->guid );\r
2936                 break;\r
2937 \r
2938         case IB_PNP_IOC_ADD:\r
2939                 CL_ASSERT( pnp_get_class( p_reg->pnp_class ) == IB_PNP_IOC );\r
2940                 p_context = pnp_create_context( p_reg, p_event->p_rec->guid );\r
2941                 break;\r
2942 \r
2943         default:\r
2944                 CL_ASSERT( pnp_get_class( p_reg->pnp_class ) == IB_PNP_IOC );\r
2945                 p_context = pnp_get_context( p_reg, p_event->p_rec->guid );\r
2946                 break;\r
2947         }\r
2948         if( !p_context )\r
2949                 return CL_NOT_FOUND;\r
2950 \r
2951         p_event->p_user_rec->context = (void*)p_context->context;\r
2952 \r
2953         /* Notify user. */\r
2954         status = p_reg->pfn_pnp_cb( p_event->p_user_rec );\r
2955 \r
2956         /* Update contexts */\r
2957         if( status != IB_SUCCESS ||\r
2958                 p_event->p_rec->pnp_event == IB_PNP_IOU_REMOVE ||\r
2959                 p_event->p_rec->pnp_event == IB_PNP_IOC_REMOVE )\r
2960         {\r
2961                 cl_qmap_remove_item( &p_reg->context_map, &p_context->map_item );\r
2962                 cl_free( p_context );\r
2963         }\r
2964         else\r
2965         {\r
2966                 p_context->context = p_event->p_user_rec->context;\r
2967         }\r
2968 \r
2969         AL_EXIT( AL_DBG_PNP );\r
2970         return CL_NOT_FOUND;\r
2971 }\r
2972 \r
2973 \r
2974 static void\r
2975 __report_iou_add(\r
2976         IN                              iou_node_t* const                       p_iou,\r
2977         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
2978 {\r
2979         al_pnp_ioc_event_t              event;\r
2980         ib_pnp_iou_rec_t                *p_rec, *p_user_rec;\r
2981 \r
2982         AL_ENTER( AL_DBG_PNP );\r
2983 \r
2984         event.rec_size = sizeof(ib_pnp_iou_rec_t);\r
2985         event.rec_size = ROUNDUP( event.rec_size, sizeof(void*) );\r
2986 \r
2987         p_rec = cl_zalloc( event.rec_size * 2 );\r
2988         if( !p_rec )\r
2989         {\r
2990                 AL_PRINT_EXIT( TRACE_LEVEL_ERROR, AL_DBG_ERROR,\r
2991                         ("Failed to allocate user record.\n") );\r
2992                 return;\r
2993         }\r
2994         p_rec->pnp_rec.pnp_event = IB_PNP_IOU_ADD;\r
2995         p_rec->pnp_rec.guid = p_iou->guid;\r
2996         p_rec->ca_guid = p_iou->ca_guid;\r
2997         p_rec->vend_id = p_iou->vend_id;\r
2998         p_rec->dev_id = p_iou->dev_id;\r
2999         p_rec->revision = p_iou->revision;\r
3000         cl_memcpy( p_rec->desc, p_iou->desc, sizeof(p_rec->desc) );\r
3001         p_user_rec = (ib_pnp_iou_rec_t*)(((uint8_t*)p_rec) + event.rec_size);\r
3002         \r
3003         event.p_rec = (ib_pnp_rec_t*)p_rec;\r
3004         event.p_user_rec = (ib_pnp_rec_t*)p_user_rec;\r
3005 \r
3006         if( p_reg )\r
3007         {\r
3008                 if( pnp_get_class( p_reg->pnp_class ) == IB_PNP_IOU )\r
3009                         __notify_users( &p_reg->list_item, &event );\r
3010                 else\r
3011                         __add_iocs( p_iou, &p_iou->ioc_map, p_reg );\r
3012         }\r
3013         else\r
3014         {\r
3015                 /* Report the IOU to all clients registered for IOU events. */\r
3016                 cl_qlist_find_from_head( &gp_ioc_pnp->iou_reg_list,\r
3017                         __notify_users, &event );\r
3018 \r
3019                 /* Report IOCs - this will in turn report the paths. */\r
3020                 __add_iocs( p_iou, &p_iou->ioc_map, NULL );\r
3021         }\r
3022 \r
3023         cl_free( p_rec );\r
3024         AL_EXIT( AL_DBG_PNP );\r
3025 }\r
3026 \r
3027 \r
3028 static void\r
3029 __report_iou_remove(\r
3030         IN                              iou_node_t* const                       p_iou )\r
3031 {\r
3032         al_pnp_ioc_event_t              event;\r
3033         ib_pnp_iou_rec_t                rec, user_rec;\r
3034 \r
3035         AL_ENTER( AL_DBG_PNP );\r
3036 \r
3037         /* Report IOCs - this will in turn report the paths. */\r
3038         __remove_iocs( p_iou, &p_iou->ioc_map );\r
3039 \r
3040         cl_memclr( &rec, sizeof(ib_pnp_iou_rec_t) );\r
3041         rec.pnp_rec.pnp_event = IB_PNP_IOU_REMOVE;\r
3042         rec.pnp_rec.guid = p_iou->guid;\r
3043 \r
3044         event.rec_size = sizeof(ib_pnp_iou_rec_t);\r
3045         event.p_rec = (ib_pnp_rec_t*)&rec;\r
3046         event.p_user_rec = (ib_pnp_rec_t*)&user_rec;\r
3047 \r
3048         /*\r
3049          * Report the IOU to all clients registered for IOU events in\r
3050          * reverse order than ADD notifications.\r
3051          */\r
3052         cl_qlist_find_from_tail( &gp_ioc_pnp->iou_reg_list,\r
3053                 __notify_users, &event );\r
3054 \r
3055         AL_EXIT( AL_DBG_PNP );\r
3056 }\r
3057 \r
3058 \r
3059 static void\r
3060 __report_ioc_add(\r
3061         IN                              iou_node_t* const                       p_iou,\r
3062         IN                              iou_ioc_t* const                        p_ioc,\r
3063         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
3064 {\r
3065         al_pnp_ioc_event_t              event;\r
3066         ib_pnp_ioc_rec_t                *p_rec;\r
3067 \r
3068         AL_ENTER( AL_DBG_PNP );\r
3069 \r
3070         event.rec_size = sizeof(ib_pnp_ioc_rec_t) +\r
3071                 (sizeof(ib_svc_entry_t) * (p_ioc->profile.num_svc_entries - 1));\r
3072         event.rec_size = ROUNDUP( event.rec_size, sizeof(void*) );\r
3073 \r
3074         /*\r
3075          * The layout of the pnp record is as follows:\r
3076          *      ib_pnp_rec_t\r
3077          *      ib_svc_entry_t\r
3078          *      ib_ioc_info_t\r
3079          *\r
3080          * This is needed to keep the service entries contiguous to the first\r
3081          * entry in the pnp record.\r
3082          */\r
3083         p_rec = (ib_pnp_ioc_rec_t*)cl_zalloc( event.rec_size * 2 );\r
3084         if( !p_rec )\r
3085                 return;\r
3086 \r
3087         p_rec->pnp_rec.pnp_event = IB_PNP_IOC_ADD;\r
3088         p_rec->pnp_rec.guid = p_ioc->profile.ioc_guid;\r
3089         p_rec->ca_guid = p_ioc->p_iou->ca_guid;\r
3090         cl_memcpy( p_rec->svc_entry_array, p_ioc->p_svc_entries,\r
3091                 p_ioc->profile.num_svc_entries * sizeof(ib_svc_entry_t) );\r
3092         p_rec->info.chassis_guid = p_iou->chassis_guid;\r
3093         p_rec->info.chassis_slot = p_iou->slot;\r
3094         p_rec->info.iou_guid = p_iou->guid;\r
3095         p_rec->info.iou_slot = p_ioc->slot;\r
3096         p_rec->info.profile = p_ioc->profile;\r
3097 \r
3098         event.p_rec = (ib_pnp_rec_t*)p_rec;\r
3099         event.p_user_rec = (ib_pnp_rec_t*)(((uint8_t*)p_rec) + event.rec_size);\r
3100 \r
3101         if( p_reg )\r
3102         {\r
3103                 __notify_users( &p_reg->list_item, &event );\r
3104         }\r
3105         else\r
3106         {\r
3107                 /* Report the IOC to all clients registered for IOC events. */\r
3108                 cl_qlist_find_from_head( &gp_ioc_pnp->ioc_reg_list,\r
3109                         __notify_users, &event );\r
3110         }\r
3111         cl_free( p_rec );\r
3112 \r
3113         /* Report the paths for this IOC only. */\r
3114         __add_ioc_paths( p_ioc, &p_iou->path_map, p_reg );\r
3115 \r
3116         AL_EXIT( AL_DBG_PNP );\r
3117 }\r
3118 \r
3119 \r
3120 static void\r
3121 __report_ioc_remove(\r
3122         IN                              iou_node_t* const                       p_iou,\r
3123         IN                              iou_ioc_t* const                        p_ioc )\r
3124 {\r
3125         al_pnp_ioc_event_t              event;\r
3126         ib_pnp_ioc_rec_t                rec, user_rec;\r
3127 \r
3128         AL_ENTER( AL_DBG_PNP );\r
3129 \r
3130         UNUSED_PARAM( p_iou );\r
3131 \r
3132         cl_memclr( &rec, sizeof(ib_pnp_ioc_rec_t) );\r
3133         rec.pnp_rec.pnp_event = IB_PNP_IOC_REMOVE;\r
3134         rec.pnp_rec.guid = p_ioc->profile.ioc_guid;\r
3135         \r
3136         event.rec_size = sizeof(ib_pnp_ioc_rec_t);\r
3137         event.p_rec = (ib_pnp_rec_t*)&rec;\r
3138         event.p_user_rec = (ib_pnp_rec_t*)&user_rec;\r
3139 \r
3140         /*\r
3141          * Report the IOC removal to all clients registered for IOC events in\r
3142          * reverse order than ADD notifications.\r
3143          */\r
3144         cl_qlist_find_from_tail( &gp_ioc_pnp->ioc_reg_list,\r
3145                 __notify_users, &event );\r
3146 \r
3147         AL_EXIT( AL_DBG_PNP );\r
3148 }\r
3149 \r
3150 \r
3151 static void\r
3152 __report_path(\r
3153         IN                              iou_ioc_t* const                        p_ioc,\r
3154         IN                              iou_path_t* const                       p_path,\r
3155         IN                              ib_pnp_event_t                          pnp_event,\r
3156         IN                              al_pnp_t* const                         p_reg OPTIONAL )\r
3157 {\r
3158         al_pnp_ioc_event_t              event;\r
3159         ib_pnp_ioc_path_rec_t   *p_rec;\r
3160 \r
3161         AL_ENTER( AL_DBG_PNP );\r
3162 \r
3163         CL_ASSERT( pnp_event == IB_PNP_IOC_PATH_ADD ||\r