1f0d4275779ca3a6d7f89d90755716626efc017f
[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         \r
943         qp_create.h_sq_cq = p_port->ib_mgr.h_send_cq;\r
944         qp_create.sq_signaled = TRUE;\r
945         status = p_port->p_adapter->p_ifc->create_qp(\r
946                 p_port->ib_mgr.h_pd, &qp_create, p_port,\r
947                 __qp_event, &p_port->ib_mgr.h_qp );\r
948         if( status != IB_SUCCESS )\r
949         {\r
950                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
951                         EVENT_IPOIB_CREATE_QP, 1, status );\r
952                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
953                         ("ib_create_qp returned %s\n", \r
954                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
955                 return status;\r
956         }\r
957         /* Query the QP so we can get our QPN. */\r
958         status = p_port->p_adapter->p_ifc->query_qp(\r
959                 p_port->ib_mgr.h_qp, &qp_attr );\r
960         if( status != IB_SUCCESS )\r
961         {\r
962                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
963                         EVENT_IPOIB_QUERY_QP, 1, status );\r
964                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
965                         ("ib_query_qp returned %s\n", \r
966                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
967                 return status;\r
968         }\r
969         p_port->ib_mgr.qpn = qp_attr.num;\r
970 \r
971         /* Register all of physical memory */\r
972         phys_create.length = MEM_REG_SIZE;\r
973         phys_create.num_ranges = 1;\r
974         phys_create.range_array = &phys_range;\r
975         phys_create.buf_offset = 0;\r
976         phys_create.hca_page_size = PAGE_SIZE;\r
977         phys_create.access_ctrl = IB_AC_LOCAL_WRITE;\r
978         phys_range.base_addr = 0;\r
979         phys_range.size = MEM_REG_SIZE;\r
980         vaddr = 0;\r
981         status = p_port->p_adapter->p_ifc->reg_phys(\r
982                 p_port->ib_mgr.h_pd, &phys_create, &vaddr,\r
983                 &p_port->ib_mgr.lkey, &rkey, &p_port->ib_mgr.h_mr );\r
984         if( status != IB_SUCCESS )\r
985         {\r
986                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
987                         EVENT_IPOIB_REG_PHYS, 1, status );\r
988                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
989                         ("ib_reg_phys returned %s\n", \r
990                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
991                 return status;\r
992         }\r
993 \r
994         IPOIB_EXIT( IPOIB_DBG_INIT );\r
995         return IB_SUCCESS;\r
996 }\r
997 \r
998 \r
999 static void\r
1000 __ib_mgr_destroy(\r
1001         IN                              ipoib_port_t* const                     p_port )\r
1002 {\r
1003         ib_api_status_t status;\r
1004 \r
1005         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1006 \r
1007         if( p_port->ib_mgr.h_ca )\r
1008         {\r
1009                 status =\r
1010                         p_port->p_adapter->p_ifc->close_ca( p_port->ib_mgr.h_ca, NULL );\r
1011                 CL_ASSERT( status == IB_SUCCESS );\r
1012                 p_port->ib_mgr.h_ca = NULL;\r
1013         }\r
1014 \r
1015         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1016 }\r
1017 \r
1018 \r
1019 \r
1020 /******************************************************************************\r
1021 *\r
1022 * Buffer manager implementation.\r
1023 *\r
1024 ******************************************************************************/\r
1025 static void\r
1026 __buf_mgr_construct(\r
1027         IN                              ipoib_port_t* const                     p_port )\r
1028 {\r
1029         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1030 \r
1031         cl_qpool_construct( &p_port->buf_mgr.recv_pool );\r
1032 \r
1033         p_port->buf_mgr.h_packet_pool = NULL;\r
1034         p_port->buf_mgr.h_buffer_pool = NULL;\r
1035 \r
1036         ExInitializeNPagedLookasideList( &p_port->buf_mgr.send_buf_list,\r
1037                 NULL, NULL, 0, MAX_XFER_BLOCK_SIZE, 'bipi', 0 );\r
1038 \r
1039         p_port->buf_mgr.h_send_pkt_pool = NULL;\r
1040         p_port->buf_mgr.h_send_buf_pool = NULL;\r
1041 \r
1042         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1043 }\r
1044 \r
1045 \r
1046 static ib_api_status_t\r
1047 __buf_mgr_init(\r
1048         IN                              ipoib_port_t* const                     p_port )\r
1049 {\r
1050         cl_status_t             cl_status;\r
1051         NDIS_STATUS             ndis_status;\r
1052         ipoib_params_t  *p_params;\r
1053 \r
1054         IPOIB_ENTER(IPOIB_DBG_INIT );\r
1055 \r
1056         CL_ASSERT( p_port );\r
1057         CL_ASSERT( p_port->p_adapter );\r
1058 \r
1059         p_params = &p_port->p_adapter->params;\r
1060 \r
1061         /* Allocate the receive descriptor pool */\r
1062         cl_status = cl_qpool_init( &p_port->buf_mgr.recv_pool,\r
1063                 p_params->rq_depth * p_params->recv_pool_ratio,\r
1064 #if IPOIB_INLINE_RECV\r
1065                 0, 0, sizeof(ipoib_recv_desc_t), __recv_ctor, NULL, p_port );\r
1066 #else   /* IPOIB_INLINE_RECV */\r
1067                 0, 0, sizeof(ipoib_recv_desc_t), __recv_ctor, __recv_dtor, p_port );\r
1068 #endif  /* IPOIB_INLINE_RECV */\r
1069         if( cl_status != CL_SUCCESS )\r
1070         {\r
1071                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1072                         EVENT_IPOIB_RECV_POOL, 1, cl_status );\r
1073                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1074                         ("cl_qpool_init for recvs returned %#x\n",\r
1075                         cl_status) );\r
1076                 return IB_INSUFFICIENT_MEMORY;\r
1077         }\r
1078 \r
1079         /* Allocate the NDIS buffer and packet pools for receive indication. */\r
1080         NdisAllocatePacketPool( &ndis_status, &p_port->buf_mgr.h_packet_pool,\r
1081                 p_params->rq_depth, PROTOCOL_RESERVED_SIZE_IN_PACKET );\r
1082         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1083         {\r
1084                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1085                         EVENT_IPOIB_RECV_PKT_POOL, 1, ndis_status );\r
1086                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1087                         ("NdisAllocatePacketPool returned %08X\n", ndis_status) );\r
1088                 return IB_INSUFFICIENT_RESOURCES;\r
1089         }\r
1090 \r
1091         NdisAllocateBufferPool( &ndis_status, &p_port->buf_mgr.h_buffer_pool,\r
1092                 p_params->rq_depth );\r
1093         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1094         {\r
1095                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1096                         EVENT_IPOIB_RECV_BUF_POOL, 1, ndis_status );\r
1097                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1098                         ("NdisAllocateBufferPool returned %08X\n", ndis_status) );\r
1099                 return IB_INSUFFICIENT_RESOURCES;\r
1100         }\r
1101 \r
1102         /* Allocate the NDIS buffer and packet pools for send formatting. */\r
1103         NdisAllocatePacketPool( &ndis_status, &p_port->buf_mgr.h_send_pkt_pool,\r
1104                 1, PROTOCOL_RESERVED_SIZE_IN_PACKET );\r
1105         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1106         {\r
1107                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1108                         EVENT_IPOIB_SEND_PKT_POOL, 1, ndis_status );\r
1109                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1110                         ("NdisAllocatePacketPool returned %08X\n", ndis_status) );\r
1111                 return IB_INSUFFICIENT_RESOURCES;\r
1112         }\r
1113 \r
1114         NdisAllocateBufferPool( &ndis_status,\r
1115                 &p_port->buf_mgr.h_send_buf_pool, 1 );\r
1116         if( ndis_status != NDIS_STATUS_SUCCESS )\r
1117         {\r
1118                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1119                         EVENT_IPOIB_SEND_BUF_POOL, 1, ndis_status );\r
1120                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1121                         ("NdisAllocateBufferPool returned %08X\n", ndis_status) );\r
1122                 return IB_INSUFFICIENT_RESOURCES;\r
1123         }\r
1124 \r
1125         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1126         return IB_SUCCESS;\r
1127 }\r
1128 \r
1129 \r
1130 static void\r
1131 __buf_mgr_destroy(\r
1132         IN                              ipoib_port_t* const                     p_port )\r
1133 {\r
1134         IPOIB_ENTER(IPOIB_DBG_INIT );\r
1135 \r
1136         CL_ASSERT( p_port );\r
1137 \r
1138         /* Destroy the send packet and buffer pools. */\r
1139         if( p_port->buf_mgr.h_send_buf_pool )\r
1140                 NdisFreeBufferPool( p_port->buf_mgr.h_send_buf_pool );\r
1141         if( p_port->buf_mgr.h_send_pkt_pool )\r
1142                 NdisFreePacketPool( p_port->buf_mgr.h_send_pkt_pool );\r
1143 \r
1144         /* Destroy the receive packet and buffer pools. */\r
1145         if( p_port->buf_mgr.h_buffer_pool )\r
1146                 NdisFreeBufferPool( p_port->buf_mgr.h_buffer_pool );\r
1147         if( p_port->buf_mgr.h_packet_pool )\r
1148                 NdisFreePacketPool( p_port->buf_mgr.h_packet_pool );\r
1149 \r
1150         /* Free the receive and send descriptors. */\r
1151         cl_qpool_destroy( &p_port->buf_mgr.recv_pool );\r
1152 \r
1153         /* Free the lookaside list of scratch buffers. */\r
1154         ExDeleteNPagedLookasideList( &p_port->buf_mgr.send_buf_list );\r
1155 \r
1156         IPOIB_EXIT(  IPOIB_DBG_INIT );\r
1157 }\r
1158 \r
1159 \r
1160 static cl_status_t\r
1161 __recv_ctor(\r
1162         IN                              void* const                                     p_object,\r
1163         IN                              void*                                           context,\r
1164                 OUT                     cl_pool_item_t** const          pp_pool_item )\r
1165 {\r
1166         ipoib_recv_desc_t       *p_desc;\r
1167         ipoib_port_t            *p_port;\r
1168         uint32_t                        ds0_len;\r
1169 \r
1170         IPOIB_ENTER( IPOIB_DBG_ALLOC );\r
1171 \r
1172         CL_ASSERT( p_object );\r
1173         CL_ASSERT( context );\r
1174 \r
1175         p_desc = (ipoib_recv_desc_t*)p_object;\r
1176         p_port = (ipoib_port_t*)context;\r
1177 \r
1178         /* Setup the work request. */\r
1179         p_desc->wr.ds_array = p_desc->local_ds;\r
1180         p_desc->wr.wr_id = (uintn_t)p_desc;\r
1181 \r
1182 #if IPOIB_INLINE_RECV\r
1183         /* Sanity check on the receive buffer layout */\r
1184         CL_ASSERT( (void*)&p_desc->buf.eth.pkt.type ==\r
1185                 (void*)&p_desc->buf.ib.pkt.type );\r
1186         CL_ASSERT( sizeof(recv_buf_t) == sizeof(ipoib_pkt_t) + sizeof(ib_grh_t) );\r
1187 \r
1188         /* Setup the local data segment. */\r
1189         p_desc->local_ds[0].vaddr = cl_get_physaddr( &p_desc->buf );\r
1190         p_desc->local_ds[0].lkey = p_port->ib_mgr.lkey;\r
1191         ds0_len =\r
1192                 PAGE_SIZE - ((uint32_t)p_desc->local_ds[0].vaddr & (PAGE_SIZE - 1));\r
1193         if( ds0_len >= sizeof(recv_buf_t) )\r
1194         {\r
1195                 /* The whole buffer is within a page. */\r
1196                 p_desc->local_ds[0].length = ds0_len;\r
1197                 p_desc->wr.num_ds = 1;\r
1198         }\r
1199         else\r
1200         {\r
1201                 /* The buffer crosses page boundaries. */\r
1202                 p_desc->local_ds[0].length = ds0_len;\r
1203                 p_desc->local_ds[1].vaddr = cl_get_physaddr( \r
1204                         ((uint8_t*)&p_desc->buf) + ds0_len );\r
1205                 p_desc->local_ds[1].lkey = p_port->ib_mgr.lkey;\r
1206                 p_desc->local_ds[1].length = sizeof(recv_buf_t) - ds0_len;\r
1207                 p_desc->wr.num_ds = 2;\r
1208         }\r
1209 #else   /* IPOIB_INLINE_RECV */\r
1210         /* Allocate the receive buffer. */\r
1211         p_desc->p_buf = (recv_buf_t*)cl_zalloc( sizeof(recv_buf_t) );\r
1212         if( !p_desc->p_buf )\r
1213         {\r
1214                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1215                         ("Failed to allocate receive buffer.\n") );\r
1216                 return CL_INSUFFICIENT_MEMORY;\r
1217         }\r
1218 \r
1219         /* Sanity check on the receive buffer layout */\r
1220         CL_ASSERT( (void*)&p_desc->p_buf->eth.pkt.type ==\r
1221                 (void*)&p_desc->p_buf->ib.pkt.type );\r
1222 \r
1223         /* Setup the local data segment. */\r
1224         p_desc->local_ds[0].vaddr = cl_get_physaddr( p_desc->p_buf );\r
1225         p_desc->local_ds[0].length = sizeof(ipoib_pkt_t) + sizeof(ib_grh_t);\r
1226         p_desc->local_ds[0].lkey = p_port->ib_mgr.lkey;\r
1227 #endif  /* IPOIB_INLINE_RECV */\r
1228 \r
1229         *pp_pool_item = &p_desc->item;\r
1230 \r
1231         IPOIB_EXIT( IPOIB_DBG_ALLOC );\r
1232         return CL_SUCCESS;\r
1233 }\r
1234 \r
1235 \r
1236 #if !IPOIB_INLINE_RECV\r
1237 static void\r
1238 __recv_dtor(\r
1239         IN              const   cl_pool_item_t* const           p_pool_item,\r
1240         IN                              void                                            *context )\r
1241 {\r
1242         ipoib_recv_desc_t       *p_desc;\r
1243 \r
1244         IPOIB_ENTER(  IPOIB_DBG_ALLOC );\r
1245 \r
1246         UNUSED_PARAM( context );\r
1247 \r
1248         p_desc = PARENT_STRUCT( p_pool_item, ipoib_recv_desc_t, item );\r
1249 \r
1250         if( p_desc->p_buf )\r
1251                 cl_free( p_desc->p_buf );\r
1252 \r
1253         IPOIB_EXIT( IPOIB_DBG_ALLOC );\r
1254 }\r
1255 #endif\r
1256 \r
1257 \r
1258 static inline ipoib_recv_desc_t*\r
1259 __buf_mgr_get_recv(\r
1260         IN                              ipoib_port_t* const                     p_port )\r
1261 {\r
1262         ipoib_recv_desc_t       *p_desc;\r
1263         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1264         p_desc = (ipoib_recv_desc_t*)cl_qpool_get( &p_port->buf_mgr.recv_pool );\r
1265         /* Reference the port object for the send. */\r
1266         if( p_desc )\r
1267         {\r
1268                 ipoib_port_ref( p_port, ref_get_recv );\r
1269                 CL_ASSERT( p_desc->wr.wr_id == (uintn_t)p_desc );\r
1270 #if IPOIB_INLINE_RECV\r
1271                 CL_ASSERT( p_desc->local_ds[0].vaddr ==\r
1272                         cl_get_physaddr( &p_desc->buf ) );\r
1273 #else   /* IPOIB_INLINE_RECV */\r
1274                 CL_ASSERT( p_desc->local_ds[0].vaddr ==\r
1275                         cl_get_physaddr( p_desc->p_buf ) );\r
1276                 CL_ASSERT( p_desc->local_ds[0].length ==\r
1277                         (sizeof(ipoib_pkt_t) + sizeof(ib_grh_t)) );\r
1278 #endif  /* IPOIB_INLINE_RECV */\r
1279                 CL_ASSERT( p_desc->local_ds[0].lkey == p_port->ib_mgr.lkey );\r
1280         }\r
1281         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1282         return p_desc;\r
1283 }\r
1284 \r
1285 \r
1286 static inline void\r
1287 __buf_mgr_put_recv(\r
1288         IN                              ipoib_port_t* const                     p_port,\r
1289         IN                              ipoib_recv_desc_t* const        p_desc,\r
1290         IN                              NDIS_PACKET* const                      p_packet OPTIONAL )\r
1291 {\r
1292         NDIS_BUFFER             *p_buf;\r
1293 \r
1294         IPOIB_ENTER(IPOIB_DBG_RECV );\r
1295 \r
1296         if( p_packet )\r
1297         {\r
1298                 /* Unchain the NDIS buffer. */\r
1299                 NdisUnchainBufferAtFront( p_packet, &p_buf );\r
1300                 CL_ASSERT( p_buf );\r
1301                 /* Return the NDIS packet and NDIS buffer to their pools. */\r
1302                 NdisDprFreePacketNonInterlocked( p_packet );\r
1303                 NdisFreeBuffer( p_buf );\r
1304         }\r
1305 \r
1306         /* Return the descriptor to its pools. */\r
1307         cl_qpool_put( &p_port->buf_mgr.recv_pool, &p_desc->item );\r
1308 \r
1309         /*\r
1310          * Dereference the port object since the receive is no longer outstanding.\r
1311          */\r
1312         ipoib_port_deref( p_port, ref_get_recv );\r
1313         IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1314 }\r
1315 \r
1316 \r
1317 static inline void\r
1318 __buf_mgr_put_recv_list(\r
1319         IN                              ipoib_port_t* const                     p_port,\r
1320         IN                              cl_qlist_t* const                       p_list )\r
1321 {\r
1322         //IPOIB_ENTER(  IPOIB_DBG_RECV );\r
1323         cl_qpool_put_list( &p_port->buf_mgr.recv_pool, p_list );\r
1324         //IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1325 }\r
1326 \r
1327 \r
1328 static inline NDIS_PACKET*\r
1329 __buf_mgr_get_ndis_pkt(\r
1330         IN                              ipoib_port_t* const                     p_port,\r
1331         IN                              ipoib_recv_desc_t* const        p_desc )\r
1332 {\r
1333         NDIS_STATUS                             status;\r
1334         NDIS_PACKET                             *p_packet;\r
1335         NDIS_BUFFER                             *p_buffer;\r
1336 \r
1337         IPOIB_ENTER(  IPOIB_DBG_RECV );\r
1338 \r
1339         NdisDprAllocatePacketNonInterlocked( &status, &p_packet,\r
1340                 p_port->buf_mgr.h_packet_pool );\r
1341         if( status != NDIS_STATUS_SUCCESS )\r
1342         {\r
1343                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1344                         ("Failed to allocate NDIS_PACKET: %08x\n", status) );\r
1345                 return NULL;\r
1346         }\r
1347 \r
1348         IPOIB_PORT_FROM_PACKET( p_packet ) = p_port;\r
1349         IPOIB_RECV_FROM_PACKET( p_packet ) = p_desc;\r
1350 \r
1351         NdisAllocateBuffer( &status, &p_buffer,\r
1352 #if IPOIB_INLINE_RECV\r
1353                 p_port->buf_mgr.h_buffer_pool, &p_desc->buf.eth.pkt, p_desc->len );\r
1354 #else   /* IPOIB_INLINE_RECV */\r
1355                 p_port->buf_mgr.h_buffer_pool, &p_desc->p_buf->eth.pkt, p_desc->len );\r
1356 #endif  /* IPOIB_INLINE_RECV */\r
1357         if( status != NDIS_STATUS_SUCCESS )\r
1358         {\r
1359                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1360                         ("Failed to allocate NDIS_BUFFER: %08x\n", status) );\r
1361                 NdisDprFreePacketNonInterlocked( p_packet );\r
1362                 return NULL;\r
1363         }\r
1364 \r
1365         NdisChainBufferAtFront( p_packet, p_buffer );\r
1366         NDIS_SET_PACKET_HEADER_SIZE( p_packet, sizeof(eth_hdr_t) );\r
1367 \r
1368         IPOIB_EXIT(  IPOIB_DBG_RECV );\r
1369         return p_packet;\r
1370 }\r
1371 \r
1372 /******************************************************************************\r
1373 *\r
1374 * Receive manager implementation.\r
1375 *\r
1376 ******************************************************************************/\r
1377 static void\r
1378 __recv_mgr_construct(\r
1379         IN                              ipoib_port_t* const                     p_port )\r
1380 {\r
1381         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1382 \r
1383         cl_qlist_init( &p_port->recv_mgr.done_list );\r
1384 \r
1385         p_port->recv_mgr.recv_pkt_array = NULL;\r
1386 \r
1387         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1388 }\r
1389 \r
1390 \r
1391 static ib_api_status_t\r
1392 __recv_mgr_init(\r
1393         IN                              ipoib_port_t* const                     p_port )\r
1394 {\r
1395         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1396 \r
1397         /* Allocate the NDIS_PACKET pointer array for indicating receives. */\r
1398         p_port->recv_mgr.recv_pkt_array = cl_malloc(\r
1399                 sizeof(NDIS_PACKET*) * p_port->p_adapter->params.rq_depth );\r
1400         if( !p_port->recv_mgr.recv_pkt_array )\r
1401         {\r
1402                 NdisWriteErrorLogEntry( p_port->p_adapter->h_adapter,\r
1403                         EVENT_IPOIB_RECV_PKT_ARRAY, 0 );\r
1404                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1405                         ("cl_malloc for PNDIS_PACKET array failed.\n") );\r
1406                 return IB_INSUFFICIENT_MEMORY;\r
1407         }\r
1408 \r
1409         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1410         return IB_SUCCESS;\r
1411 }\r
1412 \r
1413 \r
1414 static void\r
1415 __recv_mgr_destroy(\r
1416         IN                              ipoib_port_t* const                     p_port )\r
1417 {\r
1418         IPOIB_ENTER( IPOIB_DBG_INIT );\r
1419 \r
1420         CL_ASSERT( cl_is_qlist_empty( &p_port->recv_mgr.done_list ) );\r
1421         CL_ASSERT( !p_port->recv_mgr.depth );\r
1422 \r
1423         if( p_port->recv_mgr.recv_pkt_array )\r
1424                 cl_free( p_port->recv_mgr.recv_pkt_array );\r
1425 \r
1426         IPOIB_EXIT( IPOIB_DBG_INIT );\r
1427 }\r
1428 \r
1429 \r
1430 /*\r
1431  * Posts receive buffers to the receive queue and returns the number\r
1432  * of receives needed to bring the RQ to its low water mark.  Note\r
1433  * that the value is signed, and can go negative.  All tests must\r
1434  * be for > 0.\r
1435  */\r
1436 static int32_t\r
1437 __recv_mgr_repost(\r
1438         IN                              ipoib_port_t* const                     p_port )\r
1439 {\r
1440         ipoib_recv_desc_t       *p_head = NULL, *p_tail = NULL, *p_next;\r
1441         ib_api_status_t         status;\r
1442         ib_recv_wr_t            *p_failed;\r
1443         PERF_DECLARE( GetRecv );\r
1444         PERF_DECLARE( PostRecv );\r
1445 \r
1446         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1447 \r
1448         CL_ASSERT( p_port );\r
1449         cl_obj_lock( &p_port->obj );\r
1450         if( p_port->state != IB_QPS_RTS )\r
1451         {\r
1452                 cl_obj_unlock( &p_port->obj );\r
1453                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1454                         ("Port in invalid state.  Not reposting.\n") );\r
1455                 return 0;\r
1456         }\r
1457         ipoib_port_ref( p_port, ref_repost );\r
1458         cl_obj_unlock( &p_port->obj );\r
1459 \r
1460         while( p_port->recv_mgr.depth < p_port->p_adapter->params.rq_depth )\r
1461         {\r
1462                 /* Pull receives out of the pool and chain them up. */\r
1463                 cl_perf_start( GetRecv );\r
1464                 p_next = __buf_mgr_get_recv( p_port );\r
1465                 cl_perf_stop( &p_port->p_adapter->perf, GetRecv );\r
1466                 if( !p_next )\r
1467                 {\r
1468                         IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1469                                 ("Out of receive descriptors! recv queue depath 0x%x\n",p_port->recv_mgr.depth) );\r
1470                         break;\r
1471                 }\r
1472 \r
1473                 if( !p_tail )\r
1474                 {\r
1475                         p_tail = p_next;\r
1476                         p_next->wr.p_next = NULL;\r
1477                 }\r
1478                 else\r
1479                 {\r
1480                         p_next->wr.p_next = &p_head->wr;\r
1481                 }\r
1482 \r
1483                 p_head = p_next;\r
1484 \r
1485                 p_port->recv_mgr.depth++;\r
1486         }\r
1487 \r
1488         if( p_head )\r
1489         {\r
1490                 cl_perf_start( PostRecv );\r
1491                 status = p_port->p_adapter->p_ifc->post_recv(\r
1492                         p_port->ib_mgr.h_qp, &p_head->wr, &p_failed );\r
1493                 cl_perf_stop( &p_port->p_adapter->perf, PostRecv );\r
1494 \r
1495                 if( status != IB_SUCCESS )\r
1496                 {\r
1497                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1498                                 ("ip_post_recv returned %s\n", \r
1499                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1500                         /* return the descriptors to the pool */\r
1501                         while( p_failed )\r
1502                         {\r
1503                                 p_head = PARENT_STRUCT( p_failed, ipoib_recv_desc_t, wr );\r
1504                                 p_failed = p_failed->p_next;\r
1505 \r
1506                                 __buf_mgr_put_recv( p_port, p_head, NULL );\r
1507                                 p_port->recv_mgr.depth--;\r
1508                         }\r
1509                 }\r
1510         }\r
1511 \r
1512         ipoib_port_deref( p_port, ref_repost );\r
1513         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1514         return p_port->p_adapter->params.rq_low_watermark - p_port->recv_mgr.depth;\r
1515 }\r
1516 \r
1517 \r
1518 void\r
1519 ipoib_return_packet(\r
1520         IN                              NDIS_HANDLE                                     adapter_context,\r
1521         IN                              NDIS_PACKET                                     *p_packet )\r
1522 {\r
1523         cl_list_item_t          *p_item;\r
1524         ipoib_port_t            *p_port;\r
1525         ipoib_recv_desc_t       *p_desc;\r
1526         ib_api_status_t         status = IB_NOT_DONE;\r
1527         int32_t                         shortage;\r
1528         PERF_DECLARE( ReturnPacket );\r
1529         PERF_DECLARE( ReturnPutRecv );\r
1530         PERF_DECLARE( ReturnRepostRecv );\r
1531         PERF_DECLARE( ReturnPreparePkt );\r
1532         PERF_DECLARE( ReturnNdisIndicate );\r
1533 \r
1534         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1535 \r
1536         UNUSED_PARAM( adapter_context );\r
1537         CL_ASSERT( p_packet );\r
1538 \r
1539         cl_perf_start( ReturnPacket );\r
1540 \r
1541         /* Get the port and descriptor from the packet. */\r
1542         p_port = IPOIB_PORT_FROM_PACKET( p_packet );\r
1543         p_desc = IPOIB_RECV_FROM_PACKET( p_packet );\r
1544 \r
1545         cl_spinlock_acquire( &p_port->recv_lock );\r
1546 \r
1547         cl_perf_start( ReturnPutRecv );\r
1548         __buf_mgr_put_recv( p_port, p_desc, p_packet );\r
1549         cl_perf_stop( &p_port->p_adapter->perf, ReturnPutRecv );\r
1550 \r
1551         /* Repost buffers. */\r
1552         cl_perf_start( ReturnRepostRecv );\r
1553         shortage = __recv_mgr_repost( p_port );\r
1554         cl_perf_stop( &p_port->p_adapter->perf, ReturnRepostRecv );\r
1555 \r
1556         for( p_item = cl_qlist_remove_head( &p_port->recv_mgr.done_list );\r
1557                 p_item != cl_qlist_end( &p_port->recv_mgr.done_list );\r
1558                 p_item = cl_qlist_remove_head( &p_port->recv_mgr.done_list ) )\r
1559         {\r
1560                 p_desc = (ipoib_recv_desc_t*)p_item;\r
1561 \r
1562                 cl_perf_start( ReturnPreparePkt );\r
1563                 status = __recv_mgr_prepare_pkt( p_port, p_desc, &p_packet );\r
1564                 cl_perf_stop( &p_port->p_adapter->perf, ReturnPreparePkt );\r
1565                 if( status == IB_SUCCESS )\r
1566                 {\r
1567                         if( shortage > 0 )\r
1568                                 NDIS_SET_PACKET_STATUS( p_packet, NDIS_STATUS_RESOURCES );\r
1569                         else\r
1570                                 NDIS_SET_PACKET_STATUS( p_packet, NDIS_STATUS_SUCCESS );\r
1571 \r
1572                         cl_spinlock_release( &p_port->recv_lock );\r
1573                         cl_perf_start( ReturnNdisIndicate );\r
1574                         NdisMIndicateReceivePacket( p_port->p_adapter->h_adapter,\r
1575                                 &p_packet, 1 );\r
1576                         cl_perf_stop( &p_port->p_adapter->perf, ReturnNdisIndicate );\r
1577                         cl_spinlock_acquire( &p_port->recv_lock );\r
1578 \r
1579                         if( shortage > 0 )\r
1580                         {\r
1581                                 cl_perf_start( ReturnPutRecv );\r
1582                                 __buf_mgr_put_recv( p_port, p_desc, p_packet );\r
1583                                 cl_perf_stop( &p_port->p_adapter->perf, ReturnPutRecv );\r
1584 \r
1585                                 /* Repost buffers. */\r
1586                                 cl_perf_start( ReturnRepostRecv );\r
1587                                 shortage = __recv_mgr_repost( p_port );\r
1588                                 cl_perf_stop( &p_port->p_adapter->perf, ReturnRepostRecv );\r
1589                         }\r
1590                 }\r
1591                 else if( status != IB_NOT_DONE )\r
1592                 {\r
1593                         IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1594                                 ("__recv_mgr_prepare_pkt returned %s\n",\r
1595                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1596                         /* Return the item to the head of the list. */\r
1597                         cl_qlist_insert_head( &p_port->recv_mgr.done_list, p_item );\r
1598                         break;\r
1599                 }\r
1600         }\r
1601         cl_spinlock_release( &p_port->recv_lock );\r
1602         cl_perf_stop( &p_port->p_adapter->perf, ReturnPacket );\r
1603 \r
1604         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1605 }\r
1606 \r
1607 \r
1608 static void\r
1609 __recv_cb(\r
1610         IN              const   ib_cq_handle_t                          h_cq,\r
1611         IN                              void                                            *cq_context )\r
1612 {\r
1613         ipoib_port_t            *p_port;\r
1614         ib_api_status_t         status;\r
1615         ib_wc_t                         wc[MAX_RECV_WC], *p_free, *p_wc;\r
1616         int32_t                         pkt_cnt, recv_cnt = 0, shortage, discarded;\r
1617         cl_qlist_t                      done_list, bad_list;\r
1618         size_t                          i;\r
1619         PERF_DECLARE( RecvCompBundle );\r
1620         PERF_DECLARE( RecvCb );\r
1621         PERF_DECLARE( PollRecv );\r
1622         PERF_DECLARE( RepostRecv );\r
1623         PERF_DECLARE( FilterRecv );\r
1624         PERF_DECLARE( BuildPktArray );\r
1625         PERF_DECLARE( RecvNdisIndicate );\r
1626         PERF_DECLARE( RearmRecv );\r
1627         PERF_DECLARE( PutRecvList );\r
1628 \r
1629         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1630 \r
1631         cl_perf_clr( RecvCompBundle );\r
1632 \r
1633         cl_perf_start( RecvCb );\r
1634 \r
1635         UNUSED_PARAM( h_cq );\r
1636 \r
1637         p_port = (ipoib_port_t*)cq_context;\r
1638 \r
1639         cl_qlist_init( &done_list );\r
1640         cl_qlist_init( &bad_list );\r
1641 \r
1642         ipoib_port_ref( p_port, ref_recv_cb );\r
1643         for( i = 0; i < MAX_RECV_WC; i++ )\r
1644                 wc[i].p_next = &wc[i + 1];\r
1645         wc[MAX_RECV_WC - 1].p_next = NULL;\r
1646 \r
1647         /*\r
1648          * We'll be accessing the endpoint map so take a reference\r
1649          * on it to prevent modifications.\r
1650          */\r
1651         cl_obj_lock( &p_port->obj );\r
1652         cl_atomic_inc( &p_port->endpt_rdr );\r
1653         cl_obj_unlock( &p_port->obj );\r
1654 \r
1655         do\r
1656         {\r
1657                 /* If we get here, then the list of WCs is intact. */\r
1658                 p_free = wc;\r
1659 \r
1660                 cl_perf_start( PollRecv );\r
1661                 status = p_port->p_adapter->p_ifc->poll_cq(\r
1662                         p_port->ib_mgr.h_recv_cq, &p_free, &p_wc );\r
1663                 cl_perf_stop( &p_port->p_adapter->perf, PollRecv );\r
1664                 CL_ASSERT( status == IB_SUCCESS || status == IB_NOT_FOUND );\r
1665 \r
1666                 /* Look at the payload now and filter ARP and DHCP packets. */\r
1667                 cl_perf_start( FilterRecv );\r
1668                 recv_cnt += __recv_mgr_filter( p_port, p_wc, &done_list, &bad_list );\r
1669                 cl_perf_stop( &p_port->p_adapter->perf, FilterRecv );\r
1670 \r
1671         } while( !p_free );\r
1672 \r
1673         /* We're done looking at the endpoint map, release the reference. */\r
1674         cl_atomic_dec( &p_port->endpt_rdr );\r
1675 \r
1676         cl_perf_log( &p_port->p_adapter->perf, RecvCompBundle, recv_cnt );\r
1677 \r
1678         cl_spinlock_acquire( &p_port->recv_lock );\r
1679 \r
1680         /* Update our posted depth. */\r
1681         p_port->recv_mgr.depth -= recv_cnt;\r
1682 \r
1683         /* Return any discarded receives to the pool */\r
1684         cl_perf_start( PutRecvList );\r
1685         __buf_mgr_put_recv_list( p_port, &bad_list );\r
1686         cl_perf_stop( &p_port->p_adapter->perf, PutRecvList );\r
1687 \r
1688         do\r
1689         {\r
1690                 /* Repost ASAP so we don't starve the RQ. */\r
1691                 cl_perf_start( RepostRecv );\r
1692                 shortage = __recv_mgr_repost( p_port );\r
1693                 cl_perf_stop( &p_port->p_adapter->perf, RepostRecv );\r
1694 \r
1695                 cl_perf_start( BuildPktArray );\r
1696                 /* Notify NDIS of any and all possible receive buffers. */\r
1697                 pkt_cnt = __recv_mgr_build_pkt_array(\r
1698                         p_port, shortage, &done_list, &discarded );\r
1699                 cl_perf_stop( &p_port->p_adapter->perf, BuildPktArray );\r
1700 \r
1701                 /* Only indicate receives if we actually had any. */\r
1702                 if( discarded && shortage > 0 )\r
1703                 {\r
1704                         /* We may have thrown away packets, and have a shortage */\r
1705                         cl_perf_start( RepostRecv );\r
1706                         __recv_mgr_repost( p_port );\r
1707                         cl_perf_stop( &p_port->p_adapter->perf, RepostRecv );\r
1708                 }\r
1709 \r
1710                 if( !pkt_cnt )\r
1711                         break;\r
1712 \r
1713                 cl_spinlock_release( &p_port->recv_lock );\r
1714 \r
1715                 cl_perf_start( RecvNdisIndicate );\r
1716                 NdisMIndicateReceivePacket( p_port->p_adapter->h_adapter,\r
1717                         p_port->recv_mgr.recv_pkt_array, pkt_cnt );\r
1718                 cl_perf_stop( &p_port->p_adapter->perf, RecvNdisIndicate );\r
1719 \r
1720                 /*\r
1721                  * Cap the number of receives to put back to what we just indicated\r
1722                  * with NDIS_STATUS_RESOURCES.\r
1723                  */\r
1724                 if( shortage > 0 )\r
1725                 {\r
1726                         if( pkt_cnt < shortage )\r
1727                                 shortage = pkt_cnt;\r
1728 \r
1729                         /* Return all but the last packet to the pool. */\r
1730                         cl_spinlock_acquire( &p_port->recv_lock );\r
1731                         while( shortage-- > 1 )\r
1732                         {\r
1733                                 __buf_mgr_put_recv( p_port,\r
1734                                         IPOIB_RECV_FROM_PACKET( p_port->recv_mgr.recv_pkt_array[shortage] ),\r
1735                                         p_port->recv_mgr.recv_pkt_array[shortage] );\r
1736                         }\r
1737                         cl_spinlock_release( &p_port->recv_lock );\r
1738 \r
1739                         /*\r
1740                          * Return the last packet as if NDIS returned it, so that we repost\r
1741                          * and report any other pending receives.\r
1742                          */\r
1743                         ipoib_return_packet( NULL, p_port->recv_mgr.recv_pkt_array[0] );\r
1744                 }\r
1745                 cl_spinlock_acquire( &p_port->recv_lock );\r
1746 \r
1747         } while( pkt_cnt );\r
1748         cl_spinlock_release( &p_port->recv_lock );\r
1749 \r
1750         /*\r
1751          * Rearm after filtering to prevent contention on the enpoint maps\r
1752          * and eliminate the possibility of having a call to\r
1753          * __endpt_mgr_insert find a duplicate.\r
1754          */\r
1755         cl_perf_start( RearmRecv );\r
1756         status = p_port->p_adapter->p_ifc->rearm_cq(\r
1757                 p_port->ib_mgr.h_recv_cq, FALSE );\r
1758         cl_perf_stop( &p_port->p_adapter->perf, RearmRecv );\r
1759         CL_ASSERT( status == IB_SUCCESS );\r
1760 \r
1761         ipoib_port_deref( p_port, ref_recv_cb );\r
1762 \r
1763         cl_perf_stop( &p_port->p_adapter->perf, RecvCb );\r
1764 \r
1765         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1766 }\r
1767 \r
1768 \r
1769 static void\r
1770 __recv_get_endpts(\r
1771         IN                              ipoib_port_t* const                     p_port,\r
1772         IN                              ipoib_recv_desc_t* const        p_desc,\r
1773         IN                              ib_wc_t* const                          p_wc,\r
1774                 OUT                     ipoib_endpt_t** const           pp_src,\r
1775                 OUT                     ipoib_endpt_t** const           pp_dst )\r
1776 {\r
1777         ib_api_status_t         status;\r
1778         mac_addr_t                      mac;\r
1779         PERF_DECLARE( GetEndptByGid );\r
1780         PERF_DECLARE( GetEndptByLid );\r
1781         PERF_DECLARE( EndptInsert );\r
1782 \r
1783         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1784 \r
1785         /* Setup our shortcut pointers based on whether GRH is valid. */\r
1786         if( p_wc->recv.ud.recv_opt & IB_RECV_OPT_GRH_VALID )\r
1787         {\r
1788                 /* Lookup the source endpoints based on GID. */\r
1789                 cl_perf_start( GetEndptByGid );\r
1790                 *pp_src =\r
1791 #if IPOIB_INLINE_RECV\r
1792                         __endpt_mgr_get_by_gid( p_port, &p_desc->buf.ib.grh.src_gid );\r
1793 #else   /* IPOIB_INLINE_RECV */\r
1794                         __endpt_mgr_get_by_gid( p_port, &p_desc->p_buf->ib.grh.src_gid );\r
1795 #endif  /* IPOIB_INLINE_RECV */\r
1796                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByGid );\r
1797 \r
1798                 /*\r
1799                  * Lookup the destination endpoint based on GID.\r
1800                  * This is used along with the packet filter to determine\r
1801                  * whether to report this to NDIS.\r
1802                  */\r
1803                 cl_perf_start( GetEndptByGid );\r
1804                 *pp_dst =\r
1805 #if IPOIB_INLINE_RECV\r
1806                         __endpt_mgr_get_by_gid( p_port, &p_desc->buf.ib.grh.dest_gid );\r
1807 #else   /* IPOIB_INLINE_RECV */\r
1808                         __endpt_mgr_get_by_gid( p_port, &p_desc->p_buf->ib.grh.dest_gid );\r
1809 #endif  /* IPOIB_INLINE_RECV */\r
1810                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByGid );\r
1811 \r
1812                 /*\r
1813                  * Create the source endpoint if it does not exist.  Note that we\r
1814                  * can only do this for globally routed traffic since we need the\r
1815                  * information from the GRH to generate the MAC.\r
1816                  */\r
1817                 if( !*pp_src )\r
1818                 {\r
1819                         status = ipoib_mac_from_guid(\r
1820 #if IPOIB_INLINE_RECV\r
1821                                 p_desc->buf.ib.grh.src_gid.unicast.interface_id, &mac );\r
1822 #else   /* IPOIB_INLINE_RECV */\r
1823                                 p_desc->p_buf->ib.grh.src_gid.unicast.interface_id, &mac );\r
1824 #endif  /* IPOIB_INLINE_RECV */\r
1825                         if( status != IB_SUCCESS )\r
1826                         {\r
1827                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1828                                         ("ipoib_mac_from_guid returned %s\n",\r
1829                                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1830                                 return;\r
1831                         }\r
1832 \r
1833                         /* Create the endpoint. */\r
1834 #if IPOIB_INLINE_RECV\r
1835                         *pp_src = ipoib_endpt_create( &p_desc->buf.ib.grh.src_gid,\r
1836 #else   /* IPOIB_INLINE_RECV */\r
1837                         *pp_src = ipoib_endpt_create( &p_desc->p_buf->ib.grh.src_gid,\r
1838 #endif  /* IPOIB_INLINE_RECV */\r
1839                                 0, p_wc->recv.ud.remote_qp );\r
1840                         if( !*pp_src )\r
1841                         {\r
1842                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1843                                         ("ipoib_endpt_create failed\n") );\r
1844                                 return;\r
1845                         }\r
1846                         cl_perf_start( EndptInsert );\r
1847                         cl_obj_lock( &p_port->obj );\r
1848                         status = __endpt_mgr_insert( p_port, mac, *pp_src );\r
1849                         if( status != IB_SUCCESS )\r
1850                         {\r
1851                                 cl_obj_unlock( &p_port->obj );\r
1852                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1853                                         ("__endpt_mgr_insert returned %s\n",\r
1854                                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1855                                 return;\r
1856                         }\r
1857                         cl_obj_unlock( &p_port->obj );\r
1858                         cl_perf_stop( &p_port->p_adapter->perf, EndptInsert );\r
1859                 }\r
1860         }\r
1861         else\r
1862         {\r
1863                 /*\r
1864                  * Lookup the remote endpoint based on LID.  Note that only\r
1865                  * unicast traffic can be LID routed.\r
1866                  */\r
1867                 cl_perf_start( GetEndptByLid );\r
1868                 *pp_src = __endpt_mgr_get_by_lid( p_port, p_wc->recv.ud.remote_lid );\r
1869                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByLid );\r
1870                 *pp_dst = p_port->p_local_endpt;\r
1871                 CL_ASSERT( *pp_dst );\r
1872         }\r
1873 \r
1874         if( *pp_src && !ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) &&\r
1875                 (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
1876         {\r
1877                 /* Update the QPN for the endpoint. */\r
1878                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1879                         ("Updating QPN for MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
1880                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
1881                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
1882                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5]) );\r
1883                 (*pp_src)->qpn = p_wc->recv.ud.remote_qp;\r
1884         }\r
1885 \r
1886         if( *pp_src && *pp_dst )\r
1887         {\r
1888                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1889                         ("Recv:\n"\r
1890                         "\tsrc MAC: %02X-%02X-%02X-%02X-%02X-%02X\n"\r
1891                         "\tdst MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
1892                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
1893                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
1894                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5],\r
1895                         (*pp_dst )->mac.addr[0], (*pp_dst )->mac.addr[1],\r
1896                         (*pp_dst )->mac.addr[2], (*pp_dst )->mac.addr[3],\r
1897                         (*pp_dst )->mac.addr[4], (*pp_dst )->mac.addr[5]) );\r
1898         }\r
1899 \r
1900         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1901 }\r
1902 \r
1903 \r
1904 static int32_t\r
1905 __recv_mgr_filter(\r
1906         IN                              ipoib_port_t* const                     p_port,\r
1907         IN                              ib_wc_t* const                          p_done_wc_list,\r
1908                 OUT                     cl_qlist_t* const                       p_done_list,\r
1909                 OUT                     cl_qlist_t* const                       p_bad_list )\r
1910 {\r
1911         ipoib_recv_desc_t               *p_desc;\r
1912         ib_wc_t                                 *p_wc;\r
1913         ipoib_pkt_t                             *p_ipoib;\r
1914         eth_pkt_t                               *p_eth;\r
1915         ipoib_endpt_t                   *p_src, *p_dst;\r
1916         ib_api_status_t                 status;\r
1917         uint32_t                                len;\r
1918         int32_t                                 recv_cnt = 0;\r
1919         PERF_DECLARE( GetRecvEndpts );\r
1920         PERF_DECLARE( RecvGen );\r
1921         PERF_DECLARE( RecvTcp );\r
1922         PERF_DECLARE( RecvUdp );\r
1923         PERF_DECLARE( RecvDhcp );\r
1924         PERF_DECLARE( RecvArp );\r
1925 \r
1926         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1927 \r
1928         for( p_wc = p_done_wc_list; p_wc; p_wc = p_wc->p_next )\r
1929         {\r
1930                 CL_ASSERT( p_wc->status != IB_WCS_SUCCESS || p_wc->wc_type == IB_WC_RECV );\r
1931                 p_desc = (ipoib_recv_desc_t*)(uintn_t)p_wc->wr_id;\r
1932                 recv_cnt++;\r
1933 \r
1934                 if( p_wc->status != IB_WCS_SUCCESS )\r
1935                 {\r
1936                         if( p_wc->status != IB_WCS_WR_FLUSHED_ERR )\r
1937                         {\r
1938                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1939                                         ("Failed completion %s  (vendor specific %#x)\n",\r
1940                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status ),\r
1941                                         (int)p_wc->vendor_specific) );\r
1942                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1943                         }\r
1944                         else\r
1945                         {\r
1946                                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1947                                         ("Flushed completion %s\n",\r
1948                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status )) );\r
1949                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_DROPPED, 0 );\r
1950                         }\r
1951                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1952                         /* Dereference the port object on behalf of the failed receive. */\r
1953                         ipoib_port_deref( p_port, ref_failed_recv_wc );\r
1954                         continue;\r
1955                 }\r
1956 \r
1957                 len = p_wc->length - sizeof(ib_grh_t);\r
1958 \r
1959                 if( len < sizeof(ipoib_hdr_t) )\r
1960                 {\r
1961                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1962                                 ("Received ETH packet < min size\n") );\r
1963                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1964                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1965                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
1966                         continue;\r
1967                 }\r
1968 \r
1969                 if((len - sizeof(ipoib_hdr_t)) > p_port->p_adapter->params.payload_mtu)\r
1970                 {\r
1971                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1972                                 ("Received ETH packet > payload MTU (%d)\n",\r
1973                                 p_port->p_adapter->params.payload_mtu) );\r
1974                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1975                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1976                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
1977                         continue;\r
1978                         \r
1979                 }\r
1980                 /* Successful completion.  Get the receive information. */\r
1981                 p_desc->ndis_csum.Value = ( (p_wc->recv.ud.recv_opt & IB_RECV_OPT_CSUM_MASK ) >> 8 );\r
1982                 cl_perf_start( GetRecvEndpts );\r
1983                 __recv_get_endpts( p_port, p_desc, p_wc, &p_src, &p_dst );\r
1984                 cl_perf_stop( &p_port->p_adapter->perf, GetRecvEndpts );\r
1985 \r
1986 #if IPOIB_INLINE_RECV\r
1987                 p_ipoib = &p_desc->buf.ib.pkt;\r
1988                 p_eth = &p_desc->buf.eth.pkt;\r
1989 #else   /* IPOIB_INLINE_RECV */\r
1990                 p_ipoib = &p_desc->p_buf->ib.pkt;\r
1991                 p_eth = &p_desc->p_buf->eth.pkt;\r
1992 #endif  /*IPOIB_INLINE_RECV */\r
1993 \r
1994                 if( p_src )\r
1995                 {\r
1996                         /* Don't report loopback traffic - we requested SW loopback. */\r
1997                         if( !cl_memcmp( &p_port->p_adapter->params.conf_mac,\r
1998                                 &p_src->mac, sizeof(p_port->p_adapter->params.conf_mac) ) )\r
1999                         {\r
2000                                 /*\r
2001                                  * "This is not the packet you're looking for" - don't update\r
2002                                  * receive statistics, the packet never happened.\r
2003                                  */\r
2004                                 cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2005                                 /* Dereference the port object on behalf of the failed recv. */\r
2006                                 ipoib_port_deref( p_port, ref_recv_loopback );\r
2007                                 continue;\r
2008                         }\r
2009                 }\r
2010 \r
2011                 switch( p_ipoib->hdr.type )\r
2012                 {\r
2013                 case ETH_PROT_TYPE_IP:\r
2014                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t)) )\r
2015                         {\r
2016                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2017                                         ("Received IP packet < min size\n") );\r
2018                                 status = IB_INVALID_SETTING;\r
2019                                 break;\r
2020                         }\r
2021 \r
2022                         if( p_ipoib->type.ip.hdr.offset ||\r
2023                                 p_ipoib->type.ip.hdr.prot != IP_PROT_UDP )\r
2024                         {\r
2025                                 /* Unfiltered.  Setup the ethernet header and report. */\r
2026                                 cl_perf_start( RecvTcp );\r
2027                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2028                                 cl_perf_stop( &p_port->p_adapter->perf, RecvTcp );\r
2029                                 break;\r
2030                         }\r
2031 \r
2032                         /* First packet of a UDP transfer. */\r
2033                         if( len <\r
2034                                 (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) + sizeof(udp_hdr_t)) )\r
2035                         {\r
2036                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2037                                         ("Received UDP packet < min size\n") );\r
2038                                 status = IB_INVALID_SETTING;\r
2039                                 break;\r
2040                         }\r
2041 \r
2042                         /* Check if DHCP conversion is required. */\r
2043                         if( (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_SERVER &&\r
2044                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_CLIENT) ||\r
2045                                 (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_CLIENT &&\r
2046                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_SERVER) )\r
2047                         {\r
2048                                 if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) +\r
2049                                         sizeof(udp_hdr_t) + DHCP_MIN_SIZE) )\r
2050                                 {\r
2051                                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2052                                                 ("Received DHCP < min size\n") );\r
2053                                         status = IB_INVALID_SETTING;\r
2054                                         break;\r
2055                                 }\r
2056                                 if ((p_ipoib->type.ip.hdr.ver_hl & 0x0f) != 5 ) {\r
2057                                         // If there are IP options in this message, we are in trouble in any case\r
2058                                         status = IB_INVALID_SETTING;\r
2059                                         break;                                  \r
2060                                 }\r
2061                                 /* UDP packet with BOOTP ports in src/dst port numbers. */\r
2062                                 cl_perf_start( RecvDhcp );\r
2063                                 status = __recv_dhcp( p_port, p_ipoib, p_eth, p_src, p_dst );\r
2064                                 cl_perf_stop( &p_port->p_adapter->perf, RecvDhcp );\r
2065                         }\r
2066                         else\r
2067                         {\r
2068                                 /* Unfiltered.  Setup the ethernet header and report. */\r
2069                                 cl_perf_start( RecvUdp );\r
2070                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2071                                 cl_perf_stop( &p_port->p_adapter->perf, RecvUdp );\r
2072                         }\r
2073                         break;\r
2074 \r
2075                 case ETH_PROT_TYPE_ARP:\r
2076                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ipoib_arp_pkt_t)) )\r
2077                         {\r
2078                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2079                                         ("Received ARP < min size\n") );\r
2080                                 status = IB_INVALID_SETTING;\r
2081                                 break;\r
2082                         }\r
2083                         cl_perf_start( RecvArp );\r
2084                         status = __recv_arp( p_port, p_wc, p_ipoib, p_eth, &p_src, p_dst );\r
2085                         cl_perf_stop( &p_port->p_adapter->perf, RecvArp );\r
2086                         len = sizeof(ipoib_hdr_t) + sizeof(arp_pkt_t);\r
2087                         break;\r
2088 \r
2089                 default:\r
2090                         /* Unfiltered.  Setup the ethernet header and report. */\r
2091                         cl_perf_start( RecvGen );\r
2092                         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2093                         cl_perf_stop( &p_port->p_adapter->perf, RecvGen );\r
2094                 }\r
2095 \r
2096                 if( status != IB_SUCCESS )\r
2097                 {\r
2098                         /* Update stats. */\r
2099                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
2100                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2101                         /* Dereference the port object on behalf of the failed receive. */\r
2102                         ipoib_port_deref( p_port, ref_recv_filter );\r
2103                 }\r
2104                 else\r
2105                 {\r
2106                         ip_stat_sel_t               ip_stat;\r
2107                         p_desc->len =\r
2108                                 len + sizeof(eth_hdr_t) - sizeof(ipoib_hdr_t);\r
2109                         if( p_dst->h_mcast)\r
2110                         {\r
2111                                 if( p_dst->dgid.multicast.raw_group_id[10] == 0xFF &&\r
2112                                         p_dst->dgid.multicast.raw_group_id[11] == 0xFF &&\r
2113                                         p_dst->dgid.multicast.raw_group_id[12] == 0xFF &&\r
2114                                         p_dst->dgid.multicast.raw_group_id[13] == 0xFF )\r
2115                                 {\r
2116                                         p_desc->type = PKT_TYPE_BCAST;\r
2117                                         ip_stat = IP_STAT_BCAST_BYTES;\r
2118                                 }\r
2119                                 else\r
2120                                 {\r
2121                                         p_desc->type = PKT_TYPE_MCAST;\r
2122                                         ip_stat = IP_STAT_MCAST_BYTES;\r
2123                                 }\r
2124                         }\r
2125                         else\r
2126                         {\r
2127                                 p_desc->type = PKT_TYPE_UCAST;\r
2128                                 ip_stat = IP_STAT_UCAST_BYTES;\r
2129                                 \r
2130                         }\r
2131                         cl_qlist_insert_tail( p_done_list, &p_desc->item.list_item );\r
2132                         ipoib_inc_recv_stat( p_port->p_adapter,ip_stat , len );  \r
2133                 }\r
2134         }\r
2135 \r
2136         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2137         return recv_cnt;\r
2138 }\r
2139 \r
2140 \r
2141 static ib_api_status_t\r
2142 __recv_gen(\r
2143         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2144                 OUT                     eth_pkt_t* const                        p_eth,\r
2145         IN                              ipoib_endpt_t* const            p_src,\r
2146         IN                              ipoib_endpt_t* const            p_dst )\r
2147 {\r
2148         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2149 \r
2150         if( !p_src || !p_dst )\r
2151         {\r
2152                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2153                         ("Received packet with no matching endpoints.\n") );\r
2154                 return IB_NOT_DONE;\r
2155         }\r
2156 \r
2157         /*\r
2158          * Fill in the ethernet header.  Note that doing so will overwrite\r
2159          * the IPoIB header, so start by moving the information from the IPoIB\r
2160          * header.\r
2161          */\r
2162         p_eth->hdr.type = p_ipoib->hdr.type;\r
2163         p_eth->hdr.src = p_src->mac;\r
2164         p_eth->hdr.dst = p_dst->mac;\r
2165 \r
2166         if ( p_eth->hdr.dst.addr[0] == 1 && \r
2167                  p_eth->hdr.type == ETH_PROT_TYPE_IP &&\r
2168                  p_eth->hdr.dst.addr[2] == 0x5E)  \r
2169         {\r
2170                 p_eth->hdr.dst.addr[1] = 0;\r
2171                 p_eth->hdr.dst.addr[3] = p_eth->hdr.dst.addr[3] & 0x7f;\r
2172         }\r
2173         if (p_dst->h_mcast)\r
2174                 p_dst->is_in_use = TRUE;\r
2175 \r
2176         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2177         return IB_SUCCESS;\r
2178 }\r
2179 \r
2180 \r
2181 static ib_api_status_t\r
2182 __recv_dhcp(\r
2183         IN                              ipoib_port_t* const                     p_port,\r
2184         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2185                 OUT                     eth_pkt_t* const                        p_eth,\r
2186         IN                              ipoib_endpt_t* const            p_src,\r
2187         IN                              ipoib_endpt_t* const            p_dst )\r
2188 {\r
2189         ib_api_status_t         status;\r
2190         dhcp_pkt_t                      *p_dhcp;\r
2191         uint8_t                         *p_option;\r
2192         uint8_t                         *p_cid = NULL;\r
2193         ib_gid_t                        gid;\r
2194         uint8_t                         msg = 0;\r
2195 \r
2196         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2197 \r
2198         UNUSED_PARAM( p_port );\r
2199 \r
2200         /* Create the ethernet header. */\r
2201         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2202         if( status != IB_SUCCESS )\r
2203         {\r
2204                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2205                         ("__recv_gen returned %s.\n", \r
2206                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2207                 return status;\r
2208         }\r
2209 \r
2210         /* Fixup the payload. */\r
2211         p_dhcp = &p_eth->type.ip.prot.udp.dhcp;\r
2212         if( p_dhcp->op != DHCP_REQUEST && p_dhcp->op != DHCP_REPLY )\r
2213         {\r
2214                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2215                         ("Invalid DHCP op code.\n") );\r
2216                 return IB_INVALID_SETTING;\r
2217         }\r
2218 \r
2219         /*\r
2220          * Find the client identifier option, making sure to skip\r
2221          * the "magic cookie".\r
2222          */\r
2223         p_option = &p_dhcp->options[0];\r
2224         if ( *(uint32_t *)p_option != DHCP_COOKIE )\r
2225         {\r
2226                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2227                         ("DHCP cookie corrupted.\n") );\r
2228                 return IB_INVALID_PARAMETER;\r
2229         }\r
2230 \r
2231         p_option = &p_dhcp->options[4];\r
2232         while( *p_option != DHCP_OPT_END && p_option < &p_dhcp->options[312] )\r
2233         {\r
2234                 switch( *p_option )\r
2235                 {\r
2236                 case DHCP_OPT_PAD:\r
2237                         p_option++;\r
2238                         break;\r
2239 \r
2240                 case DHCP_OPT_MSG:\r
2241                         msg = p_option[2];\r
2242                         p_option += 3;\r
2243                         break;\r
2244 \r
2245                 case DHCP_OPT_CLIENT_ID:\r
2246                         p_cid = p_option;\r
2247                         /* Fall through. */\r
2248 \r
2249                 default:\r
2250                         /*\r
2251                          * All other options have a length byte following the option code.\r
2252                          * Offset by the length to get to the next option.\r
2253                          */\r
2254                         p_option += (p_option[1] + 2);\r
2255                 }\r
2256         }\r
2257 \r
2258         switch( msg )\r
2259         {\r
2260         /* message from client */\r
2261         case DHCPDISCOVER:\r
2262         case DHCPREQUEST:\r
2263         case DHCPDECLINE:\r
2264         case DHCPRELEASE:\r
2265         case DHCPINFORM:\r
2266                 if( !p_cid )\r
2267                 {\r
2268                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2269                                 ("Failed to find required Client-identifier option.\n") );\r
2270                         return IB_INVALID_SETTING;\r
2271                 }\r
2272                 if( p_dhcp->htype != DHCP_HW_TYPE_IB )\r
2273                 {\r
2274                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2275                                 ("Invalid hardware address type.\n") );\r
2276                         return IB_INVALID_SETTING;\r
2277                 }\r
2278                 break;\r
2279         /* message from DHCP server */\r
2280         case DHCPOFFER:\r
2281         case DHCPACK:\r
2282         case DHCPNAK:\r
2283                 break;\r
2284 \r
2285         default:\r
2286                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2287                         ("Invalide message type.\n") );\r
2288                 return IB_INVALID_PARAMETER;\r
2289         }\r
2290         p_eth->type.ip.prot.udp.hdr.chksum = 0;\r
2291         p_dhcp->htype = DHCP_HW_TYPE_ETH;\r
2292         p_dhcp->hlen = HW_ADDR_LEN;\r
2293 \r
2294         if( p_cid ) /* from client */\r
2295         {\r
2296                 /* Validate that the length and type of the option is as required. */\r
2297                 if( p_cid[1] != 21 )\r
2298                 {\r
2299                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2300                                 ("Client-identifier length not 21 as required.\n") );\r
2301                         return IB_INVALID_SETTING;\r
2302                 }\r
2303                 if( p_cid[2] != DHCP_HW_TYPE_IB )\r
2304                 {\r
2305                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2306                                 ("Client-identifier type is wrong.\n") );\r
2307                         return IB_INVALID_SETTING;\r
2308                 }\r
2309                 /*\r
2310                  * Copy the GID value from the option so that we can make aligned\r
2311                  * accesses to the contents.\r
2312                  * Recover CID to standard type.\r
2313                  */\r
2314                 cl_memcpy( &gid, &p_cid[7], sizeof(ib_gid_t) );\r
2315                 p_cid[1] =  HW_ADDR_LEN +1;// CID length \r
2316                 p_cid[2] =  DHCP_HW_TYPE_ETH;// CID type \r
2317                 status = ipoib_mac_from_guid( gid.unicast.interface_id, (mac_addr_t*)&p_cid[3] );\r
2318                 p_cid[HW_ADDR_LEN + 3] = DHCP_OPT_END; //terminate tag\r
2319         }\r
2320         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2321         return status;\r
2322 }\r
2323 \r
2324 \r
2325 static ib_api_status_t\r
2326 __recv_arp(\r
2327         IN                              ipoib_port_t* const                     p_port,\r
2328         IN                              ib_wc_t* const                          p_wc,\r
2329         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2330                 OUT                     eth_pkt_t* const                        p_eth,\r
2331         IN                              ipoib_endpt_t** const           pp_src,\r
2332         IN                              ipoib_endpt_t* const            p_dst )\r
2333 {\r
2334         ib_api_status_t                 status;\r
2335         arp_pkt_t                               *p_arp;\r
2336         const ipoib_arp_pkt_t   *p_ib_arp;\r
2337         ib_gid_t                                gid;\r
2338         mac_addr_t                              mac;\r
2339         ipoib_hw_addr_t                 null_hw = {0};\r
2340 \r
2341         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2342 \r
2343         if( !p_dst )\r
2344         {\r
2345                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2346                         ("Unknown destination endpoint\n") );\r
2347                 return IB_INVALID_SETTING;\r
2348         }\r
2349 \r
2350         p_ib_arp = &p_ipoib->type.arp;\r
2351         p_arp = &p_eth->type.arp;\r
2352 \r
2353         if( p_ib_arp->hw_type != ARP_HW_TYPE_IB )\r
2354         {\r
2355                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2356                         ("ARP hardware type is not IB\n") );\r
2357                 return IB_INVALID_SETTING;\r
2358         }\r
2359 \r
2360         if( p_ib_arp->hw_size != sizeof(ipoib_hw_addr_t) )\r
2361         {\r
2362                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2363                         ("ARP hardware address size is not sizeof(ipoib_hw_addr_t)\n") );\r
2364                 return IB_INVALID_SETTING;\r
2365         }\r
2366 \r
2367         if( p_ib_arp->prot_type != ETH_PROT_TYPE_IP )\r
2368         {\r
2369                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2370                         ("ARP protocal type not IP\n") );\r
2371                 return IB_INVALID_SETTING;\r
2372         }\r
2373 \r
2374         /*\r
2375          * If we don't have a source, lookup the endpoint specified in the payload.\r
2376          */\r
2377         if( !*pp_src )\r
2378                 *pp_src = __endpt_mgr_get_by_gid( p_port, &p_ib_arp->src_hw.gid );\r
2379 \r
2380         /*\r
2381          * If the endpoint exists for the GID, make sure\r
2382          * the dlid and qpn match the arp.\r
2383          */\r
2384         if( *pp_src )\r
2385         {\r
2386                 if( cl_memcmp( &(*pp_src)->dgid, &p_ib_arp->src_hw.gid,\r
2387                         sizeof(ib_gid_t) ) )\r
2388                 {\r
2389                         /*\r
2390                          * GIDs for the endpoint are different.  The ARP must\r
2391                          * have been proxied.  Dereference it.\r
2392                          */\r
2393                         *pp_src = NULL;\r
2394                 }\r
2395                 else if( (*pp_src)->dlid &&\r
2396                         (*pp_src)->dlid != p_wc->recv.ud.remote_lid )\r
2397                 {\r
2398                         /* Out of date!  Destroy the endpoint and replace it. */\r
2399                         __endpt_mgr_remove( p_port, *pp_src );\r
2400                         *pp_src = NULL;\r
2401                 }\r
2402                 else if( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) )\r
2403                 {\r
2404                         if( (*pp_src)->qpn !=\r
2405                                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) &&\r
2406                                 p_wc->recv.ud.remote_qp !=\r
2407                                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) )\r
2408                         {\r
2409                                 /* Out of date!  Destroy the endpoint and replace it. */\r
2410                                 __endpt_mgr_remove( p_port, *pp_src );\r
2411                                 *pp_src = NULL;\r
2412                         }\r
2413                 }\r
2414                 else if( (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
2415                 {\r
2416                         /* Out of date!  Destroy the endpoint and replace it. */\r
2417                         __endpt_mgr_remove( p_port, *pp_src );\r
2418                         *pp_src = NULL;\r
2419                 }\r
2420         }\r
2421 \r
2422         /* Do we need to create an endpoint for this GID? */\r
2423         if( !*pp_src )\r
2424         {\r
2425                 /* Copy the src GID to allow aligned access */\r
2426                 cl_memcpy( &gid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) );\r
2427                 status = ipoib_mac_from_guid( gid.unicast.interface_id, &mac );\r
2428                 if( status != IB_SUCCESS )\r
2429                 {\r
2430                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2431                                 ("ipoib_mac_from_guid returned %s\n",\r
2432                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2433                         return status;\r
2434                 }\r
2435                 /*\r
2436                  * Create the endpoint.  Note that the LID is left blank and will be\r
2437                  * resolved by a path query as needed.  This is done because the\r
2438                  * remote LID/GID from the work completion may not be the original\r
2439                  * initiator.\r
2440                  */\r
2441                 *pp_src = ipoib_endpt_create( &p_ib_arp->src_hw.gid,\r
2442                         0, (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2443 \r
2444                 if( !*pp_src )\r
2445                 {\r
2446                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2447                                 ("ipoib_endpt_create failed\n") );\r
2448                         return status;\r
2449                 }\r
2450 \r
2451                 cl_obj_lock( &p_port->obj );\r
2452                 status = __endpt_mgr_insert( p_port, mac, *pp_src );\r
2453                 if( status != IB_SUCCESS )\r
2454                 {\r
2455                         cl_obj_unlock( &p_port->obj );\r
2456                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2457                                 ("__endpt_mgr_insert return %s \n",\r
2458                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2459                         return status;\r
2460                 }\r
2461 \r
2462                 cl_obj_unlock( &p_port->obj );\r
2463         }\r
2464 \r
2465         CL_ASSERT( !cl_memcmp(\r
2466                 &(*pp_src)->dgid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) ) );\r
2467         CL_ASSERT( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) ||\r
2468                 (*pp_src)->qpn ==\r
2469                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2470         /* Now swizzle the data. */\r
2471         p_arp->hw_type = ARP_HW_TYPE_ETH;\r
2472         p_arp->hw_size = sizeof(mac_addr_t);\r
2473         p_arp->src_hw = (*pp_src)->mac;\r
2474         p_arp->src_ip = p_ib_arp->src_ip;\r
2475 \r
2476         if( cl_memcmp( &p_ib_arp->dst_hw, &null_hw, sizeof(ipoib_hw_addr_t) ) )\r
2477         {\r
2478                 if( cl_memcmp( &p_dst->dgid, &p_ib_arp->dst_hw.gid, sizeof(ib_gid_t) ) )\r
2479                 {\r
2480                         /*\r
2481                          * We received bcast ARP packet that means\r
2482                          * remote port lets everyone know it was changed IP/MAC\r
2483                          * or just activated\r
2484                          */\r
2485 \r
2486                         /* Guy: TODO: Check why this check fails in case of Voltaire IPR */\r
2487 \r
2488                         if ( !ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) &&\r
2489                                  !ib_gid_is_multicast( (const ib_gid_t*)&p_dst->dgid ) )\r
2490                         {\r
2491                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2492                                         ("ARP: is not ARP MCAST\n") );\r
2493                                 return IB_INVALID_SETTING;\r
2494                         }\r
2495 \r
2496                         p_arp->dst_hw = p_port->p_local_endpt->mac;\r
2497                         p_dst->mac = p_port->p_local_endpt->mac;\r
2498                         /*\r
2499                          * we don't care what receiver ip addr is,\r
2500                          * as long as OS' ARP table is global  ???\r
2501                          */\r
2502                         p_arp->dst_ip = (net32_t)0;\r
2503                 }\r
2504                 else /* we've got reply to our ARP request */\r
2505                 {\r
2506                         p_arp->dst_hw = p_dst->mac;\r
2507                         p_arp->dst_ip = p_ib_arp->dst_ip;\r
2508                         CL_ASSERT( p_dst->qpn == \r
2509                                 (p_ib_arp->dst_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2510                 }\r
2511         }\r
2512         else /* we got ARP reqeust */\r
2513         {\r
2514                 cl_memclr( &p_arp->dst_hw, sizeof(mac_addr_t) );\r
2515                 p_arp->dst_ip = p_ib_arp->dst_ip;\r
2516         }\r
2517 \r
2518         /*\r
2519          * Create the ethernet header.  Note that this is done last so that\r
2520          * we have a chance to create a new endpoint.\r
2521          */\r
2522         status = __recv_gen( p_ipoib, p_eth, *pp_src, p_dst );\r
2523         if( status != IB_SUCCESS )\r
2524         {\r
2525                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2526                         ("__recv_gen returned %s.\n", \r
2527                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2528                 return status;\r
2529         }\r
2530 \r
2531         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2532         return IB_SUCCESS;\r
2533 }\r
2534 \r
2535 \r
2536 static ib_api_status_t\r
2537 __recv_mgr_prepare_pkt(\r
2538         IN                              ipoib_port_t* const                     p_port,\r
2539         IN                              ipoib_recv_desc_t* const        p_desc,\r
2540                 OUT                     NDIS_PACKET** const                     pp_packet )\r
2541 {\r
2542         NDIS_STATUS                                                     status;\r
2543         uint32_t                                                        pkt_filter;\r
2544         ip_stat_sel_t                                           type;\r
2545         NDIS_TCP_IP_CHECKSUM_PACKET_INFO        chksum;\r
2546 \r
2547         PERF_DECLARE( GetNdisPkt );\r
2548 \r
2549         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2550 \r
2551         pkt_filter = p_port->p_adapter->packet_filter;\r
2552         /* Check the packet filter. */\r
2553         switch( p_desc->type )\r
2554         {\r
2555         default:\r
2556         case PKT_TYPE_UCAST:\r
2557                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2558                         pkt_filter & NDIS_PACKET_TYPE_ALL_FUNCTIONAL ||\r
2559                         pkt_filter & NDIS_PACKET_TYPE_SOURCE_ROUTING ||\r
2560                         pkt_filter & NDIS_PACKET_TYPE_DIRECTED )\r
2561                 {\r
2562                         /* OK to report. */\r
2563                         type = IP_STAT_UCAST_BYTES;\r
2564                         status = NDIS_STATUS_SUCCESS;\r
2565                 }\r
2566                 else\r
2567                 {\r
2568                         type = IP_STAT_DROPPED;\r
2569                         status = NDIS_STATUS_FAILURE;\r
2570                 }\r
2571                 break;\r
2572         case PKT_TYPE_BCAST:\r
2573                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2574                         pkt_filter & NDIS_PACKET_TYPE_BROADCAST )\r
2575                 {\r
2576                         /* OK to report. */\r
2577                         type = IP_STAT_BCAST_BYTES;\r
2578                         status = NDIS_STATUS_SUCCESS;\r
2579                 }\r
2580                 else\r
2581                 {\r
2582                         type = IP_STAT_DROPPED;\r
2583                         status = NDIS_STATUS_FAILURE;\r
2584                 }\r
2585                 break;\r
2586         case PKT_TYPE_MCAST:\r
2587                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2588                         pkt_filter & NDIS_PACKET_TYPE_ALL_MULTICAST ||\r
2589                         pkt_filter & NDIS_PACKET_TYPE_MULTICAST )\r
2590                 {\r
2591                         /* OK to report. */\r
2592                         type = IP_STAT_MCAST_BYTES;\r
2593                         status = NDIS_STATUS_SUCCESS;\r
2594                 }\r
2595                 else\r
2596                 {\r
2597                         type = IP_STAT_DROPPED;\r
2598                         status = NDIS_STATUS_FAILURE;\r
2599                 }\r
2600                 break;\r
2601         }\r
2602 \r
2603         if( status != NDIS_STATUS_SUCCESS )\r
2604         {\r
2605                 ipoib_inc_recv_stat( p_port->p_adapter, type, 0 );\r
2606                 /* Return the receive descriptor to the pool. */\r
2607                 __buf_mgr_put_recv( p_port, p_desc, NULL );\r
2608                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
2609                         ("Packet filter doesn't match receive.  Dropping.\n") );\r
2610                 /*\r
2611                  * Return IB_NOT_DONE since the packet has been completed,\r
2612                  * but has not consumed an array entry.\r
2613                  */\r
2614                 return IB_NOT_DONE;\r
2615         }\r
2616 \r
2617         cl_perf_start( GetNdisPkt );\r
2618         *pp_packet = __buf_mgr_get_ndis_pkt( p_port, p_desc );\r
2619         cl_perf_stop( &p_port->p_adapter->perf, GetNdisPkt );\r
2620         if( !*pp_packet )\r
2621         {\r
2622                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2623                         ("__buf_mgr_get_ndis_pkt failed\n") );\r
2624                 return IB_INSUFFICIENT_RESOURCES;\r
2625         }\r
2626 \r
2627         chksum.Value = 0;\r
2628         switch (p_port->p_adapter->params.recv_chksum_offload) {\r
2629           case CSUM_DISABLED:\r
2630                 NDIS_PER_PACKET_INFO_FROM_PACKET( *pp_packet, TcpIpChecksumPacketInfo ) =\r
2631                 (void*)(uintn_t)chksum.Value;\r
2632                 break;\r
2633           case CSUM_ENABLED:\r
2634                 /* Get the checksums directly from packet information. */\r
2635                 /* In this case, no one of cheksum's cat get false value */\r
2636                 /* If hardware checksum failed or wasn't calculated, NDIS will recalculate it again */\r
2637                 NDIS_PER_PACKET_INFO_FROM_PACKET( *pp_packet, TcpIpChecksumPacketInfo ) = \r
2638                         (void*)(uintn_t)(p_desc->ndis_csum.Value);\r
2639                 break;\r
2640           case CSUM_BYPASS:\r
2641                 /* Flag the checksums as having been calculated. */\r
2642                 chksum.Receive.NdisPacketTcpChecksumSucceeded = TRUE;\r
2643                 chksum.Receive.NdisPacketUdpChecksumSucceeded = TRUE;\r
2644                 chksum.Receive.NdisPacketIpChecksumSucceeded = TRUE;\r
2645                 NDIS_PER_PACKET_INFO_FROM_PACKET( *pp_packet, TcpIpChecksumPacketInfo ) =\r
2646                 (void*)(uintn_t)chksum.Value;\r
2647                 break;\r
2648           default:\r
2649                 ASSERT(FALSE);\r
2650                 NDIS_PER_PACKET_INFO_FROM_PACKET( *pp_packet, TcpIpChecksumPacketInfo ) =\r
2651                 (void*)(uintn_t)chksum.Value;\r
2652         }\r
2653         ipoib_inc_recv_stat( p_port->p_adapter, type, p_desc->len );\r
2654 \r
2655         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2656         return IB_SUCCESS;\r
2657 }\r
2658 \r
2659 \r
2660 static uint32_t\r
2661 __recv_mgr_build_pkt_array(\r
2662         IN                              ipoib_port_t* const                     p_port,\r
2663         IN                              int32_t                                         shortage,\r
2664                 OUT                     cl_qlist_t* const                       p_done_list,\r
2665                 OUT                     int32_t* const                          p_discarded )\r
2666 {\r
2667         cl_list_item_t                  *p_item;\r
2668         ipoib_recv_desc_t               *p_desc;\r
2669         uint32_t                                i = 0;\r
2670         ib_api_status_t                 status;\r
2671         PERF_DECLARE( PreparePkt );\r
2672 \r
2673         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2674 \r
2675         *p_discarded = 0;\r
2676 \r
2677         /* Move any existing receives to the head to preserve ordering. */\r
2678         cl_qlist_insert_list_head( p_done_list, &p_port->recv_mgr.done_list );\r
2679         p_item = cl_qlist_remove_head( p_done_list );\r
2680         while( p_item != cl_qlist_end( p_done_list ) )\r
2681         {\r
2682                 p_desc = (ipoib_recv_desc_t*)p_item;\r
2683 \r
2684                 cl_perf_start( PreparePkt );\r
2685                 status = __recv_mgr_prepare_pkt( p_port, p_desc,\r
2686                         &p_port->recv_mgr.recv_pkt_array[i] );\r
2687                 cl_perf_stop( &p_port->p_adapter->perf, PreparePkt );\r
2688                 if( status == IB_SUCCESS )\r
2689                 {\r
2690                         CL_ASSERT( p_port->recv_mgr.recv_pkt_array[i] );\r
2691                         if( shortage-- > 0 )\r
2692                         {\r
2693                                 NDIS_SET_PACKET_STATUS(\r
2694                                         p_port->recv_mgr.recv_pkt_array[i], NDIS_STATUS_RESOURCES );\r
2695                         }\r
2696                         else\r
2697                         {\r
2698                                 NDIS_SET_PACKET_STATUS(\r
2699                                         p_port->recv_mgr.recv_pkt_array[i], NDIS_STATUS_SUCCESS );\r
2700                         }\r
2701                         i++;\r
2702                 }\r
2703                 else if( status == IB_NOT_DONE )\r
2704                 {\r
2705                         (*p_discarded)++;\r
2706                 }\r
2707                 else\r
2708                 {\r
2709                         IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
2710                                 ("__recv_mgr_prepare_pkt returned %s\n",\r
2711                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2712                         /* Put all completed receives on the port's done list. */\r
2713                         cl_qlist_insert_tail( &p_port->recv_mgr.done_list, p_item );\r
2714                         cl_qlist_insert_list_tail( &p_port->recv_mgr.done_list, p_done_list );\r
2715                         break;\r
2716                 }\r
2717 \r
2718                 p_item = cl_qlist_remove_head( p_done_list );\r
2719         }\r
2720 \r
2721         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2722         return i;\r
2723 }\r
2724 \r
2725 \r
2726 \r
2727 \r
2728 /******************************************************************************\r
2729 *\r
2730 * Send manager implementation.\r
2731 *\r
2732 ******************************************************************************/\r
2733 static void\r
2734 __send_mgr_construct(\r
2735         IN                              ipoib_port_t* const                     p_port )\r
2736 {\r
2737         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2738         p_port->send_mgr.depth = 0;\r
2739         cl_qlist_init( &p_port->send_mgr.pending_list );\r
2740         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2741 }\r
2742 \r
2743 \r
2744 static void \r
2745 __pending_list_destroy(\r
2746         IN                              ipoib_port_t* const                     p_port )\r
2747 {\r
2748         cl_list_item_t  *p_item;\r
2749         NDIS_PACKET             *p_packet;\r
2750         \r
2751         cl_spinlock_acquire( &p_port->send_lock );\r
2752         /* Complete any pending packets. */\r
2753         for( p_item = cl_qlist_remove_head( &p_port->send_mgr.pending_list );\r
2754                 p_item != cl_qlist_end( &p_port->send_mgr.pending_list );\r
2755                 p_item = cl_qlist_remove_head( &p_port->send_mgr.pending_list ) )\r
2756         {\r
2757                 p_packet = IPOIB_PACKET_FROM_LIST_ITEM( p_item );\r
2758                 NdisMSendCompleteX( p_port->p_adapter->h_adapter, p_packet,\r
2759                         NDIS_STATUS_RESET_IN_PROGRESS );\r
2760         }\r
2761         cl_spinlock_release( &p_port->send_lock );\r
2762 }\r
2763 \r
2764 static void\r
2765 __send_mgr_destroy(\r
2766         IN                              ipoib_port_t* const                     p_port )\r
2767 {\r
2768         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2769         __pending_list_destroy(p_port);\r
2770 \r
2771         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2772 }\r
2773 \r
2774 \r
2775 static NDIS_STATUS\r
2776 __send_mgr_filter(\r
2777         IN                              ipoib_port_t* const                     p_port,\r
2778         IN              const   eth_hdr_t* const                        p_eth_hdr,\r
2779         IN                              NDIS_BUFFER* const                      p_buf,\r
2780         IN                              size_t                                          buf_len,\r
2781         IN      OUT                     ipoib_send_desc_t* const        p_desc )\r
2782 {\r
2783         NDIS_STATUS             status;\r
2784 \r
2785         PERF_DECLARE( FilterIp );\r
2786         PERF_DECLARE( FilterArp );\r
2787         PERF_DECLARE( SendGen );\r
2788 \r
2789         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2790 \r
2791         /*\r
2792          * We already checked the ethernet header length, so we know it's safe\r
2793          * to decrement the buf_len without underflowing.\r
2794          */\r
2795         buf_len -= sizeof(eth_hdr_t);\r
2796 \r
2797         switch( p_eth_hdr->type )\r
2798         {\r
2799         case ETH_PROT_TYPE_IP:\r
2800                 cl_perf_start( FilterIp );\r
2801                 status = __send_mgr_filter_ip(\r
2802                         p_port, p_eth_hdr, p_buf, buf_len, p_desc );\r
2803                 cl_perf_stop( &p_port->p_adapter->perf, FilterIp );\r
2804                 break;\r
2805 \r
2806         case ETH_PROT_TYPE_ARP:\r
2807                 cl_perf_start( FilterArp );\r
2808                 status = __send_mgr_filter_arp(\r
2809                         p_port, p_eth_hdr, p_buf, buf_len, p_desc );\r
2810                 cl_perf_stop( &p_port->p_adapter->perf, FilterArp );\r
2811                 break;\r
2812 \r
2813         default:\r
2814                 /*\r
2815                  * The IPoIB spec doesn't define how to send non IP or ARP packets.\r
2816                  * Just send the payload and hope for the best.\r
2817                  */\r
2818                 cl_perf_start( SendGen );\r
2819                 status = __send_gen( p_port, p_desc, 0 );\r
2820                 cl_perf_stop( &p_port->p_adapter->perf, SendGen );\r
2821                 break;\r
2822         }\r
2823 \r
2824         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2825         return status;\r
2826 }\r
2827 \r
2828 \r
2829 static NDIS_STATUS\r
2830 __send_copy(\r
2831         IN                              ipoib_port_t* const                     p_port,\r
2832         IN                              ipoib_send_desc_t* const        p_desc )\r
2833 {\r
2834         NDIS_PACKET                             *p_packet;\r
2835         NDIS_BUFFER                             *p_buf;\r
2836         NDIS_STATUS                             status;\r
2837         UINT                                    tot_len, bytes_copied = 0;\r
2838 \r
2839         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2840 \r
2841         p_desc->p_buf = \r
2842                 ExAllocateFromNPagedLookasideList( &p_port->buf_mgr.send_buf_list );\r
2843         if( !p_desc->p_buf )\r
2844         {\r
2845                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2846                         ("Failed to allocate buffer for packet copy.\n") );\r
2847                 return NDIS_STATUS_RESOURCES;\r
2848         }\r
2849 \r
2850         NdisAllocatePacket( &status, &p_packet, p_port->buf_mgr.h_send_pkt_pool );\r
2851         if( status != NDIS_STATUS_SUCCESS )\r
2852         {\r
2853                 IPOIB_PRINT_EXIT( TRACE_LEVEL_WARNING, IPOIB_DBG_SEND,\r
2854                         ("Failed to allocate NDIS_PACKET for copy.\n") );\r
2855                 return status;\r
2856         }\r
2857 \r
2858         NdisAllocateBuffer( &status, &p_buf, p_port->buf_mgr.h_send_buf_pool,\r
2859                 p_desc->p_buf, p_port->p_adapter->params.xfer_block_size );\r
2860         if( status != NDIS_STATUS_SUCCESS )\r
2861         {\r
2862                 NdisFreePacket( p_packet );\r
2863                 IPOIB_PRINT_EXIT( TRACE_LEVEL_WARNING, IPOIB_DBG_SEND,\r
2864                         ("Failed to allocate NDIS_BUFFER for copy.\n") );\r
2865                 return status;\r
2866         }\r
2867 \r
2868         NdisChainBufferAtFront( p_packet, p_buf );\r
2869 \r
2870         NdisQueryPacketLength( p_desc->p_pkt, &tot_len );\r
2871 \r
2872         /* Setup the work request. */\r
2873         p_desc->local_ds[1].vaddr = cl_get_physaddr(\r
2874                 ((uint8_t*)p_desc->p_buf) + sizeof(eth_hdr_t) );\r
2875         p_desc->local_ds[1].length = tot_len - sizeof(eth_hdr_t);\r
2876         p_desc->local_ds[1].lkey = p_port->ib_mgr.lkey;\r
2877         p_desc->wr.num_ds = 2;\r
2878 \r
2879         /* Copy the packet. */\r
2880         NdisCopyFromPacketToPacketSafe( p_packet, bytes_copied, tot_len,\r
2881                 p_desc->p_pkt, bytes_copied, &bytes_copied,\r
2882                 NormalPagePriority );\r
2883 \r
2884         /* Free our temp packet now that the data is copied. */\r
2885         NdisUnchainBufferAtFront( p_packet, &p_buf );\r
2886         NdisFreeBuffer( p_buf );\r
2887         NdisFreePacket( p_packet );\r
2888 \r
2889         if( bytes_copied != tot_len )\r
2890         {\r
2891                 /* Something went wrong.  Drop the packet. */\r
2892                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2893                         ("Failed to copy full packet: %d of %d bytes copied.\n",\r
2894                         bytes_copied, tot_len) );\r
2895                 return NDIS_STATUS_RESOURCES;\r
2896         }\r
2897 \r
2898         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2899         return NDIS_STATUS_SUCCESS;\r
2900 }\r
2901 \r
2902 \r
2903 #if !IPOIB_USE_DMA\r
2904 /* Send using the MDL's page information rather than the SGL. */\r
2905 static ib_api_status_t\r
2906 __send_gen(\r
2907         IN                              ipoib_port_t* const                     p_port,\r
2908         IN                              ipoib_send_desc_t* const        p_desc )\r
2909 {\r
2910         uint32_t                                i, j = 1;\r
2911         ULONG                                   offset;\r
2912         MDL                                             *p_mdl;\r
2913         UINT                                    num_pages, tot_len;\r
2914         ULONG                                   buf_len;\r
2915         PPFN_NUMBER                             page_array;\r
2916         boolean_t                               hdr_done = FALSE;\r
2917         ib_api_status_t                 status;\r
2918 \r
2919         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2920 \r
2921         NdisQueryPacket( p_desc->p_pkt, &num_pages, NULL, &p_mdl,\r
2922                 &tot_len );\r
2923 \r
2924         if( !p_mdl )\r
2925         {\r
2926                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2927                         ("No buffers associated with packet.\n") );\r
2928                 return IB_ERROR;\r
2929         }\r
2930 \r
2931         /* Remember that one of the DS entries is reserved for the IPoIB header. */\r
2932         if( num_pages >= MAX_SEND_SGE )\r
2933         {\r
2934                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_SEND,\r
2935                         ("Too many buffers to fit in WR ds_array.  Copying data.\n") );\r
2936                 status = __send_copy( p_port, p_desc );\r
2937                 IPOIB_EXIT( IPOIB_DBG_SEND );\r
2938                 return status;\r
2939         }\r
2940 \r
2941         CL_ASSERT( tot_len > sizeof(eth_hdr_t) );\r
2942         CL_ASSERT( tot_len <= p_port->p_adapter->params.xfer_block_size );\r
2943         /*\r
2944          * Assume that the ethernet header is always fully contained\r
2945          * in the first page of the first MDL.  This makes for much\r
2946          * simpler code.\r
2947          */\r
2948         offset = MmGetMdlByteOffset( p_mdl ) + sizeof(eth_hdr_t);\r
2949         CL_ASSERT( offset <= PAGE_SIZE );\r
2950 \r
2951         while( tot_len )\r
2952         {\r
2953                 buf_len = MmGetMdlByteCount( p_mdl );\r
2954                 page_array = MmGetMdlPfnArray( p_mdl );\r
2955                 CL_ASSERT( page_array );\r
2956                 i = 0;\r
2957                 if( !hdr_done )\r
2958                 {\r
2959                         CL_ASSERT( buf_len >= sizeof(eth_hdr_t) );\r
2960                         /* Skip the ethernet header. */\r
2961                         buf_len -= sizeof(eth_hdr_t);\r
2962                         CL_ASSERT( buf_len <= p_port->p_adapter->params.payload_mtu );\r
2963                         if( buf_len )\r
2964                         {\r
2965                                 /* The ethernet header is a subset of this MDL. */\r
2966                                 CL_ASSERT( i == 0 );\r
2967                                 if( offset < PAGE_SIZE )\r
2968                                 {\r
2969                                         p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2970                                         p_desc->local_ds[j].vaddr = (page_array[i] << PAGE_SHIFT);\r
2971                                         /* Add the byte offset since we're on the 1st page. */\r
2972                                         p_desc->local_ds[j].vaddr += offset;\r
2973                                         if( offset + buf_len > PAGE_SIZE )\r
2974                                         {\r
2975                                                 p_desc->local_ds[j].length = PAGE_SIZE - offset;\r
2976                                                 buf_len -= p_desc->local_ds[j].length;\r
2977                                         }\r
2978                                         else\r
2979                                         {\r
2980                                                 p_desc->local_ds[j].length = buf_len;\r
2981                                                 buf_len = 0;\r
2982                                         }\r
2983                                         /* This data segment is done.  Move to the next. */\r
2984                                         j++;\r
2985                                 }\r
2986                                 /* This page is done.  Move to the next. */\r
2987                                 i++;\r
2988                         }\r
2989                         /* Done handling the ethernet header. */\r
2990                         hdr_done = TRUE;\r
2991                 }\r
2992 \r
2993                 /* Finish this MDL */\r
2994                 while( buf_len )\r
2995                 {\r
2996                         p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2997                         p_desc->local_ds[j].vaddr = (page_array[i] << PAGE_SHIFT);\r
2998                         /* Add the first page's offset if we're on the first page. */\r
2999                         if( i == 0 )\r
3000                                 p_desc->local_ds[j].vaddr += MmGetMdlByteOffset( p_mdl );\r
3001 \r
3002                         if( i == 0 && (MmGetMdlByteOffset( p_mdl ) + buf_len) > PAGE_SIZE )\r