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