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