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