[IPOIB] bug fix: free pending list in ipoib_port_down
[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 %s\n", cl_status_text[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 %s\n", cl_status_text[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 %s\n", cl_status_text[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 %s\n", cl_status_text[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 %s\n",\r
1002                         cl_status_text[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                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1779                                         ("__endpt_mgr_insert returned %s\n",\r
1780                                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
1781                                 return;\r
1782                         }\r
1783                         cl_obj_unlock( &p_port->obj );\r
1784                         cl_perf_stop( &p_port->p_adapter->perf, EndptInsert );\r
1785                 }\r
1786         }\r
1787         else\r
1788         {\r
1789                 /*\r
1790                  * Lookup the remote endpoint based on LID.  Note that only\r
1791                  * unicast traffic can be LID routed.\r
1792                  */\r
1793                 cl_perf_start( GetEndptByLid );\r
1794                 *pp_src = __endpt_mgr_get_by_lid( p_port, p_wc->recv.ud.remote_lid );\r
1795                 cl_perf_stop( &p_port->p_adapter->perf, GetEndptByLid );\r
1796                 *pp_dst = p_port->p_local_endpt;\r
1797                 CL_ASSERT( *pp_dst );\r
1798         }\r
1799 \r
1800         if( *pp_src && !ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) &&\r
1801                 (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
1802         {\r
1803                 /* Update the QPN for the endpoint. */\r
1804                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1805                         ("Updating QPN for MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
1806                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
1807                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
1808                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5]) );\r
1809                 (*pp_src)->qpn = p_wc->recv.ud.remote_qp;\r
1810         }\r
1811 \r
1812         if( *pp_src && *pp_dst )\r
1813         {\r
1814                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1815                         ("Recv:\n"\r
1816                         "\tsrc MAC: %02X-%02X-%02X-%02X-%02X-%02X\n"\r
1817                         "\tdst MAC: %02X-%02X-%02X-%02X-%02X-%02X\n",\r
1818                         (*pp_src )->mac.addr[0], (*pp_src )->mac.addr[1],\r
1819                         (*pp_src )->mac.addr[2], (*pp_src )->mac.addr[3],\r
1820                         (*pp_src )->mac.addr[4], (*pp_src )->mac.addr[5],\r
1821                         (*pp_dst )->mac.addr[0], (*pp_dst )->mac.addr[1],\r
1822                         (*pp_dst )->mac.addr[2], (*pp_dst )->mac.addr[3],\r
1823                         (*pp_dst )->mac.addr[4], (*pp_dst )->mac.addr[5]) );\r
1824         }\r
1825 \r
1826         IPOIB_EXIT( IPOIB_DBG_RECV );\r
1827 }\r
1828 \r
1829 \r
1830 static int32_t\r
1831 __recv_mgr_filter(\r
1832         IN                              ipoib_port_t* const                     p_port,\r
1833         IN                              ib_wc_t* const                          p_done_wc_list,\r
1834                 OUT                     cl_qlist_t* const                       p_done_list,\r
1835                 OUT                     cl_qlist_t* const                       p_bad_list )\r
1836 {\r
1837         ipoib_recv_desc_t               *p_desc;\r
1838         ib_wc_t                                 *p_wc;\r
1839         ipoib_pkt_t                             *p_ipoib;\r
1840         eth_pkt_t                               *p_eth;\r
1841         ipoib_endpt_t                   *p_src, *p_dst;\r
1842         ib_api_status_t                 status;\r
1843         uint32_t                                len;\r
1844         int32_t                                 recv_cnt = 0;\r
1845         PERF_DECLARE( GetRecvEndpts );\r
1846         PERF_DECLARE( RecvGen );\r
1847         PERF_DECLARE( RecvTcp );\r
1848         PERF_DECLARE( RecvUdp );\r
1849         PERF_DECLARE( RecvDhcp );\r
1850         PERF_DECLARE( RecvArp );\r
1851 \r
1852         IPOIB_ENTER( IPOIB_DBG_RECV );\r
1853 \r
1854         for( p_wc = p_done_wc_list; p_wc; p_wc = p_wc->p_next )\r
1855         {\r
1856                 CL_ASSERT( p_wc->wc_type == IB_WC_RECV );\r
1857                 p_desc = (ipoib_recv_desc_t*)(uintn_t)p_wc->wr_id;\r
1858                 recv_cnt++;\r
1859 \r
1860                 if( p_wc->status != IB_WCS_SUCCESS )\r
1861                 {\r
1862                         if( p_wc->status != IB_WCS_WR_FLUSHED_ERR )\r
1863                         {\r
1864                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1865                                         ("Failed completion %s  (vendor specific %#x)\n",\r
1866                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status ),\r
1867                                         (int)p_wc->vendor_specific) );\r
1868                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1869                         }\r
1870                         else\r
1871                         {\r
1872                                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
1873                                         ("Flushed completion %s\n",\r
1874                                         p_port->p_adapter->p_ifc->get_wc_status_str( p_wc->status )) );\r
1875                                 ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_DROPPED, 0 );\r
1876                         }\r
1877                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1878                         /* Dereference the port object on behalf of the failed receive. */\r
1879                         ipoib_port_deref( p_port, ref_failed_recv_wc );\r
1880                         continue;\r
1881                 }\r
1882 \r
1883                 len = p_wc->length - sizeof(ib_grh_t);\r
1884 \r
1885                 if( len < sizeof(ipoib_hdr_t) )\r
1886                 {\r
1887                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1888                                 ("Received ETH packet < min size\n") );\r
1889                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1890                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1891                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
1892                         continue;\r
1893                 }\r
1894 \r
1895                 if((len - sizeof(ipoib_hdr_t)) > p_port->p_adapter->params.payload_mtu)\r
1896                 {\r
1897                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1898                                 ("Received ETH packet > payload MTU (%d)\n",\r
1899                                 p_port->p_adapter->params.payload_mtu) );\r
1900                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
1901                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1902                         ipoib_port_deref( p_port, ref_recv_inv_len );\r
1903                         continue;\r
1904                         \r
1905                 }\r
1906                 /* Successful completion.  Get the receive information. */\r
1907                 cl_perf_start( GetRecvEndpts );\r
1908                 __recv_get_endpts( p_port, p_desc, p_wc, &p_src, &p_dst );\r
1909                 cl_perf_stop( &p_port->p_adapter->perf, GetRecvEndpts );\r
1910 \r
1911 #if IPOIB_INLINE_RECV\r
1912                 p_ipoib = &p_desc->buf.ib.pkt;\r
1913                 p_eth = &p_desc->buf.eth.pkt;\r
1914 #else   /* IPOIB_INLINE_RECV */\r
1915                 p_ipoib = &p_desc->p_buf->ib.pkt;\r
1916                 p_eth = &p_desc->p_buf->eth.pkt;\r
1917 #endif  /*IPOIB_INLINE_RECV */\r
1918 \r
1919                 if( p_src )\r
1920                 {\r
1921                         /* Don't report loopback traffic - we requested SW loopback. */\r
1922                         if( !cl_memcmp( &p_port->p_adapter->params.conf_mac,\r
1923                                 &p_src->mac, sizeof(p_port->p_adapter->params.conf_mac) ) )\r
1924                         {\r
1925                                 /*\r
1926                                  * "This is not the packet you're looking for" - don't update\r
1927                                  * receive statistics, the packet never happened.\r
1928                                  */\r
1929                                 cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
1930                                 /* Dereference the port object on behalf of the failed recv. */\r
1931                                 ipoib_port_deref( p_port, ref_recv_loopback );\r
1932                                 continue;\r
1933                         }\r
1934                 }\r
1935 \r
1936                 switch( p_ipoib->hdr.type )\r
1937                 {\r
1938                 case ETH_PROT_TYPE_IP:\r
1939                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t)) )\r
1940                         {\r
1941                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1942                                         ("Received IP packet < min size\n") );\r
1943                                 status = IB_INVALID_SETTING;\r
1944                                 break;\r
1945                         }\r
1946 \r
1947                         if( p_ipoib->type.ip.hdr.offset ||\r
1948                                 p_ipoib->type.ip.hdr.prot != IP_PROT_UDP )\r
1949                         {\r
1950                                 /* Unfiltered.  Setup the ethernet header and report. */\r
1951                                 cl_perf_start( RecvTcp );\r
1952                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
1953                                 cl_perf_stop( &p_port->p_adapter->perf, RecvTcp );\r
1954                                 break;\r
1955                         }\r
1956 \r
1957                         /* First packet of a UDP transfer. */\r
1958                         if( len <\r
1959                                 (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) + sizeof(udp_hdr_t)) )\r
1960                         {\r
1961                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1962                                         ("Received UDP packet < min size\n") );\r
1963                                 status = IB_INVALID_SETTING;\r
1964                                 break;\r
1965                         }\r
1966 \r
1967                         /* Check if DHCP conversion is required. */\r
1968                         if( (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_SERVER &&\r
1969                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_CLIENT) ||\r
1970                                 (p_ipoib->type.ip.prot.udp.hdr.dst_port == DHCP_PORT_CLIENT &&\r
1971                                 p_ipoib->type.ip.prot.udp.hdr.src_port == DHCP_PORT_SERVER) )\r
1972                         {\r
1973                                 if( len < (sizeof(ipoib_hdr_t) + sizeof(ip_hdr_t) +\r
1974                                         sizeof(udp_hdr_t) + DHCP_MIN_SIZE) )\r
1975                                 {\r
1976                                         IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1977                                                 ("Received DHCP < min size\n") );\r
1978                                         status = IB_INVALID_SETTING;\r
1979                                         break;\r
1980                                 }\r
1981                                 /* UDP packet with BOOTP ports in src/dst port numbers. */\r
1982                                 cl_perf_start( RecvDhcp );\r
1983                                 status = __recv_dhcp( p_port, p_ipoib, p_eth, p_src, p_dst );\r
1984                                 cl_perf_stop( &p_port->p_adapter->perf, RecvDhcp );\r
1985                         }\r
1986                         else\r
1987                         {\r
1988                                 /* Unfiltered.  Setup the ethernet header and report. */\r
1989                                 cl_perf_start( RecvUdp );\r
1990                                 status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
1991                                 cl_perf_stop( &p_port->p_adapter->perf, RecvUdp );\r
1992                         }\r
1993                         break;\r
1994 \r
1995                 case ETH_PROT_TYPE_ARP:\r
1996                         if( len < (sizeof(ipoib_hdr_t) + sizeof(ipoib_arp_pkt_t)) )\r
1997                         {\r
1998                                 IPOIB_PRINT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
1999                                         ("Received ARP < min size\n") );\r
2000                                 status = IB_INVALID_SETTING;\r
2001                                 break;\r
2002                         }\r
2003                         cl_perf_start( RecvArp );\r
2004                         status = __recv_arp( p_port, p_wc, p_ipoib, p_eth, &p_src, p_dst );\r
2005                         cl_perf_stop( &p_port->p_adapter->perf, RecvArp );\r
2006                         len = sizeof(ipoib_hdr_t) + sizeof(arp_pkt_t);\r
2007                         break;\r
2008 \r
2009                 default:\r
2010                         /* Unfiltered.  Setup the ethernet header and report. */\r
2011                         cl_perf_start( RecvGen );\r
2012                         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2013                         cl_perf_stop( &p_port->p_adapter->perf, RecvGen );\r
2014                 }\r
2015 \r
2016                 if( status != IB_SUCCESS )\r
2017                 {\r
2018                         /* Update stats. */\r
2019                         ipoib_inc_recv_stat( p_port->p_adapter, IP_STAT_ERROR, 0 );\r
2020                         cl_qlist_insert_tail( p_bad_list, &p_desc->item.list_item );\r
2021                         /* Dereference the port object on behalf of the failed receive. */\r
2022                         ipoib_port_deref( p_port, ref_recv_filter );\r
2023                 }\r
2024                 else\r
2025                 {\r
2026                         p_desc->len =\r
2027                                 len + sizeof(eth_hdr_t) - sizeof(ipoib_hdr_t);\r
2028                         if( p_dst->h_mcast)\r
2029                         {\r
2030                                 if( p_dst->dgid.multicast.raw_group_id[10] == 0xFF &&\r
2031                                         p_dst->dgid.multicast.raw_group_id[11] == 0xFF &&\r
2032                                         p_dst->dgid.multicast.raw_group_id[12] == 0xFF &&\r
2033                                         p_dst->dgid.multicast.raw_group_id[13] == 0xFF )\r
2034                                 {\r
2035                                         p_desc->type = PKT_TYPE_BCAST;\r
2036                                 }\r
2037                                 else\r
2038                                 {\r
2039                                         p_desc->type = PKT_TYPE_MCAST;\r
2040                                 }\r
2041                         }\r
2042                         else\r
2043                         {\r
2044                                 p_desc->type = PKT_TYPE_UCAST;\r
2045                         }\r
2046                         cl_qlist_insert_tail( p_done_list, &p_desc->item.list_item );\r
2047                 }\r
2048         }\r
2049 \r
2050         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2051         return recv_cnt;\r
2052 }\r
2053 \r
2054 \r
2055 static ib_api_status_t\r
2056 __recv_gen(\r
2057         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2058                 OUT                     eth_pkt_t* const                        p_eth,\r
2059         IN                              ipoib_endpt_t* const            p_src,\r
2060         IN                              ipoib_endpt_t* const            p_dst )\r
2061 {\r
2062         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2063 \r
2064         if( !p_src || !p_dst )\r
2065         {\r
2066                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2067                         ("Received packet with no matching endpoints.\n") );\r
2068                 return IB_NOT_DONE;\r
2069         }\r
2070 \r
2071         /*\r
2072          * Fill in the ethernet header.  Note that doing so will overwrite\r
2073          * the IPoIB header, so start by moving the information from the IPoIB\r
2074          * header.\r
2075          */\r
2076         p_eth->hdr.type = p_ipoib->hdr.type;\r
2077         p_eth->hdr.src = p_src->mac;\r
2078         p_eth->hdr.dst = p_dst->mac;\r
2079 \r
2080         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2081         return IB_SUCCESS;\r
2082 }\r
2083 \r
2084 \r
2085 static ib_api_status_t\r
2086 __recv_dhcp(\r
2087         IN                              ipoib_port_t* const                     p_port,\r
2088         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2089                 OUT                     eth_pkt_t* const                        p_eth,\r
2090         IN                              ipoib_endpt_t* const            p_src,\r
2091         IN                              ipoib_endpt_t* const            p_dst )\r
2092 {\r
2093         ib_api_status_t         status;\r
2094         dhcp_pkt_t                      *p_dhcp;\r
2095         uint8_t                         *p_option;\r
2096         uint8_t                         *p_cid = NULL;\r
2097         ib_gid_t                        gid;\r
2098         uint8_t                         msg = 0;\r
2099 \r
2100         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2101 \r
2102         UNUSED_PARAM( p_port );\r
2103 \r
2104         /* Create the ethernet header. */\r
2105         status = __recv_gen( p_ipoib, p_eth, p_src, p_dst );\r
2106         if( status != IB_SUCCESS )\r
2107         {\r
2108                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2109                         ("__recv_gen returned %s.\n", \r
2110                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2111                 return status;\r
2112         }\r
2113 \r
2114         /* Fixup the payload. */\r
2115         p_dhcp = &p_eth->type.ip.prot.udp.dhcp;\r
2116         if( p_dhcp->op != DHCP_REQUEST && p_dhcp->op != DHCP_REPLY )\r
2117         {\r
2118                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2119                         ("Invalid DHCP op code.\n") );\r
2120                 return IB_INVALID_SETTING;\r
2121         }\r
2122 \r
2123         /*\r
2124          * Find the client identifier option, making sure to skip\r
2125          * the "magic cookie".\r
2126          */\r
2127         p_option = &p_dhcp->options[0];\r
2128         if ( *(uint32_t *)p_option != DHCP_COOKIE )\r
2129         {\r
2130                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2131                         ("DHCP cookie corrupted.\n") );\r
2132                 return IB_INVALID_PARAMETER;\r
2133         }\r
2134 \r
2135         p_option = &p_dhcp->options[4];\r
2136         while( *p_option != DHCP_OPT_END )\r
2137         {\r
2138                 switch( *p_option )\r
2139                 {\r
2140                 case DHCP_OPT_PAD:\r
2141                         p_option++;\r
2142                         break;\r
2143 \r
2144                 case DHCP_OPT_MSG:\r
2145                         msg = p_option[2];\r
2146                         p_option += 3;\r
2147                         break;\r
2148 \r
2149                 case DHCP_OPT_CLIENT_ID:\r
2150                         p_cid = p_option;\r
2151                         /* Fall through. */\r
2152 \r
2153                 default:\r
2154                         /*\r
2155                          * All other options have a length byte following the option code.\r
2156                          * Offset by the length to get to the next option.\r
2157                          */\r
2158                         p_option += (p_option[1] + 2);\r
2159                 }\r
2160         }\r
2161 \r
2162         switch( msg )\r
2163         {\r
2164         /* message from client */\r
2165         case DHCPDISCOVER:\r
2166         case DHCPREQUEST:\r
2167         case DHCPDECLINE:\r
2168         case DHCPRELEASE:\r
2169         case DHCPINFORM:\r
2170                 if( !p_cid )\r
2171                 {\r
2172                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2173                                 ("Failed to find required Client-identifier option.\n") );\r
2174                         return IB_INVALID_SETTING;\r
2175                 }\r
2176                 if( p_dhcp->htype != DHCP_HW_TYPE_IB )\r
2177                 {\r
2178                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2179                                 ("Invalid hardware address type.\n") );\r
2180                         return IB_INVALID_SETTING;\r
2181                 }\r
2182                 break;\r
2183         /* message from DHCP server */\r
2184         case DHCPOFFER:\r
2185         case DHCPACK:\r
2186         case DHCPNAK:\r
2187                 break;\r
2188 \r
2189         default:\r
2190                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2191                         ("Invalide message type.\n") );\r
2192                 return IB_INVALID_PARAMETER;\r
2193         }\r
2194         p_eth->type.ip.prot.udp.hdr.chksum = 0;\r
2195         p_dhcp->htype = DHCP_HW_TYPE_ETH;\r
2196         p_dhcp->hlen = HW_ADDR_LEN;\r
2197 \r
2198         if( p_cid ) /* from client */\r
2199         {\r
2200                 /* Validate that the length and type of the option is as required. */\r
2201                 if( p_cid[1] != 21 )\r
2202                 {\r
2203                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2204                                 ("Client-identifier length not 21 as required.\n") );\r
2205                         return IB_INVALID_SETTING;\r
2206                 }\r
2207                 if( p_cid[2] != DHCP_HW_TYPE_IB )\r
2208                 {\r
2209                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2210                                 ("Client-identifier type is wrong.\n") );\r
2211                         return IB_INVALID_SETTING;\r
2212                 }\r
2213                 /*\r
2214                  * Copy the GID value from the option so that we can make aligned\r
2215                  * accesses to the contents.\r
2216                  * Recover CID to standard type.\r
2217                  */\r
2218                 cl_memcpy( &gid, &p_cid[7], sizeof(ib_gid_t) );\r
2219                 p_cid[1] =  HW_ADDR_LEN +1;// CID length \r
2220                 p_cid[2] =  DHCP_HW_TYPE_ETH;// CID type \r
2221                 status = ipoib_mac_from_guid( gid.unicast.interface_id, (mac_addr_t*)&p_cid[3] );\r
2222                 p_cid[HW_ADDR_LEN + 3] = DHCP_OPT_END; //terminate tag\r
2223         }\r
2224         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2225         return status;\r
2226 }\r
2227 \r
2228 \r
2229 static ib_api_status_t\r
2230 __recv_arp(\r
2231         IN                              ipoib_port_t* const                     p_port,\r
2232         IN                              ib_wc_t* const                          p_wc,\r
2233         IN              const   ipoib_pkt_t* const                      p_ipoib,\r
2234                 OUT                     eth_pkt_t* const                        p_eth,\r
2235         IN                              ipoib_endpt_t** const           pp_src,\r
2236         IN                              ipoib_endpt_t* const            p_dst )\r
2237 {\r
2238         ib_api_status_t                 status;\r
2239         arp_pkt_t                               *p_arp;\r
2240         const ipoib_arp_pkt_t   *p_ib_arp;\r
2241         ib_gid_t                                gid;\r
2242         mac_addr_t                              mac;\r
2243         ipoib_hw_addr_t                 null_hw = {0};\r
2244 \r
2245         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2246 \r
2247         if( !p_dst )\r
2248         {\r
2249                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2250                         ("Unknown destination endpoint\n") );\r
2251                 return IB_INVALID_SETTING;\r
2252         }\r
2253 \r
2254         p_ib_arp = &p_ipoib->type.arp;\r
2255         p_arp = &p_eth->type.arp;\r
2256 \r
2257         if( p_ib_arp->hw_type != ARP_HW_TYPE_IB )\r
2258         {\r
2259                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2260                         ("ARP hardware type is not IB\n") );\r
2261                 return IB_INVALID_SETTING;\r
2262         }\r
2263 \r
2264         if( p_ib_arp->hw_size != sizeof(ipoib_hw_addr_t) )\r
2265         {\r
2266                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2267                         ("ARP hardware address size is not sizeof(ipoib_hw_addr_t)\n") );\r
2268                 return IB_INVALID_SETTING;\r
2269         }\r
2270 \r
2271         if( p_ib_arp->prot_type != ETH_PROT_TYPE_IP )\r
2272         {\r
2273                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2274                         ("ARP protocal type not IP\n") );\r
2275                 return IB_INVALID_SETTING;\r
2276         }\r
2277 \r
2278         /*\r
2279          * If we don't have a source, lookup the endpoint specified in the payload.\r
2280          */\r
2281         if( !*pp_src )\r
2282                 *pp_src = __endpt_mgr_get_by_gid( p_port, &p_ib_arp->src_hw.gid );\r
2283 \r
2284         /*\r
2285          * If the endpoint exists for the GID, make sure\r
2286          * the dlid and qpn match the arp.\r
2287          */\r
2288         if( *pp_src )\r
2289         {\r
2290                 if( cl_memcmp( &(*pp_src)->dgid, &p_ib_arp->src_hw.gid,\r
2291                         sizeof(ib_gid_t) ) )\r
2292                 {\r
2293                         /*\r
2294                          * GIDs for the endpoint are different.  The ARP must\r
2295                          * have been proxied.  Dereference it.\r
2296                          */\r
2297                         *pp_src = NULL;\r
2298                 }\r
2299                 else if( (*pp_src)->dlid &&\r
2300                         (*pp_src)->dlid != p_wc->recv.ud.remote_lid )\r
2301                 {\r
2302                         /* Out of date!  Destroy the endpoint and replace it. */\r
2303                         __endpt_mgr_remove( p_port, *pp_src );\r
2304                         *pp_src = NULL;\r
2305                 }\r
2306                 else if( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) )\r
2307                 {\r
2308                         if( (*pp_src)->qpn !=\r
2309                                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) &&\r
2310                                 p_wc->recv.ud.remote_qp !=\r
2311                                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) )\r
2312                         {\r
2313                                 /* Out of date!  Destroy the endpoint and replace it. */\r
2314                                 __endpt_mgr_remove( p_port, *pp_src );\r
2315                                 *pp_src = NULL;\r
2316                         }\r
2317                 }\r
2318                 else if( (*pp_src)->qpn != p_wc->recv.ud.remote_qp )\r
2319                 {\r
2320                         /* Out of date!  Destroy the endpoint and replace it. */\r
2321                         __endpt_mgr_remove( p_port, *pp_src );\r
2322                         *pp_src = NULL;\r
2323                 }\r
2324         }\r
2325 \r
2326         /* Do we need to create an endpoint for this GID? */\r
2327         if( !*pp_src )\r
2328         {\r
2329                 /* Copy the src GID to allow aligned access */\r
2330                 cl_memcpy( &gid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) );\r
2331                 status = ipoib_mac_from_guid( gid.unicast.interface_id, &mac );\r
2332                 if( status != IB_SUCCESS )\r
2333                 {\r
2334                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2335                                 ("ipoib_mac_from_guid returned %s\n",\r
2336                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2337                         return status;\r
2338                 }\r
2339                 /*\r
2340                  * Create the endpoint.  Note that the LID is left blank and will be\r
2341                  * resolved by a path query as needed.  This is done because the\r
2342                  * remote LID/GID from the work completion may not be the original\r
2343                  * initiator.\r
2344                  */\r
2345                 *pp_src = ipoib_endpt_create( &p_ib_arp->src_hw.gid,\r
2346                         0, (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2347 \r
2348                 if( !*pp_src )\r
2349                 {\r
2350                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2351                                 ("ipoib_endpt_create failed\n") );\r
2352                         return status;\r
2353                 }\r
2354 \r
2355                 cl_obj_lock( &p_port->obj );\r
2356                 status = __endpt_mgr_insert( p_port, mac, *pp_src );\r
2357                 if( status != IB_SUCCESS )\r
2358                 {\r
2359                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2360                                 ("__endpt_mgr_insert return %s \n",\r
2361                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2362                         return status;\r
2363                 }\r
2364 \r
2365                 cl_obj_unlock( &p_port->obj );\r
2366         }\r
2367 \r
2368         CL_ASSERT( !cl_memcmp(\r
2369                 &(*pp_src)->dgid, &p_ib_arp->src_hw.gid, sizeof(ib_gid_t) ) );\r
2370         CL_ASSERT( ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) ||\r
2371                 (*pp_src)->qpn ==\r
2372                 (p_ib_arp->src_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2373         /* Now swizzle the data. */\r
2374         p_arp->hw_type = ARP_HW_TYPE_ETH;\r
2375         p_arp->hw_size = sizeof(mac_addr_t);\r
2376         p_arp->src_hw = (*pp_src)->mac;\r
2377         p_arp->src_ip = p_ib_arp->src_ip;\r
2378 \r
2379         if( cl_memcmp( &p_ib_arp->dst_hw, &null_hw, sizeof(ipoib_hw_addr_t) ) )\r
2380         {\r
2381                 if( cl_memcmp( &p_dst->dgid, &p_ib_arp->dst_hw.gid, sizeof(ib_gid_t) ) )\r
2382                 {\r
2383                         /*\r
2384                          * We received bcast ARP packet that means\r
2385                          * remote port lets everyone know it was changed IP/MAC\r
2386                          * or just activated\r
2387                          */\r
2388 \r
2389                         /* Guy: TODO: Check why this check fails in case of Voltaire IPR */\r
2390 \r
2391                         if ( !ipoib_is_voltaire_router_gid( &(*pp_src)->dgid ) &&\r
2392                                  !ib_gid_is_multicast( (const ib_gid_t*)&p_dst->dgid ) )\r
2393                         {\r
2394                                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2395                                         ("ARP: is not ARP MCAST\n") );\r
2396                                 return IB_INVALID_SETTING;\r
2397                         }\r
2398 \r
2399                         p_arp->dst_hw = p_port->p_local_endpt->mac;\r
2400                         p_dst->mac = p_port->p_local_endpt->mac;\r
2401                         /*\r
2402                          * we don't care what receiver ip addr is,\r
2403                          * as long as OS' ARP table is global  ???\r
2404                          */\r
2405                         p_arp->dst_ip = (net32_t)0;\r
2406                 }\r
2407                 else /* we've got reply to our ARP request */\r
2408                 {\r
2409                         p_arp->dst_hw = p_dst->mac;\r
2410                         p_arp->dst_ip = p_ib_arp->dst_ip;\r
2411                         CL_ASSERT( p_dst->qpn == \r
2412                                 (p_ib_arp->dst_hw.flags_qpn & CL_HTON32(0x00FFFFFF)) );\r
2413                 }\r
2414         }\r
2415         else /* we got ARP reqeust */\r
2416         {\r
2417                 cl_memclr( &p_arp->dst_hw, sizeof(mac_addr_t) );\r
2418                 p_arp->dst_ip = p_ib_arp->dst_ip;\r
2419         }\r
2420 \r
2421         /*\r
2422          * Create the ethernet header.  Note that this is done last so that\r
2423          * we have a chance to create a new endpoint.\r
2424          */\r
2425         status = __recv_gen( p_ipoib, p_eth, *pp_src, p_dst );\r
2426         if( status != IB_SUCCESS )\r
2427         {\r
2428                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2429                         ("__recv_gen returned %s.\n", \r
2430                         p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2431                 return status;\r
2432         }\r
2433 \r
2434         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2435         return IB_SUCCESS;\r
2436 }\r
2437 \r
2438 \r
2439 static ib_api_status_t\r
2440 __recv_mgr_prepare_pkt(\r
2441         IN                              ipoib_port_t* const                     p_port,\r
2442         IN                              ipoib_recv_desc_t* const        p_desc,\r
2443                 OUT                     NDIS_PACKET** const                     pp_packet )\r
2444 {\r
2445         NDIS_STATUS                                                     status;\r
2446         uint32_t                                                        pkt_filter;\r
2447         ip_stat_sel_t                                           type;\r
2448         NDIS_TCP_IP_CHECKSUM_PACKET_INFO        chksum;\r
2449         PERF_DECLARE( GetNdisPkt );\r
2450 \r
2451         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2452 \r
2453         pkt_filter = p_port->p_adapter->packet_filter;\r
2454         /* Check the packet filter. */\r
2455         switch( p_desc->type )\r
2456         {\r
2457         default:\r
2458         case PKT_TYPE_UCAST:\r
2459                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2460                         pkt_filter & NDIS_PACKET_TYPE_ALL_FUNCTIONAL ||\r
2461                         pkt_filter & NDIS_PACKET_TYPE_SOURCE_ROUTING ||\r
2462                         pkt_filter & NDIS_PACKET_TYPE_DIRECTED )\r
2463                 {\r
2464                         /* OK to report. */\r
2465                         type = IP_STAT_UCAST_BYTES;\r
2466                         status = NDIS_STATUS_SUCCESS;\r
2467                 }\r
2468                 else\r
2469                 {\r
2470                         type = IP_STAT_DROPPED;\r
2471                         status = NDIS_STATUS_FAILURE;\r
2472                 }\r
2473                 break;\r
2474         case PKT_TYPE_BCAST:\r
2475                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2476                         pkt_filter & NDIS_PACKET_TYPE_BROADCAST )\r
2477                 {\r
2478                         /* OK to report. */\r
2479                         type = IP_STAT_BCAST_BYTES;\r
2480                         status = NDIS_STATUS_SUCCESS;\r
2481                 }\r
2482                 else\r
2483                 {\r
2484                         type = IP_STAT_DROPPED;\r
2485                         status = NDIS_STATUS_FAILURE;\r
2486                 }\r
2487                 break;\r
2488         case PKT_TYPE_MCAST:\r
2489                 if( pkt_filter & NDIS_PACKET_TYPE_PROMISCUOUS ||\r
2490                         pkt_filter & NDIS_PACKET_TYPE_ALL_MULTICAST ||\r
2491                         pkt_filter & NDIS_PACKET_TYPE_MULTICAST )\r
2492                 {\r
2493                         /* OK to report. */\r
2494                         type = IP_STAT_MCAST_BYTES;\r
2495                         status = NDIS_STATUS_SUCCESS;\r
2496                 }\r
2497                 else\r
2498                 {\r
2499                         type = IP_STAT_DROPPED;\r
2500                         status = NDIS_STATUS_FAILURE;\r
2501                 }\r
2502                 break;\r
2503         }\r
2504 \r
2505         if( status != NDIS_STATUS_SUCCESS )\r
2506         {\r
2507                 ipoib_inc_recv_stat( p_port->p_adapter, type, 0 );\r
2508                 /* Return the receive descriptor to the pool. */\r
2509                 __buf_mgr_put_recv( p_port, p_desc, NULL );\r
2510                 IPOIB_PRINT_EXIT( TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
2511                         ("Packet filter doesn't match receive.  Dropping.\n") );\r
2512                 /*\r
2513                  * Return IB_NOT_DONE since the packet has been completed,\r
2514                  * but has not consumed an array entry.\r
2515                  */\r
2516                 return IB_NOT_DONE;\r
2517         }\r
2518 \r
2519         cl_perf_start( GetNdisPkt );\r
2520         *pp_packet = __buf_mgr_get_ndis_pkt( p_port, p_desc );\r
2521         cl_perf_stop( &p_port->p_adapter->perf, GetNdisPkt );\r
2522         if( !*pp_packet )\r
2523         {\r
2524                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2525                         ("__buf_mgr_get_ndis_pkt failed\n") );\r
2526                 return IB_INSUFFICIENT_RESOURCES;\r
2527         }\r
2528 \r
2529         /* Flag the checksums as having been calculated. */\r
2530         chksum.Value = 0;\r
2531         chksum.Receive.NdisPacketTcpChecksumSucceeded = TRUE;\r
2532         chksum.Receive.NdisPacketUdpChecksumSucceeded = TRUE;\r
2533         chksum.Receive.NdisPacketIpChecksumSucceeded = TRUE;\r
2534         NDIS_PER_PACKET_INFO_FROM_PACKET( *pp_packet, TcpIpChecksumPacketInfo ) =\r
2535                 (void*)(uintn_t)chksum.Value;\r
2536 \r
2537         ipoib_inc_recv_stat( p_port->p_adapter, type, p_desc->len );\r
2538 \r
2539         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2540         return IB_SUCCESS;\r
2541 }\r
2542 \r
2543 \r
2544 static uint32_t\r
2545 __recv_mgr_build_pkt_array(\r
2546         IN                              ipoib_port_t* const                     p_port,\r
2547         IN                              int32_t                                         shortage,\r
2548                 OUT                     cl_qlist_t* const                       p_done_list,\r
2549                 OUT                     int32_t* const                          p_discarded )\r
2550 {\r
2551         cl_list_item_t                  *p_item;\r
2552         ipoib_recv_desc_t               *p_desc;\r
2553         uint32_t                                i = 0;\r
2554         ib_api_status_t                 status;\r
2555         PERF_DECLARE( PreparePkt );\r
2556 \r
2557         IPOIB_ENTER( IPOIB_DBG_RECV );\r
2558 \r
2559         *p_discarded = 0;\r
2560 \r
2561         /* Move any existing receives to the head to preserve ordering. */\r
2562         cl_qlist_insert_list_head( p_done_list, &p_port->recv_mgr.done_list );\r
2563         p_item = cl_qlist_remove_head( p_done_list );\r
2564         while( p_item != cl_qlist_end( p_done_list ) )\r
2565         {\r
2566                 p_desc = (ipoib_recv_desc_t*)p_item;\r
2567 \r
2568                 cl_perf_start( PreparePkt );\r
2569                 status = __recv_mgr_prepare_pkt( p_port, p_desc,\r
2570                         &p_port->recv_mgr.recv_pkt_array[i] );\r
2571                 cl_perf_stop( &p_port->p_adapter->perf, PreparePkt );\r
2572                 if( status == IB_SUCCESS )\r
2573                 {\r
2574                         CL_ASSERT( p_port->recv_mgr.recv_pkt_array[i] );\r
2575                         if( shortage-- > 0 )\r
2576                         {\r
2577                                 NDIS_SET_PACKET_STATUS(\r
2578                                         p_port->recv_mgr.recv_pkt_array[i], NDIS_STATUS_RESOURCES );\r
2579                         }\r
2580                         else\r
2581                         {\r
2582                                 NDIS_SET_PACKET_STATUS(\r
2583                                         p_port->recv_mgr.recv_pkt_array[i], NDIS_STATUS_SUCCESS );\r
2584                         }\r
2585                         i++;\r
2586                 }\r
2587                 else if( status == IB_NOT_DONE )\r
2588                 {\r
2589                         (*p_discarded)++;\r
2590                 }\r
2591                 else\r
2592                 {\r
2593                         IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_RECV,\r
2594                                 ("__recv_mgr_prepare_pkt returned %s\n",\r
2595                                 p_port->p_adapter->p_ifc->get_err_str( status )) );\r
2596                         /* Put all completed receives on the port's done list. */\r
2597                         cl_qlist_insert_tail( &p_port->recv_mgr.done_list, p_item );\r
2598                         cl_qlist_insert_list_tail( &p_port->recv_mgr.done_list, p_done_list );\r
2599                         break;\r
2600                 }\r
2601 \r
2602                 p_item = cl_qlist_remove_head( p_done_list );\r
2603         }\r
2604 \r
2605         IPOIB_EXIT( IPOIB_DBG_RECV );\r
2606         return i;\r
2607 }\r
2608 \r
2609 \r
2610 \r
2611 \r
2612 /******************************************************************************\r
2613 *\r
2614 * Send manager implementation.\r
2615 *\r
2616 ******************************************************************************/\r
2617 static void\r
2618 __send_mgr_construct(\r
2619         IN                              ipoib_port_t* const                     p_port )\r
2620 {\r
2621         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2622         p_port->send_mgr.depth = 0;\r
2623         cl_qlist_init( &p_port->send_mgr.pending_list );\r
2624         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2625 }\r
2626 \r
2627 \r
2628 static void \r
2629 __pending_list_destroy(\r
2630         IN                              ipoib_port_t* const                     p_port )\r
2631 {\r
2632         cl_list_item_t  *p_item;\r
2633         NDIS_PACKET             *p_packet;\r
2634         \r
2635         cl_spinlock_acquire( &p_port->send_lock );\r
2636         /* Complete any pending packets. */\r
2637         for( p_item = cl_qlist_remove_head( &p_port->send_mgr.pending_list );\r
2638                 p_item != cl_qlist_end( &p_port->send_mgr.pending_list );\r
2639                 p_item = cl_qlist_remove_head( &p_port->send_mgr.pending_list ) )\r
2640         {\r
2641                 p_packet = IPOIB_PACKET_FROM_LIST_ITEM( p_item );\r
2642                 NdisMSendComplete( p_port->p_adapter->h_adapter, p_packet,\r
2643                         NDIS_STATUS_RESET_IN_PROGRESS );\r
2644         }\r
2645         cl_spinlock_release( &p_port->send_lock );\r
2646 }\r
2647 \r
2648 static void\r
2649 __send_mgr_destroy(\r
2650         IN                              ipoib_port_t* const                     p_port )\r
2651 {\r
2652         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2653         __pending_list_destroy(p_port);\r
2654 \r
2655         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2656 }\r
2657 \r
2658 \r
2659 static NDIS_STATUS\r
2660 __send_mgr_filter(\r
2661         IN                              ipoib_port_t* const                     p_port,\r
2662         IN              const   eth_hdr_t* const                        p_eth_hdr,\r
2663         IN                              NDIS_BUFFER* const                      p_buf,\r
2664         IN                              size_t                                          buf_len,\r
2665         IN      OUT                     ipoib_send_desc_t* const        p_desc )\r
2666 {\r
2667         NDIS_STATUS             status;\r
2668 \r
2669         PERF_DECLARE( FilterIp );\r
2670         PERF_DECLARE( FilterArp );\r
2671         PERF_DECLARE( SendGen );\r
2672 \r
2673         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2674 \r
2675         /*\r
2676          * We already checked the ethernet header length, so we know it's safe\r
2677          * to decrement the buf_len without underflowing.\r
2678          */\r
2679         buf_len -= sizeof(eth_hdr_t);\r
2680 \r
2681         switch( p_eth_hdr->type )\r
2682         {\r
2683         case ETH_PROT_TYPE_IP:\r
2684                 cl_perf_start( FilterIp );\r
2685                 status = __send_mgr_filter_ip(\r
2686                         p_port, p_eth_hdr, p_buf, buf_len, p_desc );\r
2687                 cl_perf_stop( &p_port->p_adapter->perf, FilterIp );\r
2688                 break;\r
2689 \r
2690         case ETH_PROT_TYPE_ARP:\r
2691                 cl_perf_start( FilterArp );\r
2692                 status = __send_mgr_filter_arp(\r
2693                         p_port, p_eth_hdr, p_buf, buf_len, p_desc );\r
2694                 cl_perf_stop( &p_port->p_adapter->perf, FilterArp );\r
2695                 break;\r
2696 \r
2697         default:\r
2698                 /*\r
2699                  * The IPoIB spec doesn't define how to send non IP or ARP packets.\r
2700                  * Just send the payload and hope for the best.\r
2701                  */\r
2702                 cl_perf_start( SendGen );\r
2703                 status = __send_gen( p_port, p_desc );\r
2704                 cl_perf_stop( &p_port->p_adapter->perf, SendGen );\r
2705                 break;\r
2706         }\r
2707 \r
2708         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2709         return status;\r
2710 }\r
2711 \r
2712 \r
2713 static NDIS_STATUS\r
2714 __send_copy(\r
2715         IN                              ipoib_port_t* const                     p_port,\r
2716         IN                              ipoib_send_desc_t* const        p_desc )\r
2717 {\r
2718         NDIS_PACKET                             *p_packet;\r
2719         NDIS_BUFFER                             *p_buf;\r
2720         NDIS_STATUS                             status;\r
2721         UINT                                    tot_len, bytes_copied = 0;\r
2722 \r
2723         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2724 \r
2725         p_desc->p_buf = \r
2726                 ExAllocateFromNPagedLookasideList( &p_port->buf_mgr.send_buf_list );\r
2727         if( !p_desc->p_buf )\r
2728         {\r
2729                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2730                         ("Failed to allocate buffer for packet copy.\n") );\r
2731                 return NDIS_STATUS_RESOURCES;\r
2732         }\r
2733 \r
2734         NdisAllocatePacket( &status, &p_packet, p_port->buf_mgr.h_send_pkt_pool );\r
2735         if( status != NDIS_STATUS_SUCCESS )\r
2736         {\r
2737                 IPOIB_PRINT_EXIT( TRACE_LEVEL_WARNING, IPOIB_DBG_SEND,\r
2738                         ("Failed to allocate NDIS_PACKET for copy.\n") );\r
2739                 return status;\r
2740         }\r
2741 \r
2742         NdisAllocateBuffer( &status, &p_buf, p_port->buf_mgr.h_send_buf_pool,\r
2743                 p_desc->p_buf, p_port->p_adapter->params.xfer_block_size );\r
2744         if( status != NDIS_STATUS_SUCCESS )\r
2745         {\r
2746                 NdisFreePacket( p_packet );\r
2747                 IPOIB_PRINT_EXIT( TRACE_LEVEL_WARNING, IPOIB_DBG_SEND,\r
2748                         ("Failed to allocate NDIS_BUFFER for copy.\n") );\r
2749                 return status;\r
2750         }\r
2751 \r
2752         NdisChainBufferAtFront( p_packet, p_buf );\r
2753 \r
2754         NdisQueryPacketLength( p_desc->p_pkt, &tot_len );\r
2755 \r
2756         /* Setup the work request. */\r
2757         p_desc->local_ds[1].vaddr = cl_get_physaddr(\r
2758                 ((uint8_t*)p_desc->p_buf) + sizeof(eth_hdr_t) );\r
2759         p_desc->local_ds[1].length = tot_len - sizeof(eth_hdr_t);\r
2760         p_desc->local_ds[1].lkey = p_port->ib_mgr.lkey;\r
2761         p_desc->wr.num_ds = 2;\r
2762 \r
2763         /* Copy the packet. */\r
2764         NdisCopyFromPacketToPacketSafe( p_packet, bytes_copied, tot_len,\r
2765                 p_desc->p_pkt, bytes_copied, &bytes_copied,\r
2766                 NormalPagePriority );\r
2767 \r
2768         /* Free our temp packet now that the data is copied. */\r
2769         NdisUnchainBufferAtFront( p_packet, &p_buf );\r
2770         NdisFreeBuffer( p_buf );\r
2771         NdisFreePacket( p_packet );\r
2772 \r
2773         if( bytes_copied != tot_len )\r
2774         {\r
2775                 /* Something went wrong.  Drop the packet. */\r
2776                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2777                         ("Failed to copy full packet: %d of %d bytes copied.\n",\r
2778                         bytes_copied, tot_len) );\r
2779                 return NDIS_STATUS_RESOURCES;\r
2780         }\r
2781 \r
2782         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2783         return NDIS_STATUS_SUCCESS;\r
2784 }\r
2785 \r
2786 \r
2787 #if !IPOIB_USE_DMA\r
2788 /* Send using the MDL's page information rather than the SGL. */\r
2789 static ib_api_status_t\r
2790 __send_gen(\r
2791         IN                              ipoib_port_t* const                     p_port,\r
2792         IN                              ipoib_send_desc_t* const        p_desc )\r
2793 {\r
2794         uint32_t                                i, j = 1;\r
2795         ULONG                                   offset;\r
2796         MDL                                             *p_mdl;\r
2797         UINT                                    num_pages, tot_len;\r
2798         ULONG                                   buf_len;\r
2799         PPFN_NUMBER                             page_array;\r
2800         boolean_t                               hdr_done = FALSE;\r
2801         ib_api_status_t                 status;\r
2802 \r
2803         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2804 \r
2805         NdisQueryPacket( p_desc->p_pkt, &num_pages, NULL, &p_mdl,\r
2806                 &tot_len );\r
2807 \r
2808         if( !p_mdl )\r
2809         {\r
2810                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2811                         ("No buffers associated with packet.\n") );\r
2812                 return IB_ERROR;\r
2813         }\r
2814 \r
2815         /* Remember that one of the DS entries is reserved for the IPoIB header. */\r
2816         if( num_pages >= MAX_SEND_SGE )\r
2817         {\r
2818                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_SEND,\r
2819                         ("Too many buffers to fit in WR ds_array.  Copying data.\n") );\r
2820                 status = __send_copy( p_port, p_desc );\r
2821                 IPOIB_EXIT( IPOIB_DBG_SEND );\r
2822                 return status;\r
2823         }\r
2824 \r
2825         CL_ASSERT( tot_len > sizeof(eth_hdr_t) );\r
2826         CL_ASSERT( tot_len <= p_port->p_adapter->params.xfer_block_size );\r
2827         /*\r
2828          * Assume that the ethernet header is always fully contained\r
2829          * in the first page of the first MDL.  This makes for much\r
2830          * simpler code.\r
2831          */\r
2832         offset = MmGetMdlByteOffset( p_mdl ) + sizeof(eth_hdr_t);\r
2833         CL_ASSERT( offset <= PAGE_SIZE );\r
2834 \r
2835         while( tot_len )\r
2836         {\r
2837                 buf_len = MmGetMdlByteCount( p_mdl );\r
2838                 page_array = MmGetMdlPfnArray( p_mdl );\r
2839                 CL_ASSERT( page_array );\r
2840                 i = 0;\r
2841                 if( !hdr_done )\r
2842                 {\r
2843                         CL_ASSERT( buf_len >= sizeof(eth_hdr_t) );\r
2844                         /* Skip the ethernet header. */\r
2845                         buf_len -= sizeof(eth_hdr_t);\r
2846                         CL_ASSERT( buf_len <= p_port->p_adapter->params.payload_mtu );\r
2847                         if( buf_len )\r
2848                         {\r
2849                                 /* The ethernet header is a subset of this MDL. */\r
2850                                 CL_ASSERT( i == 0 );\r
2851                                 if( offset < PAGE_SIZE )\r
2852                                 {\r
2853                                         p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2854                                         p_desc->local_ds[j].vaddr = (page_array[i] << PAGE_SHIFT);\r
2855                                         /* Add the byte offset since we're on the 1st page. */\r
2856                                         p_desc->local_ds[j].vaddr += offset;\r
2857                                         if( offset + buf_len > PAGE_SIZE )\r
2858                                         {\r
2859                                                 p_desc->local_ds[j].length = PAGE_SIZE - offset;\r
2860                                                 buf_len -= p_desc->local_ds[j].length;\r
2861                                         }\r
2862                                         else\r
2863                                         {\r
2864                                                 p_desc->local_ds[j].length = buf_len;\r
2865                                                 buf_len = 0;\r
2866                                         }\r
2867                                         /* This data segment is done.  Move to the next. */\r
2868                                         j++;\r
2869                                 }\r
2870                                 /* This page is done.  Move to the next. */\r
2871                                 i++;\r
2872                         }\r
2873                         /* Done handling the ethernet header. */\r
2874                         hdr_done = TRUE;\r
2875                 }\r
2876 \r
2877                 /* Finish this MDL */\r
2878                 while( buf_len )\r
2879                 {\r
2880                         p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2881                         p_desc->local_ds[j].vaddr = (page_array[i] << PAGE_SHIFT);\r
2882                         /* Add the first page's offset if we're on the first page. */\r
2883                         if( i == 0 )\r
2884                                 p_desc->local_ds[j].vaddr += MmGetMdlByteOffset( p_mdl );\r
2885 \r
2886                         if( i == 0 && (MmGetMdlByteOffset( p_mdl ) + buf_len) > PAGE_SIZE )\r
2887                         {\r
2888                                 /* Buffers spans pages. */\r
2889                                 p_desc->local_ds[j].length =\r
2890                                         PAGE_SIZE - MmGetMdlByteOffset( p_mdl );\r
2891                                 buf_len -= p_desc->local_ds[j].length;\r
2892                                 /* This page is done.  Move to the next. */\r
2893                                 i++;\r
2894                         }\r
2895                         else\r
2896                         {\r
2897                                 /* Last page of the buffer. */\r
2898                                 p_desc->local_ds[j].length = buf_len;\r
2899                                 buf_len = 0;\r
2900                         }\r
2901                         /* This data segment is done.  Move to the next. */\r
2902                         j++;\r
2903                 }\r
2904 \r
2905                 tot_len -= MmGetMdlByteCount( p_mdl );\r
2906                 if( !tot_len )\r
2907                         break;\r
2908 \r
2909                 NdisGetNextBuffer( p_mdl, &p_mdl );\r
2910                 if( !p_mdl )\r
2911                 {\r
2912                         IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2913                                 ("Failed to get next buffer.\n") );\r
2914                         return IB_ERROR;\r
2915                 }\r
2916         }\r
2917 \r
2918         /* Set the number of data segments. */\r
2919         p_desc->wr.num_ds = j;\r
2920 \r
2921         IPOIB_EXIT( IPOIB_DBG_SEND );\r
2922         return IB_SUCCESS;\r
2923 }\r
2924 \r
2925 #else\r
2926 \r
2927 static NDIS_STATUS\r
2928 __send_gen(\r
2929         IN                              ipoib_port_t* const                     p_port,\r
2930         IN                              ipoib_send_desc_t* const        p_desc )\r
2931 {\r
2932         ib_api_status_t                 status;\r
2933         SCATTER_GATHER_LIST             *p_sgl;\r
2934         uint32_t                                i, j = 1;\r
2935         uint32_t                                offset = sizeof(eth_hdr_t);\r
2936         PERF_DECLARE( SendCopy );\r
2937 \r
2938         IPOIB_ENTER( IPOIB_DBG_SEND );\r
2939 \r
2940         p_sgl = NDIS_PER_PACKET_INFO_FROM_PACKET( p_desc->p_pkt,\r
2941                 ScatterGatherListPacketInfo );\r
2942         if( !p_sgl )\r
2943         {\r
2944                 ASSERT( p_sgl );\r
2945                 IPOIB_PRINT_EXIT( TRACE_LEVEL_ERROR, IPOIB_DBG_ERROR,\r
2946                         ("Failed to get SGL from packet.\n") );\r
2947                 return NDIS_STATUS_FAILURE;\r
2948         }\r
2949 \r
2950         /* Remember that one of the DS entries is reserved for the IPoIB header. */\r
2951         if( ( p_sgl->NumberOfElements >= MAX_SEND_SGE &&\r
2952                 p_sgl->Elements[0].Length > sizeof(eth_hdr_t)) ||\r
2953                 ( p_sgl->NumberOfElements > MAX_SEND_SGE &&\r
2954                 p_sgl->Elements[0].Length <= sizeof(eth_hdr_t)) )\r
2955         {\r
2956                 IPOIB_PRINT(TRACE_LEVEL_INFORMATION, IPOIB_DBG_SEND,\r
2957                         ("Too many buffers to fit in WR ds_array.  Copying data.\n") );\r
2958                 cl_perf_start( SendCopy );\r
2959                 status = __send_copy( p_port, p_desc );\r
2960                 cl_perf_stop( &p_port->p_adapter->perf, SendCopy );\r
2961                 IPOIB_EXIT( IPOIB_DBG_SEND );\r
2962                 return status;\r
2963         }\r
2964 \r
2965         /*\r
2966          * Skip the ethernet header.  It is either the first element,\r
2967          * or part of it.\r
2968          */\r
2969         i = 0;\r
2970         while( offset )\r
2971         {\r
2972                 if( p_sgl->Elements[i].Length <= sizeof(eth_hdr_t) )\r
2973                 {\r
2974                         offset -= p_sgl->Elements[i++].Length;\r
2975                 }\r
2976                 else\r
2977                 {\r
2978                         p_desc->local_ds[j].vaddr =\r
2979                                 p_sgl->Elements[i].Address.QuadPart + offset;\r
2980                         p_desc->local_ds[j].length =\r
2981                                 p_sgl->Elements[i].Length - offset;\r
2982                         p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2983                         i++;\r
2984                         j++;\r
2985                         break;\r
2986                 }\r
2987         }\r
2988         /* Now fill in the rest of the local data segments. */\r
2989         while( i < p_sgl->NumberOfElements )\r
2990         {\r
2991                 p_desc->local_ds[j].vaddr = p_sgl->Elements[i].Address.QuadPart;\r
2992                 p_desc->local_ds[j].length = p_sgl->Elements[i].Length;\r
2993                 p_desc->local_ds[j].lkey = p_port->ib_mgr.lkey;\r
2994                 i++;\r
2995                 j++;\r
2996         }\r
2997 \r
2998         /* Set the number of data segments. */\r
2999         p_desc->wr.num_ds = j;\r
3000 \r
3001         IPOIB_EXIT( IPOIB_DBG_SEND );\r
3002         return NDIS_STATUS_SUCCESS;\r
3003 }\r
3004 #endif\r
3005 \r
3006 \r
3007 static NDIS_STATUS\r
3008 __send_mgr_filter_ip(\r
3009         IN                              ipoib_port_t* const                     p_port,\r
3010         IN              const   eth_hdr_t* const                        p_eth_hdr,\r
3011         IN                              NDIS_BUFFER*                            p_buf,\r
3012         IN                              size_t                                          buf_len,\r
3013         IN      OUT                     ipoib_send_desc_t* const        p_desc )\r
3014 {\r
3015         NDIS_STATUS            &nb