[IPoIB NDIS 6.0 CM]
[mirror/winof/.git] / ulp / ipoib_NDIS6_CM / kernel / ipoib_port.cpp
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.\r
4  * Portions Copyright (c) 2008 Microsoft Corporation.  All rights reserved.\r
5  *\r
6  * This software is available to you under the OpenIB.org BSD license\r
7  * below:\r
8  *\r
9  *     Redistribution and use in source and binary forms, with or\r
10  *     without modification, are permitted provided that the following\r
11  *     conditions are met:\r
12  *\r
13  *      - Redistributions of source code must retain the above\r
14  *        copyright notice, this list of conditions and the following\r
15  *        disclaimer.\r
16  *\r
17  *      - Redistributions in binary form must reproduce the above\r
18  *        copyright notice, this list of conditions and the following\r
19  *        disclaimer in the documentation and/or other materials\r
20  *        provided with the distribution.\r
21  *\r
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
29  * SOFTWARE.\r
30  *\r
31  * $Id: ipoib_port.c 4506 2009-06-23 14:40:54Z xalex $\r
32  */\r
33 \r
34 \r
35 \r
36 #include "ipoib_endpoint.h"\r
37 #include "ipoib_port.h"\r
38 #include "ipoib_adapter.h"\r
39 #include "ipoib_debug.h"\r
40 #if defined(EVENT_TRACING)\r
41 #ifdef offsetof\r
42 #undef offsetof\r
43 #endif\r
44 #include "ipoib_port.tmh"\r
45 #endif\r
46 #include <offload.h>\r
47 \r
48 #include "wdm.h"\r
49 #include <ntddk.h>\r
50 \r
51 \r
52 \r
53 ib_gid_t        bcast_mgid_template = {\r
54         0xff,                                                           /* multicast field */\r
55         0x12,                                                           /* scope (to be filled in) */\r
56         0x40, 0x1b,                                                     /* IPv4 signature */\r
57         0xff, 0xff,                                                     /* 16 bits of P_Key (to be filled in) */\r
58         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     /* 48 bits of zeros */\r
59         0xff, 0xff, 0xff, 0xff,                         /* 32 bit IPv4 broadcast address */\r
60 };\r
61 \r
62 \r
63 #ifdef _DEBUG_\r
64 /* Handy pointer for debug use. */\r
65 ipoib_port_t    *gp_ipoib_port;\r
66 #endif\r
67 \r
68 static void __port_mcast_garbage_dpc(KDPC *p_gc_dpc,void *context,void *s_arg1, void *s_arg2);\r
69 static void __port_do_mcast_garbage(ipoib_port_t* const p_port );\r
70 \r
71 \r
72 static void __recv_cb_dpc(KDPC *p_gc_dpc,void *context,void *s_arg1, void *s_arg2);\r
73 \r
74 \r
75 /******************************************************************************\r
76 *\r
77 * Declarations\r
78 *\r
79 ******************************************************************************/\r
80 static void\r
81 __port_construct(\r
82         IN                              ipoib_port_t* const                     p_port );\r
83 \r
84 static ib_api_status_t\r
85 __port_init(\r
86         IN                              ipoib_port_t* const                     p_port,\r
87         IN                              ipoib_adapter_t* const          p_adapter,\r
88         IN                              ib_pnp_port_rec_t* const        p_pnp_rec );\r
89 \r
90 static void\r
91 __port_destroying(\r
92         IN                              cl_obj_t* const                         p_obj );\r
93 \r
94 static void\r
95 __port_cleanup(\r
96         IN                              cl_obj_t* const                         p_obj );\r
97 \r
98 static void\r
99 __port_free(\r
100         IN                              cl_obj_t* const                         p_obj );\r
101 \r
102 static ib_api_status_t\r
103 __port_query_ca_attrs( \r
104         IN              ipoib_port_t* const                                     p_port,\r
105         IN              ib_ca_attr_t**                                          pp_ca_attrs );\r
106 \r
107 static void\r
108 __srq_async_event_cb(\r
109 IN                      ib_async_event_rec_t            *p_event_rec );\r
110 \r
111 /******************************************************************************\r
112 *\r
113 * IB resource manager operations\r
114 *\r
115 ******************************************************************************/\r
116 static void\r
117 __ib_mgr_construct(\r
118         IN                              ipoib_port_t* const                     p_port );\r
119 \r
120 static ib_api_status_t\r
121 __ib_mgr_init(\r
122         IN                              ipoib_port_t* const                     p_port );\r
123 \r
124 static void\r
125 __ib_mgr_destroy(\r
126         IN                              ipoib_port_t* const                     p_port );\r
127 \r
128 static void\r
129 __qp_event(\r
130         IN                              ib_async_event_rec_t            *p_event_rec );\r
131 \r
132 static void\r
133 __cq_event(\r
134         IN                              ib_async_event_rec_t            *p_event_rec );\r
135 \r
136 static ib_api_status_t\r
137 __ib_mgr_activate(\r
138         IN                              ipoib_port_t* const                     p_port );\r
139 \r
140 /******************************************************************************\r
141 *\r
142 * Buffer manager operations.\r
143 *\r
144 ******************************************************************************/\r
145 static void\r
146 __buf_mgr_construct(\r
147         IN                              ipoib_port_t* const                     p_port );\r
148 \r
149 static ib_api_status_t\r
150 __buf_mgr_init(\r
151         IN                              ipoib_port_t* const                     p_port );\r
152 \r
153 static void\r
154 __buf_mgr_destroy(\r
155         IN                              ipoib_port_t* const                     p_port );\r
156 \r
157 static cl_status_t\r
158 __recv_ctor(\r
159         IN                              void* const                                     p_object,\r
160         IN                              void*                                           context,\r
161                 OUT                     cl_pool_item_t** const          pp_pool_item );\r
162 \r
163 #if !IPOIB_INLINE_RECV\r
164 static void\r
165 __recv_dtor(\r
166         IN              const   cl_pool_item_t* const           p_pool_item,\r
167         IN                              void                                            *context );\r
168 #endif  /* IPOIB_INLINE_RECV */\r
169 \r
170 static inline ipoib_send_desc_t*\r
171 __buf_mgr_get_send(\r
172         IN                              ipoib_port_t* const                     p_port );\r
173 \r
174 static inline void\r
175 __buf_mgr_put_send(\r
176         IN                              ipoib_port_t* const                     p_port,\r
177         IN                              ipoib_send_desc_t* const        p_desc );\r
178 \r
179 static inline ipoib_recv_desc_t*\r
180 __buf_mgr_get_recv(\r
181         IN                              ipoib_port_t* const                     p_port );\r
182 \r
183 static inline void\r
184 __buf_mgr_put_recv(\r
185         IN                              ipoib_port_t* const                     p_port,\r
186         IN                              ipoib_recv_desc_t* const        p_desc,\r
187         IN                              NET_BUFFER_LIST* const          p_net_buffer_list OPTIONAL );\r
188 \r
189 static inline void\r
190 __buf_mgr_put_recv_list(\r
191         IN                              ipoib_port_t* const                     p_port,\r
192         IN                              cl_qlist_t* const                       p_list );\r
193 \r
194 //NDIS60\r
195 static inline NET_BUFFER_LIST*\r
196 __buf_mgr_get_ndis_pkt(\r
197         IN                              ipoib_port_t* const                     p_port,\r
198         IN                              ipoib_recv_desc_t* const        p_desc );\r
199 \r
200 \r
201 /******************************************************************************\r
202 *\r
203 * Receive manager operations.\r
204 *\r
205 ******************************************************************************/\r
206 static void\r
207 __recv_mgr_construct(\r
208         IN                              ipoib_port_t* const                     p_port );\r
209 \r
210 static ib_api_status_t\r
211 __recv_mgr_init(\r
212         IN                              ipoib_port_t* const                     p_port );\r
213 \r
214 static void\r
215 __recv_mgr_destroy(\r
216         IN                              ipoib_port_t* const                     p_port );\r
217 \r
218 /* Posts receive buffers to the receive queue. */\r
219 int32_t\r
220 __recv_mgr_repost(\r
221         IN                              ipoib_port_t* const                     p_port );\r
222 \r
223 static void\r
224 __recv_cb(\r
225         IN              const   ib_cq_handle_t                          h_cq,\r
226         IN                              void                                            *cq_context );\r
227 \r
228 static void\r
229 __recv_get_endpts(\r
230         IN                              ipoib_port_t* const                     p_port,\r
231         IN                              ipoib_recv_desc_t* const        p_desc,\r
232         IN                              ib_wc_t* const                          p_wc,\r
233                 OUT                     ipoib_endpt_t** const           pp_src,\r
234                 OUT                     ipoib_endpt_t** const           pp_dst );\r
235 \r
236 static int32_t\r
237 __recv_mgr_filter(\r
238         IN                              ipoib_port_t* const                     p_port,\r
239         IN                              ib_wc_t* const                          p_done_wc_list,\r
240                 OUT                     cl_qlist_t* const                       p_done_list,\r
241                 OUT                     cl_qlist_t* const                       p_bad_list );\r
242 \r
243 static ib_api_status_t\r
244 __recv_gen(\r
245         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
246                 OUT                     eth_pkt_t* const                        p_eth,\r
247         IN                              ipoib_endpt_t* const            p_src,\r
248         IN                              ipoib_endpt_t* const            p_dst );\r
249 \r
250 static ib_api_status_t\r
251 __recv_dhcp(\r
252         IN                              ipoib_port_t* const                     p_port,\r
253         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
254                 OUT                     eth_pkt_t* const                        p_eth,\r
255         IN                              ipoib_endpt_t* const            p_src,\r
256         IN                              ipoib_endpt_t* const            p_dst );\r
257 \r
258 static ib_api_status_t\r
259 __recv_arp(\r
260         IN                              ipoib_port_t* const                     p_port,\r
261         IN                              ib_wc_t* const                          p_wc,\r
262         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
263                 OUT                     eth_pkt_t* const                        p_eth,\r
264         IN                              ipoib_endpt_t** const           p_src,\r
265         IN                              ipoib_endpt_t* const            p_dst );\r
266 \r
267 static ib_api_status_t\r
268 __recv_mgr_prepare_pkt(\r
269         IN                              ipoib_port_t* const                     p_port,\r
270         IN                              ipoib_recv_desc_t* const        p_desc,\r
271                 OUT                     NET_BUFFER_LIST** const         pp_net_buffer_list );\r
272 \r
273 static uint32_t\r
274 __recv_mgr_build_pkt_array(\r
275         IN                              ipoib_port_t* const                     p_port,\r
276         IN                              int32_t                                         shortage,\r
277                 OUT                     cl_qlist_t* const                       p_done_list,\r
278                 OUT                     int32_t* const                          p_discarded );\r
279 \r
280 /******************************************************************************\r
281 *\r
282 * Send manager operations.\r
283 *\r
284 ******************************************************************************/\r
285 static void\r
286 __send_mgr_construct(\r
287         IN                              ipoib_port_t* const                     p_port );\r
288 \r
289 static void\r
290 __send_mgr_destroy(\r
291         IN                              ipoib_port_t* const                     p_port );\r
292 \r
293 static NDIS_STATUS\r
294 __send_gen(\r
295         IN                              ipoib_port_t* const                     p_port,\r
296         IN                              ipoib_send_desc_t* const        p_desc,\r
297         IN                              SCATTER_GATHER_LIST                     *p_sgl,\r
298         IN                              INT                                             lso_data_index);\r
299 \r
300 static NDIS_STATUS\r
301 __send_mgr_filter_ip(\r
302         IN                              ipoib_port_t* const                     p_port,\r
303         IN              const   eth_hdr_t* const                        p_eth_hdr,\r
304         IN                              MDL*                                            p_mdl,\r
305         IN                              size_t                                          buf_len,\r
306         IN                              SCATTER_GATHER_LIST                     *p_sgl,\r
307         IN      OUT                     ipoib_send_desc_t* const        p_desc );\r
308 \r
309 static NDIS_STATUS\r
310 __send_mgr_filter_igmp_v2(\r
311         IN                              ipoib_port_t* const                     p_port,\r
312     IN          const   ip_hdr_t* const                         p_ip_hdr,\r
313         IN                              size_t                                          iph_options_size,\r
314         IN                              MDL*                                            p_mdl,\r
315         IN                              size_t                                          buf_len );\r
316 \r
317 static NDIS_STATUS\r
318 __send_mgr_filter_udp(\r
319         IN                              ipoib_port_t* const                     p_port,\r
320         IN              const   ip_hdr_t* const                         p_ip_hdr,\r
321         IN                              MDL*                                            p_mdl,\r
322         IN                              size_t                                          buf_len,\r
323         IN                              SCATTER_GATHER_LIST                     *p_sgl,\r
324         IN      OUT                     ipoib_send_desc_t* const        p_desc );\r
325 \r
326 static NDIS_STATUS\r
327 __send_mgr_filter_dhcp(\r
328         IN                              ipoib_port_t* const                     p_port,\r
329         IN              const   udp_hdr_t* const                        p_udp_hdr,\r
330         IN                              MDL*                                            p_mdl,\r
331         IN                              size_t                                          buf_len,\r
332         IN      OUT                     ipoib_send_desc_t* const        p_desc );\r
333 \r
334 static NDIS_STATUS\r
335 __send_mgr_filter_arp(\r
336         IN                              ipoib_port_t* const                     p_port,\r
337         IN              const   eth_hdr_t* const                        p_eth_hdr,\r
338         IN                              MDL*                                            p_mdl,\r
339         IN                              size_t                                          buf_len,\r
340         IN      OUT                     ipoib_send_desc_t* const        p_desc );\r
341 \r
342 static void\r
343 __process_failed_send(\r
344         IN                              ipoib_port_t* const                     p_port,\r
345         IN                              ipoib_send_desc_t* const        p_desc,\r
346         IN              const   NDIS_STATUS                                     status,\r
347         IN              ULONG                                           send_complete_flags );\r
348 \r
349 static inline NDIS_STATUS\r
350 __send_mgr_queue(\r
351         IN                              ipoib_port_t* const                     p_port,\r
352         IN                              eth_hdr_t* const                        p_eth_hdr,\r
353                 OUT                     ipoib_endpt_t** const           pp_endpt );\r
354 \r
355 static NDIS_STATUS\r
356 __build_send_desc(\r
357         IN                              ipoib_port_t* const                     p_port,\r
358         IN                              eth_hdr_t* const                        p_eth_hdr,\r
359         IN                              MDL* const                                      p_mdl,\r
360         IN              const   size_t                                          mdl_len,\r
361         IN                              SCATTER_GATHER_LIST                     *p_sgl,\r
362         IN      OUT                     ipoib_send_desc_t* const        p_desc );\r
363 \r
364 \r
365 static void\r
366 __send_cb(\r
367         IN              const   ib_cq_handle_t                          h_cq,\r
368         IN                              void                                            *cq_context );\r
369 \r
370 static NDIS_STATUS \r
371 GetLsoHeaderSize(\r
372         IN              PNET_BUFFER             pNetBuffer,\r
373         IN              LsoData                 *pLsoData,\r
374         OUT     UINT                    *IndexOfData,\r
375         IN              ipoib_hdr_t *ipoib_hdr );\r
376 \r
377 \r
378 static NDIS_STATUS\r
379 __build_lso_desc(\r
380         IN                              ipoib_port_t* const                     p_port,\r
381         IN      OUT                     ipoib_send_desc_t* const        p_desc,\r
382         IN                              ULONG                                           mss,\r
383         IN                              SCATTER_GATHER_LIST                     *p_sgl,\r
384         IN                              int32_t                                         hdr_idx,\r
385         IN PNDIS_TCP_LARGE_SEND_OFFLOAD_NET_BUFFER_LIST_INFO p_lso_info );\r
386 \r
387 static NDIS_STATUS\r
388 __send_fragments(\r
389         IN              ipoib_port_t* const                                     p_port,\r
390         IN              ipoib_send_desc_t* const                        p_desc,\r
391         IN              eth_hdr_t* const                                        p_eth_hdr,\r
392         IN              ip_hdr_t* const                                         p_ip_hdr,\r
393         IN              uint32_t                                                        buf_len,\r
394         IN              NDIS_BUFFER*                                            p_ndis_buf );\r
395 \r
396 //TODO CM Restore\r
397 #if 0\r
398 static void\r
399 __update_fragment_ip_hdr(\r
400 IN              ip_hdr_t* const         p_ip_hdr,\r
401 IN              uint16_t                        fragment_size, \r
402 IN              uint16_t                        fragment_offset, \r
403 IN              BOOLEAN                         more_fragments );\r
404 \r
405 static void\r
406 __copy_ip_options(\r
407 IN              uint8_t*                        p_buf,\r
408 IN              uint8_t*                        p_options,\r
409 IN              uint32_t                        options_len,\r
410 IN              BOOLEAN                         copy_all );\r
411 #endif\r
412 /******************************************************************************\r
413 *\r
414 * Endpoint manager operations\r
415 *\r
416 ******************************************************************************/\r
417 static void\r
418 __endpt_mgr_construct(\r
419         IN                              ipoib_port_t* const                     p_port );\r
420 \r
421 static ib_api_status_t\r
422 __endpt_mgr_init(\r
423         IN                              ipoib_port_t* const                     p_port );\r
424 \r
425 static void\r
426 __endpt_mgr_destroy(\r
427         IN                              ipoib_port_t* const                     p_port );\r
428 \r
429 /****f* IPoIB/__endpt_mgr_remove_all\r
430 * NAME\r
431 *       __endpt_mgr_remove_all\r
432 *\r
433 * DESCRIPTION\r
434 *       Removes all enpoints from the port, dereferencing them to initiate\r
435 *       destruction.\r
436 *\r
437 * SYNOPSIS\r
438 */\r
439 static void\r
440 __endpt_mgr_remove_all(\r
441         IN                              ipoib_port_t* const                     p_port );\r
442 /*\r
443 ********/\r
444 \r
445 static void\r
446 __endpt_mgr_remove(\r
447         IN                              ipoib_port_t* const                     p_port,\r
448         IN                              ipoib_endpt_t* const            p_endpt );\r
449 \r
450 static void\r
451 __endpt_mgr_reset_all(\r
452         IN                              ipoib_port_t* const                     p_port );\r
453 \r
454 static inline NDIS_STATUS\r
455 __endpt_mgr_ref(\r
456         IN                              ipoib_port_t* const                     p_port,\r
457         IN              const   mac_addr_t                                      mac,\r
458                 OUT                     ipoib_endpt_t** const           pp_endpt );\r
459 \r
460 static inline NDIS_STATUS\r
461 __endpt_mgr_get_gid_qpn(\r
462         IN                              ipoib_port_t* const                     p_port,\r
463         IN              const   mac_addr_t                                      mac,\r
464                 OUT                     ib_gid_t* const                         p_gid,\r
465                 OUT                     UNALIGNED net32_t* const        p_qpn );\r
466 \r
467 static inline ipoib_endpt_t*\r
468 __endpt_mgr_get_by_gid(\r
469         IN                              ipoib_port_t* const                     p_port,\r
470         IN              const   ib_gid_t* const                         p_gid );\r
471 \r
472 static inline ipoib_endpt_t*\r
473 __endpt_mgr_get_by_lid(\r
474         IN                              ipoib_port_t* const                     p_port,\r
475         IN              const   net16_t                                         lid );\r
476 \r
477 static inline ib_api_status_t\r
478 __endpt_mgr_insert_locked(\r
479         IN                              ipoib_port_t* const                     p_port,\r
480         IN              const   mac_addr_t                                      mac,\r
481         IN                              ipoib_endpt_t* const            p_endpt );\r
482 \r
483 static inline ib_api_status_t\r
484 __endpt_mgr_insert(\r
485         IN                              ipoib_port_t* const                     p_port,\r
486         IN              const   mac_addr_t                                      mac,\r
487         IN                              ipoib_endpt_t* const            p_endpt );\r
488 \r
489 static ib_api_status_t\r
490 __endpt_mgr_add_local(\r
491         IN                              ipoib_port_t* const                     p_port,\r
492         IN                              ib_port_info_t* const           p_port_info );\r
493 \r
494 static ib_api_status_t\r
495 __endpt_mgr_add_bcast(\r
496         IN                              ipoib_port_t* const                     p_port,\r
497         IN                              ib_mcast_rec_t                          *p_mcast_rec );\r
498 \r
499 /******************************************************************************\r
500 *\r
501 * MCast operations.\r
502 *\r
503 ******************************************************************************/\r
504 static ib_api_status_t\r
505 __port_get_bcast(\r
506         IN                              ipoib_port_t* const                     p_port );\r
507 \r
508 static ib_api_status_t\r
509 __port_join_bcast(\r
510         IN                              ipoib_port_t* const                     p_port,\r
511         IN                              ib_member_rec_t* const          p_member_rec );\r
512 \r
513 static ib_api_status_t\r
514 __port_create_bcast(\r
515         IN                              ipoib_port_t* const                     p_port );\r
516 \r
517 \r
518 \r
519 static void\r
520 __bcast_get_cb(\r
521         IN                              ib_query_rec_t                          *p_query_rec );\r
522 \r
523 \r
524 static void\r
525 __bcast_cb(\r
526         IN                              ib_mcast_rec_t                          *p_mcast_rec );\r
527 \r
528 \r
529 static void\r
530 __mcast_cb(\r
531         IN                              ib_mcast_rec_t                          *p_mcast_rec );\r
532 \r
533 void\r
534 __leave_error_mcast_cb(\r
535         IN                              void                            *context );\r
536 \r
537 \r
538 static intn_t\r
539 __gid_cmp(\r
540         IN              const   void* const                                     p_key1,\r
541         IN              const   void* const                                     p_key2 )\r
542 {\r
543         return cl_memcmp( p_key1, p_key2, sizeof(ib_gid_t) );\r
544 }\r
545 \r
546 \r
547 inline void ipoib_port_ref( ipoib_port_t * p_port, int type )\r
548 {\r
549         cl_obj_ref( &p_port->obj );\r
550 #if DBG\r
551         cl_atomic_inc( &p_port->ref[type % ref_mask] );\r
552         if ((p_port->obj.ref_cnt % 20)==0)\r
553         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_OBJ,\r
554                 ("ref type %d ref_cnt %d\n", type, p_port->obj.ref_cnt) );\r
555         //TODO remove\r
556         //ASSERT (p_port->obj.ref_cnt < 100);\r
557 #else\r
558         UNREFERENCED_PARAMETER(type);\r
559 #endif\r
560 }\r
561 \r
562 \r
563 inline void ipoib_port_deref(ipoib_port_t * p_port, int type)\r
564 {\r
565 #if DBG\r
566         cl_atomic_dec( &p_port->ref[type % ref_mask] );\r
567         if ((p_port->obj.ref_cnt % 20) == 0)\r
568         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_OBJ,\r
569                 ("deref type %d ref_cnt %d\n", type, p_port->obj.ref_cnt) );\r
570 #else\r
571         UNREFERENCED_PARAMETER(type);\r
572 #endif\r
573         cl_obj_deref( &p_port->obj );\r
574 \r
575 }\r
576 \r
577 /* function returns pointer to payload that is going after IP header.\r
578 *  asssuming that payload and IP header are in the same buffer\r
579 */\r
580 static void* GetIpPayloadPtr(const      ip_hdr_t* const p_ip_hdr)\r
581 {\r
582         return (void*)((uint8_t*)p_ip_hdr + IP_HEADER_LENGTH(p_ip_hdr));\r
583 }\r
584 \r
585 /******************************************************************************\r
586 *\r
587 * Implementation\r
588 *\r
589 ******************************************************************************/\r
590 ib_api_status_t\r
591 ipoib_create_port(\r
592         IN                              ipoib_adapter_t* const          p_adapter,\r
593         IN                              ib_pnp_port_rec_t* const        p_pnp_rec,\r
594                 OUT                     ipoib_port_t** const            pp_port )\r
595 {\r
596         ib_api_status_t         status;\r
597         ipoib_port_t            *p_port;\r
598 \r
599         IPOIB_ENTER( IPOIB_DBG_INIT );\r
600 \r
601         CL_ASSERT( !p_adapter->p_port );\r
602 \r
603         p_port = (ipoib_port_t *) cl_zalloc( sizeof(ipoib_port_t) +\r
604                 (sizeof(ipoib_hdr_t) * (p_adapter->params.sq_depth - 1)) );\r
605         if( !p_port )\r
606         {\r
607                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
608                         ("Failed to allocate ipoib_port_t (%d bytes)\n",\r
609                         sizeof(ipoib_port_t)) );\r
610                 return IB_INSUFFICIENT_MEMORY;\r
611         }\r
612 \r
613 #ifdef _DEBUG_\r
614         gp_ipoib_port = p_port;\r
615 #endif\r
616 \r
617         __port_construct( p_port );\r
618 \r
619         status = __port_init( p_port, p_adapter, p_pnp_rec );\r
620         if( status != IB_SUCCESS )\r
621         {\r
622                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
623                         ("ipoib_port_init returned %s.\n", \r
624                         p_adapter->p_ifc->get_err_str( status )) );\r
625                 __port_cleanup( &p_port->obj );\r
626                 __port_free( &p_port->obj );\r
627                 return status;\r
628         }\r
629 \r
630         *pp_port = p_port;\r
631         IPOIB_EXIT( IPOIB_DBG_INIT );\r
632         return IB_SUCCESS;\r
633 }\r
634 \r
635 \r
636 void\r
637 ipoib_port_destroy(\r
638         IN                              ipoib_port_t* const                     p_port )\r
639 {\r
640         IPOIB_ENTER( IPOIB_DBG_INIT );\r
641 \r
642         CL_ASSERT( p_port );\r
643         CL_ASSERT( p_port->p_adapter );\r
644         CL_ASSERT( !p_port->p_adapter->p_port );\r
645 \r
646         cl_obj_destroy( &p_port->obj );\r
647 \r
648         IPOIB_EXIT( IPOIB_DBG_INIT );\r
649 }\r
650 \r
651 \r
652 static void\r
653 __port_construct(\r
654         IN                              ipoib_port_t* const                     p_port )\r
655 {\r
656         IPOIB_ENTER( IPOIB_DBG_INIT );\r
657 \r
658         p_port->state = IB_QPS_RESET;\r
659 \r
660         cl_obj_construct( &p_port->obj, IPOIB_OBJ_PORT );\r
661         cl_spinlock_construct( &p_port->send_lock );\r
662         cl_spinlock_construct( &p_port->recv_lock );\r
663         __ib_mgr_construct( p_port );\r
664         __buf_mgr_construct( p_port );\r
665 \r
666         __recv_mgr_construct( p_port );\r
667         __send_mgr_construct( p_port );\r
668 \r
669         __endpt_mgr_construct( p_port );\r
670 \r
671         KeInitializeEvent( &p_port->sa_event, NotificationEvent, TRUE );\r
672         KeInitializeEvent( &p_port->leave_mcast_event, NotificationEvent, TRUE );\r
673         \r
674         IPOIB_EXIT( IPOIB_DBG_INIT );\r
675 }\r
676 \r
677 \r
678 static ib_api_status_t\r
679 __port_init(\r
680         IN                              ipoib_port_t* const                     p_port,\r
681         IN                              ipoib_adapter_t* const          p_adapter,\r
682         IN                              ib_pnp_port_rec_t* const        p_pnp_rec )\r
683 {\r
684         cl_status_t                     cl_status;\r
685         ib_api_status_t         status;\r
686 \r
687         IPOIB_ENTER( IPOIB_DBG_INIT );\r
688 \r
689         p_port->port_num = p_pnp_rec->p_port_attr->port_num;\r
690         p_port->p_adapter = p_adapter;\r
691 \r
692         cl_status = cl_spinlock_init( &p_port->send_lock );\r
693         if( cl_status != CL_SUCCESS )\r
694         {\r
695                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
696                         ("cl_spinlock_init returned %#x\n", cl_status) );\r
697                 return IB_ERROR;\r
698         }\r
699 \r
700         cl_status = cl_spinlock_init( &p_port->recv_lock );\r
701         if( cl_status != CL_SUCCESS )\r
702         {\r
703                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
704                         ("cl_spinlock_init returned %#x\n", cl_status) );\r
705                 return IB_ERROR;\r
706         }\r
707 \r
708         /* Initialize the IB resource manager. */\r
709         status = __ib_mgr_init( p_port );\r
710         if( status != IB_SUCCESS )\r
711         {\r
712                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
713                         ("__ib_mgr_init returned %s\n", \r
714                         p_adapter->p_ifc->get_err_str( status )) );\r
715                 return status;\r
716         }\r
717 \r
718         /* Initialize the buffer manager. */\r
719         status = __buf_mgr_init( p_port );\r
720         if( status != IB_SUCCESS )\r
721         {\r
722                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
723                         ("__buf_mgr_init returned %s\n", \r
724                         p_adapter->p_ifc->get_err_str( status )) );\r
725                 return status;\r
726         }\r
727 \r
728         /* Initialize the receive manager. */\r
729         status = __recv_mgr_init( p_port );\r
730         if( status != IB_SUCCESS )\r
731         {\r
732                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
733                         ("__recv_mgr_init returned %s\n", \r
734                         p_adapter->p_ifc->get_err_str( status )) );\r
735                 return status;\r
736         }\r
737 \r
738         /* Initialize the endpoint manager. */\r
739         status = __endpt_mgr_init( p_port );\r
740         if( status != IB_SUCCESS )\r
741         {\r
742                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
743                         ("__endpt_mgr_init returned %s\n", \r
744                         p_adapter->p_ifc->get_err_str( status )) );\r
745                 return status;\r
746         }\r
747 \r
748          KeInitializeDpc(&p_port->recv_dpc,(PKDEFERRED_ROUTINE)__recv_cb_dpc,p_port);\r
749 \r
750 \r
751          /* Initialize multicast garbage collector timer and DPC object */\r
752          KeInitializeDpc(&p_port->gc_dpc,(PKDEFERRED_ROUTINE)__port_mcast_garbage_dpc,p_port);\r
753          KeInitializeTimerEx(&p_port->gc_timer,SynchronizationTimer);\r
754 \r
755         /* We only ever destroy from the PnP callback thread. */\r
756         cl_status = cl_obj_init( &p_port->obj, CL_DESTROY_SYNC,\r
757                 __port_destroying, __port_cleanup, __port_free );\r
758 \r
759 #if DBG\r
760         cl_atomic_inc( &p_port->ref[ref_init] );\r
761         IPOIB_PRINT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_OBJ,\r
762                 ("ref type %d ref_cnt %d\n", ref_init, p_port->obj.ref_cnt) );\r
763 #endif\r
764 \r
765         if( cl_status != CL_SUCCESS )\r
766         {\r
767                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
768                         ("cl_obj_init returned %#x\n", cl_status) );\r
769                 return IB_ERROR;\r
770         }\r
771 \r
772         cl_status = cl_obj_insert_rel( &p_port->rel, &p_adapter->obj, &p_port->obj );\r
773         if( cl_status != CL_SUCCESS )\r
774         {\r
775                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
776                         ("cl_obj_insert_rel returned %#x\n", cl_status) );\r
777                 cl_obj_destroy( &p_port->obj );\r
778                 return IB_ERROR;\r
779         }\r
780 \r
781 #if DBG\r
782         cl_atomic_inc( &p_port->ref[ref_init] );\r
783         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_OBJ,\r
784                 ("ref type %d ref_cnt %d\n", ref_init, p_port->obj.ref_cnt) );\r
785 #endif\r
786 \r
787         IPOIB_EXIT( IPOIB_DBG_INIT );\r
788         return IB_SUCCESS;\r
789 }\r
790 \r
791 \r
792 static void\r
793 __port_destroying(\r
794         IN                              cl_obj_t* const                         p_obj )\r
795 {\r
796         ipoib_port_t    *p_port;\r
797 \r
798         IPOIB_ENTER( IPOIB_DBG_INIT );\r
799 \r
800         CL_ASSERT( p_obj );\r
801 \r
802         p_port = PARENT_STRUCT( p_obj, ipoib_port_t, obj );\r
803 \r
804         ipoib_port_down( p_port );\r
805 \r
806         __endpt_mgr_remove_all( p_port );\r
807 \r
808 #if 0\r
809         if( p_port->p_adapter->params.cm_enabled )\r
810         {\r
811                 endpt_cm_buf_mgr_destroy(  p_port );\r
812                 ipoib_port_srq_destroy( p_port );\r
813                 p_port->endpt_mgr.thread_is_done = 1;\r
814                 cl_event_signal( &p_port->endpt_mgr.event );\r
815         }\r
816 #endif\r
817         ASSERT(FALSE);\r
818         //TODO NDIS6.0\r
819         ipoib_port_resume( p_port, FALSE );\r
820 \r
821         IPOIB_EXIT( IPOIB_DBG_INIT );\r
822 }\r
823 \r
824 \r
825 static void\r
826 __port_cleanup(\r
827         IN                              cl_obj_t* const                         p_obj )\r
828 {\r
829         ipoib_port_t    *p_port;\r
830 \r
831         IPOIB_ENTER( IPOIB_DBG_INIT );\r
832 \r
833         CL_ASSERT( p_obj );\r
834 \r
835         p_port = PARENT_STRUCT( p_obj, ipoib_port_t, obj );\r
836 \r
837         /* Wait for all sends and receives to get flushed. */\r
838         while( p_port->send_mgr.depth || p_port->recv_mgr.depth )\r
839                 cl_thread_suspend( 0 );\r
840 \r
841         /* Destroy the send and receive managers before closing the CA. */\r
842         __ib_mgr_destroy( p_port );\r
843 \r
844         IPOIB_EXIT( IPOIB_DBG_INIT );\r
845 }\r
846 \r
847 \r
848 static void\r
849 __port_free(\r
850         IN                              cl_obj_t* const                         p_obj )\r
851 {\r
852         ipoib_port_t    *p_port;\r
853 \r
854         IPOIB_ENTER( IPOIB_DBG_INIT );\r
855 \r
856         CL_ASSERT( p_obj );\r
857 \r
858         p_port = PARENT_STRUCT( p_obj, ipoib_port_t, obj );\r
859 \r
860         KeCancelTimer(&p_port->gc_timer);\r
861         KeFlushQueuedDpcs();\r
862         __endpt_mgr_destroy( p_port );\r
863         __recv_mgr_destroy( p_port );\r
864         __send_mgr_destroy( p_port );\r
865         __buf_mgr_destroy( p_port );\r
866 \r
867         cl_spinlock_destroy( &p_port->send_lock );\r
868         cl_spinlock_destroy( &p_port->recv_lock );\r
869 \r
870         cl_obj_deinit( p_obj );\r
871         if( p_port->p_ca_attrs )\r
872         {\r
873                 cl_free ( p_port->p_ca_attrs );\r
874         }\r
875         cl_free( p_port );\r
876 \r
877         IPOIB_EXIT( IPOIB_DBG_INIT );\r
878 }\r
879 \r
880 \r
881 \r
882 /******************************************************************************\r
883 *\r
884 * IB resource manager implementation.\r
885 *\r
886 ******************************************************************************/\r
887 static void\r
888 __ib_mgr_construct(\r
889         IN                              ipoib_port_t* const                     p_port )\r
890 {\r
891         IPOIB_ENTER( IPOIB_DBG_INIT );\r
892 \r
893         cl_memclr( &p_port->ib_mgr, sizeof(ipoib_ib_mgr_t) );\r
894 \r
895         IPOIB_EXIT( IPOIB_DBG_INIT );\r
896 }\r
897 \r
898 \r
899 static ib_api_status_t\r
900 __ib_mgr_init(\r
901         IN                              ipoib_port_t* const                     p_port )\r
902 {\r
903         ib_api_status_t         status;\r
904         ib_cq_create_t          cq_create;\r
905         ib_qp_create_t          qp_create;\r
906         ib_phys_create_t        phys_create;\r
907         ib_phys_range_t         phys_range;\r
908         uint64_t                        vaddr;\r
909         net32_t                         rkey;\r
910         ib_qp_attr_t            qp_attr;\r
911 \r
912         IPOIB_ENTER( IPOIB_DBG_INIT );\r
913 \r
914         /* Open the CA. */\r
915         status = p_port->p_adapter->p_ifc->open_ca(\r
916                 p_port->p_adapter->h_al, p_port->p_adapter->guids.ca_guid,\r
917                 NULL, p_port, &p_port->ib_mgr.h_ca );\r
918         if( status != IB_SUCCESS )\r
919         {\r
920                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
921                         EVENT_IPOIB_OPEN_CA, 1, status );\r
922                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
923                         ("ib_open_ca returned %s\n", \r
924                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
925                 return status;\r
926         }\r
927 \r
928         status = __port_query_ca_attrs( p_port, &p_port->p_ca_attrs );\r
929         if( status != IB_SUCCESS )\r
930         {\r
931                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
932                         ("Query CA attributes failed\n" ) );\r
933                 return status;\r
934         }\r
935 #if 0\r
936         if( p_port->p_adapter->params.cm_enabled )\r
937         {\r
938                 uint32_t payload_mtu = __port_attr_to_mtu_size( \r
939                         p_port->p_ca_attrs->p_port_attr[p_port->port_num - 1].mtu )\r
940                         - sizeof(ipoib_hdr_t);\r
941                 /* adjust ipoib UD payload MTU to actual port MTU size. */\r
942                 p_port->p_adapter->params.payload_mtu = \r
943                                 max( DEFAULT_PAYLOAD_MTU, payload_mtu );\r
944                 p_port->p_adapter->params.xfer_block_size = \r
945                         (sizeof(eth_hdr_t) + p_port->p_adapter->params.payload_mtu);\r
946         }\r
947 #endif\r
948 #if IPOIB_USE_DMA\r
949         /* init DMA only once while running MiniportInitialize */\r
950         if ( !p_port->p_adapter->reset )\r
951         {\r
952                 ULONG max_phys_mapping;\r
953                 if( p_port->p_adapter->params.cm_enabled )\r
954                 {\r
955                         max_phys_mapping = p_port->p_adapter->params.cm_xfer_block_size;\r
956                 }\r
957                 else if( p_port->p_adapter->params.lso )\r
958                 {\r
959                         max_phys_mapping = LARGE_SEND_OFFLOAD_SIZE;\r
960                 }\r
961                 else\r
962                 {\r
963                         max_phys_mapping = p_port->p_adapter->params.xfer_block_size;\r
964                 }\r
965                 /*if( NdisMInitializeScatterGatherDma( p_port->p_adapter->h_adapter,\r
966                                                         TRUE, max_phys_mapping )!= NDIS_STATUS_SUCCESS )\r
967                 {\r
968                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
969                                 ("NdisMInitializeScatterGatherDma failed\n" ) );\r
970                         return IB_INSUFFICIENT_RESOURCES;\r
971                 }*/\r
972         }\r
973 #endif\r
974 \r
975         /* Allocate the PD. */\r
976         status = p_port->p_adapter->p_ifc->alloc_pd(\r
977                 p_port->ib_mgr.h_ca, IB_PDT_UD, p_port, &p_port->ib_mgr.h_pd );\r
978         if( status != IB_SUCCESS )\r
979         {\r
980                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
981                         EVENT_IPOIB_ALLOC_PD, 1, status );\r
982                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
983                         ("ib_alloc_pd returned %s\n", \r
984                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
985                 return status;\r
986         }\r
987 \r
988         /* Allocate receive CQ. */\r
989         cq_create.size = p_port->p_adapter->params.rq_depth;\r
990         cq_create.pfn_comp_cb = __recv_cb;\r
991         cq_create.h_wait_obj = NULL;\r
992 \r
993         status = p_port->p_adapter->p_ifc->create_cq(\r
994                 p_port->ib_mgr.h_ca, &cq_create, p_port,\r
995                 __cq_event, &p_port->ib_mgr.h_recv_cq );\r
996         if( status != IB_SUCCESS )\r
997         {\r
998                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
999                         EVENT_IPOIB_CREATE_RECV_CQ, 1, status );\r
1000                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1001                         ("ib_create_cq returned %s.\n", \r
1002                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1003                 return status;\r
1004         }\r
1005 \r
1006         /* Allocate send CQ. */\r
1007         cq_create.size = p_port->p_adapter->params.sq_depth;\r
1008         cq_create.pfn_comp_cb = __send_cb;\r
1009 \r
1010         status = p_port->p_adapter->p_ifc->create_cq(\r
1011                 p_port->ib_mgr.h_ca, &cq_create, p_port,\r
1012                 __cq_event, &p_port->ib_mgr.h_send_cq );\r
1013         if( status != IB_SUCCESS )\r
1014         {\r
1015                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1016                         EVENT_IPOIB_CREATE_SEND_CQ, 1, status );\r
1017                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1018                         ("ib_create_cq returned %s.\n", \r
1019                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1020                 return status;\r
1021         }\r
1022         \r
1023         /* Allocate the QP. */\r
1024         cl_memclr( &qp_create, sizeof(qp_create) );\r
1025         qp_create.qp_type = IB_QPT_UNRELIABLE_DGRM;\r
1026         qp_create.rq_depth = p_port->p_adapter->params.rq_depth;\r
1027         qp_create.rq_sge = 2;   /* To support buffers spanning pages. */\r
1028         qp_create.h_rq_cq = p_port->ib_mgr.h_recv_cq;\r
1029         qp_create.sq_depth = p_port->p_adapter->params.sq_depth;\r
1030 \r
1031 #define UD_QP_USED_SGE 3\r
1032         qp_create.sq_sge = MAX_SEND_SGE < p_port->p_ca_attrs->max_sges ? \r
1033                         MAX_SEND_SGE  : ( p_port->p_ca_attrs->max_sges - UD_QP_USED_SGE );\r
1034         if ( !p_port->p_ca_attrs->ipoib_csum ) \r
1035         { \r
1036                 /* checksum is not supported by device\r
1037                 user must specify BYPASS to explicitly cancel checksum calculation */\r
1038                 if (p_port->p_adapter->params.send_chksum_offload == CSUM_ENABLED)\r
1039                         p_port->p_adapter->params.send_chksum_offload = CSUM_DISABLED;\r
1040                 if (p_port->p_adapter->params.recv_chksum_offload == CSUM_ENABLED)\r
1041                         p_port->p_adapter->params.recv_chksum_offload = CSUM_DISABLED;\r
1042         }\r
1043 \r
1044         qp_create.h_sq_cq = p_port->ib_mgr.h_send_cq;\r
1045         qp_create.sq_signaled = FALSE;\r
1046         status = p_port->p_adapter->p_ifc->create_qp(\r
1047                 p_port->ib_mgr.h_pd, &qp_create, p_port,\r
1048                 __qp_event, &p_port->ib_mgr.h_qp );\r
1049         if( status != IB_SUCCESS )\r
1050         {\r
1051                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1052                         EVENT_IPOIB_CREATE_QP, 1, status );\r
1053                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1054                         ("ib_create_qp returned %s\n", \r
1055                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1056                 return status;\r
1057         }\r
1058         /* Query the QP so we can get our QPN. */\r
1059         status = p_port->p_adapter->p_ifc->query_qp(\r
1060                 p_port->ib_mgr.h_qp, &qp_attr );\r
1061         if( status != IB_SUCCESS )\r
1062         {\r
1063                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1064                         EVENT_IPOIB_QUERY_QP, 1, status );\r
1065                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1066                         ("ib_query_qp returned %s\n", \r
1067                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1068                 return status;\r
1069         }\r
1070         p_port->ib_mgr.qpn = qp_attr.num;\r
1071 \r
1072         /* Register all of physical memory */\r
1073         phys_create.length = MEM_REG_SIZE;\r
1074         phys_create.num_ranges = 1;\r
1075         phys_create.range_array = &phys_range;\r
1076         phys_create.buf_offset = 0;\r
1077         phys_create.hca_page_size = PAGE_SIZE;\r
1078         phys_create.access_ctrl = IB_AC_LOCAL_WRITE;\r
1079         phys_range.base_addr = 0;\r
1080         phys_range.size = MEM_REG_SIZE;\r
1081         vaddr = 0;\r
1082         status = p_port->p_adapter->p_ifc->reg_phys(\r
1083                 p_port->ib_mgr.h_pd, &phys_create, &vaddr,\r
1084                 &p_port->ib_mgr.lkey, &rkey, &p_port->ib_mgr.h_mr );\r
1085         if( status != IB_SUCCESS )\r
1086         {\r
1087                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1088                         EVENT_IPOIB_REG_PHYS, 1, status );\r
1089                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1090                         ("ib_reg_phys returned %s\n", \r
1091                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1092                 return status;\r
1093         }\r
1094 \r
1095         status = ipoib_port_srq_init( p_port );\r
1096         if( status != IB_SUCCESS )\r
1097         {\r
1098                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1099                         ("ipoib_port_srq_init failed %s\n",\r
1100                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1101                 /* disable further CM initialization */\r
1102                 p_port->p_adapter->params.cm_enabled = FALSE;\r
1103 \r
1104                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1105                                 EVENT_IPOIB_CONNECTED_MODE_ERR, 1, 0xbadc0de1 );\r
1106 \r
1107         }\r
1108 //CM\r
1109 #if 0\r
1110         if( p_port->p_adapter->params.cm_enabled )\r
1111         {\r
1112                 status = endpt_cm_buf_mgr_init( p_port );\r
1113                 if( status != IB_SUCCESS )\r
1114                 {\r
1115                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1116                                 ("CM Init buf mgr failed status %#x\n", status ) );\r
1117                         ipoib_port_srq_destroy( p_port );\r
1118                         p_port->p_adapter->params.cm_enabled = FALSE;\r
1119 \r
1120                         NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1121                                 EVENT_IPOIB_CONNECTED_MODE_ERR, 1, 0xbadc0de2 );\r
1122                 }\r
1123                 else \r
1124                 {\r
1125                         if ( p_port->p_adapter->params.send_chksum_offload )\r
1126                         p_port->p_adapter->params.send_chksum_offload = CSUM_DISABLED;\r
1127                 }\r
1128         }\r
1129 #endif\r
1130         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1131         return IB_SUCCESS;\r
1132 }\r
1133 \r
1134 static void\r
1135 __srq_async_event_cb(\r
1136 IN                      ib_async_event_rec_t            *p_event_rec )\r
1137 {\r
1138         ipoib_port_t* p_port = \r
1139                 (ipoib_port_t *)p_event_rec->context;\r
1140 \r
1141         switch( p_event_rec->code )\r
1142         {\r
1143         case IB_AE_SRQ_LIMIT_REACHED:\r
1144                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1145                         ("SRQ ASYNC EVENT CODE %d: %s\n", \r
1146                         p_event_rec->code, "IB_AE_SRQ_LIMIT_REACHED" ) );\r
1147                         break;\r
1148         case IB_AE_SRQ_CATAS_ERROR:\r
1149                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1150                                 ("SRQ ASYNC EVENT CODE %d: %s\n", \r
1151                                 p_event_rec->code, "IB_AE_SRQ_CATAS_ERROR" ) );\r
1152                         /*SRQ is in err state, must reinitialize */\r
1153                         p_port->p_adapter->hung = TRUE;\r
1154                         break;\r
1155         case IB_AE_SRQ_QP_LAST_WQE_REACHED:\r
1156                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1157                                 ("SRQ ASYNC EVENT CODE %d: %s\n", \r
1158                                 p_event_rec->code, "IB_AE_SRQ_QP_LAST_WQE_REACHED" ) );\r
1159                         /*SRQ is in err state, must reinitialize */\r
1160                         p_port->p_adapter->hung = TRUE;\r
1161                         break;\r
1162         default:\r
1163                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1164                                 ("ASYNC EVENT CODE ARRIVED %d(%#x)\n", \r
1165                                 p_event_rec->code, p_event_rec->code ) );\r
1166         }\r
1167 }\r
1168 \r
1169 ib_api_status_t\r
1170 ipoib_port_srq_init(\r
1171         IN                              ipoib_port_t* const                     p_port )\r
1172 {\r
1173         ib_api_status_t         ib_status;\r
1174         ib_srq_handle_t         h_srq;\r
1175         ib_srq_attr_t           srq_attr;\r
1176 \r
1177         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1178         \r
1179         if( !p_port->p_adapter->params.cm_enabled )\r
1180                 return IB_SUCCESS;\r
1181 \r
1182         srq_attr.max_sge = min( 2, p_port->p_ca_attrs->max_srq_sges );\r
1183         srq_attr.srq_limit = 10;\r
1184         srq_attr.max_wr = \r
1185                 min( (uint32_t)p_port->p_adapter->params.rq_depth * 8,\r
1186                                 p_port->p_ca_attrs->max_srq_wrs/2 );\r
1187 \r
1188         ib_status = p_port->p_adapter->p_ifc->create_srq( \r
1189                                                                 p_port->ib_mgr.h_pd, \r
1190                                                                 &srq_attr, \r
1191                                                                 p_port, \r
1192                                                                 __srq_async_event_cb, \r
1193                                                                 &h_srq );\r
1194         if( ib_status != IB_SUCCESS )\r
1195         {\r
1196                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1197                         EVENT_IPOIB_CREATE_QP, 1, ib_status );\r
1198                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1199                         ("ib_create_srq failed status %s\n", \r
1200                         p_port->p_adapter->p_ifc->get_err_str( ib_status )) );\r
1201                 return ib_status;\r
1202         }\r
1203         p_port->ib_mgr.h_srq = h_srq;\r
1204 \r
1205         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1206 \r
1207         return ib_status;\r
1208 }\r
1209 \r
1210 /*  __port_query_ca_attrs() \r
1211  *  returns a pointer to allocated memory.\r
1212  *  must be released by caller.\r
1213  */\r
1214 static ib_api_status_t\r
1215 __port_query_ca_attrs( \r
1216         IN              ipoib_port_t* const             p_port,\r
1217         IN              ib_ca_attr_t**                  pp_ca_attrs )\r
1218 {\r
1219         ib_api_status_t         ib_status;\r
1220         uint32_t                        attr_size;\r
1221         ib_ca_attr_t*           p_ca_attrs;\r
1222 \r
1223         *pp_ca_attrs = NULL;\r
1224 \r
1225         ib_status = \r
1226                 p_port->p_adapter->p_ifc->query_ca( p_port->ib_mgr.h_ca, NULL , &attr_size );\r
1227         if( ib_status != IB_INSUFFICIENT_MEMORY )\r
1228         {\r
1229                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1230                         ("ib_query_ca failed status %s\n",\r
1231                         p_port->p_adapter->p_ifc->get_err_str( ib_status )) );\r
1232                 goto done;\r
1233         }\r
1234         CL_ASSERT( attr_size );\r
1235 \r
1236         p_ca_attrs = (ib_ca_attr_t *) cl_zalloc( attr_size );\r
1237         if ( p_ca_attrs == NULL )\r
1238         {\r
1239                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1240                         ("Allocate %d bytes failed for CA Attributes\n", attr_size ));\r
1241                 ib_status = IB_INSUFFICIENT_MEMORY;\r
1242                 goto done;\r
1243         }\r
1244 \r
1245         ib_status = \r
1246                 p_port->p_adapter->p_ifc->query_ca( p_port->ib_mgr.h_ca, p_ca_attrs , &attr_size );\r
1247         if ( ib_status != IB_SUCCESS )\r
1248         {\r
1249                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1250                         ("CA attributes query failed\n") );\r
1251                 cl_free ( p_ca_attrs );\r
1252                 goto done;\r
1253         }\r
1254 \r
1255         *pp_ca_attrs = p_ca_attrs;\r
1256 done:\r
1257         return ib_status;\r
1258 }\r
1259 \r
1260 void\r
1261 ipoib_port_srq_destroy( \r
1262         IN                              ipoib_port_t* const                     p_port )\r
1263 {\r
1264         ib_api_status_t status;\r
1265 \r
1266         if( p_port->ib_mgr.h_srq )\r
1267         {\r
1268                 status =\r
1269                         p_port->p_adapter->p_ifc->destroy_srq( p_port->ib_mgr.h_srq, NULL );\r
1270                 CL_ASSERT( status == IB_SUCCESS );\r
1271                 p_port->ib_mgr.h_srq = NULL;\r
1272         }\r
1273 }\r
1274 \r
1275 static void\r
1276 __ib_mgr_destroy(\r
1277         IN                              ipoib_port_t* const                     p_port )\r
1278 {\r
1279         ib_api_status_t status;\r
1280 \r
1281         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1282 \r
1283         if( p_port->ib_mgr.h_ca )\r
1284         {\r
1285                 status =\r
1286                         p_port->p_adapter->p_ifc->close_ca( p_port->ib_mgr.h_ca, NULL );\r
1287                 CL_ASSERT( status == IB_SUCCESS );\r
1288                 p_port->ib_mgr.h_ca = NULL;\r
1289         }\r
1290 \r
1291         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1292 }\r
1293 \r
1294 \r
1295 \r
1296 /******************************************************************************\r
1297 *\r
1298 * Buffer manager implementation.\r
1299 *\r
1300 ******************************************************************************/\r
1301 static void\r
1302 __buf_mgr_construct(\r
1303         IN                              ipoib_port_t* const                     p_port )\r
1304 {\r
1305         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1306 \r
1307         cl_qpool_construct( &p_port->buf_mgr.recv_pool );\r
1308 \r
1309         p_port->buf_mgr.h_packet_pool = NULL;\r
1310         p_port->buf_mgr.h_buffer_pool = NULL;\r
1311 \r
1312         NdisInitializeNPagedLookasideList( &p_port->buf_mgr.send_buf_list,\r
1313                 NULL, NULL, 0, MAX_XFER_BLOCK_SIZE, 'bipi', 0 );\r
1314 \r
1315         p_port->buf_mgr.h_send_pkt_pool = NULL;\r
1316         p_port->buf_mgr.h_send_buf_pool = NULL;\r
1317 \r
1318         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1319 }\r
1320 \r
1321 \r
1322 static ib_api_status_t\r
1323 __buf_mgr_init(\r
1324         IN                              ipoib_port_t* const                     p_port )\r
1325 {\r
1326         cl_status_t             cl_status;\r
1327         ipoib_params_t  *p_params;\r
1328         NET_BUFFER_LIST_POOL_PARAMETERS pool_parameters;\r
1329         IPOIB_ENTER(IPOIB_DBG_INIT );\r
1330 \r
1331         CL_ASSERT( p_port );\r
1332         CL_ASSERT( p_port->p_adapter );\r
1333 \r
1334         p_params = &p_port->p_adapter->params;\r
1335 \r
1336         /* Allocate the receive descriptor pool */\r
1337         cl_status = cl_qpool_init( &p_port->buf_mgr.recv_pool,\r
1338                 p_params->rq_depth * p_params->recv_pool_ratio,\r
1339 #if IPOIB_INLINE_RECV\r
1340                 0, 0, sizeof(ipoib_recv_desc_t), __recv_ctor, NULL, p_port );\r
1341 #else   /* IPOIB_INLINE_RECV */\r
1342                 0, 0, sizeof(ipoib_recv_desc_t), __recv_ctor, __recv_dtor, p_port );\r
1343 #endif  /* IPOIB_INLINE_RECV */\r
1344         if( cl_status != CL_SUCCESS )\r
1345         {\r
1346                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1347                         EVENT_IPOIB_RECV_POOL, 1, cl_status );\r
1348                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1349                         ("cl_qpool_init for recvs returned %#x\n",\r
1350                         cl_status) );\r
1351                 return IB_INSUFFICIENT_MEMORY;\r
1352         }\r
1353 \r
1354         /* Allocate the NET BUFFER list pools for receive indication. */\r
1355         NdisZeroMemory(&pool_parameters, sizeof(NET_BUFFER_LIST_POOL_PARAMETERS));\r
1356     pool_parameters.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;\r
1357     pool_parameters.Header.Revision = NET_BUFFER_LIST_POOL_PARAMETERS_REVISION_1;\r
1358     pool_parameters.Header.Size = sizeof(pool_parameters);\r
1359     pool_parameters.ProtocolId = 0;\r
1360     pool_parameters.ContextSize = 0;\r
1361     pool_parameters.fAllocateNetBuffer = TRUE;\r
1362     pool_parameters.PoolTag = 'CRPI';\r
1363 \r
1364     p_port->buf_mgr.h_packet_pool = NdisAllocateNetBufferListPool(\r
1365                 p_port->p_adapter->h_adapter,\r
1366                 &pool_parameters); \r
1367 \r
1368         if( !p_port->buf_mgr.h_packet_pool )\r
1369         {\r
1370                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1371                         EVENT_IPOIB_RECV_PKT_POOL, 1, NDIS_STATUS_RESOURCES  );\r
1372                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1373                         ("NdisAllocatePacketPool returned %08X\n", (UINT)NDIS_STATUS_RESOURCES) );\r
1374                 return IB_INSUFFICIENT_RESOURCES;\r
1375         }\r
1376 /*\r
1377         NdisAllocateBufferPool( &ndis_status, &p_port->buf_mgr.h_buffer_pool,\r
1378                 p_params->rq_depth );\r
1379         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1380         {\r
1381                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1382                         EVENT_IPOIB_RECV_BUF_POOL, 1, ndis_status );\r
1383                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1384                         ("NdisAllocateBufferPool returned %08X\n", ndis_status) );\r
1385                 return IB_INSUFFICIENT_RESOURCES;\r
1386         }\r
1387 */\r
1388         /* Allocate the NET buffer list pool for send formatting. */\r
1389     pool_parameters.PoolTag = 'XTPI';\r
1390 \r
1391     p_port->buf_mgr.h_send_pkt_pool = NdisAllocateNetBufferListPool(\r
1392                 p_port->p_adapter->h_adapter,\r
1393                 &pool_parameters); \r
1394         if( !p_port->buf_mgr.h_send_pkt_pool)\r
1395         {\r
1396                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1397                         EVENT_IPOIB_SEND_PKT_POOL, 1, NDIS_STATUS_RESOURCES );\r
1398                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1399                         ("NdisAllocatePacketPool returned %08X\n", (UINT)NDIS_STATUS_RESOURCES) );\r
1400                 return IB_INSUFFICIENT_RESOURCES;\r
1401         }\r
1402 /*\r
1403         NdisAllocateBufferPool( &ndis_status,\r
1404                 &p_port->buf_mgr.h_send_buf_pool, 1 );\r
1405         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1406         {\r
1407                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1408                         EVENT_IPOIB_SEND_BUF_POOL, 1, ndis_status );\r
1409                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1410                         ("NdisAllocateBufferPool returned %08X\n", ndis_status) );\r
1411                 return IB_INSUFFICIENT_RESOURCES;\r
1412         }\r
1413 */\r
1414         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1415         return IB_SUCCESS;\r
1416 }\r
1417 \r
1418 \r
1419 static void\r
1420 __buf_mgr_destroy(\r
1421         IN                              ipoib_port_t* const                     p_port )\r
1422 {\r
1423         IPOIB_ENTER(IPOIB_DBG_INIT );\r
1424 \r
1425         CL_ASSERT( p_port );\r
1426 \r
1427         /* Destroy the send packet and buffer pools. \r
1428         if( p_port->buf_mgr.h_send_buf_pool )\r
1429                 NdisFreeBufferPool( p_port->buf_mgr.h_send_buf_pool );*/\r
1430         if( p_port->buf_mgr.h_send_pkt_pool )\r
1431                 NdisFreeNetBufferListPool ( p_port->buf_mgr.h_send_pkt_pool );\r
1432 \r
1433         /* Destroy the receive packet and buffer pools. \r
1434         if( p_port->buf_mgr.h_buffer_pool )\r
1435                 NdisFreeBufferPool( p_port->buf_mgr.h_buffer_pool );*/\r
1436         if( p_port->buf_mgr.h_packet_pool )\r
1437                 NdisFreeNetBufferListPool ( p_port->buf_mgr.h_packet_pool );\r
1438 \r
1439         /* Free the receive and send descriptors. */\r
1440         cl_qpool_destroy( &p_port->buf_mgr.recv_pool );\r
1441 \r
1442         /* Free the lookaside list of scratch buffers. */\r
1443         NdisDeleteNPagedLookasideList( &p_port->buf_mgr.send_buf_list );\r
1444 \r
1445         IPOIB_EXIT(  IPOIB_DBG_INIT );\r
1446 }\r
1447 \r
1448 \r
1449 static cl_status_t\r
1450 __recv_ctor(\r
1451         IN                              void* const                                     p_object,\r
1452         IN                              void*                                           context,\r
1453                 OUT                     cl_pool_item_t** const          pp_pool_item )\r
1454 {\r
1455         ipoib_recv_desc_t       *p_desc;\r
1456         ipoib_port_t            *p_port;\r
1457 \r
1458 #if IPOIB_INLINE_RECV\r
1459         uint32_t                        ds0_len;\r
1460 #endif\r
1461 \r
1462         IPOIB_ENTER( IPOIB_DBG_ALLOC );\r
1463 \r
1464         CL_ASSERT( p_object );\r
1465         CL_ASSERT( context );\r
1466 \r
1467         p_desc = (ipoib_recv_desc_t*)p_object;\r
1468         p_port = (ipoib_port_t*)context;\r
1469 \r
1470         /* Setup the work request. */\r
1471         p_desc->wr.ds_array = p_desc->local_ds;\r
1472         p_desc->wr.wr_id = (uintn_t)p_desc;\r
1473 \r
1474 #if IPOIB_INLINE_RECV\r
1475         /* Sanity check on the receive buffer layout */\r
1476         CL_ASSERT( (void*)&p_desc->buf.eth.pkt.type ==\r
1477                 (void*)&p_desc->buf.ib.pkt.type );\r
1478         CL_ASSERT( sizeof(recv_buf_t) == sizeof(ipoib_pkt_t) + sizeof(ib_grh_t) );\r
1479 \r
1480         /* Setup the local data segment. */\r
1481         p_desc->local_ds[0].vaddr = cl_get_physaddr( &p_desc->buf );\r
1482         p_desc->local_ds[0].lkey = p_port->ib_mgr.lkey;\r
1483         ds0_len =\r
1484                 PAGE_SIZE - ((uint32_t)p_desc->local_ds[0].vaddr & (PAGE_SIZE - 1));\r
1485         if( ds0_len >= sizeof(recv_buf_t) )\r
1486         {\r
1487                 /* The whole buffer is within a page. */\r
1488                 p_desc->local_ds[0].length = ds0_len;\r
1489                 p_desc->wr.num_ds = 1;\r
1490         }\r
1491         else\r
1492         {\r
1493                 /* The buffer crosses page boundaries. */\r
1494                 p_desc->local_ds[0].length = ds0_len;\r
1495                 p_desc->local_ds[1].vaddr = cl_get_physaddr( \r
1496                         ((uint8_t*)&p_desc->buf) + ds0_len );\r
1497                 p_desc->local_ds[1].lkey = p_port->ib_mgr.lkey;\r
1498                 p_desc->local_ds[1].length = sizeof(recv_buf_t) - ds0_len;\r
1499                 p_desc->wr.num_ds = 2;\r
1500         }\r
1501 #else   /* IPOIB_INLINE_RECV */\r
1502         /* Allocate the receive buffer. */\r
1503         p_desc->p_buf = (recv_buf_t*)cl_zalloc( sizeof(recv_buf_t) );\r
1504         if( !p_desc->p_buf )\r
1505         {\r
1506                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1507                         ("Failed to allocate receive buffer.\n") );\r
1508                 return CL_INSUFFICIENT_MEMORY;\r
1509         }\r
1510 \r
1511         /* Sanity check on the receive buffer layout */\r
1512         CL_ASSERT( (void*)&p_desc->p_buf->eth.pkt.type ==\r
1513                 (void*)&p_desc->p_buf->ib.pkt.type );\r
1514 \r
1515         /* Setup the local data segment. */\r
1516         p_desc->local_ds[0].vaddr = cl_get_physaddr( p_desc->p_buf );\r
1517         p_desc->local_ds[0].length = sizeof(ipoib_pkt_t) + sizeof(ib_grh_t);\r
1518         p_desc->local_ds[0].lkey = p_port->ib_mgr.lkey;\r
1519         p_desc->wr.num_ds = 1;\r
1520 #endif  /* IPOIB_INLINE_RECV */\r
1521 \r
1522         *pp_pool_item = &p_desc->item;\r
1523 \r
1524         IPOIB_EXIT( IPOIB_DBG_ALLOC );\r
1525         return CL_SUCCESS;\r
1526 }\r
1527 \r
1528 \r
1529 #if !IPOIB_INLINE_RECV\r
1530 static void\r
1531 __recv_dtor(\r
1532         IN              const   cl_pool_item_t* const           p_pool_item,\r
1533         IN                              void                                            *context )\r
1534 {\r
1535         ipoib_recv_desc_t       *p_desc;\r
1536 \r
1537         IPOIB_ENTER(  IPOIB_DBG_ALLOC );\r
1538 \r
1539         UNUSED_PARAM( context );\r
1540 \r
1541         p_desc = PARENT_STRUCT( p_pool_item, ipoib_recv_desc_t, item );\r
1542 \r
1543         if( p_desc->p_buf )\r
1544                 cl_free( p_desc->p_buf );\r
1545 \r
1546         IPOIB_EXIT( IPOIB_DBG_ALLOC );\r
1547 }\r
1548 #endif\r
1549 \r
1550 \r
1551 static inline ipoib_recv_desc_t*\r
1552 __buf_mgr_get_recv(\r
1553         IN                              ipoib_port_t* const                     p_port )\r
1554 {\r
1555         ipoib_recv_desc_t       *p_desc;\r
1556         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1557         p_desc = (ipoib_recv_desc_t*)cl_qpool_get( &p_port->buf_mgr.recv_pool );\r
1558         /* Reference the port object for the send. */\r
1559         if( p_desc )\r
1560         {\r
1561                 ipoib_port_ref( p_port, ref_get_recv );\r
1562                 CL_ASSERT( p_desc->wr.wr_id == (uintn_t)p_desc );\r
1563 #if IPOIB_INLINE_RECV\r
1564                 CL_ASSERT( p_desc->local_ds[0].vaddr ==\r
1565                         cl_get_physaddr( &p_desc->buf ) );\r
1566 #else   /* IPOIB_INLINE_RECV */\r
1567                 CL_ASSERT( p_desc->local_ds[0].vaddr ==\r
1568                         cl_get_physaddr( p_desc->p_buf ) );\r
1569                 CL_ASSERT( p_desc->local_ds[0].length ==\r
1570                         (sizeof(ipoib_pkt_t) + sizeof(ib_grh_t)) );\r
1571 #endif  /* IPOIB_INLINE_RECV */\r
1572                 CL_ASSERT( p_desc->local_ds[0].lkey == p_port->ib_mgr.lkey );\r
1573         }\r
1574         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1575         return p_desc;\r
1576 }\r
1577 \r
1578 \r
1579 //NDIS60\r
1580 static inline void\r
1581 __buf_mgr_put_recv(\r
1582         IN                              ipoib_port_t* const                     p_port,\r
1583         IN                              ipoib_recv_desc_t* const        p_desc,\r
1584         IN                              NET_BUFFER_LIST* const          p_net_buffer_list OPTIONAL )\r
1585 {\r
1586         NET_BUFFER              *p_buf = NULL;\r
1587         MDL                             *p_mdl = NULL;\r
1588         IPOIB_ENTER(IPOIB_DBG_RECV );\r
1589 \r
1590         if( p_net_buffer_list )\r
1591         {\r
1592                 NET_BUFFER_LIST_NEXT_NBL(p_net_buffer_list) = NULL;\r
1593                 p_buf = NET_BUFFER_LIST_FIRST_NB(p_net_buffer_list);\r
1594                 CL_ASSERT( p_buf );\r
1595                 p_mdl = NET_BUFFER_FIRST_MDL(p_buf);\r
1596                 CL_ASSERT( p_mdl );\r
1597                 NdisFreeMdl(p_mdl);\r
1598                 NdisFreeNetBufferList(p_net_buffer_list);\r
1599         }\r
1600 \r
1601         /* Return the descriptor to its pools. */\r
1602         cl_qpool_put( &p_port->buf_mgr.recv_pool, &p_desc->item );\r
1603 \r
1604         /*\r
1605          * Dereference the port object since the receive is no longer outstanding.\r
1606          */\r
1607         ipoib_port_deref( p_port, ref_get_recv );\r
1608         IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1609 }\r
1610 \r
1611 \r
1612 static inline void\r
1613 __buf_mgr_put_recv_list(\r
1614         IN                              ipoib_port_t* const                     p_port,\r
1615         IN                              cl_qlist_t* const                       p_list )\r
1616 {\r
1617         //IPOIB_ENTER(  IPOIB_DBG_RECV );\r
1618         cl_qpool_put_list( &p_port->buf_mgr.recv_pool, p_list );\r
1619         //IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1620 }\r
1621 \r
1622 \r
1623 static inline NET_BUFFER_LIST*\r
1624 __buf_mgr_get_ndis_pkt(\r
1625         IN                              ipoib_port_t* const                     p_port,\r
1626         IN                              ipoib_recv_desc_t* const        p_desc )\r
1627 {\r
1628         NET_BUFFER_LIST                 *p_net_buffer_list;\r
1629         MDL                                             *p_mdl;\r
1630 \r
1631         IPOIB_ENTER(  IPOIB_DBG_RECV );\r
1632 \r
1633         p_mdl = NdisAllocateMdl(p_port->p_adapter->h_adapter,\r
1634                                                         &p_desc->buf.eth.pkt,\r
1635                                                         p_desc->len );\r
1636         if( !p_mdl )\r
1637         {\r
1638                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1639                         ("Failed to allocate MDL\n") );\r
1640                 return NULL;\r
1641         }\r
1642 \r
1643         p_net_buffer_list = NdisAllocateNetBufferAndNetBufferList(\r
1644                                                 p_port->buf_mgr.h_packet_pool,\r
1645                                                 0,\r
1646                                                 0,\r
1647                                                 p_mdl,\r
1648                                                 0,\r
1649                                                 0);\r
1650 \r
1651         if( !p_net_buffer_list )\r
1652         {\r
1653                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1654                         ("Failed to allocate NET_BUFFER_LIST\n") );\r
1655                 NdisFreeMdl(p_mdl);\r
1656                 return NULL;\r
1657         }\r
1658 \r
1659         NET_BUFFER_LIST_NEXT_NBL(p_net_buffer_list) = NULL;\r
1660         IPOIB_PORT_FROM_PACKET( p_net_buffer_list ) = p_port;\r
1661         IPOIB_RECV_FROM_PACKET( p_net_buffer_list ) = p_desc;\r
1662         p_net_buffer_list->SourceHandle = p_port->p_adapter->h_adapter;\r
1663 \r
1664         IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1665         return p_net_buffer_list;\r
1666 }\r
1667 \r
1668 \r
1669 /******************************************************************************\r
1670 *\r
1671 * Receive manager implementation.\r
1672 *\r
1673 ******************************************************************************/\r
1674 static void\r
1675 __recv_mgr_construct(\r
1676         IN                              ipoib_port_t* const                     p_port )\r
1677 {\r
1678         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1679 \r
1680         cl_qlist_init( &p_port->recv_mgr.done_list );\r
1681 \r
1682         p_port->recv_mgr.recv_pkt_array = NULL;\r
1683 \r
1684         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1685 }\r
1686 \r
1687 \r
1688 static ib_api_status_t\r
1689 __recv_mgr_init(\r
1690         IN                              ipoib_port_t* const                     p_port )\r
1691 {\r
1692         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1693 \r
1694         /* Allocate the NDIS_PACKET pointer array for indicating receives. */\r
1695         p_port->recv_mgr.recv_pkt_array = (NET_BUFFER_LIST **)cl_malloc(\r
1696                 sizeof(NET_BUFFER_LIST*) * p_port->p_adapter->params.rq_depth );\r
1697         if( !p_port->recv_mgr.recv_pkt_array )\r
1698         {\r
1699                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1700                         EVENT_IPOIB_RECV_PKT_ARRAY, 0 );\r
1701                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1702                         ("cl_malloc for PNDIS_PACKET array failed.\n") );\r
1703                 return IB_INSUFFICIENT_MEMORY;\r
1704         }\r
1705 \r
1706         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1707         return IB_SUCCESS;\r
1708 }\r
1709 \r
1710 \r
1711 static void\r
1712 __recv_mgr_destroy(\r
1713         IN                              ipoib_port_t* const                     p_port )\r
1714 {\r
1715         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1716 \r
1717         CL_ASSERT( cl_is_qlist_empty( &p_port->recv_mgr.done_list ) );\r
1718         CL_ASSERT( !p_port->recv_mgr.depth );\r
1719 \r
1720         if( p_port->recv_mgr.recv_pkt_array )\r
1721                 cl_free( p_port->recv_mgr.recv_pkt_array );\r
1722 \r
1723         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1724 }\r
1725 \r
1726 \r
1727 /*\r
1728  * Posts receive buffers to the receive queue and returns the number\r
1729  * of receives needed to bring the RQ to its low water mark.  Note\r
1730  * that the value is signed, and can go negative.  All tests must\r
1731  * be for > 0.\r
1732  */\r
1733 int32_t\r
1734 __recv_mgr_repost(\r
1735         IN                              ipoib_port_t* const                     p_port )\r
1736 {\r
1737         ipoib_recv_desc_t       *p_head = NULL, *p_tail = NULL, *p_next;\r
1738         ib_api_status_t         status;\r
1739         ib_recv_wr_t            *p_failed;\r
1740         PERF_DECLARE( GetRecv );\r
1741         PERF_DECLARE( PostRecv );\r
1742 \r
1743         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1744 \r
1745         CL_ASSERT( p_port );\r
1746         cl_obj_lock( &p_port->obj );\r
1747         if( p_port->state != IB_QPS_RTS )\r
1748         {\r
1749                 cl_obj_unlock( &p_port->obj );\r
1750                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1751                         ("Port in invalid state.  Not reposting.\n") );\r
1752                 return 0;\r
1753         }\r
1754         ipoib_port_ref( p_port, ref_repost );\r
1755         cl_obj_unlock( &p_port->obj );\r
1756 \r
1757         while( p_port->recv_mgr.depth < p_port->p_adapter->params.rq_depth )\r
1758         {\r
1759                 /* Pull receives out of the pool and chain them up. */\r
1760                 cl_perf_start( GetRecv );\r
1761                 p_next = __buf_mgr_get_recv( p_port );\r
1762                 cl_perf_stop( &p_port->p_adapter->perf, GetRecv );\r
1763                 if( !p_next )\r
1764                 {\r
1765                         IPOIB_PRINT(TRACE_LEVEL_VERBOSE, IPOIB_DBG_RECV,\r
1766                                 ("Out of receive descriptors! recv queue depth 0x%x\n",p_port->recv_mgr.depth) );\r
1767                         break;\r
1768                 }\r
1769 \r
1770                 if( !p_tail )\r
1771                 {\r
1772                         p_tail = p_next;\r
1773                         p_next->wr.p_next = NULL;\r
1774                 }\r
1775                 else\r
1776                 {\r
1777                         p_next->wr.p_next = &p_head->wr;\r
1778                 }\r
1779 \r
1780                 p_head = p_next;\r
1781 \r
1782                 p_port->recv_mgr.depth++;\r
1783         }\r
1784 \r
1785         if( p_head )\r
1786         {\r
1787                 cl_perf_start( PostRecv );\r
1788                 status = p_port->p_adapter->p_ifc->post_recv(\r
1789                         p_port->ib_mgr.h_qp, &p_head->wr, &p_failed );\r
1790                 cl_perf_stop( &p_port->p_adapter->perf, PostRecv );\r
1791 \r
1792                 if( status != IB_SUCCESS )\r
1793                 {\r
1794                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1795                                 ("ip_post_recv returned %s\n", \r
1796                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1797                         /* return the descriptors to the pool */\r
1798                         while( p_failed )\r
1799                         {\r
1800                                 p_head = PARENT_STRUCT( p_failed, ipoib_recv_desc_t, wr );\r
1801                                 p_failed = p_failed->p_next;\r
1802 \r
1803                                 __buf_mgr_put_recv( p_port, p_head, NULL );\r
1804                                 p_port->recv_mgr.depth--;\r
1805                         }\r
1806                 }\r
1807         }\r
1808 \r
1809         ipoib_port_deref( p_port, ref_repost );\r
1810         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1811         return p_port->p_adapter->params.rq_low_watermark - p_port->recv_mgr.depth;\r
1812 }\r
1813 \r
1814 void\r
1815 ipoib_return_net_buffer_list(\r
1816         IN                              NDIS_HANDLE                                     adapter_context,\r
1817         IN                              NET_BUFFER_LIST                         *p_net_buffer_lists,\r
1818         IN                              ULONG                                           return_flags)\r
1819 {\r
1820 //      cl_list_item_t          *p_item;\r
1821         ipoib_port_t            *p_port;\r
1822         ipoib_recv_desc_t       *p_desc;\r
1823         NET_BUFFER_LIST         *cur_net_buffer_list,*next_net_buffer_list;\r
1824 //      ib_api_status_t         status = IB_NOT_DONE;\r
1825 //      int32_t                         shortage;\r
1826 //      ULONG                           complete_flags = 0;\r
1827         PERF_DECLARE( ReturnPacket );\r
1828         PERF_DECLARE( ReturnPutRecv );\r
1829         PERF_DECLARE( ReturnRepostRecv );\r
1830         PERF_DECLARE( ReturnPreparePkt );\r
1831         PERF_DECLARE( ReturnNdisIndicate );\r
1832 \r
1833         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1834 \r
1835         UNUSED_PARAM( return_flags );\r
1836 \r
1837         p_port = ((ipoib_adapter_t*)adapter_context)->p_port;\r
1838         CL_ASSERT( p_net_buffer_lists );\r
1839 \r
1840         cl_perf_start( ReturnPacket );\r
1841         cl_spinlock_acquire( &p_port->recv_lock );\r
1842         for (cur_net_buffer_list = p_net_buffer_lists;\r
1843                  cur_net_buffer_list != NULL;\r
1844                  cur_net_buffer_list = next_net_buffer_list)\r
1845         {\r
1846                 next_net_buffer_list = NET_BUFFER_LIST_NEXT_NBL(cur_net_buffer_list);\r
1847 \r
1848                 /* Get the port and descriptor from the packet. */\r
1849                 CL_ASSERT(p_port == IPOIB_PORT_FROM_PACKET( cur_net_buffer_list ));\r
1850                 p_desc = IPOIB_RECV_FROM_PACKET( cur_net_buffer_list );\r
1851 \r
1852                 \r
1853                 //TODO: NDIS60, rewrite this block\r
1854                 /* Get descriptor from the packet. */\r
1855 #if 0\r
1856                 if( p_desc->type == PKT_TYPE_CM_UCAST )\r
1857                 {\r
1858                         NDIS_BUFFER             *p_buf;\r
1859 \r
1860                         /* Unchain the NDIS buffer. */\r
1861                         NdisUnchainBufferAtFront( p_packet, &p_buf );\r
1862                         CL_ASSERT( p_buf );\r
1863                         /* Return the NDIS packet and NDIS buffer to their pools. */\r
1864                         NdisDprFreePacketNonInterlocked( p_packet );\r
1865                         NdisFreeBuffer( p_buf );\r
1866 \r
1867                         endpt_cm_buf_mgr_put_recv( &p_port->cm_buf_mgr, (ipoib_cm_desc_t *)p_desc );\r
1868                         status = endpt_cm_post_recv( p_port );\r
1869                         if(  status != IB_SUCCESS )\r
1870                         {\r
1871                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1872                                         ("Post Recv QP failed\n" ) );\r
1873                         }\r
1874                         cl_spinlock_release( &p_port->recv_lock );\r
1875                         return;\r
1876                 }\r
1877 #endif\r
1878 \r
1879                 cl_perf_start( ReturnPutRecv );\r
1880                 __buf_mgr_put_recv( p_port, p_desc, cur_net_buffer_list );\r
1881                 cl_perf_stop( &p_port->p_adapter->perf, ReturnPutRecv );\r
1882         }\r
1883 #if 0            \r
1884         /* Repost buffers. */\r
1885         cl_perf_start( ReturnRepostRecv );\r
1886         shortage = __recv_mgr_repost( p_port );\r
1887         cl_perf_stop( &p_port->p_adapter->perf, ReturnRepostRecv );\r
1888 \r
1889         for( p_item = cl_qlist_remove_head( &p_port->recv_mgr.done_list );\r
1890                 p_item != cl_qlist_end( &p_port->recv_mgr.done_list );\r
1891                 p_item = cl_qlist_remove_head( &p_port->recv_mgr.done_list ) )\r
1892         {\r
1893                 p_desc = (ipoib_recv_desc_t*)p_item;\r
1894 \r
1895                 cl_perf_start( ReturnPreparePkt );\r
1896                 status = __recv_mgr_prepare_pkt( p_port, p_desc, &cur_net_buffer_list );\r
1897                 cl_perf_stop( &p_port->p_adapter->perf, ReturnPreparePkt );\r
1898                 if( status == IB_SUCCESS )\r
1899                 {\r
1900                         if( shortage > 0 )\r
1901                                 NET_BUFFER_LIST_STATUS( cur_net_buffer_list) = NDIS_STATUS_RESOURCES;\r
1902                         else\r
1903                                 NET_BUFFER_LIST_STATUS( cur_net_buffer_list) = NDIS_STATUS_SUCCESS;\r
1904 \r
1905                         cl_spinlock_release( &p_port->recv_lock );\r
1906                         NET_BUFFER_LIST_NEXT_NBL(cur_net_buffer_list) = NULL;\r
1907                         cl_perf_start( ReturnNdisIndicate );\r
1908                         NdisMRecvIndicate( p_port->p_adapter->h_adapter,\r
1909                                 cur_net_buffer_list, complete_flags );\r
1910                         cl_perf_stop( &p_port->p_adapter->perf, ReturnNdisIndicate );\r
1911                         cl_spinlock_acquire( &p_port->recv_lock );\r
1912 \r
1913                         if( shortage > 0 )\r
1914                         {\r
1915                                 cl_perf_start( ReturnPutRecv );\r
1916                                 __buf_mgr_put_recv( p_port, p_desc, cur_net_buffer_list );\r
1917                                 cl_perf_stop( &p_port->p_adapter->perf, ReturnPutRecv );\r
1918 \r
1919                                 /* Repost buffers. */\r
1920                                 cl_perf_start( ReturnRepostRecv );\r
1921                                 shortage = __recv_mgr_repost( p_port );\r
1922                                 cl_perf_stop( &p_port->p_adapter->perf, ReturnRepostRecv );\r
1923                         }\r
1924                 }\r
1925                 else if( status != IB_NOT_DONE )\r
1926                 {\r
1927                         IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1928                                 ("__recv_mgr_prepare_pkt returned %s\n",\r
1929                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1930                         /* Return the item to the head of the list. */\r
1931                         cl_qlist_insert_head( &p_port->recv_mgr.done_list, p_item );\r
1932                         break;\r
1933                 }\r
1934         }\r
1935         #endif\r
1936         cl_spinlock_release( &p_port->recv_lock );\r
1937         cl_perf_stop( &p_port->p_adapter->perf, ReturnPacket );\r
1938 \r
1939         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1940 }\r
1941 \r
1942 static void __recv_cb_dpc(KDPC *p_gc_dpc,void *context,void * s_arg1 , void * s_arg2)\r
1943 {\r
1944 \r
1945         ipoib_port_t *p_port = (ipoib_port_t *) context;\r
1946 \r
1947         UNREFERENCED_PARAMETER(p_gc_dpc);\r
1948         UNREFERENCED_PARAMETER(s_arg1);\r
1949         UNREFERENCED_PARAMETER(s_arg2);\r
1950 \r
1951 \r
1952         __recv_cb(NULL, p_port);\r
1953         ipoib_port_deref( p_port, ref_recv_cb );\r
1954 \r
1955 \r
1956 }\r
1957 \r
1958 \r
1959 static void\r
1960 __recv_cb(\r
1961         IN              const   ib_cq_handle_t                          h_cq,\r
1962         IN                              void                                            *cq_context )\r
1963 {\r
1964         ipoib_port_t            *p_port;\r
1965         ib_api_status_t         status;\r
1966         ib_wc_t                         wc[MAX_RECV_WC], *p_free, *p_wc;\r
1967         int32_t                         pkt_cnt, recv_cnt = 0, shortage, discarded;\r
1968         cl_qlist_t                      done_list, bad_list;\r
1969         size_t                          i;\r
1970         ULONG                           recv_complete_flags = 0;\r
1971 \r
1972         PERF_DECLARE( RecvCompBundle );\r
1973         PERF_DECLARE( RecvCb );\r
1974         PERF_DECLARE( PollRecv );\r
1975         PERF_DECLARE( RepostRecv );\r
1976         PERF_DECLARE( FilterRecv );\r
1977         PERF_DECLARE( BuildPktArray );\r
1978         PERF_DECLARE( RecvNdisIndicate );\r
1979         PERF_DECLARE( RearmRecv );\r
1980         PERF_DECLARE( PutRecvList );\r
1981 \r
1982         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1983 \r
1984         cl_perf_clr( RecvCompBundle );\r
1985 \r
1986         cl_perf_start( RecvCb );\r
1987 //return ;\r
1988         UNUSED_PARAM( h_cq );\r
1989 \r
1990         NDIS_SET_SEND_COMPLETE_FLAG(recv_complete_flags, NDIS_RECEIVE_FLAGS_DISPATCH_LEVEL );\r
1991 \r
1992         p_port = (ipoib_port_t*)cq_context;\r
1993 \r
1994         cl_qlist_init( &done_list );\r
1995         cl_qlist_init( &bad_list );\r
1996 \r
1997         ipoib_port_ref( p_port, ref_recv_cb );\r
1998         for( i = 0; i < MAX_RECV_WC; i++ )\r
1999                 wc[i].p_next = &wc[i + 1];\r
2000         wc[MAX_RECV_WC - 1].p_next = NULL;\r
2001 \r
2002         /*\r
2003          * We'll be accessing the endpoint map so take a reference\r
2004          * on it to prevent modifications.\r
2005          */\r
2006         cl_obj_lock( &p_port->obj );\r
2007         cl_atomic_inc( &p_port->endpt_rdr );\r
2008         cl_obj_unlock( &p_port->obj );\r
2009 \r
2010         do\r
2011         {\r
2012                 /* If we get here, then the list of WCs is intact. */\r
2013                 p_free = wc;\r
2014 \r
2015                 cl_perf_start( PollRecv );\r
2016                 status = p_port->p_adapter->p_ifc->poll_cq(\r
2017                         p_port->ib_mgr.h_recv_cq, &p_free, &p_wc );\r
2018                 cl_perf_stop( &p_port->p_adapter->perf, PollRecv );\r
2019                 CL_ASSERT( status == IB_SUCCESS || status == IB_NOT_FOUND );\r
2020 \r
2021                 /* Look at the payload now and filter ARP and DHCP packets. */\r
2022                 cl_perf_start( FilterRecv );\r
2023                 recv_cnt += __recv_mgr_filter( p_port, p_wc, &done_list, &bad_list );\r
2024                 cl_perf_stop( &p_port->p_adapter->perf, FilterRecv );\r
2025 \r
2026         } while( (!p_free) && (recv_cnt < 128));\r
2027 \r
2028         /* We're done looking at the endpoint map, release the reference. */\r
2029         cl_atomic_dec( &p_port->endpt_rdr );\r
2030 \r
2031         cl_perf_log( &p_port->p_adapter->perf, RecvCompBundle, recv_cnt );\r
2032 \r
2033         cl_spinlock_acquire( &p_port->recv_lock );\r
2034 \r
2035         /* Update our posted depth. */\r
2036         p_port->recv_mgr.depth -= recv_cnt;\r
2037 \r
2038         /* Return any discarded receives to the pool */\r
2039         cl_perf_start( PutRecvList );\r
2040         __buf_mgr_put_recv_list( p_port, &bad_list );\r
2041         cl_perf_stop( &p_port->p_adapter->perf, PutRecvList );\r
2042 \r
2043         do\r
2044         {\r
2045                 int32_t cnt;\r
2046                 /* Repost ASAP so we don't starve the RQ. */\r
2047                 cl_perf_start( RepostRecv );\r
2048                 shortage = __recv_mgr_repost( p_port );\r
2049                 cl_perf_stop( &p_port->p_adapter->perf, RepostRecv );\r
2050 \r
2051                 cl_perf_start( BuildPktArray );\r
2052                 /* Notify NDIS of any and all possible receive buffers. */\r
2053                 pkt_cnt = __recv_mgr_build_pkt_array(\r
2054                         p_port, shortage, &done_list, &discarded );\r
2055                 cl_perf_stop( &p_port->p_adapter->perf, BuildPktArray );\r
2056 \r
2057                 /* Only indicate receives if we actually had any. */\r
2058                 if( discarded && shortage > 0 )\r
2059                 {\r
2060                         /* We may have thrown away packets, and have a shortage */\r
2061                         cl_perf_start( RepostRecv );\r
2062                         __recv_mgr_repost( p_port );\r
2063                         cl_perf_stop( &p_port->p_adapter->perf, RepostRecv );\r
2064                 }\r
2065 \r
2066                 if( !pkt_cnt )\r
2067                         break;\r
2068 \r
2069                 cl_spinlock_release( &p_port->recv_lock );\r
2070                 for( cnt = 0; cnt < pkt_cnt -1; cnt++)\r
2071                 {\r
2072                         NET_BUFFER_LIST_NEXT_NBL(p_port->recv_mgr.recv_pkt_array[cnt]) = \r
2073                                 p_port->recv_mgr.recv_pkt_array[cnt + 1];\r
2074                 }\r
2075                 cl_perf_start( RecvNdisIndicate );\r
2076 #ifndef NDIS_DEFAULT_PORT_NUMBER\r
2077 #define NDIS_DEFAULT_PORT_NUMBER 0\r
2078 #endif\r
2079                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2080                                         ("Indicate NDIS with  %d received NBs\n",\r
2081                                         pkt_cnt) );\r
2082                 NdisMIndicateReceiveNetBufferLists(\r
2083                         p_port->p_adapter->h_adapter,\r
2084                         p_port->recv_mgr.recv_pkt_array[0],\r
2085                         NDIS_DEFAULT_PORT_NUMBER,\r
2086                         pkt_cnt,\r
2087                         recv_complete_flags);\r
2088 \r
2089                 cl_perf_stop( &p_port->p_adapter->perf, RecvNdisIndicate );\r
2090 \r
2091                 /*\r
2092                  * Cap the number of receives to put back to what we just indicated\r
2093                  * with NDIS_STATUS_RESOURCES.\r
2094                  */\r
2095                 if( shortage > 0 )\r
2096                 {\r
2097                         if( pkt_cnt < shortage )\r
2098                                 shortage = pkt_cnt;\r
2099 \r
2100                         /* Return all but the last packet to the pool. */\r
2101                         cl_spinlock_acquire( &p_port->recv_lock );\r
2102                         while( shortage-- > 1 )\r
2103                         {\r
2104                                 __buf_mgr_put_recv( p_port,\r
2105                                         (ipoib_recv_desc_t *)IPOIB_RECV_FROM_PACKET( p_port->recv_mgr.recv_pkt_array[shortage] ),\r
2106                                         p_port->recv_mgr.recv_pkt_array[shortage] );\r
2107                         }\r
2108                         cl_spinlock_release( &p_port->recv_lock );\r
2109 \r
2110                         /*\r
2111                          * Return the last packet as if NDIS returned it, so that we repost\r
2112                          * and report any other pending receives.\r
2113                          */\r
2114                         ipoib_return_net_buffer_list( NULL, p_port->recv_mgr.recv_pkt_array[0],recv_complete_flags );\r
2115                 }\r
2116                 cl_spinlock_acquire( &p_port->recv_lock );\r
2117 \r
2118         } while( pkt_cnt );\r
2119         cl_spinlock_release( &p_port->recv_lock );\r
2120 \r
2121         if (p_free ) {\r
2122                 /*\r
2123                  * Rearm after filtering to prevent contention on the enpoint maps\r
2124                  * and eliminate the possibility of having a call to\r
2125                  * __endpt_mgr_insert find a duplicate.\r
2126                  */\r
2127                 cl_perf_start( RearmRecv );\r
2128                 status = p_port->p_adapter->p_ifc->rearm_cq(\r
2129                         p_port->ib_mgr.h_recv_cq, FALSE );\r
2130                 cl_perf_stop( &p_port->p_adapter->perf, RearmRecv );\r
2131                 CL_ASSERT( status == IB_SUCCESS );\r
2132 \r
2133                 ipoib_port_deref( p_port, ref_recv_cb );\r
2134         } else {\r
2135                 // Please note the reference is still up\r
2136                 KeInsertQueueDpc(&p_port->recv_dpc, NULL, NULL);\r
2137         }\r
2138 \r
2139         cl_perf_stop( &p_port->p_adapter->perf, RecvCb );\r
2140 \r
2141         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2142 }\r
2143 \r
2144 \r
2145 static void\r
2146 __recv_get_endpts(\r
2147         IN                              ipoib_port_t* const                     p_port,\r
2148         IN                              ipoib_recv_desc_t* const        p_desc,\r
2149         IN                              ib_wc_t* const                          p_wc,\r
2150                 OUT                     ipoib_endpt_t** const           pp_src,\r
2151                 OUT                     ipoib_endpt_t** const           pp_dst )\r
2152 {\r
2153         ib_api_status_t         status;\r
2154         mac_addr_t                      mac;\r
2155         PERF_DECLARE( GetEndptByGid );\r
2156         PERF_DECLARE( GetEndptByLid );\r
2157         PERF_DECLARE( EndptInsert );\r
2158 \r
2159         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2160 \r
2161         /* Setup our shortcut pointers based on whether GRH is valid. */\r
2162         if( p_wc->recv.ud.recv_opt & IB_RECV_OPT_GRH_VALID )\r
2163         {\r
2164                 /* Lookup the source endpoints based on GID. */\r
2165                 cl_perf_start( GetEndptByGid );\r
2166                 *pp_src =\r
2167 #if IPOIB_INLINE_RECV\r
2168                         __endpt_mgr_get_by_gid( p_port, &p_desc->buf.ib.grh.src_gid );\r
2169 #else   /* IPOIB_INLINE_RECV */\r
2170                         __endpt_mgr_get_by_gid( p_port, &p_desc->p_buf->ib.grh.src_gid );\r
2171 #endif  /* IPOIB_INLINE_RECV */\r
2172                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByGid );\r
2173 \r
2174                 /*\r
2175                  * Lookup the destination endpoint based on GID.\r
2176                  * This is used along with the packet filter to determine\r
2177                  * whether to report this to NDIS.\r
2178                  */\r
2179                 cl_perf_start( GetEndptByGid );\r
2180                 *pp_dst =\r
2181 #if IPOIB_INLINE_RECV\r
2182                         __endpt_mgr_get_by_gid( p_port, &p_desc->buf.ib.grh.dest_gid );\r
2183 #else   /* IPOIB_INLINE_RECV */\r
2184                         __endpt_mgr_get_by_gid( p_port, &p_desc->p_buf->ib.grh.dest_gid );\r
2185 #endif  /* IPOIB_INLINE_RECV */\r
2186                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByGid );\r
2187 \r
2188                 /*\r
2189                  * Create the source endpoint if it does not exist.  Note that we\r
2190                  * can only do this for globally routed traffic since we need the\r
2191                  * information from the GRH to generate the MAC.\r
2192                  */\r
2193                 if( !*pp_src )\r
2194                 {\r
2195                         status = ipoib_mac_from_guid(\r
2196 #if IPOIB_INLINE_RECV\r
2197                                 p_desc->buf.ib.grh.src_gid.unicast.interface_id, p_port->p_adapter->params.guid_mask, &mac );\r
2198 #else   /* IPOIB_INLINE_RECV */\r
2199                                 p_desc->p_buf->ib.grh.src_gid.unicast.interface_id, p_port->p_adapter->params.guid_mask, &mac );\r
2200 #endif  /* IPOIB_INLINE_RECV */\r
2201                         if( status != IB_SUCCESS )\r
2202                         {\r
2203                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2204                                         ("ipoib_mac_from_guid returned %s\n",\r
2205                                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2206                                 return;\r
2207                         }\r
2208 \r
2209                         /* Create the endpoint. */\r
2210 #if IPOIB_INLINE_RECV\r
2211                         *pp_src = ipoib_endpt_create( &p_desc->buf.ib.grh.src_gid,\r
2212 #else   /* IPOIB_INLINE_RECV */\r
2213                         *pp_src = ipoib_endpt_create( &p_desc->p_buf->ib.grh.src_gid,\r
2214 #endif  /* IPOIB_INLINE_RECV */\r
2215                                 p_wc->recv.ud.remote_lid, p_wc->recv.ud.remote_qp );\r
2216                         if( !*pp_src )\r
2217                         {\r
2218                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2219                                         ("ipoib_endpt_create failed\n") );\r
2220                                 return;\r
2221                         }\r
2222                         cl_perf_start( EndptInsert );\r
2223                         cl_obj_lock( &p_port->obj );\r
2224                         status = __endpt_mgr_insert( p_port, mac, *pp_src );\r
2225                         if( status != IB_SUCCESS )\r
2226                         {\r
2227                                 cl_obj_unlock( &p_port->obj );\r
2228                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2229                                         ("__endpt_mgr_insert returned %s\n",\r
2230                                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2231                                 *pp_src = NULL;\r
2232                                 return;\r
2233                         }\r
2234                         cl_obj_unlock( &p_port->obj );\r
2235                         cl_perf_stop( &p_port->p_adapter->perf, EndptInsert );\r
2236                 }\r
2237         }\r
2238         else\r
2239         {\r
2240                 /*\r
2241                  * Lookup the remote endpoint based on LID.  Note that only\r
2242                  * unicast traffic can be LID routed.\r
2243                  */\r
2244                 cl_perf_start( GetEndptByLid );\r
2245                 *pp_src = __endpt_mgr_get_by_lid( p_port, p_wc->recv.ud.remote_lid );\r
2246                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByLid );\r
2247                 *pp_dst = p_port->p_local_endpt;\r
2248                 CL_ASSERT( *pp_dst );\r
2249         }\r
2250 \r
2251         if( *pp_src && !ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) &&\r
2252                 (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
2253         {\r
2254                 /* Update the QPN for the endpoint. */\r
2255                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
2256                         ("Updating QPN for MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
2257                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
2258                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
2259                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5]) );\r
2260 //              (*pp_src)->qpn = p_wc->recv.ud.remote_qp;\r
2261         }\r
2262 \r
2263         if( *pp_src && *pp_dst )\r
2264         {\r
2265                 IPOIB_PRINT(TRACE_LEVEL_VERBOSE, IPOIB_DBG_RECV,\r
2266                         ("Recv:\n"\r
2267                         "\tsrc MAC: %02X-%02X-%02X-%02X-%02X-%02X\n"\r
2268                         "\tdst MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
2269                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
2270                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
2271                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5],\r
2272                         (*pp_dst )->mac.addr[0], (*pp_dst )->mac.addr[1],\r
2273                         (*pp_dst )->mac.addr[2], (*pp_dst )->mac.addr[3],\r
2274                         (*pp_dst )->mac.addr[4], (*pp_dst )->mac.addr[5]) );\r
2275         }\r
2276 \r
2277         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2278 }\r
2279 \r
2280 \r
2281 static int32_t\r
2282 __recv_mgr_filter(\r
2283         IN                              ipoib_port_t* const                     p_port,\r
2284         IN                              ib_wc_t* const                          p_done_wc_list,\r
2285                 OUT                     cl_qlist_t* const                       p_done_list,\r
2286                 OUT                     cl_qlist_t* const                       p_bad_list )\r
2287 {\r
2288         ipoib_recv_desc_t               *p_desc;\r
2289         ib_wc_t                                 *p_wc;\r
2290         ipoib_pkt_t                             *p_ipoib;\r
2291         eth_pkt_t                               *p_eth;\r
2292         ipoib_endpt_t                   *p_src, *p_dst;\r
2293         ib_api_status_t                 status;\r
2294         uint32_t                                len;\r
2295         int32_t                                 recv_cnt = 0;\r
2296         PERF_DECLARE( GetRecvEndpts );\r
2297         PERF_DECLARE( RecvGen );\r
2298         PERF_DECLARE( RecvTcp );\r
2299         PERF_DECLARE( RecvUdp );\r
2300         PERF_DECLARE( RecvDhcp );\r
2301         PERF_DECLARE( RecvArp );\r
2302 \r
2303         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2304 \r
2305         for( p_wc = p_done_wc_list; p_wc; p_wc = p_wc->p_next )\r
2306         {\r
2307                 CL_ASSERT( p_wc->status != IB_WCS_SUCCESS || p_wc->wc_type == IB_WC_RECV );\r
2308                 p_desc = (ipoib_recv_desc_t*)(uintn_t)p_wc->wr_id;\r
2309                 recv_cnt++;\r
2310 \r
2311                 if( p_wc->status != IB_WCS_SUCCESS )\r
2312                 {\r
2313                         if( p_wc->status != IB_WCS_WR_FLUSHED_ERR )\r
2314                         {\r
2315                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2316                                         ("Failed completion %s  (vendor specific %#x)\n",\r
2317                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status ),\r
2318                                         (int)p_wc->vendor_specific) );\r
2319                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0, 0 );\r
2320                         }\r
2321                         else\r
2322                         {\r
2323                                 IPOIB_PRINT(TRACE_LEVEL_VERBOSE, IPOIB_DBG_RECV,\r
2324                                         ("Flushed completion %s\n",\r
2325                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status )) );\r
2326                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_DROPPED, 0, 0 );\r
2327                         }\r
2328                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2329                         /* Dereference the port object on behalf of the failed receive. */\r
2330                         ipoib_port_deref( p_port, ref_failed_recv_wc );\r
2331                         continue;\r
2332                 }\r
2333 \r
2334                 len = p_wc->length - sizeof(ib_grh_t);\r
2335 \r
2336                 if( len < sizeof(ipoib_hdr_t) )\r
2337                 {\r
2338                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2339                                 ("Received ETH packet < min size\n") );\r
2340                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0, 0 );\r
2341                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2342                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
2343                         continue;\r
2344                 }\r
2345 \r
2346                 if((len - sizeof(ipoib_hdr_t)) > p_port->p_adapter->params.payload_mtu)\r
2347                 {\r
2348                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2349                                 ("Received ETH packet len %d > payload MTU (%d)\n",\r
2350                                 (len - sizeof(ipoib_hdr_t)),\r
2351                                 p_port->p_adapter->params.payload_mtu) );\r
2352                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0, 0 );\r
2353                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2354                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
2355                         continue;\r
2356                         \r
2357                 }\r
2358                 /* Successful completion.  Get the receive information. */\r
2359                 p_desc->ndis_csum.Value = ( ( p_wc->recv.ud.recv_opt & IB_RECV_OPT_CSUM_MASK ) >> 8 );\r
2360                 p_desc->len = len + 14 - 4 ;\r
2361                 cl_perf_start( GetRecvEndpts );\r
2362                 __recv_get_endpts( p_port, p_desc, p_wc, &p_src, &p_dst );\r
2363                 cl_perf_stop( &p_port->p_adapter->perf, GetRecvEndpts );\r
2364 \r
2365 #if IPOIB_INLINE_RECV\r
2366                 p_ipoib = &p_desc->buf.ib.pkt;\r
2367                 p_eth = &p_desc->buf.eth.pkt;\r
2368 #else   /* IPOIB_INLINE_RECV */\r
2369                 p_ipoib = &p_desc->p_buf->ib.pkt;\r
2370                 p_eth = &p_desc->p_buf->eth.pkt;\r
2371 #endif  /*IPOIB_INLINE_RECV */\r
2372 \r
2373                 if( p_src )\r
2374                 {\r
2375                         /* Don't report loopback traffic - we requested SW loopback. */\r
2376                         if( !cl_memcmp( &p_port->p_adapter->params.conf_mac,\r
2377                                 &p_src->mac, sizeof(p_port->p_adapter->params.conf_mac) ) )\r
2378                         {\r
2379                                 /*\r
2380                                  * "This is not the packet you're looking for" - don't update\r
2381                                  * receive statistics, the packet never happened.\r
2382                                  */\r
2383                                 cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2384                                 /* Dereference the port object on behalf of the failed recv. */\r
2385                                 ipoib_port_deref( p_port, ref_recv_loopback );\r
2386                                 continue;\r
2387                         }\r
2388                 }\r
2389 \r
2390                 switch( p_ipoib->hdr.type )\r
2391                 {\r
2392                 case ETH_PROT_TYPE_IP:\r
2393                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t)) )\r
2394                         {\r
2395                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2396                                         ("Received IP packet < min size\n") );\r
2397                                 status = IB_INVALID_SETTING;\r
2398                                 break;\r
2399                         }\r
2400 \r
2401                         if( p_ipoib->type.ip.hdr.offset ||\r
2402                                 p_ipoib->type.ip.hdr.prot != IP_PROT_UDP )\r
2403                         {\r
2404                                 /* Unfiltered.  Setup the ethernet header and report. */\r
2405                                 cl_perf_start( RecvTcp );\r
2406                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2407                                 cl_perf_stop( &p_port->p_adapter->perf, RecvTcp );\r
2408                                 break;\r
2409                         }\r
2410 \r
2411                         /* First packet of a UDP transfer. */\r
2412                         if( len <\r
2413                                 (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) + sizeof(udp_hdr_t)) )\r
2414                         {\r
2415                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2416                                         ("Received UDP packet < min size\n") );\r
2417                                 status = IB_INVALID_SETTING;\r
2418                                 break;\r
2419                         }\r
2420 \r
2421                         /* Check if DHCP conversion is required. */\r
2422                         if( (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_SERVER &&\r
2423                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_CLIENT) ||\r
2424                                 (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_CLIENT &&\r
2425                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_SERVER) )\r
2426                         {\r
2427                                 if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) +\r
2428                                         sizeof(udp_hdr_t) + DHCP_MIN_SIZE) )\r
2429                                 {\r
2430                                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2431                                                 ("Received DHCP < min size\n") );\r
2432                                         status = IB_INVALID_SETTING;\r
2433                                         break;\r
2434                                 }\r
2435                                 if ((p_ipoib->type.ip.hdr.ver_hl & 0x0f) != 5 ) {\r
2436                                         // If there are IP options in this message, we are in trouble in any case\r
2437                                         status = IB_INVALID_SETTING;\r
2438                                         break;                                  \r
2439                                 }\r
2440                                 /* UDP packet with BOOTP ports in src/dst port numbers. */\r
2441                                 cl_perf_start( RecvDhcp );\r
2442                                 status = __recv_dhcp( p_port, p_ipoib, p_eth, p_src, p_dst );\r
2443                                 cl_perf_stop( &p_port->p_adapter->perf, RecvDhcp );\r
2444                         }\r
2445                         else\r
2446                         {\r
2447                                 /* Unfiltered.  Setup the ethernet header and report. */\r
2448                                 cl_perf_start( RecvUdp );\r
2449                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2450                                 cl_perf_stop( &p_port->p_adapter->perf, RecvUdp );\r
2451                         }\r
2452                         break;\r
2453 \r
2454                 case ETH_PROT_TYPE_ARP:\r
2455                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ipoib_arp_pkt_t)) )\r
2456                         {\r
2457                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2458                                         ("Received ARP < min size\n") );\r
2459                                 status = IB_INVALID_SETTING;\r
2460                                 break;\r
2461                         }\r
2462                         cl_perf_start( RecvArp );\r
2463                         status = __recv_arp( p_port, p_wc, p_ipoib, p_eth, &p_src, p_dst );\r
2464                         cl_perf_stop( &p_port->p_adapter->perf, RecvArp );\r
2465                         len = sizeof(ipoib_hdr_t) + sizeof(arp_pkt_t);\r
2466                         break;\r
2467 \r
2468                 default:\r
2469                         /* Unfiltered.  Setup the ethernet header and report. */\r
2470                         cl_perf_start( RecvGen );\r
2471                         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2472                         cl_perf_stop( &p_port->p_adapter->perf, RecvGen );\r
2473                 }\r
2474 \r
2475                 if( status != IB_SUCCESS )\r
2476                 {\r
2477                         /* Update stats. */\r
2478                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0, 0 );\r
2479                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2480                         /* Dereference the port object on behalf of the failed receive. */\r
2481                         ipoib_port_deref( p_port, ref_recv_filter );\r
2482                 }\r
2483                 else\r
2484                 {\r
2485                         ip_stat_sel_t               ip_stat;\r
2486                         p_desc->len =\r
2487                                 len + sizeof(eth_hdr_t) - sizeof(ipoib_hdr_t);\r
2488                         if( p_dst->h_mcast)\r
2489                         {\r
2490                                 if( p_dst->dgid.multicast.raw_group_id[10] == 0xFF &&\r
2491                                         p_dst->dgid.multicast.raw_group_id[11] == 0xFF &&\r
2492                                         p_dst->dgid.multicast.raw_group_id[12] == 0xFF &&\r
2493                                         p_dst->dgid.multicast.raw_group_id[13] == 0xFF )\r
2494                                 {\r
2495                                         p_desc->type = PKT_TYPE_BCAST;\r
2496                                         ip_stat = IP_STAT_BCAST_BYTES;\r
2497                                 }\r
2498                                 else\r
2499                                 {\r
2500                                         p_desc->type = PKT_TYPE_MCAST;\r
2501                                         ip_stat = IP_STAT_MCAST_BYTES;\r
2502                                 }\r
2503                         }\r
2504                         else\r
2505                         {\r
2506                                 p_desc->type = PKT_TYPE_UCAST;\r
2507                                 ip_stat = IP_STAT_UCAST_BYTES;\r
2508                                 \r
2509                         }\r
2510                         cl_qlist_insert_tail( p_done_list, &p_desc->item.list_item );\r
2511                         ipoib_inc_recv_stat( p_port->p_adapter, ip_stat, len, 1 );  \r
2512                 }\r
2513         }\r
2514 \r
2515         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2516         return recv_cnt;\r
2517 }\r
2518 \r
2519 \r
2520 static ib_api_status_t\r
2521 __recv_gen(\r
2522         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2523                 OUT                     eth_pkt_t* const                        p_eth,\r
2524         IN                              ipoib_endpt_t* const            p_src,\r
2525         IN                              ipoib_endpt_t* const            p_dst )\r
2526 {\r
2527         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2528 \r
2529         if( !p_src || !p_dst )\r
2530         {\r
2531                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2532                         ("Received packet with no matching endpoints.\n") );\r
2533                 return IB_NOT_DONE;\r
2534         }\r
2535 \r
2536         /*\r
2537          * Fill in the ethernet header.  Note that doing so will overwrite\r
2538          * the IPoIB header, so start by moving the information from the IPoIB\r
2539          * header.\r
2540          */\r
2541         p_eth->hdr.type = p_ipoib->hdr.type;\r
2542         p_eth->hdr.src = p_src->mac;\r
2543         p_eth->hdr.dst = p_dst->mac;\r
2544 \r
2545         if ( p_eth->hdr.dst.addr[0] == 1 && \r
2546                  p_eth->hdr.type == ETH_PROT_TYPE_IP &&\r
2547                  p_eth->hdr.dst.addr[2] == 0x5E)  \r
2548         {\r
2549                 p_eth->hdr.dst.addr[1] = 0;\r
2550                 p_eth->hdr.dst.addr[3] = p_eth->hdr.dst.addr[3] & 0x7f;\r
2551         }\r
2552         if (p_dst->h_mcast)\r
2553                 p_dst->is_in_use = TRUE;\r
2554 \r
2555         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2556         return IB_SUCCESS;\r
2557 }\r
2558 \r
2559 \r
2560 static ib_api_status_t\r
2561 __recv_dhcp(\r
2562         IN                              ipoib_port_t* const                     p_port,\r
2563         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2564                 OUT                     eth_pkt_t* const                        p_eth,\r
2565         IN                              ipoib_endpt_t* const            p_src,\r
2566         IN                              ipoib_endpt_t* const            p_dst )\r
2567 {\r
2568         ib_api_status_t         status;\r
2569         dhcp_pkt_t                      *p_dhcp;\r
2570         uint8_t                         *p_option;\r
2571         uint8_t                         *p_cid = NULL;\r
2572         ib_gid_t                        gid;\r
2573         uint8_t                         msg = 0;\r
2574 \r
2575         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2576 \r
2577         UNUSED_PARAM( p_port );\r
2578 \r
2579         /* Create the ethernet header. */\r
2580         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2581         if( status != IB_SUCCESS )\r
2582         {\r
2583                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2584                         ("__recv_gen returned %s.\n", \r
2585                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2586                 return status;\r
2587         }\r
2588 \r
2589         /* Fixup the payload. */\r
2590         p_dhcp = &p_eth->type.ip.prot.udp.dhcp;\r
2591         if( p_dhcp->op != DHCP_REQUEST && p_dhcp->op != DHCP_REPLY )\r
2592         {\r
2593                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2594                         ("Invalid DHCP op code.\n") );\r
2595                 return IB_INVALID_SETTING;\r
2596         }\r
2597 \r
2598         /*\r
2599          * Find the client identifier option, making sure to skip\r
2600          * the "magic cookie".\r
2601          */\r
2602         p_option = &p_dhcp->options[0];\r
2603         if ( *(uint32_t *)p_option != DHCP_COOKIE )\r
2604         {\r
2605                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2606                         ("DHCP cookie corrupted.\n") );\r
2607                 return IB_INVALID_PARAMETER;\r
2608         }\r
2609 \r
2610         p_option = &p_dhcp->options[4];\r
2611         while( *p_option != DHCP_OPT_END && p_option < &p_dhcp->options[312] )\r
2612         {\r
2613                 switch( *p_option )\r
2614                 {\r
2615                 case DHCP_OPT_PAD:\r
2616                         p_option++;\r
2617                         break;\r
2618 \r
2619                 case DHCP_OPT_MSG:\r
2620                         msg = p_option[2];\r
2621                         p_option += 3;\r
2622                         break;\r
2623 \r
2624                 case DHCP_OPT_CLIENT_ID:\r
2625                         p_cid = p_option;\r
2626                         /* Fall through. */\r
2627 \r
2628                 default:\r
2629                         /*\r
2630                          * All other options have a length byte following the option code.\r
2631                          * Offset by the length to get to the next option.\r
2632                          */\r
2633                         p_option += (p_option[1] + 2);\r
2634                 }\r
2635         }\r
2636 \r
2637         switch( msg )\r
2638         {\r
2639         /* message from client */\r
2640         case DHCPDISCOVER:\r
2641         case DHCPREQUEST:\r
2642         case DHCPDECLINE:\r
2643         case DHCPRELEASE:\r
2644         case DHCPINFORM:\r
2645                 if( !p_cid )\r
2646                 {\r
2647                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2648                                 ("Failed to find required Client-identifier option.\n") );\r
2649                         return IB_INVALID_SETTING;\r
2650                 }\r
2651                 if( p_dhcp->htype != DHCP_HW_TYPE_IB )\r
2652                 {\r
2653                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2654                                 ("Invalid hardware address type.\n") );\r
2655                         return IB_INVALID_SETTING;\r
2656                 }\r
2657                 break;\r
2658         /* message from DHCP server */\r
2659         case DHCPOFFER:\r
2660         case DHCPACK:\r
2661         case DHCPNAK:\r
2662                 break;\r
2663 \r
2664         default:\r
2665                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2666                         ("Invalide message type.\n") );\r
2667                 return IB_INVALID_PARAMETER;\r
2668         }\r
2669         p_eth->type.ip.prot.udp.hdr.chksum = 0;\r
2670         p_dhcp->htype = DHCP_HW_TYPE_ETH;\r
2671         p_dhcp->hlen = HW_ADDR_LEN;\r
2672 \r
2673         if( p_cid ) /* from client */\r
2674         {\r
2675                 /* Validate that the length and type of the option is as required. */\r
2676                 if( p_cid[1] != 21 )\r
2677                 {\r
2678                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2679                                 ("Client-identifier length not 21 as required.\n") );\r
2680                         return IB_INVALID_SETTING;\r
2681                 }\r
2682                 if( p_cid[2] != DHCP_HW_TYPE_IB )\r
2683                 {\r
2684                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2685                                 ("Client-identifier type is wrong.\n") );\r
2686                         return IB_INVALID_SETTING;\r
2687                 }\r
2688                 /*\r
2689                  * Copy the GID value from the option so that we can make aligned\r
2690                  * accesses to the contents.\r
2691                  * Recover CID to standard type.\r
2692                  */\r
2693                 cl_memcpy( &gid, &p_cid[7], sizeof(ib_gid_t) );\r
2694                 p_cid[1] =  HW_ADDR_LEN +1;// CID length \r
2695                 p_cid[2] =  DHCP_HW_TYPE_ETH;// CID type \r
2696                 status = ipoib_mac_from_guid( gid.unicast.interface_id, p_port->p_adapter->params.guid_mask, (mac_addr_t*)&p_cid[3] );\r
2697                 if (status == IB_INVALID_GUID_MASK)\r
2698                 {\r
2699                         IPOIB_PRINT( TRACE_LEVEL_WARNING, IPOIB_DBG_ERROR,\r
2700                                 ("Invalid GUID mask received, rejecting it") );\r
2701                         ipoib_create_log(p_port->p_adapter->h_adapter, GUID_MASK_LOG_INDEX, EVENT_IPOIB_WRONG_PARAMETER_WRN);\r
2702                         status = IB_SUCCESS;\r
2703                 }\r
2704                 p_cid[HW_ADDR_LEN + 3] = DHCP_OPT_END; //terminate tag\r
2705         }\r
2706         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2707         return status;\r
2708 }\r
2709 \r
2710 \r
2711 static ib_api_status_t\r
2712 __recv_arp(\r
2713         IN                              ipoib_port_t* const                     p_port,\r
2714         IN                              ib_wc_t* const                          p_wc,\r
2715         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2716                 OUT                     eth_pkt_t* const                        p_eth,\r
2717         IN                              ipoib_endpt_t** const           pp_src,\r
2718         IN                              ipoib_endpt_t* const            p_dst )\r
2719 {\r
2720         ib_api_status_t                 status;\r
2721         arp_pkt_t                               *p_arp;\r
2722         const ipoib_arp_pkt_t   *p_ib_arp;\r
2723         ib_gid_t                                gid;\r
2724         mac_addr_t                              mac;\r
2725         ipoib_hw_addr_t                 null_hw = {0};\r
2726         uint8_t                                 cm_capable = 0;\r
2727 \r
2728         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2729 \r
2730         if( !p_dst )\r
2731         {\r
2732                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2733                         ("Unknown destination endpoint\n") );\r
2734                 return IB_INVALID_SETTING;\r
2735         }\r
2736 \r
2737         p_ib_arp = &p_ipoib->type.arp;\r
2738         p_arp = &p_eth->type.arp;\r
2739 \r
2740         if( p_ib_arp->hw_type != ARP_HW_TYPE_IB )\r
2741         {\r
2742                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2743                         ("ARP hardware type is not IB\n") );\r
2744                 return IB_INVALID_SETTING;\r
2745         }\r
2746 \r
2747         if( p_ib_arp->hw_size != sizeof(ipoib_hw_addr_t) )\r
2748         {\r
2749                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2750                         ("ARP hardware address size is not sizeof(ipoib_hw_addr_t)\n") );\r
2751                 return IB_INVALID_SETTING;\r
2752         }\r
2753 \r
2754         if( p_ib_arp->prot_type != ETH_PROT_TYPE_IP )\r
2755         {\r
2756                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2757                         ("ARP protocal type not IP\n") );\r
2758                 return IB_INVALID_SETTING;\r
2759         }\r
2760 \r
2761         cm_capable = ipoib_addr_get_flags( &p_ib_arp->src_hw );\r
2762 \r
2763         /*\r
2764          * If we don't have a source, lookup the endpoint specified in the payload.\r
2765          */\r
2766         if( !*pp_src )\r
2767                 *pp_src = __endpt_mgr_get_by_gid( p_port, &p_ib_arp->src_hw.gid );\r
2768 \r
2769         /*\r
2770          * If the endpoint exists for the GID, make sure\r
2771          * the dlid and qpn match the arp.\r
2772          */\r
2773         if( *pp_src )\r
2774         {\r
2775                 if( cl_memcmp( &(*pp_src)->dgid, &p_ib_arp->src_hw.gid,\r
2776                         sizeof(ib_gid_t) ) )\r
2777                 {\r
2778                         /*\r
2779                          * GIDs for the endpoint are different.  The ARP must\r
2780                          * have been proxied.  Dereference it.\r
2781                          */\r
2782                         *pp_src = NULL;\r
2783                 }\r
2784                 else if( (*pp_src)->dlid &&\r
2785                         (*pp_src)->dlid != p_wc->recv.ud.remote_lid )\r
2786                 {\r
2787                         /* Out of date!  Destroy the endpoint and replace it. */\r
2788                         __endpt_mgr_remove( p_port, *pp_src );\r
2789                         *pp_src = NULL;\r
2790                 }\r
2791                 else if ( ! ((*pp_src)->dlid)) {\r
2792                         /* Out of date!  Destroy the endpoint and replace it. */\r
2793                         __endpt_mgr_remove( p_port, *pp_src );\r
2794                         *pp_src = NULL;\r
2795                 }\r
2796                 else if( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) )\r
2797                 {\r
2798                         if( (*pp_src)->qpn != ipoib_addr_get_qpn( &p_ib_arp->src_hw ) &&\r
2799                                  p_wc->recv.ud.remote_qp !=     ipoib_addr_get_qpn( &p_ib_arp->src_hw ) )\r
2800                         {\r
2801                                 /* Out of date!  Destroy the endpoint and replace it. */\r
2802                                 __endpt_mgr_remove( p_port, *pp_src );\r
2803                                 *pp_src = NULL;\r
2804                         }\r
2805                 }\r
2806                 else if( (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
2807                 {\r
2808                         /* Out of date!  Destroy the endpoint and replace it. */\r
2809                         __endpt_mgr_remove( p_port, *pp_src );\r
2810                         *pp_src = NULL;\r
2811                 }\r
2812         }\r
2813 \r
2814         /* Do we need to create an endpoint for this GID? */\r
2815         if( !*pp_src )\r
2816         {\r
2817                 /* Copy the src GID to allow aligned access */\r
2818                 cl_memcpy( &gid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) );\r
2819                 status = ipoib_mac_from_guid( gid.unicast.interface_id, p_port->p_adapter->params.guid_mask, &mac );\r
2820                 if (status == IB_INVALID_GUID_MASK)\r
2821                 {\r
2822                         IPOIB_PRINT( TRACE_LEVEL_WARNING, IPOIB_DBG_ERROR,\r
2823                                 ("Invalid GUID mask received, rejecting it") );\r
2824                         ipoib_create_log(p_port->p_adapter->h_adapter, GUID_MASK_LOG_INDEX, EVENT_IPOIB_WRONG_PARAMETER_WRN);\r
2825                 }\r
2826                 else if( status != IB_SUCCESS )\r
2827                 {\r
2828                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2829                                 ("ipoib_mac_from_guid returned %s\n",\r
2830                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2831                         return status;\r
2832                 }\r
2833                 /*\r
2834                  * Create the endpoint.\r
2835                  */\r
2836                 *pp_src = ipoib_endpt_create( &p_ib_arp->src_hw.gid,\r
2837                         p_wc->recv.ud.remote_lid, ipoib_addr_get_qpn( &p_ib_arp->src_hw ) );\r
2838 \r
2839                 if( !*pp_src )\r
2840                 {\r
2841                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2842                                 ("ipoib_endpt_create failed\n") );\r
2843                         return status;\r
2844                 }\r
2845 \r
2846                 cl_obj_lock( &p_port->obj );\r
2847                 status = __endpt_mgr_insert( p_port, mac, *pp_src );\r
2848                 if( status != IB_SUCCESS )\r
2849                 {\r
2850                         cl_obj_unlock( &p_port->obj );\r
2851                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2852                                 ("__endpt_mgr_insert return %s \n",\r
2853                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2854                         return status;\r
2855                 }\r
2856 \r
2857                 cl_obj_unlock( &p_port->obj );\r
2858         }\r
2859 \r
2860         (*pp_src)->cm_flag = cm_capable;\r
2861 \r
2862         CL_ASSERT( !cl_memcmp(\r
2863                 &(*pp_src)->dgid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) ) );\r
2864         CL_ASSERT( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) ||\r
2865                 (*pp_src)->qpn == ipoib_addr_get_qpn( &p_ib_arp->src_hw ) );\r
2866 #if 0\r
2867         if( p_port->p_adapter->params.cm_enabled &&\r
2868                 p_ib_arp->op == ARP_OP_REQ &&\r
2869                 cm_capable == IPOIB_CM_FLAG_RC )\r
2870         {\r
2871                 /* if we've got ARP request and RC flag is set, \r
2872                 save SID for connect REQ to be sent in ARP reply\r
2873                 when requestor's path get resolved */\r
2874                 if( endpt_cm_get_state( (*pp_src) ) == IPOIB_CM_DISCONNECTED )\r
2875                 {\r
2876                         (*pp_src)->cm_flag = cm_capable;\r
2877                         ipoib_addr_set_sid( \r
2878                                 &(*pp_src)->conn.service_id,\r
2879                                 ipoib_addr_get_qpn( &p_ib_arp->src_hw ) );\r
2880                 }\r
2881         }\r
2882 #endif\r
2883 #if 0 //DBG\r
2884         if( p_port->p_adapter->params.cm_enabled )\r
2885         {\r
2886                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_INIT,\r
2887                         (" ARP %s from ENDPT[%p] state %d CM cap: %d QPN: %#x MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",\r
2888                         ((p_ib_arp->op == ARP_OP_REQ )? "REQUEST" : "REPLY"),\r
2889                         *pp_src, endpt_cm_get_state( *pp_src ), \r
2890                         ((cm_capable == IPOIB_CM_FLAG_RC)? 1: 0),\r
2891                         cl_ntoh32( ipoib_addr_get_qpn( &p_ib_arp->src_hw ) ),\r
2892                         (*pp_src)->mac.addr[0], (*pp_src)->mac.addr[1],\r
2893                         (*pp_src)->mac.addr[2], (*pp_src)->mac.addr[3],\r
2894                         (*pp_src)->mac.addr[4], (*pp_src)->mac.addr[5] ));\r
2895         }\r
2896 #endif\r
2897 \r
2898         /* Now swizzle the data. */\r
2899         p_arp->hw_type = ARP_HW_TYPE_ETH;\r
2900         p_arp->hw_size = sizeof(mac_addr_t);\r
2901         p_arp->src_hw = (*pp_src)->mac;\r
2902         p_arp->src_ip = p_ib_arp->src_ip;\r
2903 \r
2904         if( cl_memcmp( &p_ib_arp->dst_hw, &null_hw, sizeof(ipoib_hw_addr_t) ) )\r
2905      &nb