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