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