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