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