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