staging: wlags49_h2: Remove references to SSN
[pandora-kernel.git] / drivers / staging / wlags49_h2 / hcf.c
1 /************************************************************************************************************
2  *
3  * FILE   :  HCF.C
4  *
5  * DATE    : $Date: 2004/08/05 11:47:10 $   $Revision: 1.10 $
6  * Original: 2004/06/02 10:22:22    Revision: 1.85      Tag: hcf7_t20040602_01
7  * Original: 2004/04/15 09:24:41    Revision: 1.63      Tag: hcf7_t7_20040415_01
8  * Original: 2004/04/13 14:22:44    Revision: 1.62      Tag: t7_20040413_01
9  * Original: 2004/04/01 15:32:55    Revision: 1.59      Tag: t7_20040401_01
10  * Original: 2004/03/10 15:39:27    Revision: 1.55      Tag: t20040310_01
11  * Original: 2004/03/04 11:03:37    Revision: 1.53      Tag: t20040304_01
12  * Original: 2004/03/02 14:51:21    Revision: 1.50      Tag: t20040302_03
13  * Original: 2004/02/24 13:00:27    Revision: 1.43      Tag: t20040224_01
14  * Original: 2004/02/19 10:57:25    Revision: 1.39      Tag: t20040219_01
15  *
16  * AUTHOR :  Nico Valster
17  *
18  * SPECIFICATION: ........
19  *
20  * DESCRIPTION : HCF Routines for Hermes-II (callable via the Wireless Connection I/F or WCI)
21  *               Local Support Routines for above procedures
22  *
23  *           Customizable via HCFCFG.H, which is included by HCF.H
24  *
25  *************************************************************************************************************
26  *
27  *
28  * SOFTWARE LICENSE
29  *
30  * This software is provided subject to the following terms and conditions,
31  * which you should read carefully before using the software.  Using this
32  * software indicates your acceptance of these terms and conditions.  If you do
33  * not agree with these terms and conditions, do not use the software.
34  *
35  * COPYRIGHT © 1994 - 1995   by AT&T.                All Rights Reserved
36  * COPYRIGHT © 1996 - 2000 by Lucent Technologies.   All Rights Reserved
37  * COPYRIGHT © 2001 - 2004   by Agere Systems Inc.   All Rights Reserved
38  * All rights reserved.
39  *
40  * Redistribution and use in source or binary forms, with or without
41  * modifications, are permitted provided that the following conditions are met:
42  *
43  * . Redistributions of source code must retain the above copyright notice, this
44  *    list of conditions and the following Disclaimer as comments in the code as
45  *    well as in the documentation and/or other materials provided with the
46  *    distribution.
47  *
48  * . Redistributions in binary form must reproduce the above copyright notice,
49  *    this list of conditions and the following Disclaimer in the documentation
50  *    and/or other materials provided with the distribution.
51  *
52  * . Neither the name of Agere Systems Inc. nor the names of the contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * Disclaimer
57  *
58  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
59  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
60  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
61  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
62  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
63  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
65  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
66  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
68  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
69  * DAMAGE.
70  *
71  *
72  ************************************************************************************************************/
73
74
75 /************************************************************************************************************
76  **
77  ** Implementation Notes
78  **
79  * - a leading marker of //! is used. The purpose of such a sequence is to help to understand the flow
80  *   An example is:  //!rc = HCF_SUCCESS;
81  *   if this is superfluous because rc is already guaranteed to be 0 but it shows to the (maintenance)
82  *   programmer it is an intentional omission at the place where someone could consider it most appropriate at
83  *   first glance
84  * - using near pointers in a model where ss!=ds is an invitation for disaster, so be aware of how you specify
85  *   your model and how you define variables which are used at interrupt time
86  * - remember that sign extension on 32 bit platforms may cause problems unless code is carefully constructed,
87  *   e.g. use "(hcf_16)~foo" rather than "~foo"
88  *
89  ************************************************************************************************************/
90
91 #include "hcf.h"                // HCF and MSF common include file
92 #include "hcfdef.h"             // HCF specific include file
93 #include "mmd.h"                // MoreModularDriver common include file
94 #include <linux/kernel.h>
95
96 #if ! defined offsetof
97 #define offsetof(s,m)   ((unsigned int)&(((s *)0)->m))
98 #endif // offsetof
99
100
101 /***********************************************************************************************************/
102 /***************************************  PROTOTYPES  ******************************************************/
103 /***********************************************************************************************************/
104 HCF_STATIC int          cmd_exe( IFBP ifbp, hcf_16 cmd_code, hcf_16 par_0 );
105 HCF_STATIC int          init( IFBP ifbp );
106 HCF_STATIC int          put_info( IFBP ifbp, LTVP ltvp );
107 #if (HCF_EXT) & HCF_EXT_MB
108 HCF_STATIC int          put_info_mb( IFBP ifbp, CFG_MB_INFO_STRCT FAR * ltvp );
109 #endif // HCF_EXT_MB
110 #if (HCF_TYPE) & HCF_TYPE_WPA
111 HCF_STATIC void         calc_mic( hcf_32* p, hcf_32 M );
112 void                    calc_mic_rx_frag( IFBP ifbp, wci_bufp p, int len );
113 void                    calc_mic_tx_frag( IFBP ifbp, wci_bufp p, int len );
114 HCF_STATIC int          check_mic( IFBP ifbp );
115 #endif // HCF_TYPE_WPA
116
117 HCF_STATIC void         calibrate( IFBP ifbp );
118 HCF_STATIC int          cmd_cmpl( IFBP ifbp );
119 HCF_STATIC hcf_16       get_fid( IFBP ifbp );
120 HCF_STATIC void         isr_info( IFBP ifbp );
121 #if HCF_DMA
122 HCF_STATIC DESC_STRCT*  get_frame_lst(IFBP ifbp, int tx_rx_flag);
123 #endif // HCF_DMA
124 HCF_STATIC void         get_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) );   //char*, byte count (usually even)
125 #if HCF_DMA
126 HCF_STATIC void         put_frame_lst( IFBP ifbp, DESC_STRCT *descp, int tx_rx_flag );
127 #endif // HCF_DMA
128 HCF_STATIC void         put_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) );
129 HCF_STATIC void         put_frag_finalize( IFBP ifbp );
130 HCF_STATIC int          setup_bap( IFBP ifbp, hcf_16 fid, int offset, int type );
131 #if (HCF_ASSERT) & HCF_ASSERT_PRINTF
132 static int fw_printf(IFBP ifbp, CFG_FW_PRINTF_STRCT FAR *ltvp);
133 #endif // HCF_ASSERT_PRINTF
134
135 HCF_STATIC int          download( IFBP ifbp, CFG_PROG_STRCT FAR *ltvp );
136 HCF_STATIC hcf_8        hcf_encap( wci_bufp type );
137 HCF_STATIC hcf_8        null_addr[4] = { 0, 0, 0, 0 };
138 #if ! defined IN_PORT_WORD          //replace I/O Macros with logging facility
139 extern FILE *log_file;
140
141 #define IN_PORT_WORD(port)          in_port_word( (hcf_io)(port) )
142
143 static hcf_16 in_port_word( hcf_io port ) {
144         hcf_16 i = (hcf_16)_inpw( port );
145         if ( log_file ) {
146                 fprintf( log_file, "\nR %2.2x %4.4x", (port)&0xFF, i);
147         }
148         return i;
149 } // in_port_word
150
151 #define OUT_PORT_WORD(port, value)  out_port_word( (hcf_io)(port), (hcf_16)(value) )
152
153 static void out_port_word( hcf_io port, hcf_16 value ) {
154         _outpw( port, value );
155         if ( log_file ) {
156                 fprintf( log_file, "\nW %2.02x %4.04x", (port)&0xFF, value );
157         }
158 }
159
160 void IN_PORT_STRING_32( hcf_io prt, hcf_32 FAR * dst, int n)    {
161         int i = 0;
162         hcf_16 FAR * p;
163         if ( log_file ) {
164                 fprintf( log_file, "\nread string_32 length %04x (%04d) at port %02.2x to addr %lp",
165                          (hcf_16)n, (hcf_16)n, (hcf_16)(prt)&0xFF, dst);
166         }
167         while ( n-- ) {
168                 p = (hcf_16 FAR *)dst;
169                 *p++ = (hcf_16)_inpw( prt );
170                 *p   = (hcf_16)_inpw( prt );
171                 if ( log_file ) {
172                         fprintf( log_file, "%s%08lx ", i++ % 0x08 ? " " : "\n", *dst);
173                 }
174                 dst++;
175         }
176 } // IN_PORT_STRING_32
177
178 void IN_PORT_STRING_8_16( hcf_io prt, hcf_8 FAR * dst, int n) { //also handles byte alignment problems
179         hcf_16 FAR * p = (hcf_16 FAR *)dst;                         //this needs more elaborate code in non-x86 platforms
180         int i = 0;
181         if ( log_file ) {
182                 fprintf( log_file, "\nread string_16 length %04x (%04d) at port %02.2x to addr %lp",
183                          (hcf_16)n, (hcf_16)n, (hcf_16)(prt)&0xFF, dst );
184         }
185         while ( n-- ) {
186                 *p =(hcf_16)_inpw( prt);
187                 if ( log_file ) {
188                         if ( i++ % 0x10 ) {
189                                 fprintf( log_file, "%04x ", *p);
190                         } else {
191                                 fprintf( log_file, "\n%04x ", *p);
192                         }
193                 }
194                 p++;
195         }
196 } // IN_PORT_STRING_8_16
197
198 void OUT_PORT_STRING_32( hcf_io prt, hcf_32 FAR * src, int n)   {
199         int i = 0;
200         hcf_16 FAR * p;
201         if ( log_file ) {
202                 fprintf( log_file, "\nwrite string_32 length %04x (%04d) at port %02.2x",
203                          (hcf_16)n, (hcf_16)n, (hcf_16)(prt)&0xFF);
204         }
205         while ( n-- ) {
206                 p = (hcf_16 FAR *)src;
207                 _outpw( prt, *p++ );
208                 _outpw( prt, *p   );
209                 if ( log_file ) {
210                         fprintf( log_file, "%s%08lx ", i++ % 0x08 ? " " : "\n", *src);
211                 }
212                 src++;
213         }
214 } // OUT_PORT_STRING_32
215
216 void OUT_PORT_STRING_8_16( hcf_io prt, hcf_8 FAR * src, int n)  {   //also handles byte alignment problems
217         hcf_16 FAR * p = (hcf_16 FAR *)src;                             //this needs more elaborate code in non-x86 platforms
218         int i = 0;
219         if ( log_file ) {
220                 fprintf( log_file, "\nwrite string_16 length %04x (%04d) at port %04x", n, n, (hcf_16)prt);
221         }
222         while ( n-- ) {
223                 (void)_outpw( prt, *p);
224                 if ( log_file ) {
225                         if ( i++ % 0x10 ) {
226                                 fprintf( log_file, "%04x ", *p);
227                         } else {
228                                 fprintf( log_file, "\n%04x ", *p);
229                         }
230                 }
231                 p++;
232         }
233 } // OUT_PORT_STRING_8_16
234
235 #endif // IN_PORT_WORD
236
237 /************************************************************************************************************
238  ******************************* D A T A    D E F I N I T I O N S ********************************************
239  ************************************************************************************************************/
240
241 #if HCF_ASSERT
242 IFBP BASED assert_ifbp = NULL;          //to make asserts easily work under MMD and DHF
243 #endif // HCF_ASSERT
244
245 /* SNAP header to be inserted in Ethernet-II frames */
246 HCF_STATIC  hcf_8 BASED snap_header[] = { 0xAA, 0xAA, 0x03, 0x00, 0x00, //5 bytes signature +
247                                           0 };                          //1 byte protocol identifier
248
249 #if (HCF_TYPE) & HCF_TYPE_WPA
250 HCF_STATIC hcf_8 BASED mic_pad[8] = { 0x5A, 0, 0, 0, 0, 0, 0, 0 };      //MIC padding of message
251 #endif // HCF_TYPE_WPA
252
253 #if defined MSF_COMPONENT_ID
254 CFG_IDENTITY_STRCT BASED cfg_drv_identity = {
255         sizeof(cfg_drv_identity)/sizeof(hcf_16) - 1,    //length of RID
256         CFG_DRV_IDENTITY,           // (0x0826)
257         MSF_COMPONENT_ID,
258         MSF_COMPONENT_VAR,
259         MSF_COMPONENT_MAJOR_VER,
260         MSF_COMPONENT_MINOR_VER
261 } ;
262
263 CFG_RANGES_STRCT BASED cfg_drv_sup_range = {
264         sizeof(cfg_drv_sup_range)/sizeof(hcf_16) - 1,   //length of RID
265         CFG_DRV_SUP_RANGE,          // (0x0827)
266
267         COMP_ROLE_SUPL,
268         COMP_ID_DUI,
269         {{  DUI_COMPAT_VAR,
270             DUI_COMPAT_BOT,
271             DUI_COMPAT_TOP
272         }}
273 } ;
274
275 struct CFG_RANGE3_STRCT BASED cfg_drv_act_ranges_pri = {
276         sizeof(cfg_drv_act_ranges_pri)/sizeof(hcf_16) - 1,  //length of RID
277         CFG_DRV_ACT_RANGES_PRI,     // (0x0828)
278
279         COMP_ROLE_ACT,
280         COMP_ID_PRI,
281         {
282                 { 0, 0, 0 },                           // HCF_PRI_VAR_1 not supported by HCF 7
283                 { 0, 0, 0 },                           // HCF_PRI_VAR_2 not supported by HCF 7
284                 {  3,                                  //var_rec[2] - Variant number
285                    CFG_DRV_ACT_RANGES_PRI_3_BOTTOM,        //       - Bottom Compatibility
286                    CFG_DRV_ACT_RANGES_PRI_3_TOP            //       - Top Compatibility
287                 }
288         }
289 } ;
290
291
292 struct CFG_RANGE4_STRCT BASED cfg_drv_act_ranges_sta = {
293         sizeof(cfg_drv_act_ranges_sta)/sizeof(hcf_16) - 1,  //length of RID
294         CFG_DRV_ACT_RANGES_STA,     // (0x0829)
295
296         COMP_ROLE_ACT,
297         COMP_ID_STA,
298         {
299 #if defined HCF_STA_VAR_1
300                 {  1,                                  //var_rec[1] - Variant number
301                    CFG_DRV_ACT_RANGES_STA_1_BOTTOM,        //       - Bottom Compatibility
302                    CFG_DRV_ACT_RANGES_STA_1_TOP            //       - Top Compatibility
303                 },
304 #else
305                 { 0, 0, 0 },
306 #endif // HCF_STA_VAR_1
307 #if defined HCF_STA_VAR_2
308                 {  2,                                  //var_rec[1] - Variant number
309                    CFG_DRV_ACT_RANGES_STA_2_BOTTOM,        //       - Bottom Compatibility
310                    CFG_DRV_ACT_RANGES_STA_2_TOP            //       - Top Compatibility
311                 },
312 #else
313                 { 0, 0, 0 },
314 #endif // HCF_STA_VAR_2
315 // For Native_USB (Not used!)
316 #if defined HCF_STA_VAR_3
317                 {  3,                                  //var_rec[1] - Variant number
318                    CFG_DRV_ACT_RANGES_STA_3_BOTTOM,        //       - Bottom Compatibility
319                    CFG_DRV_ACT_RANGES_STA_3_TOP            //       - Top Compatibility
320                 },
321 #else
322                 { 0, 0, 0 },
323 #endif // HCF_STA_VAR_3
324 // Warp
325 #if defined HCF_STA_VAR_4
326                 {  4,                                  //var_rec[1] - Variant number
327                    CFG_DRV_ACT_RANGES_STA_4_BOTTOM,        //           - Bottom Compatibility
328                    CFG_DRV_ACT_RANGES_STA_4_TOP            //           - Top Compatibility
329                 }
330 #else
331                 { 0, 0, 0 }
332 #endif // HCF_STA_VAR_4
333         }
334 } ;
335
336
337 struct CFG_RANGE6_STRCT BASED cfg_drv_act_ranges_hsi = {
338         sizeof(cfg_drv_act_ranges_hsi)/sizeof(hcf_16) - 1,  //length of RID
339         CFG_DRV_ACT_RANGES_HSI,     // (0x082A)
340         COMP_ROLE_ACT,
341         COMP_ID_HSI,
342         {
343 #if defined HCF_HSI_VAR_0                   // Controlled deployment
344                 {  0,                                  // var_rec[1] - Variant number
345                    CFG_DRV_ACT_RANGES_HSI_0_BOTTOM,        //           - Bottom Compatibility
346                    CFG_DRV_ACT_RANGES_HSI_0_TOP            //           - Top Compatibility
347                 },
348 #else
349                 { 0, 0, 0 },
350 #endif // HCF_HSI_VAR_0
351                 { 0, 0, 0 },                           // HCF_HSI_VAR_1 not supported by HCF 7
352                 { 0, 0, 0 },                           // HCF_HSI_VAR_2 not supported by HCF 7
353                 { 0, 0, 0 },                           // HCF_HSI_VAR_3 not supported by HCF 7
354 #if defined HCF_HSI_VAR_4                   // Hermes-II all types
355                 {  4,                                  // var_rec[1] - Variant number
356                    CFG_DRV_ACT_RANGES_HSI_4_BOTTOM,        //           - Bottom Compatibility
357                    CFG_DRV_ACT_RANGES_HSI_4_TOP            //           - Top Compatibility
358                 },
359 #else
360                 { 0, 0, 0 },
361 #endif // HCF_HSI_VAR_4
362 #if defined HCF_HSI_VAR_5                   // WARP Hermes-2.5
363                 {  5,                                  // var_rec[1] - Variant number
364                    CFG_DRV_ACT_RANGES_HSI_5_BOTTOM,        //           - Bottom Compatibility
365                    CFG_DRV_ACT_RANGES_HSI_5_TOP            //           - Top Compatibility
366                 }
367 #else
368                 { 0, 0, 0 }
369 #endif // HCF_HSI_VAR_5
370         }
371 } ;
372
373
374 CFG_RANGE4_STRCT BASED cfg_drv_act_ranges_apf = {
375         sizeof(cfg_drv_act_ranges_apf)/sizeof(hcf_16) - 1,  //length of RID
376         CFG_DRV_ACT_RANGES_APF,     // (0x082B)
377
378         COMP_ROLE_ACT,
379         COMP_ID_APF,
380         {
381 #if defined HCF_APF_VAR_1               //(Fake) Hermes-I
382                 {  1,                                  //var_rec[1] - Variant number
383                    CFG_DRV_ACT_RANGES_APF_1_BOTTOM,        //           - Bottom Compatibility
384                    CFG_DRV_ACT_RANGES_APF_1_TOP            //           - Top Compatibility
385                 },
386 #else
387                 { 0, 0, 0 },
388 #endif // HCF_APF_VAR_1
389 #if defined HCF_APF_VAR_2               //Hermes-II
390                 {  2,                                  // var_rec[1] - Variant number
391                    CFG_DRV_ACT_RANGES_APF_2_BOTTOM,        //           - Bottom Compatibility
392                    CFG_DRV_ACT_RANGES_APF_2_TOP            //           - Top Compatibility
393                 },
394 #else
395                 { 0, 0, 0 },
396 #endif // HCF_APF_VAR_2
397 #if defined HCF_APF_VAR_3                       // Native_USB
398                 {  3,                                      // var_rec[1] - Variant number
399                    CFG_DRV_ACT_RANGES_APF_3_BOTTOM,        //           - Bottom Compatibility !!!!!see note below!!!!!!!
400                    CFG_DRV_ACT_RANGES_APF_3_TOP            //           - Top Compatibility
401                 },
402 #else
403                 { 0, 0, 0 },
404 #endif // HCF_APF_VAR_3
405 #if defined HCF_APF_VAR_4                       // WARP Hermes 2.5
406                 {  4,                                      // var_rec[1] - Variant number
407                    CFG_DRV_ACT_RANGES_APF_4_BOTTOM,        //           - Bottom Compatibility !!!!!see note below!!!!!!!
408                    CFG_DRV_ACT_RANGES_APF_4_TOP            //           - Top Compatibility
409                 }
410 #else
411                 { 0, 0, 0 }
412 #endif // HCF_APF_VAR_4
413         }
414 } ;
415 #define HCF_VERSION  TEXT( "HCF$Revision: 1.10 $" )
416
417 static struct /*CFG_HCF_OPT_STRCT*/ {
418         hcf_16  len;                    //length of cfg_hcf_opt struct
419         hcf_16  typ;                    //type 0x082C
420         hcf_16   v0;                        //offset HCF_VERSION
421         hcf_16   v1;                        // MSF_COMPONENT_ID
422         hcf_16   v2;                        // HCF_ALIGN
423         hcf_16   v3;                        // HCF_ASSERT
424         hcf_16   v4;                        // HCF_BIG_ENDIAN
425         hcf_16   v5;                        // /* HCF_DLV | HCF_DLNV */
426         hcf_16   v6;                        // HCF_DMA
427         hcf_16   v7;                        // HCF_ENCAP
428         hcf_16   v8;                        // HCF_EXT
429         hcf_16   v9;                        // HCF_INT_ON
430         hcf_16  v10;                        // HCF_IO
431         hcf_16  v11;                        // HCF_LEGACY
432         hcf_16  v12;                        // HCF_MAX_LTV
433         hcf_16  v13;                        // HCF_PROT_TIME
434         hcf_16  v14;                        // HCF_SLEEP
435         hcf_16  v15;                        // HCF_TALLIES
436         hcf_16  v16;                        // HCF_TYPE
437         hcf_16  v17;                        // HCF_NIC_TAL_CNT
438         hcf_16  v18;                        // HCF_HCF_TAL_CNT
439         hcf_16  v19;                        // offset tallies
440         char    val[sizeof(HCF_VERSION)];
441 } BASED cfg_hcf_opt = {
442         sizeof(cfg_hcf_opt)/sizeof(hcf_16) -1,
443         CFG_HCF_OPT,                // (0x082C)
444         ( sizeof(cfg_hcf_opt) - sizeof(HCF_VERSION) - 4 )/sizeof(hcf_16),
445 #if defined MSF_COMPONENT_ID
446         MSF_COMPONENT_ID,
447 #else
448         0,
449 #endif // MSF_COMPONENT_ID
450         HCF_ALIGN,
451         HCF_ASSERT,
452         HCF_BIG_ENDIAN,
453         0,                                  // /* HCF_DLV | HCF_DLNV*/,
454         HCF_DMA,
455         HCF_ENCAP,
456         HCF_EXT,
457         HCF_INT_ON,
458         HCF_IO,
459         HCF_LEGACY,
460         HCF_MAX_LTV,
461         HCF_PROT_TIME,
462         HCF_SLEEP,
463         HCF_TALLIES,
464         HCF_TYPE,
465 #if (HCF_TALLIES) & ( HCF_TALLIES_NIC | HCF_TALLIES_HCF )
466         HCF_NIC_TAL_CNT,
467         HCF_HCF_TAL_CNT,
468         offsetof(IFB_STRCT, IFB_TallyLen ),
469 #else
470         0, 0, 0,
471 #endif // HCF_TALLIES_NIC / HCF_TALLIES_HCF
472         HCF_VERSION
473 }; // cfg_hcf_opt
474 #endif // MSF_COMPONENT_ID
475
476 #if defined MSF_COMPONENT_ID || (HCF_EXT) & HCF_EXT_MB
477 #if (HCF_EXT) & HCF_EXT_MB
478 HCF_STATIC LTV_STRCT BASED cfg_null = { 1, CFG_NULL, {0} };
479 #endif // HCF_EXT_MB
480 HCF_STATIC hcf_16* BASED xxxx[ ] = {
481 #if (HCF_EXT) & HCF_EXT_MB
482         &cfg_null.len,                          //CFG_NULL                      0x0820
483 #endif // HCF_EXT_MB
484 #if defined MSF_COMPONENT_ID
485         &cfg_drv_identity.len,                  //CFG_DRV_IDENTITY              0x0826
486         &cfg_drv_sup_range.len,                 //CFG_DRV_SUP_RANGE             0x0827
487         &cfg_drv_act_ranges_pri.len,            //CFG_DRV_ACT_RANGES_PRI        0x0828
488         &cfg_drv_act_ranges_sta.len,            //CFG_DRV_ACT_RANGES_STA        0x0829
489         &cfg_drv_act_ranges_hsi.len,            //CFG_DRV_ACT_RANGES_HSI        0x082A
490         &cfg_drv_act_ranges_apf.len,            //CFG_DRV_ACT_RANGES_APF        0x082B
491         &cfg_hcf_opt.len,                       //CFG_HCF_OPT                   0x082C
492         NULL,                                   //IFB_PRIIdentity placeholder   0xFD02
493         NULL,                                   //IFB_PRISup placeholder        0xFD03
494 #endif // MSF_COMPONENT_ID
495         NULL                                    //endsentinel
496 };
497 #define xxxx_PRI_IDENTITY_OFFSET    (ARRAY_SIZE(xxxx) - 3)
498
499 #endif // MSF_COMPONENT_ID / HCF_EXT_MB
500
501
502 /************************************************************************************************************
503  ************************** T O P   L E V E L   H C F   R O U T I N E S **************************************
504  ************************************************************************************************************/
505
506 /************************************************************************************************************
507  *
508  *.MODULE        int hcf_action( IFBP ifbp, hcf_16 action )
509  *.PURPOSE       Changes the run-time Card behavior.
510  *               Performs Miscellanuous actions.
511  *
512  *.ARGUMENTS
513  *   ifbp                    address of the Interface Block
514  *   action                  number identifying the type of change
515  *    - HCF_ACT_CCX_OFF      disable CKIP
516  *    - HCF_ACT_CCX_ON       enable CKIP
517  *    - HCF_ACT_INT_FORCE_ON enable interrupt generation by WaveLAN NIC
518  *    - HCF_ACT_INT_OFF      disable interrupt generation by WaveLAN NIC
519  *    - HCF_ACT_INT_ON       compensate 1 HCF_ACT_INT_OFF, enable interrupt generation if balance reached
520  *    - HCF_ACT_PRS_SCAN     Hermes Probe Respons Scan (F102) command
521  *    - HCF_ACT_RX_ACK       acknowledge non-DMA receiver to Hermes
522  *    - HCF_ACT_SCAN         Hermes Inquire Scan (F101) command (non-WARP only)
523  *    - HCF_ACT_SLEEP        DDS Sleep request
524  *    - HCF_ACT_TALLIES      Hermes Inquire Tallies (F100) command
525  *
526  *.RETURNS
527  *   HCF_SUCCESS             all (including invalid)
528  *   HCF_INT_PENDING         HCF_ACT_INT_OFF, interrupt pending
529  *   HCF_ERR_NO_NIC          HCF_ACT_INT_OFF, NIC presence check fails
530  *
531  *.CONDITIONS
532  * Except for hcf_action with HCF_ACT_INT_FORCE_ON or HCF_ACT_INT_OFF as parameter or hcf_connect with an I/O
533  * address (i.e. not HCF_DISCONNECT), all hcf-function calls MUST be preceded by a call of hcf_action with
534  * HCF_ACT_INT_OFF as parameter.
535  * Note that hcf_connect defaults to NIC interrupt disabled mode, i.e. as if hcf_action( HCF_ACT_INT_OFF )
536  * was called.
537  *
538  *.DESCRIPTION
539  * hcf_action supports the following mode changing action-code pairs that are antonyms
540  *    - HCF_ACT_CCX_OFF / HCF_ACT_CCX_ON
541  *    - HCF_ACT_INT_[FORCE_]ON / HCF_ACT_INT_OFF
542  *
543  * Additionally hcf_action can start the following actions in the NIC:
544  *    - HCF_ACT_PRS_SCAN
545  *    - HCF_ACT_RX_ACK
546  *    - HCF_ACT_SCAN
547  *    - HCF_ACT_SLEEP
548  *    - HCF_ACT_TALLIES
549  *
550  * o HCF_ACT_INT_OFF: Sets NIC Interrupts mode Disabled.
551  * This command, and the associated [Force] Enable NIC interrupts command, are only available if the HCF_INT_ON
552  * compile time option is not set at 0x0000.
553  *
554  * o HCF_ACT_INT_ON: Sets NIC Interrupts mode Enabled.
555  * Enable NIC Interrupts, depending on the number of preceding Disable NIC Interrupt calls.
556  *
557  * o HCF_ACT_INT_FORCE_ON: Force NIC Interrupts mode Enabled.
558  * Sets NIC Interrupts mode Enabled, regardless off the number of preceding Disable NIC Interrupt calls.
559  *
560  * The disabling and enabling of interrupts are antonyms.
561  * These actions must be balanced.
562  * For each "disable interrupts" there must be a matching "enable interrupts".
563  * The disable interrupts may be executed multiple times in a row without intervening enable interrupts, in
564  * other words, the disable interrupts may be nested.
565  * The interrupt generation mechanism is disabled at the first call with HCF_ACT_INT_OFF.
566  * The interrupt generation mechanism is re-enabled when the number of calls with HCF_ACT_INT_ON matches the
567  * number of calls with INT_OFF.
568  *
569  * It is not allowed to have more Enable NIC Interrupts calls than Disable NIC Interrupts calls.
570  * The interrupt generation mechanism is initially (i.e. after hcf_connect) disabled.
571  * An MSF based on a interrupt strategy must call hcf_action with INT_ON in its initialization logic.
572  *
573  *!  The INT_OFF/INT_ON housekeeping is initialized at 0x0000 by hcf_connect, causing the interrupt generation
574  *   mechanism to be disabled at first. This suits MSF implementation based on a polling strategy.
575  *
576  * o HCF_ACT_CCX_OFF / HCF_ACT_CCX_ON
577  *!! This can use some more explanation;?
578  * Disables and Enables support in the HCF runtime code for the CCX feature. Each time one of these action
579  * codes is used, the effects of the preceding use cease.
580  *
581  * o HCF_ACT_SLEEP: Initiates the Disconnected DeepSleep process
582  * This command is only available if the HCF_DDS compile time option is set. It triggers the F/W to start the
583  * sleep handshaking. Regardless whether the Host initiates a Disconnected DeepSleep (DDS) or the F/W initiates
584  * a Connected DeepSleep (CDS), the Host-F/W sleep handshaking is completed when the NIC Interrupts mode is
585  * enabled (by means of the balancing HCF_ACT_INT_ON), i.e. at that moment the F/W really goes into sleep mode.
586  * The F/W is wokenup by the HCF when the NIC Interrupts mode are disabled, i.e. at the first HCF_ACT_INT_OFF
587  * after going into sleep.
588  *
589  * The following Miscellanuous actions are defined:
590  *
591  * o HCF_ACT_RX_ACK: Receiver Acknowledgement (non-DMA, non-USB mode only)
592  * Acking the receiver, frees the NIC memory used to hold the Rx frame and allows the F/W to
593  * report the existence of the next Rx frame.
594  * If the MSF does not need access (any longer) to the current frame, e.g. because it is rejected based on the
595  * look ahead or copied to another buffer, the receiver may be acked. Acking earlier is assumed to have the
596  * potential of improving the performance.
597  * If the MSF does not explitly ack te receiver, the acking is done implicitly if:
598  * - the received frame fits in the look ahead buffer, by the hcf_service_nic call that reported the Rx frame
599  * - if not in the above step, by hcf_rcv_msg (assuming hcf_rcv_msg is called)
600  * - if neither of the above implicit acks nor an explicit ack by the MSF, by the first hcf_service_nic after
601  *   the hcf_service_nic that reported the Rx frame.
602  * Note: If an Rx frame is already acked, an explicit ACK by the MSF acts as a NoOperation.
603  *
604  * o HCF_ACT_TALLIES: Inquire Tallies command
605  * This command is only operational if the F/W is enabled.
606  * The Inquire Tallies command requests the F/W to provide its current set of tallies.
607  * See also hcf_get_info with CFG_TALLIES as parameter.
608  *
609  * o HCF_ACT_PRS_SCAN: Inquire Probe Respons Scan command
610  * This command is only operational if the F/W is enabled.
611  * The Probe Respons Scan command starts a scan sequence.
612  * The HCF puts the result of this action in an MSF defined buffer (see CFG_RID_LOG_STRCT).
613  *
614  * o HCF_ACT_SCAN: Inquire Scan command
615  * This command is only supported for HII F/W (i.e. pre-WARP) and it is operational if the F/W is enabled.
616  * The Inquire Scan command starts a scan sequence.
617  * The HCF puts the result of this action in an MSF defined buffer (see CFG_RID_LOG_STRCT).
618  *
619  * Assert fails if
620  * - ifbp has a recognizable out-of-range value.
621  * - NIC interrupts are not disabled while required by parameter action.
622  * - an invalid code is specified in parameter action.
623  * - HCF_ACT_INT_ON commands outnumber the HCF_ACT_INT_OFF commands.
624  * - reentrancy, may be  caused by calling hcf_functions without adequate protection against NIC interrupts or
625  *   multi-threading
626  *
627  * - Since the HCF does not maintain status information relative to the F/W enabled state, it is not asserted
628  *   whether HCF_ACT_SCAN, HCF_ACT_PRS_SCAN or HCF_ACT_TALLIES are only used while F/W is enabled.
629  *
630  *.DIAGRAM
631  * 0: The assert embedded in HCFLOGENTRY checks against re-entrancy. Re-entrancy could be caused by a MSF logic
632  *   at task-level calling hcf_functions without shielding with HCF_ACT_ON/_OFF. However the HCF_ACT_INT_OFF
633  *   action itself can per definition not be protected this way. Based on code inspection, it can be concluded,
634  *   that there is no re-entrancy PROBLEM in this particular flow. It does not seem worth the trouble to
635  *   explicitly check for this condition (although there was a report of an MSF which ran into this assert.
636  * 2:IFB_IntOffCnt is used to balance the INT_OFF and INT_ON calls.  Disabling of the interrupts is achieved by
637  *   writing a zero to the Hermes IntEn register.  In a shared interrupt environment (e.g. the mini-PCI NDIS
638  *   driver) it is considered more correct to return the status HCF_INT_PENDING if and only if, the current
639  *   invocation of hcf_service_nic is (apparently) called in the ISR when the ISR was activated as result of a
640  *   change in HREG_EV_STAT matching a bit in HREG_INT_EN, i.e. not if invoked as result of another device
641  *   generating an interrupt on the shared interrupt line.
642  *   Note 1: it has been observed that under certain adverse conditions on certain platforms the writing of
643  *   HREG_INT_EN can apparently fail, therefor it is paramount that HREG_INT_EN is written again with 0 for
644  *   each and every call to HCF_ACT_INT_OFF.
645  *   Note 2: it has been observed that under certain H/W & S/W architectures this logic is called when there is
646  *   no NIC at all. To cater for this, the value of HREG_INT_EN is validated. If the unused bit 0x0100 is set,
647  *   it is assumed there is no NIC.
648  *   Note 3: During the download process, some versions of the F/W reset HREG_SW_0, hence checking this
649  *   register for HCF_MAGIC (the classical NIC presence test) when HCF_ACT_INT_OFF is called due to another
650  *   card interrupting via a shared IRQ during a download, fails.
651  *4: The construction "if ( ifbp->IFB_IntOffCnt-- == 0 )" is optimal (in the sense of shortest/quickest
652  *   path in error free flows) but NOT fail safe in case of too many INT_ON invocations compared to INT_OFF).
653  *   Enabling of the interrupts is achieved by writing the Hermes IntEn register.
654  *    - If the HCF is in Defunct mode, the interrupts stay disabled.
655  *    - Under "normal" conditions, the HCF is only interested in Info Events, Rx Events and Notify Events.
656  *    - When the HCF is out of Tx/Notify resources, the HCF is also interested in Alloc Events.
657  *    - via HCF_EXT, the MSF programmer can also request HREG_EV_TICK and/or HREG_EV_TX_EXC interrupts.
658  *   For DMA operation, the DMA hardware handles the alloc events. The DMA engine will generate a 'TxDmaDone'
659  *   event as soon as it has pumped a frame from host ram into NIC-RAM (note that the frame does not have to be
660  *   transmitted then), and a 'RxDmaDone' event as soon as a received frame has been pumped from NIC-RAM into
661  *   host ram.  Note that the 'alloc' event has been removed from the event-mask, because the DMA engine will
662  *   react to and acknowledge this event.
663  *6: ack the "old" Rx-event. See "Rx Buffer free strategy" in hcf_service_nic above for more explanation.
664  *   IFB_RxFID and IFB_RxLen must be cleared to bring both the internal HCF house keeping and the information
665  *   supplied to the MSF in the state "no frame received".
666  *8: The HCF_ACT_SCAN, HCF_ACT_PRS_SCAN and HCF_ACT_TALLIES activity are merged by "clever" algebraic
667  *   manipulations of the RID-values and action codes, so foregoing robustness against migration problems for
668  *   ease of implementation. The assumptions about numerical relationships between CFG_TALLIES etc and
669  *   HCF_ACT_TALLIES etc are checked by the "#if" statements just prior to the body of this routine, resulting
670  *   in: err "maintenance" during compilation if the assumptions are no longer met. The writing of HREG_PARAM_1
671  *   with 0x3FFF in case of an PRS scan, is a kludge to get around lack of specification, hence different
672  *   implementation in F/W and Host.
673  *   When there is no NIC RAM available, some versions of the Hermes F/W do report 0x7F00 as error in the
674  *   Result field of the Status register and some F/W versions don't. To mask this difference to the MSF all
675  *   return codes of the Hermes are ignored ("best" and "most simple" solution to these types of analomies with
676  *   an acceptable loss due to ignoring all error situations as well).
677  *   The "No inquire space" is reported via the Hermes tallies.
678  *30: do not HCFASSERT( rc, rc ) since rc == HCF_INT_PENDING is no error
679  *
680  *.ENDDOC                END DOCUMENTATION
681  *
682  ************************************************************************************************************/
683 #if ( (HCF_TYPE) & HCF_TYPE_HII5 ) == 0
684 #if CFG_SCAN != CFG_TALLIES - HCF_ACT_TALLIES + HCF_ACT_SCAN
685 err: "maintenance" apparently inviolated the underlying assumption about the numerical values of these macros
686 #endif
687 #endif // HCF_TYPE_HII5
688 #if CFG_PRS_SCAN != CFG_TALLIES - HCF_ACT_TALLIES + HCF_ACT_PRS_SCAN
689 err: "maintenance" apparently inviolated the underlying assumption about the numerical values of these macros
690 #endif
691 int
692 hcf_action( IFBP ifbp, hcf_16 action )
693 {
694         int rc = HCF_SUCCESS;
695
696         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
697 #if HCF_INT_ON
698         HCFLOGENTRY( action == HCF_ACT_INT_FORCE_ON ? HCF_TRACE_ACTION_KLUDGE : HCF_TRACE_ACTION, action );                                                      /* 0 */
699 #if (HCF_SLEEP)
700         HCFASSERT( ifbp->IFB_IntOffCnt != 0xFFFE || action == HCF_ACT_INT_OFF,
701                    MERGE_2( action, ifbp->IFB_IntOffCnt ) );
702 #else
703         HCFASSERT( ifbp->IFB_IntOffCnt != 0xFFFE, action );
704 #endif // HCF_SLEEP
705         HCFASSERT( ifbp->IFB_IntOffCnt != 0xFFFF ||
706                    action == HCF_ACT_INT_OFF || action == HCF_ACT_INT_FORCE_ON,  action );
707         HCFASSERT( ifbp->IFB_IntOffCnt <= 16 || ifbp->IFB_IntOffCnt >= 0xFFFE,
708                    MERGE_2( action, ifbp->IFB_IntOffCnt ) ); //nesting more than 16 deep seems unreasonable
709 #endif // HCF_INT_ON
710
711         switch (action) {
712 #if HCF_INT_ON
713                 hcf_16  i;
714         case HCF_ACT_INT_OFF:                     // Disable Interrupt generation
715 #if HCF_SLEEP
716                 if ( ifbp->IFB_IntOffCnt == 0xFFFE ) {  // WakeUp test  ;?tie this to the "new" super-LinkStat
717                         ifbp->IFB_IntOffCnt++;                      // restore conventional I/F
718                         OPW(HREG_IO, HREG_IO_WAKEUP_ASYNC );        // set wakeup bit
719                         OPW(HREG_IO, HREG_IO_WAKEUP_ASYNC );        // set wakeup bit to counteract the clearing by F/W
720                         // 800 us latency before FW switches to high power
721                         MSF_WAIT(800);                              // MSF-defined function to wait n microseconds.
722 //OOR                   if ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_DS_OOR ) { // OutOfRange
723 //                              printk( "<5>ACT_INT_OFF: Deepsleep phase terminated, enable and go to AwaitConnection\n" );     //;?remove me 1 day
724 //                              hcf_cntl( ifbp, HCF_CNTL_ENABLE );
725 //                      }
726 //                      ifbp->IFB_DSLinkStat &= ~( CFG_LINK_STAT_DS_IR | CFG_LINK_STAT_DS_OOR); //clear IR/OOR state
727                 }
728 #endif // HCF_SLEEP
729         /*2*/   ifbp->IFB_IntOffCnt++;
730 //!             rc = 0;
731                 i = IPW( HREG_INT_EN );
732                 OPW( HREG_INT_EN, 0 );
733                 if ( i & 0x1000 ) {
734                         rc = HCF_ERR_NO_NIC;
735                 } else {
736                         if ( i & IPW( HREG_EV_STAT ) ) {
737                                 rc = HCF_INT_PENDING;
738                         }
739                 }
740                 break;
741
742         case HCF_ACT_INT_FORCE_ON:                // Enforce Enable Interrupt generation
743                 ifbp->IFB_IntOffCnt = 0;
744                 //Fall through in HCF_ACT_INT_ON
745
746         case HCF_ACT_INT_ON:                      // Enable Interrupt generation
747         /*4*/   if ( ifbp->IFB_IntOffCnt-- == 0 && ifbp->IFB_CardStat == 0 ) {
748                                                   //determine Interrupt Event mask
749 #if HCF_DMA
750                         if ( ifbp->IFB_CntlOpt & USE_DMA ) {
751                                 i = HREG_EV_INFO | HREG_EV_RDMAD | HREG_EV_TDMAD | HREG_EV_TX_EXT;  //mask when DMA active
752                         } else
753 #endif // HCF_DMA
754                         {
755                                 i = HREG_EV_INFO | HREG_EV_RX | HREG_EV_TX_EXT;                     //mask when DMA not active
756                                 if ( ifbp->IFB_RscInd == 0 ) {
757                                         i |= HREG_EV_ALLOC;                                         //mask when no TxFID available
758                                 }
759                         }
760 #if HCF_SLEEP
761                         if ( ( IPW(HREG_EV_STAT) & ( i | HREG_EV_SLEEP_REQ ) ) == HREG_EV_SLEEP_REQ ) {
762                                 // firmware indicates it would like to go into sleep modus
763                                 // only acknowledge this request if no other events that can cause an interrupt are pending
764                                 ifbp->IFB_IntOffCnt--;          //becomes 0xFFFE
765                                 OPW( HREG_INT_EN, i | HREG_EV_TICK );
766                                 OPW( HREG_EV_ACK, HREG_EV_SLEEP_REQ | HREG_EV_TICK | HREG_EV_ACK_REG_READY );
767                         } else
768 #endif // HCF_SLEEP
769                         {
770                                 OPW( HREG_INT_EN, i | HREG_EV_SLEEP_REQ );
771                         }
772                 }
773                 break;
774 #endif // HCF_INT_ON
775
776 #if (HCF_SLEEP) & HCF_DDS
777         case HCF_ACT_SLEEP:                       // DDS Sleep request
778                 hcf_cntl( ifbp, HCF_CNTL_DISABLE );
779                 cmd_exe( ifbp, HCMD_SLEEP, 0 );
780                 break;
781 //      case HCF_ACT_WAKEUP:                      // DDS Wakeup request
782 //              HCFASSERT( ifbp->IFB_IntOffCnt == 0xFFFE, ifbp->IFB_IntOffCnt );
783 //              ifbp->IFB_IntOffCnt++;                  // restore conventional I/F
784 //              OPW( HREG_IO, HREG_IO_WAKEUP_ASYNC );
785 //              MSF_WAIT(800);                          // MSF-defined function to wait n microseconds.
786 //              rc = hcf_action( ifbp, HCF_ACT_INT_OFF );   /*bogus, IFB_IntOffCnt == 0xFFFF, so if you carefully look
787 //                                                           *at the #if HCF_DDS statements, HCF_ACT_INT_OFF is empty
788 //                                                           *for DDS. "Much" better would be to merge the flows for
789 //                                                           *DDS and DEEP_SLEEP
790 //                                                           */
791 //              break;
792 #endif // HCF_DDS
793
794 #if (HCF_TYPE) & HCF_TYPE_CCX
795         case HCF_ACT_CCX_ON:                      // enable CKIP
796         case HCF_ACT_CCX_OFF:                     // disable CKIP
797                 ifbp->IFB_CKIPStat = action;
798                 break;
799 #endif // HCF_TYPE_CCX
800
801         case HCF_ACT_RX_ACK:                      //Receiver ACK
802         /*6*/   if ( ifbp->IFB_RxFID ) {
803                         DAWA_ACK( HREG_EV_RX );
804                 }
805                 ifbp->IFB_RxFID = ifbp->IFB_RxLen = 0;
806                 break;
807
808   /*8*/ case  HCF_ACT_PRS_SCAN:                   // Hermes PRS Scan (F102)
809                 OPW( HREG_PARAM_1, 0x3FFF );
810                 //Fall through in HCF_ACT_TALLIES
811         case HCF_ACT_TALLIES:                     // Hermes Inquire Tallies (F100)
812 #if ( (HCF_TYPE) & HCF_TYPE_HII5 ) == 0
813         case HCF_ACT_SCAN:                        // Hermes Inquire Scan (F101)
814 #endif // HCF_TYPE_HII5
815                 /*!! the assumptions about numerical relationships between CFG_TALLIES etc and HCF_ACT_TALLIES etc
816                  *   are checked by #if statements just prior to this routine resulting in: err "maintenance"   */
817                 cmd_exe( ifbp, HCMD_INQUIRE, action - HCF_ACT_TALLIES + CFG_TALLIES );
818                 break;
819
820         default:
821                 HCFASSERT( DO_ASSERT, action );
822                 break;
823         }
824         //! do not HCFASSERT( rc == HCF_SUCCESS, rc )                                                       /* 30*/
825         HCFLOGEXIT( HCF_TRACE_ACTION );
826         return rc;
827 } // hcf_action
828
829
830 /************************************************************************************************************
831  *
832  *.MODULE        int hcf_cntl( IFBP ifbp, hcf_16 cmd )
833  *.PURPOSE       Connect or disconnect a specific port to a specific network.
834  *!!  ;???????????????? continue needs more explanation
835  *               recovers by means of "continue" when the connect process in CCX mode fails
836  *               Enables or disables data transmission and reception for the NIC.
837  *               Activates static NIC configuration for a specific port at connect.
838  *               Activates static configuration for all ports at enable.
839  *
840  *.ARGUMENTS
841  *   ifbp        address of the Interface Block
842  *   cmd         0x001F: Hermes command (disable, enable, connect, disconnect, continue)
843  *                   HCF_CNTL_ENABLE     Enable
844  *                   HCF_CNTL_DISABLE    Disable
845  *                   HCF_CNTL_CONTINUE   Continue
846  *                   HCF_CNTL_CONNECT    Connect
847  *                   HCF_CNTL_DISCONNECT Disconnect
848  *               0x0100: command qualifier (continue)
849  *                   HCMD_RETRY          retry flag
850  *               0x0700:  port number (connect/disconnect)
851  *                   HCF_PORT_0          MAC Port 0
852  *                   HCF_PORT_1          MAC Port 1
853  *                   HCF_PORT_2          MAC Port 2
854  *                   HCF_PORT_3          MAC Port 3
855  *                   HCF_PORT_4          MAC Port 4
856  *                   HCF_PORT_5          MAC Port 5
857  *                   HCF_PORT_6          MAC Port 6
858  *
859  *.RETURNS
860  *   HCF_SUCCESS
861  *!! via cmd_exe
862  *   HCF_ERR_NO_NIC
863  *   HCF_ERR_DEFUNCT_...
864  *   HCF_ERR_TIME_OUT
865  *
866  *.DESCRIPTION
867  * The parameter cmd contains a number of subfields.
868  * The actual value for cmd is created by logical or-ing the appropriate mnemonics for the subfields.
869  * The field 0x001F contains the command code
870  *  - HCF_CNTL_ENABLE
871  *  - HCF_CNTL_DISABLE
872  *  - HCF_CNTL_CONNECT
873  *  - HCF_CNTL_DISCONNECT
874  *  - HCF_CNTL_CONTINUE
875  *
876  * For HCF_CNTL_CONTINUE, the field 0x0100 contains the retry flag HCMD_RETRY.
877  * For HCF_CNTL_CONNECT and HCF_CNTL_DISCONNECT, the field 0x0700 contains the port number as HCF_PORT_#.
878  * For Station as well as AccessPoint F/W, MAC Port 0 is the "normal" communication channel.
879  * For AccessPoint F/W, MAC Port 1 through 6 control the WDS links.
880  *
881  * Note that despite the names HCF_CNTL_DISABLE and HCF_CNTL_ENABLE, hcf_cntl does not influence the NIC
882  * Interrupts mode.
883  *
884  * The Connect is used by the MSF to bring a particular port in an inactive state as far as data transmission
885  * and reception are concerned.
886  * When a particular port is disconnected:
887  * - the F/W disables the receiver for that port.
888  * - the F/W ignores send commands for that port.
889  * - all frames (Receive as well as pending Transmit) for that port on the NIC are discarded.
890  *
891  * When the NIC is disabled, above list applies to all ports, i.e. the result is like all ports are
892  * disconnected.
893  *
894  * When a particular port is connected:
895  * - the F/W effectuates the static configuration for that port.
896  * - enables the receiver for that port.
897  * - accepts send commands for that port.
898  *
899  * Enabling has the following effects:
900  * - the F/W effectuates the static configuration for all ports.
901  *   The F/W only updates its static configuration at a transition from disabled to enabled or from
902  *   disconnected to connected.
903  *   In order to enforce the static configuration, the MSF must assure that such a transition takes place.
904  *   Due to such a disable/enable or disconnect/connect sequence, Rx/Tx frames may be lost, in other words,
905  *   configuration may impact communication.
906  * - The DMA Engine (if applicable) is enabled.
907  * Note that the Enable Function by itself only enables data transmission and reception, it
908  * does not enable the Interrupt Generation mechanism. This is done by hcf_action.
909  *
910  * Disabling has the following effects:
911  *!!  ;?????is the following statement really true
912  * - it acts as a disconnect on all ports.
913  * - The DMA Engine (if applicable) is disabled.
914  *
915  * For impact of the disable command on the behavior of hcf_dma_tx/rx_get see the appropriate sections.
916  *
917  * Although the Enable/Disable and Connect/Disconnect are antonyms, there is no restriction on their sequencing,
918  * in other words, they may be called multiple times in arbitrary sequence without being paired or balanced.
919  * Each time one of these functions is called, the effects of the preceding calls cease.
920  *
921  * Assert fails if
922  * - ifbp has a recognizable out-of-range value.
923  * - NIC interrupts are not disabled.
924  * - A command other than Continue, Enable, Disable, Connect or Disconnect is given.
925  * - An invalid combination of the subfields is given or a bit outside the subfields is given.
926  * - any return code besides HCF_SUCCESS.
927  * - reentrancy, may be  caused by calling a hcf_function without adequate protection against NIC interrupts or
928  *   multi-threading
929  *
930  *.DIAGRAM
931  *   hcf_cntl takes successively the following actions:
932  *2: If the HCF is in Defunct mode or incompatible with the Primary or Station Supplier in the Hermes,
933  *   hcf_cntl() returns immediately with HCF_ERR_NO_NIC;? as status.
934  *8: when the port is disabled, the DMA engine needs to be de-activated, so the host can safely reclaim tx
935  *   packets from the tx descriptor chain.
936  *
937  *.ENDDOC                END DOCUMENTATION
938  *
939  ************************************************************************************************************/
940 int
941 hcf_cntl( IFBP ifbp, hcf_16 cmd )
942 {
943         int rc = HCF_ERR_INCOMP_FW;
944 #if HCF_ASSERT
945         {   int x = cmd & HCMD_CMD_CODE;
946                 if ( x == HCF_CNTL_CONTINUE ) x &= ~HCMD_RETRY;
947                 else if ( (x == HCMD_DISABLE || x == HCMD_ENABLE) && ifbp->IFB_FWIdentity.comp_id == COMP_ID_FW_AP ) {
948                         x &= ~HFS_TX_CNTL_PORT;
949                 }
950                 HCFASSERT( x==HCF_CNTL_ENABLE  || x==HCF_CNTL_DISABLE    || HCF_CNTL_CONTINUE ||
951                            x==HCF_CNTL_CONNECT || x==HCF_CNTL_DISCONNECT, cmd );
952         }
953 #endif // HCF_ASSERT
954 // #if (HCF_SLEEP) & HCF_DDS
955 //      HCFASSERT( ifbp->IFB_IntOffCnt != 0xFFFE, cmd );
956 // #endif // HCF_DDS
957         HCFLOGENTRY( HCF_TRACE_CNTL, cmd );
958         if ( ifbp->IFB_CardStat == 0 ) {                                                                 /*2*/
959         /*6*/   rc = cmd_exe( ifbp, cmd, 0 );
960 #if (HCF_SLEEP) & HCF_DDS
961                 ifbp->IFB_TickCnt = 0;              //start 2 second period (with 1 tick uncertanty)
962 #endif // HCF_DDS
963         }
964 #if HCF_DMA
965         //!rlav : note that this piece of code is always executed, regardless of the DEFUNCT bit in IFB_CardStat.
966         // The reason behind this is that the MSF should be able to get all its DMA resources back from the HCF,
967         // even if the hardware is disfunctional. Practical example under Windows : surprise removal.
968         if ( ifbp->IFB_CntlOpt & USE_DMA ) {
969                 hcf_io io_port = ifbp->IFB_IOBase;
970                 DESC_STRCT *p;
971                 if ( cmd == HCF_CNTL_DISABLE || cmd == HCF_CNTL_ENABLE ) {
972                         OUT_PORT_DWORD( (io_port + HREG_DMA_CTRL), DMA_CTRLSTAT_RESET);                     /*8*/
973                         ifbp->IFB_CntlOpt &= ~DMA_ENABLED;
974                 }
975                 if ( cmd == HCF_CNTL_ENABLE ) {
976                         OUT_PORT_DWORD( (io_port + HREG_DMA_CTRL), DMA_CTRLSTAT_GO);
977                         /* ;? by rewriting hcf_dma_rx_put you can probably just call hcf_dma_rx_put( ifbp->IFB_FirstDesc[DMA_RX] )
978                          * as additional beneficiary side effect, the SOP and EOP bits will also be cleared
979                          */
980                         ifbp->IFB_CntlOpt |= DMA_ENABLED;
981                         HCFASSERT( NT_ASSERT, NEVER_TESTED );
982                         // make the entire rx descriptor chain DMA-owned, so the DMA engine can (re-)use it.
983                         p = ifbp->IFB_FirstDesc[DMA_RX];
984                         if (p != NULL) {   //;? Think this over again in the light of the new chaining strategy
985                                 if ( 1 )    { //begin alternative
986                                         HCFASSERT( NT_ASSERT, NEVER_TESTED );
987                                         put_frame_lst( ifbp, ifbp->IFB_FirstDesc[DMA_RX], DMA_RX );
988                                         if ( ifbp->IFB_FirstDesc[DMA_RX] ) {
989                                                 put_frame_lst( ifbp, ifbp->IFB_FirstDesc[DMA_RX]->next_desc_addr, DMA_RX );
990                                         }
991                                 } else {
992                                         while ( p ) {
993                                                 //p->buf_cntl.cntl_stat |= DESC_DMA_OWNED;
994                                                 p->BUF_CNT |= DESC_DMA_OWNED;
995                                                 p = p->next_desc_addr;
996                                         }
997                                         // a rx chain is available so hand it over to the DMA engine
998                                         p = ifbp->IFB_FirstDesc[DMA_RX];
999                                         OUT_PORT_DWORD( (io_port + HREG_RXDMA_PTR32), p->desc_phys_addr);
1000                                 }  //end alternative
1001                         }
1002                 }
1003         }
1004 #endif // HCF_DMA
1005         HCFASSERT( rc == HCF_SUCCESS, rc );
1006         HCFLOGEXIT( HCF_TRACE_CNTL );
1007         return rc;
1008 } // hcf_cntl
1009
1010
1011 /************************************************************************************************************
1012  *
1013  *.MODULE        int hcf_connect( IFBP ifbp, hcf_io io_base )
1014  *.PURPOSE       Grants access right for the HCF to the IFB.
1015  *               Initializes Card and HCF housekeeping.
1016  *
1017  *.ARGUMENTS
1018  *   ifbp        (near) address of the Interface Block
1019  *   io_base     non-USB: I/O Base address of the NIC (connect)
1020  *               non-USB: HCF_DISCONNECT
1021  *               USB:     HCF_CONNECT, HCF_DISCONNECT
1022  *
1023  *.RETURNS
1024  *   HCF_SUCCESS
1025  *   HCF_ERR_INCOMP_PRI
1026  *   HCF_ERR_INCOMP_FW
1027  *   HCF_ERR_DEFUNCT_CMD_SEQ
1028  *!! HCF_ERR_NO_NIC really returned ;?
1029  *   HCF_ERR_NO_NIC
1030  *   HCF_ERR_TIME_OUT
1031  *
1032  *   MSF-accessible fields of Result Block:
1033  *   IFB_IOBase              entry parameter io_base
1034  *   IFB_IORange             HREG_IO_RANGE (0x40/0x80)
1035  *   IFB_Version             version of the IFB layout
1036  *   IFB_FWIdentity          CFG_FW_IDENTITY_STRCT, specifies the identity of the
1037  *                           "running" F/W, i.e. tertiary F/W under normal conditions
1038  *   IFB_FWSup               CFG_SUP_RANGE_STRCT, specifies the supplier range of
1039  *                           the "running" F/W, i.e. tertiary F/W under normal conditions
1040  *   IFB_HSISup              CFG_SUP_RANGE_STRCT, specifies the HW/SW I/F range of the NIC
1041  *   IFB_PRIIdentity         CFG_PRI_IDENTITY_STRCT, specifies the Identity of the Primary F/W
1042  *   IFB_PRISup              CFG_SUP_RANGE_STRCT, specifies the supplier range of the Primary F/W
1043  *   all other               all MSF accessible fields, which are not specified above, are zero-filled
1044  *
1045  *.CONDITIONS
1046  * It is the responsibility of the MSF to assure the correctness of the I/O Base address.
1047  *
1048  * Note: hcf_connect defaults to NIC interrupt disabled mode, i.e. as if hcf_action( HCF_ACT_INT_OFF )
1049  * was called.
1050  *
1051  *.DESCRIPTION
1052  * hcf_connect passes the MSF-defined location of the IFB to the HCF and grants or revokes access right for the
1053  * HCF to the IFB. Revoking is done by specifying HCF_DISCONNECT rather than an I/O address for the parameter
1054  * io_base.  Every call of hcf_connect in "connect" mode, must eventually be followed by a call of hcf_connect
1055  * in "disconnect" mode. Clalling hcf_connect in "connect"/"disconnect" mode can not be nested.
1056  * The IFB address must be used as a handle with all subsequent HCF-function calls and the HCF uses the IFB
1057  * address as a handle when it performs a call(back) of an MSF-function (i.e. msf_assert).
1058  *
1059  * Note that not only the MSF accessible fields are cleared, but also all internal housekeeping
1060  * information is re-initialized.
1061  * This implies that all settings which are done via hcf_action and hcf_put_info (e.g. CFG_MB_ASSERT, CFG_REG_MB,
1062  * CFG_REG_INFO_LOG) must be done again. The only field which is not cleared, is IFB_MSFSup.
1063  *
1064  * If HCF_INT_ON is selected as compile option, NIC interrupts are disabled.
1065  *
1066  * Assert fails if
1067  * - ifbp is not properly aligned ( ref chapter HCF_ALIGN in 4.1.1)
1068  * - I/O Base Address is not a multiple of 0x40 (note: 0x0000 is explicitly allowed).
1069  *
1070  *.DIAGRAM
1071  *
1072  *0: Throughout hcf_connect you need to distinguish the connect from the disconnect case, which requires
1073  *   some attention about what to use as "I/O" address when for which purpose.
1074  *2:
1075  *2a: Reset H-II by toggling reset bit in IO-register on and off.
1076  *   The HCF_TYPE_PRELOADED caters for the DOS environment where H-II is loaded by a separate program to
1077  *   overcome the 64k size limit posed on DOS drivers.
1078  *   The macro OPW is not yet useable because the IFB_IOBase field is not set.
1079  *   Note 1: hopefully the clearing and initializing of the IFB (see below) acts as a delay which meets the
1080  *   specification for S/W reset
1081  *   Note 2: it turns out that on some H/W constellations, the clock to access the EEProm is not lowered
1082  *   to an appropriate frequency by HREG_IO_SRESET. By giving an HCMD_INI first, this problem is worked around.
1083  *2b: Experimentally it is determined over a wide range of F/W versions that waiting for the for Cmd bit in
1084  *   Ev register gives a workable strategy. The available documentation does not give much clues.
1085  *4: clear and initialize the IFB
1086  *   The HCF house keeping info is designed such that zero is the appropriate initial value for as much as
1087  *   feasible IFB-items.
1088  *   The readable fields mentioned in the description section and some HCF specific fields are given their
1089  *   actual value.
1090  *   IFB_TickIni is initialized at best guess before calibration
1091  *   Hcf_connect defaults to "no interrupt generation" (implicitly achieved by the zero-filling).
1092  *6: Register compile-time linked MSF Routine and set default filter level
1093  *   cast needed to get around the "near" problem in DOS COM model
1094  *   er C2446: no conversion from void (__near __cdecl *)(unsigned char __far *,unsigned int,unsigned short,int)
1095  *                           to   void (__far  __cdecl *)(unsigned char __far *,unsigned int,unsigned short,int)
1096  *8: If a command is apparently still active (as indicated by the Busy bit in Cmd register) this may indicate a
1097  *   blocked cmd pipe line.  To unblock the following actions are done:
1098  *    - Ack everything
1099  *    - Wait for Busy bit drop  in Cmd register
1100  *    - Wait for Cmd  bit raise in Ev  register
1101  *   The two waits are combined in a single HCF_WAIT_WHILE to optimize memory size. If either of these waits
1102  *   fail (prot_cnt becomes 0), then something is serious wrong. Rather than PANICK, the assumption is that the
1103  *   next cmd_exe will fail, causing the HCF to go into DEFUNCT mode
1104  *10:    Ack everything to unblock a (possibly blocked) cmd pipe line
1105  *   Note 1: it is very likely that an Alloc event is pending and very well possible that a (Send) Cmd event is
1106  *   pending on non-initial calls
1107  *   Note 2: it is assumed that this strategy takes away the need to ack every conceivable event after an
1108  *   Hermes Initialize
1109  *12:    Only H-II NEEDS the Hermes Initialize command. Due to the different semantics for H-I and H-II
1110  *   Initialize command, init() does not (and can not, since it is called e.g. after a download) execute the
1111  *   Hermes Initialize command. Executing the Hermes Initialize command for H-I would not harm but not do
1112  *   anything useful either, so it is skipped.
1113  *   The return status of cmd_exe is ignored. It is assumed that if cmd_exe fails, init fails too
1114  *14:    use io_base as a flag to merge hcf_connect and hcf_disconnect into 1 routine
1115  *   the call to init and its subsequent call of cmd_exe will return HCF_ERR_NO_NIC if appropriate. This status
1116  *   is (badly) needed by some legacy combination of NT4 and card services which do not yield an I/O address in
1117  *   time.
1118  *
1119  *.NOTICE
1120  *   On platforms where the NULL-pointer is not a bit-pattern of all zeros, the zero-filling of the IFB results
1121  *   in an incorrect initialization of pointers.
1122  *   The implementation of the MailBox manipulation in put_mb_info protects against the absence of a MailBox
1123  *   based on IFB_MBSize, IFB_MBWp and ifbp->IFB_MBRp. This has ramifications on the initialization of the
1124  *   MailBox via hcf_put_info with the CFG_REG_MB type, but it prevents dependency on the "NULL-"ness of
1125  *   IFB_MBp.
1126  *
1127  *.NOTICE
1128  *   There are a number of problems when asserting and logging hcf_connect, e.g.
1129  *    - Asserting on re-entrancy of hcf_connect by means of
1130  *    "HCFASSERT( (ifbp->IFB_AssertTrace & HCF_ASSERT_CONNECT) == 0, 0 )" is not useful because IFB contents
1131  *    are undefined
1132  *    - Asserting before the IFB is cleared will cause mdd_assert() to interpret the garbage in IFB_AssertRtn
1133  *    as a routine address
1134  *   Therefore HCFTRACE nor HCFLOGENTRY is called by hcf_connect.
1135  *.ENDDOC                END DOCUMENTATION
1136  *
1137  ************************************************************************************************************/
1138 int
1139 hcf_connect( IFBP ifbp, hcf_io io_base )
1140 {
1141         int         rc = HCF_SUCCESS;
1142         hcf_io      io_addr;
1143         hcf_32      prot_cnt;
1144         hcf_8       *q;
1145         LTV_STRCT   x;
1146 #if HCF_ASSERT
1147         hcf_16 xa = ifbp->IFB_FWIdentity.typ;
1148         /* is assumed to cause an assert later on if hcf_connect is called without intervening hcf_disconnect.
1149          * xa == CFG_FW_IDENTITY in subsequent calls without preceding hcf_disconnect,
1150          * xa == 0 in subsequent calls with preceding hcf_disconnect,
1151          * xa == "garbage" (any value except CFG_FW_IDENTITY is acceptable) in the initial call
1152          */
1153 #endif // HCF_ASSERT
1154
1155         if ( io_base == HCF_DISCONNECT ) {                  //disconnect
1156                 io_addr = ifbp->IFB_IOBase;
1157                 OPW( HREG_INT_EN, 0 );      //;?workaround against dying F/W on subsequent hcf_connect calls
1158         } else {                                            //connect                               /* 0 */
1159                 io_addr = io_base;
1160         }
1161
1162 #if 0 //;? if a subsequent hcf_connect is preceded by an hcf_disconnect the wakeup is not needed !!
1163 #if HCF_SLEEP
1164         OUT_PORT_WORD( .....+HREG_IO, HREG_IO_WAKEUP_ASYNC );       //OPW not yet useable
1165         MSF_WAIT(800);                              // MSF-defined function to wait n microseconds.
1166         note that MSF_WAIT uses not yet defined!!!! IFB_IOBase and IFB_TickIni (via PROT_CNT_INI)
1167         so be careful if this code is restored
1168 #endif // HCF_SLEEP
1169 #endif // 0
1170
1171 #if ( (HCF_TYPE) & HCF_TYPE_PRELOADED ) == 0    //switch clock back for SEEPROM access  !!!
1172         OUT_PORT_WORD( io_addr + HREG_CMD, HCMD_INI );          //OPW not yet useable
1173         prot_cnt = INI_TICK_INI;
1174         HCF_WAIT_WHILE( (IN_PORT_WORD( io_addr +  HREG_EV_STAT) & HREG_EV_CMD) == 0 );
1175         OUT_PORT_WORD( (io_addr + HREG_IO), HREG_IO_SRESET );   //OPW not yet useable                   /* 2a*/
1176 #endif // HCF_TYPE_PRELOADED
1177         for ( q = (hcf_8*)(&ifbp->IFB_Magic); q > (hcf_8*)ifbp; *--q = 0 ) /*NOP*/;                     /* 4 */
1178         ifbp->IFB_Magic     = HCF_MAGIC;
1179         ifbp->IFB_Version   = IFB_VERSION;
1180 #if defined MSF_COMPONENT_ID //a new IFB demonstrates how dirty the solution is
1181         xxxx[xxxx_PRI_IDENTITY_OFFSET] = NULL;      //IFB_PRIIdentity placeholder   0xFD02
1182         xxxx[xxxx_PRI_IDENTITY_OFFSET+1] = NULL;    //IFB_PRISup placeholder        0xFD03
1183 #endif // MSF_COMPONENT_ID
1184 #if (HCF_TALLIES) & ( HCF_TALLIES_NIC | HCF_TALLIES_HCF )
1185         ifbp->IFB_TallyLen = 1 + 2 * (HCF_NIC_TAL_CNT + HCF_HCF_TAL_CNT);   //convert # of Tallies to L value for LTV
1186         ifbp->IFB_TallyTyp = CFG_TALLIES;           //IFB_TallyTyp: set T value
1187 #endif // HCF_TALLIES_NIC / HCF_TALLIES_HCF
1188         ifbp->IFB_IOBase    = io_addr;              //set IO_Base asap, so asserts via HREG_SW_2 don't harm
1189         ifbp->IFB_IORange   = HREG_IO_RANGE;
1190         ifbp->IFB_CntlOpt   = USE_16BIT;
1191 #if HCF_ASSERT
1192         assert_ifbp = ifbp;
1193         ifbp->IFB_AssertLvl = 1;
1194 #if (HCF_ASSERT) & HCF_ASSERT_LNK_MSF_RTN
1195         if ( io_base != HCF_DISCONNECT ) {
1196                 ifbp->IFB_AssertRtn = (MSF_ASSERT_RTNP)msf_assert;                                          /* 6 */
1197         }
1198 #endif // HCF_ASSERT_LNK_MSF_RTN
1199 #if (HCF_ASSERT) & HCF_ASSERT_MB                //build the structure to pass the assert info to hcf_put_info
1200         ifbp->IFB_AssertStrct.len = sizeof(ifbp->IFB_AssertStrct)/sizeof(hcf_16) - 1;
1201         ifbp->IFB_AssertStrct.typ = CFG_MB_INFO;
1202         ifbp->IFB_AssertStrct.base_typ = CFG_MB_ASSERT;
1203         ifbp->IFB_AssertStrct.frag_cnt = 1;
1204         ifbp->IFB_AssertStrct.frag_buf[0].frag_len =
1205                 ( offsetof(IFB_STRCT, IFB_AssertLvl) - offsetof(IFB_STRCT, IFB_AssertLine) ) / sizeof(hcf_16);
1206         ifbp->IFB_AssertStrct.frag_buf[0].frag_addr = &ifbp->IFB_AssertLine;
1207 #endif // HCF_ASSERT_MB
1208 #endif // HCF_ASSERT
1209         IF_PROT_TIME( prot_cnt = ifbp->IFB_TickIni = INI_TICK_INI );
1210 #if ( (HCF_TYPE) & HCF_TYPE_PRELOADED ) == 0
1211         //!! No asserts before Reset-bit in HREG_IO is cleared
1212         OPW( HREG_IO, 0x0000 );                     //OPW useable                                       /* 2b*/
1213         HCF_WAIT_WHILE( (IPW( HREG_EV_STAT) & HREG_EV_CMD) == 0 );
1214         IF_PROT_TIME( HCFASSERT( prot_cnt, IPW( HREG_EV_STAT) ) );
1215         IF_PROT_TIME( if ( prot_cnt ) prot_cnt = ifbp->IFB_TickIni );
1216 #endif // HCF_TYPE_PRELOADED
1217         //!! No asserts before Reset-bit in HREG_IO is cleared
1218         HCFASSERT( DO_ASSERT, MERGE_2( HCF_ASSERT, 0xCAF0 ) ); //just to proof that the complete assert machinery is working
1219         HCFASSERT( xa != CFG_FW_IDENTITY, 0 );       // assert if hcf_connect is called without intervening hcf_disconnect.
1220         HCFASSERT( ((hcf_32)(void*)ifbp & (HCF_ALIGN-1) ) == 0, (hcf_32)(void*)ifbp );
1221         HCFASSERT( (io_addr & 0x003F) == 0, io_addr );
1222                                                 //if Busy bit in Cmd register
1223         if (IPW( HREG_CMD ) & HCMD_BUSY ) {                                                             /* 8 */
1224                 //.  Ack all to unblock a (possibly) blocked cmd pipe line
1225                 OPW( HREG_EV_ACK, ~HREG_EV_SLEEP_REQ );
1226                                                 //.  Wait for Busy bit drop  in Cmd register
1227                                                 //.  Wait for Cmd  bit raise in Ev  register
1228                 HCF_WAIT_WHILE( ( IPW( HREG_CMD ) & HCMD_BUSY ) && (IPW( HREG_EV_STAT) & HREG_EV_CMD) == 0 );
1229                 IF_PROT_TIME( HCFASSERT( prot_cnt, IPW( HREG_EV_STAT) ) ); /* if prot_cnt == 0, cmd_exe will fail, causing DEFUNCT */
1230         }
1231         OPW( HREG_EV_ACK, ~HREG_EV_SLEEP_REQ );
1232 #if ( (HCF_TYPE) & HCF_TYPE_PRELOADED ) == 0                                                        /*12*/
1233         (void)cmd_exe( ifbp, HCMD_INI, 0 );
1234 #endif // HCF_TYPE_PRELOADED
1235         if ( io_base != HCF_DISCONNECT ) {
1236                 rc = init( ifbp );                                                                          /*14*/
1237                 if ( rc == HCF_SUCCESS ) {
1238                         x.len = 2;
1239                         x.typ = CFG_NIC_BUS_TYPE;
1240                         (void)hcf_get_info( ifbp, &x );
1241                         ifbp->IFB_BusType = x.val[0];
1242                         //CFG_NIC_BUS_TYPE not supported -> default 32 bits/DMA, MSF has to overrule via CFG_CNTL_OPT
1243                         if ( x.len == 0 || x.val[0] == 0x0002 || x.val[0] == 0x0003 ) {
1244 #if (HCF_IO) & HCF_IO_32BITS
1245                                 ifbp->IFB_CntlOpt &= ~USE_16BIT;            //reset USE_16BIT
1246 #endif // HCF_IO_32BITS
1247 #if HCF_DMA
1248                                 ifbp->IFB_CntlOpt |= USE_DMA;               //SET DMA
1249 #else
1250                                 ifbp->IFB_IORange = 0x40 /*i.s.o. HREG_IO_RANGE*/;
1251 #endif // HCF_DMA
1252                         }
1253                 }
1254         } else HCFASSERT(  ( ifbp->IFB_Magic ^= HCF_MAGIC ) == 0, ifbp->IFB_Magic ) /*NOP*/;
1255         /* of above HCFASSERT only the side effect is needed, NOP in case HCFASSERT is dummy */
1256         ifbp->IFB_IOBase = io_base;                                                                     /* 0*/
1257         return rc;
1258 } // hcf_connect
1259
1260 #if HCF_DMA
1261 /************************************************************************************************************
1262  * Function get_frame_lst
1263  *  - resolve the "last host-owned descriptor" problems when a descriptor list is reclaimed by the MSF.
1264  *
1265  * The FrameList to be reclaimed as well as the DescriptorList always start in IFB_FirstDesc[tx_rx_flag]
1266  * and this is always the "current" DELWA Descriptor.
1267  *
1268  * If a FrameList is available, the last descriptor of the FrameList to turned into a new DELWA Descriptor:
1269  *  - a copy is made from the information in the last descriptor of the FrameList into the current
1270  *    DELWA Descriptor
1271  *  - the remainder of the DescriptorList is detached from the copy by setting the next_desc_addr at NULL
1272  *  - the DMA control bits of the copy are cleared to do not confuse the MSF
1273  *  - the copy of the last descriptor (i.e. the "old" DELWA Descriptor) is chained to the prev Descriptor
1274  *    of the FrameList, thus replacing the original last Descriptor of the FrameList.
1275  *  - IFB_FirstDesc is changed to the address of that replaced (original) last descriptor of the FrameList,
1276  *    i.e. the "new" DELWA Descriptor.
1277  *
1278  * This function makes a copy of that last host-owned descriptor, so the MSF will get a copy of the descriptor.
1279  * On top of that, it adjusts DMA related fields in the IFB structure.
1280  // perform a copying-scheme to circumvent the 'last host owned descriptor cannot be reclaimed' limitation imposed by H2.5's DMA hardware design
1281  // a 'reclaim descriptor' should be available in the HCF:
1282  *
1283  * Returns: address of the first descriptor of the FrameList
1284  *
1285  8: Be careful once you start re-ordering the steps in the copy process, that it still works for cases
1286  *   of FrameLists of 1, 2 and more than 2 descriptors
1287  *
1288  * Input parameters:
1289  * tx_rx_flag      : specifies 'transmit' or 'receive' descriptor.
1290  *
1291  ************************************************************************************************************/
1292 HCF_STATIC DESC_STRCT*
1293 get_frame_lst( IFBP ifbp, int tx_rx_flag )
1294 {
1295
1296         DESC_STRCT *head = ifbp->IFB_FirstDesc[tx_rx_flag];
1297         DESC_STRCT *copy, *p, *prev;
1298
1299         HCFASSERT( tx_rx_flag == DMA_RX || tx_rx_flag == DMA_TX, tx_rx_flag );
1300                                                 //if FrameList
1301         if ( head ) {
1302                                                 //.  search for last descriptor of first FrameList
1303                 p = prev = head;
1304                 while ( ( p->BUF_SIZE & DESC_EOP ) == 0 && p->next_desc_addr ) {
1305                         if ( ( ifbp->IFB_CntlOpt & DMA_ENABLED ) == 0 ) {   //clear control bits when disabled
1306                                 p->BUF_CNT &= DESC_CNT_MASK;
1307                         }
1308                         prev = p;
1309                         p = p->next_desc_addr;
1310                 }
1311                                                 //.  if DMA enabled
1312                 if ( ifbp->IFB_CntlOpt & DMA_ENABLED ) {
1313                                                 //.  .  if last descriptor of FrameList is DMA owned
1314                                                 //.  .  or if FrameList is single (DELWA) Descriptor
1315                         if ( p->BUF_CNT & DESC_DMA_OWNED || head->next_desc_addr == NULL ) {
1316                                                 //.  .  .  refuse to return FrameList to caller
1317                                 head = NULL;
1318                         }
1319                 }
1320         }
1321                                                 //if returnable FrameList found
1322         if ( head ) {
1323                                                 //.  if FrameList is single (DELWA) Descriptor (implies DMA disabled)
1324                 if ( head->next_desc_addr == NULL ) {
1325                                                 //.  .  clear DescriptorList
1326                         /*;?ifbp->IFB_LastDesc[tx_rx_flag] =*/ ifbp->IFB_FirstDesc[tx_rx_flag] = NULL;
1327                                                 //.  else
1328                 } else {
1329                                                 //.  .  strip hardware-related bits from last descriptor
1330                                                 //.  .  remove DELWA Descriptor from head of DescriptorList
1331                         copy = head;
1332                         head = head->next_desc_addr;
1333                                                 //.   .  exchange first (Confined) and last (possibly imprisoned) Descriptor
1334                         copy->buf_phys_addr = p->buf_phys_addr;
1335                         copy->buf_addr = p->buf_addr;
1336                         copy->BUF_SIZE = p->BUF_SIZE &= DESC_CNT_MASK;  //get rid of DESC_EOP and possibly DESC_SOP
1337                         copy->BUF_CNT = p->BUF_CNT &= DESC_CNT_MASK;    //get rid of DESC_DMA_OWNED
1338 #if (HCF_EXT) & HCF_DESC_STRCT_EXT
1339                         copy->DESC_MSFSup = p->DESC_MSFSup;
1340 #endif // HCF_DESC_STRCT_EXT
1341                                                 //.  .  turn into a DELWA Descriptor
1342                         p->buf_addr = NULL;
1343                                                 //.  .  chain copy to prev                                          /* 8*/
1344                         prev->next_desc_addr = copy;
1345                                                 //.  .  detach remainder of the DescriptorList from FrameList
1346                         copy->next_desc_addr = NULL;
1347                         copy->next_desc_phys_addr = 0xDEAD0000; //! just to be nice, not really needed
1348                                                 //.  .  save the new start (i.e. DELWA Descriptor) in IFB_FirstDesc
1349                         ifbp->IFB_FirstDesc[tx_rx_flag] = p;
1350                 }
1351                                                 //.  strip DESC_SOP from first descriptor
1352                 head->BUF_SIZE &= DESC_CNT_MASK;
1353                 //head->BUF_CNT &= DESC_CNT_MASK;  get rid of DESC_DMA_OWNED
1354                 head->next_desc_phys_addr = 0xDEAD0000; //! just to be nice, not really needed
1355         }
1356                                                 //return the just detached FrameList (if any)
1357         return head;
1358 } // get_frame_lst
1359
1360
1361 /************************************************************************************************************
1362  * Function put_frame_lst
1363  *
1364  * This function
1365  *
1366  * Returns: address of the first descriptor of the FrameList
1367  *
1368  * Input parameters:
1369  * tx_rx_flag      : specifies 'transmit' or 'receive' descriptor.
1370  *
1371  * The following list should be kept in sync with hcf_dma_tx/rx_put, in order to get them in the WCI-spec !!!!
1372  * Assert fails if
1373  * - DMA is not enabled
1374  * - descriptor list is NULL
1375  * - a descriptor in the descriptor list is not double word aligned
1376  * - a count of size field of a descriptor contains control bits, i.e. bits in the high order nibble.
1377  * - the DELWA descriptor is not a "singleton" DescriptorList.
1378  * - the DELWA descriptor is not the first Descriptor supplied
1379  * - a non_DMA descriptor is supplied before the DELWA Descriptor is supplied
1380  * - Possibly more checks could be added !!!!!!!!!!!!!
1381
1382  *.NOTICE
1383  * The asserts marked with *sc* are really sanity checks for the HCF, they can (supposedly) not be influenced
1384  * by incorrect MSF behavior
1385
1386  // The MSF is required to supply the HCF with a single descriptor for MSF tx reclaim purposes.
1387  // This 'reclaim descriptor' can be recognized by the fact that its buf_addr field is zero.
1388  *********************************************************************************************
1389  * Although not required from a hardware perspective:
1390  * - make each descriptor in this rx-chain DMA-owned.
1391  * - Also set the count to zero. EOP and SOP bits are also cleared.
1392  *********************************************************************************************/
1393 HCF_STATIC void
1394 put_frame_lst( IFBP ifbp, DESC_STRCT *descp, int tx_rx_flag )
1395 {
1396         DESC_STRCT  *p = descp;
1397         hcf_16 port;
1398
1399         HCFASSERT( ifbp->IFB_CntlOpt & USE_DMA, ifbp->IFB_CntlOpt); //only hcf_dma_tx_put must also be DMA_ENABLED
1400         HCFASSERT( tx_rx_flag == DMA_RX || tx_rx_flag == DMA_TX, tx_rx_flag );
1401         HCFASSERT( p , 0 );
1402
1403         while ( p ) {
1404                 HCFASSERT( ((hcf_32)p & 3 ) == 0, (hcf_32)p );
1405                 HCFASSERT( (p->BUF_CNT & ~DESC_CNT_MASK) == 0, p->BUF_CNT );
1406                 HCFASSERT( (p->BUF_SIZE & ~DESC_CNT_MASK) == 0, p->BUF_SIZE );
1407                 p->BUF_SIZE &= DESC_CNT_MASK;                   //!!this SHOULD be superfluous in case of correct MSF
1408                 p->BUF_CNT &= tx_rx_flag == DMA_RX ? 0 : DESC_CNT_MASK; //!!this SHOULD be superfluous in case of correct MSF
1409                 p->BUF_CNT |= DESC_DMA_OWNED;
1410                 if ( p->next_desc_addr ) {
1411 //                      HCFASSERT( p->buf_addr && p->buf_phys_addr  && p->BUF_SIZE && +/- p->BUF_SIZE, ... );
1412                         HCFASSERT( p->next_desc_addr->desc_phys_addr, (hcf_32)p->next_desc_addr );
1413                         p->next_desc_phys_addr = p->next_desc_addr->desc_phys_addr;
1414                 } else {                                    //
1415                         p->next_desc_phys_addr = 0;
1416                         if ( p->buf_addr == NULL ) {            // DELWA Descriptor
1417                                 HCFASSERT( descp == p, (hcf_32)descp );  //singleton DescriptorList
1418                                 HCFASSERT( ifbp->IFB_FirstDesc[tx_rx_flag] == NULL, (hcf_32)ifbp->IFB_FirstDesc[tx_rx_flag]);
1419                                 HCFASSERT( ifbp->IFB_LastDesc[tx_rx_flag] == NULL, (hcf_32)ifbp->IFB_LastDesc[tx_rx_flag]);
1420                                 descp->BUF_CNT = 0; //&= ~DESC_DMA_OWNED;
1421                                 ifbp->IFB_FirstDesc[tx_rx_flag] = descp;
1422 // part of alternative ifbp->IFB_LastDesc[tx_rx_flag] = ifbp->IFB_FirstDesc[tx_rx_flag] = descp;
1423                                                 // if "recycling" a FrameList
1424                                                 // (e.g. called from hcf_cntl( HCF_CNTL_ENABLE )
1425                                                 // .  prepare for activation DMA controller
1426 // part of alternative descp = descp->next_desc_addr;
1427                         } else {                                //a "real" FrameList, hand it over to the DMA engine
1428                                 HCFASSERT( ifbp->IFB_FirstDesc[tx_rx_flag], (hcf_32)descp );
1429                                 HCFASSERT( ifbp->IFB_LastDesc[tx_rx_flag], (hcf_32)descp );
1430                                 HCFASSERT( ifbp->IFB_LastDesc[tx_rx_flag]->next_desc_addr == NULL,
1431                                            (hcf_32)ifbp->IFB_LastDesc[tx_rx_flag]->next_desc_addr);
1432 //                              p->buf_cntl.cntl_stat |= DESC_DMA_OWNED;
1433                                 ifbp->IFB_LastDesc[tx_rx_flag]->next_desc_addr = descp;
1434                                 ifbp->IFB_LastDesc[tx_rx_flag]->next_desc_phys_addr = descp->desc_phys_addr;
1435                                 port = HREG_RXDMA_PTR32;
1436                                 if ( tx_rx_flag ) {
1437                                         p->BUF_SIZE |= DESC_EOP;    // p points at the last descriptor in the caller-supplied descriptor chain
1438                                         descp->BUF_SIZE |= DESC_SOP;
1439                                         port = HREG_TXDMA_PTR32;
1440                                 }
1441                                 OUT_PORT_DWORD( (ifbp->IFB_IOBase + port), descp->desc_phys_addr );
1442                         }
1443                         ifbp->IFB_LastDesc[tx_rx_flag] = p;
1444                 }
1445                 p = p->next_desc_addr;
1446         }
1447 } // put_frame_lst
1448
1449
1450 /************************************************************************************************************
1451  *
1452  *.MODULE        DESC_STRCT* hcf_dma_rx_get( IFBP ifbp )
1453  *.PURPOSE       decapsulate a message and provides that message to the MSF.
1454  *               reclaim all descriptors in the rx descriptor chain.
1455  *
1456  *.ARGUMENTS
1457  *   ifbp        address of the Interface Block
1458  *
1459  *.RETURNS
1460  *   pointer to a FrameList
1461  *
1462  *.DESCRIPTION
1463  * hcf_dma_rx_get is intended to  return a received frame when such a frame is deposited in Host memory by the
1464  * DMA engine. In addition hcf_dma_rx_get can be used to reclaim all descriptors in the rx descriptor chain
1465  * when the DMA Engine is disabled, e.g. as part of a driver unloading strategy.
1466  * hcf_dma_rx_get must be called repeatedly by the MSF when hcf_service_nic signals availability of a rx frame
1467  * through the HREG_EV_RDMAD flag of IFB_DmaPackets. The calling must stop when a NULL pointer is returned, at
1468  * which time the HREG_EV_RDMAD flag is also cleared by the HCF to arm the mechanism for the next frame
1469  * reception.
1470  * Regardless whether the DMA Engine is currently enabled (as controlled via hcf_cntl), if the DMA controller
1471  * deposited an Rx-frame in the Rx-DescriptorList, this frame is detached from the Rx-DescriptorList,
1472  * transformed into a FrameList (i.e.  updating the housekeeping fields in the descriptors) and returned to the
1473  * caller.
1474  * If no such Rx-frame is available in the Rx-DescriptorList, the behavior of hcf_dma_rx_get depends on the
1475  * status of the DMA Engine.
1476  * If the DMA Engine is enabled, a NULL pointer is returned.
1477  * If the DMA Engine is disabled, the following strategy is used:
1478  * - the complete Rx-DescriptorList is returned. The DELWA Descriptor is not part of the Rx-DescriptorList.
1479  * - If there is no Rx-DescriptorList, the DELWA Descriptor is returned.
1480  * - If there is no DELWA Descriptor, a NULL pointer is returned.
1481  *
1482  * If the MSF performs an disable/enable sequence without exhausting the Rx-DescriptorList as described above,
1483  * the enable command will reset all house keeping information, i.e. already received but not yet by the MSF
1484  * retrieved frames are lost and the next frame will be received starting with the oldest descriptor.
1485  *
1486  * The HCF can be used in 2 fashions: with and without decapsulation for data transfer.
1487  * This is controlled at compile time by the HCF_ENC bit of the HCF_ENCAP system constant.
1488  * If appropriate, decapsulation is done by moving some data inside the buffers and updating the descriptors
1489  * accordingly.
1490  *!! ;?????where did I describe why a simple manipulation with the count values does not suffice?
1491  *
1492  *.DIAGRAM
1493  *
1494  *.ENDDOC                END DOCUMENTATION
1495  *
1496  ************************************************************************************************************/
1497
1498 DESC_STRCT*
1499 hcf_dma_rx_get (IFBP ifbp)
1500 {
1501         DESC_STRCT *descp;  // pointer to start of FrameList
1502
1503         descp = get_frame_lst( ifbp, DMA_RX );
1504         if ( descp && descp->buf_addr )  //!be aware of the missing curly bracket
1505
1506                                                 //skip decapsulation at confined descriptor
1507 #if (HCF_ENCAP) == HCF_ENC
1508 #if (HCF_TYPE) & HCF_TYPE_CCX
1509                 if ( ifbp->IFB_CKIPStat == HCF_ACT_CCX_OFF )
1510 #endif // HCF_TYPE_CCX
1511                 {
1512                         int i;
1513                         DESC_STRCT *p = descp->next_desc_addr;  //pointer to 2nd descriptor of frame
1514                         HCFASSERT(p, 0);
1515                         // The 2nd descriptor contains (maybe) a SNAP header plus part or whole of the payload.
1516                         //determine decapsulation sub-flag in RxFS
1517                         i = *(wci_recordp)&descp->buf_addr[HFS_STAT] & ( HFS_STAT_MSG_TYPE | HFS_STAT_ERR );
1518                         if ( i == HFS_STAT_TUNNEL ||
1519                              ( i == HFS_STAT_1042 && hcf_encap( (wci_bufp)&p->buf_addr[HCF_DASA_SIZE] ) != ENC_TUNNEL )) {
1520                                 // The 2nd descriptor contains a SNAP header plus part or whole of the payload.
1521                                 HCFASSERT( p->BUF_CNT == (p->buf_addr[5] + (p->buf_addr[4]<<8) + 2*6 + 2 - 8), p->BUF_CNT );
1522                                 // perform decapsulation
1523                                 HCFASSERT(p->BUF_SIZE >=8, p->BUF_SIZE);
1524                                 // move SA[2:5] in the second buffer to replace part of the SNAP header
1525                                 for ( i=3; i >= 0; i--) p->buf_addr[i+8] = p->buf_addr[i];
1526                                 // copy DA[0:5], SA[0:1] from first buffer to second buffer
1527                                 for ( i=0; i<8; i++) p->buf_addr[i] = descp->buf_addr[HFS_ADDR_DEST + i];
1528                                 // make first buffer shorter in count
1529                                 descp->BUF_CNT = HFS_ADDR_DEST;
1530                         }
1531                 }
1532 #endif // HCF_ENC
1533         if ( descp == NULL ) ifbp->IFB_DmaPackets &= (hcf_16)~HREG_EV_RDMAD;  //;?could be integrated into get_frame_lst
1534         HCFLOGEXIT( HCF_TRACE_DMA_RX_GET );
1535         return descp;
1536 } // hcf_dma_rx_get
1537
1538
1539 /************************************************************************************************************
1540  *
1541  *.MODULE        void hcf_dma_rx_put( IFBP ifbp, DESC_STRCT *descp )
1542  *.PURPOSE       supply buffers for receive purposes.
1543  *               supply the Rx-DELWA descriptor.
1544  *
1545  *.ARGUMENTS
1546  *   ifbp        address of the Interface Block
1547  *   descp       address of a DescriptorList
1548  *
1549  *.RETURNS       N.A.
1550  *
1551  *.DESCRIPTION
1552  * This function is called by the MSF to supply the HCF with new/more buffers for receive purposes.
1553  * The HCF can be used in 2 fashions: with and without encapsulation for data transfer.
1554  * This is controlled at compile time by the HCF_ENC bit of the HCF_ENCAP system constant.
1555  * As a consequence, some additional constraints apply to the number of descriptor and the buffers associated
1556  * with the first 2 descriptors. Independent of the encapsulation feature, the COUNT fields are ignored.
1557  * A special case is the supplying of the DELWA descriptor, which must be supplied as the first descriptor.
1558  *
1559  * Assert fails if
1560  * - ifbp has a recognizable out-of-range value.
1561  * - NIC interrupts are not disabled while required by parameter action.
1562  * - in case decapsulation by the HCF is selected:
1563  *     - The first databuffer does not have the exact size corresponding with the RxFS up to the 802.3 DestAddr
1564  *       field (== 29 words).
1565  *     - The FrameList does not consists of at least 2 Descriptors.
1566  *     - The second databuffer does not have the minimum size of 8 bytes.
1567  *!! The 2nd part of the list of asserts should be kept in sync with put_frame_lst, in order to get
1568  *!! them in the WCI-spec !!!!
1569  * - DMA is not enabled
1570  * - descriptor list is NULL
1571  * - a descriptor in the descriptor list is not double word aligned
1572  * - a count of size field of a descriptor contains control bits, i.e. bits in the high order nibble.
1573  * - the DELWA descriptor is not a "singleton" DescriptorList.
1574  * - the DELWA descriptor is not the first Descriptor supplied
1575  * - a non_DMA descriptor is supplied before the DELWA Descriptor is supplied
1576  *!! - Possibly more checks could be added !!!!!!!!!!!!!
1577  *
1578  *.DIAGRAM
1579  *
1580  *
1581  *.ENDDOC                END DOCUMENTATION
1582  *
1583  ************************************************************************************************************/
1584 void
1585 hcf_dma_rx_put( IFBP ifbp, DESC_STRCT *descp )
1586 {
1587
1588         HCFLOGENTRY( HCF_TRACE_DMA_RX_PUT, 0xDA01 );
1589         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
1590         HCFASSERT_INT;
1591
1592         put_frame_lst( ifbp, descp, DMA_RX );
1593 #if HCF_ASSERT && (HCF_ENCAP) == HCF_ENC
1594         if ( descp->buf_addr ) {
1595                 HCFASSERT( descp->BUF_SIZE == HCF_DMA_RX_BUF1_SIZE, descp->BUF_SIZE );
1596                 HCFASSERT( descp->next_desc_addr, 0 ); // first descriptor should be followed by another descriptor
1597                 // The second DB is for SNAP and payload purposes. It should be a minimum of 12 bytes in size.
1598                 HCFASSERT( descp->next_desc_addr->BUF_SIZE >= 12, descp->next_desc_addr->BUF_SIZE );
1599         }
1600 #endif // HCFASSERT / HCF_ENC
1601         HCFLOGEXIT( HCF_TRACE_DMA_RX_PUT );
1602 } // hcf_dma_rx_put
1603
1604
1605 /************************************************************************************************************
1606  *
1607  *.MODULE        DESC_STRCT* hcf_dma_tx_get( IFBP ifbp )
1608  *.PURPOSE       DMA mode: reclaims and decapsulates packets in the tx descriptor chain if:
1609  *                - A Tx packet has been copied from host-RAM into NIC-RAM by the DMA engine
1610  *                - The Hermes/DMAengine have been disabled
1611  *
1612  *.ARGUMENTS
1613  *   ifbp        address of the Interface Block
1614  *
1615  *.RETURNS
1616  *   pointer to a reclaimed Tx packet.
1617  *
1618  *.DESCRIPTION
1619  * impact of the disable command:
1620  * When a non-empty pool of Tx descriptors exists (created by means of hcf_dma_put_tx), the MSF
1621  * is supposed to empty that pool by means of hcf_dma_tx_get calls after the disable in an
1622  * disable/enable sequence.
1623  *
1624  *.DIAGRAM
1625  *
1626  *.NOTICE
1627  *
1628  *.ENDDOC                END DOCUMENTATION
1629  *
1630  ************************************************************************************************************/
1631 DESC_STRCT*
1632 hcf_dma_tx_get( IFBP ifbp )
1633 {
1634         DESC_STRCT *descp;  // pointer to start of FrameList
1635
1636         descp = get_frame_lst( ifbp, DMA_TX );
1637         if ( descp && descp->buf_addr )  //!be aware of the missing curly bracket
1638                                                 //skip decapsulation at confined descriptor
1639 #if (HCF_ENCAP) == HCF_ENC
1640                 if ( ( descp->BUF_CNT == HFS_TYPE )
1641 #if (HCF_TYPE) & HCF_TYPE_CCX
1642                      || ( descp->BUF_CNT == HFS_DAT )
1643 #endif // HCF_TYPE_CCX
1644                         ) { // perform decapsulation if needed
1645                         descp->next_desc_addr->buf_phys_addr -= HCF_DASA_SIZE;
1646                         descp->next_desc_addr->BUF_CNT       += HCF_DASA_SIZE;
1647                 }
1648 #endif // HCF_ENC
1649         if ( descp == NULL ) {  //;?could be integrated into get_frame_lst
1650                 ifbp->IFB_DmaPackets &= (hcf_16)~HREG_EV_TDMAD;
1651         }
1652         HCFLOGEXIT( HCF_TRACE_DMA_TX_GET );
1653         return descp;
1654 } // hcf_dma_tx_get
1655
1656
1657 /************************************************************************************************************
1658  *
1659  *.MODULE        void hcf_dma_tx_put( IFBP ifbp, DESC_STRCT *descp, hcf_16 tx_cntl )
1660  *.PURPOSE       puts a packet in the Tx DMA queue in host ram and kicks off the TxDma engine.
1661  *               supply the Tx-DELWA descriptor.
1662  *
1663  *.ARGUMENTS
1664  *   ifbp        address of the Interface Block
1665  *   descp       address of Tx Descriptor Chain (i.e. a single Tx frame)
1666  *   tx_cntl     indicates MAC-port and (Hermes) options
1667  *
1668  *.RETURNS       N.A.
1669  *
1670  *.DESCRIPTION
1671  * The HCF can be used in 2 fashions: with and without encapsulation for data transfer.
1672  * This is controlled at compile time by the HCF_ENC bit of the HCF_ENCAP system constant.
1673  *
1674  * Regardless of the HCF_ENCAP system constant, the descriptor list created to describe the frame to be
1675  * transmitted, must supply space to contain the 802.11 header, preceding the actual frame to be transmitted.
1676  * Basically, this only supplies working storage to the HCF which passes this on to the DMA engine.
1677  * As a consequence the contents of this space do not matter.
1678  * Nevertheless BUF_CNT must take in account this storage.
1679  * This working space to contain the 802.11 header may not be fragmented, the first buffer must be
1680  * sufficiently large to contain at least the 802.11 header, i.e. HFS_ADDR_DEST (29 words or 0x3A bytes).
1681  * This way, the HCF can simply, regardless whether or not the HCF encapsulates the frame, write the parameter
1682  * tx_cntl at offset 0x36 (HFS_TX_CNTL) in the first buffer.
1683  * Note that it is allowed to have part or all of the actual frame represented by the first descriptor as long
1684  * as the requirement for storage for the 802.11 header is met, i.e. the 802.3 frame starts at offset
1685  * HFS_ADDR_DEST.
1686  * Except for the Assert on the 1st buffer in case of Encapsualtion, the SIZE fields are ignored.
1687  *
1688  * In case the encapsulation feature is compiled in, there are the following additional requirements.
1689  * o The BUF_CNT of the first buffer changes from a minimum of 0x3A bytes to exactly 0x3A, i.e. the workspace
1690  *   to store the 802.11 header
1691  * o The BUF_SIZE of the first buffer is at least the space needed to store the
1692  *   - 802.11 header (29 words)
1693  *   - 802.3 header, i.e. 12 bytes addressing information and 2 bytes length field
1694  *   - 6 bytes SNAP-header
1695  *   This results in 39 words or 0x4E bytes or HFS_TYPE.
1696  *   Note that if the BUF_SIZE is larger than 0x4E, this surplus is not used.
1697  * o The actual frame begins in the 2nd descriptor (which is already implied by the BUF_CNT == 0x3A requirement) and the associated buffer contains at least the 802.3 header, i.e. the 14 bytes representing addressing information and length/type field
1698  *
1699  *   When the HCF does not encapsulates (i.e. length/type field <= 1500),  no changes are made to descriptors
1700  *   or buffers.
1701  *
1702  *   When the HCF actually encapsulates (i.e. length/type field > 1500), it successively writes, starting at
1703  *   offset HFS_ADDR_DEST (0x3A) in the first buffer:
1704  *     - the 802.3 addressing information, copied from the begin of the second buffer
1705  *     - the frame length, derived from the total length of the individual fragments, corrected for the SNAP
1706  *       header length and Type field and ignoring the Destination Address, Source Address and Length field
1707  *     - the appropriate snap header (Tunnel or 1042, depending on the value of the type field).
1708  *
1709  *    The information in the first two descriptors is adjusted accordingly:
1710  *     - the first descriptor count is changed from 0x3A to 0x4E (HFS_TYPE), which matches 0x3A + 12 + 2 + 6
1711  *     - the second descriptor count is decreased by 12, being the moved addressing information
1712  *     - the second descriptor (physical) buffer address is increased by 12.
1713  *
1714  * When the descriptors are returned by hcf_dma_tx_get, the transformation of the first two descriptors is
1715  * undone.
1716  *
1717  * Under any of the above scenarios, the assert BUF_CNT <= BUF_SIZE must be true for all descriptors
1718  * In case of encapsulation, BUF_SIZE of the 1st descriptor is asserted to be at least HFS_TYPE (0x4E), so it is NOT tested.
1719  *
1720  * Assert fails if
1721  * - ifbp has a recognizable out-of-range value.
1722  * - tx_cntl has a recognizable out-of-range value.
1723  * - NIC interrupts are not disabled while required by parameter action.
1724  * - in case encapsulation by the HCF is selected:
1725  *     - The FrameList does not consists of at least 2 Descriptors.
1726  *     - The first databuffer does not contain exactly the (space for) the 802.11 header (== 28 words)
1727  *     - The first databuffer does not have a size to additionally accommodate the 802.3 header and the
1728  *       SNAP header of the frame after encapsulation (== 39 words).
1729  *     - The second databuffer does not contain at least DA, SA and 'type/length' (==14 bytes or 7 words)
1730  *!! The 2nd part of the list of asserts should be kept in sync with put_frame_lst, in order to get
1731  *!! them in the WCI-spec !!!!
1732  * - DMA is not enabled
1733  * - descriptor list is NULL
1734  * - a descriptor in the descriptor list is not double word aligned
1735  * - a count of size field of a descriptor contains control bits, i.e. bits in the high order nibble.
1736  * - the DELWA descriptor is not a "singleton" DescriptorList.
1737  * - the DELWA descriptor is not the first Descriptor supplied
1738  * - a non_DMA descriptor is supplied before the DELWA Descriptor is supplied
1739  *!! - Possibly more checks could be added !!!!!!!!!!!!!
1740  *.DIAGRAM
1741  *
1742  *.NOTICE
1743  *
1744  *.ENDDOC                END DOCUMENTATION
1745  *
1746  *
1747  *1: Write tx_cntl parameter to HFS_TX_CNTL field into the Hermes-specific header in buffer 1
1748  *4: determine whether encapsulation is needed and write the type (tunnel or 1042) already at the appropriate
1749  *   offset in the 1st buffer
1750  *6: Build the encapsualtion enveloppe in the free space at the end of the 1st buffer
1751  *   - Copy DA/SA fields from the 2nd buffer
1752  *   - Calculate total length of the message (snap-header + type-field + the length of all buffer fragments
1753  *     associated with the 802.3 frame (i.e all descriptors except the first), but not the DestinationAddress,
1754  *     SourceAddress and length-field)
1755  *     Assert the message length
1756  *     Write length. Note that the message is in BE format, hence on LE platforms the length must be converted
1757  *     ;? THIS IS NOT WHAT CURRENTLY IS IMPLEMENTED
1758  *   - Write snap header. Note that the last byte of the snap header is NOT copied, that byte is already in
1759  *     place as result of the call to hcf_encap.
1760  *   Note that there are many ways to skin a cat. To express the offsets in the 1st buffer while writing
1761  *   the snap header, HFS_TYPE is chosen as a reference point to make it easier to grasp that the snap header
1762  *   and encapsualtion type are at least relative in the right.
1763  *8: modify 1st descriptor to reflect moved part of the 802.3 header + Snap-header
1764  *   modify 2nd descriptor to skip the moved part of the 802.3 header (DA/SA
1765  *10: set each descriptor to 'DMA owned',  clear all other control bits.
1766  *   Set SOP bit on first descriptor. Set EOP bit on last descriptor.
1767  *12: Either append the current frame to an existing descriptor list or
1768  *14: create a list beginning with the current frame
1769  *16: remember the new end of the list
1770  *20: hand the frame over to the DMA engine
1771  ************************************************************************************************************/
1772 void
1773 hcf_dma_tx_put( IFBP ifbp, DESC_STRCT *descp, hcf_16 tx_cntl )
1774 {
1775         DESC_STRCT  *p = descp->next_desc_addr;
1776         int         i;
1777
1778 #if HCF_ASSERT
1779         int x = ifbp->IFB_FWIdentity.comp_id == COMP_ID_FW_AP ? tx_cntl & ~HFS_TX_CNTL_PORT : tx_cntl;
1780         HCFASSERT( (x & ~HCF_TX_CNTL_MASK ) == 0, tx_cntl );
1781 #endif // HCF_ASSERT
1782         HCFLOGENTRY( HCF_TRACE_DMA_TX_PUT, 0xDA03 );
1783         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
1784         HCFASSERT_INT;
1785         HCFASSERT( ( ifbp->IFB_CntlOpt & (USE_DMA|DMA_ENABLED) ) == (USE_DMA|DMA_ENABLED), ifbp->IFB_CntlOpt);
1786
1787         if ( descp->buf_addr ) {
1788                 *(hcf_16*)(descp->buf_addr + HFS_TX_CNTL) = tx_cntl;                                            /*1*/
1789 #if (HCF_ENCAP) == HCF_ENC
1790                 HCFASSERT( descp->next_desc_addr, 0 );                                   //at least 2 descripors
1791                 HCFASSERT( descp->BUF_CNT == HFS_ADDR_DEST, descp->BUF_CNT );    //exact length required for 1st buffer
1792                 HCFASSERT( descp->BUF_SIZE >= HCF_DMA_TX_BUF1_SIZE, descp->BUF_SIZE );   //minimal storage for encapsulation
1793                 HCFASSERT( p->BUF_CNT >= 14, p->BUF_CNT );                  //at least DA, SA and 'type' in 2nd buffer
1794
1795 #if (HCF_TYPE) & HCF_TYPE_CCX
1796                 /* if we are doing PPK +/- CMIC, or we are sending a DDP frame */
1797                 if ( ( ifbp->IFB_CKIPStat == HCF_ACT_CCX_ON ) ||
1798                      ( ( p->BUF_CNT >= 20 )      && ( ifbp->IFB_CKIPStat == HCF_ACT_CCX_OFF ) &&
1799                        ( p->buf_addr[12] == 0xAA ) && ( p->buf_addr[13] == 0xAA ) &&
1800                        ( p->buf_addr[14] == 0x03 ) && ( p->buf_addr[15] == 0x00 ) &&
1801                        ( p->buf_addr[16] == 0x40 ) && ( p->buf_addr[17] == 0x96 ) &&
1802                        ( p->buf_addr[18] == 0x00 ) && ( p->buf_addr[19] == 0x00 )))
1803                 {
1804                         /* copy the DA/SA to the first buffer */
1805                         for ( i = 0; i < HCF_DASA_SIZE; i++ ) {
1806                                 descp->buf_addr[i + HFS_ADDR_DEST] = p->buf_addr[i];
1807                         }
1808                         /* calculate the length of the second fragment only */
1809                         i = 0;
1810                         do { i += p->BUF_CNT; } while( p = p->next_desc_addr );
1811                         i -= HCF_DASA_SIZE ;
1812                         /* convert the length field to big endian, using the endian friendly macros */
1813                         i = CNV_SHORT_TO_BIG(i);        //!! this converts ONLY on LE platforms, how does that relate to the non-CCX code
1814                         *(hcf_16*)(&descp->buf_addr[HFS_LEN]) = (hcf_16)i;
1815                         descp->BUF_CNT = HFS_DAT;
1816                         // modify 2nd descriptor to skip the 'Da/Sa' fields
1817                         descp->next_desc_addr->buf_phys_addr += HCF_DASA_SIZE;
1818                         descp->next_desc_addr->BUF_CNT       -= HCF_DASA_SIZE;
1819                 }
1820                 else
1821 #endif // HCF_TYPE_CCX
1822                 {
1823                         descp->buf_addr[HFS_TYPE-1] = hcf_encap(&descp->next_desc_addr->buf_addr[HCF_DASA_SIZE]);       /*4*/
1824                         if ( descp->buf_addr[HFS_TYPE-1] != ENC_NONE ) {
1825                                 for ( i=0; i < HCF_DASA_SIZE; i++ ) {                                                       /*6*/
1826                                         descp->buf_addr[i + HFS_ADDR_DEST] = descp->next_desc_addr->buf_addr[i];
1827                                 }
1828                                 i = sizeof(snap_header) + 2 - ( 2*6 + 2 );
1829                                 do { i += p->BUF_CNT; } while ( ( p = p->next_desc_addr ) != NULL );
1830                                 *(hcf_16*)(&descp->buf_addr[HFS_LEN]) = CNV_END_SHORT(i);   //!! this converts on ALL platforms, how does that relate to the CCX code
1831                                 for ( i=0; i < sizeof(snap_header) - 1; i++) {
1832                                         descp->buf_addr[HFS_TYPE - sizeof(snap_header) + i] = snap_header[i];
1833                                 }
1834                                 descp->BUF_CNT = HFS_TYPE;                                                                  /*8*/
1835                                 descp->next_desc_addr->buf_phys_addr    += HCF_DASA_SIZE;
1836                                 descp->next_desc_addr->BUF_CNT          -= HCF_DASA_SIZE;
1837                         }
1838                 }
1839 #endif // HCF_ENC
1840         }
1841         put_frame_lst( ifbp, descp, DMA_TX );
1842         HCFLOGEXIT( HCF_TRACE_DMA_TX_PUT );
1843 } // hcf_dma_tx_put
1844
1845 #endif // HCF_DMA
1846
1847 /************************************************************************************************************
1848  *
1849  *.MODULE        hcf_8 hcf_encap( wci_bufp type )
1850  *.PURPOSE       test whether RFC1042 or Bridge-Tunnel encapsulation is needed.
1851  *
1852  *.ARGUMENTS
1853  *   type        (Far) pointer to the (Big Endian) Type/Length field in the message
1854  *
1855  *.RETURNS
1856  *   ENC_NONE        len/type is "len" ( (BIG_ENDIAN)type <= 1500 )
1857  *   ENC_TUNNEL      len/type is "type" and 0x80F3 or 0x8137
1858  *   ENC_1042        len/type is "type" but not 0x80F3 or 0x8137
1859  *
1860  *.CONDITIONS
1861  *   NIC Interrupts  d.c
1862  *
1863  *.DESCRIPTION
1864  * Type must point to the Len/Type field of the message, this is the 2-byte field immediately after the 6 byte
1865  * Destination Address and 6 byte Source Address.  The 2 successive bytes addressed by type are interpreted as
1866  * a Big Endian value.  If that value is less than or equal to 1500, the message is assumed to be in 802.3
1867  * format.  Otherwise the message is assumed to be in Ethernet-II format.  Depending on the value of Len/Typ,
1868  * Bridge Tunnel or RFC1042 encapsulation is needed.
1869  *
1870  *.DIAGRAM
1871  *
1872  *  1:   presume 802.3, hence preset return value at ENC_NONE
1873  *  2:   convert type from "network" Endian format to native Endian
1874  *  4:   the litmus test to distinguish type and len.
1875  *   The hard code "magic" value of 1500 is intentional and should NOT be replaced by a mnemonic because it is
1876  *   not related at all to the maximum frame size supported  by the Hermes.
1877  *  6:   check type against:
1878  *       0x80F3  //AppleTalk Address Resolution Protocol (AARP)
1879  *       0x8137  //IPX
1880  *   to determine the type of encapsulation
1881  *
1882  *.ENDDOC                END DOCUMENTATION
1883  *
1884  ************************************************************************************************************/
1885 HCF_STATIC hcf_8
1886 hcf_encap( wci_bufp type )
1887 {
1888
1889         hcf_8   rc = ENC_NONE;                                                                                  /* 1 */
1890         hcf_16  t = (hcf_16)(*type<<8) + *(type+1);                                                             /* 2 */
1891
1892         if ( t > 1500 ) {                                                                                   /* 4 */
1893                 if ( t == 0x8137 || t == 0x80F3 ) {
1894                         rc = ENC_TUNNEL;                                                                            /* 6 */
1895                 } else {
1896                         rc = ENC_1042;
1897                 }
1898         }
1899         return rc;
1900 } // hcf_encap
1901
1902
1903 /************************************************************************************************************
1904  *
1905  *.MODULE        int hcf_get_info( IFBP ifbp, LTVP ltvp )
1906  *.PURPOSE       Obtains transient and persistent configuration information from the Card and from the HCF.
1907  *
1908  *.ARGUMENTS
1909  *   ifbp        address of the Interface Block
1910  *   ltvp        address of LengthTypeValue structure specifying the "what" and the "how much" of the
1911  *               information to be collected from the HCF or from the Hermes
1912  *
1913  *.RETURNS
1914  *   HCF_ERR_LEN         The provided buffer was too small
1915  *   HCF_SUCCESS         Success
1916  *!! via cmd_exe ( type >= CFG_RID_FW_MIN )
1917  *   HCF_ERR_NO_NIC      NIC removed during retrieval
1918  *   HCF_ERR_TIME_OUT    Expected Hermes event did not occur in expected time
1919  *!! via cmd_exe and setup_bap (type >= CFG_RID_FW_MIN )
1920  *   HCF_ERR_DEFUNCT_... HCF is in defunct mode (bits 0x7F reflect cause)
1921  *
1922  *.DESCRIPTION
1923  * The T-field of the LTV-record (provided by the MSF in parameter ltvp) specifies the RID wanted. The RID
1924  * information identified by the T-field is copied into the V-field.
1925  * On entry, the L-field specifies the size of the buffer, also called the "Initial DataLength". The L-value
1926  * includes the size of the T-field, but not the size of the L-field itself.
1927  * On return, the L-field indicates the number of words actually contained by the Type and Value fields.
1928  * As the size of the Type field in the LTV-record is included in the "Initial DataLength" of the record, the
1929  * V-field can contain at most "Initial DataLength" - 1 words of data.
1930  * Copying stops if either the complete Information is copied or if the number of words indicated by the
1931  * "Initial DataLength" were copied.  The "Initial DataLength" acts as a safe guard against Configuration
1932  * Information blocks that have different sizes for different F/W versions, e.g. when later versions support
1933  * more tallies than earlier versions.
1934  * If the size of Value field of the RID exceeds the size of the "Initial DataLength" -1, as much data
1935  * as fits is copied, and an error status of HCF_ERR_LEN is returned.
1936  *
1937  * It is the responsibility of the MSF to detect card removal and re-insertion and not call the HCF when the
1938  * NIC is absent. The MSF cannot, however, timely detect a Card removal if the Card is removed while
1939  * hcf_get_info is in progress.  Therefore, the HCF performs its own check on Card presence after the read
1940  * operation of the NIC data.  If the Card is not present or removed during the execution of hcf_get_info,
1941  * HCF_ERR_NO_NIC is returned and the content of the Data Buffer is unpredictable. This check is not performed
1942  * in case of the "HCF embedded" pseudo RIDs like CFG_TALLIES.
1943  *
1944  * Assert fails if
1945  * - ifbp has a recognizable out-of-range value.
1946  * - reentrancy, may be  caused by calling hcf_functions without adequate protection
1947  *   against NIC interrupts or multi-threading.
1948  * - ltvp is a NULL pointer.
1949  * - length field of the LTV-record at entry is 0 or 1 or has an excessive value (i.e. exceeds HCF_MAX_LTV).
1950  * - type field of the LTV-record is invalid.
1951  *
1952  *.DIAGRAM
1953  *   Hcf_get_mb_info copies the contents of the oldest MailBox Info block in the MailBox to PC RAM. If len is
1954  *   less than the size of the MailBox Info block, only as much as fits in the PC RAM buffer is copied. After
1955  *   the copying the MailBox Read pointer is updated to point to the next MailBox Info block, hence the
1956  *   remainder of an "oversized" MailBox Info block is lost. The truncation of the MailBox Info block is NOT
1957  *   reflected in the return status. Note that hcf_get_info guarantees the length of the PC RAM buffer meets
1958  *   the minimum requirements of at least 2, so no PC RAM buffer overrun.
1959  *
1960  *   Calling hcf_get_mb_info when their is no MailBox Info block available or when there is no MailBox at all,
1961  *   results in a "NULL" MailBox Info block.
1962  *
1963  *12:    see NOTICE
1964  *17: The return status of cmd_wait and the first hcfio_in_string can be ignored, because when one fails, the
1965  *   other fails via the IFB_DefunctStat mechanism
1966  *20: "HCFASSERT( rc == HCF_SUCCESS, rc )" is not suitable because this will always trigger as side effect of
1967  *   the HCFASSERT in hcf_put_info which calls hcf_get_info to figure out whether the RID exists at all.
1968
1969  *.NOTICE
1970  *
1971  *   "HCF embedded" pseudo RIDs:
1972  *   CFG_MB_INFO, CFG_TALLIES, CFG_DRV_IDENTITY, CFG_DRV_SUP_RANGE, CFG_DRV_ACT_RANGES_PRI,
1973  *   CFG_DRV_ACT_RANGES_STA, CFG_DRV_ACT_RANGES_HSI
1974  *   Note the HCF_ERR_LEN is NOT adequately set, when L >= 2 but less than needed
1975  *
1976  *   Remarks: Transfers operation information and transient and persistent configuration information from the
1977  *   Card and from the HCF to the MSF.
1978  *   The exact layout of the provided data structure depends on the action code. Copying stops if either the
1979  *   complete Configuration Information is copied or if the number of bytes indicated by len is copied.  Len
1980  *   acts as a safe guard against Configuration Information blocks which have different sizes for different
1981  *   Hermes versions, e.g. when later versions support more tallies than earlier versions. It is a conscious
1982  *   decision that unused parts of the PC RAM buffer are not cleared.
1983  *
1984  *   Remarks: The only error against which is protected is the "Read error" as result of Card removal. Only the
1985  *   last hcf_io_string need to be protected because if the first fails the second will fail as well. Checking
1986  *   for cmd_exe errors is supposed superfluous because problems in cmd_exe are already caught or will be
1987  *   caught by hcf_enable.
1988  *
1989  *   CFG_MB_INFO: copy the oldest MailBox Info Block or the "null" block if none available.
1990  *
1991  *   The mechanism to HCF_ASSERT on invalid typ-codes in the LTV record is based on the following strategy:
1992  *     - during the pseudo-asynchronous Hermes commands (diagnose, download) only CFG_MB_INFO is acceptable
1993  *     - some codes (e.g. CFG_TALLIES) are explicitly handled by the HCF which implies that these codes
1994  *       are valid
1995  *     - all other codes in the range 0xFC00 through 0xFFFF are passed to the Hermes.  The Hermes returns an
1996  *       LTV record with a zero value in the L-field for all Typ-codes it does not recognize. This is
1997  *       defined and intended behavior, so HCF_ASSERT does not catch on this phenomena.
1998  *     - all remaining codes are invalid and cause an ASSERT.
1999  *
2000  *.CONDITIONS
2001  * In case of USB, HCF_MAX_MSG ;?USED;? to limit the amount of data that can be retrieved via hcf_get_info.
2002  *
2003  *
2004  *.ENDDOC                END DOCUMENTATION
2005  *
2006  ************************************************************************************************************/
2007 int
2008 hcf_get_info( IFBP ifbp, LTVP ltvp )
2009 {
2010
2011         int         rc = HCF_SUCCESS;
2012         hcf_16      len = ltvp->len;
2013         hcf_16      type = ltvp->typ;
2014         wci_recordp p = &ltvp->len;     //destination word pointer (in LTV record)
2015         hcf_16      *q = NULL;              /* source word pointer  Note!! DOS COM can't cope with FAR
2016                                              * as a consequence MailBox must be near which is usually true anyway
2017                                              */
2018         int         i;
2019
2020         HCFLOGENTRY( HCF_TRACE_GET_INFO, ltvp->typ );
2021         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
2022         HCFASSERT_INT;
2023         HCFASSERT( ltvp, 0 );
2024         HCFASSERT( 1 < ltvp->len && ltvp->len <= HCF_MAX_LTV + 1, MERGE_2( ltvp->typ, ltvp->len ) );
2025
2026         ltvp->len = 0;                              //default to: No Info Available
2027 #if defined MSF_COMPONENT_ID || (HCF_EXT) & HCF_EXT_MB //filter out all specials
2028         for ( i = 0; ( q = xxxx[i] ) != NULL && q[1] != type; i++ ) /*NOP*/;
2029 #endif // MSF_COMPONENT_ID / HCF_EXT_MB
2030 #if HCF_TALLIES
2031         if ( type == CFG_TALLIES ) {                                                    /*3*/
2032                 (void)hcf_action( ifbp, HCF_ACT_TALLIES );
2033                 q = (hcf_16*)&ifbp->IFB_TallyLen;
2034         }
2035 #endif // HCF_TALLIES
2036 #if (HCF_EXT) & HCF_EXT_MB
2037         if ( type == CFG_MB_INFO ) {
2038                 if ( ifbp->IFB_MBInfoLen ) {
2039                         if ( ifbp->IFB_MBp[ifbp->IFB_MBRp] == 0xFFFF ) {
2040                                 ifbp->IFB_MBRp = 0; //;?Probably superfluous
2041                         }
2042                         q = &ifbp->IFB_MBp[ifbp->IFB_MBRp];
2043                         ifbp->IFB_MBRp += *q + 1;   //update read pointer
2044                         if ( ifbp->IFB_MBp[ifbp->IFB_MBRp] == 0xFFFF ) {
2045                                 ifbp->IFB_MBRp = 0;
2046                         }
2047                         ifbp->IFB_MBInfoLen = ifbp->IFB_MBp[ifbp->IFB_MBRp];
2048                 }
2049         }
2050 #endif // HCF_EXT_MB
2051         if ( q != NULL ) {                      //a special or CFG_TALLIES or CFG_MB_INFO
2052                 i = min( len, *q ) + 1;             //total size of destination (including T-field)
2053                 while ( i-- ) {
2054                         *p++ = *q;
2055 #if (HCF_TALLIES) & HCF_TALLIES_RESET
2056                         if ( q > &ifbp->IFB_TallyTyp && type == CFG_TALLIES ) {
2057                                 *q = 0;
2058                         }
2059 #endif // HCF_TALLIES_RESET
2060                         q++;
2061                 }
2062         } else {                                // not a special nor CFG_TALLIES nor CFG_MB_INFO
2063                 if ( type == CFG_CNTL_OPT ) {                                       //read back effective options
2064                         ltvp->len = 2;
2065                         ltvp->val[0] = ifbp->IFB_CntlOpt;
2066 #if (HCF_EXT) & HCF_EXT_NIC_ACCESS
2067                 } else if ( type == CFG_PROD_DATA ) {  //only needed for some test tool on top of H-II NDIS driver
2068                         hcf_io      io_port;
2069                         wci_bufp    pt;                 //pointer with the "right" type, just to help ease writing macros with embedded assembly
2070                         OPW( HREG_AUX_PAGE, (hcf_16)(PLUG_DATA_OFFSET >> 7) );
2071                         OPW( HREG_AUX_OFFSET, (hcf_16)(PLUG_DATA_OFFSET & 0x7E) );
2072                         io_port = ifbp->IFB_IOBase + HREG_AUX_DATA;     //to prevent side effects of the MSF-defined macro
2073                         p = ltvp->val;                  //destination char pointer (in LTV record)
2074                         i = len - 1;
2075                         if (i > 0 ) {
2076                                 pt = (wci_bufp)p;   //just to help ease writing macros with embedded assembly
2077                                 IN_PORT_STRING_8_16( io_port, pt, i ); //space used by T: -1
2078                         }
2079                 } else if ( type == CFG_CMD_HCF ) {
2080 #define P ((CFG_CMD_HCF_STRCT FAR *)ltvp)
2081                         HCFASSERT( P->cmd == CFG_CMD_HCF_REG_ACCESS, P->cmd );       //only Hermes register access supported
2082                         if ( P->cmd == CFG_CMD_HCF_REG_ACCESS ) {
2083                                 HCFASSERT( P->mode < ifbp->IFB_IOBase, P->mode );        //Check Register space
2084                                 ltvp->len = min( len, 4 );                              //RESTORE ltv length
2085                                 P->add_info = IPW( P->mode );
2086                         }
2087 #undef P
2088 #endif // HCF_EXT_NIC_ACCESS
2089 #if (HCF_ASSERT) & HCF_ASSERT_PRINTF
2090                 } else if (type == CFG_FW_PRINTF) {
2091                         rc = fw_printf(ifbp, (CFG_FW_PRINTF_STRCT*)ltvp);
2092 #endif // HCF_ASSERT_PRINTF
2093                 } else if ( type >= CFG_RID_FW_MIN ) {
2094 //;? by using HCMD_BUSY option when calling cmd_exe, using a get_frag with length 0 just to set up the
2095 //;? BAP and calling cmd_cmpl, you could merge the 2 Busy waits. Whether this really helps (and what
2096 //;? would be the optimal sequence in cmd_exe and get_frag) would have to be MEASURED
2097                 /*17*/  if ( ( rc = cmd_exe( ifbp, HCMD_ACCESS, type ) ) == HCF_SUCCESS &&
2098                                  ( rc = setup_bap( ifbp, type, 0, IO_IN ) ) == HCF_SUCCESS ) {
2099                                 get_frag( ifbp, (wci_bufp)&ltvp->len, 2*len+2 BE_PAR(2) );
2100                                 if ( IPW( HREG_STAT ) == 0xFFFF ) {                 //NIC removal test
2101                                         ltvp->len = 0;
2102                                         HCFASSERT( DO_ASSERT, type );
2103                                 }
2104                         }
2105         /*12*/  } else HCFASSERT( DO_ASSERT, type ) /*NOP*/; //NOP in case HCFASSERT is dummy
2106         }
2107         if ( len < ltvp->len ) {
2108                 ltvp->len = len;
2109                 if ( rc == HCF_SUCCESS ) {
2110                         rc = HCF_ERR_LEN;
2111                 }
2112         }
2113         HCFASSERT( rc == HCF_SUCCESS || ( rc == HCF_ERR_LEN && ifbp->IFB_AssertTrace & 1<<HCF_TRACE_PUT_INFO ),
2114                    MERGE_2( type, rc ) );                                                                /*20*/
2115         HCFLOGEXIT( HCF_TRACE_GET_INFO );
2116         return rc;
2117 } // hcf_get_info
2118
2119
2120 /************************************************************************************************************
2121  *
2122  *.MODULE        int hcf_put_info( IFBP ifbp, LTVP ltvp )
2123  *.PURPOSE       Transfers operation and configuration information to the Card and to the HCF.
2124  *
2125  *.ARGUMENTS
2126  *   ifbp        address of the Interface Block
2127  *   ltvp        specifies the RID (as defined by Hermes I/F) or pseudo-RID (as defined by WCI)
2128  *
2129  *.RETURNS
2130  *   HCF_SUCCESS
2131  *!! via cmd_exe
2132  *   HCF_ERR_NO_NIC      NIC removed during data retrieval
2133  *   HCF_ERR_TIME_OUT    Expected F/W event did not occur in time
2134  *   HCF_ERR_DEFUNCT_...
2135  *!! via download                CFG_DLNV_START <= type <= CFG_DL_STOP
2136  *!! via put_info                CFG_RID_CFG_MIN <= type <= CFG_RID_CFG_MAX
2137  *!! via put_frag
2138  *
2139  *.DESCRIPTION
2140  * The L-field of the LTV-record (provided by the MSF in parameter ltvp) specifies the size of the buffer.
2141  * The L-value includes the size of the T-field, but not the size of the L-field.
2142  * The T- field specifies the RID placed in the V-field by the MSF.
2143  *
2144  * Not all CFG-codes can be used for hcf_put_info.  The following CFG-codes are valid for hcf_put_info:
2145  * o One of the CFG-codes in the group "Network Parameters, Static Configuration Entities"
2146  * Changes made by hcf_put_info to CFG_codes in this group will not affect the F/W
2147  * and HCF behavior until hcf_cntl_port( HCF_PORT_ENABLE) is called.
2148  * o One of the CFG-codes in the group "Network Parameters, Dynamic Configuration Entities"
2149  * Changes made by hcf_put_info to CFG_codes will affect the F/W and HCF behavior immediately.
2150  * o CFG_PROG.
2151  * This code is used to initiate and terminate the process to download data either to
2152  * volatile or to non-volatile RAM on the NIC as well as for the actual download.
2153  * o CFG-codes related to the HCF behavior.
2154  * The related CFG-codes are:
2155  *  - CFG_REG_MB
2156  *  - CFG_REG_ASSERT_RTNP
2157  *  - CFG_REG_INFO_LOG
2158  *  - CFG_CMD_NIC
2159  *  - CFG_CMD_DONGLE
2160  *  - CFG_CMD_HCF
2161  *  - CFG_NOTIFY
2162  *
2163  * All LTV-records "unknown" to the HCF are forwarded to the F/W.
2164  *
2165  * Assert fails if
2166  * - ifbp has a recognizable out-of-range value.
2167  * - ltvp is a NULL pointer.
2168  * - hcf_put_info was called without prior call to hcf_connect
2169  * - type field of the LTV-record is invalid, i.e. neither HCF nor F/W can handle the value.
2170  * - length field of the LTV-record at entry is less than 1 or exceeds MAX_LTV_SIZE.
2171  * - registering a MailBox with size less than 60 or a non-aligned buffer address is used.
2172  * - reentrancy, may be  caused by calling hcf_functions without adequate protection against
2173  *   NIC interrupts or multi-threading.
2174  *
2175  *.DIAGRAM
2176  *
2177  *.NOTICE
2178  *   Remarks:  In case of Hermes Configuration LTVs, the codes for the type are "cleverly" chosen to be
2179  *   identical to the RID. Hermes Configuration information is copied from the provided data structure into the
2180  *   Card.
2181  *   In case of HCF Configuration LTVs, the type values are chosen in a range which does not overlap the
2182  *   RID-range.
2183  *
2184  *20:
2185  *
2186  *.ENDDOC                END DOCUMENTATION
2187  *
2188  ************************************************************************************************************/
2189
2190 int
2191 hcf_put_info( IFBP ifbp, LTVP ltvp )
2192 {
2193         int rc = HCF_SUCCESS;
2194
2195         HCFLOGENTRY( HCF_TRACE_PUT_INFO, ltvp->typ );
2196         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
2197         HCFASSERT_INT;
2198         HCFASSERT( ltvp, 0 );
2199         HCFASSERT( 1 < ltvp->len && ltvp->len <= HCF_MAX_LTV + 1, ltvp->len );
2200
2201                                                 //all codes between 0xFA00 and 0xFCFF are passed to Hermes
2202 #if (HCF_TYPE) & HCF_TYPE_WPA
2203         {
2204                 hcf_16 i;
2205                 hcf_32 FAR * key_p;
2206
2207                 if ( ltvp->typ == CFG_ADD_TKIP_DEFAULT_KEY || ltvp->typ == CFG_ADD_TKIP_MAPPED_KEY ) {
2208                         key_p = (hcf_32*)((CFG_ADD_TKIP_MAPPED_KEY_STRCT FAR *)ltvp)->tx_mic_key;
2209                         i = TX_KEY;     //i.e. TxKeyIndicator == 1, KeyID == 0
2210                         if ( ltvp->typ == CFG_ADD_TKIP_DEFAULT_KEY ) {
2211                                 key_p = (hcf_32*)((CFG_ADD_TKIP_DEFAULT_KEY_STRCT FAR *)ltvp)->tx_mic_key;
2212                                 i = CNV_LITTLE_TO_SHORT(((CFG_ADD_TKIP_DEFAULT_KEY_STRCT FAR *)ltvp)->tkip_key_id_info);
2213                         }
2214                         if ( i & TX_KEY ) { /* TxKeyIndicator == 1
2215                                                (either really set by MSF in case of DEFAULT or faked by HCF in case of MAPPED ) */
2216                                 ifbp->IFB_MICTxCntl = (hcf_16)( HFS_TX_CNTL_MIC | (i & KEY_ID )<<8 );
2217                                 ifbp->IFB_MICTxKey[0] = CNV_LONGP_TO_LITTLE( key_p );
2218                                 ifbp->IFB_MICTxKey[1] = CNV_LONGP_TO_LITTLE( (key_p+1) );
2219                         }
2220                         i = ( i & KEY_ID ) * 2;
2221                         ifbp->IFB_MICRxKey[i]   = CNV_LONGP_TO_LITTLE( (key_p+2) );
2222                         ifbp->IFB_MICRxKey[i+1] = CNV_LONGP_TO_LITTLE( (key_p+3) );
2223                 }
2224 #define P ((CFG_REMOVE_TKIP_DEFAULT_KEY_STRCT FAR *)ltvp)
2225                 if ( ( ltvp->typ == CFG_REMOVE_TKIP_MAPPED_KEY )    ||
2226                      ( ltvp->typ == CFG_REMOVE_TKIP_DEFAULT_KEY &&
2227                        ( (ifbp->IFB_MICTxCntl >> 8) & KEY_ID ) == CNV_SHORT_TO_LITTLE(P->tkip_key_id )
2228                              )
2229                         ) { ifbp->IFB_MICTxCntl = 0; }      //disable MIC-engine
2230 #undef P
2231         }
2232 #endif // HCF_TYPE_WPA
2233
2234         if ( ltvp->typ == CFG_PROG ) {
2235                 rc = download( ifbp, (CFG_PROG_STRCT FAR *)ltvp );
2236         } else switch (ltvp->typ) {
2237 #if (HCF_ASSERT) & HCF_ASSERT_RT_MSF_RTN
2238                 case CFG_REG_ASSERT_RTNP:                                         //Register MSF Routines
2239 #define P ((CFG_REG_ASSERT_RTNP_STRCT FAR *)ltvp)
2240                         ifbp->IFB_AssertRtn = P->rtnp;
2241 //                      ifbp->IFB_AssertLvl = P->lvl;       //TODO not yet supported so default is set in hcf_connect
2242                         HCFASSERT( DO_ASSERT, MERGE_2( HCF_ASSERT, 0xCAF1 ) );   //just to proof that the complete assert machinery is working
2243 #undef P
2244                         break;
2245 #endif // HCF_ASSERT_RT_MSF_RTN
2246 #if (HCF_EXT) & HCF_EXT_INFO_LOG
2247                 case CFG_REG_INFO_LOG:                                            //Register Log filter
2248                         ifbp->IFB_RIDLogp = ((CFG_RID_LOG_STRCT FAR*)ltvp)->recordp;
2249                         break;
2250 #endif // HCF_EXT_INFO_LOG
2251                 case CFG_CNTL_OPT:                                                //overrule option
2252                         HCFASSERT( ( ltvp->val[0] & ~(USE_DMA | USE_16BIT) ) == 0, ltvp->val[0] );
2253                         if ( ( ltvp->val[0] & USE_DMA ) == 0 ) ifbp->IFB_CntlOpt &= ~USE_DMA;
2254                         ifbp->IFB_CntlOpt |=  ltvp->val[0] & USE_16BIT;
2255                         break;
2256 #if (HCF_EXT) & HCF_EXT_MB
2257                 case CFG_REG_MB:                                                  //Register MailBox
2258 #define P ((CFG_REG_MB_STRCT FAR *)ltvp)
2259                         HCFASSERT( ( (hcf_32)P->mb_addr & 0x0001 ) == 0, (hcf_32)P->mb_addr );
2260                         HCFASSERT( (P)->mb_size >= 60, (P)->mb_size );
2261                         ifbp->IFB_MBp = P->mb_addr;
2262                         /* if no MB present, size must be 0 for ;?the old;? put_info_mb to work correctly */
2263                         ifbp->IFB_MBSize = ifbp->IFB_MBp == NULL ? 0 : P->mb_size;
2264                         ifbp->IFB_MBWp = ifbp->IFB_MBRp = 0;
2265                         ifbp->IFB_MBp[0] = 0;                                           //flag the MailBox as empty
2266                         ifbp->IFB_MBInfoLen = 0;
2267                         HCFASSERT( ifbp->IFB_MBSize >= 60 || ifbp->IFB_MBp == NULL, ifbp->IFB_MBSize );
2268 #undef P
2269                         break;
2270                 case CFG_MB_INFO:                                                 //store MailBoxInfoBlock
2271                         rc = put_info_mb( ifbp, (CFG_MB_INFO_STRCT FAR *)ltvp );
2272                         break;
2273 #endif // HCF_EXT_MB
2274
2275 #if (HCF_EXT) & HCF_EXT_NIC_ACCESS
2276                 case CFG_CMD_NIC:
2277 #define P ((CFG_CMD_NIC_STRCT FAR *)ltvp)
2278                         OPW( HREG_PARAM_2, P->parm2 );
2279                         OPW( HREG_PARAM_1, P->parm1 );
2280                         rc = cmd_exe( ifbp, P->cmd, P->parm0 );
2281                         P->hcf_stat = (hcf_16)rc;
2282                         P->stat = IPW( HREG_STAT );
2283                         P->resp0 = IPW( HREG_RESP_0 );
2284                         P->resp1 = IPW( HREG_RESP_1 );
2285                         P->resp2 = IPW( HREG_RESP_2 );
2286                         P->ifb_err_cmd = ifbp->IFB_ErrCmd;
2287                         P->ifb_err_qualifier = ifbp->IFB_ErrQualifier;
2288 #undef P
2289                         break;
2290                 case CFG_CMD_HCF:
2291 #define P ((CFG_CMD_HCF_STRCT FAR *)ltvp)
2292                         HCFASSERT( P->cmd == CFG_CMD_HCF_REG_ACCESS, P->cmd );       //only Hermes register access supported
2293                         if ( P->cmd == CFG_CMD_HCF_REG_ACCESS ) {
2294                                 HCFASSERT( P->mode < ifbp->IFB_IOBase, P->mode );        //Check Register space
2295                                 OPW( P->mode, P->add_info);
2296                         }
2297 #undef P
2298                         break;
2299 #endif // HCF_EXT_NIC_ACCESS
2300
2301 #if (HCF_ASSERT) & HCF_ASSERT_PRINTF
2302                 case CFG_FW_PRINTF_BUFFER_LOCATION:
2303                         ifbp->IFB_FwPfBuff = *(CFG_FW_PRINTF_BUFFER_LOCATION_STRCT*)ltvp;
2304                         break;
2305 #endif // HCF_ASSERT_PRINTF
2306
2307                 default:                      //pass everything unknown above the "FID" range to the Hermes or Dongle
2308                         rc = put_info( ifbp, ltvp );
2309                 }
2310         //DO NOT !!! HCFASSERT( rc == HCF_SUCCESS, rc )                                             /* 20 */
2311         HCFLOGEXIT( HCF_TRACE_PUT_INFO );
2312         return rc;
2313 } // hcf_put_info
2314
2315
2316 /************************************************************************************************************
2317  *
2318  *.MODULE        int hcf_rcv_msg( IFBP ifbp, DESC_STRCT *descp, unsigned int offset )
2319  *.PURPOSE       All: decapsulate a message.
2320  *               pre-HermesII.5: verify MIC.
2321  *               non-USB, non-DMA mode: Transfer a message from the NIC to the Host and acknowledge reception.
2322  *               USB: Transform a message from proprietary USB format to 802.3 format
2323  *
2324  *.ARGUMENTS
2325  *   ifbp        address of the Interface Block
2326  *   descp       Pointer to the Descriptor List location.
2327  *   offset      USB: not used
2328  *               non-USB: specifies the beginning of the data to be obtained (0 corresponds with DestAddr field
2329  *               of frame).
2330  *
2331  *.RETURNS
2332  *   HCF_SUCCESS         No WPA error ( or HCF_ERR_MIC already reported by hcf_service_nic)
2333  *   HCF_ERR_MIC         message contains an erroneous MIC ( HCF_SUCCESS is reported if HCF_ERR_MIC is already
2334  *                       reported by hcf_service_nic)
2335  *   HCF_ERR_NO_NIC      NIC removed during data retrieval
2336  *   HCF_ERR_DEFUNCT...
2337  *
2338  *.DESCRIPTION
2339  * The Receive Message Function can be executed by the MSF to obtain the Data Info fields of the message that
2340  * is reported to be available by the Service NIC Function.
2341  *
2342  * The Receive Message Function copies the message data available in the Card memory into a buffer structure
2343  * provided by the MSF.
2344  * Only data of the message indicated by the Service NIC Function can be obtained.
2345  * Execution of the Service NIC function may result in the availability of a new message, but it definitely
2346  * makes the message reported by the preceding Service NIC function, unavailable.
2347  *
2348  * in non-USB/non-DMA mode, hcf_rcv_msg starts the copy process at the (non-negative) offset requested by the
2349  * parameter offset, relative to HFS_ADDR_DEST, e.g offset 0 starts copying from the Destination Address, the
2350  * very begin of the 802.3 frame message. Offset must either lay within the part of the 802.3 frame as stored
2351  * by hcf_service_nic in the lookahead buffer or be just behind it, i.e. the first byte not yet read.
2352  * When offset is within lookahead, data is copied from lookahead.
2353  * When offset is beyond lookahead, data is read directly from RxFS in NIC with disregard of the actual value
2354  * of offset
2355  *
2356  *.NOTICE:
2357  * o at entry: look ahead buffer as passed with hcf_service_nic is still accessible and unchanged
2358  * o at exit: Receive Frame in NIC memory is released
2359  *
2360  * Description:
2361  * Starting at the byte indicated by the Offset value, the bytes are copied from the Data Info
2362  * Part of the current Receive Frame Structure to the Host memory data buffer structure
2363  * identified by descp.
2364  * The maximum value for Offset is the number of characters of the 802.3 frame read into the
2365  * look ahead buffer by hcf_service_nic (i.e. the look ahead buffer size minus
2366  * Control and 802.11 fields)
2367  * If Offset is less than the maximum value, copying starts from the look ahead buffer till the
2368  * end of that buffer is reached
2369  * Then (or if the maximum value is specified for Offset), the
2370  * message is directly copied from NIC memory to Host memory.
2371  * If an invalid (i.e. too large) offset is specified, an assert catches but the buffer contents are
2372  * undefined.
2373  * Copying stops if either:
2374  * o the end of the 802.3 frame is reached
2375  * o the Descriptor with a NULL pointer in the next_desc_addr field is reached
2376  *
2377  * When the copying stops, the receiver is ack'ed, thus freeing the NIC memory where the frame is stored
2378  * As a consequence, hcf_rcv_msg can only be called once for any particular Rx frame.
2379  *
2380  * For the time being (PCI Bus mastering not yet supported), only the following fields of each
2381  * of the descriptors in the descriptor list must be set by the MSF:
2382  * o buf_cntl.buf_dim[1]
2383  * o *next_desc_addr
2384  * o *buf_addr
2385  * At return from hcf_rcv_msg, the field buf_cntl.buf_dim[0] of the used Descriptors reflects
2386  * the number of bytes in the buffer corresponding with the Descriptor.
2387  * On the last used Descriptor, buf_cntl.buf_dim[0] is less or equal to buf_cntl.buf_dim[1].
2388  * On all preceding Descriptors buf_cntl.buf_dim[0] is equal to buf_cntl.buf_dim[1].
2389  * On all succeeding (unused) Descriptors, buf_cntl.buf_dim[0] is zero.
2390  * Note: this I/F is based on the assumptions how the I/F needed for PCI Bus mastering will
2391  * be, so it may change.
2392  *
2393  * The most likely handling of HCF_ERR_NO_NIC by the MSF is to drop the already copied
2394  * data as elegantly as possible under the constraints and requirements posed by the (N)OS.
2395  * If no received Frame Structure is pending, "Success" rather than "Read error" is returned.
2396  * This error constitutes a logic flaw in the MSF
2397  * The HCF can only catch a minority of this
2398  * type of errors
2399  * Based on consistency ideas, the HCF catches none of these errors.
2400  *
2401  * Assert fails if
2402  * - ifbp has a recognizable out-of-range value
2403  * - there is no unacknowledged Rx-message available
2404  * - offset is out of range (outside look ahead buffer)
2405  * - descp is a NULL pointer
2406  * - any of the descriptors is not double word aligned
2407  * - reentrancy, may be  caused by calling hcf_functions without adequate protection
2408  *   against NIC interrupts or multi-threading.
2409  * - Interrupts are enabled.
2410  *
2411  *.DIAGRAM
2412  *
2413  *.NOTICE
2414  * - by using unsigned int as type for offset, no need to worry about negative offsets
2415  * - Asserting on being enabled/present is superfluous, since a non-zero IFB_lal implies that hcf_service_nic
2416  *   was called and detected a Rx-message. A zero IFB_lal will set the BUF_CNT field of at least the first
2417  *   descriptor to zero.
2418  *
2419  *.ENDDOC                END DOCUMENTATION
2420  *
2421  ************************************************************************************************************/
2422 int
2423 hcf_rcv_msg( IFBP ifbp, DESC_STRCT *descp, unsigned int offset )
2424 {
2425         int         rc = HCF_SUCCESS;
2426         wci_bufp    cp;                                     //char oriented working pointer
2427         hcf_16      i;
2428         int         tot_len = ifbp->IFB_RxLen - offset;     //total length
2429         wci_bufp    lap = ifbp->IFB_lap + offset;           //start address in LookAhead Buffer
2430         hcf_16      lal = ifbp->IFB_lal - offset;           //available data within LookAhead Buffer
2431         hcf_16      j;
2432
2433         HCFLOGENTRY( HCF_TRACE_RCV_MSG, offset );
2434         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
2435         HCFASSERT_INT;
2436         HCFASSERT( descp, HCF_TRACE_RCV_MSG );
2437         HCFASSERT( ifbp->IFB_RxLen, HCF_TRACE_RCV_MSG );
2438         HCFASSERT( ifbp->IFB_RxLen >= offset, MERGE_2( offset, ifbp->IFB_RxLen ) );
2439         HCFASSERT( ifbp->IFB_lal >= offset, offset );
2440         HCFASSERT( (ifbp->IFB_CntlOpt & USE_DMA) == 0, 0xDADA );
2441
2442         if ( tot_len < 0 ) {
2443                 lal = 0; tot_len = 0;               //suppress all copying activity in the do--while loop
2444         }
2445         do {                                    //loop over all available fragments
2446                 // obnoxious hcf.c(1480) : warning C4769: conversion of near pointer to long integer
2447                 HCFASSERT( ((hcf_32)descp & 3 ) == 0, (hcf_32)descp );
2448                 cp = descp->buf_addr;
2449                 j = min( (hcf_16)tot_len, descp->BUF_SIZE );    //minimum of "what's` available" and fragment size
2450                 descp->BUF_CNT = j;
2451                 tot_len -= j;                       //adjust length still to go
2452                 if ( lal ) {                        //if lookahead Buffer not yet completely copied
2453                         i = min( lal, j );              //minimum of "what's available" in LookAhead and fragment size
2454                         lal -= i;                       //adjust length still available in LookAhead
2455                         j -= i;                         //adjust length still available in current fragment
2456                         /*;? while loop could be improved by moving words but that is complicated on platforms with
2457                          * alignment requirements*/
2458                         while ( i-- ) *cp++ = *lap++;
2459                 }
2460                 if ( j ) {  //if LookAhead Buffer exhausted but still space in fragment, copy directly from NIC RAM
2461                         get_frag( ifbp, cp, j BE_PAR(0) );
2462                         CALC_RX_MIC( cp, j );
2463                 }
2464         } while ( ( descp = descp->next_desc_addr ) != NULL );
2465 #if (HCF_TYPE) & HCF_TYPE_WPA
2466         if ( ifbp->IFB_RxFID ) {
2467                 rc = check_mic( ifbp );             //prevents MIC error report if hcf_service_nic already consumed all
2468         }
2469 #endif // HCF_TYPE_WPA
2470         (void)hcf_action( ifbp, HCF_ACT_RX_ACK );       //only 1 shot to get the data, so free the resources in the NIC
2471         HCFASSERT( rc == HCF_SUCCESS, rc );
2472         HCFLOGEXIT( HCF_TRACE_RCV_MSG );
2473         return rc;
2474 } // hcf_rcv_msg
2475
2476
2477 /************************************************************************************************************
2478  *
2479  *.MODULE        int hcf_send_msg( IFBP ifbp, DESC_STRCT *descp, hcf_16 tx_cntl )
2480  *.PURPOSE       Encapsulate a message and append padding and MIC.
2481  *               non-USB: Transfers the resulting message from Host to NIC and initiates transmission.
2482  *               USB: Transfer resulting message into a flat buffer.
2483  *
2484  *.ARGUMENTS
2485  *   ifbp        address of the Interface Block
2486  *   descp       pointer to the DescriptorList or NULL
2487  *   tx_cntl     indicates MAC-port and (Hermes) options
2488  *                   HFS_TX_CNTL_SPECTRALINK
2489  *                   HFS_TX_CNTL_PRIO
2490  *                   HFS_TX_CNTL_TX_OK
2491  *                   HFS_TX_CNTL_TX_EX
2492  *                   HFS_TX_CNTL_TX_DELAY
2493  *                   HFS_TX_CNTL_TX_CONT
2494  *                   HCF_PORT_0               MAC Port 0 (default)
2495  *                   HCF_PORT_1 (AP only)     MAC Port 1
2496  *                   HCF_PORT_2 (AP only)     MAC Port 2
2497  *                   HCF_PORT_3 (AP only)     MAC Port 3
2498  *                   HCF_PORT_4 (AP only)     MAC Port 4
2499  *                   HCF_PORT_5 (AP only)     MAC Port 5
2500  *                   HCF_PORT_6 (AP only)     MAC Port 6
2501  *
2502  *.RETURNS
2503  *   HCF_SUCCESS
2504  *   HCF_ERR_DEFUNCT_..
2505  *   HCF_ERR_TIME_OUT
2506  *
2507  *.DESCRIPTION:
2508  * The Send Message Function embodies 2 functions:
2509  * o transfers a message (including MAC header) from the provided buffer structure in Host memory to the Transmit
2510  * Frame Structure (TxFS) in NIC memory.
2511  * o Issue a send command to the F/W to actually transmit the contents of the TxFS.
2512  *
2513  * Control is based on the Resource Indicator IFB_RscInd.
2514  * The Resource Indicator is maintained by the HCF and should only be interpreted but not changed by the MSF.
2515  * The MSF must check IFB_RscInd to be non-zero before executing the call to the Send Message Function.
2516  * When no resources are available, the MSF must handle the queuing of the Transmit frame and check the
2517  * Resource Indicator periodically after calling hcf_service_nic.
2518  *
2519  * The Send Message Functions transfers a message to NIC memory when it is called with a non-NULL descp.
2520  * Before the Send Message Function is invoked this way, the Resource Indicator (IFB_RscInd) must be checked.
2521  * If the Resource is not available, Send Message Function execution must be postponed until after processing of
2522  * a next hcf_service_nic it appears that the Resource has become available.
2523  * The message is copied from the buffer structure identified by descp to the NIC.
2524  * Copying stops if a NULL pointer in the next_desc_addr field is reached.
2525  * Hcf_send_msg does not check for transmit buffer overflow, because the F/W does this protection.
2526  * In case of a transmit buffer overflow, the surplus which does not fit in the buffer is simply dropped.
2527  *
2528  * The Send Message Function activates the F/W to actually send the message to the medium when the
2529  * HFS_TX_CNTL_TX_DELAY bit of the tx_cntl parameter is not set.
2530  * If the descp parameter of the current call is non-NULL, the message as represented by descp is send.
2531  * If the descp parameter of the current call is NULL, and if the preceding call of the Send Message Function had
2532  * a non-NULL descp and the preceding call had the HFS_TX_CNTL_TX_DELAY bit of tx_cntl set, then the message as
2533  * represented by the descp of the preceding call is send.
2534  *
2535  * Hcf_send_msg supports encapsulation (see HCF_ENCAP) of Ethernet-II frames.
2536  * An Ethernet-II frame is transferred to the Transmit Frame structure as an 802.3 frame.
2537  * Hcf_send_msg distinguishes between an 802.3 and an Ethernet-II frame by looking at the data length/type field
2538  * of the frame. If this field contains a value larger than 1514, the frame is considered to be an Ethernet-II
2539  * frame, otherwise it is treated as an 802.3 frame.
2540  * To ease implementation of the HCF, this type/type field must be located in the first descriptor structure,
2541  * i.e. the 1st fragment must have a size of at least 14 (to contain DestAddr, SrcAddr and Len/Type field).
2542  * An Ethernet-II frame is encapsulated by inserting a SNAP header between the addressing information and the
2543  * type field.  This insertion is transparent for the MSF.
2544  * The HCF contains a fixed table that stores a number of types. If the value specified by the type/type field
2545  * occurs in this table, Bridge Tunnel Encapsulation is used, otherwise RFC1042 encapsulation is used.
2546  * Bridge Tunnel uses    AA AA 03 00 00 F8 as SNAP header,
2547  * RFC1042 uses  AA AA 03 00 00 00 as SNAP header.
2548  * The table currently contains:
2549  * 0 0x80F3  AppleTalk Address Resolution Protocol (AARP)
2550  * 0 0x8137  IPX
2551  *
2552  * The algorithm to distinguish between 802.3 and Ethernet-II frames limits the maximum length for frames of
2553  * 802.3 frames to 1514 bytes.
2554  * Encapsulation can be suppressed by means of the system constant HCF_ENCAP, e.g. to support proprietary
2555  * protocols with 802.3 like frames with a size larger than 1514 bytes.
2556  *
2557  * In case the HCF encapsulates the frame, the number of bytes that is actually transmitted is determined by the
2558  * cumulative value of the buf_cntl.buf_dim[0] fields.
2559  * In case the HCF does not encapsulate the frame, the number of bytes that is actually transmitted is not
2560  * determined by the cumulative value of the buf_cntl.buf_dim[DESC_CNTL_CNT] fields of the desc_strct's but by
2561  * the Length field of the 802.3 frame.
2562  * If there is a conflict between the cumulative value of the buf_cntl.buf_dim[0] fields and the
2563  * 802.3 Length field the 802.3 Length field determines the number of bytes actually transmitted by the NIC while
2564  * the cumulative value of the buf_cntl.buf_dim[0] fields determines the position of the MIC, hence a mismatch
2565  * will result in MIC errors on the Receiving side.
2566  * Currently this problem is flagged on the Transmit side by an Assert.
2567  * The following fields of each of the descriptors in the descriptor list must be set by the MSF:
2568  * o buf_cntl.buf_dim[0]
2569  * o *next_desc_addr
2570  * o *buf_addr
2571  *
2572  * All bits of the tx_cntl parameter except HFS_TX_CNTL_TX_DELAY and the HCF_PORT# bits are passed to the F/W via
2573  * the HFS_TX_CNTL field of the TxFS.
2574  *
2575  * Note that hcf_send_msg does not detect NIC absence.  The MSF is supposed to have its own -platform dependent-
2576  * way to recognize card removal/insertion.
2577  * The total system must be robust against card removal and there is no principal difference between card removal
2578  * just after hcf_send_msg returns but before the actual transmission took place or sometime earlier.
2579  *
2580  * Assert fails if
2581  * - ifbp has a recognizable out-of-range value
2582  * - descp is a NULL pointer
2583  * - no resources for PIF available.
2584  * - Interrupts are enabled.
2585  * - reentrancy, may be  caused by calling hcf_functions without adequate protection
2586  *   against NIC interrupts or multi-threading.
2587  *
2588  *.DIAGRAM
2589  *4: for the normal case (i.e. no HFS_TX_CNTL_TX_DELAY option active), a fid is acquired via the
2590  *   routine get_fid.  If no FID is acquired, the remainder is skipped without an error notification.  After
2591  *   all, the MSF is not supposed to call hcf_send_msg when no Resource is available.
2592  *7: The ControlField of the TxFS is written.  Since put_frag can only return the fatal Defunct or "No NIC", the
2593  *   return status can be ignored because when it fails, cmd_wait will fail as well.  (see also the note on the
2594  *   need for a return code below).
2595  *   Note that HFS_TX_CNTL has different values for H-I, H-I/WPA and H-II and HFS_ADDR_DEST has different
2596  *   values for H-I (regardless of WPA) and H-II.
2597  *   By writing 17, 1 or 2 ( implying 16, 0 or 1 garbage word after HFS_TX_CNTL) the BAP just gets to
2598  *   HFS_ADDR_DEST for H-I, H-I/WPA and H-II respectively.
2599  *10: if neither encapsulation nor MIC calculation is needed, splitting the first fragment in two does not
2600  *   really help but it makes the flow easier to follow to do not optimize on this difference
2601  *
2602  *   hcf_send_msg checks whether the frame is an Ethernet-II rather than an "official" 802.3 frame.
2603  *   The E-II check is based on the length/type field in the MAC header. If this field has a value larger than
2604  *   1500, E-II is assumed. The implementation of this test fails if the length/type field is not in the first
2605  *   descriptor.  If E-II is recognized, a SNAP header is inserted. This SNAP header represents either RFC1042
2606  *   or Bridge-Tunnel encapsulation, depending on the return status of the support routine hcf_encap.
2607  *
2608  *.NOTICE
2609  *   hcf_send_msg leaves the responsibility to only send messages on enabled ports at the MSF level.
2610  *   This is considered the strategy which is sufficiently adequate for all "robust" MSFs, have the least
2611  *   processor utilization and being still acceptable robust at the WCI !!!!!
2612  *
2613  *   hcf_send_msg does not NEED a return value to report NIC absence or removal during the execution of
2614  *   hcf_send_msg(), because the MSF and higher layers must be able to cope anyway with the NIC being removed
2615  *   after a successful completion of hcf_send_msg() but before the actual transmission took place.
2616  *   To accommodate user expectations the current implementation does report NIC absence.
2617  *   Defunct blocks all NIC access and will (also) be reported on a number of other calls.
2618  *
2619  *   hcf_send_msg does not check for transmit buffer overflow because the Hermes does this protection.
2620  *   In case of a transmit buffer overflow, the surplus which does not fit in the buffer is simply dropped.
2621  *   Note that this possibly results in the transmission of incomplete frames.
2622  *
2623  *   After some deliberation with F/W team, it is decided that - being in the twilight zone of not knowing
2624  *   whether the problem at hand is an MSF bug, HCF buf, F/W bug, H/W malfunction or even something else - there
2625  *   is no "best thing to do" in case of a failing send, hence the HCF considers the TxFID ownership to be taken
2626  *   over by the F/W and hopes for an Allocate event in due time
2627  *
2628  *.ENDDOC                END DOCUMENTATION
2629  *
2630  ************************************************************************************************************/
2631 int
2632 hcf_send_msg( IFBP ifbp, DESC_STRCT *descp, hcf_16 tx_cntl )
2633 {
2634         int         rc = HCF_SUCCESS;
2635         DESC_STRCT  *p /* = descp*/;        //working pointer
2636         hcf_16      len;                    // total byte count
2637         hcf_16      i;
2638
2639         hcf_16      fid = 0;
2640
2641         HCFASSERT( ifbp->IFB_RscInd || descp == NULL, ifbp->IFB_RscInd );
2642         HCFASSERT( (ifbp->IFB_CntlOpt & USE_DMA) == 0, 0xDADB );
2643
2644         HCFLOGENTRY( HCF_TRACE_SEND_MSG, tx_cntl );
2645         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
2646         HCFASSERT_INT;
2647         /* obnoxious c:/hcf/hcf.c(1480) : warning C4769: conversion of near pointer to long integer,
2648          * so skip */
2649         HCFASSERT( ((hcf_32)descp & 3 ) == 0, (hcf_32)descp );
2650 #if HCF_ASSERT
2651         {   int x = ifbp->IFB_FWIdentity.comp_id == COMP_ID_FW_AP ? tx_cntl & ~HFS_TX_CNTL_PORT : tx_cntl;
2652                 HCFASSERT( (x & ~HCF_TX_CNTL_MASK ) == 0, tx_cntl );
2653         }
2654 #endif // HCF_ASSERT
2655
2656         if ( descp ) ifbp->IFB_TxFID = 0;               //cancel a pre-put message
2657
2658 #if (HCF_EXT) & HCF_EXT_TX_CONT             // Continuous transmit test
2659         if ( tx_cntl == HFS_TX_CNTL_TX_CONT ) {
2660                 fid = get_fid(ifbp);
2661                 if (fid != 0 ) {
2662                                                 //setup BAP to begin of TxFS
2663                         (void)setup_bap( ifbp, fid, 0, IO_OUT );
2664                                                 //copy all the fragments in a transparent fashion
2665                         for ( p = descp; p; p = p->next_desc_addr ) {
2666                                 /* obnoxious warning C4769: conversion of near pointer to long integer */
2667                                 HCFASSERT( ((hcf_32)p & 3 ) == 0, (hcf_32)p );
2668                                 put_frag( ifbp, p->buf_addr, p->BUF_CNT BE_PAR(0) );
2669                         }
2670                         rc = cmd_exe( ifbp, HCMD_THESEUS | HCMD_BUSY | HCMD_STARTPREAMBLE, fid );
2671                         if ( ifbp->IFB_RscInd == 0 ) {
2672                                 ifbp->IFB_RscInd = get_fid( ifbp );
2673                         }
2674                 }
2675                                                 // een slecht voorbeeld doet goed volgen ;?
2676                 HCFLOGEXIT( HCF_TRACE_SEND_MSG );
2677                 return rc;
2678         }
2679 #endif // HCF_EXT_TX_CONT
2680         /* the following initialization code is redundant for a pre-put message
2681          * but moving it inside the "if fid" logic makes the merging with the
2682          * USB flow awkward
2683          */
2684 #if (HCF_TYPE) & HCF_TYPE_WPA
2685         tx_cntl |= ifbp->IFB_MICTxCntl;
2686 #endif // HCF_TYPE_WPA
2687         fid = ifbp->IFB_TxFID;
2688         if (fid == 0 && ( fid = get_fid( ifbp ) ) != 0 )        /* 4 */
2689                 /* skip the next compound statement if:
2690                    - pre-put message or
2691                    - no fid available (which should never occur if the MSF adheres to the WCI)
2692                 */
2693         {       // to match the closing curly bracket of above "if" in case of HCF_TYPE_USB
2694                                                 //calculate total length ;? superfluous unless CCX or Encapsulation
2695                 len = 0;
2696                 p = descp;
2697                 do len += p->BUF_CNT; while ( ( p = p->next_desc_addr ) != NULL );
2698                 p = descp;
2699 //;?            HCFASSERT( len <= HCF_MAX_MSG, len );
2700         /*7*/   (void)setup_bap( ifbp, fid, HFS_TX_CNTL, IO_OUT );
2701 #if (HCF_TYPE) & HCF_TYPE_TX_DELAY
2702                 HCFASSERT( ( descp != NULL ) ^ ( tx_cntl & HFS_TX_CNTL_TX_DELAY ), tx_cntl );
2703                 if ( tx_cntl & HFS_TX_CNTL_TX_DELAY ) {
2704                         tx_cntl &= ~HFS_TX_CNTL_TX_DELAY;       //!!HFS_TX_CNTL_TX_DELAY no longer available
2705                         ifbp->IFB_TxFID = fid;
2706                         fid = 0;                                //!!fid no longer available, be careful when modifying code
2707                 }
2708 #endif // HCF_TYPE_TX_DELAY
2709                 OPW( HREG_DATA_1, tx_cntl ) ;
2710                 OPW( HREG_DATA_1, 0 );
2711 #if ! ( (HCF_TYPE) & HCF_TYPE_CCX )
2712                 HCFASSERT( p->BUF_CNT >= 14, p->BUF_CNT );
2713                                                 /* assume DestAddr/SrcAddr/Len/Type ALWAYS contained in 1st fragment
2714                                                  * otherwise life gets too cumbersome for MIC and Encapsulation !!!!!!!!
2715                  if ( p->BUF_CNT >= 14 ) {   alternatively: add a safety escape !!!!!!!!!!!! }   */
2716 #endif // HCF_TYPE_CCX
2717                 CALC_TX_MIC( NULL, -1 );        //initialize MIC
2718         /*10*/  put_frag( ifbp, p->buf_addr, HCF_DASA_SIZE BE_PAR(0) ); //write DA, SA with MIC calculation
2719                 CALC_TX_MIC( p->buf_addr, HCF_DASA_SIZE );      //MIC over DA, SA
2720                 CALC_TX_MIC( null_addr, 4 );        //MIC over (virtual) priority field
2721 #if (HCF_TYPE) & HCF_TYPE_CCX
2722                 //!!be careful do not use positive test on HCF_ACT_CCX_OFF, because IFB_CKIPStat is initially 0
2723                 if(( ifbp->IFB_CKIPStat == HCF_ACT_CCX_ON ) ||
2724                    ((GET_BUF_CNT(p) >= 20 )   && ( ifbp->IFB_CKIPStat == HCF_ACT_CCX_OFF ) &&
2725                     (p->buf_addr[12] == 0xAA) && (p->buf_addr[13] == 0xAA) &&
2726                     (p->buf_addr[14] == 0x03) && (p->buf_addr[15] == 0x00) &&
2727                     (p->buf_addr[16] == 0x40) && (p->buf_addr[17] == 0x96) &&
2728                     (p->buf_addr[18] == 0x00) && (p->buf_addr[19] == 0x00)))
2729                 {
2730                         i = HCF_DASA_SIZE;
2731
2732                         OPW( HREG_DATA_1, CNV_SHORT_TO_BIG( len - i ));
2733
2734                         /* need to send out the remainder of the fragment */
2735                         put_frag( ifbp, &p->buf_addr[i], GET_BUF_CNT(p) - i BE_PAR(0) );
2736                 }
2737                 else
2738 #endif // HCF_TYPE_CCX
2739                 {
2740                                                 //if encapsulation needed
2741 #if (HCF_ENCAP) == HCF_ENC
2742                                                 //write length (with SNAP-header,Type, without //DA,SA,Length ) no MIC calc.
2743                         if ( ( snap_header[sizeof(snap_header)-1] = hcf_encap( &p->buf_addr[HCF_DASA_SIZE] ) ) != ENC_NONE ) {
2744                                 OPW( HREG_DATA_1, CNV_END_SHORT( len + (sizeof(snap_header) + 2) - ( 2*6 + 2 ) ) );
2745                                                 //write splice with MIC calculation
2746                                 put_frag( ifbp, snap_header, sizeof(snap_header) BE_PAR(0) );
2747                                 CALC_TX_MIC( snap_header, sizeof(snap_header) );    //MIC over 6 byte SNAP
2748                                 i = HCF_DASA_SIZE;
2749                         } else
2750 #endif // HCF_ENC
2751                         {
2752                                 OPW( HREG_DATA_1, *(wci_recordp)&p->buf_addr[HCF_DASA_SIZE] );
2753                                 i = 14;
2754                         }
2755                                                 //complete 1st fragment starting with Type with MIC calculation
2756                         put_frag( ifbp, &p->buf_addr[i], p->BUF_CNT - i BE_PAR(0) );
2757                         CALC_TX_MIC( &p->buf_addr[i], p->BUF_CNT - i );
2758                 }
2759                                                 //do the remaining fragments with MIC calculation
2760                 while ( ( p = p->next_desc_addr ) != NULL ) {
2761                         /* obnoxious c:/hcf/hcf.c(1480) : warning C4769: conversion of near pointer to long integer,
2762                          * so skip */
2763                         HCFASSERT( ((hcf_32)p & 3 ) == 0, (hcf_32)p );
2764                         put_frag( ifbp, p->buf_addr, p->BUF_CNT BE_PAR(0) );
2765                         CALC_TX_MIC( p->buf_addr, p->BUF_CNT );
2766                 }
2767                                                 //pad message, finalize MIC calculation and write MIC to NIC
2768                 put_frag_finalize( ifbp );
2769         }
2770         if ( fid ) {
2771         /*16*/  rc = cmd_exe( ifbp, HCMD_BUSY | HCMD_TX | HCMD_RECL, fid );
2772                 ifbp->IFB_TxFID = 0;
2773                 /* probably this (i.e. no RscInd AND "HREG_EV_ALLOC") at this point in time occurs so infrequent,
2774                  * that it might just as well be acceptable to skip this
2775                  * "optimization" code and handle that additional interrupt once in a while
2776                  */
2777 // 180 degree error in logic ;? #if ALLOC_15
2778         /*20*/  if ( ifbp->IFB_RscInd == 0 ) {
2779                         ifbp->IFB_RscInd = get_fid( ifbp );
2780                 }
2781 // #endif // ALLOC_15
2782         }
2783 //      HCFASSERT( level::ifbp->IFB_RscInd, ifbp->IFB_RscInd );
2784         HCFLOGEXIT( HCF_TRACE_SEND_MSG );
2785         return rc;
2786 } // hcf_send_msg
2787
2788
2789 /************************************************************************************************************
2790  *
2791  *.MODULE        int hcf_service_nic( IFBP ifbp, wci_bufp bufp, unsigned int len )
2792  *.PURPOSE       Services (most) NIC events.
2793  *               Provides received message
2794  *               Provides status information.
2795  *
2796  *.ARGUMENTS
2797  *   ifbp        address of the Interface Block
2798  *  In non-DMA mode:
2799  *   bufp        address of char buffer, sufficiently large to hold the first part of the RxFS up through HFS_TYPE
2800  *   len         length in bytes of buffer specified by bufp
2801  *               value between HFS_TYPE + 2 and HFS_ADDR_DEST + HCF_MAX_MSG
2802  *
2803  *.RETURNS
2804  *   HCF_SUCCESS
2805  *   HCF_ERR_MIC message contains an erroneous MIC (only if frame fits completely in bufp)
2806  *
2807  *.DESCRIPTION
2808  *
2809  * MSF-accessible fields of Result Block
2810  * - IFB_RxLen           0 or Frame size.
2811  * - IFB_MBInfoLen       0 or the L-field of the oldest MBIB.
2812  * - IFB_RscInd
2813  * - IFB_HCF_Tallies     updated if a corresponding event occurred.
2814  * - IFB_NIC_Tallies     updated if a Tally Info frame received from the NIC.
2815  * - IFB_DmaPackets
2816  * - IFB_TxFsStat
2817  * - IFB_TxFsSwSup
2818  * - IFB_LinkStat        reflects new link status or 0x0000 if no change relative to previous hcf_service_nic call.
2819 or
2820 * - IFB_LinkStat        link status, 0x8000 reflects change relative to previous hcf_service_nic call.
2821 *
2822 * When IFB_MBInfoLen is non-zero, at least one MBIB is available.
2823 *
2824 * IFB_RxLen reflects the number of received bytes in 802.3 view (Including DestAddr, SrcAddr and Length,
2825 * excluding MIC-padding, MIC and sum check) of active Rx Frame Structure. If no Rx Data s available, IFB_RxLen
2826 * equals 0x0000.
2827 * Repeated execution causes the Service NIC Function to provide information about subsequently received
2828 * messages, irrespective whether a hcf_rcv_msg or hcf_action(HCF_ACT_RX) is performed in between.
2829 *
2830 * When IFB_RxLen is non-zero, a Received Frame Structure is available to be routed to the protocol stack.
2831 * When Monitor Mode is not active, this is guaranteed to be an error-free non-WMP frame.
2832 * In case of Monitor Mode, it may also be a frame with an error or a WMP frame.
2833 * Erroneous frames have a non-zero error-sub field in the HFS_STAT field in the look ahead buffer.
2834 *
2835 * If a Receive message is available in NIC RAM, the Receive Frame Structure is (partly) copied from the NIC to
2836 * the buffer identified by bufp.
2837 * Copying stops either after len bytes or when the complete 802.3 frame is copied.
2838 * During the copying the message is decapsulated (if appropriate).
2839 * If the frame is read completely by hcf_service_nic (i.e. the frame fits completely in the lookahead buffer),
2840 * the frame is automatically ACK'ed to the F/W and still available via the look ahead buffer and hcf_rcv_msg.
2841 * Only if the frame is read completely by hcf_service_nic, hcf_service_nic checks the MIC and sets the return
2842 * status accordingly.  In this case, hcf_rcv_msg does not check the MIC.
2843 *
2844 * The MIC calculation algorithm works more efficient if the length of the look ahead buffer is
2845 * such that it fits exactly 4 n bytes of the 802.3 frame, i.e. len == HFS_ADDR_DEST + 4*n.
2846 *
2847 * The Service NIC Function supports the NIC event service handling process.
2848 * It performs the appropriate actions to service the NIC, such that the event cause is eliminated and related
2849 * information is saved.
2850 * The Service NIC Function is executed by the MSF ISR or polling routine as first step to determine the event
2851 * cause(s).  It is the responsibility of the MSF to perform all not directly NIC related interrupt service
2852 * actions, e.g. in a PC environment this includes servicing the PIC, and managing the Processor Interrupt
2853 * Enabling/Disabling.
2854 * In case of a polled based system, the Service NIC Function must be executed "frequently".
2855 * The Service NIC Function may have side effects related to the Mailbox and Resource Indicator (IFB_RscInd).
2856 *
2857 * hcf_service_nic returns:
2858 * - The length of the data in the available MBIB (IFB_MBInfoLen)
2859 * - Changes in the link status (IFB_LinkStat)
2860 * - The length of the data in the available Receive Frame Structure (IFB_RxLen)
2861 * - updated IFB_RscInd
2862 * - Updated Tallies
2863 *
2864 * hcf_service_nic is presumed to neither interrupt other HCF-tasks nor to be interrupted by other HCF-tasks.
2865 * A way to achieve this is to precede hcf_service_nic as well as all other HCF-tasks with a call to
2866 * hcf_action to disable the card interrupts and, after all work is completed, with a call to hcf_action to
2867 * restore (which is not necessarily the same as enabling) the card interrupts.
2868 * In case of a polled environment, it is assumed that the MSF programmer is sufficiently familiar with the
2869 * specific requirements of that environment to translate the interrupt strategy to a polled strategy.
2870 *
2871 * hcf_service_nic services the following Hermes events:
2872 * - HREG_EV_INFO        Asynchronous Information Frame
2873 * - HREG_EV_INFO_DROP   WMAC did not have sufficient RAM to build Unsolicited Information Frame
2874 * - HREG_EV_TX_EXC      (if applicable, i.e. selected via HCF_EXT_INT_TX_EX bit of HCF_EXT)
2875 * - HREG_EV_SLEEP_REQ   (if applicable, i.e. selected via HCF_DDS/HCF_CDS bit of HCF_SLEEP)
2876 * ** in non_DMA mode
2877 * - HREG_EV_ALLOC       Asynchronous part of Allocation/Reclaim completed while out of resources at
2878 *                       completion of hcf_send_msg/notify
2879 * - HREG_EV_RX          the detection of the availability of received messages
2880 *                       including WaveLAN Management Protocol (WMP) message processing
2881 * ** in DMA mode
2882 * - HREG_EV_RDMAD
2883 * - HREG_EV_TDMAD
2884 *!! hcf_service_nic does not service the following Hermes events:
2885 *!!     HREG_EV_TX          (the "OK" Tx Event) is no longer supported by the WCI, if it occurs it is unclear
2886 *!!                         what the cause is, so no meaningful strategy is available. Not acking the bit is
2887 *!!                         probably the best help that can be given to the debugger.
2888 *!!     HREG_EV_CMD         handled in cmd_wait.
2889 *!!     HREG_EV_FW_DMA      (i.e. HREG_EV_RXDMA, HREG_EV_TXDMA and_EV_LPESC) are either not used or used
2890 *!!                         between the F/W and the DMA engine.
2891 *!!     HREG_EV_ACK_REG_READY is only applicable for H-II (i.e. not HII.5 and up, see DAWA)
2892 *
2893 *   If, in non-DMA mode, a Rx message is available, its length is reflected by the IFB_RxLen field of the IFB.
2894 *   This length reflects the data itself and the Destination Address, Source Address and DataLength/Type field
2895 *   but not the SNAP-header in case of decapsulation by the HCF.  If no message is available, IFB_RxLen is
2896 *   zero.  Former versions of the HCF handled WMP messages and supported a "monitor" mode in hcf_service_nic,
2897 *   which deposited certain or all Rx messages in the MailBox. The responsibility to handle these frames is
2898 *   moved to the MSF. The HCF offers as supports hcf_put_info with CFG_MB_INFO as parameter to emulate the old
2899 *   implementation under control of the MSF.
2900 *
2901 * **Rx Buffer free strategy
2902 *   When hcf_service_nic reports the availability of a non-DMA message, the MSF can access that message by
2903 *   means of hcf_rcv_msg. It must be prevented that the LAN Controller writes new data in the NIC buffer
2904 *   before the MSF is finished with the current message. The NIC buffer is returned to the LAN Controller
2905 *   when:
2906 *    - the complete frame fits in the lookahead buffer or
2907 *    - hcf_rcv_msg is called or
2908 *    - hcf_action with HCF_ACT_RX is called or
2909 *    - hcf_service_nic is called again
2910 *   It can be reasoned that hcf_action( INT_ON ) should not be given before the MSF has completely processed
2911 *   a reported Rx-frame. The reason is that the INT_ON action is guaranteed to cause a (Rx-)interrupt (the
2912 *   MSF is processing a Rx-frame, hence the Rx-event bit in the Hermes register must be active). This
2913 *   interrupt will cause hcf_service_nic to be called, which will cause the ack-ing of the "last" Rx-event
2914 *   to the Hermes, causing the Hermes to discard the associated NIC RAM buffer.
2915 * Assert fails if
2916 * - ifbp is zero or other recognizable out-of-range value.
2917 * - hcf_service_nic is called without a prior call to hcf_connect.
2918 * - interrupts are enabled.
2919 * - reentrancy, may be  caused by calling hcf_functions without adequate protection
2920 *   against NIC interrupts or multi-threading.
2921 *
2922 *
2923 *.DIAGRAM
2924 *1: IFB_LinkStat is cleared, if a LinkStatus frame is received, IFB_LinkStat will be updated accordingly
2925 *   by isr_info.
2926 or
2927 *1: IFB_LinkStat change indication is cleared. If a LinkStatus frame is received, IFB_LinkStat will be updated
2928 *   accordingly by isr_info.
2929 *2: IFB_RxLen must be cleared before the NIC presence check otherwise:
2930 *    -  this value may stay non-zero if the NIC is pulled out at an inconvenient moment.
2931 *    -  the RxAck on a zero-FID needs a zero-value for IFB_RxLen to work
2932 *    Note that as side-effect of the hcf_action call, the remainder of Rx related info is re-initialized as
2933 *    well.
2934 *4: In case of Defunct mode, the information supplied by Hermes is unreliable, so the body of
2935 *   hcf_service_nic is skipped. Since hcf_cntl turns into a NOP if Primary or Station F/W is incompatible,
2936 *   hcf_service_nic is also skipped in those cases.
2937 *   To prevent that hcf_service_nic reports bogus information to the MSF with all - possibly difficult to
2938 *   debug - undesirable side effects, it is paramount to check the NIC presence. In former days the presence
2939 *   test was based on the Hermes register HREG_SW_0. Since in HCF_ACT_INT_OFF is chosen for strategy based on
2940 *   HREG_EV_STAT, this is now also used in hcf_service_nic. The motivation to change strategy is partly
2941 *   due to inconsistent F/W implementations with respect to HREG_SW_0 manipulation around reset and download.
2942 *   Note that in polled environments Card Removal is not detected by INT_OFF which makes the check in
2943 *   hcf_service_nic even more important.
2944 *8: The event status register of the Hermes is sampled
2945 *   The assert checks for unexpected events ;?????????????????????????????????????.
2946 *    - HREG_EV_INFO_DROP is explicitly excluded from the acceptable HREG_EV_STAT bits because it indicates
2947 *      a too heavily loaded system.
2948 *    - HREG_EV_ACK_REG_READY is 0x0000 for H-I (and hopefully H-II.5)
2949 *
2950 *
2951 *   HREG_EV_TX_EXC is accepted (via HREG_EV_TX_EXT) if and only if HCF_EXT_INT_TX_EX set in the HCF_EXT
2952 *   definition at compile time.
2953 *   The following activities are handled:
2954 *    -  Alloc events are handled by hcf_send_msg (and notify). Only if there is no "spare" resource, the
2955 *       alloc event is superficially serviced by hcf_service_nic to create a pseudo-resource with value
2956 *       0x001. This value is recognized by get_fid (called by hcf_send_msg and notify) where the real
2957 *       TxFid is retrieved and the Hermes is acked and - hopefully - the "normal" case with a spare TxFid
2958 *       in IFB_RscInd is restored.
2959 *    -  Info drop events are handled by incrementing a tally
2960 *    -  LinkEvent (including solicited and unsolicited tallies) are handled by procedure isr_info.
2961 *   -   TxEx (if selected at compile time) is handled by copying the significant part of the TxFS
2962 *       into the IFB for further processing by the MSF.
2963 *       Note the complication of the zero-FID protection sub-scheme in DAWA.
2964 *   Note, the Ack of all of above events is handled at the end of hcf_service_nic
2965 *16: In case of  non-DMA ( either not compiled in or due to a run-time choice):
2966 *   If an Rx-frame is available, first the FID of that frame is read, including the complication of the
2967 *   zero-FID protection sub-scheme in DAWA. Note that such a zero-FID is acknowledged at the end of
2968 *   hcf_service_nic and that this depends on the IFB_RxLen initialization in the begin of hcf_service_nic.
2969 *   The Assert validates the HCF assumption about Hermes implementation upon which the range of
2970 *   Pseudo-RIDs is based.
2971 *   Then the control fields up to the start of the 802.3 frame are read from the NIC into the lookahead buffer.
2972 *   The status field is converted to native Endianess.
2973 *   The length is, after implicit Endianess conversion if needed, and adjustment for the 14 bytes of the
2974 *   802.3 MAC header, stored in IFB_RxLen.
2975 *   In MAC Monitor mode, 802.11 control frames with a TOTAL length of 14 are received, so without this
2976 *   length adjustment, IFB_RxLen could not be used to distinguish these frames from "no frame".
2977 *   No MIC calculation processes are associated with the reading of these Control fields.
2978 *26: This length test feels like superfluous robustness against malformed frames, but it turned out to be
2979 *   needed in the real (hostile) world.
2980 *   The decapsulation check needs sufficient data to represent DA, SA, L, SNAP and Type which amounts to
2981 *   22 bytes. In MAC Monitor mode, 802.11 control frames with a smaller length are received. To prevent
2982 *   that the implementation goes haywire, a check on the length is needed.
2983 *   The actual decapsulation takes place on the fly in the copying process by overwriting the SNAP header.
2984 *   Note that in case of decapsulation the SNAP header is not passed to the MSF, hence IFB_RxLen must be
2985 *   compensated for the SNAP header length.
2986 *   The 22 bytes needed for decapsulation are (more than) sufficient for the exceptional handling of the
2987 *   MIC algorithm of the L-field (replacing the 2 byte L-field with 4 0x00 bytes).
2988 *30: The 12 in the no-WPA branch corresponds with the get_frag, the 2 with the IPW of the WPA branch
2989 *32: If Hermes reported MIC-presence, than the MIC engine is initialized with the non-dummy MIC calculation
2990 *   routine address and appropriate key.
2991 *34: The 8 bytes after the DA, SA, L are read and it is checked whether decapsulation is needed i.e.:
2992 *     - the Hermes reported Tunnel encapsulation or
2993 *     - the Hermes reported 1042 Encapsulation and hcf_encap reports that the HCF would not have used
2994 *       1042 as the encapsulation mechanism
2995 *   Note that the first field of the RxFS in bufp has Native Endianess due to the conversion done by the
2996 *   BE_PAR in get_frag.
2997 *36: The Type field is the only word kept (after moving) of the just read 8 bytes, it is moved to the
2998 *   L-field.  The original L-field and 6 byte SNAP header are discarded, so IFB_RxLen and buf_addr must
2999 *   be adjusted by 8.
3000 *40: Determine how much of the frame (starting with DA) fits in the Lookahead buffer, then read the not-yet
3001 *   read data into the lookahead buffer.
3002 *   If the lookahead buffer contains the complete message, check the MIC. The majority considered this
3003 *   I/F more appropriate then have the MSF call hcf_get_data only to check the MIC.
3004 *44: Since the complete message is copied from NIC RAM to PC RAM, the Rx can be acknowledged to the Hermes
3005 *   to optimize the flow ( a better chance to get new Rx data in the next pass through hcf_service_nic ).
3006 *   This acknowledgement can not be done via hcf_action( HCF_ACT_RX_ACK ) because this also clears
3007 *   IFB_RxLEN thus corrupting the I/F to the MSF.
3008 *;?: In case of DMA (compiled in and activated):
3009
3010
3011 *54: Limiting the number of places where the F/W is acked (e.g. the merging of the Rx-ACK with the other
3012 *   ACKs), is supposed to diminish the potential of race conditions in the F/W.
3013 *   Note 1: The CMD event is acknowledged in cmd_cmpl
3014 *   Note 2: HREG_EV_ACK_REG_READY is 0x0000 for H-I (and hopefully H-II.5)
3015 *   Note 3: The ALLOC event is acknowledged in get_fid (except for the initialization flow)
3016 *
3017 *.NOTICE
3018 * The Non-DMA HREG_EV_RX is handled different compared with the other F/W events.
3019 * The HREG_EV_RX event is acknowledged by the first hcf_service_nic call after the
3020 * hcf_service_nic call that reported the occurrence of this event.
3021 * This acknowledgment
3022 * makes the next Receive Frame Structure (if any) available.
3023 * An updated IFB_RxLen
3024 * field reflects this availability.
3025 *
3026 *.NOTICE
3027 * The minimum size for Len must supply space for:
3028 * - an F/W dependent number of bytes of Control Info field including the 802.11 Header field
3029 * - Destination Address
3030 * - Source Address
3031 * - Length field
3032 * - [ SNAP Header]
3033 * - [ Ethernet-II Type]
3034 * This results in 68 for Hermes-I and 80 for Hermes-II
3035 * This way the minimum amount of information is available needed by the HCF to determine whether the frame
3036 * must be decapsulated.
3037 *.ENDDOC                END DOCUMENTATION
3038 *
3039 ************************************************************************************************************/
3040 int
3041 hcf_service_nic( IFBP ifbp, wci_bufp bufp, unsigned int len )
3042 {
3043
3044         int         rc = HCF_SUCCESS;
3045         hcf_16      stat;
3046         wci_bufp    buf_addr;
3047         hcf_16      i;
3048
3049         HCFLOGENTRY( HCF_TRACE_SERVICE_NIC, ifbp->IFB_IntOffCnt );
3050         HCFASSERT( ifbp->IFB_Magic == HCF_MAGIC, ifbp->IFB_Magic );
3051         HCFASSERT_INT;
3052
3053         ifbp->IFB_LinkStat = 0; // ;? to be obsoleted ASAP                                              /* 1*/
3054         ifbp->IFB_DSLinkStat &= ~CFG_LINK_STAT_CHANGE;                                                  /* 1*/
3055         (void)hcf_action( ifbp, HCF_ACT_RX_ACK );                                                       /* 2*/
3056         if ( ifbp->IFB_CardStat == 0 && ( stat = IPW( HREG_EV_STAT ) ) != 0xFFFF ) {                    /* 4*/
3057 /*              IF_NOT_DMA( HCFASSERT( !( stat & ~HREG_EV_BASIC_MASK, stat ) )
3058  *              IF_NOT_USE_DMA( HCFASSERT( !( stat & ~HREG_EV_BASIC_MASK, stat ) )
3059  *              IF_USE_DMA( HCFASSERT( !( stat & ~( HREG_EV_BASIC_MASK ^ ( HREG_EV_...DMA.... ), stat ) )
3060  */
3061                                                                                                         /* 8*/
3062                 if ( ifbp->IFB_RscInd == 0 && stat & HREG_EV_ALLOC ) { //Note: IFB_RscInd is ALWAYS 1 for DMA
3063                         ifbp->IFB_RscInd = 1;
3064                 }
3065                 IF_TALLY( if ( stat & HREG_EV_INFO_DROP ) { ifbp->IFB_HCF_Tallies.NoBufInfo++; } );
3066 #if (HCF_EXT) & HCF_EXT_INT_TICK
3067                 if ( stat & HREG_EV_TICK ) {
3068                         ifbp->IFB_TickCnt++;
3069                 }
3070 #if 0 // (HCF_SLEEP) & HCF_DDS
3071                 if ( ifbp->IFB_TickCnt == 3 && ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_CONNECTED ) == 0 ) {
3072                         CFG_DDS_TICK_TIME_STRCT ltv;
3073                         // 2 second period (with 1 tick uncertanty) in not-connected mode -->go into DS_OOR
3074                         hcf_action( ifbp, HCF_ACT_SLEEP );
3075                         ifbp->IFB_DSLinkStat |= CFG_LINK_STAT_DS_OOR; //set OutOfRange
3076                         ltv.len = 2;
3077                         ltv.typ = CFG_DDS_TICK_TIME;
3078                         ltv.tick_time = ( ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_TIMER ) + 0x10 ) *64; //78 is more right
3079                         hcf_put_info( ifbp, (LTVP)&ltv );
3080                         printk( "<5>Preparing for sleep, link_status: %04X, timer : %d\n",
3081                                 ifbp->IFB_DSLinkStat, ltv.tick_time );//;?remove me 1 day
3082                         ifbp->IFB_TickCnt++; //;?just to make sure we do not keep on printing above message
3083                         if ( ltv.tick_time < 300 * 125 ) ifbp->IFB_DSLinkStat += 0x0010;
3084
3085                 }
3086 #endif // HCF_DDS
3087 #endif // HCF_EXT_INT_TICK
3088                 if ( stat & HREG_EV_INFO ) {
3089                         isr_info( ifbp );
3090                 }
3091 #if (HCF_EXT) & HCF_EXT_INT_TX_EX
3092                 if ( stat & HREG_EV_TX_EXT && ( i = IPW( HREG_TX_COMPL_FID ) ) != 0 /*DAWA*/ ) {
3093                         DAWA_ZERO_FID( HREG_TX_COMPL_FID );
3094                         (void)setup_bap( ifbp, i, 0, IO_IN );
3095                         get_frag( ifbp, &ifbp->IFB_TxFsStat, HFS_SWSUP BE_PAR(1) );
3096                 }
3097 #endif // HCF_EXT_INT_TX_EX
3098 //!rlav DMA engine will handle the rx event, not the driver
3099 #if HCF_DMA
3100                 if ( !( ifbp->IFB_CntlOpt & USE_DMA ) ) //!! be aware of the logical indentations
3101 #endif // HCF_DMA
3102                 /*16*/  if ( stat & HREG_EV_RX && ( ifbp->IFB_RxFID = IPW( HREG_RX_FID ) ) != 0 ) { //if 0 then DAWA_ACK
3103                                 HCFASSERT( bufp, len );
3104                                 HCFASSERT( len >= HFS_DAT + 2, len );
3105                                 DAWA_ZERO_FID( HREG_RX_FID );
3106                                 HCFASSERT( ifbp->IFB_RxFID < CFG_PROD_DATA, ifbp->IFB_RxFID);
3107                                 (void)setup_bap( ifbp, ifbp->IFB_RxFID, 0, IO_IN );
3108                                 get_frag( ifbp, bufp, HFS_ADDR_DEST BE_PAR(1) );
3109                                 ifbp->IFB_lap = buf_addr = bufp + HFS_ADDR_DEST;
3110                                 ifbp->IFB_RxLen = (hcf_16)(bufp[HFS_DAT_LEN] + (bufp[HFS_DAT_LEN+1]<<8) + 2*6 + 2);
3111                         /*26*/  if ( ifbp->IFB_RxLen >= 22 ) {  // convenient for MIC calculation (5 DWs + 1 "skipped" W)
3112                                                                 //.  get DA,SA,Len/Type and (SNAP,Type or 8 data bytes)
3113                                 /*30*/  get_frag( ifbp, buf_addr, 22 BE_PAR(0) );
3114                                 /*32*/  CALC_RX_MIC( bufp, -1 );        //.  initialize MIC
3115                                         CALC_RX_MIC( buf_addr, HCF_DASA_SIZE ); //.  MIC over DA, SA
3116                                         CALC_RX_MIC( null_addr, 4 );    //.  MIC over (virtual) priority field
3117                                         CALC_RX_MIC( buf_addr+14, 8 );  //.  skip Len, MIC over SNAP,Type or 8 data bytes)
3118                                         buf_addr += 22;
3119 #if (HCF_TYPE) & HCF_TYPE_CCX
3120 //!!be careful do not use positive test on HCF_ACT_CCX_OFF, because IFB_CKIPStat is initially 0
3121                                         if( ifbp->IFB_CKIPStat != HCF_ACT_CCX_ON  )
3122 #endif // HCF_TYPE_CCX
3123                                         {
3124 #if (HCF_ENCAP) == HCF_ENC
3125                                                 HCFASSERT( len >= HFS_DAT + 2 + sizeof(snap_header), len );
3126                                         /*34*/  i = *(wci_recordp)&bufp[HFS_STAT] & ( HFS_STAT_MSG_TYPE | HFS_STAT_ERR );
3127                                                 if ( i == HFS_STAT_TUNNEL ||
3128                                                      ( i == HFS_STAT_1042 && hcf_encap( (wci_bufp)&bufp[HFS_TYPE] ) != ENC_TUNNEL ) ) {
3129                                                                         //.  copy E-II Type to 802.3 LEN field
3130                                                 /*36*/  bufp[HFS_LEN  ] = bufp[HFS_TYPE  ];
3131                                                         bufp[HFS_LEN+1] = bufp[HFS_TYPE+1];
3132                                                                         //.  discard Snap by overwriting with data
3133                                                         ifbp->IFB_RxLen -= (HFS_TYPE - HFS_LEN);
3134                                                         buf_addr -= ( HFS_TYPE - HFS_LEN ); // this happens to bring us at a DW boundary of 36
3135                                                 }
3136 #endif // HCF_ENC
3137                                         }
3138                                 }
3139                         /*40*/  ifbp->IFB_lal = min( (hcf_16)(len - HFS_ADDR_DEST), ifbp->IFB_RxLen );
3140                                 i = ifbp->IFB_lal - ( buf_addr - ( bufp + HFS_ADDR_DEST ) );
3141                                 get_frag( ifbp, buf_addr, i BE_PAR(0) );
3142                                 CALC_RX_MIC( buf_addr, i );
3143 #if (HCF_TYPE) & HCF_TYPE_WPA
3144                                 if ( ifbp->IFB_lal == ifbp->IFB_RxLen ) {
3145                                         rc = check_mic( ifbp );
3146                                 }
3147 #endif // HCF_TYPE_WPA
3148                         /*44*/  if ( len - HFS_ADDR_DEST >= ifbp->IFB_RxLen ) {
3149                                         ifbp->IFB_RxFID = 0;
3150                                 } else { /* IFB_RxFID is cleared, so  you do not get another Rx_Ack at next entry of hcf_service_nic */
3151                                         stat &= (hcf_16)~HREG_EV_RX;    //don't ack Rx if processing not yet completed
3152                                 }
3153                         }
3154                 // in case of DMA: signal availability of rx and/or tx packets to MSF
3155                 IF_USE_DMA( ifbp->IFB_DmaPackets |= stat & ( HREG_EV_RDMAD | HREG_EV_TDMAD ) );
3156                 // rlav : pending HREG_EV_RDMAD or HREG_EV_TDMAD events get acknowledged here.
3157         /*54*/  stat &= (hcf_16)~( HREG_EV_SLEEP_REQ | HREG_EV_CMD | HREG_EV_ACK_REG_READY | HREG_EV_ALLOC | HREG_EV_FW_DMA );
3158 //a positive mask would be easier to understand /*54*/  stat &= (hcf_16)~( HREG_EV_SLEEP_REQ | HREG_EV_CMD | HREG_EV_ACK_REG_READY | HREG_EV_ALLOC | HREG_EV_FW_DMA );
3159                 IF_USE_DMA( stat &= (hcf_16)~HREG_EV_RX );
3160                 if ( stat ) {
3161                         DAWA_ACK( stat );   /*DAWA*/
3162                 }
3163         }
3164         HCFLOGEXIT( HCF_TRACE_SERVICE_NIC );
3165         return rc;
3166 } // hcf_service_nic
3167
3168
3169 /************************************************************************************************************
3170  ************************** H C F   S U P P O R T   R O U T I N E S ******************************************
3171  ************************************************************************************************************/
3172
3173
3174 /************************************************************************************************************
3175  *
3176  *.SUBMODULE     void calc_mic( hcf_32* p, hcf_32 m )
3177  *.PURPOSE       calculate MIC on a quad byte.
3178  *
3179  *.ARGUMENTS
3180  *   p           address of the MIC
3181  *   m           32 bit value to be processed by the MIC calculation engine
3182  *
3183  *.RETURNS       N.A.
3184  *
3185  *.DESCRIPTION
3186  * calc_mic is the implementation of the MIC algorithm. It is a monkey-see monkey-do copy of
3187  * Michael::appendByte()
3188  * of Appendix C of ..........
3189  *
3190  *
3191  *.DIAGRAM
3192  *
3193  *.NOTICE
3194  *.ENDDOC                END DOCUMENTATION
3195  *
3196  ************************************************************************************************************/
3197
3198 #if (HCF_TYPE) & HCF_TYPE_WPA
3199
3200 #define ROL32( A, n ) ( ((A) << (n)) | ( ((A)>>(32-(n)))  & ( (1UL << (n)) - 1 ) ) )
3201 #define ROR32( A, n ) ROL32( (A), 32-(n) )
3202
3203 #define L   *p
3204 #define R   *(p+1)
3205
3206 void
3207 calc_mic( hcf_32* p, hcf_32 m )
3208 {
3209 #if HCF_BIG_ENDIAN
3210         m = (m >> 16) | (m << 16);
3211 #endif // HCF_BIG_ENDIAN
3212         L ^= m;
3213         R ^= ROL32( L, 17 );
3214         L += R;
3215         R ^= ((L & 0xff00ff00) >> 8) | ((L & 0x00ff00ff) << 8);
3216         L += R;
3217         R ^= ROL32( L, 3 );
3218         L += R;
3219         R ^= ROR32( L, 2 );
3220         L += R;
3221 } // calc_mic
3222 #undef R
3223 #undef L
3224 #endif // HCF_TYPE_WPA
3225
3226
3227
3228 #if (HCF_TYPE) & HCF_TYPE_WPA
3229 /************************************************************************************************************
3230  *
3231  *.SUBMODULE     void calc_mic_rx_frag( IFBP ifbp, wci_bufp p, int len )
3232  *.PURPOSE       calculate MIC on a single fragment.
3233  *
3234  *.ARGUMENTS
3235  *   ifbp        address of the Interface Block
3236  *   bufp        (byte) address of buffer
3237  *   len         length in bytes of buffer specified by bufp
3238  *
3239  *.RETURNS       N.A.
3240  *
3241  *.DESCRIPTION
3242  * calc_mic_rx_frag ........
3243  *
3244  * The MIC is located in the IFB.
3245  * The MIC is separate for Tx and Rx, thus allowing hcf_send_msg to occur between hcf_service_nic and
3246  * hcf_rcv_msg.
3247  *
3248  *
3249  *.DIAGRAM
3250  *
3251  *.NOTICE
3252  *.ENDDOC                END DOCUMENTATION
3253  *
3254  ************************************************************************************************************/
3255 void
3256 calc_mic_rx_frag( IFBP ifbp, wci_bufp p, int len )
3257 {
3258         static union { hcf_32 x32; hcf_16 x16[2]; hcf_8 x8[4]; } x; //* area to accumulate 4 bytes input for MIC engine
3259         int i;
3260
3261         if ( len == -1 ) {                              //initialize MIC housekeeping
3262                 i = *(wci_recordp)&p[HFS_STAT];
3263                 /* i = CNV_SHORTP_TO_LITTLE(&p[HFS_STAT]); should not be neede to prevent alignment poroblems
3264                  * since len == -1 if and only if p is lookahaead buffer which MUST be word aligned
3265                  * to be re-investigated by NvR
3266                  */
3267
3268                 if ( ( i & HFS_STAT_MIC ) == 0 ) {
3269                         ifbp->IFB_MICRxCarry = 0xFFFF;          //suppress MIC calculation
3270                 } else {
3271                         ifbp->IFB_MICRxCarry = 0;
3272 //* Note that "coincidentally" the bit positions used in HFS_STAT
3273 //* correspond with the offset of the key in IFB_MICKey
3274                         i = ( i & HFS_STAT_MIC_KEY_ID ) >> 10;  /* coincidentally no shift needed for i itself */
3275                         ifbp->IFB_MICRx[0] = CNV_LONG_TO_LITTLE(ifbp->IFB_MICRxKey[i  ]);
3276                         ifbp->IFB_MICRx[1] = CNV_LONG_TO_LITTLE(ifbp->IFB_MICRxKey[i+1]);
3277                 }
3278         } else {
3279                 if ( ifbp->IFB_MICRxCarry == 0 ) {
3280                         x.x32 = CNV_LONGP_TO_LITTLE(p);
3281                         p += 4;
3282                         if ( len < 4 ) {
3283                                 ifbp->IFB_MICRxCarry = (hcf_16)len;
3284                         } else {
3285                                 ifbp->IFB_MICRxCarry = 4;
3286                                 len -= 4;
3287                         }
3288                 } else while ( ifbp->IFB_MICRxCarry < 4 && len ) {      //note for hcf_16 applies: 0xFFFF > 4
3289                                 x.x8[ifbp->IFB_MICRxCarry++] = *p++;
3290                                 len--;
3291                         }
3292                 while ( ifbp->IFB_MICRxCarry == 4 ) {   //contrived so we have only 1 call to calc_mic so we could bring it in-line
3293                         calc_mic( ifbp->IFB_MICRx, x.x32 );
3294                         x.x32 = CNV_LONGP_TO_LITTLE(p);
3295                         p += 4;
3296                         if ( len < 4 ) {
3297                                 ifbp->IFB_MICRxCarry = (hcf_16)len;
3298                         }
3299                         len -= 4;
3300                 }
3301         }
3302 } // calc_mic_rx_frag
3303 #endif // HCF_TYPE_WPA
3304
3305
3306 #if (HCF_TYPE) & HCF_TYPE_WPA
3307 /************************************************************************************************************
3308  *
3309  *.SUBMODULE     void calc_mic_tx_frag( IFBP ifbp, wci_bufp p, int len )
3310  *.PURPOSE       calculate MIC on a single fragment.
3311  *
3312  *.ARGUMENTS
3313  *   ifbp        address of the Interface Block
3314  *   bufp        (byte) address of buffer
3315  *   len         length in bytes of buffer specified by bufp
3316  *
3317  *.RETURNS       N.A.
3318  *
3319  *.DESCRIPTION
3320  * calc_mic_tx_frag ........
3321  *
3322  * The MIC is located in the IFB.
3323  * The MIC is separate for Tx and Rx, thus allowing hcf_send_msg to occur between hcf_service_nic and
3324  * hcf_rcv_msg.
3325  *
3326  *
3327  *.DIAGRAM
3328  *
3329  *.NOTICE
3330  *.ENDDOC                END DOCUMENTATION
3331  *
3332  ************************************************************************************************************/
3333 void
3334 calc_mic_tx_frag( IFBP ifbp, wci_bufp p, int len )
3335 {
3336         static union { hcf_32 x32; hcf_16 x16[2]; hcf_8 x8[4]; } x; //* area to accumulate 4 bytes input for MIC engine
3337
3338                                                 //if initialization request
3339         if ( len == -1 ) {
3340                                                 //.  presume MIC calculation disabled
3341                 ifbp->IFB_MICTxCarry = 0xFFFF;
3342                                                 //.  if MIC calculation enabled
3343                 if ( ifbp->IFB_MICTxCntl ) {
3344                                                 //.  .  clear MIC carry
3345                         ifbp->IFB_MICTxCarry = 0;
3346                                                 //.  .  initialize MIC-engine
3347                         ifbp->IFB_MICTx[0] = CNV_LONG_TO_LITTLE(ifbp->IFB_MICTxKey[0]); /*Tx always uses Key 0 */
3348                         ifbp->IFB_MICTx[1] = CNV_LONG_TO_LITTLE(ifbp->IFB_MICTxKey[1]);
3349                 }
3350                                                 //else
3351         } else {
3352                                                 //.  if MIC enabled (Tx) / if MIC present (Rx)
3353                                                 //.  and no carry from previous calc_mic_frag
3354                 if ( ifbp->IFB_MICTxCarry == 0 ) {
3355                                                 //.  .  preset accu with 4 bytes from buffer
3356                         x.x32 = CNV_LONGP_TO_LITTLE(p);
3357                                                 //.  .  adjust pointer accordingly
3358                         p += 4;
3359                                                 //.  .  if buffer contained less then 4 bytes
3360                         if ( len < 4 ) {
3361                                                 //.  .  .  promote valid bytes in accu to carry
3362                                                 //.  .  .  flag accu to contain incomplete double word
3363                                 ifbp->IFB_MICTxCarry = (hcf_16)len;
3364                                                 //.  .  else
3365                         } else {
3366                                                 //.  .  .  flag accu to contain complete double word
3367                                 ifbp->IFB_MICTxCarry = 4;
3368                                                 //.  .  adjust remaining buffer length
3369                                 len -= 4;
3370                         }
3371                                                 //.  else if MIC enabled
3372                                                 //.  and if carry bytes from previous calc_mic_tx_frag
3373                                                 //.  .  move (1-3) bytes from carry into accu
3374                 } else while ( ifbp->IFB_MICTxCarry < 4 && len ) {      /* note for hcf_16 applies: 0xFFFF > 4 */
3375                                 x.x8[ifbp->IFB_MICTxCarry++] = *p++;
3376                                 len--;
3377                         }
3378                                                 //.  while accu contains complete double word
3379                                                 //.  and MIC enabled
3380                 while ( ifbp->IFB_MICTxCarry == 4 ) {
3381                                                 //.  .  pass accu to MIC engine
3382                         calc_mic( ifbp->IFB_MICTx, x.x32 );
3383                                                 //.  .  copy next 4 bytes from buffer to accu
3384                         x.x32 = CNV_LONGP_TO_LITTLE(p);
3385                                                 //.  .  adjust buffer pointer
3386                         p += 4;
3387                                                 //.  .  if buffer contained less then 4 bytes
3388                                                 //.  .  .  promote valid bytes in accu to carry
3389                                                 //.  .  .  flag accu to contain incomplete double word
3390                         if ( len < 4 ) {
3391                                 ifbp->IFB_MICTxCarry = (hcf_16)len;
3392                         }
3393                                                 //.  .  adjust remaining buffer length
3394                         len -= 4;
3395                 }
3396         }
3397 } // calc_mic_tx_frag
3398 #endif // HCF_TYPE_WPA
3399
3400
3401 #if HCF_PROT_TIME
3402 /************************************************************************************************************
3403  *
3404  *.SUBMODULE     void calibrate( IFBP ifbp )
3405  *.PURPOSE       calibrates the S/W protection counter against the Hermes Timer tick.
3406  *
3407  *.ARGUMENTS
3408  *   ifbp        address of the Interface Block
3409  *
3410  *.RETURNS       N.A.
3411  *
3412  *.DESCRIPTION
3413  * calibrates the S/W protection counter against the Hermes Timer tick
3414  * IFB_TickIni is the value used to initialize the S/W protection counter such that the expiration period
3415  * more or less independent of the processor speed. If IFB_TickIni is not yet calibrated, it is done now.
3416  * This calibration is "reasonably" accurate because the Hermes is in a quiet state as a result of the
3417  * Initialize command.
3418  *
3419  *
3420  *.DIAGRAM
3421  *
3422  *1: IFB_TickIni is initialized at INI_TICK_INI by hcf_connect. If calibrate succeeds, IFB_TickIni is
3423  *   guaranteed to be changed. As a consequence there will be only 1 shot at calibration (regardless of the
3424  *   number of init calls) under normal circumstances.
3425  *2: Calibration is done HCF_PROT_TIME_CNT times. This diminish the effects of jitter and interference,
3426  *   especially in a pre-emptive environment. HCF_PROT_TIME_CNT is in the range of 16 through 32 and derived
3427  *   from the HCF_PROT_TIME specified by the MSF programmer. The divisor needed to scale HCF_PROT_TIME into the
3428  *   16-32 range, is used as a multiplicator after the calibration, to scale the found value back to the
3429  *   requested range. This way a compromise is achieved between accuracy and duration of the calibration
3430  *   process.
3431  *3: Acknowledge the Timer Tick Event.
3432  *   Each cycle is limited to at most INI_TICK_INI samples of the TimerTick status of the Hermes.
3433  *   Since the start of calibrate is unrelated to the Hermes Internal Timer, the first interval may last from 0
3434  *   to the normal interval, all subsequent intervals should be the full length of the Hermes Tick interval.
3435  *   The Hermes Timer Tick is not reprogrammed by the HCF, hence it is running at the default of 10 k
3436  *   microseconds.
3437  *4: If the Timer Tick Event is continuously up (prot_cnt still has the value INI_TICK_INI) or no Timer Tick
3438  *   Event occurred before the protection counter expired, reset IFB_TickIni to INI_TICK_INI,
3439  *   set the defunct bit of IFB_CardStat (thus rendering the Hermes inoperable) and exit the calibrate routine.
3440  *8: ifbp->IFB_TickIni is multiplied to scale the found value back to the requested range as explained under 2.
3441  *
3442  *.NOTICE
3443  * o Although there are a number of viewpoints possible, calibrate() uses as error strategy that a single
3444  *   failure of the Hermes TimerTick is considered fatal.
3445  * o There is no hard and concrete time-out value defined for Hermes activities. The default 1 seconds is
3446  *   believed to be sufficiently "relaxed" for real life and to be sufficiently short to be still useful in an
3447  *   environment with humans.
3448  * o Note that via IFB_DefunctStat time outs in cmd_wait and in hcfio_string block all Hermes access till the
3449  *   next init so functions which call a mix of cmd_wait and hcfio_string only need to check the return status
3450  *   of the last call
3451  * o The return code is preset at Time out.
3452  *   The additional complication that no calibrated value for the protection count can be assumed since
3453  *   calibrate() does not yet have determined a calibrated value (a catch 22), is handled by setting the
3454  *   initial value at INI_TICK_INI (by hcf_connect). This approach is considered safe, because:
3455  *     - the HCF does not use the pipeline mechanism of Hermes commands.
3456  *     - the likelihood of failure (the only time when protection count is relevant) is small.
3457  *     - the time will be sufficiently large on a fast machine (busy bit drops on good NIC before counter
3458  *       expires)
3459  *     - the time will be sufficiently small on a slow machine (counter expires on bad NIC before the end user
3460  *       switches the power off in despair
3461  *   The time needed to wrap a 32 bit counter around is longer than many humans want to wait, hence the more or
3462  *   less arbitrary value of 0x40000L is chosen, assuming it does not take too long on an XT and is not too
3463  *   short on a scream-machine.
3464  *
3465  *.ENDDOC                END DOCUMENTATION
3466  *
3467  ************************************************************************************************************/
3468 HCF_STATIC void
3469 calibrate( IFBP ifbp )
3470 {
3471         int     cnt = HCF_PROT_TIME_CNT;
3472         hcf_32  prot_cnt;
3473
3474         HCFTRACE( ifbp, HCF_TRACE_CALIBRATE );
3475         if ( ifbp->IFB_TickIni == INI_TICK_INI ) {                                                  /*1*/
3476                 ifbp->IFB_TickIni = 0;                                                                  /*2*/
3477                 while ( cnt-- ) {
3478                         prot_cnt = INI_TICK_INI;
3479                         OPW( HREG_EV_ACK, HREG_EV_TICK );                                               /*3*/
3480                         while ( (IPW( HREG_EV_STAT ) & HREG_EV_TICK) == 0 && --prot_cnt ) {
3481                                 ifbp->IFB_TickIni++;
3482                         }
3483                         if ( prot_cnt == 0 || prot_cnt == INI_TICK_INI ) {                              /*4*/
3484                                 ifbp->IFB_TickIni = INI_TICK_INI;
3485                                 ifbp->IFB_DefunctStat = HCF_ERR_DEFUNCT_TIMER;
3486                                 ifbp->IFB_CardStat |= CARD_STAT_DEFUNCT;
3487                                 HCFASSERT( DO_ASSERT, prot_cnt );
3488                         }
3489                 }
3490                 ifbp->IFB_TickIni <<= HCF_PROT_TIME_SHFT;                                               /*8*/
3491         }
3492         HCFTRACE( ifbp, HCF_TRACE_CALIBRATE | HCF_TRACE_EXIT );
3493 } // calibrate
3494 #endif // HCF_PROT_TIME
3495
3496
3497 #if (HCF_TYPE) & HCF_TYPE_WPA
3498 /************************************************************************************************************
3499  *
3500  *.SUBMODULE     int check_mic( IFBP ifbp )
3501  *.PURPOSE       verifies the MIC of a received non-USB frame.
3502  *
3503  *.ARGUMENTS
3504  *   ifbp        address of the Interface Block
3505  *
3506  *.RETURNS
3507  *   HCF_SUCCESS
3508  *   HCF_ERR_MIC
3509  *
3510  *.DESCRIPTION
3511  *
3512  *
3513  *.DIAGRAM
3514  *
3515  *4: test whether or not a MIC is reported by the Hermes
3516  *14: the calculated MIC and the received MIC are compared, the return status is set when there is a mismatch
3517  *
3518  *.NOTICE
3519  *.ENDDOC                END DOCUMENTATION
3520  *
3521  ************************************************************************************************************/
3522 int
3523 check_mic( IFBP ifbp )
3524 {
3525         int     rc = HCF_SUCCESS;
3526         hcf_32 x32[2];              //* area to save rcvd 8 bytes MIC
3527
3528                                                             //if MIC present in RxFS
3529         if ( *(wci_recordp)&ifbp->IFB_lap[-HFS_ADDR_DEST] & HFS_STAT_MIC ) {
3530                 //or if ( ifbp->IFB_MICRxCarry != 0xFFFF )
3531                 CALC_RX_MIC( mic_pad, 8 );                  //.  process up to 3 remaining bytes of data and append 5 to 8 bytes of padding to MIC calculation
3532                 get_frag( ifbp, (wci_bufp)x32, 8 BE_PAR(0));//.  get 8 byte MIC from NIC
3533                                                             //.  if calculated and received MIC do not match
3534                                                             //.  .  set status at HCF_ERR_MIC
3535         /*14*/  if ( x32[0] != CNV_LITTLE_TO_LONG(ifbp->IFB_MICRx[0]) ||
3536                      x32[1] != CNV_LITTLE_TO_LONG(ifbp->IFB_MICRx[1])     ) {
3537                         rc = HCF_ERR_MIC;
3538                 }
3539         }
3540                                                             //return status
3541         return rc;
3542 } // check_mic
3543 #endif // HCF_TYPE_WPA
3544
3545
3546 /************************************************************************************************************
3547  *
3548  *.SUBMODULE     int cmd_cmpl( IFBP ifbp )
3549  *.PURPOSE       waits for Hermes Command Completion.
3550  *
3551  *.ARGUMENTS
3552  *   ifbp        address of the Interface Block
3553  *
3554  *.RETURNS
3555  *   IFB_DefunctStat
3556  *   HCF_ERR_TIME_OUT
3557  *   HCF_ERR_DEFUNCT_CMD_SEQ
3558  *   HCF_SUCCESS
3559  *
3560  *.DESCRIPTION
3561  *
3562  *
3563  *.DIAGRAM
3564  *
3565  *2: Once cmd_cmpl is called, the Busy option bit in IFB_Cmd must be cleared
3566  *4: If Status register and command code don't match either:
3567  *    - the Hermes and Host are out of sync ( a fatal error)
3568  *    - error bits are reported via the Status Register.
3569  *   Out of sync is considered fatal and brings the HCF in Defunct mode
3570  *   Errors reported via the Status Register should be caused by sequence violations in Hermes command
3571  *   sequences and hence these bugs should have been found during engineering testing. Since there is no
3572  *   strategy to cope with this problem, it might as well be ignored at run time. Note that for any particular
3573  *   situation where a strategy is formulated to handle the consequences of a particular bug causing a
3574  *   particular Error situation reported via the Status Register, the bug should be removed rather than adding
3575  *   logic to cope with the consequences of the bug.
3576  *   There have been HCF versions where an error report via the Status Register even brought the HCF in defunct
3577  *   mode (although it was not yet named like that at that time). This is particular undesirable behavior for a
3578  *   general library.
3579  *   Simply reporting the error (as "interesting") is debatable. There also have been HCF versions with this
3580  *   strategy using the "vague" HCF_FAILURE code.
3581  *   The error is reported via:
3582  *    - MiscErr tally of the HCF Tally set
3583  *    - the (informative) fields IFB_ErrCmd and IFB_ErrQualifier
3584  *    - the assert mechanism
3585  *8: Here the Defunct case and the Status error are separately treated
3586  *
3587  *
3588  *.ENDDOC                END DOCUMENTATION
3589  *
3590  ************************************************************************************************************/
3591 HCF_STATIC int
3592 cmd_cmpl( IFBP ifbp )
3593 {
3594
3595         PROT_CNT_INI;
3596         int     rc = HCF_SUCCESS;
3597         hcf_16  stat;
3598
3599         HCFLOGENTRY( HCF_TRACE_CMD_CPL, ifbp->IFB_Cmd );
3600         ifbp->IFB_Cmd &= ~HCMD_BUSY;                                                /* 2 */
3601         HCF_WAIT_WHILE( (IPW( HREG_EV_STAT) & HREG_EV_CMD) == 0 );                  /* 4 */
3602         stat = IPW( HREG_STAT );
3603 #if HCF_PROT_TIME
3604         if ( prot_cnt == 0 ) {
3605                 IF_TALLY( ifbp->IFB_HCF_Tallies.MiscErr++ );
3606                 rc = HCF_ERR_TIME_OUT;
3607                 HCFASSERT( DO_ASSERT, ifbp->IFB_Cmd );
3608         } else
3609 #endif // HCF_PROT_TIME
3610         {
3611                 DAWA_ACK( HREG_EV_CMD );
3612         /*4*/   if ( stat != (ifbp->IFB_Cmd & HCMD_CMD_CODE) ) {
3613                 /*8*/   if ( ( (stat ^ ifbp->IFB_Cmd ) & HCMD_CMD_CODE) != 0 ) {
3614                                 rc = ifbp->IFB_DefunctStat = HCF_ERR_DEFUNCT_CMD_SEQ;
3615                                 ifbp->IFB_CardStat |= CARD_STAT_DEFUNCT;
3616                         }
3617                         IF_TALLY( ifbp->IFB_HCF_Tallies.MiscErr++ );
3618                         ifbp->IFB_ErrCmd = stat;
3619                         ifbp->IFB_ErrQualifier = IPW( HREG_RESP_0 );
3620                         HCFASSERT( DO_ASSERT, MERGE_2( IPW( HREG_PARAM_0 ), ifbp->IFB_Cmd ) );
3621                         HCFASSERT( DO_ASSERT, MERGE_2( ifbp->IFB_ErrQualifier, ifbp->IFB_ErrCmd ) );
3622                 }
3623         }
3624         HCFASSERT( rc == HCF_SUCCESS, rc);
3625         HCFLOGEXIT( HCF_TRACE_CMD_CPL );
3626         return rc;
3627 } // cmd_cmpl
3628
3629
3630 /************************************************************************************************************
3631  *
3632  *.SUBMODULE     int cmd_exe( IFBP ifbp, int cmd_code, int par_0 )
3633  *.PURPOSE       Executes synchronous part of Hermes Command and - optionally - waits for Command Completion.
3634  *
3635  *.ARGUMENTS
3636  *   ifbp        address of the Interface Block
3637  *   cmd_code
3638  *   par_0
3639  *
3640  *.RETURNS
3641  *   IFB_DefunctStat
3642  *   HCF_ERR_DEFUNCT_CMD_SEQ
3643  *   HCF_SUCCESS
3644  *   HCF_ERR_TO_BE_ADDED <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3645  *
3646  *.DESCRIPTION
3647  * Executes synchronous Hermes Command and waits for Command Completion
3648  *
3649  * The general HCF strategy is to wait for command completion. As a consequence:
3650  * - the read of the busy bit before writing the command register is superfluous
3651  * - the Hermes requirement that no Inquiry command may be executed if there is still an unacknowledged
3652  *   Inquiry command outstanding, is automatically met.
3653  * The Tx command uses the "Busy" bit in the cmd_code parameter to deviate from this general HCF strategy.
3654  * The idea is that by not busy-waiting on completion of this frequently used command the processor
3655  * utilization is diminished while using the busy-wait on all other seldom used commands the flow is kept
3656  * simple.
3657  *
3658  *
3659  *
3660  *.DIAGRAM
3661  *
3662  *1: skip the body of cmd_exe when in defunct mode or when - based on the S/W Support register write and
3663  *   read back test - there is apparently no NIC.
3664  *   Note: we gave up on the "old" strategy to write the S/W Support register at magic only when needed. Due to
3665  *   the intricateness of Hermes F/W varieties ( which behave differently as far as corruption of the S/W
3666  *   Support register is involved), the increasing number of Hermes commands which do an implicit initialize
3667  *   (thus modifying the S/W Support register) and the workarounds of some OS/Support S/W induced aspects (e.g.
3668  *   the System Soft library at WinNT which postpones the actual mapping of I/O space up to 30 seconds after
3669  *   giving the go-ahead), the "magic" strategy is now reduced to a simple write and read back. This means that
3670  *   problems like a bug tramping over the memory mapped Hermes registers will no longer be noticed as side
3671  *   effect of the S/W Support register check.
3672  *2: check whether the preceding command skipped the busy wait and if so, check for command completion
3673  *
3674  *.NOTICE
3675  *.ENDDOC                END DOCUMENTATION
3676  *
3677  ************************************************************************************************************/
3678
3679 HCF_STATIC int
3680 cmd_exe( IFBP ifbp, hcf_16 cmd_code, hcf_16 par_0 ) //if HCMD_BUSY of cmd_code set, then do NOT wait for completion
3681 {
3682         int rc;
3683
3684         HCFLOGENTRY( HCF_TRACE_CMD_EXE, cmd_code );
3685         HCFASSERT( (cmd_code & HCMD_CMD_CODE) != HCMD_TX || cmd_code & HCMD_BUSY, cmd_code ); //Tx must have Busy bit set
3686         OPW( HREG_SW_0, HCF_MAGIC );
3687         if ( IPW( HREG_SW_0 ) == HCF_MAGIC ) {                                                      /* 1 */
3688                 rc = ifbp->IFB_DefunctStat;
3689         }
3690         else rc = HCF_ERR_NO_NIC;
3691         if ( rc == HCF_SUCCESS ) {
3692                 //;?is this a hot idea, better MEASURE performance impact
3693         /*2*/   if ( ifbp->IFB_Cmd & HCMD_BUSY ) {
3694                         rc = cmd_cmpl( ifbp );
3695                 }
3696                 OPW( HREG_PARAM_0, par_0 );
3697                 OPW( HREG_CMD, cmd_code &~HCMD_BUSY );
3698                 ifbp->IFB_Cmd = cmd_code;
3699                 if ( (cmd_code & HCMD_BUSY) == 0 ) {    //;?is this a hot idea, better MEASURE performance impact
3700                         rc = cmd_cmpl( ifbp );
3701                 }
3702         }
3703         HCFASSERT( rc == HCF_SUCCESS, MERGE_2( rc, cmd_code ) );
3704         HCFLOGEXIT( HCF_TRACE_CMD_EXE );
3705         return rc;
3706 } // cmd_exe
3707
3708
3709 /************************************************************************************************************
3710  *
3711  *.SUBMODULE     int download( IFBP ifbp, CFG_PROG_STRCT FAR *ltvp )
3712  *.PURPOSE       downloads F/W image into NIC and initiates execution of the downloaded F/W.
3713  *
3714  *.ARGUMENTS
3715  *   ifbp        address of the Interface Block
3716  *   ltvp        specifies the pseudo-RID (as defined by WCI)
3717  *
3718  *.RETURNS
3719  *
3720  *.DESCRIPTION
3721  *
3722  *
3723  *.DIAGRAM
3724  *1: First, Ack everything to unblock a (possibly) blocked cmd pipe line
3725  *   Note 1: it is very likely that an Alloc event is pending and very well possible that a (Send) Cmd event is
3726  *   pending
3727  *   Note 2: it is assumed that this strategy takes away the need to ack every conceivable event after an
3728  *   Hermes Initialize
3729  *
3730  *
3731  *.ENDDOC                END DOCUMENTATION
3732  *
3733  ************************************************************************************************************/
3734 HCF_STATIC int
3735 download( IFBP ifbp, CFG_PROG_STRCT FAR *ltvp )                     //Hermes-II download (volatile only)
3736 {
3737         hcf_16              i;
3738         int                 rc = HCF_SUCCESS;
3739         wci_bufp            cp;
3740         hcf_io              io_port = ifbp->IFB_IOBase + HREG_AUX_DATA;
3741
3742         HCFLOGENTRY( HCF_TRACE_DL, ltvp->typ );
3743 #if (HCF_TYPE) & HCF_TYPE_PRELOADED
3744         HCFASSERT( DO_ASSERT, ltvp->mode );
3745 #else
3746                                                 //if initial "program" LTV
3747         if ( ifbp->IFB_DLMode == CFG_PROG_STOP && ltvp->mode == CFG_PROG_VOLATILE) {
3748                                                 //.  switch Hermes to initial mode
3749         /*1*/   OPW( HREG_EV_ACK, ~HREG_EV_SLEEP_REQ );
3750                 rc = cmd_exe( ifbp, HCMD_INI, 0 );  /* HCMD_INI can not be part of init() because that is called on
3751                                                      * other occasions as well */
3752                 rc = init( ifbp );
3753         }
3754                                                 //if final "program" LTV
3755         if ( ltvp->mode == CFG_PROG_STOP && ifbp->IFB_DLMode == CFG_PROG_VOLATILE) {
3756                                                 //.  start tertiary (or secondary)
3757                 OPW( HREG_PARAM_1, (hcf_16)(ltvp->nic_addr >> 16) );
3758                 rc = cmd_exe( ifbp, HCMD_EXECUTE, (hcf_16) ltvp->nic_addr );
3759                 if (rc == HCF_SUCCESS) {
3760                         rc = init( ifbp );  /*;? do we really want to skip init if cmd_exe failed, i.e.
3761                                              *   IFB_FW_Comp_Id is than possibly incorrect */
3762                 }
3763                                                 //else (non-final)
3764         } else {
3765                                                 //.  if mode == Readback SEEPROM
3766 #if 0   //;? as long as the next if contains a hard coded 0, might as well leave it out even more obvious
3767                 if ( 0 /*len is definitely not want we want;?*/ && ltvp->mode == CFG_PROG_SEEPROM_READBACK ) {
3768                         OPW( HREG_PARAM_1, (hcf_16)(ltvp->nic_addr >> 16) );
3769                         OPW( HREG_PARAM_2, MUL_BY_2(ltvp->len - 4));
3770                                                 //.  .  perform Hermes prog cmd with appropriate mode bits
3771                         rc = cmd_exe( ifbp, HCMD_PROGRAM | ltvp->mode, (hcf_16)ltvp->nic_addr );
3772                                                 //.  .  set up NIC RAM addressability according Resp0-1
3773                         OPW( HREG_AUX_PAGE,   IPW( HREG_RESP_1) );
3774                         OPW( HREG_AUX_OFFSET, IPW( HREG_RESP_0) );
3775                                                 //.  .  set up L-field of LTV according Resp2
3776                         i = ( IPW( HREG_RESP_2 ) + 1 ) / 2;  // i contains max buffer size in words, a probably not very useful piece of information ;?
3777 /*Nico's code based on i is the "real amount of data available"
3778                         if ( ltvp->len - 4 < i ) rc = HCF_ERR_LEN;
3779                         else ltvp->len = i + 4;
3780 */
3781 /* Rolands code based on the idea that a MSF should not ask for more than is available
3782                         // check if number of bytes requested exceeds max buffer size
3783                         if ( ltvp->len - 4 > i ) {
3784                                 rc = HCF_ERR_LEN;
3785                                 ltvp->len = i + 4;
3786                         }
3787 */
3788                                                 //.  .  copy data from NIC via AUX port to LTV
3789                         cp = (wci_bufp)ltvp->host_addr;                     /*IN_PORT_STRING_8_16 macro may modify its parameters*/
3790                         i = ltvp->len - 4;
3791                         IN_PORT_STRING_8_16( io_port, cp, i );      //!!!WORD length, cp MUST be a char pointer // $$ char
3792                                                 //.  else (non-final programming)
3793                 } else
3794 #endif //;? as long as the above if contains a hard coded 0, might as well leave it out even more obvious
3795                 {                               //.  .  get number of words to program
3796                         HCFASSERT( ltvp->segment_size, *ltvp->host_addr );
3797                         i = ltvp->segment_size/2;
3798                                                 //.  .  copy data (words) from LTV via AUX port to NIC
3799                         cp = (wci_bufp)ltvp->host_addr;                     //OUT_PORT_STRING_8_16 macro may modify its parameters
3800                                                 //.  .  if mode == volatile programming
3801                         if ( ltvp->mode == CFG_PROG_VOLATILE ) {
3802                                                 //.  .  .  set up NIC RAM addressability via AUX port
3803                                 OPW( HREG_AUX_PAGE, (hcf_16)(ltvp->nic_addr >> 16 << 9 | (ltvp->nic_addr & 0xFFFF) >> 7 ) );
3804                                 OPW( HREG_AUX_OFFSET, (hcf_16)(ltvp->nic_addr & 0x007E) );
3805                                 OUT_PORT_STRING_8_16( io_port, cp, i );     //!!!WORD length, cp MUST be a char pointer
3806                         }
3807                 }
3808         }
3809         ifbp->IFB_DLMode = ltvp->mode;                  //save state in IFB_DLMode
3810 #endif // HCF_TYPE_PRELOADED
3811         HCFASSERT( rc == HCF_SUCCESS, rc );
3812         HCFLOGEXIT( HCF_TRACE_DL );
3813         return rc;
3814 } // download
3815
3816
3817 #if (HCF_ASSERT) & HCF_ASSERT_PRINTF
3818 /**************************************************
3819  * Certain Hermes-II firmware versions can generate
3820  * debug information. This debug information is
3821  * contained in a buffer in nic-RAM, and can be read
3822  * via the aux port.
3823  **************************************************/
3824 HCF_STATIC int
3825 fw_printf(IFBP ifbp, CFG_FW_PRINTF_STRCT FAR *ltvp)
3826 {
3827         int rc = HCF_SUCCESS;
3828         hcf_16 fw_cnt;
3829 //      hcf_32 DbMsgBuffer = 0x29D2, DbMsgCount= 0x000029D0;
3830 //      hcf_16 DbMsgSize=0x00000080;
3831         hcf_32 DbMsgBuffer;
3832         CFG_FW_PRINTF_BUFFER_LOCATION_STRCT *p = &ifbp->IFB_FwPfBuff;
3833         ltvp->len = 1;
3834         if ( p->DbMsgSize != 0 ) {
3835                 // first, check the counter in nic-RAM and compare it to the latest counter value of the HCF
3836                 OPW( HREG_AUX_PAGE, (hcf_16)(p->DbMsgCount >> 7) );
3837                 OPW( HREG_AUX_OFFSET, (hcf_16)(p->DbMsgCount & 0x7E) );
3838                 fw_cnt = ((IPW( HREG_AUX_DATA) >>1 ) & ((hcf_16)p->DbMsgSize - 1));
3839                 if ( fw_cnt != ifbp->IFB_DbgPrintF_Cnt ) {
3840 //                      DbgPrint("fw_cnt=%d IFB_DbgPrintF_Cnt=%d\n", fw_cnt, ifbp->IFB_DbgPrintF_Cnt);
3841                         DbMsgBuffer = p->DbMsgBuffer + ifbp->IFB_DbgPrintF_Cnt * 6; // each entry is 3 words
3842                         OPW( HREG_AUX_PAGE, (hcf_16)(DbMsgBuffer >> 7) );
3843                         OPW( HREG_AUX_OFFSET, (hcf_16)(DbMsgBuffer & 0x7E) );
3844                         ltvp->msg_id     = IPW(HREG_AUX_DATA);
3845                         ltvp->msg_par    = IPW(HREG_AUX_DATA);
3846                         ltvp->msg_tstamp = IPW(HREG_AUX_DATA);
3847                         ltvp->len = 4;
3848                         ifbp->IFB_DbgPrintF_Cnt++;
3849                         ifbp->IFB_DbgPrintF_Cnt &= (p->DbMsgSize - 1);
3850                 }
3851         }
3852         return rc;
3853 };
3854 #endif // HCF_ASSERT_PRINTF
3855
3856
3857 /************************************************************************************************************
3858  *
3859  *.SUBMODULE     hcf_16 get_fid( IFBP ifbp )
3860  *.PURPOSE       get allocated FID for either transmit or notify.
3861  *
3862  *.ARGUMENTS
3863  *   ifbp        address of the Interface Block
3864  *
3865  *.RETURNS
3866  *   0   no FID available
3867  *   <>0 FID number
3868  *
3869  *.DESCRIPTION
3870  *
3871  *
3872  *.DIAGRAM
3873  *   The preference is to use a "pending" alloc. If no alloc is pending, then - if available - the "spare" FID
3874  *   is used.
3875  *   If the spare FID is used, IFB_RscInd (representing the spare FID) must be cleared
3876  *   If the pending alloc is used, the alloc event must be acknowledged to the Hermes.
3877  *   In case the spare FID was depleted and the IFB_RscInd has been "faked" as pseudo resource with a 0x0001
3878  *   value by hcf_service_nic, IFB_RscInd has to be "corrected" again to its 0x0000 value.
3879  *
3880  *   Note that due to the Hermes-II H/W problems which are intended to be worked around by DAWA, the Alloc bit
3881  *   in the Event register is no longer a reliable indication of the presence/absence of a FID. The "Clear FID"
3882  *   part of the DAWA logic, together with the choice of the definition of the return information from get_fid,
3883  *   handle this automatically, i.e. without additional code in get_fid.
3884  *.ENDDOC                END DOCUMENTATION
3885  *
3886  ************************************************************************************************************/
3887 HCF_STATIC hcf_16
3888 get_fid( IFBP ifbp )
3889 {
3890
3891         hcf_16 fid = 0;
3892 #if ( (HCF_TYPE) & HCF_TYPE_HII5 ) == 0
3893         PROT_CNT_INI;
3894 #endif // HCF_TYPE_HII5
3895
3896         IF_DMA( HCFASSERT(!(ifbp->IFB_CntlOpt & USE_DMA), ifbp->IFB_CntlOpt) );
3897
3898         if ( IPW( HREG_EV_STAT) & HREG_EV_ALLOC) {
3899                 fid = IPW( HREG_ALLOC_FID );
3900                 HCFASSERT( fid, ifbp->IFB_RscInd );
3901                 DAWA_ZERO_FID( HREG_ALLOC_FID );
3902 #if ( (HCF_TYPE) & HCF_TYPE_HII5 ) == 0
3903                 HCF_WAIT_WHILE( ( IPW( HREG_EV_STAT ) & HREG_EV_ACK_REG_READY ) == 0 );
3904                 HCFASSERT( prot_cnt, IPW( HREG_EV_STAT ) );
3905 #endif // HCF_TYPE_HII5
3906                 DAWA_ACK( HREG_EV_ALLOC );          //!!note that HREG_EV_ALLOC is written only once
3907 // 180 degree error in logic ;? #if ALLOC_15
3908                 if ( ifbp->IFB_RscInd == 1 ) {
3909                         ifbp->IFB_RscInd = 0;
3910                 }
3911 //#endif // ALLOC_15
3912         } else {
3913 // 180 degree error in logic ;? #if ALLOC_15
3914                 fid = ifbp->IFB_RscInd;
3915 //#endif // ALLOC_15
3916                 ifbp->IFB_RscInd = 0;
3917         }
3918         return fid;
3919 } // get_fid
3920
3921
3922 /************************************************************************************************************
3923  *
3924  *.SUBMODULE     void get_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) )
3925  *.PURPOSE       reads with 16/32 bit I/O via BAP1 port from NIC RAM to Host memory.
3926  *
3927  *.ARGUMENTS
3928  *   ifbp        address of the Interface Block
3929  *   bufp        (byte) address of buffer
3930  *   len         length in bytes of buffer specified by bufp
3931  *   word_len    Big Endian only: number of leading bytes to swap in pairs
3932  *
3933  *.RETURNS       N.A.
3934  *
3935  *.DESCRIPTION
3936  * process the single byte (if applicable) read by the previous get_frag and copy len (or len-1) bytes from
3937  * NIC to bufp.
3938  * On a Big Endian platform, the parameter word_len controls the number of leading bytes whose endianess is
3939  * converted (i.e. byte swapped)
3940  *
3941  *
3942  *.DIAGRAM
3943  *10: The PCMCIA card can be removed in the middle of the transfer. By depositing a "magic number" in the
3944  *   HREG_SW_0 register of the Hermes at initialization time and by verifying this register, it can be
3945  *   determined whether the card is still present. The return status is set accordingly.
3946  *   Clearing the buffer is a (relative) cheap way to prevent that failing I/O results in run-away behavior
3947  *   because the garbage in the buffer is interpreted by the caller irrespective of the return status (e.g.
3948  *   hcf_service_nic has this behavior).
3949  *
3950  *.NOTICE
3951  *   It turns out DOS ODI uses zero length fragments. The HCF code can cope with it, but as a consequence, no
3952  *   Assert on len is possible
3953  *
3954  *.ENDDOC                END DOCUMENTATION
3955  *
3956  ************************************************************************************************************/
3957 HCF_STATIC void
3958 get_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) )
3959 {
3960         hcf_io      io_port = ifbp->IFB_IOBase + HREG_DATA_1;   //BAP data register
3961         wci_bufp    p = bufp;                                   //working pointer
3962         int         i;                                          //prevent side effects from macro
3963         int         j;
3964
3965         HCFASSERT( ((hcf_32)bufp & (HCF_ALIGN-1) ) == 0, (hcf_32)bufp );
3966
3967 /*1:    here recovery logic for intervening BAP access between hcf_service_nic and hcf_rcv_msg COULD be added
3968  *  if current access is RxInitial
3969  *  .  persistent_offset += len
3970  */
3971
3972         i = len;
3973                                                 //if buffer length > 0 and carry from previous get_frag
3974         if ( i && ifbp->IFB_CarryIn ) {
3975                                                 //.  move carry to buffer
3976                                                 //.  adjust buffer length and pointer accordingly
3977                 *p++ = (hcf_8)(ifbp->IFB_CarryIn>>8);
3978                 i--;
3979                                                 //.  clear carry flag
3980                 ifbp->IFB_CarryIn = 0;
3981         }
3982 #if (HCF_IO) & HCF_IO_32BITS
3983         //skip zero-length I/O, single byte I/O and I/O not worthwhile (i.e. less than 6 bytes)for DW logic
3984                                                 //if buffer length >= 6 and 32 bits I/O support
3985         if ( !(ifbp->IFB_CntlOpt & USE_16BIT) && i >= 6 ) {
3986                 hcf_32 FAR  *p4; //prevent side effects from macro
3987                 if ( ( (hcf_32)p & 0x1 ) == 0 ) {           //.  if buffer at least word aligned
3988                         if ( (hcf_32)p & 0x2 ) {            //.  .  if buffer not double word aligned
3989                                                             //.  .  .  read single word to get double word aligned
3990                                 *(wci_recordp)p = IN_PORT_WORD( io_port );
3991                                                             //.  .  .  adjust buffer length and pointer accordingly
3992                                 p += 2;
3993                                 i -= 2;
3994                         }
3995                                                             //.  .  read as many double word as possible
3996                         p4 = (hcf_32 FAR *)p;
3997                         j = i/4;
3998                         IN_PORT_STRING_32( io_port, p4, j );
3999                                                             //.  .  adjust buffer length and pointer accordingly
4000                         p += i & ~0x0003;
4001                         i &= 0x0003;
4002                 }
4003         }
4004 #endif // HCF_IO_32BITS
4005                                                 //if no 32-bit support OR byte aligned OR 1-3 bytes left
4006         if ( i ) {
4007                                                 //.  read as many word as possible in "alignment safe" way
4008                 j = i/2;
4009                 IN_PORT_STRING_8_16( io_port, p, j );
4010                                                 //.  if 1 byte left
4011                 if ( i & 0x0001 ) {
4012                                                 //.  .  read 1 word
4013                         ifbp->IFB_CarryIn = IN_PORT_WORD( io_port );
4014                                                 //.  .  store LSB in last char of buffer
4015                         bufp[len-1] = (hcf_8)ifbp->IFB_CarryIn;
4016                                                 //.  .  save MSB in carry, set carry flag
4017                         ifbp->IFB_CarryIn |= 0x1;
4018                 }
4019         }
4020 #if HCF_BIG_ENDIAN
4021         HCFASSERT( word_len == 0 || word_len == 2 || word_len == 4, word_len );
4022         HCFASSERT( word_len == 0 || ((hcf_32)bufp & 1 ) == 0, (hcf_32)bufp );
4023         HCFASSERT( word_len <= len, MERGE2( word_len, len ) );
4024         //see put_frag for an alternative implementation, but be careful about what are int's and what are
4025         //hcf_16's
4026         if ( word_len ) {                               //.  if there is anything to convert
4027                 hcf_8 c;
4028                 c = bufp[1];                                //.  .  convert the 1st hcf_16
4029                 bufp[1] = bufp[0];
4030                 bufp[0] = c;
4031                 if ( word_len > 1 ) {                       //.  .  if there is to convert more than 1 word ( i.e 2 )
4032                         c = bufp[3];                            //.  .  .  convert the 2nd hcf_16
4033                         bufp[3] = bufp[2];
4034                         bufp[2] = c;
4035                 }
4036         }
4037 #endif // HCF_BIG_ENDIAN
4038 } // get_frag
4039
4040 /************************************************************************************************************
4041  *
4042  *.SUBMODULE     int init( IFBP ifbp )
4043  *.PURPOSE       Handles common initialization aspects (H-I init, calibration, config.mngmt, allocation).
4044  *
4045  *.ARGUMENTS
4046  *   ifbp        address of the Interface Block
4047  *
4048  *.RETURNS
4049  *   HCF_ERR_INCOMP_PRI
4050  *   HCF_ERR_INCOMP_FW
4051  *   HCF_ERR_TIME_OUT
4052  *   >>hcf_get_info
4053  *       HCF_ERR_NO_NIC
4054  *       HCF_ERR_LEN
4055  *
4056  *.DESCRIPTION
4057  * init will successively:
4058  * - in case of a (non-preloaded) H-I, initialize the NIC
4059  * - calibrate the S/W protection timer against the Hermes Timer
4060  * - collect HSI, "active" F/W Configuration Management Information
4061  * - in case active F/W is Primary F/W: collect Primary F/W Configuration Management Information
4062  * - check HSI and Primary F/W compatibility with the HCF
4063  * - in case active F/W is Station or AP F/W: check Station or AP F/W compatibility with the HCF
4064  * - in case active F/W is not Primary F/W: allocate FIDs to be used in transmit/notify process
4065  *
4066  *
4067  *.DIAGRAM
4068  *2: drop all error status bits in IFB_CardStat since they are expected to be re-evaluated.
4069  *4: Ack everything except HREG_EV_SLEEP_REQ. It is very likely that an Alloc event is pending and
4070  *   very well possible that a Send Cmd event is pending. Acking HREG_EV_SLEEP_REQ is handled by hcf_action(
4071  *   HCF_ACT_INT_ON ) !!!
4072  *10: Calibrate the S/W time-out protection mechanism by calling calibrate(). Note that possible errors
4073  *   in the calibration process are nor reported by init but will show up via the defunct mechanism in
4074  *   subsequent hcf-calls.
4075  *14: usb_check_comp() is called to have the minimal visual clutter for the legacy H-I USB dongle
4076  *   compatibility check.
4077  *16: The following configuration management related information is retrieved from the NIC:
4078  *    - HSI supplier
4079  *    - F/W Identity
4080  *    - F/W supplier
4081  *    if appropriate:
4082  *    - PRI Identity
4083  *    - PRI supplier
4084  *    appropriate means on H-I: always
4085  *    and on H-II if F/W supplier reflects a primary (i.e. only after an Hermes Reset or Init
4086  *    command).
4087  *    QUESTION ;? !!!!!! should, For each of the above RIDs the Endianess is converted to native Endianess.
4088  *    Only the return code of the first hcf_get_info is used. All hcf_get_info calls are made, regardless of
4089  *    the success or failure of the 1st hcf_get_info. The assumptions are:
4090  *     - if any call fails, they all fail, so remembering the result of the 1st call is adequate
4091  *     - a failing call will overwrite the L-field with a 0x0000 value, which services both as an
4092  *       error indication for the values cached in the IFB as making mmd_check_comp fail.
4093  *    In case of H-I, when getting the F/W identity fails, the F/W is assumed to be H-I AP F/W pre-dating
4094  *    version 9.0 and the F/W Identity and Supplier are faked accordingly.
4095  *    In case of H-II, the Primary, Station and AP Identity are merged into a single F/W Identity.
4096  *    The same applies to the Supplier information. As a consequence the PRI information can no longer be
4097  *    retrieved when a Tertiary runs. To accommodate MSFs and Utilities who depend on PRI information being
4098  *    available at any time, this information is cached in the IFB. In this cache the generic "F/W" value of
4099  *    the typ-fields is overwritten with the specific (legacy) "PRI" values. To actually re-route the (legacy)
4100  *    PRI request via hcf_get_info, the xxxx-table must be set.  In case of H-I, this caching, modifying and
4101  *    re-routing is not needed because PRI information is always available directly from the NIC. For
4102  *    consistency the caching fields in the IFB are filled with the PRI information anyway.
4103  *18: mdd_check_comp() is called to check the Supplier Variant and Range of the Host-S/W I/F (HSI) and the
4104  *   Primary Firmware Variant and Range against the Top and Bottom level supported by this HCF.  If either of
4105  *   these tests fails, the CARD_STAT_INCOMP_PRI bit of IFB_CardStat is set
4106  *   Note: There should always be a primary except during production, so this makes the HCF in its current form
4107  *   unsuitable for manufacturing test systems like the FTS. This can be remedied by an adding a test like
4108  *   ifbp->IFB_PRISup.id == COMP_ID_PRI
4109  *20: In case there is Tertiary F/W and this F/W is Station F/W, the Supplier Variant and Range of the Station
4110  *   Firmware function as retrieved from the Hermes is checked against the Top and Bottom level supported by
4111  *   this HCF.
4112  *   Note: ;? the tertiary F/W compatibility checks could be moved to the DHF, which already has checked the
4113  *   CFI and MFI compatibility of the image with the NIC before the image was downloaded.
4114  *28: In case of non-Primary F/W: allocates and acknowledge a (TX or Notify) FID and allocates without
4115  *   acknowledge another (TX or Notify) FID (the so-called 1.5 alloc scheme) with the following steps:
4116  *   - execute the allocate command by calling cmd_exe
4117  *   - wait till either the alloc event or a time-out occurs
4118  *   - regardless whether the alloc event occurs, call get_fid to
4119  *     - read the FID and save it in IFB_RscInd to be used as "spare FID"
4120  *     - acknowledge the alloc event
4121  *     - do another "half" allocate to complete the "1.5 Alloc scheme"
4122  *     Note that above 3 steps do not harm and thus give the "cheapest" acceptable strategy.
4123  *     If a time-out occurred, then report time out status (after all)
4124  *
4125  *.ENDDOC                END DOCUMENTATION
4126  *
4127  ************************************************************************************************************/
4128 HCF_STATIC int
4129 init( IFBP ifbp )
4130 {
4131
4132         int rc = HCF_SUCCESS;
4133
4134         HCFLOGENTRY( HCF_TRACE_INIT, 0 );
4135
4136         ifbp->IFB_CardStat = 0;                                                                         /* 2*/
4137         OPW( HREG_EV_ACK, ~HREG_EV_SLEEP_REQ );                                             /* 4*/
4138         IF_PROT_TIME( calibrate( ifbp ) );                                                  /*10*/
4139 #if 0 // OOR
4140         ifbp->IFB_FWIdentity.len = 2;                           //misuse the IFB space for a put
4141         ifbp->IFB_FWIdentity.typ = CFG_TICK_TIME;
4142         ifbp->IFB_FWIdentity.comp_id = (1000*1000)/1024 + 1;    //roughly 1 second
4143         hcf_put_info( ifbp, (LTVP)&ifbp->IFB_FWIdentity.len );
4144 #endif // OOR
4145         ifbp->IFB_FWIdentity.len = sizeof(CFG_FW_IDENTITY_STRCT)/sizeof(hcf_16) - 1;
4146         ifbp->IFB_FWIdentity.typ = CFG_FW_IDENTITY;
4147         rc = hcf_get_info( ifbp, (LTVP)&ifbp->IFB_FWIdentity.len );
4148 /* ;? conversion should not be needed for mmd_check_comp */
4149 #if HCF_BIG_ENDIAN
4150         ifbp->IFB_FWIdentity.comp_id       = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWIdentity.comp_id );
4151         ifbp->IFB_FWIdentity.variant       = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWIdentity.variant );
4152         ifbp->IFB_FWIdentity.version_major = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWIdentity.version_major );
4153         ifbp->IFB_FWIdentity.version_minor = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWIdentity.version_minor );
4154 #endif // HCF_BIG_ENDIAN
4155 #if defined MSF_COMPONENT_ID                                                                        /*14*/
4156         if ( rc == HCF_SUCCESS ) {                                                                      /*16*/
4157                 ifbp->IFB_HSISup.len = sizeof(CFG_SUP_RANGE_STRCT)/sizeof(hcf_16) - 1;
4158                 ifbp->IFB_HSISup.typ = CFG_NIC_HSI_SUP_RANGE;
4159                 rc = hcf_get_info( ifbp, (LTVP)&ifbp->IFB_HSISup.len );
4160 /* ;? conversion should not be needed for mmd_check_comp , BUT according to a report of a BE-user it is
4161  * should be resolved in the WARP release
4162  * since some compilers make ugly but unnecessary code of these instructions even for LE,
4163  * it is conditionally compiled */
4164 #if HCF_BIG_ENDIAN
4165                 ifbp->IFB_HSISup.role    = CNV_LITTLE_TO_SHORT( ifbp->IFB_HSISup.role );
4166                 ifbp->IFB_HSISup.id      = CNV_LITTLE_TO_SHORT( ifbp->IFB_HSISup.id );
4167                 ifbp->IFB_HSISup.variant = CNV_LITTLE_TO_SHORT( ifbp->IFB_HSISup.variant );
4168                 ifbp->IFB_HSISup.bottom  = CNV_LITTLE_TO_SHORT( ifbp->IFB_HSISup.bottom );
4169                 ifbp->IFB_HSISup.top     = CNV_LITTLE_TO_SHORT( ifbp->IFB_HSISup.top );
4170 #endif // HCF_BIG_ENDIAN
4171                 ifbp->IFB_FWSup.len = sizeof(CFG_SUP_RANGE_STRCT)/sizeof(hcf_16) - 1;
4172                 ifbp->IFB_FWSup.typ = CFG_FW_SUP_RANGE;
4173                 (void)hcf_get_info( ifbp, (LTVP)&ifbp->IFB_FWSup.len );
4174 /* ;? conversion should not be needed for mmd_check_comp */
4175 #if HCF_BIG_ENDIAN
4176                 ifbp->IFB_FWSup.role    = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWSup.role );
4177                 ifbp->IFB_FWSup.id      = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWSup.id );
4178                 ifbp->IFB_FWSup.variant = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWSup.variant );
4179                 ifbp->IFB_FWSup.bottom  = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWSup.bottom );
4180                 ifbp->IFB_FWSup.top     = CNV_LITTLE_TO_SHORT( ifbp->IFB_FWSup.top );
4181 #endif // HCF_BIG_ENDIAN
4182
4183                 if ( ifbp->IFB_FWSup.id == COMP_ID_PRI ) {                                              /* 20*/
4184                         int i = sizeof( CFG_FW_IDENTITY_STRCT) + sizeof(CFG_SUP_RANGE_STRCT );
4185                         while ( i-- ) ((hcf_8*)(&ifbp->IFB_PRIIdentity))[i] = ((hcf_8*)(&ifbp->IFB_FWIdentity))[i];
4186                         ifbp->IFB_PRIIdentity.typ = CFG_PRI_IDENTITY;
4187                         ifbp->IFB_PRISup.typ = CFG_PRI_SUP_RANGE;
4188                         xxxx[xxxx_PRI_IDENTITY_OFFSET] = &ifbp->IFB_PRIIdentity.len;
4189                         xxxx[xxxx_PRI_IDENTITY_OFFSET+1] = &ifbp->IFB_PRISup.len;
4190                 }
4191                 if ( !mmd_check_comp( (void*)&cfg_drv_act_ranges_hsi, &ifbp->IFB_HSISup)                 /* 22*/
4192 #if ( (HCF_TYPE) & HCF_TYPE_PRELOADED ) == 0
4193 //;? the PRI compatibility check is only relevant for DHF
4194                      || !mmd_check_comp( (void*)&cfg_drv_act_ranges_pri, &ifbp->IFB_PRISup)
4195 #endif // HCF_TYPE_PRELOADED
4196                         ) {
4197                         ifbp->IFB_CardStat = CARD_STAT_INCOMP_PRI;
4198                         rc = HCF_ERR_INCOMP_PRI;
4199                 }
4200                 if ( ( ifbp->IFB_FWSup.id == COMP_ID_STA && !mmd_check_comp( (void*)&cfg_drv_act_ranges_sta, &ifbp->IFB_FWSup) ) ||
4201                      ( ifbp->IFB_FWSup.id == COMP_ID_APF && !mmd_check_comp( (void*)&cfg_drv_act_ranges_apf, &ifbp->IFB_FWSup) )
4202                         ) {                                                                                  /* 24 */
4203                         ifbp->IFB_CardStat |= CARD_STAT_INCOMP_FW;
4204                         rc = HCF_ERR_INCOMP_FW;
4205                 }
4206         }
4207 #endif // MSF_COMPONENT_ID
4208
4209         if ( rc == HCF_SUCCESS && ifbp->IFB_FWIdentity.comp_id >= COMP_ID_FW_STA ) {
4210                 PROT_CNT_INI;
4211                 /**************************************************************************************
4212                  * rlav: the DMA engine needs the host to cause a 'hanging alloc event' for it to consume.
4213                  * not sure if this is the right spot in the HCF, thinking about hcf_enable...
4214                  **************************************************************************************/
4215                 rc = cmd_exe( ifbp, HCMD_ALLOC, 0 );
4216 // 180 degree error in logic ;? #if ALLOC_15
4217 //              ifbp->IFB_RscInd = 1;   //let's hope that by the time hcf_send_msg isa called, there will be a FID
4218 //#else
4219                 if ( rc == HCF_SUCCESS ) {
4220                         HCF_WAIT_WHILE( (IPW( HREG_EV_STAT ) & HREG_EV_ALLOC) == 0 );
4221                         IF_PROT_TIME( HCFASSERT(prot_cnt, IPW( HREG_EV_STAT )) );
4222 #if HCF_DMA
4223                         if ( ! ( ifbp->IFB_CntlOpt & USE_DMA ) )
4224 #endif // HCF_DMA
4225                         {
4226                                 ifbp->IFB_RscInd = get_fid( ifbp );
4227                                 HCFASSERT( ifbp->IFB_RscInd, 0 );
4228                                 cmd_exe( ifbp, HCMD_ALLOC, 0 );
4229                                 IF_PROT_TIME( if ( prot_cnt == 0 ) rc = HCF_ERR_TIME_OUT );
4230                         }
4231                 }
4232 //#endif // ALLOC_15
4233         }
4234
4235         HCFASSERT( rc == HCF_SUCCESS, rc );
4236         HCFLOGEXIT( HCF_TRACE_INIT );
4237         return rc;
4238 } // init
4239
4240 /************************************************************************************************************
4241  *
4242  *.SUBMODULE     void isr_info( IFBP ifbp )
4243  *.PURPOSE       handles link events.
4244  *
4245  *.ARGUMENTS
4246  *   ifbp        address of the Interface Block
4247  *
4248  *.RETURNS       N.A.
4249  *
4250  *.DESCRIPTION
4251  *
4252  *
4253  *.DIAGRAM
4254  *1: First the FID number corresponding with the InfoEvent is determined.
4255  *   Note the complication of the zero-FID protection sub-scheme in DAWA.
4256  *   Next the L-field and the T-field are fetched into scratch buffer info.
4257  *2: In case of tallies, the 16 bits Hermes values are accumulated in the IFB into 32 bits values. Info[0]
4258  *   is (expected to be) HCF_NIC_TAL_CNT + 1. The contraption "while ( info[0]-- >1 )" rather than
4259  *   "while ( --info[0] )" is used because it is dangerous to determine the length of the Value field by
4260  *   decrementing info[0]. As a result of a bug in some version of the F/W, info[0] may be 0, resulting
4261  *   in a very long loop in the pre-decrement logic.
4262  *4: In case of a link status frame, the information is copied to the IFB field IFB_linkStat
4263  *6: All other than Tallies (including "unknown" ones) are checked against the selection set by the MSF
4264  *   via CFG_RID_LOG. If a match is found or the selection set has the wild-card type (i.e non-NULL buffer
4265  *   pointer at the terminating zero-type), the frame is copied to the (type-specific) log buffer.
4266  *   Note that to accumulate tallies into IFB AND to log them or to log a frame when a specific match occures
4267  *   AND based on the wild-card selection, you have to call setup_bap again after the 1st copy.
4268  *
4269  *.ENDDOC                END DOCUMENTATION
4270  *
4271  ************************************************************************************************************/
4272 HCF_STATIC void
4273 isr_info( IFBP ifbp )
4274 {
4275         hcf_16  info[2], fid;
4276 #if (HCF_EXT) & HCF_EXT_INFO_LOG
4277         RID_LOGP    ridp = ifbp->IFB_RIDLogp;   //NULL or pointer to array of RID_LOG structures (terminated by zero typ)
4278 #endif // HCF_EXT_INFO_LOG
4279
4280         HCFTRACE( ifbp, HCF_TRACE_ISR_INFO );                                                               /* 1 */
4281         fid = IPW( HREG_INFO_FID );
4282         DAWA_ZERO_FID( HREG_INFO_FID );
4283         if ( fid ) {
4284                 (void)setup_bap( ifbp, fid, 0, IO_IN );
4285                 get_frag( ifbp, (wci_bufp)info, 4 BE_PAR(2) );
4286                 HCFASSERT( info[0] <= HCF_MAX_LTV + 1, MERGE_2( info[1], info[0] ) );  //;? a smaller value makes more sense
4287 #if (HCF_TALLIES) & HCF_TALLIES_NIC     //Hermes tally support
4288                 if ( info[1] == CFG_TALLIES ) {
4289                         hcf_32  *p;
4290                 /*2*/   if ( info[0] > HCF_NIC_TAL_CNT ) {
4291                                 info[0] = HCF_NIC_TAL_CNT + 1;
4292                         }
4293                         p = (hcf_32*)&ifbp->IFB_NIC_Tallies;
4294                         while ( info[0]-- >1 ) *p++ += IPW( HREG_DATA_1 );  //request may return zero length
4295                 }
4296                 else
4297 #endif // HCF_TALLIES_NIC
4298                 {
4299                 /*4*/   if ( info[1] == CFG_LINK_STAT ) {
4300                                 ifbp->IFB_LinkStat = IPW( HREG_DATA_1 );
4301                         }
4302 #if (HCF_EXT) & HCF_EXT_INFO_LOG
4303                 /*6*/   while ( 1 ) {
4304                                 if ( ridp->typ == 0 || ridp->typ == info[1] ) {
4305                                         if ( ridp->bufp ) {
4306                                                 HCFASSERT( ridp->len >= 2, ridp->typ );
4307                                                 ridp->bufp[0] = min((hcf_16)(ridp->len - 1), info[0] );     //save L
4308                                                 ridp->bufp[1] = info[1];                        //save T
4309                                                 get_frag( ifbp, (wci_bufp)&ridp->bufp[2], (ridp->bufp[0] - 1)*2 BE_PAR(0) );
4310                                         }
4311                                         break;
4312                                 }
4313                                 ridp++;
4314                         }
4315 #endif // HCF_EXT_INFO_LOG
4316                 }
4317                 HCFTRACE( ifbp, HCF_TRACE_ISR_INFO | HCF_TRACE_EXIT );
4318         }
4319         return;
4320 } // isr_info
4321
4322 //
4323 //
4324 // #endif // HCF_TALLIES_NIC
4325 // /*4*/    if ( info[1] == CFG_LINK_STAT ) {
4326 //          ifbp->IFB_DSLinkStat = IPW( HREG_DATA_1 ) | CFG_LINK_STAT_CHANGE;   //corrupts BAP !! ;?
4327 //          ifbp->IFB_LinkStat = ifbp->IFB_DSLinkStat & CFG_LINK_STAT_FW; //;? to be obsoleted
4328 //          printk( "<4>linkstatus: %04x\n", ifbp->IFB_DSLinkStat );        //;?remove me 1 day
4329 // #if (HCF_SLEEP) & HCF_DDS
4330 //          if ( ( ifbp->IFB_DSLinkStat & CFG_LINK_STAT_CONNECTED ) == 0 ) {    //even values are disconnected etc.
4331 //              ifbp->IFB_TickCnt = 0;              //start 2 second period (with 1 tick uncertanty)
4332 //              printk( "<5>isr_info: AwaitConnection phase started, IFB_TickCnt = 0\n" );      //;?remove me 1 day
4333 //          }
4334 // #endif // HCF_DDS
4335 //      }
4336 // #if (HCF_EXT) & HCF_EXT_INFO_LOG
4337 // /*6*/    while ( 1 ) {
4338 //          if ( ridp->typ == 0 || ridp->typ == info[1] ) {
4339 //              if ( ridp->bufp ) {
4340 //                  HCFASSERT( ridp->len >= 2, ridp->typ );
4341 //                  (void)setup_bap( ifbp, fid, 2, IO_IN );         //restore BAP for tallies, linkstat and specific type followed by wild card
4342 //                  ridp->bufp[0] = min( ridp->len - 1, info[0] );  //save L
4343 //                  get_frag( ifbp, (wci_bufp)&ridp->bufp[1], ridp->bufp[0]*2 BE_PAR(0) );
4344 //              }
4345 //              break; //;?this break is no longer needed due to setup_bap but lets concentrate on DDS first
4346 //          }
4347 //          ridp++;
4348 //      }
4349 // #endif // HCF_EXT_INFO_LOG
4350 //  }
4351 //  HCFTRACE( ifbp, HCF_TRACE_ISR_INFO | HCF_TRACE_EXIT );
4352 //
4353 //
4354 //
4355 //
4356 //  return;
4357 //} // isr_info
4358
4359
4360 /************************************************************************************************************
4361  *
4362  *.SUBMODULE     void mdd_assert( IFBP ifbp, unsigned int line_number, hcf_32 q )
4363  *.PURPOSE       filters assert on level and interfaces to the MSF supplied msf_assert routine.
4364  *
4365  *.ARGUMENTS
4366  *   ifbp        address of the Interface Block
4367  *   line_number line number of the line which caused the assert
4368  *   q           qualifier, additional information which may give a clue about the problem
4369  *
4370  *.RETURNS       N.A.
4371  *
4372  *.DESCRIPTION
4373  *
4374  *
4375  *.DIAGRAM
4376  *
4377  *.NOTICE
4378  * mdd_assert has been through a turmoil, renaming hcf_assert to assert and hcf_assert again and supporting off
4379  * and on being called from the MSF level and other ( immature ) ModularDriverDevelopment modules like DHF and
4380  * MMD.
4381  * !!!! The assert routine is not an hcf_..... routine in the sense that it may be called by the MSF,
4382  *      however it is called from mmd.c and dhf.c, so it must be external.
4383  *      To prevent namespace pollution it needs a prefix, to prevent that MSF programmers think that
4384  *      they are allowed to call the assert logic, the prefix HCF can't be used, so MDD is selected!!!!
4385  *
4386  * When called from the DHF module the line number is incremented by DHF_FILE_NAME_OFFSET and when called from
4387  * the MMD module by MMD_FILE_NAME_OFFSET.
4388  *
4389  *.ENDDOC                END DOCUMENTATION
4390  *
4391  ************************************************************************************************************/
4392 #if HCF_ASSERT
4393 void
4394 mdd_assert( IFBP ifbp, unsigned int line_number, hcf_32 q )
4395 {
4396         hcf_16  run_time_flag = ifbp->IFB_AssertLvl;
4397
4398         if ( run_time_flag /* > ;?????? */ ) { //prevent recursive behavior, later to be extended to level filtering
4399                 ifbp->IFB_AssertQualifier = q;
4400                 ifbp->IFB_AssertLine = (hcf_16)line_number;
4401 #if (HCF_ASSERT) & ( HCF_ASSERT_LNK_MSF_RTN | HCF_ASSERT_RT_MSF_RTN )
4402                 if ( ifbp->IFB_AssertRtn ) {
4403                         ifbp->IFB_AssertRtn( line_number, ifbp->IFB_AssertTrace, q );
4404                 }
4405 #endif // HCF_ASSERT_LNK_MSF_RTN / HCF_ASSERT_RT_MSF_RTN
4406 #if (HCF_ASSERT) & HCF_ASSERT_SW_SUP
4407                 OPW( HREG_SW_2, line_number );
4408                 OPW( HREG_SW_2, ifbp->IFB_AssertTrace );
4409                 OPW( HREG_SW_2, (hcf_16)q );
4410                 OPW( HREG_SW_2, (hcf_16)(q >> 16 ) );
4411 #endif // HCF_ASSERT_SW_SUP
4412
4413 #if (HCF_EXT) & HCF_EXT_MB && (HCF_ASSERT) & HCF_ASSERT_MB
4414                 ifbp->IFB_AssertLvl = 0;                                    // prevent recursive behavior
4415                 hcf_put_info( ifbp, (LTVP)&ifbp->IFB_AssertStrct );
4416                 ifbp->IFB_AssertLvl = run_time_flag;                        // restore appropriate filter level
4417 #endif // HCF_EXT_MB / HCF_ASSERT_MB
4418         }
4419 } // mdd_assert
4420 #endif // HCF_ASSERT
4421
4422
4423 /************************************************************************************************************
4424  *
4425  *.SUBMODULE     void put_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) )
4426  *.PURPOSE       writes with 16/32 bit I/O via BAP1 port from Host memory to NIC RAM.
4427  *
4428  *.ARGUMENTS
4429  *   ifbp        address of the Interface Block
4430  *   bufp        (byte) address of buffer
4431  *   len         length in bytes of buffer specified by bufp
4432  *   word_len    Big Endian only: number of leading bytes to swap in pairs
4433  *
4434  *.RETURNS       N.A.
4435  *
4436  *.DESCRIPTION
4437  * process the single byte (if applicable) not yet written by the previous put_frag and copy len
4438  * (or len-1) bytes from bufp to NIC.
4439  *
4440  *
4441  *.DIAGRAM
4442  *
4443  *.NOTICE
4444  *   It turns out DOS ODI uses zero length fragments. The HCF code can cope with it, but as a consequence, no
4445  *   Assert on len is possible
4446  *
4447  *.ENDDOC                END DOCUMENTATION
4448  *
4449  ************************************************************************************************************/
4450 HCF_STATIC void
4451 put_frag( IFBP ifbp, wci_bufp bufp, int len BE_PAR( int word_len ) )
4452 {
4453         hcf_io      io_port = ifbp->IFB_IOBase + HREG_DATA_1;   //BAP data register
4454         int         i;                                          //prevent side effects from macro
4455         hcf_16      j;
4456         HCFASSERT( ((hcf_32)bufp & (HCF_ALIGN-1) ) == 0, (hcf_32)bufp );
4457 #if HCF_BIG_ENDIAN
4458         HCFASSERT( word_len == 0 || word_len == 2 || word_len == 4, word_len );
4459         HCFASSERT( word_len == 0 || ((hcf_32)bufp & 1 ) == 0, (hcf_32)bufp );
4460         HCFASSERT( word_len <= len, MERGE_2( word_len, len ) );
4461
4462         if ( word_len ) {                                   //if there is anything to convert
4463                                                 //.  convert and write the 1st hcf_16
4464                 j = bufp[1] | bufp[0]<<8;
4465                 OUT_PORT_WORD( io_port, j );
4466                                                 //.  update pointer and counter accordingly
4467                 len -= 2;
4468                 bufp += 2;
4469                 if ( word_len > 1 ) {           //.  if there is to convert more than 1 word ( i.e 2 )
4470                                                 //.  .  convert and write the 2nd hcf_16
4471                         j = bufp[1] | bufp[0]<<8;   /*bufp is already incremented by 2*/
4472                         OUT_PORT_WORD( io_port, j );
4473                                                 //.  .  update pointer and counter accordingly
4474                         len -= 2;
4475                         bufp += 2;
4476                 }
4477         }
4478 #endif // HCF_BIG_ENDIAN
4479         i = len;
4480         if ( i && ifbp->IFB_CarryOut ) {                    //skip zero-length
4481                 j = ((*bufp)<<8) + ( ifbp->IFB_CarryOut & 0xFF );
4482                 OUT_PORT_WORD( io_port, j );
4483                 bufp++; i--;
4484                 ifbp->IFB_CarryOut = 0;
4485         }
4486 #if (HCF_IO) & HCF_IO_32BITS
4487         //skip zero-length I/O, single byte I/O and I/O not worthwhile (i.e. less than 6 bytes)for DW logic
4488                                                                 //if buffer length >= 6 and 32 bits I/O support
4489         if ( !(ifbp->IFB_CntlOpt & USE_16BIT) && i >= 6 ) {
4490                 hcf_32 FAR  *p4; //prevent side effects from macro
4491                 if ( ( (hcf_32)bufp & 0x1 ) == 0 ) {            //.  if buffer at least word aligned
4492                         if ( (hcf_32)bufp & 0x2 ) {             //.  .  if buffer not double word aligned
4493                                                                 //.  .  .  write a single word to get double word aligned
4494                                 j = *(wci_recordp)bufp;     //just to help ease writing macros with embedded assembly
4495                                 OUT_PORT_WORD( io_port, j );
4496                                                                 //.  .  .  adjust buffer length and pointer accordingly
4497                                 bufp += 2; i -= 2;
4498                         }
4499                                                                 //.  .  write as many double word as possible
4500                         p4 = (hcf_32 FAR *)bufp;
4501                         j = (hcf_16)i/4;
4502                         OUT_PORT_STRING_32( io_port, p4, j );
4503                                                                 //.  .  adjust buffer length and pointer accordingly
4504                         bufp += i & ~0x0003;
4505                         i &= 0x0003;
4506                 }
4507         }
4508 #endif // HCF_IO_32BITS
4509                                                 //if no 32-bit support OR byte aligned OR 1 word left
4510         if ( i ) {
4511                                                 //.  if odd number of bytes left
4512                 if ( i & 0x0001 ) {
4513                                                 //.  .  save left over byte (before bufp is corrupted) in carry, set carry flag
4514                         ifbp->IFB_CarryOut = (hcf_16)bufp[i-1] | 0x0100;    //note that i and bufp are always simultaneously modified, &bufp[i-1] is invariant
4515                 }
4516                                                 //.  write as many word as possible in "alignment safe" way
4517                 j = (hcf_16)i/2;
4518                 OUT_PORT_STRING_8_16( io_port, bufp, j );
4519         }
4520 } // put_frag
4521
4522
4523 /************************************************************************************************************
4524  *
4525  *.SUBMODULE     void put_frag_finalize( IFBP ifbp )
4526  *.PURPOSE       cleanup after put_frag for trailing odd byte and MIC transfer to NIC.
4527  *
4528  *.ARGUMENTS
4529  *   ifbp        address of the Interface Block
4530  *
4531  *.RETURNS       N.A.
4532  *
4533  *.DESCRIPTION
4534  * finalize the MIC calculation with the padding pattern, output the last byte (if applicable)
4535  * of the message and the MIC to the TxFS
4536  *
4537  *
4538  *.DIAGRAM
4539  *2: 1 byte of the last put_frag may be still in IFB_CarryOut ( the put_frag carry holder ), so ........
4540  *   1 - 3 bytes of the last put_frag may be still in IFB_tx_32 ( the MIC engine carry holder ), so ........
4541  *   The call to the MIC calculation routine feeds these remaining bytes (if any) of put_frag and the
4542  *   just as many bytes of the padding as needed to the MIC calculation engine. Note that the "unneeded" pad
4543  *   bytes simply end up in the MIC engine carry holder and are never used.
4544  *8: write the remainder of the MIC and possible some garbage to NIC RAM
4545  *   Note: i is always 4 (a loop-invariant of the while in point 2)
4546  *
4547  *.NOTICE
4548  *
4549  *.ENDDOC                END DOCUMENTATION
4550  *
4551  ************************************************************************************************************/
4552 HCF_STATIC void
4553 put_frag_finalize( IFBP ifbp )
4554 {
4555 #if (HCF_TYPE) & HCF_TYPE_WPA
4556         if ( ifbp->IFB_MICTxCarry != 0xFFFF) {      //if MIC calculation active
4557                 CALC_TX_MIC( mic_pad, 8);               //.  feed (up to 8 bytes of) virtual padding to MIC engine
4558                                                         //.  write (possibly) trailing byte + (most of) MIC
4559                 put_frag( ifbp, (wci_bufp)ifbp->IFB_MICTx, 8 BE_PAR(0) );
4560         }
4561 #endif // HCF_TYPE_WPA
4562         put_frag( ifbp, null_addr, 1 BE_PAR(0) );   //write (possibly) trailing data or MIC byte
4563 } // put_frag_finalize
4564
4565
4566 /************************************************************************************************************
4567  *
4568  *.SUBMODULE     int put_info( IFBP ifbp, LTVP ltvp )
4569  *.PURPOSE       support routine to handle the "basic" task of hcf_put_info to pass RIDs to the NIC.
4570  *
4571  *.ARGUMENTS
4572  *   ifbp        address of the Interface Block
4573  *   ltvp        address in NIC RAM where LVT-records are located
4574  *
4575  *.RETURNS
4576  *   HCF_SUCCESS
4577  *   >>put_frag
4578  *   >>cmd_wait
4579  *
4580  *.DESCRIPTION
4581  *
4582  *
4583  *.DIAGRAM
4584  *20: do not write RIDs to NICs which have incompatible Firmware
4585  *24: If the RID does not exist, the L-field is set to zero.
4586  *   Note that some RIDs can not be read, e.g. the pseudo RIDs for direct Hermes commands and CFG_DEFAULT_KEYS
4587  *28: If the RID is written successful, pass it to the NIC by means of an Access Write command
4588  *
4589  *.NOTICE
4590  *   The mechanism to HCF_ASSERT on invalid typ-codes in the LTV record is based on the following strategy:
4591  *     - some codes (e.g. CFG_REG_MB) are explicitly handled by the HCF which implies that these codes
4592  *       are valid. These codes are already consumed by hcf_put_info.
4593  *     - all other codes are passed to the Hermes. Before the put action is executed, hcf_get_info is called
4594  *       with an LTV record with a value of 1 in the L-field and the intended put action type in the Typ-code
4595  *       field. If the put action type is valid, it is also valid as a get action type code - except
4596  *       for CFG_DEFAULT_KEYS and CFG_ADD_TKIP_DEFAULT_KEY - so the HCF_ASSERT logic of hcf_get_info should
4597  *       not catch.
4598  *
4599  *.ENDDOC                END DOCUMENTATION
4600  *
4601  ************************************************************************************************************/
4602 HCF_STATIC int
4603 put_info( IFBP ifbp, LTVP ltvp  )
4604 {
4605
4606         int rc = HCF_SUCCESS;
4607
4608         HCFASSERT( ifbp->IFB_CardStat == 0, MERGE_2( ltvp->typ, ifbp->IFB_CardStat ) );
4609         HCFASSERT( CFG_RID_CFG_MIN <= ltvp->typ && ltvp->typ <= CFG_RID_CFG_MAX, ltvp->typ );
4610
4611         if ( ifbp->IFB_CardStat == 0 &&                                                             /* 20*/
4612              ( ( CFG_RID_CFG_MIN <= ltvp->typ    && ltvp->typ <= CFG_RID_CFG_MAX ) ||
4613                ( CFG_RID_ENG_MIN <= ltvp->typ /* && ltvp->typ <= 0xFFFF */       )     ) ) {
4614 #if HCF_ASSERT //FCC8, FCB0, FCB4, FCB6, FCB7, FCB8, FCC0, FCC4, FCBC, FCBD, FCBE, FCBF
4615                 {
4616                         hcf_16     t = ltvp->typ;
4617                         LTV_STRCT  x = { 2, t, {0} };                                                          /*24*/
4618                         hcf_get_info( ifbp, (LTVP)&x );
4619                         if ( x.len == 0 &&
4620                              ( t != CFG_DEFAULT_KEYS && t != CFG_ADD_TKIP_DEFAULT_KEY && t != CFG_REMOVE_TKIP_DEFAULT_KEY &&
4621                                t != CFG_ADD_TKIP_MAPPED_KEY && t != CFG_REMOVE_TKIP_MAPPED_KEY &&
4622                                t != CFG_HANDOVER_ADDR && t != CFG_DISASSOCIATE_ADDR &&
4623                                t != CFG_FCBC && t != CFG_FCBD && t != CFG_FCBE && t != CFG_FCBF &&
4624                                t != CFG_DEAUTHENTICATE_ADDR
4625                                      )
4626                                 ) {
4627                                 HCFASSERT( DO_ASSERT, ltvp->typ );
4628                         }
4629                 }
4630 #endif // HCF_ASSERT
4631
4632                 rc = setup_bap( ifbp, ltvp->typ, 0, IO_OUT );
4633                 put_frag( ifbp, (wci_bufp)ltvp, 2*ltvp->len + 2 BE_PAR(2) );
4634         /*28*/  if ( rc == HCF_SUCCESS ) {
4635                         rc = cmd_exe( ifbp, HCMD_ACCESS + HCMD_ACCESS_WRITE, ltvp->typ );
4636                 }
4637         }
4638         return rc;
4639 } // put_info
4640
4641
4642 /************************************************************************************************************
4643  *
4644  *.SUBMODULE     int put_info_mb( IFBP ifbp, CFG_MB_INFO_STRCT FAR * ltvp )
4645  *.PURPOSE       accumulates a ( series of) buffers into a single Info block into the MailBox.
4646  *
4647  *.ARGUMENTS
4648  *   ifbp        address of the Interface Block
4649  *   ltvp        address of structure specifying the "type" and the fragments of the information to be synthesized
4650  *               as an LTV into the MailBox
4651  *
4652  *.RETURNS
4653  *
4654  *.DESCRIPTION
4655  * If the data does not fit (including no MailBox is available), the IFB_MBTally is incremented and an
4656  * error status is returned.
4657  * HCF_ASSERT does not catch.
4658  * Calling put_info_mb when their is no MailBox available, is considered a design error in the MSF.
4659  *
4660  * Note that there is always at least 1 word of unused space in the mail box.
4661  * As a consequence:
4662  * - no problem in pointer arithmetic (MB_RP == MB_WP means unambiguously mail box is completely empty
4663  * - There is always free space to write an L field with a value of zero after each MB_Info block.  This
4664  *   allows for an easy scan mechanism in the "get MB_Info block" logic.
4665  *
4666  *
4667  *.DIAGRAM
4668  *1: Calculate L field of the MBIB, i.e. 1 for the T-field + the cumulative length of the fragments.
4669  *2: The free space in the MailBox is calculated (2a: free part from Write Ptr to Read Ptr, 2b: free part
4670  *   turns out to wrap around) . If this space suffices to store the number of words reflected by len (T-field
4671  *   + Value-field) plus the additional MailBox Info L-field + a trailing 0 to act as the L-field of a trailing
4672  *   dummy or empty LTV record, then a MailBox Info block is build in the MailBox consisting of
4673  *     - the value len in the first word
4674  *     - type in the second word
4675  *     - a copy of the contents of the fragments in the second and higher word
4676  *
4677  *4: Since put_info_mb() can more or less directly be called from the MSF level, the I/F must be robust
4678  *   against out-of-range variables. As failsafe coding, the MB update is skipped by changing tlen to 0 if
4679  *   len == 0; This will indirectly cause an assert as result of the violation of the next if clause.
4680  *6: Check whether the free space in MailBox suffices (this covers the complete absence of the MailBox).
4681  *   Note that len is unsigned, so even MSF I/F violation works out O.K.
4682  *   The '2' in the expression "len+2" is used because 1 word is needed for L itself and 1 word is needed
4683  *   for the zero-sentinel
4684  *8: update MailBox Info length report to MSF with "oldest" MB Info Block size. Be careful here, if you get
4685  *   here before the MailBox is registered, you can't read from the buffer addressed by IFB_MBp (it is the
4686  *   Null buffer) so don't move this code till the end of this routine but keep it where there is garuanteed
4687  *   a buffer.
4688  *
4689  *.NOTICE
4690  *   boundary testing depends on the fact that IFB_MBSize is guaranteed to be zero if no MailBox is present,
4691  *   and to a lesser degree, that IFB_MBWp = IFB_MBRp = 0
4692  *
4693  *.ENDDOC                END DOCUMENTATION
4694  *
4695  ************************************************************************************************************/
4696 #if (HCF_EXT) & HCF_EXT_MB
4697
4698 HCF_STATIC int
4699 put_info_mb( IFBP ifbp, CFG_MB_INFO_STRCT FAR * ltvp )
4700 {
4701
4702         int         rc = HCF_SUCCESS;
4703         hcf_16      i;                      //work counter
4704         hcf_16      *dp;                    //destination pointer (in MailBox)
4705         wci_recordp sp;                     //source pointer
4706         hcf_16      len;                    //total length to copy to MailBox
4707         hcf_16      tlen;                   //free length/working length/offset in WMP frame
4708
4709         if ( ifbp->IFB_MBp == NULL ) return rc;  //;?not sufficient
4710         HCFASSERT( ifbp->IFB_MBp != NULL, 0 );                   //!!!be careful, don't get into an endless recursion
4711         HCFASSERT( ifbp->IFB_MBSize, 0 );
4712
4713         len = 1;                                                                                            /* 1 */
4714         for ( i = 0; i < ltvp->frag_cnt; i++ ) {
4715                 len += ltvp->frag_buf[i].frag_len;
4716         }
4717         if ( ifbp->IFB_MBRp > ifbp->IFB_MBWp ) {
4718                 tlen = ifbp->IFB_MBRp - ifbp->IFB_MBWp;                                                         /* 2a*/
4719         } else {
4720                 if ( ifbp->IFB_MBRp == ifbp->IFB_MBWp ) {
4721                         ifbp->IFB_MBRp = ifbp->IFB_MBWp = 0;    // optimize Wrapping
4722                 }
4723                 tlen = ifbp->IFB_MBSize - ifbp->IFB_MBWp;                                                       /* 2b*/
4724                 if ( ( tlen <= len + 2 ) && ( len + 2 < ifbp->IFB_MBRp ) ) {    //if trailing space is too small but
4725                                                                                 //   leading space is sufficiently large
4726                         ifbp->IFB_MBp[ifbp->IFB_MBWp] = 0xFFFF;                 //flag dummy LTV to fill the trailing space
4727                         ifbp->IFB_MBWp = 0;                                     //reset WritePointer to begin of MailBox
4728                         tlen = ifbp->IFB_MBRp;                                  //get new available space size
4729                 }
4730         }
4731         dp = &ifbp->IFB_MBp[ifbp->IFB_MBWp];
4732         if ( len == 0 ) {
4733                 tlen = 0; //;? what is this good for
4734         }
4735         if ( len + 2 >= tlen ){                                                                             /* 6 */
4736                 //Do Not ASSERT, this is a normal condition
4737                 IF_TALLY( ifbp->IFB_HCF_Tallies.NoBufMB++ );
4738                 rc = HCF_ERR_LEN;
4739         } else {
4740                 *dp++ = len;                                    //write Len (= size of T+V in words to MB_Info block
4741                 *dp++ = ltvp->base_typ;                         //write Type to MB_Info block
4742                 ifbp->IFB_MBWp += len + 1;                      //update WritePointer of MailBox
4743                 for ( i = 0; i < ltvp->frag_cnt; i++ ) {                // process each of the fragments
4744                         sp = ltvp->frag_buf[i].frag_addr;
4745                         len = ltvp->frag_buf[i].frag_len;
4746                         while ( len-- ) *dp++ = *sp++;
4747                 }
4748                 ifbp->IFB_MBp[ifbp->IFB_MBWp] = 0;              //to assure get_info for CFG_MB_INFO stops
4749                 ifbp->IFB_MBInfoLen = ifbp->IFB_MBp[ifbp->IFB_MBRp];                                            /* 8 */
4750         }
4751         return rc;
4752 } // put_info_mb
4753
4754 #endif // HCF_EXT_MB
4755
4756
4757 /************************************************************************************************************
4758  *
4759  *.SUBMODULE     int setup_bap( IFBP ifbp, hcf_16 fid, int offset, int type )
4760  *.PURPOSE       set up data access to NIC RAM via BAP_1.
4761  *
4762  *.ARGUMENTS
4763  *   ifbp            address of I/F Block
4764  *   fid             FID/RID
4765  *   offset          !!even!! offset in FID/RID
4766  *   type            IO_IN, IO_OUT
4767  *
4768  *.RETURNS
4769  *   HCF_SUCCESS                 O.K
4770  *   HCF_ERR_NO_NIC              card is removed
4771  *   HCF_ERR_DEFUNCT_TIME_OUT    Fatal malfunction detected
4772  *   HCF_ERR_DEFUNCT_.....       if and only if IFB_DefunctStat <> 0
4773  *
4774  *.DESCRIPTION
4775  *
4776  * A non-zero return status indicates:
4777  * - the NIC is considered nonoperational, e.g. due to a time-out of some Hermes activity in the past
4778  * - BAP_1 could not properly be initialized
4779  * - the card is removed before completion of the data transfer
4780  * In all other cases, a zero is returned.
4781  * BAP Initialization failure indicates an H/W error which is very likely to signal complete H/W failure.
4782  * Once a BAP Initialization failure has occurred all subsequent interactions with the Hermes will return a
4783  * "defunct" status till the Hermes is re-initialized by means of an hcf_connect.
4784  *
4785  * A BAP is a set of registers (Offset, Select and Data) offering read/write access to a particular FID or
4786  * RID. This access is based on a auto-increment feature.
4787  * There are two BAPs but these days the HCF uses only BAP_1 and leaves BAP_0 to the PCI Busmastering H/W.
4788  *
4789  * The BAP-mechanism is based on the Busy bit in the Offset register (see the Hermes definition). The waiting
4790  * for Busy must occur between writing the Offset register and accessing the Data register. The
4791  * implementation to wait for the Busy bit drop after each write to the Offset register, implies that the
4792  * requirement that the Busy bit is low  before the Select register is written, is automatically met.
4793  * BAP-setup may be time consuming (e.g. 380 usec for large offsets occurs frequently). The wait for Busy bit
4794  * drop is protected by a loop counter, which is initialized with IFB_TickIni, which is calibrated in init.
4795  *
4796  * The NIC I/F is optimized for word transfer and can only handle word transfer at a word boundary in NIC
4797  * RAM. The intended solution for transfer of a single byte has multiple H/W flaws. There have been different
4798  * S/W Workaround strategies. RID access is hcf_16 based by "nature", so no byte access problems.  For Tx/Rx
4799  * FID access,  the byte logic became obsolete by absorbing it in the double word oriented nature of the MIC
4800  * feature.
4801  *
4802  *
4803  *.DIAGRAM
4804  *
4805  *2: the test on rc checks whether the HCF went into "defunct" mode ( e.g. BAP initialization or a call to
4806  *   cmd_wait did ever fail).
4807  *4: the select register and offset register are set
4808  *   the offset register is monitored till a successful condition (no busy bit) is detected or till the
4809  *   (calibrated) protection counter expires
4810  *   If the counter expires, this is reflected in IFB_DefunctStat, so all subsequent calls to setup_bap fail
4811  *   immediately ( see 2)
4812  *6: initialization of the carry as used by pet/get_frag
4813  *8: HREG_OFFSET_ERR is ignored as error because:
4814  *    a: the Hermes is robust against it
4815  *    b: it is not known what causes it (probably a bug), hence no strategy can be specified which level is
4816  *       to handle this error in which way. In the past, it could be induced by the MSF level, e.g. by calling
4817  *       hcf_rcv_msg while there was no Rx-FID available. Since this is an MSF-error which is caught by ASSERT,
4818  *       there is no run-time action required by the HCF.
4819  *   Lumping the Offset error in with the Busy bit error, as has been done in the past turns out to be a
4820  *   disaster or a life saver, just depending on what the cause of the error is. Since no prediction can be
4821  *   done about the future, it is "felt" to be the best strategy to ignore this error. One day the code was
4822  *   accompanied by the following comment:
4823  *   //  ignore HREG_OFFSET_ERR, someone, supposedly the MSF programmer ;) made a bug. Since we don't know
4824  *   //  what is going on, we might as well go on - under management pressure - by ignoring it
4825  *
4826  *.ENDDOC                          END DOCUMENTATION
4827  *
4828  ************************************************************************************************************/
4829 HCF_STATIC int
4830 setup_bap( IFBP ifbp, hcf_16 fid, int offset, int type )
4831 {
4832         PROT_CNT_INI;
4833         int rc;
4834
4835         HCFTRACE( ifbp, HCF_TRACE_STRIO );
4836         rc = ifbp->IFB_DefunctStat;
4837         if (rc == HCF_SUCCESS) {                                        /*2*/
4838                 OPW( HREG_SELECT_1, fid );                                                              /*4*/
4839                 OPW( HREG_OFFSET_1, offset );
4840                 if ( type == IO_IN ) {
4841                         ifbp->IFB_CarryIn = 0;
4842                 }
4843                 else ifbp->IFB_CarryOut = 0;
4844                 HCF_WAIT_WHILE( IPW( HREG_OFFSET_1) & HCMD_BUSY );
4845                 HCFASSERT( !( IPW( HREG_OFFSET_1) & HREG_OFFSET_ERR ), MERGE_2( fid, offset ) );         /*8*/
4846                 if ( prot_cnt == 0 ) {
4847                         HCFASSERT( DO_ASSERT, MERGE_2( fid, offset ) );
4848                         rc = ifbp->IFB_DefunctStat = HCF_ERR_DEFUNCT_TIME_OUT;
4849                         ifbp->IFB_CardStat |= CARD_STAT_DEFUNCT;
4850                 }
4851         }
4852         HCFTRACE( ifbp, HCF_TRACE_STRIO | HCF_TRACE_EXIT );
4853         return rc;
4854 } // setup_bap
4855