plip: replace spin_lock_irq with spin_lock_irqsave in irq context
[linux-2.6] / drivers / net / tokenring / smctr.c
1 /*
2  *  smctr.c: A network driver for the SMC Token Ring Adapters.
3  *
4  *  Written by Jay Schulist <jschlst@samba.org>
5  *
6  *  This software may be used and distributed according to the terms
7  *  of the GNU General Public License, incorporated herein by reference.
8  *
9  *  This device driver works with the following SMC adapters:
10  *      - SMC TokenCard Elite   (8115T, chips 825/584)
11  *      - SMC TokenCard Elite/A MCA (8115T/A, chips 825/594)
12  *
13  *  Source(s):
14  *      - SMC TokenCard SDK.
15  *
16  *  Maintainer(s):
17  *    JS        Jay Schulist <jschlst@samba.org>
18  *
19  * Changes:
20  *    07102000          JS      Fixed a timing problem in smctr_wait_cmd();
21  *                              Also added a bit more discriptive error msgs.
22  *    07122000          JS      Fixed problem with detecting a card with
23  *                              module io/irq/mem specified.
24  *
25  *  To do:
26  *    1. Multicast support.
27  *
28  *  Initial 2.5 cleanup Alan Cox <alan@redhat.com>  2002/10/28
29  */
30
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/fcntl.h>
35 #include <linux/interrupt.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
38 #include <linux/in.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/time.h>
42 #include <linux/errno.h>
43 #include <linux/init.h>
44 #include <linux/mca-legacy.h>
45 #include <linux/delay.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/trdevice.h>
50 #include <linux/bitops.h>
51
52 #include <asm/system.h>
53 #include <asm/io.h>
54 #include <asm/dma.h>
55 #include <asm/irq.h>
56
57 #if BITS_PER_LONG == 64
58 #error FIXME: driver does not support 64-bit platforms
59 #endif
60
61 #include "smctr.h"               /* Our Stuff */
62 #include "smctr_firmware.h"      /* SMC adapter firmware */
63
64 static char version[] __initdata = KERN_INFO "smctr.c: v1.4 7/12/00 by jschlst@samba.org\n";
65 static const char cardname[] = "smctr";
66
67
68 #define SMCTR_IO_EXTENT   20
69
70 #ifdef CONFIG_MCA_LEGACY
71 static unsigned int smctr_posid = 0x6ec6;
72 #endif
73
74 static int ringspeed;
75
76 /* SMC Name of the Adapter. */
77 static char smctr_name[] = "SMC TokenCard";
78 static char *smctr_model = "Unknown";
79
80 /* Use 0 for production, 1 for verification, 2 for debug, and
81  * 3 for very verbose debug.
82  */
83 #ifndef SMCTR_DEBUG
84 #define SMCTR_DEBUG 1
85 #endif
86 static unsigned int smctr_debug = SMCTR_DEBUG;
87
88 /* smctr.c prototypes and functions are arranged alphabeticly 
89  * for clearity, maintainability and pure old fashion fun. 
90  */
91 /* A */
92 static int smctr_alloc_shared_memory(struct net_device *dev);
93
94 /* B */
95 static int smctr_bypass_state(struct net_device *dev);
96
97 /* C */
98 static int smctr_checksum_firmware(struct net_device *dev);
99 static int __init smctr_chk_isa(struct net_device *dev);
100 static int smctr_chg_rx_mask(struct net_device *dev);
101 static int smctr_clear_int(struct net_device *dev);
102 static int smctr_clear_trc_reset(int ioaddr);
103 static int smctr_close(struct net_device *dev);
104
105 /* D */
106 static int smctr_decode_firmware(struct net_device *dev);
107 static int smctr_disable_16bit(struct net_device *dev);
108 static int smctr_disable_adapter_ctrl_store(struct net_device *dev);
109 static int smctr_disable_bic_int(struct net_device *dev);
110
111 /* E */
112 static int smctr_enable_16bit(struct net_device *dev);
113 static int smctr_enable_adapter_ctrl_store(struct net_device *dev);
114 static int smctr_enable_adapter_ram(struct net_device *dev);
115 static int smctr_enable_bic_int(struct net_device *dev);
116
117 /* G */
118 static int __init smctr_get_boardid(struct net_device *dev, int mca);
119 static int smctr_get_group_address(struct net_device *dev);
120 static int smctr_get_functional_address(struct net_device *dev);
121 static unsigned int smctr_get_num_rx_bdbs(struct net_device *dev);
122 static int smctr_get_physical_drop_number(struct net_device *dev);
123 static __u8 *smctr_get_rx_pointer(struct net_device *dev, short queue);
124 static int smctr_get_station_id(struct net_device *dev);
125 static struct net_device_stats *smctr_get_stats(struct net_device *dev);
126 static FCBlock *smctr_get_tx_fcb(struct net_device *dev, __u16 queue,
127         __u16 bytes_count);
128 static int smctr_get_upstream_neighbor_addr(struct net_device *dev);
129
130 /* H */
131 static int smctr_hardware_send_packet(struct net_device *dev,
132         struct net_local *tp);
133 /* I */
134 static int smctr_init_acbs(struct net_device *dev);
135 static int smctr_init_adapter(struct net_device *dev);
136 static int smctr_init_card_real(struct net_device *dev);
137 static int smctr_init_rx_bdbs(struct net_device *dev);
138 static int smctr_init_rx_fcbs(struct net_device *dev);
139 static int smctr_init_shared_memory(struct net_device *dev);
140 static int smctr_init_tx_bdbs(struct net_device *dev);
141 static int smctr_init_tx_fcbs(struct net_device *dev);
142 static int smctr_internal_self_test(struct net_device *dev);
143 static irqreturn_t smctr_interrupt(int irq, void *dev_id);
144 static int smctr_issue_enable_int_cmd(struct net_device *dev,
145         __u16 interrupt_enable_mask);
146 static int smctr_issue_int_ack(struct net_device *dev, __u16 iack_code,
147         __u16 ibits);
148 static int smctr_issue_init_timers_cmd(struct net_device *dev);
149 static int smctr_issue_init_txrx_cmd(struct net_device *dev);
150 static int smctr_issue_insert_cmd(struct net_device *dev);
151 static int smctr_issue_read_ring_status_cmd(struct net_device *dev);
152 static int smctr_issue_read_word_cmd(struct net_device *dev, __u16 aword_cnt);
153 static int smctr_issue_remove_cmd(struct net_device *dev);
154 static int smctr_issue_resume_acb_cmd(struct net_device *dev);
155 static int smctr_issue_resume_rx_bdb_cmd(struct net_device *dev, __u16 queue);
156 static int smctr_issue_resume_rx_fcb_cmd(struct net_device *dev, __u16 queue);
157 static int smctr_issue_resume_tx_fcb_cmd(struct net_device *dev, __u16 queue);
158 static int smctr_issue_test_internal_rom_cmd(struct net_device *dev);
159 static int smctr_issue_test_hic_cmd(struct net_device *dev);
160 static int smctr_issue_test_mac_reg_cmd(struct net_device *dev);
161 static int smctr_issue_trc_loopback_cmd(struct net_device *dev);
162 static int smctr_issue_tri_loopback_cmd(struct net_device *dev);
163 static int smctr_issue_write_byte_cmd(struct net_device *dev,
164         short aword_cnt, void *byte);
165 static int smctr_issue_write_word_cmd(struct net_device *dev,
166         short aword_cnt, void *word);
167
168 /* J */
169 static int smctr_join_complete_state(struct net_device *dev);
170
171 /* L */
172 static int smctr_link_tx_fcbs_to_bdbs(struct net_device *dev);
173 static int smctr_load_firmware(struct net_device *dev);
174 static int smctr_load_node_addr(struct net_device *dev);
175 static int smctr_lobe_media_test(struct net_device *dev);
176 static int smctr_lobe_media_test_cmd(struct net_device *dev);
177 static int smctr_lobe_media_test_state(struct net_device *dev);
178
179 /* M */
180 static int smctr_make_8025_hdr(struct net_device *dev,
181         MAC_HEADER *rmf, MAC_HEADER *tmf, __u16 ac_fc);
182 static int smctr_make_access_pri(struct net_device *dev,
183         MAC_SUB_VECTOR *tsv);
184 static int smctr_make_addr_mod(struct net_device *dev, MAC_SUB_VECTOR *tsv);
185 static int smctr_make_auth_funct_class(struct net_device *dev,
186         MAC_SUB_VECTOR *tsv);
187 static int smctr_make_corr(struct net_device *dev,
188         MAC_SUB_VECTOR *tsv, __u16 correlator);
189 static int smctr_make_funct_addr(struct net_device *dev,
190         MAC_SUB_VECTOR *tsv);
191 static int smctr_make_group_addr(struct net_device *dev,
192         MAC_SUB_VECTOR *tsv);
193 static int smctr_make_phy_drop_num(struct net_device *dev,
194         MAC_SUB_VECTOR *tsv);
195 static int smctr_make_product_id(struct net_device *dev, MAC_SUB_VECTOR *tsv);
196 static int smctr_make_station_id(struct net_device *dev, MAC_SUB_VECTOR *tsv);
197 static int smctr_make_ring_station_status(struct net_device *dev,
198         MAC_SUB_VECTOR *tsv);
199 static int smctr_make_ring_station_version(struct net_device *dev,
200         MAC_SUB_VECTOR *tsv);
201 static int smctr_make_tx_status_code(struct net_device *dev,
202         MAC_SUB_VECTOR *tsv, __u16 tx_fstatus);
203 static int smctr_make_upstream_neighbor_addr(struct net_device *dev,
204         MAC_SUB_VECTOR *tsv);
205 static int smctr_make_wrap_data(struct net_device *dev,
206         MAC_SUB_VECTOR *tsv);
207
208 /* O */
209 static int smctr_open(struct net_device *dev);
210 static int smctr_open_tr(struct net_device *dev);
211
212 /* P */
213 struct net_device *smctr_probe(int unit);
214 static int __init smctr_probe1(struct net_device *dev, int ioaddr);
215 static int smctr_process_rx_packet(MAC_HEADER *rmf, __u16 size,
216         struct net_device *dev, __u16 rx_status);
217
218 /* R */
219 static int smctr_ram_memory_test(struct net_device *dev);
220 static int smctr_rcv_chg_param(struct net_device *dev, MAC_HEADER *rmf,
221         __u16 *correlator);
222 static int smctr_rcv_init(struct net_device *dev, MAC_HEADER *rmf,
223         __u16 *correlator);
224 static int smctr_rcv_tx_forward(struct net_device *dev, MAC_HEADER *rmf);
225 static int smctr_rcv_rq_addr_state_attch(struct net_device *dev,
226         MAC_HEADER *rmf, __u16 *correlator);
227 static int smctr_rcv_unknown(struct net_device *dev, MAC_HEADER *rmf,
228         __u16 *correlator);
229 static int smctr_reset_adapter(struct net_device *dev);
230 static int smctr_restart_tx_chain(struct net_device *dev, short queue);
231 static int smctr_ring_status_chg(struct net_device *dev);
232 static int smctr_rx_frame(struct net_device *dev);
233
234 /* S */
235 static int smctr_send_dat(struct net_device *dev);
236 static int smctr_send_packet(struct sk_buff *skb, struct net_device *dev);
237 static int smctr_send_lobe_media_test(struct net_device *dev);
238 static int smctr_send_rpt_addr(struct net_device *dev, MAC_HEADER *rmf,
239         __u16 correlator);
240 static int smctr_send_rpt_attch(struct net_device *dev, MAC_HEADER *rmf,
241         __u16 correlator);
242 static int smctr_send_rpt_state(struct net_device *dev, MAC_HEADER *rmf,
243         __u16 correlator);
244 static int smctr_send_rpt_tx_forward(struct net_device *dev,
245         MAC_HEADER *rmf, __u16 tx_fstatus);
246 static int smctr_send_rsp(struct net_device *dev, MAC_HEADER *rmf,
247         __u16 rcode, __u16 correlator);
248 static int smctr_send_rq_init(struct net_device *dev);
249 static int smctr_send_tx_forward(struct net_device *dev, MAC_HEADER *rmf,
250         __u16 *tx_fstatus);
251 static int smctr_set_auth_access_pri(struct net_device *dev,
252         MAC_SUB_VECTOR *rsv);
253 static int smctr_set_auth_funct_class(struct net_device *dev,
254         MAC_SUB_VECTOR *rsv);
255 static int smctr_set_corr(struct net_device *dev, MAC_SUB_VECTOR *rsv,
256         __u16 *correlator);
257 static int smctr_set_error_timer_value(struct net_device *dev,
258         MAC_SUB_VECTOR *rsv);
259 static int smctr_set_frame_forward(struct net_device *dev,
260         MAC_SUB_VECTOR *rsv, __u8 dc_sc);
261 static int smctr_set_local_ring_num(struct net_device *dev,
262         MAC_SUB_VECTOR *rsv);
263 static unsigned short smctr_set_ctrl_attention(struct net_device *dev);
264 static void smctr_set_multicast_list(struct net_device *dev);
265 static int smctr_set_page(struct net_device *dev, __u8 *buf);
266 static int smctr_set_phy_drop(struct net_device *dev,
267         MAC_SUB_VECTOR *rsv);
268 static int smctr_set_ring_speed(struct net_device *dev);
269 static int smctr_set_rx_look_ahead(struct net_device *dev);
270 static int smctr_set_trc_reset(int ioaddr);
271 static int smctr_setup_single_cmd(struct net_device *dev,
272         __u16 command, __u16 subcommand);
273 static int smctr_setup_single_cmd_w_data(struct net_device *dev,
274         __u16 command, __u16 subcommand);
275 static char *smctr_malloc(struct net_device *dev, __u16 size);
276 static int smctr_status_chg(struct net_device *dev);
277
278 /* T */
279 static void smctr_timeout(struct net_device *dev);
280 static int smctr_trc_send_packet(struct net_device *dev, FCBlock *fcb,
281         __u16 queue);
282 static __u16 smctr_tx_complete(struct net_device *dev, __u16 queue);
283 static unsigned short smctr_tx_move_frame(struct net_device *dev,
284         struct sk_buff *skb, __u8 *pbuff, unsigned int bytes);
285
286 /* U */
287 static int smctr_update_err_stats(struct net_device *dev);
288 static int smctr_update_rx_chain(struct net_device *dev, __u16 queue);
289 static int smctr_update_tx_chain(struct net_device *dev, FCBlock *fcb,
290         __u16 queue);
291
292 /* W */
293 static int smctr_wait_cmd(struct net_device *dev);
294 static int smctr_wait_while_cbusy(struct net_device *dev);
295
296 #define TO_256_BYTE_BOUNDRY(X)  (((X + 0xff) & 0xff00) - X)
297 #define TO_PARAGRAPH_BOUNDRY(X) (((X + 0x0f) & 0xfff0) - X)
298 #define PARAGRAPH_BOUNDRY(X)    smctr_malloc(dev, TO_PARAGRAPH_BOUNDRY(X))
299
300 /* Allocate Adapter Shared Memory.
301  * IMPORTANT NOTE: Any changes to this function MUST be mirrored in the
302  * function "get_num_rx_bdbs" below!!!
303  *
304  * Order of memory allocation:
305  *
306  *       0. Initial System Configuration Block Pointer
307  *       1. System Configuration Block
308  *       2. System Control Block
309  *       3. Action Command Block
310  *       4. Interrupt Status Block
311  *
312  *       5. MAC TX FCB'S
313  *       6. NON-MAC TX FCB'S
314  *       7. MAC TX BDB'S
315  *       8. NON-MAC TX BDB'S
316  *       9. MAC RX FCB'S
317  *      10. NON-MAC RX FCB'S
318  *      11. MAC RX BDB'S
319  *      12. NON-MAC RX BDB'S
320  *      13. MAC TX Data Buffer( 1, 256 byte buffer)
321  *      14. MAC RX Data Buffer( 1, 256 byte buffer)
322  *
323  *      15. NON-MAC TX Data Buffer
324  *      16. NON-MAC RX Data Buffer
325  */
326 static int smctr_alloc_shared_memory(struct net_device *dev)
327 {
328         struct net_local *tp = netdev_priv(dev);
329
330         if(smctr_debug > 10)
331                 printk(KERN_DEBUG "%s: smctr_alloc_shared_memory\n", dev->name);
332
333         /* Allocate initial System Control Block pointer.
334          * This pointer is located in the last page, last offset - 4.
335          */
336         tp->iscpb_ptr = (ISCPBlock *)(tp->ram_access + ((__u32)64 * 0x400)
337                 - (long)ISCP_BLOCK_SIZE);
338
339         /* Allocate System Control Blocks. */
340         tp->scgb_ptr = (SCGBlock *)smctr_malloc(dev, sizeof(SCGBlock));
341         PARAGRAPH_BOUNDRY(tp->sh_mem_used);
342
343         tp->sclb_ptr = (SCLBlock *)smctr_malloc(dev, sizeof(SCLBlock));
344         PARAGRAPH_BOUNDRY(tp->sh_mem_used);
345
346         tp->acb_head = (ACBlock *)smctr_malloc(dev,
347                 sizeof(ACBlock)*tp->num_acbs);
348         PARAGRAPH_BOUNDRY(tp->sh_mem_used);
349
350         tp->isb_ptr = (ISBlock *)smctr_malloc(dev, sizeof(ISBlock));
351         PARAGRAPH_BOUNDRY(tp->sh_mem_used);
352
353         tp->misc_command_data = (__u16 *)smctr_malloc(dev, MISC_DATA_SIZE);
354         PARAGRAPH_BOUNDRY(tp->sh_mem_used);
355
356         /* Allocate transmit FCBs. */
357         tp->tx_fcb_head[MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
358                 sizeof(FCBlock) * tp->num_tx_fcbs[MAC_QUEUE]);
359
360         tp->tx_fcb_head[NON_MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
361                 sizeof(FCBlock) * tp->num_tx_fcbs[NON_MAC_QUEUE]);
362
363         tp->tx_fcb_head[BUG_QUEUE] = (FCBlock *)smctr_malloc(dev,
364                 sizeof(FCBlock) * tp->num_tx_fcbs[BUG_QUEUE]);
365
366         /* Allocate transmit BDBs. */
367         tp->tx_bdb_head[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
368                 sizeof(BDBlock) * tp->num_tx_bdbs[MAC_QUEUE]);
369
370         tp->tx_bdb_head[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
371                 sizeof(BDBlock) * tp->num_tx_bdbs[NON_MAC_QUEUE]);
372
373         tp->tx_bdb_head[BUG_QUEUE] = (BDBlock *)smctr_malloc(dev,
374                 sizeof(BDBlock) * tp->num_tx_bdbs[BUG_QUEUE]);
375
376         /* Allocate receive FCBs. */
377         tp->rx_fcb_head[MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
378                 sizeof(FCBlock) * tp->num_rx_fcbs[MAC_QUEUE]);
379
380         tp->rx_fcb_head[NON_MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
381                 sizeof(FCBlock) * tp->num_rx_fcbs[NON_MAC_QUEUE]);
382
383         /* Allocate receive BDBs. */
384         tp->rx_bdb_head[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
385                 sizeof(BDBlock) * tp->num_rx_bdbs[MAC_QUEUE]);
386
387         tp->rx_bdb_end[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev, 0);
388
389         tp->rx_bdb_head[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
390                 sizeof(BDBlock) * tp->num_rx_bdbs[NON_MAC_QUEUE]);
391
392         tp->rx_bdb_end[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev, 0);
393
394         /* Allocate MAC transmit buffers.
395          * MAC Tx Buffers doen't have to be on an ODD Boundry.
396          */
397         tp->tx_buff_head[MAC_QUEUE]
398                 = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[MAC_QUEUE]);
399         tp->tx_buff_curr[MAC_QUEUE] = tp->tx_buff_head[MAC_QUEUE];
400         tp->tx_buff_end [MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
401
402         /* Allocate BUG transmit buffers. */
403         tp->tx_buff_head[BUG_QUEUE]
404                 = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[BUG_QUEUE]);
405         tp->tx_buff_curr[BUG_QUEUE] = tp->tx_buff_head[BUG_QUEUE];
406         tp->tx_buff_end[BUG_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
407
408         /* Allocate MAC receive data buffers.
409          * MAC Rx buffer doesn't have to be on a 256 byte boundary.
410          */
411         tp->rx_buff_head[MAC_QUEUE] = (__u16 *)smctr_malloc(dev,
412                 RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[MAC_QUEUE]);
413         tp->rx_buff_end[MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
414
415         /* Allocate Non-MAC transmit buffers.
416          * ?? For maximum Netware performance, put Tx Buffers on
417          * ODD Boundry and then restore malloc to Even Boundrys.
418          */
419         smctr_malloc(dev, 1L);
420         tp->tx_buff_head[NON_MAC_QUEUE]
421                 = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[NON_MAC_QUEUE]);
422         tp->tx_buff_curr[NON_MAC_QUEUE] = tp->tx_buff_head[NON_MAC_QUEUE];
423         tp->tx_buff_end [NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
424         smctr_malloc(dev, 1L);
425
426         /* Allocate Non-MAC receive data buffers.
427          * To guarantee a minimum of 256 contigous memory to
428          * UM_Receive_Packet's lookahead pointer, before a page
429          * change or ring end is encountered, place each rx buffer on
430          * a 256 byte boundary.
431          */
432         smctr_malloc(dev, TO_256_BYTE_BOUNDRY(tp->sh_mem_used));
433         tp->rx_buff_head[NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev,
434                 RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[NON_MAC_QUEUE]);
435         tp->rx_buff_end[NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
436
437         return (0);
438 }
439
440 /* Enter Bypass state. */
441 static int smctr_bypass_state(struct net_device *dev)
442 {
443         int err;
444
445         if(smctr_debug > 10)
446                 printk(KERN_DEBUG "%s: smctr_bypass_state\n", dev->name);
447
448         err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE, JS_BYPASS_STATE);
449
450         return (err);
451 }
452
453 static int smctr_checksum_firmware(struct net_device *dev)
454 {
455         struct net_local *tp = netdev_priv(dev);
456         __u16 i, checksum = 0;
457
458         if(smctr_debug > 10)
459                 printk(KERN_DEBUG "%s: smctr_checksum_firmware\n", dev->name);
460
461         smctr_enable_adapter_ctrl_store(dev);
462
463         for(i = 0; i < CS_RAM_SIZE; i += 2)
464                 checksum += *((__u16 *)(tp->ram_access + i));
465
466         tp->microcode_version = *(__u16 *)(tp->ram_access
467                 + CS_RAM_VERSION_OFFSET);
468         tp->microcode_version >>= 8;
469
470         smctr_disable_adapter_ctrl_store(dev);
471
472         if(checksum)
473                 return (checksum);
474
475         return (0);
476 }
477
478 static int __init smctr_chk_mca(struct net_device *dev)
479 {
480 #ifdef CONFIG_MCA_LEGACY
481         struct net_local *tp = netdev_priv(dev);
482         int current_slot;
483         __u8 r1, r2, r3, r4, r5;
484
485         current_slot = mca_find_unused_adapter(smctr_posid, 0);
486         if(current_slot == MCA_NOTFOUND)
487                 return (-ENODEV);
488
489         mca_set_adapter_name(current_slot, smctr_name);
490         mca_mark_as_used(current_slot);
491         tp->slot_num = current_slot;
492
493         r1 = mca_read_stored_pos(tp->slot_num, 2);
494         r2 = mca_read_stored_pos(tp->slot_num, 3);
495
496         if(tp->slot_num)
497                 outb(CNFG_POS_CONTROL_REG, (__u8)((tp->slot_num - 1) | CNFG_SLOT_ENABLE_BIT));
498         else
499                 outb(CNFG_POS_CONTROL_REG, (__u8)((tp->slot_num) | CNFG_SLOT_ENABLE_BIT));
500
501         r1 = inb(CNFG_POS_REG1);
502         r2 = inb(CNFG_POS_REG0);
503
504         tp->bic_type = BIC_594_CHIP;
505
506         /* IO */
507         r2 = mca_read_stored_pos(tp->slot_num, 2);
508         r2 &= 0xF0;
509         dev->base_addr = ((__u16)r2 << 8) + (__u16)0x800;
510         request_region(dev->base_addr, SMCTR_IO_EXTENT, smctr_name);
511
512         /* IRQ */
513         r5 = mca_read_stored_pos(tp->slot_num, 5);
514         r5 &= 0xC;
515         switch(r5)
516         {
517                 case 0:
518                         dev->irq = 3;
519                         break;
520
521                 case 0x4:
522                         dev->irq = 4;
523                         break;
524
525                 case 0x8:
526                         dev->irq = 10;
527                         break;
528
529                 default:
530                         dev->irq = 15;
531                         break;
532         }
533         if (request_irq(dev->irq, smctr_interrupt, IRQF_SHARED, smctr_name, dev)) {
534                 release_region(dev->base_addr, SMCTR_IO_EXTENT);
535                 return -ENODEV;
536         }
537
538         /* Get RAM base */
539         r3 = mca_read_stored_pos(tp->slot_num, 3);
540         tp->ram_base = ((__u32)(r3 & 0x7) << 13) + 0x0C0000;
541         if (r3 & 0x8)
542                 tp->ram_base += 0x010000;
543         if (r3 & 0x80)
544                 tp->ram_base += 0xF00000;
545
546         /* Get Ram Size */
547         r3 &= 0x30;
548         r3 >>= 4;
549
550         tp->ram_usable = (__u16)CNFG_SIZE_8KB << r3;
551         tp->ram_size = (__u16)CNFG_SIZE_64KB;
552         tp->board_id |= TOKEN_MEDIA;
553
554         r4 = mca_read_stored_pos(tp->slot_num, 4);
555         tp->rom_base = ((__u32)(r4 & 0x7) << 13) + 0x0C0000;
556         if (r4 & 0x8)
557                 tp->rom_base += 0x010000;
558
559         /* Get ROM size. */
560         r4 >>= 4;
561         switch (r4) {
562                 case 0:
563                         tp->rom_size = CNFG_SIZE_8KB;
564                         break;
565                 case 1:
566                         tp->rom_size = CNFG_SIZE_16KB;
567                         break;
568                 case 2:
569                         tp->rom_size = CNFG_SIZE_32KB;
570                         break;
571                 default:
572                         tp->rom_size = ROM_DISABLE;
573         }
574
575         /* Get Media Type. */
576         r5 = mca_read_stored_pos(tp->slot_num, 5);
577         r5 &= CNFG_MEDIA_TYPE_MASK;
578         switch(r5)
579         {
580                 case (0):
581                         tp->media_type = MEDIA_STP_4;
582                         break;
583
584                 case (1):
585                         tp->media_type = MEDIA_STP_16;
586                         break;
587
588                 case (3):
589                         tp->media_type = MEDIA_UTP_16;
590                         break;
591
592                 default:
593                         tp->media_type = MEDIA_UTP_4;
594                         break;
595         }
596         tp->media_menu = 14;
597
598         r2 = mca_read_stored_pos(tp->slot_num, 2);
599         if(!(r2 & 0x02))
600                 tp->mode_bits |= EARLY_TOKEN_REL;
601
602         /* Disable slot */
603         outb(CNFG_POS_CONTROL_REG, 0);
604
605         tp->board_id = smctr_get_boardid(dev, 1);
606         switch(tp->board_id & 0xffff)
607         {
608                 case WD8115TA:
609                         smctr_model = "8115T/A";
610                         break;
611
612                 case WD8115T:
613                         if(tp->extra_info & CHIP_REV_MASK)
614                                 smctr_model = "8115T rev XE";
615                         else
616                                 smctr_model = "8115T rev XD";
617                         break;
618
619                 default:
620                         smctr_model = "Unknown";
621                         break;
622         }
623
624         return (0);
625 #else
626         return (-1);
627 #endif /* CONFIG_MCA_LEGACY */
628 }
629
630 static int smctr_chg_rx_mask(struct net_device *dev)
631 {
632         struct net_local *tp = netdev_priv(dev);
633         int err = 0;
634
635         if(smctr_debug > 10)
636                 printk(KERN_DEBUG "%s: smctr_chg_rx_mask\n", dev->name);
637
638         smctr_enable_16bit(dev);
639         smctr_set_page(dev, (__u8 *)tp->ram_access);
640
641         if(tp->mode_bits & LOOPING_MODE_MASK)
642                 tp->config_word0 |= RX_OWN_BIT;
643         else
644                 tp->config_word0 &= ~RX_OWN_BIT;
645
646         if(tp->receive_mask & PROMISCUOUS_MODE)
647                 tp->config_word0 |= PROMISCUOUS_BIT;
648         else
649                 tp->config_word0 &= ~PROMISCUOUS_BIT;
650
651         if(tp->receive_mask & ACCEPT_ERR_PACKETS)
652                 tp->config_word0 |= SAVBAD_BIT;
653         else
654                 tp->config_word0 &= ~SAVBAD_BIT;
655
656         if(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
657                 tp->config_word0 |= RXATMAC;
658         else
659                 tp->config_word0 &= ~RXATMAC;
660
661         if(tp->receive_mask & ACCEPT_MULTI_PROM)
662                 tp->config_word1 |= MULTICAST_ADDRESS_BIT;
663         else
664                 tp->config_word1 &= ~MULTICAST_ADDRESS_BIT;
665
666         if(tp->receive_mask & ACCEPT_SOURCE_ROUTING_SPANNING)
667                 tp->config_word1 |= SOURCE_ROUTING_SPANNING_BITS;
668         else
669         {
670                 if(tp->receive_mask & ACCEPT_SOURCE_ROUTING)
671                         tp->config_word1 |= SOURCE_ROUTING_EXPLORER_BIT;
672                 else
673                         tp->config_word1 &= ~SOURCE_ROUTING_SPANNING_BITS;
674         }
675
676         if((err = smctr_issue_write_word_cmd(dev, RW_CONFIG_REGISTER_0,
677                 &tp->config_word0)))
678         {
679                 return (err);
680         }
681
682         if((err = smctr_issue_write_word_cmd(dev, RW_CONFIG_REGISTER_1,
683                 &tp->config_word1)))
684         {
685                 return (err);
686         }
687
688         smctr_disable_16bit(dev);
689
690         return (0);
691 }
692
693 static int smctr_clear_int(struct net_device *dev)
694 {
695         struct net_local *tp = netdev_priv(dev);
696
697         outb((tp->trc_mask | CSR_CLRTINT), dev->base_addr + CSR);
698
699         return (0);
700 }
701
702 static int smctr_clear_trc_reset(int ioaddr)
703 {
704         __u8 r;
705
706         r = inb(ioaddr + MSR);
707         outb(~MSR_RST & r, ioaddr + MSR);
708
709         return (0);
710 }
711
712 /*
713  * The inverse routine to smctr_open().
714  */
715 static int smctr_close(struct net_device *dev)
716 {
717         struct net_local *tp = netdev_priv(dev);
718         struct sk_buff *skb;
719         int err;
720
721         netif_stop_queue(dev);
722         
723         tp->cleanup = 1;
724
725         /* Check to see if adapter is already in a closed state. */
726         if(tp->status != OPEN)
727                 return (0);
728
729         smctr_enable_16bit(dev);
730         smctr_set_page(dev, (__u8 *)tp->ram_access);
731
732         if((err = smctr_issue_remove_cmd(dev)))
733         {
734                 smctr_disable_16bit(dev);
735                 return (err);
736         }
737
738         for(;;)
739         {
740                 skb = skb_dequeue(&tp->SendSkbQueue);
741                 if(skb == NULL)
742                         break;
743                 tp->QueueSkb++;
744                 dev_kfree_skb(skb);
745         }
746
747
748         return (0);
749 }
750
751 static int smctr_decode_firmware(struct net_device *dev)
752 {
753         struct net_local *tp = netdev_priv(dev);
754         short bit = 0x80, shift = 12;
755         DECODE_TREE_NODE *tree;
756         short branch, tsize;
757         __u16 buff = 0;
758         long weight;
759         __u8 *ucode;
760         __u16 *mem;
761
762         if(smctr_debug > 10)
763                 printk(KERN_DEBUG "%s: smctr_decode_firmware\n", dev->name);
764
765         weight  = *(long *)(tp->ptr_ucode + WEIGHT_OFFSET);
766         tsize   = *(__u8 *)(tp->ptr_ucode + TREE_SIZE_OFFSET);
767         tree    = (DECODE_TREE_NODE *)(tp->ptr_ucode + TREE_OFFSET);
768         ucode   = (__u8 *)(tp->ptr_ucode + TREE_OFFSET
769                         + (tsize * sizeof(DECODE_TREE_NODE)));
770         mem     = (__u16 *)(tp->ram_access);
771
772         while(weight)
773         {
774                 branch = ROOT;
775                 while((tree + branch)->tag != LEAF && weight)
776                 {
777                         branch = *ucode & bit ? (tree + branch)->llink
778                                 : (tree + branch)->rlink;
779
780                         bit >>= 1;
781                         weight--;
782
783                         if(bit == 0)
784                         {
785                                 bit = 0x80;
786                                 ucode++;
787                         }
788                 }
789
790                 buff |= (tree + branch)->info << shift;
791                 shift -= 4;
792
793                 if(shift < 0)
794                 {
795                         *(mem++) = SWAP_BYTES(buff);
796                         buff    = 0;
797                         shift   = 12;
798                 }
799         }
800
801         /* The following assumes the Control Store Memory has
802          * been initialized to zero. If the last partial word
803          * is zero, it will not be written.
804          */
805         if(buff)
806                 *(mem++) = SWAP_BYTES(buff);
807
808         return (0);
809 }
810
811 static int smctr_disable_16bit(struct net_device *dev)
812 {
813         return (0);
814 }
815
816 /*
817  * On Exit, Adapter is:
818  * 1. TRC is in a reset state and un-initialized.
819  * 2. Adapter memory is enabled.
820  * 3. Control Store memory is out of context (-WCSS is 1).
821  */
822 static int smctr_disable_adapter_ctrl_store(struct net_device *dev)
823 {
824         struct net_local *tp = netdev_priv(dev);
825         int ioaddr = dev->base_addr;
826
827         if(smctr_debug > 10)
828                 printk(KERN_DEBUG "%s: smctr_disable_adapter_ctrl_store\n", dev->name);
829
830         tp->trc_mask |= CSR_WCSS;
831         outb(tp->trc_mask, ioaddr + CSR);
832
833         return (0);
834 }
835
836 static int smctr_disable_bic_int(struct net_device *dev)
837 {
838         struct net_local *tp = netdev_priv(dev);
839         int ioaddr = dev->base_addr;
840
841         tp->trc_mask = CSR_MSK_ALL | CSR_MSKCBUSY
842                 | CSR_MSKTINT | CSR_WCSS;
843         outb(tp->trc_mask, ioaddr + CSR);
844
845         return (0);
846 }
847
848 static int smctr_enable_16bit(struct net_device *dev)
849 {
850         struct net_local *tp = netdev_priv(dev);
851         __u8    r;
852
853         if(tp->adapter_bus == BUS_ISA16_TYPE)
854         {
855                 r = inb(dev->base_addr + LAAR);
856                 outb((r | LAAR_MEM16ENB), dev->base_addr + LAAR);
857         }
858
859         return (0);
860 }
861
862 /*
863  * To enable the adapter control store memory:
864  * 1. Adapter must be in a RESET state.
865  * 2. Adapter memory must be enabled.
866  * 3. Control Store Memory is in context (-WCSS is 0).
867  */
868 static int smctr_enable_adapter_ctrl_store(struct net_device *dev)
869 {
870         struct net_local *tp = netdev_priv(dev);
871         int ioaddr = dev->base_addr;
872
873         if(smctr_debug > 10)
874                 printk(KERN_DEBUG "%s: smctr_enable_adapter_ctrl_store\n", dev->name);
875
876         smctr_set_trc_reset(ioaddr);
877         smctr_enable_adapter_ram(dev);
878
879         tp->trc_mask &= ~CSR_WCSS;
880         outb(tp->trc_mask, ioaddr + CSR);
881
882         return (0);
883 }
884
885 static int smctr_enable_adapter_ram(struct net_device *dev)
886 {
887         int ioaddr = dev->base_addr;
888         __u8 r;
889
890         if(smctr_debug > 10)
891                 printk(KERN_DEBUG "%s: smctr_enable_adapter_ram\n", dev->name);
892
893         r = inb(ioaddr + MSR);
894         outb(MSR_MEMB | r, ioaddr + MSR);
895
896         return (0);
897 }
898
899 static int smctr_enable_bic_int(struct net_device *dev)
900 {
901         struct net_local *tp = netdev_priv(dev);
902         int ioaddr = dev->base_addr;
903         __u8 r;
904
905         switch(tp->bic_type)
906         {
907                 case (BIC_584_CHIP):
908                         tp->trc_mask = CSR_MSKCBUSY | CSR_WCSS;
909                         outb(tp->trc_mask, ioaddr + CSR);
910                         r = inb(ioaddr + IRR);
911                         outb(r | IRR_IEN, ioaddr + IRR);
912                         break;
913
914                 case (BIC_594_CHIP):
915                         tp->trc_mask = CSR_MSKCBUSY | CSR_WCSS;
916                         outb(tp->trc_mask, ioaddr + CSR);
917                         r = inb(ioaddr + IMCCR);
918                         outb(r | IMCCR_EIL, ioaddr + IMCCR);
919                         break;
920         }
921
922         return (0);
923 }
924
925 static int __init smctr_chk_isa(struct net_device *dev)
926 {
927         struct net_local *tp = netdev_priv(dev);
928         int ioaddr = dev->base_addr;
929         __u8 r1, r2, b, chksum = 0;
930         __u16 r;
931         int i;
932         int err = -ENODEV;
933
934         if(smctr_debug > 10)
935                 printk(KERN_DEBUG "%s: smctr_chk_isa %#4x\n", dev->name, ioaddr);
936
937         if((ioaddr & 0x1F) != 0)
938                 goto out;
939
940         /* Grab the region so that no one else tries to probe our ioports. */
941         if (!request_region(ioaddr, SMCTR_IO_EXTENT, smctr_name)) {
942                 err = -EBUSY;
943                 goto out;
944         }
945
946         /* Checksum SMC node address */
947         for(i = 0; i < 8; i++)
948         {
949                 b = inb(ioaddr + LAR0 + i);
950                 chksum += b;
951         }
952
953         if (chksum != NODE_ADDR_CKSUM)
954                 goto out2;
955
956         b = inb(ioaddr + BDID);
957         if(b != BRD_ID_8115T)
958         {
959                 printk(KERN_ERR "%s: The adapter found is not supported\n", dev->name);
960                 goto out2;
961         }
962
963         /* Check for 8115T Board ID */
964         r2 = 0;
965         for(r = 0; r < 8; r++)
966         {
967             r1 = inb(ioaddr + 0x8 + r);
968             r2 += r1;
969         }
970
971         /* value of RegF adds up the sum to 0xFF */
972         if((r2 != 0xFF) && (r2 != 0xEE))
973                 goto out2;
974
975         /* Get adapter ID */
976         tp->board_id = smctr_get_boardid(dev, 0);
977         switch(tp->board_id & 0xffff)
978         {
979                 case WD8115TA:
980                         smctr_model = "8115T/A";
981                         break;
982
983                 case WD8115T:
984                         if(tp->extra_info & CHIP_REV_MASK)
985                                 smctr_model = "8115T rev XE";
986                         else
987                                 smctr_model = "8115T rev XD";
988                         break;
989
990                 default:
991                         smctr_model = "Unknown";
992                         break;
993         }
994
995         /* Store BIC type. */
996         tp->bic_type = BIC_584_CHIP;
997         tp->nic_type = NIC_825_CHIP;
998
999         /* Copy Ram Size */
1000         tp->ram_usable  = CNFG_SIZE_16KB;
1001         tp->ram_size    = CNFG_SIZE_64KB;
1002
1003         /* Get 58x Ram Base */
1004         r1 = inb(ioaddr);
1005         r1 &= 0x3F;
1006
1007         r2 = inb(ioaddr + CNFG_LAAR_584);
1008         r2 &= CNFG_LAAR_MASK;
1009         r2 <<= 3;
1010         r2 |= ((r1 & 0x38) >> 3);
1011
1012         tp->ram_base = ((__u32)r2 << 16) + (((__u32)(r1 & 0x7)) << 13);
1013
1014         /* Get 584 Irq */
1015         r1 = 0;
1016         r1 = inb(ioaddr + CNFG_ICR_583);
1017         r1 &= CNFG_ICR_IR2_584;
1018
1019         r2 = inb(ioaddr + CNFG_IRR_583);
1020         r2 &= CNFG_IRR_IRQS;     /* 0x60 */
1021         r2 >>= 5;
1022
1023         switch(r2)
1024         {
1025                 case 0:
1026                         if(r1 == 0)
1027                                 dev->irq = 2;
1028                         else
1029                                 dev->irq = 10;
1030                         break;
1031
1032                 case 1:
1033                         if(r1 == 0)
1034                                 dev->irq = 3;
1035                         else
1036                                 dev->irq = 11;
1037                         break;
1038
1039                 case 2:
1040                         if(r1 == 0)
1041                         {
1042                                 if(tp->extra_info & ALTERNATE_IRQ_BIT)
1043                                         dev->irq = 5;
1044                                 else
1045                                         dev->irq = 4;
1046                         }
1047                         else
1048                                 dev->irq = 15;
1049                         break;
1050
1051                 case 3:
1052                         if(r1 == 0)
1053                                 dev->irq = 7;
1054                         else
1055                                 dev->irq = 4;
1056                         break;
1057
1058                 default:
1059                         printk(KERN_ERR "%s: No IRQ found aborting\n", dev->name);
1060                         goto out2;
1061          }
1062
1063         if (request_irq(dev->irq, smctr_interrupt, IRQF_SHARED, smctr_name, dev))
1064                 goto out2;
1065
1066         /* Get 58x Rom Base */
1067         r1 = inb(ioaddr + CNFG_BIO_583);
1068         r1 &= 0x3E;
1069         r1 |= 0x40;
1070
1071         tp->rom_base = (__u32)r1 << 13;
1072
1073         /* Get 58x Rom Size */
1074         r1 = inb(ioaddr + CNFG_BIO_583);
1075         r1 &= 0xC0;
1076         if(r1 == 0)
1077                 tp->rom_size = ROM_DISABLE;
1078         else
1079         {
1080                 r1 >>= 6;
1081                 tp->rom_size = (__u16)CNFG_SIZE_8KB << r1;
1082         }
1083
1084         /* Get 58x Boot Status */
1085         r1 = inb(ioaddr + CNFG_GP2);
1086
1087         tp->mode_bits &= (~BOOT_STATUS_MASK);
1088
1089         if(r1 & CNFG_GP2_BOOT_NIBBLE)
1090                 tp->mode_bits |= BOOT_TYPE_1;
1091
1092         /* Get 58x Zero Wait State */
1093         tp->mode_bits &= (~ZERO_WAIT_STATE_MASK);
1094
1095         r1 = inb(ioaddr + CNFG_IRR_583);
1096
1097         if(r1 & CNFG_IRR_ZWS)
1098                  tp->mode_bits |= ZERO_WAIT_STATE_8_BIT;
1099
1100         if(tp->board_id & BOARD_16BIT)
1101         {
1102                 r1 = inb(ioaddr + CNFG_LAAR_584);
1103
1104                 if(r1 & CNFG_LAAR_ZWS)
1105                         tp->mode_bits |= ZERO_WAIT_STATE_16_BIT;
1106         }
1107
1108         /* Get 584 Media Menu */
1109         tp->media_menu = 14;
1110         r1 = inb(ioaddr + CNFG_IRR_583);
1111
1112         tp->mode_bits &= 0xf8ff;       /* (~CNFG_INTERFACE_TYPE_MASK) */
1113         if((tp->board_id & TOKEN_MEDIA) == TOKEN_MEDIA)
1114         {
1115                 /* Get Advanced Features */
1116                 if(((r1 & 0x6) >> 1) == 0x3)
1117                         tp->media_type |= MEDIA_UTP_16;
1118                 else
1119                 {
1120                         if(((r1 & 0x6) >> 1) == 0x2)
1121                                 tp->media_type |= MEDIA_STP_16;
1122                         else
1123                         {
1124                                 if(((r1 & 0x6) >> 1) == 0x1)
1125                                         tp->media_type |= MEDIA_UTP_4;
1126
1127                                 else
1128                                         tp->media_type |= MEDIA_STP_4;
1129                         }
1130                 }
1131
1132                 r1 = inb(ioaddr + CNFG_GP2);
1133                 if(!(r1 & 0x2) )           /* GP2_ETRD */
1134                         tp->mode_bits |= EARLY_TOKEN_REL;
1135
1136                 /* see if the chip is corrupted
1137                 if(smctr_read_584_chksum(ioaddr))
1138                 {
1139                         printk(KERN_ERR "%s: EEPROM Checksum Failure\n", dev->name);
1140                         free_irq(dev->irq, dev);
1141                         goto out2;
1142                 }
1143                 */
1144         }
1145
1146         return (0);
1147
1148 out2:
1149         release_region(ioaddr, SMCTR_IO_EXTENT);
1150 out:
1151         return err;
1152 }
1153
1154 static int __init smctr_get_boardid(struct net_device *dev, int mca)
1155 {
1156         struct net_local *tp = netdev_priv(dev);
1157         int ioaddr = dev->base_addr;
1158         __u8 r, r1, IdByte;
1159         __u16 BoardIdMask;
1160
1161         tp->board_id = BoardIdMask = 0;
1162
1163         if(mca)
1164         {
1165                 BoardIdMask |= (MICROCHANNEL+INTERFACE_CHIP+TOKEN_MEDIA+PAGED_RAM+BOARD_16BIT);
1166                 tp->extra_info |= (INTERFACE_594_CHIP+RAM_SIZE_64K+NIC_825_BIT+ALTERNATE_IRQ_BIT+SLOT_16BIT);
1167         }
1168         else
1169         {
1170                 BoardIdMask|=(INTERFACE_CHIP+TOKEN_MEDIA+PAGED_RAM+BOARD_16BIT);
1171                 tp->extra_info |= (INTERFACE_584_CHIP + RAM_SIZE_64K
1172                         + NIC_825_BIT + ALTERNATE_IRQ_BIT);
1173         }
1174
1175         if(!mca)
1176         {
1177                 r = inb(ioaddr + BID_REG_1);
1178                 r &= 0x0c;
1179                 outb(r, ioaddr + BID_REG_1);
1180                 r = inb(ioaddr + BID_REG_1);
1181
1182                 if(r & BID_SIXTEEN_BIT_BIT)
1183                 {
1184                         tp->extra_info |= SLOT_16BIT;
1185                         tp->adapter_bus = BUS_ISA16_TYPE;
1186                 }
1187                 else
1188                         tp->adapter_bus = BUS_ISA8_TYPE;
1189         }
1190         else
1191                 tp->adapter_bus = BUS_MCA_TYPE;
1192
1193         /* Get Board Id Byte */
1194         IdByte = inb(ioaddr + BID_BOARD_ID_BYTE);
1195
1196         /* if Major version > 1.0 then
1197          *      return;
1198          */
1199         if(IdByte & 0xF8)
1200                 return (-1);
1201
1202         r1 = inb(ioaddr + BID_REG_1);
1203         r1 &= BID_ICR_MASK;
1204         r1 |= BID_OTHER_BIT;
1205
1206         outb(r1, ioaddr + BID_REG_1);
1207         r1 = inb(ioaddr + BID_REG_3);
1208
1209         r1 &= BID_EAR_MASK;
1210         r1 |= BID_ENGR_PAGE;
1211
1212         outb(r1, ioaddr + BID_REG_3);
1213         r1 = inb(ioaddr + BID_REG_1);
1214         r1 &= BID_ICR_MASK;
1215         r1 |= (BID_RLA | BID_OTHER_BIT);
1216
1217         outb(r1, ioaddr + BID_REG_1);
1218
1219         r1 = inb(ioaddr + BID_REG_1);
1220         while(r1 & BID_RECALL_DONE_MASK)
1221                 r1 = inb(ioaddr + BID_REG_1);
1222
1223         r = inb(ioaddr + BID_LAR_0 + BID_REG_6);
1224
1225         /* clear chip rev bits */
1226         tp->extra_info &= ~CHIP_REV_MASK;
1227         tp->extra_info |= ((r & BID_EEPROM_CHIP_REV_MASK) << 6);
1228
1229         r1 = inb(ioaddr + BID_REG_1);
1230         r1 &= BID_ICR_MASK;
1231         r1 |= BID_OTHER_BIT;
1232
1233         outb(r1, ioaddr + BID_REG_1);
1234         r1 = inb(ioaddr + BID_REG_3);
1235
1236         r1 &= BID_EAR_MASK;
1237         r1 |= BID_EA6;
1238
1239         outb(r1, ioaddr + BID_REG_3);
1240         r1 = inb(ioaddr + BID_REG_1);
1241
1242         r1 &= BID_ICR_MASK;
1243         r1 |= BID_RLA;
1244
1245         outb(r1, ioaddr + BID_REG_1);
1246         r1 = inb(ioaddr + BID_REG_1);
1247
1248         while(r1 & BID_RECALL_DONE_MASK)
1249                 r1 = inb(ioaddr + BID_REG_1);
1250
1251         return (BoardIdMask);
1252 }
1253
1254 static int smctr_get_group_address(struct net_device *dev)
1255 {
1256         smctr_issue_read_word_cmd(dev, RW_INDIVIDUAL_GROUP_ADDR);
1257
1258         return(smctr_wait_cmd(dev));
1259 }
1260
1261 static int smctr_get_functional_address(struct net_device *dev)
1262 {
1263         smctr_issue_read_word_cmd(dev, RW_FUNCTIONAL_ADDR);
1264
1265         return(smctr_wait_cmd(dev));
1266 }
1267
1268 /* Calculate number of Non-MAC receive BDB's and data buffers.
1269  * This function must simulate allocateing shared memory exactly
1270  * as the allocate_shared_memory function above.
1271  */
1272 static unsigned int smctr_get_num_rx_bdbs(struct net_device *dev)
1273 {
1274         struct net_local *tp = netdev_priv(dev);
1275         unsigned int mem_used = 0;
1276
1277         /* Allocate System Control Blocks. */
1278         mem_used += sizeof(SCGBlock);
1279
1280         mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
1281         mem_used += sizeof(SCLBlock);
1282
1283         mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
1284         mem_used += sizeof(ACBlock) * tp->num_acbs;
1285
1286         mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
1287         mem_used += sizeof(ISBlock);
1288
1289         mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
1290         mem_used += MISC_DATA_SIZE;
1291
1292         /* Allocate transmit FCB's. */
1293         mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
1294
1295         mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[MAC_QUEUE];
1296         mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[NON_MAC_QUEUE];
1297         mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[BUG_QUEUE];
1298
1299         /* Allocate transmit BDBs. */
1300         mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[MAC_QUEUE];
1301         mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[NON_MAC_QUEUE];
1302         mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[BUG_QUEUE];
1303
1304         /* Allocate receive FCBs. */
1305         mem_used += sizeof(FCBlock) * tp->num_rx_fcbs[MAC_QUEUE];
1306         mem_used += sizeof(FCBlock) * tp->num_rx_fcbs[NON_MAC_QUEUE];
1307
1308         /* Allocate receive BDBs. */
1309         mem_used += sizeof(BDBlock) * tp->num_rx_bdbs[MAC_QUEUE];
1310
1311         /* Allocate MAC transmit buffers.
1312          * MAC transmit buffers don't have to be on an ODD Boundry.
1313          */
1314         mem_used += tp->tx_buff_size[MAC_QUEUE];
1315
1316         /* Allocate BUG transmit buffers. */
1317         mem_used += tp->tx_buff_size[BUG_QUEUE];
1318
1319         /* Allocate MAC receive data buffers.
1320          * MAC receive buffers don't have to be on a 256 byte boundary.
1321          */
1322         mem_used += RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[MAC_QUEUE];
1323
1324         /* Allocate Non-MAC transmit buffers.
1325          * For maximum Netware performance, put Tx Buffers on
1326          * ODD Boundry,and then restore malloc to Even Boundrys.
1327          */
1328         mem_used += 1L;
1329         mem_used += tp->tx_buff_size[NON_MAC_QUEUE];
1330         mem_used += 1L;
1331
1332         /* CALCULATE NUMBER OF NON-MAC RX BDB'S
1333          * AND NON-MAC RX DATA BUFFERS
1334          *
1335          * Make sure the mem_used offset at this point is the
1336          * same as in allocate_shared memory or the following
1337          * boundary adjustment will be incorrect (i.e. not allocating
1338          * the non-mac receive buffers above cannot change the 256
1339          * byte offset).
1340          *
1341          * Since this cannot be guaranteed, adding the full 256 bytes
1342          * to the amount of shared memory used at this point will guaranteed
1343          * that the rx data buffers do not overflow shared memory.
1344          */
1345         mem_used += 0x100;
1346
1347         return((0xffff - mem_used) / (RX_DATA_BUFFER_SIZE + sizeof(BDBlock)));
1348 }
1349
1350 static int smctr_get_physical_drop_number(struct net_device *dev)
1351 {
1352         smctr_issue_read_word_cmd(dev, RW_PHYSICAL_DROP_NUMBER);
1353
1354         return(smctr_wait_cmd(dev));
1355 }
1356
1357 static __u8 * smctr_get_rx_pointer(struct net_device *dev, short queue)
1358 {
1359         struct net_local *tp = netdev_priv(dev);
1360         BDBlock *bdb;
1361
1362         bdb = (BDBlock *)((__u32)tp->ram_access
1363                 + (__u32)(tp->rx_fcb_curr[queue]->trc_bdb_ptr));
1364
1365         tp->rx_fcb_curr[queue]->bdb_ptr = bdb;
1366
1367         return ((__u8 *)bdb->data_block_ptr);
1368 }
1369
1370 static int smctr_get_station_id(struct net_device *dev)
1371 {
1372         smctr_issue_read_word_cmd(dev, RW_INDIVIDUAL_MAC_ADDRESS);
1373
1374         return(smctr_wait_cmd(dev));
1375 }
1376
1377 /*
1378  * Get the current statistics. This may be called with the card open
1379  * or closed.
1380  */
1381 static struct net_device_stats *smctr_get_stats(struct net_device *dev)
1382 {
1383         struct net_local *tp = netdev_priv(dev);
1384
1385         return ((struct net_device_stats *)&tp->MacStat);
1386 }
1387
1388 static FCBlock *smctr_get_tx_fcb(struct net_device *dev, __u16 queue,
1389         __u16 bytes_count)
1390 {
1391         struct net_local *tp = netdev_priv(dev);
1392         FCBlock *pFCB;
1393         BDBlock *pbdb;
1394         unsigned short alloc_size;
1395         unsigned short *temp;
1396
1397         if(smctr_debug > 20)
1398                 printk(KERN_DEBUG "smctr_get_tx_fcb\n");
1399
1400         /* check if there is enough FCB blocks */
1401         if(tp->num_tx_fcbs_used[queue] >= tp->num_tx_fcbs[queue])
1402                 return ((FCBlock *)(-1L));
1403
1404         /* round off the input pkt size to the nearest even number */
1405         alloc_size = (bytes_count + 1) & 0xfffe;
1406
1407         /* check if enough mem */
1408         if((tp->tx_buff_used[queue] + alloc_size) > tp->tx_buff_size[queue])
1409                 return ((FCBlock *)(-1L));
1410
1411         /* check if past the end ;
1412          * if exactly enough mem to end of ring, alloc from front.
1413          * this avoids update of curr when curr = end
1414          */
1415         if(((unsigned long)(tp->tx_buff_curr[queue]) + alloc_size)
1416                 >= (unsigned long)(tp->tx_buff_end[queue]))
1417         {
1418                 /* check if enough memory from ring head */
1419                 alloc_size = alloc_size +
1420                         (__u16)((__u32)tp->tx_buff_end[queue]
1421                         - (__u32)tp->tx_buff_curr[queue]);
1422
1423                 if((tp->tx_buff_used[queue] + alloc_size)
1424                         > tp->tx_buff_size[queue])
1425                 {
1426                         return ((FCBlock *)(-1L));
1427                 }
1428
1429                 /* ring wrap */
1430                 tp->tx_buff_curr[queue] = tp->tx_buff_head[queue];
1431         }
1432
1433         tp->tx_buff_used[queue] += alloc_size;
1434         tp->num_tx_fcbs_used[queue]++;
1435         tp->tx_fcb_curr[queue]->frame_length = bytes_count;
1436         tp->tx_fcb_curr[queue]->memory_alloc = alloc_size;
1437         temp = tp->tx_buff_curr[queue];
1438         tp->tx_buff_curr[queue]
1439                 = (__u16 *)((__u32)temp + (__u32)((bytes_count + 1) & 0xfffe));
1440
1441         pbdb = tp->tx_fcb_curr[queue]->bdb_ptr;
1442         pbdb->buffer_length = bytes_count;
1443         pbdb->data_block_ptr = temp;
1444         pbdb->trc_data_block_ptr = TRC_POINTER(temp);
1445
1446         pFCB = tp->tx_fcb_curr[queue];
1447         tp->tx_fcb_curr[queue] = tp->tx_fcb_curr[queue]->next_ptr;
1448
1449         return (pFCB);
1450 }
1451
1452 static int smctr_get_upstream_neighbor_addr(struct net_device *dev)
1453 {
1454         smctr_issue_read_word_cmd(dev, RW_UPSTREAM_NEIGHBOR_ADDRESS);
1455
1456         return(smctr_wait_cmd(dev));
1457 }
1458
1459 static int smctr_hardware_send_packet(struct net_device *dev,
1460         struct net_local *tp)
1461 {
1462         struct tr_statistics *tstat = &tp->MacStat;
1463         struct sk_buff *skb;
1464         FCBlock *fcb;
1465
1466         if(smctr_debug > 10)
1467                 printk(KERN_DEBUG"%s: smctr_hardware_send_packet\n", dev->name);
1468
1469         if(tp->status != OPEN)
1470                 return (-1);
1471
1472         if(tp->monitor_state_ready != 1)
1473                 return (-1);
1474
1475         for(;;)
1476         {
1477                 /* Send first buffer from queue */
1478                 skb = skb_dequeue(&tp->SendSkbQueue);
1479                 if(skb == NULL)
1480                         return (-1);
1481
1482                 tp->QueueSkb++;
1483
1484                 if(skb->len < SMC_HEADER_SIZE || skb->len > tp->max_packet_size)                        return (-1);
1485
1486                 smctr_enable_16bit(dev);
1487                 smctr_set_page(dev, (__u8 *)tp->ram_access);
1488
1489                 if((fcb = smctr_get_tx_fcb(dev, NON_MAC_QUEUE, skb->len))
1490                         == (FCBlock *)(-1L))
1491                 {
1492                         smctr_disable_16bit(dev);
1493                         return (-1);
1494                 }
1495
1496                 smctr_tx_move_frame(dev, skb,
1497                         (__u8 *)fcb->bdb_ptr->data_block_ptr, skb->len);
1498
1499                 smctr_set_page(dev, (__u8 *)fcb);
1500
1501                 smctr_trc_send_packet(dev, fcb, NON_MAC_QUEUE);
1502                 dev_kfree_skb(skb);
1503
1504                 tstat->tx_packets++;
1505
1506                 smctr_disable_16bit(dev);
1507         }
1508
1509         return (0);
1510 }
1511
1512 static int smctr_init_acbs(struct net_device *dev)
1513 {
1514         struct net_local *tp = netdev_priv(dev);
1515         unsigned int i;
1516         ACBlock *acb;
1517
1518         if(smctr_debug > 10)
1519                 printk(KERN_DEBUG "%s: smctr_init_acbs\n", dev->name);
1520
1521         acb                     = tp->acb_head;
1522         acb->cmd_done_status    = (ACB_COMMAND_DONE | ACB_COMMAND_SUCCESSFUL);
1523         acb->cmd_info           = ACB_CHAIN_END;
1524         acb->cmd                = 0;
1525         acb->subcmd             = 0;
1526         acb->data_offset_lo     = 0;
1527         acb->data_offset_hi     = 0;
1528         acb->next_ptr
1529                 = (ACBlock *)(((char *)acb) + sizeof(ACBlock));
1530         acb->trc_next_ptr       = TRC_POINTER(acb->next_ptr);
1531
1532         for(i = 1; i < tp->num_acbs; i++)
1533         {
1534                 acb             = acb->next_ptr;
1535                 acb->cmd_done_status
1536                         = (ACB_COMMAND_DONE | ACB_COMMAND_SUCCESSFUL);
1537                 acb->cmd_info = ACB_CHAIN_END;
1538                 acb->cmd        = 0;
1539                 acb->subcmd     = 0;
1540                 acb->data_offset_lo = 0;
1541                 acb->data_offset_hi = 0;
1542                 acb->next_ptr
1543                         = (ACBlock *)(((char *)acb) + sizeof(ACBlock));
1544                 acb->trc_next_ptr = TRC_POINTER(acb->next_ptr);
1545         }
1546
1547         acb->next_ptr           = tp->acb_head;
1548         acb->trc_next_ptr       = TRC_POINTER(tp->acb_head);
1549         tp->acb_next            = tp->acb_head->next_ptr;
1550         tp->acb_curr            = tp->acb_head->next_ptr;
1551         tp->num_acbs_used       = 0;
1552
1553         return (0);
1554 }
1555
1556 static int smctr_init_adapter(struct net_device *dev)
1557 {
1558         struct net_local *tp = netdev_priv(dev);
1559         int err;
1560
1561         if(smctr_debug > 10)
1562                 printk(KERN_DEBUG "%s: smctr_init_adapter\n", dev->name);
1563
1564         tp->status              = CLOSED;
1565         tp->page_offset_mask    = (tp->ram_usable * 1024) - 1;
1566         skb_queue_head_init(&tp->SendSkbQueue);
1567         tp->QueueSkb = MAX_TX_QUEUE;
1568
1569         if(!(tp->group_address_0 & 0x0080))
1570                 tp->group_address_0 |= 0x00C0;
1571
1572         if(!(tp->functional_address_0 & 0x00C0))
1573                 tp->functional_address_0 |= 0x00C0;
1574
1575         tp->functional_address[0] &= 0xFF7F;
1576
1577         if(tp->authorized_function_classes == 0)
1578                 tp->authorized_function_classes = 0x7FFF;
1579
1580         if(tp->authorized_access_priority == 0)
1581                 tp->authorized_access_priority = 0x06;
1582
1583         smctr_disable_bic_int(dev);
1584         smctr_set_trc_reset(dev->base_addr);
1585
1586         smctr_enable_16bit(dev);
1587         smctr_set_page(dev, (__u8 *)tp->ram_access);
1588
1589         if(smctr_checksum_firmware(dev))
1590         {
1591                 printk(KERN_ERR "%s: Previously loaded firmware is missing\n",dev->name);                return (-ENOENT);
1592         }
1593
1594         if((err = smctr_ram_memory_test(dev)))
1595         {
1596                 printk(KERN_ERR "%s: RAM memory test failed.\n", dev->name);
1597                 return (-EIO);
1598         }
1599
1600         smctr_set_rx_look_ahead(dev);
1601         smctr_load_node_addr(dev);
1602
1603         /* Initialize adapter for Internal Self Test. */
1604         smctr_reset_adapter(dev);
1605         if((err = smctr_init_card_real(dev)))
1606         {
1607                 printk(KERN_ERR "%s: Initialization of card failed (%d)\n",
1608                         dev->name, err);
1609                 return (-EINVAL);
1610         }
1611
1612         /* This routine clobbers the TRC's internal registers. */
1613         if((err = smctr_internal_self_test(dev)))
1614         {
1615                 printk(KERN_ERR "%s: Card failed internal self test (%d)\n",
1616                         dev->name, err);
1617                 return (-EINVAL);
1618         }
1619
1620         /* Re-Initialize adapter's internal registers */
1621         smctr_reset_adapter(dev);
1622         if((err = smctr_init_card_real(dev)))
1623         {
1624                 printk(KERN_ERR "%s: Initialization of card failed (%d)\n",
1625                         dev->name, err);
1626                 return (-EINVAL);
1627         }
1628
1629         smctr_enable_bic_int(dev);
1630
1631         if((err = smctr_issue_enable_int_cmd(dev, TRC_INTERRUPT_ENABLE_MASK)))
1632                 return (err);
1633
1634         smctr_disable_16bit(dev);
1635
1636         return (0);
1637 }
1638
1639 static int smctr_init_card_real(struct net_device *dev)
1640 {
1641         struct net_local *tp = netdev_priv(dev);
1642         int err = 0;
1643
1644         if(smctr_debug > 10)
1645                 printk(KERN_DEBUG "%s: smctr_init_card_real\n", dev->name);
1646
1647         tp->sh_mem_used = 0;
1648         tp->num_acbs    = NUM_OF_ACBS;
1649
1650         /* Range Check Max Packet Size */
1651         if(tp->max_packet_size < 256)
1652                 tp->max_packet_size = 256;
1653         else
1654         {
1655                 if(tp->max_packet_size > NON_MAC_TX_BUFFER_MEMORY)
1656                         tp->max_packet_size = NON_MAC_TX_BUFFER_MEMORY;
1657         }
1658
1659         tp->num_of_tx_buffs = (NON_MAC_TX_BUFFER_MEMORY
1660                 / tp->max_packet_size) - 1;
1661
1662         if(tp->num_of_tx_buffs > NUM_NON_MAC_TX_FCBS)
1663                 tp->num_of_tx_buffs = NUM_NON_MAC_TX_FCBS;
1664         else
1665         {
1666                 if(tp->num_of_tx_buffs == 0)
1667                         tp->num_of_tx_buffs = 1;
1668         }
1669
1670         /* Tx queue constants */
1671         tp->num_tx_fcbs        [BUG_QUEUE]     = NUM_BUG_TX_FCBS;
1672         tp->num_tx_bdbs        [BUG_QUEUE]     = NUM_BUG_TX_BDBS;
1673         tp->tx_buff_size       [BUG_QUEUE]     = BUG_TX_BUFFER_MEMORY;
1674         tp->tx_buff_used       [BUG_QUEUE]     = 0;
1675         tp->tx_queue_status    [BUG_QUEUE]     = NOT_TRANSMITING;
1676
1677         tp->num_tx_fcbs        [MAC_QUEUE]     = NUM_MAC_TX_FCBS;
1678         tp->num_tx_bdbs        [MAC_QUEUE]     = NUM_MAC_TX_BDBS;
1679         tp->tx_buff_size       [MAC_QUEUE]     = MAC_TX_BUFFER_MEMORY;
1680         tp->tx_buff_used       [MAC_QUEUE]     = 0;
1681         tp->tx_queue_status    [MAC_QUEUE]     = NOT_TRANSMITING;
1682
1683         tp->num_tx_fcbs        [NON_MAC_QUEUE] = NUM_NON_MAC_TX_FCBS;
1684         tp->num_tx_bdbs        [NON_MAC_QUEUE] = NUM_NON_MAC_TX_BDBS;
1685         tp->tx_buff_size       [NON_MAC_QUEUE] = NON_MAC_TX_BUFFER_MEMORY;
1686         tp->tx_buff_used       [NON_MAC_QUEUE] = 0;
1687         tp->tx_queue_status    [NON_MAC_QUEUE] = NOT_TRANSMITING;
1688
1689         /* Receive Queue Constants */
1690         tp->num_rx_fcbs[MAC_QUEUE] = NUM_MAC_RX_FCBS;
1691         tp->num_rx_bdbs[MAC_QUEUE] = NUM_MAC_RX_BDBS;
1692
1693         if(tp->extra_info & CHIP_REV_MASK)
1694                 tp->num_rx_fcbs[NON_MAC_QUEUE] = 78;    /* 825 Rev. XE */
1695         else
1696                 tp->num_rx_fcbs[NON_MAC_QUEUE] = 7;     /* 825 Rev. XD */
1697
1698         tp->num_rx_bdbs[NON_MAC_QUEUE] = smctr_get_num_rx_bdbs(dev);
1699
1700         smctr_alloc_shared_memory(dev);
1701         smctr_init_shared_memory(dev);
1702
1703         if((err = smctr_issue_init_timers_cmd(dev)))
1704                 return (err);
1705
1706         if((err = smctr_issue_init_txrx_cmd(dev)))
1707         {
1708                 printk(KERN_ERR "%s: Hardware failure\n", dev->name);
1709                 return (err);
1710         }
1711
1712         return (0);
1713 }
1714
1715 static int smctr_init_rx_bdbs(struct net_device *dev)
1716 {
1717         struct net_local *tp = netdev_priv(dev);
1718         unsigned int i, j;
1719         BDBlock *bdb;
1720         __u16 *buf;
1721
1722         if(smctr_debug > 10)
1723                 printk(KERN_DEBUG "%s: smctr_init_rx_bdbs\n", dev->name);
1724
1725         for(i = 0; i < NUM_RX_QS_USED; i++)
1726         {
1727                 bdb = tp->rx_bdb_head[i];
1728                 buf = tp->rx_buff_head[i];
1729                 bdb->info = (BDB_CHAIN_END | BDB_NO_WARNING);
1730                 bdb->buffer_length = RX_DATA_BUFFER_SIZE;
1731                 bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
1732                 bdb->data_block_ptr = buf;
1733                 bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);
1734
1735                 if(i == NON_MAC_QUEUE)
1736                         bdb->trc_data_block_ptr = RX_BUFF_TRC_POINTER(buf);
1737                 else
1738                         bdb->trc_data_block_ptr = TRC_POINTER(buf);
1739
1740                 for(j = 1; j < tp->num_rx_bdbs[i]; j++)
1741                 {
1742                         bdb->next_ptr->back_ptr = bdb;
1743                         bdb = bdb->next_ptr;
1744                         buf = (__u16 *)((char *)buf + RX_DATA_BUFFER_SIZE);
1745                         bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
1746                         bdb->buffer_length = RX_DATA_BUFFER_SIZE;
1747                         bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
1748                         bdb->data_block_ptr = buf;
1749                         bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);
1750
1751                         if(i == NON_MAC_QUEUE)
1752                                 bdb->trc_data_block_ptr = RX_BUFF_TRC_POINTER(buf);
1753                         else
1754                                 bdb->trc_data_block_ptr = TRC_POINTER(buf);
1755                 }
1756
1757                 bdb->next_ptr           = tp->rx_bdb_head[i];
1758                 bdb->trc_next_ptr       = TRC_POINTER(tp->rx_bdb_head[i]);
1759
1760                 tp->rx_bdb_head[i]->back_ptr    = bdb;
1761                 tp->rx_bdb_curr[i]              = tp->rx_bdb_head[i]->next_ptr;
1762         }
1763
1764         return (0);
1765 }
1766
1767 static int smctr_init_rx_fcbs(struct net_device *dev)
1768 {
1769         struct net_local *tp = netdev_priv(dev);
1770         unsigned int i, j;
1771         FCBlock *fcb;
1772
1773         for(i = 0; i < NUM_RX_QS_USED; i++)
1774         {
1775                 fcb               = tp->rx_fcb_head[i];
1776                 fcb->frame_status = 0;
1777                 fcb->frame_length = 0;
1778                 fcb->info         = FCB_CHAIN_END;
1779                 fcb->next_ptr     = (FCBlock *)(((char*)fcb) + sizeof(FCBlock));
1780                 if(i == NON_MAC_QUEUE)
1781                         fcb->trc_next_ptr = RX_FCB_TRC_POINTER(fcb->next_ptr);
1782                 else
1783                         fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);
1784
1785                 for(j = 1; j < tp->num_rx_fcbs[i]; j++)
1786                 {
1787                         fcb->next_ptr->back_ptr = fcb;
1788                         fcb                     = fcb->next_ptr;
1789                         fcb->frame_status       = 0;
1790                         fcb->frame_length       = 0;
1791                         fcb->info               = FCB_WARNING;
1792                         fcb->next_ptr
1793                                 = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));
1794
1795                         if(i == NON_MAC_QUEUE)
1796                                 fcb->trc_next_ptr
1797                                         = RX_FCB_TRC_POINTER(fcb->next_ptr);
1798                         else
1799                                 fcb->trc_next_ptr
1800                                         = TRC_POINTER(fcb->next_ptr);
1801                 }
1802
1803                 fcb->next_ptr = tp->rx_fcb_head[i];
1804
1805                 if(i == NON_MAC_QUEUE)
1806                         fcb->trc_next_ptr = RX_FCB_TRC_POINTER(fcb->next_ptr);
1807                 else
1808                         fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);
1809
1810                 tp->rx_fcb_head[i]->back_ptr    = fcb;
1811                 tp->rx_fcb_curr[i]              = tp->rx_fcb_head[i]->next_ptr;
1812         }
1813
1814         return(0);
1815 }
1816
1817 static int smctr_init_shared_memory(struct net_device *dev)
1818 {
1819         struct net_local *tp = netdev_priv(dev);
1820         unsigned int i;
1821         __u32 *iscpb;
1822
1823         if(smctr_debug > 10)
1824                 printk(KERN_DEBUG "%s: smctr_init_shared_memory\n", dev->name);
1825
1826         smctr_set_page(dev, (__u8 *)(unsigned int)tp->iscpb_ptr);
1827
1828         /* Initialize Initial System Configuration Point. (ISCP) */
1829         iscpb = (__u32 *)PAGE_POINTER(&tp->iscpb_ptr->trc_scgb_ptr);
1830         *iscpb = (__u32)(SWAP_WORDS(TRC_POINTER(tp->scgb_ptr)));
1831
1832         smctr_set_page(dev, (__u8 *)tp->ram_access);
1833
1834         /* Initialize System Configuration Pointers. (SCP) */
1835         tp->scgb_ptr->config = (SCGB_ADDRESS_POINTER_FORMAT
1836                 | SCGB_MULTI_WORD_CONTROL | SCGB_DATA_FORMAT
1837                 | SCGB_BURST_LENGTH);
1838
1839         tp->scgb_ptr->trc_sclb_ptr      = TRC_POINTER(tp->sclb_ptr);
1840         tp->scgb_ptr->trc_acb_ptr       = TRC_POINTER(tp->acb_head);
1841         tp->scgb_ptr->trc_isb_ptr       = TRC_POINTER(tp->isb_ptr);
1842         tp->scgb_ptr->isbsiz            = (sizeof(ISBlock)) - 2;
1843
1844         /* Initialize System Control Block. (SCB) */
1845         tp->sclb_ptr->valid_command    = SCLB_VALID | SCLB_CMD_NOP;
1846         tp->sclb_ptr->iack_code        = 0;
1847         tp->sclb_ptr->resume_control   = 0;
1848         tp->sclb_ptr->int_mask_control = 0;
1849         tp->sclb_ptr->int_mask_state   = 0;
1850
1851         /* Initialize Interrupt Status Block. (ISB) */
1852         for(i = 0; i < NUM_OF_INTERRUPTS; i++)
1853         {
1854                 tp->isb_ptr->IStatus[i].IType = 0xf0;
1855                 tp->isb_ptr->IStatus[i].ISubtype = 0;
1856         }
1857
1858         tp->current_isb_index = 0;
1859
1860         /* Initialize Action Command Block. (ACB) */
1861         smctr_init_acbs(dev);
1862
1863         /* Initialize transmit FCB's and BDB's. */
1864         smctr_link_tx_fcbs_to_bdbs(dev);
1865         smctr_init_tx_bdbs(dev);
1866         smctr_init_tx_fcbs(dev);
1867
1868         /* Initialize receive FCB's and BDB's. */
1869         smctr_init_rx_bdbs(dev);
1870         smctr_init_rx_fcbs(dev);
1871
1872         return (0);
1873 }
1874
1875 static int smctr_init_tx_bdbs(struct net_device *dev)
1876 {
1877         struct net_local *tp = netdev_priv(dev);
1878         unsigned int i, j;
1879         BDBlock *bdb;
1880
1881         for(i = 0; i < NUM_TX_QS_USED; i++)
1882         {
1883                 bdb = tp->tx_bdb_head[i];
1884                 bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
1885                 bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
1886                 bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);
1887
1888                 for(j = 1; j < tp->num_tx_bdbs[i]; j++)
1889                 {
1890                         bdb->next_ptr->back_ptr = bdb;
1891                         bdb = bdb->next_ptr;
1892                         bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
1893                         bdb->next_ptr
1894                                 = (BDBlock *)(((char *)bdb) + sizeof( BDBlock));                        bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);
1895                 }
1896
1897                 bdb->next_ptr = tp->tx_bdb_head[i];
1898                 bdb->trc_next_ptr = TRC_POINTER(tp->tx_bdb_head[i]);
1899                 tp->tx_bdb_head[i]->back_ptr = bdb;
1900         }
1901
1902         return (0);
1903 }
1904
1905 static int smctr_init_tx_fcbs(struct net_device *dev)
1906 {
1907         struct net_local *tp = netdev_priv(dev);
1908         unsigned int i, j;
1909         FCBlock *fcb;
1910
1911         for(i = 0; i < NUM_TX_QS_USED; i++)
1912         {
1913                 fcb               = tp->tx_fcb_head[i];
1914                 fcb->frame_status = 0;
1915                 fcb->frame_length = 0;
1916                 fcb->info         = FCB_CHAIN_END;
1917                 fcb->next_ptr = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));
1918                 fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);
1919
1920                 for(j = 1; j < tp->num_tx_fcbs[i]; j++)
1921                 {
1922                         fcb->next_ptr->back_ptr = fcb;
1923                         fcb                     = fcb->next_ptr;
1924                         fcb->frame_status       = 0;
1925                         fcb->frame_length       = 0;
1926                         fcb->info               = FCB_CHAIN_END;
1927                         fcb->next_ptr
1928                                 = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));
1929                         fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);
1930                 }
1931
1932                 fcb->next_ptr           = tp->tx_fcb_head[i];
1933                 fcb->trc_next_ptr       = TRC_POINTER(tp->tx_fcb_head[i]);
1934
1935                 tp->tx_fcb_head[i]->back_ptr    = fcb;
1936                 tp->tx_fcb_end[i]               = tp->tx_fcb_head[i]->next_ptr;
1937                 tp->tx_fcb_curr[i]              = tp->tx_fcb_head[i]->next_ptr;
1938                 tp->num_tx_fcbs_used[i]         = 0;
1939         }
1940
1941         return (0);
1942 }
1943
1944 static int smctr_internal_self_test(struct net_device *dev)
1945 {
1946         struct net_local *tp = netdev_priv(dev);
1947         int err;
1948
1949         if((err = smctr_issue_test_internal_rom_cmd(dev)))
1950                 return (err);
1951
1952         if((err = smctr_wait_cmd(dev)))
1953                 return (err);
1954
1955         if(tp->acb_head->cmd_done_status & 0xff)
1956                 return (-1);
1957
1958         if((err = smctr_issue_test_hic_cmd(dev)))
1959                 return (err);
1960
1961         if((err = smctr_wait_cmd(dev)))
1962                 return (err);
1963
1964         if(tp->acb_head->cmd_done_status & 0xff)
1965                 return (-1);
1966
1967         if((err = smctr_issue_test_mac_reg_cmd(dev)))
1968                 return (err);
1969
1970         if((err = smctr_wait_cmd(dev)))
1971                 return (err);
1972
1973         if(tp->acb_head->cmd_done_status & 0xff)
1974                 return (-1);
1975
1976         return (0);
1977 }
1978
1979 /*
1980  * The typical workload of the driver: Handle the network interface interrupts.
1981  */
1982 static irqreturn_t smctr_interrupt(int irq, void *dev_id)
1983 {
1984         struct net_device *dev = dev_id;
1985         struct net_local *tp;
1986         int ioaddr;
1987         __u16 interrupt_unmask_bits = 0, interrupt_ack_code = 0xff00;
1988         __u16 err1, err = NOT_MY_INTERRUPT;
1989         __u8 isb_type, isb_subtype;
1990         __u16 isb_index;
1991
1992         ioaddr = dev->base_addr;
1993         tp = netdev_priv(dev);
1994
1995         if(tp->status == NOT_INITIALIZED)
1996                 return IRQ_NONE;
1997
1998         spin_lock(&tp->lock);
1999         
2000         smctr_disable_bic_int(dev);
2001         smctr_enable_16bit(dev);
2002
2003         smctr_clear_int(dev);
2004
2005         /* First read the LSB */
2006         while((tp->isb_ptr->IStatus[tp->current_isb_index].IType & 0xf0) == 0)
2007         {
2008                 isb_index       = tp->current_isb_index;
2009                 isb_type        = tp->isb_ptr->IStatus[isb_index].IType;
2010                 isb_subtype     = tp->isb_ptr->IStatus[isb_index].ISubtype;
2011
2012                 (tp->current_isb_index)++;
2013                 if(tp->current_isb_index == NUM_OF_INTERRUPTS)
2014                         tp->current_isb_index = 0;
2015
2016                 if(isb_type >= 0x10)
2017                 {
2018                         smctr_disable_16bit(dev);
2019                         spin_unlock(&tp->lock);
2020                         return IRQ_HANDLED;
2021                 }
2022
2023                 err = HARDWARE_FAILED;
2024                 interrupt_ack_code = isb_index;
2025                 tp->isb_ptr->IStatus[isb_index].IType |= 0xf0;
2026
2027                 interrupt_unmask_bits |= (1 << (__u16)isb_type);
2028
2029                 switch(isb_type)
2030                 {
2031                         case ISB_IMC_MAC_TYPE_3:
2032                                 smctr_disable_16bit(dev);
2033
2034                                 switch(isb_subtype)
2035                                 {
2036                                         case 0:
2037                                                 tp->monitor_state = MS_MONITOR_FSM_INACTIVE;
2038                                                break;
2039
2040                                         case 1:
2041                                                 tp->monitor_state = MS_REPEAT_BEACON_STATE;
2042                                                 break;
2043
2044                                         case 2:
2045                                                 tp->monitor_state = MS_REPEAT_CLAIM_TOKEN_STATE;
2046                                                 break;
2047
2048                                         case 3:
2049                                                 tp->monitor_state = MS_TRANSMIT_CLAIM_TOKEN_STATE;                                                break;
2050
2051                                         case 4:
2052                                                 tp->monitor_state = MS_STANDBY_MONITOR_STATE;
2053                                                 break;
2054
2055                                         case 5:
2056                                                 tp->monitor_state = MS_TRANSMIT_BEACON_STATE;
2057                                                 break;
2058
2059                                         case 6:
2060                                                 tp->monitor_state = MS_ACTIVE_MONITOR_STATE;
2061                                                 break;
2062
2063                                         case 7:
2064                                                 tp->monitor_state = MS_TRANSMIT_RING_PURGE_STATE;
2065                                                 break;
2066
2067                                         case 8:   /* diagnostic state */
2068                                                 break;
2069
2070                                         case 9:
2071                                                 tp->monitor_state = MS_BEACON_TEST_STATE;
2072                                                 if(smctr_lobe_media_test(dev))
2073                                                 {
2074                                                         tp->ring_status_flags = RING_STATUS_CHANGED;
2075                                                         tp->ring_status = AUTO_REMOVAL_ERROR;
2076                                                         smctr_ring_status_chg(dev);
2077                                                         smctr_bypass_state(dev);
2078                                                 }
2079                                                 else
2080                                                         smctr_issue_insert_cmd(dev);
2081                                                 break;
2082
2083                                         /* case 0x0a-0xff, illegal states */
2084                                         default:
2085                                                 break;
2086                                 }
2087
2088                                 tp->ring_status_flags = MONITOR_STATE_CHANGED;
2089                                 err = smctr_ring_status_chg(dev);
2090
2091                                 smctr_enable_16bit(dev);
2092                                 break;
2093
2094                         /* Type 0x02 - MAC Error Counters Interrupt
2095                          * One or more MAC Error Counter is half full
2096                          *      MAC Error Counters
2097                          *      Lost_FR_Error_Counter
2098                          *      RCV_Congestion_Counter
2099                          *      FR_copied_Error_Counter
2100                          *      FREQ_Error_Counter
2101                          *      Token_Error_Counter
2102                          *      Line_Error_Counter
2103                          *      Internal_Error_Count
2104                          */
2105                         case ISB_IMC_MAC_ERROR_COUNTERS:
2106                                 /* Read 802.5 Error Counters */
2107                                 err = smctr_issue_read_ring_status_cmd(dev);
2108                                 break;
2109
2110                         /* Type 0x04 - MAC Type 2 Interrupt
2111                          * HOST needs to enqueue MAC Frame for transmission
2112                          * SubType Bit 15 - RQ_INIT_PDU( Request Initialization)                         * Changed from RQ_INIT_PDU to
2113                          * TRC_Status_Changed_Indicate
2114                          */
2115                         case ISB_IMC_MAC_TYPE_2:
2116                                 err = smctr_issue_read_ring_status_cmd(dev);
2117                                 break;
2118
2119
2120                         /* Type 0x05 - TX Frame Interrupt (FI). */
2121                         case ISB_IMC_TX_FRAME:
2122                                 /* BUG QUEUE for TRC stuck receive BUG */
2123                                 if(isb_subtype & TX_PENDING_PRIORITY_2)
2124                                 {
2125                                         if((err = smctr_tx_complete(dev, BUG_QUEUE)) != SUCCESS)
2126                                                 break;
2127                                 }
2128
2129                                 /* NON-MAC frames only */
2130                                 if(isb_subtype & TX_PENDING_PRIORITY_1)
2131                                 {
2132                                         if((err = smctr_tx_complete(dev, NON_MAC_QUEUE)) != SUCCESS)
2133                                                 break;
2134                                 }
2135
2136                                 /* MAC frames only */
2137                                 if(isb_subtype & TX_PENDING_PRIORITY_0)
2138                                         err = smctr_tx_complete(dev, MAC_QUEUE);                                break;
2139
2140                         /* Type 0x06 - TX END OF QUEUE (FE) */
2141                         case ISB_IMC_END_OF_TX_QUEUE:
2142                                 /* BUG queue */
2143                                 if(isb_subtype & TX_PENDING_PRIORITY_2)
2144                                 {
2145                                         /* ok to clear Receive FIFO overrun
2146                                          * imask send_BUG now completes.
2147                                          */
2148                                         interrupt_unmask_bits |= 0x800;
2149
2150                                         tp->tx_queue_status[BUG_QUEUE] = NOT_TRANSMITING;
2151                                         if((err = smctr_tx_complete(dev, BUG_QUEUE)) != SUCCESS)
2152                                                 break;
2153                                         if((err = smctr_restart_tx_chain(dev, BUG_QUEUE)) != SUCCESS)
2154                                                 break;
2155                                 }
2156
2157                                 /* NON-MAC queue only */
2158                                 if(isb_subtype & TX_PENDING_PRIORITY_1)
2159                                 {
2160                                         tp->tx_queue_status[NON_MAC_QUEUE] = NOT_TRANSMITING;
2161                                         if((err = smctr_tx_complete(dev, NON_MAC_QUEUE)) != SUCCESS)
2162                                                 break;
2163                                         if((err = smctr_restart_tx_chain(dev, NON_MAC_QUEUE)) != SUCCESS)
2164                                                 break;
2165                                 }
2166
2167                                 /* MAC queue only */
2168                                 if(isb_subtype & TX_PENDING_PRIORITY_0)
2169                                 {
2170                                         tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
2171                                         if((err = smctr_tx_complete(dev, MAC_QUEUE)) != SUCCESS)
2172                                                 break;
2173
2174                                         err = smctr_restart_tx_chain(dev, MAC_QUEUE);
2175                                 }
2176                                 break;
2177
2178                         /* Type 0x07 - NON-MAC RX Resource Interrupt
2179                          *   Subtype bit 12 - (BW) BDB warning
2180                          *   Subtype bit 13 - (FW) FCB warning
2181                          *   Subtype bit 14 - (BE) BDB End of chain
2182                          *   Subtype bit 15 - (FE) FCB End of chain
2183                          */
2184                         case ISB_IMC_NON_MAC_RX_RESOURCE:
2185                                 tp->rx_fifo_overrun_count = 0;
2186                                 tp->receive_queue_number = NON_MAC_QUEUE;
2187                                 err1 = smctr_rx_frame(dev);
2188
2189                                 if(isb_subtype & NON_MAC_RX_RESOURCE_FE)
2190                                 {
2191                                         if((err = smctr_issue_resume_rx_fcb_cmd(                                                dev, NON_MAC_QUEUE)) != SUCCESS)                                                break;
2192
2193                                         if(tp->ptr_rx_fcb_overruns)
2194                                                 (*tp->ptr_rx_fcb_overruns)++;
2195                                 }
2196
2197                                 if(isb_subtype & NON_MAC_RX_RESOURCE_BE)
2198                                 {
2199                                         if((err = smctr_issue_resume_rx_bdb_cmd(                                                dev, NON_MAC_QUEUE)) != SUCCESS)                                                break;
2200
2201                                         if(tp->ptr_rx_bdb_overruns)
2202                                                 (*tp->ptr_rx_bdb_overruns)++;
2203                                 }
2204                                 err = err1;
2205                                 break;
2206
2207                         /* Type 0x08 - MAC RX Resource Interrupt
2208                          *   Subtype bit 12 - (BW) BDB warning
2209                          *   Subtype bit 13 - (FW) FCB warning
2210                          *   Subtype bit 14 - (BE) BDB End of chain
2211                          *   Subtype bit 15 - (FE) FCB End of chain
2212                          */
2213                         case ISB_IMC_MAC_RX_RESOURCE:
2214                                 tp->receive_queue_number = MAC_QUEUE;
2215                                 err1 = smctr_rx_frame(dev);
2216
2217                                 if(isb_subtype & MAC_RX_RESOURCE_FE)
2218                                 {
2219                                         if((err = smctr_issue_resume_rx_fcb_cmd(                                                dev, MAC_QUEUE)) != SUCCESS)
2220                                                 break;
2221
2222                                         if(tp->ptr_rx_fcb_overruns)
2223                                                 (*tp->ptr_rx_fcb_overruns)++;
2224                                 }
2225
2226                                 if(isb_subtype & MAC_RX_RESOURCE_BE)
2227                                 {
2228                                         if((err = smctr_issue_resume_rx_bdb_cmd(                                                dev, MAC_QUEUE)) != SUCCESS)
2229                                                 break;
2230
2231                                         if(tp->ptr_rx_bdb_overruns)
2232                                                 (*tp->ptr_rx_bdb_overruns)++;
2233                                 }
2234                                 err = err1;
2235                                 break;
2236
2237                         /* Type 0x09 - NON_MAC RX Frame Interrupt */
2238                         case ISB_IMC_NON_MAC_RX_FRAME:
2239                                 tp->rx_fifo_overrun_count = 0;
2240                                 tp->receive_queue_number = NON_MAC_QUEUE;
2241                                 err = smctr_rx_frame(dev);
2242                                 break;
2243
2244                         /* Type 0x0A - MAC RX Frame Interrupt */
2245                         case ISB_IMC_MAC_RX_FRAME:
2246                                 tp->receive_queue_number = MAC_QUEUE;
2247                                 err = smctr_rx_frame(dev);
2248                                 break;
2249
2250                         /* Type 0x0B - TRC status
2251                          * TRC has encountered an error condition
2252                          * subtype bit 14 - transmit FIFO underrun
2253                          * subtype bit 15 - receive FIFO overrun
2254                          */
2255                         case ISB_IMC_TRC_FIFO_STATUS:
2256                                 if(isb_subtype & TRC_FIFO_STATUS_TX_UNDERRUN)
2257                                 {
2258                                         if(tp->ptr_tx_fifo_underruns)
2259                                                 (*tp->ptr_tx_fifo_underruns)++;
2260                                 }
2261
2262                                 if(isb_subtype & TRC_FIFO_STATUS_RX_OVERRUN)
2263                                 {
2264                                         /* update overrun stuck receive counter
2265                                          * if >= 3, has to clear it by sending
2266                                          * back to back frames. We pick
2267                                          * DAT(duplicate address MAC frame)
2268                                          */
2269                                         tp->rx_fifo_overrun_count++;
2270
2271                                         if(tp->rx_fifo_overrun_count >= 3)
2272                                         {
2273                                                 tp->rx_fifo_overrun_count = 0;
2274
2275                                                 /* delay clearing fifo overrun
2276                                                  * imask till send_BUG tx
2277                                                  * complete posted
2278                                                  */
2279                                                 interrupt_unmask_bits &= (~0x800);
2280                                                 printk(KERN_CRIT "Jay please send bug\n");//                                              smctr_send_bug(dev);
2281                                         }
2282
2283                                         if(tp->ptr_rx_fifo_overruns)
2284                                                 (*tp->ptr_rx_fifo_overruns)++;
2285                                 }
2286
2287                                 err = SUCCESS;
2288                                 break;
2289
2290                         /* Type 0x0C - Action Command Status Interrupt
2291                          * Subtype bit 14 - CB end of command chain (CE)
2292                          * Subtype bit 15 - CB command interrupt (CI)
2293                          */
2294                         case ISB_IMC_COMMAND_STATUS:
2295                                 err = SUCCESS;
2296                                 if(tp->acb_head->cmd == ACB_CMD_HIC_NOP)
2297                                 {
2298                                         printk(KERN_ERR "i1\n");
2299                                         smctr_disable_16bit(dev);
2300
2301                                         /* XXXXXXXXXXXXXXXXX */
2302                                 /*      err = UM_Interrupt(dev); */
2303
2304                                         smctr_enable_16bit(dev);
2305                                 }
2306                                 else
2307                                 {
2308                                         if((tp->acb_head->cmd
2309                                                 == ACB_CMD_READ_TRC_STATUS)
2310                                                 && (tp->acb_head->subcmd
2311                                                 == RW_TRC_STATUS_BLOCK))
2312                                         {
2313                                                 if(tp->ptr_bcn_type)
2314                                                 {
2315                                                         *(tp->ptr_bcn_type)
2316                                                                 = (__u32)((SBlock *)tp->misc_command_data)->BCN_Type;
2317                                                 }
2318
2319                                                 if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & ERROR_COUNTERS_CHANGED)
2320                                                 {
2321                                                         smctr_update_err_stats(dev);
2322                                                 }
2323
2324                                                 if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & TI_NDIS_RING_STATUS_CHANGED)
2325                                                 {
2326                                                         tp->ring_status
2327                                                                 = ((SBlock*)tp->misc_command_data)->TI_NDIS_Ring_Status;
2328                                                         smctr_disable_16bit(dev);
2329                                                         err = smctr_ring_status_chg(dev);
2330                                                         smctr_enable_16bit(dev);
2331                                                         if((tp->ring_status & REMOVE_RECEIVED)
2332                                                                 && (tp->config_word0 & NO_AUTOREMOVE))
2333                                                         {
2334                                                                 smctr_issue_remove_cmd(dev);
2335                                                         }
2336
2337                                                         if(err != SUCCESS)
2338                                                         {
2339                                                                 tp->acb_pending = 0;
2340                                                                 break;
2341                                                         }
2342                                                 }
2343
2344                                                 if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & UNA_CHANGED)
2345                                                 {
2346                                                         if(tp->ptr_una)
2347                                                         {
2348                                                                 tp->ptr_una[0] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[0]);
2349                                                                 tp->ptr_una[1] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[1]);
2350                                                                 tp->ptr_una[2] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[2]);
2351                                                         }
2352
2353                                                 }
2354
2355                                                 if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & READY_TO_SEND_RQ_INIT)                                                {
2356                                                         err = smctr_send_rq_init(dev);
2357                                                 }
2358                                         }
2359                                 }
2360
2361                                 tp->acb_pending = 0;
2362                                 break;
2363
2364                         /* Type 0x0D - MAC Type 1 interrupt
2365                          * Subtype -- 00 FR_BCN received at S12
2366                          *            01 FR_BCN received at S21
2367                          *            02 FR_DAT(DA=MA, A<>0) received at S21
2368                          *            03 TSM_EXP at S21
2369                          *            04 FR_REMOVE received at S42
2370                          *            05 TBR_EXP, BR_FLAG_SET at S42
2371                          *            06 TBT_EXP at S53
2372                          */
2373                         case ISB_IMC_MAC_TYPE_1:
2374                                 if(isb_subtype > 8)
2375                                 {
2376                                         err = HARDWARE_FAILED;
2377                                         break;
2378                                 }
2379
2380                                 err = SUCCESS;
2381                                 switch(isb_subtype)
2382                                 {
2383                                         case 0:
2384                                                 tp->join_state = JS_BYPASS_STATE;
2385                                                 if(tp->status != CLOSED)
2386                                                 {
2387                                                         tp->status = CLOSED;
2388                                                         err = smctr_status_chg(dev);
2389                                                 }
2390                                                 break;
2391
2392                                         case 1:
2393                                                 tp->join_state = JS_LOBE_TEST_STATE;
2394                                                 break;
2395
2396                                         case 2:
2397                                                 tp->join_state = JS_DETECT_MONITOR_PRESENT_STATE;
2398                                                 break;
2399
2400                                         case 3:
2401                                                 tp->join_state = JS_AWAIT_NEW_MONITOR_STATE;
2402                                                 break;
2403
2404                                         case 4:
2405                                                 tp->join_state = JS_DUPLICATE_ADDRESS_TEST_STATE;
2406                                                 break;
2407
2408                                         case 5:
2409                                                 tp->join_state = JS_NEIGHBOR_NOTIFICATION_STATE;
2410                                                 break;
2411
2412                                         case 6:
2413                                                 tp->join_state = JS_REQUEST_INITIALIZATION_STATE;
2414                                                 break;
2415
2416                                         case 7:
2417                                                 tp->join_state = JS_JOIN_COMPLETE_STATE;
2418                                                 tp->status = OPEN;
2419                                                 err = smctr_status_chg(dev);
2420                                                 break;
2421
2422                                         case 8:
2423                                                 tp->join_state = JS_BYPASS_WAIT_STATE;
2424                                                 break;
2425                                 }
2426                                 break ;
2427
2428                         /* Type 0x0E - TRC Initialization Sequence Interrupt
2429                          * Subtype -- 00-FF Initializatin sequence complete
2430                          */
2431                         case ISB_IMC_TRC_INTRNL_TST_STATUS:
2432                                 tp->status = INITIALIZED;
2433                                 smctr_disable_16bit(dev);
2434                                 err = smctr_status_chg(dev);
2435                                 smctr_enable_16bit(dev);
2436                                 break;
2437
2438                         /* other interrupt types, illegal */
2439                         default:
2440                                 break;
2441                 }
2442
2443                 if(err != SUCCESS)
2444                         break;
2445         }
2446
2447         /* Checking the ack code instead of the unmask bits here is because :
2448          * while fixing the stuck receive, DAT frame are sent and mask off
2449          * FIFO overrun interrupt temporarily (interrupt_unmask_bits = 0)
2450          * but we still want to issue ack to ISB
2451          */
2452         if(!(interrupt_ack_code & 0xff00))
2453                 smctr_issue_int_ack(dev, interrupt_ack_code, interrupt_unmask_bits);
2454
2455         smctr_disable_16bit(dev);
2456         smctr_enable_bic_int(dev);
2457         spin_unlock(&tp->lock);
2458
2459         return IRQ_HANDLED;
2460 }
2461
2462 static int smctr_issue_enable_int_cmd(struct net_device *dev,
2463         __u16 interrupt_enable_mask)
2464 {
2465         struct net_local *tp = netdev_priv(dev);
2466         int err;
2467
2468         if((err = smctr_wait_while_cbusy(dev)))
2469                 return (err);
2470
2471         tp->sclb_ptr->int_mask_control  = interrupt_enable_mask;
2472         tp->sclb_ptr->valid_command     = SCLB_VALID | SCLB_CMD_CLEAR_INTERRUPT_MASK;
2473
2474         smctr_set_ctrl_attention(dev);
2475
2476         return (0);
2477 }
2478
2479 static int smctr_issue_int_ack(struct net_device *dev, __u16 iack_code, __u16 ibits)
2480 {
2481         struct net_local *tp = netdev_priv(dev);
2482
2483         if(smctr_wait_while_cbusy(dev))
2484                 return (-1);
2485
2486         tp->sclb_ptr->int_mask_control = ibits;
2487         tp->sclb_ptr->iack_code = iack_code << 1; /* use the offset from base */        tp->sclb_ptr->resume_control = 0;
2488         tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_IACK_CODE_VALID | SCLB_CMD_CLEAR_INTERRUPT_MASK;
2489
2490         smctr_set_ctrl_attention(dev);
2491
2492         return (0);
2493 }
2494
2495 static int smctr_issue_init_timers_cmd(struct net_device *dev)
2496 {
2497         struct net_local *tp = netdev_priv(dev);
2498         unsigned int i;
2499         int err;
2500         __u16 *pTimer_Struc = (__u16 *)tp->misc_command_data;
2501
2502         if((err = smctr_wait_while_cbusy(dev)))
2503                 return (err);
2504
2505         if((err = smctr_wait_cmd(dev)))
2506                 return (err);
2507
2508         tp->config_word0 = THDREN | DMA_TRIGGER | USETPT | NO_AUTOREMOVE;
2509         tp->config_word1 = 0;
2510
2511         if((tp->media_type == MEDIA_STP_16)
2512                 || (tp->media_type == MEDIA_UTP_16)
2513                 || (tp->media_type == MEDIA_STP_16_UTP_16))
2514         {
2515                 tp->config_word0 |= FREQ_16MB_BIT;
2516         }
2517
2518         if(tp->mode_bits & EARLY_TOKEN_REL)
2519                 tp->config_word0 |= ETREN;
2520
2521         if(tp->mode_bits & LOOPING_MODE_MASK)
2522                 tp->config_word0 |= RX_OWN_BIT;
2523         else
2524                 tp->config_word0 &= ~RX_OWN_BIT;
2525
2526         if(tp->receive_mask & PROMISCUOUS_MODE)
2527                 tp->config_word0 |= PROMISCUOUS_BIT;
2528         else
2529                 tp->config_word0 &= ~PROMISCUOUS_BIT;
2530
2531         if(tp->receive_mask & ACCEPT_ERR_PACKETS)
2532                 tp->config_word0 |= SAVBAD_BIT;
2533         else
2534                 tp->config_word0 &= ~SAVBAD_BIT;
2535
2536         if(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
2537                 tp->config_word0 |= RXATMAC;
2538         else
2539                 tp->config_word0 &= ~RXATMAC;
2540
2541         if(tp->receive_mask & ACCEPT_MULTI_PROM)
2542                 tp->config_word1 |= MULTICAST_ADDRESS_BIT;
2543         else
2544                 tp->config_word1 &= ~MULTICAST_ADDRESS_BIT;
2545
2546         if(tp->receive_mask & ACCEPT_SOURCE_ROUTING_SPANNING)
2547                 tp->config_word1 |= SOURCE_ROUTING_SPANNING_BITS;
2548         else
2549         {
2550                 if(tp->receive_mask & ACCEPT_SOURCE_ROUTING)
2551                         tp->config_word1 |= SOURCE_ROUTING_EXPLORER_BIT;
2552                 else
2553                         tp->config_word1 &= ~SOURCE_ROUTING_SPANNING_BITS;
2554         }
2555
2556         if((tp->media_type == MEDIA_STP_16)
2557                 || (tp->media_type == MEDIA_UTP_16)
2558                 || (tp->media_type == MEDIA_STP_16_UTP_16))
2559         {
2560                 tp->config_word1 |= INTERFRAME_SPACING_16;
2561         }
2562         else
2563                 tp->config_word1 |= INTERFRAME_SPACING_4;
2564
2565         *pTimer_Struc++ = tp->config_word0;
2566         *pTimer_Struc++ = tp->config_word1;
2567
2568         if((tp->media_type == MEDIA_STP_4)
2569                 || (tp->media_type == MEDIA_UTP_4)
2570                 || (tp->media_type == MEDIA_STP_4_UTP_4))
2571         {
2572                 *pTimer_Struc++ = 0x00FA;       /* prescale */
2573                 *pTimer_Struc++ = 0x2710;       /* TPT_limit */
2574                 *pTimer_Struc++ = 0x2710;       /* TQP_limit */
2575                 *pTimer_Struc++ = 0x0A28;       /* TNT_limit */
2576                 *pTimer_Struc++ = 0x3E80;       /* TBT_limit */
2577                 *pTimer_Struc++ = 0x3A98;       /* TSM_limit */
2578                 *pTimer_Struc++ = 0x1B58;       /* TAM_limit */
2579                 *pTimer_Struc++ = 0x00C8;       /* TBR_limit */
2580                 *pTimer_Struc++ = 0x07D0;       /* TER_limit */
2581                 *pTimer_Struc++ = 0x000A;       /* TGT_limit */
2582                 *pTimer_Struc++ = 0x1162;       /* THT_limit */
2583                 *pTimer_Struc++ = 0x07D0;       /* TRR_limit */
2584                 *pTimer_Struc++ = 0x1388;       /* TVX_limit */
2585                 *pTimer_Struc++ = 0x0000;       /* reserved */
2586         }
2587         else
2588         {
2589                 *pTimer_Struc++ = 0x03E8;       /* prescale */
2590                 *pTimer_Struc++ = 0x9C40;       /* TPT_limit */
2591                 *pTimer_Struc++ = 0x9C40;       /* TQP_limit */
2592                 *pTimer_Struc++ = 0x0A28;       /* TNT_limit */
2593                 *pTimer_Struc++ = 0x3E80;       /* TBT_limit */
2594                 *pTimer_Struc++ = 0x3A98;       /* TSM_limit */
2595                 *pTimer_Struc++ = 0x1B58;       /* TAM_limit */
2596                 *pTimer_Struc++ = 0x00C8;       /* TBR_limit */
2597                 *pTimer_Struc++ = 0x07D0;       /* TER_limit */
2598                 *pTimer_Struc++ = 0x000A;       /* TGT_limit */
2599                 *pTimer_Struc++ = 0x4588;       /* THT_limit */
2600                 *pTimer_Struc++ = 0x1F40;       /* TRR_limit */
2601                 *pTimer_Struc++ = 0x4E20;       /* TVX_limit */
2602                 *pTimer_Struc++ = 0x0000;       /* reserved */
2603         }
2604
2605         /* Set node address. */
2606         *pTimer_Struc++ = dev->dev_addr[0] << 8
2607                 | (dev->dev_addr[1] & 0xFF);
2608         *pTimer_Struc++ = dev->dev_addr[2] << 8
2609                 | (dev->dev_addr[3] & 0xFF);
2610         *pTimer_Struc++ = dev->dev_addr[4] << 8
2611                 | (dev->dev_addr[5] & 0xFF);
2612
2613         /* Set group address. */
2614         *pTimer_Struc++ = tp->group_address_0 << 8
2615                 | tp->group_address_0 >> 8;
2616         *pTimer_Struc++ = tp->group_address[0] << 8
2617                 | tp->group_address[0] >> 8;
2618         *pTimer_Struc++ = tp->group_address[1] << 8
2619                 | tp->group_address[1] >> 8;
2620
2621         /* Set functional address. */
2622         *pTimer_Struc++ = tp->functional_address_0 << 8
2623                 | tp->functional_address_0 >> 8;
2624         *pTimer_Struc++ = tp->functional_address[0] << 8
2625                 | tp->functional_address[0] >> 8;
2626         *pTimer_Struc++ = tp->functional_address[1] << 8
2627                 | tp->functional_address[1] >> 8;
2628
2629         /* Set Bit-Wise group address. */
2630         *pTimer_Struc++ = tp->bitwise_group_address[0] << 8
2631                 | tp->bitwise_group_address[0] >> 8;
2632         *pTimer_Struc++ = tp->bitwise_group_address[1] << 8
2633                 | tp->bitwise_group_address[1] >> 8;
2634
2635         /* Set ring number address. */
2636         *pTimer_Struc++ = tp->source_ring_number;
2637         *pTimer_Struc++ = tp->target_ring_number;
2638
2639         /* Physical drop number. */
2640         *pTimer_Struc++ = (unsigned short)0;
2641         *pTimer_Struc++ = (unsigned short)0;
2642
2643         /* Product instance ID. */
2644         for(i = 0; i < 9; i++)
2645                 *pTimer_Struc++ = (unsigned short)0;
2646
2647         err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_INIT_TRC_TIMERS, 0);
2648
2649         return (err);
2650 }
2651
2652 static int smctr_issue_init_txrx_cmd(struct net_device *dev)
2653 {
2654         struct net_local *tp = netdev_priv(dev);
2655         unsigned int i;
2656         int err;
2657         void **txrx_ptrs = (void *)tp->misc_command_data;
2658
2659         if((err = smctr_wait_while_cbusy(dev)))
2660                 return (err);
2661
2662         if((err = smctr_wait_cmd(dev)))
2663         {
2664                 printk(KERN_ERR "%s: Hardware failure\n", dev->name);
2665                 return (err);
2666         }
2667
2668         /* Initialize Transmit Queue Pointers that are used, to point to
2669          * a single FCB.
2670          */
2671         for(i = 0; i < NUM_TX_QS_USED; i++)
2672                 *txrx_ptrs++ = (void *)TRC_POINTER(tp->tx_fcb_head[i]);
2673
2674         /* Initialize Transmit Queue Pointers that are NOT used to ZERO. */
2675         for(; i < MAX_TX_QS; i++)
2676                 *txrx_ptrs++ = (void *)0;
2677
2678         /* Initialize Receive Queue Pointers (MAC and Non-MAC) that are
2679          * used, to point to a single FCB and a BDB chain of buffers.
2680          */
2681         for(i = 0; i < NUM_RX_QS_USED; i++)
2682         {
2683                 *txrx_ptrs++ = (void *)TRC_POINTER(tp->rx_fcb_head[i]);
2684                 *txrx_ptrs++ = (void *)TRC_POINTER(tp->rx_bdb_head[i]);
2685         }
2686
2687         /* Initialize Receive Queue Pointers that are NOT used to ZERO. */
2688         for(; i < MAX_RX_QS; i++)
2689         {
2690                 *txrx_ptrs++ = (void *)0;
2691                 *txrx_ptrs++ = (void *)0;
2692         }
2693
2694         err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_INIT_TX_RX, 0);
2695
2696         return (err);
2697 }
2698
2699 static int smctr_issue_insert_cmd(struct net_device *dev)
2700 {
2701         int err;
2702
2703         err = smctr_setup_single_cmd(dev, ACB_CMD_INSERT, ACB_SUB_CMD_NOP);
2704
2705         return (err);
2706 }
2707
2708 static int smctr_issue_read_ring_status_cmd(struct net_device *dev)
2709 {
2710         int err;
2711
2712         if((err = smctr_wait_while_cbusy(dev)))
2713                 return (err);
2714
2715         if((err = smctr_wait_cmd(dev)))
2716                 return (err);
2717
2718         err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_READ_TRC_STATUS,
2719                 RW_TRC_STATUS_BLOCK);
2720
2721         return (err);
2722 }
2723
2724 static int smctr_issue_read_word_cmd(struct net_device *dev, __u16 aword_cnt)
2725 {
2726         int err;
2727
2728         if((err = smctr_wait_while_cbusy(dev)))
2729                 return (err);
2730
2731         if((err = smctr_wait_cmd(dev)))
2732                 return (err);
2733
2734         err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_READ_VALUE,
2735                 aword_cnt);
2736
2737         return (err);
2738 }
2739
2740 static int smctr_issue_remove_cmd(struct net_device *dev)
2741 {
2742         struct net_local *tp = netdev_priv(dev);
2743         int err;
2744
2745         if((err = smctr_wait_while_cbusy(dev)))
2746                 return (err);
2747
2748         tp->sclb_ptr->resume_control    = 0;
2749         tp->sclb_ptr->valid_command     = SCLB_VALID | SCLB_CMD_REMOVE;
2750
2751         smctr_set_ctrl_attention(dev);
2752
2753         return (0);
2754 }
2755
2756 static int smctr_issue_resume_acb_cmd(struct net_device *dev)
2757 {
2758         struct net_local *tp = netdev_priv(dev);
2759         int err;
2760
2761         if((err = smctr_wait_while_cbusy(dev)))
2762                 return (err);
2763
2764         tp->sclb_ptr->resume_control = SCLB_RC_ACB;
2765         tp->sclb_ptr->valid_command  = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;
2766
2767         tp->acb_pending = 1;
2768
2769         smctr_set_ctrl_attention(dev);
2770
2771         return (0);
2772 }
2773
2774 static int smctr_issue_resume_rx_bdb_cmd(struct net_device *dev, __u16 queue)
2775 {
2776         struct net_local *tp = netdev_priv(dev);
2777         int err;
2778
2779         if((err = smctr_wait_while_cbusy(dev)))
2780                 return (err);
2781
2782         if(queue == MAC_QUEUE)
2783                 tp->sclb_ptr->resume_control = SCLB_RC_RX_MAC_BDB;
2784         else
2785                 tp->sclb_ptr->resume_control = SCLB_RC_RX_NON_MAC_BDB;
2786
2787         tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;
2788
2789         smctr_set_ctrl_attention(dev);
2790
2791         return (0);
2792 }
2793
2794 static int smctr_issue_resume_rx_fcb_cmd(struct net_device *dev, __u16 queue)
2795 {
2796         struct net_local *tp = netdev_priv(dev);
2797
2798         if(smctr_debug > 10)
2799                 printk(KERN_DEBUG "%s: smctr_issue_resume_rx_fcb_cmd\n", dev->name);
2800
2801         if(smctr_wait_while_cbusy(dev))
2802                 return (-1);
2803
2804         if(queue == MAC_QUEUE)
2805                 tp->sclb_ptr->resume_control = SCLB_RC_RX_MAC_FCB;
2806         else
2807                 tp->sclb_ptr->resume_control = SCLB_RC_RX_NON_MAC_FCB;
2808
2809         tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;
2810
2811         smctr_set_ctrl_attention(dev);
2812
2813         return (0);
2814 }
2815
2816 static int smctr_issue_resume_tx_fcb_cmd(struct net_device *dev, __u16 queue)
2817 {
2818         struct net_local *tp = netdev_priv(dev);
2819
2820         if(smctr_debug > 10)
2821                 printk(KERN_DEBUG "%s: smctr_issue_resume_tx_fcb_cmd\n", dev->name);
2822
2823         if(smctr_wait_while_cbusy(dev))
2824                 return (-1);
2825
2826         tp->sclb_ptr->resume_control = (SCLB_RC_TFCB0 << queue);
2827         tp->sclb_ptr->valid_command = SCLB_RESUME_CONTROL_VALID | SCLB_VALID;
2828
2829         smctr_set_ctrl_attention(dev);
2830
2831         return (0);
2832 }
2833
2834 static int smctr_issue_test_internal_rom_cmd(struct net_device *dev)
2835 {
2836         int err;
2837
2838         err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
2839                 TRC_INTERNAL_ROM_TEST);
2840
2841         return (err);
2842 }
2843
2844 static int smctr_issue_test_hic_cmd(struct net_device *dev)
2845 {
2846         int err;
2847
2848         err = smctr_setup_single_cmd(dev, ACB_CMD_HIC_TEST,
2849                 TRC_HOST_INTERFACE_REG_TEST);
2850
2851         return (err);
2852 }
2853
2854 static int smctr_issue_test_mac_reg_cmd(struct net_device *dev)
2855 {
2856         int err;
2857
2858         err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
2859                 TRC_MAC_REGISTERS_TEST);
2860
2861         return (err);
2862 }
2863
2864 static int smctr_issue_trc_loopback_cmd(struct net_device *dev)
2865 {
2866         int err;
2867
2868         err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
2869                 TRC_INTERNAL_LOOPBACK);
2870
2871         return (err);
2872 }
2873
2874 static int smctr_issue_tri_loopback_cmd(struct net_device *dev)
2875 {
2876         int err;
2877
2878         err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
2879                 TRC_TRI_LOOPBACK);
2880
2881         return (err);
2882 }
2883
2884 static int smctr_issue_write_byte_cmd(struct net_device *dev,
2885         short aword_cnt, void *byte)
2886 {
2887         struct net_local *tp = netdev_priv(dev);
2888         unsigned int iword, ibyte;
2889         int err;
2890
2891         if((err = smctr_wait_while_cbusy(dev)))
2892                 return (err);
2893
2894         if((err = smctr_wait_cmd(dev)))
2895                 return (err);
2896
2897         for(iword = 0, ibyte = 0; iword < (unsigned int)(aword_cnt & 0xff);
2898                 iword++, ibyte += 2)
2899         {
2900                 tp->misc_command_data[iword] = (*((__u8 *)byte + ibyte) << 8)
2901                         | (*((__u8 *)byte + ibyte + 1));
2902         }
2903
2904         return (smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_WRITE_VALUE, 
2905                 aword_cnt));
2906 }
2907
2908 static int smctr_issue_write_word_cmd(struct net_device *dev,
2909         short aword_cnt, void *word)
2910 {
2911         struct net_local *tp = netdev_priv(dev);
2912         unsigned int i, err;
2913
2914         if((err = smctr_wait_while_cbusy(dev)))
2915                 return (err);
2916
2917         if((err = smctr_wait_cmd(dev)))
2918                 return (err);
2919
2920         for(i = 0; i < (unsigned int)(aword_cnt & 0xff); i++)
2921                 tp->misc_command_data[i] = *((__u16 *)word + i);
2922
2923         err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_WRITE_VALUE,
2924                 aword_cnt);
2925
2926         return (err);
2927 }
2928
2929 static int smctr_join_complete_state(struct net_device *dev)
2930 {
2931         int err;
2932
2933         err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE,
2934                 JS_JOIN_COMPLETE_STATE);
2935
2936         return (err);
2937 }
2938
2939 static int smctr_link_tx_fcbs_to_bdbs(struct net_device *dev)
2940 {
2941         struct net_local *tp = netdev_priv(dev);
2942         unsigned int i, j;
2943         FCBlock *fcb;
2944         BDBlock *bdb;
2945
2946         for(i = 0; i < NUM_TX_QS_USED; i++)
2947         {
2948                 fcb = tp->tx_fcb_head[i];
2949                 bdb = tp->tx_bdb_head[i];
2950
2951                 for(j = 0; j < tp->num_tx_fcbs[i]; j++)
2952                 {
2953                         fcb->bdb_ptr            = bdb;
2954                         fcb->trc_bdb_ptr        = TRC_POINTER(bdb);
2955                         fcb = (FCBlock *)((char *)fcb + sizeof(FCBlock));
2956                         bdb = (BDBlock *)((char *)bdb + sizeof(BDBlock));
2957                 }
2958         }
2959
2960         return (0);
2961 }
2962
2963 static int smctr_load_firmware(struct net_device *dev)
2964 {
2965         struct net_local *tp = netdev_priv(dev);
2966         __u16 i, checksum = 0;
2967         int err = 0;
2968
2969         if(smctr_debug > 10)
2970                 printk(KERN_DEBUG "%s: smctr_load_firmware\n", dev->name);
2971
2972         tp->ptr_ucode           = smctr_code;
2973         tp->num_of_tx_buffs     = 4;
2974         tp->mode_bits          |= UMAC;
2975         tp->receive_mask        = 0;
2976         tp->max_packet_size     = 4177;
2977
2978         /* Can only upload the firmware once per adapter reset. */
2979         if(tp->microcode_version != 0)
2980                 return (UCODE_PRESENT);
2981
2982         /* Verify the firmware exists and is there in the right amount. */
2983         if (!tp->ptr_ucode
2984                 || (*(tp->ptr_ucode + UCODE_VERSION_OFFSET) < UCODE_VERSION))
2985         {
2986                 return (UCODE_NOT_PRESENT);
2987         }
2988
2989         /* UCODE_SIZE is not included in Checksum. */
2990         for(i = 0; i < *((__u16 *)(tp->ptr_ucode + UCODE_SIZE_OFFSET)); i += 2)
2991                 checksum += *((__u16 *)(tp->ptr_ucode + 2 + i));
2992         if(checksum)
2993                 return (UCODE_NOT_PRESENT);
2994
2995         /* At this point we have a valid firmware image, lets kick it on up. */
2996         smctr_enable_adapter_ram(dev);
2997         smctr_enable_16bit(dev);
2998         smctr_set_page(dev, (__u8 *)tp->ram_access);
2999
3000         if((smctr_checksum_firmware(dev))
3001                 || (*(tp->ptr_ucode + UCODE_VERSION_OFFSET)
3002                 > tp->microcode_version))
3003         {
3004                 smctr_enable_adapter_ctrl_store(dev);
3005
3006                 /* Zero out ram space for firmware. */
3007                 for(i = 0; i < CS_RAM_SIZE; i += 2)
3008                         *((__u16 *)(tp->ram_access + i)) = 0;
3009
3010                 smctr_decode_firmware(dev);
3011
3012                 tp->microcode_version = *(tp->ptr_ucode + UCODE_VERSION_OFFSET);                *((__u16 *)(tp->ram_access + CS_RAM_VERSION_OFFSET))
3013                         = (tp->microcode_version << 8);
3014                 *((__u16 *)(tp->ram_access + CS_RAM_CHECKSUM_OFFSET))
3015                         = ~(tp->microcode_version << 8) + 1;
3016
3017                 smctr_disable_adapter_ctrl_store(dev);
3018
3019                 if(smctr_checksum_firmware(dev))
3020                         err = HARDWARE_FAILED;
3021         }
3022         else
3023                 err = UCODE_PRESENT;
3024
3025         smctr_disable_16bit(dev);
3026
3027         return (err);
3028 }
3029
3030 static int smctr_load_node_addr(struct net_device *dev)
3031 {
3032         int ioaddr = dev->base_addr;
3033         unsigned int i;
3034         __u8 r;
3035
3036         for(i = 0; i < 6; i++)
3037         {
3038                 r = inb(ioaddr + LAR0 + i);
3039                 dev->dev_addr[i] = (char)r;
3040         }
3041         dev->addr_len = 6;
3042
3043         return (0);
3044 }
3045
3046 /* Lobe Media Test.
3047  * During the transmission of the initial 1500 lobe media MAC frames,
3048  * the phase lock loop in the 805 chip may lock, and then un-lock, causing
3049  * the 825 to go into a PURGE state. When performing a PURGE, the MCT
3050  * microcode will not transmit any frames given to it by the host, and
3051  * will consequently cause a timeout.
3052  *
3053  * NOTE 1: If the monitor_state is MS_BEACON_TEST_STATE, all transmit
3054  * queues other then the one used for the lobe_media_test should be
3055  * disabled.!?
3056  *
3057  * NOTE 2: If the monitor_state is MS_BEACON_TEST_STATE and the receive_mask
3058  * has any multi-cast or promiscous bits set, the receive_mask needs to
3059  * be changed to clear the multi-cast or promiscous mode bits, the lobe_test
3060  * run, and then the receive mask set back to its original value if the test
3061  * is successful.
3062  */
3063 static int smctr_lobe_media_test(struct net_device *dev)
3064 {
3065         struct net_local *tp = netdev_priv(dev);
3066         unsigned int i, perror = 0;
3067         unsigned short saved_rcv_mask;
3068
3069         if(smctr_debug > 10)
3070                 printk(KERN_DEBUG "%s: smctr_lobe_media_test\n", dev->name);
3071
3072         /* Clear receive mask for lobe test. */
3073         saved_rcv_mask          = tp->receive_mask;
3074         tp->receive_mask        = 0;
3075
3076         smctr_chg_rx_mask(dev);
3077
3078         /* Setup the lobe media test. */
3079         smctr_lobe_media_test_cmd(dev);
3080         if(smctr_wait_cmd(dev))
3081         {
3082                 smctr_reset_adapter(dev);
3083                 tp->status = CLOSED;
3084                 return (LOBE_MEDIA_TEST_FAILED);
3085         }
3086
3087         /* Tx lobe media test frames. */
3088         for(i = 0; i < 1500; ++i)
3089         {
3090                 if(smctr_send_lobe_media_test(dev))
3091                 {
3092                         if(perror)
3093                         {
3094                                 smctr_reset_adapter(dev);
3095                                 tp->state = CLOSED;
3096                                 return (LOBE_MEDIA_TEST_FAILED);
3097                         }
3098                         else
3099                         {
3100                                 perror = 1;
3101                                 if(smctr_lobe_media_test_cmd(dev))
3102                                 {
3103                                         smctr_reset_adapter(dev);
3104                                         tp->state = CLOSED;
3105                                         return (LOBE_MEDIA_TEST_FAILED);
3106                                 }
3107                         }
3108                 }
3109         }
3110
3111         if(smctr_send_dat(dev))
3112         {
3113                 if(smctr_send_dat(dev))
3114                 {
3115                         smctr_reset_adapter(dev);
3116                         tp->state = CLOSED;
3117                         return (LOBE_MEDIA_TEST_FAILED);
3118                 }
3119         }
3120
3121         /* Check if any frames received during test. */
3122         if((tp->rx_fcb_curr[MAC_QUEUE]->frame_status)
3123                 || (tp->rx_fcb_curr[NON_MAC_QUEUE]->frame_status))
3124         {
3125                 smctr_reset_adapter(dev);
3126                 tp->state = CLOSED;
3127                 return (LOBE_MEDIA_TEST_FAILED);
3128         }
3129
3130         /* Set receive mask to "Promisc" mode. */
3131         tp->receive_mask = saved_rcv_mask;
3132
3133         smctr_chg_rx_mask(dev);
3134
3135         return (0);
3136 }
3137
3138 static int smctr_lobe_media_test_cmd(struct net_device *dev)
3139 {
3140         struct net_local *tp = netdev_priv(dev);
3141         int err;
3142
3143         if(smctr_debug > 10)
3144                 printk(KERN_DEBUG "%s: smctr_lobe_media_test_cmd\n", dev->name);
3145
3146         /* Change to lobe media test state. */
3147         if(tp->monitor_state != MS_BEACON_TEST_STATE)
3148         {
3149                 smctr_lobe_media_test_state(dev);
3150                 if(smctr_wait_cmd(dev))
3151                 {
3152                         printk(KERN_ERR "Lobe Failed test state\n");
3153                         return (LOBE_MEDIA_TEST_FAILED);
3154                 }
3155         }
3156
3157         err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
3158                 TRC_LOBE_MEDIA_TEST);
3159
3160         return (err);
3161 }
3162
3163 static int smctr_lobe_media_test_state(struct net_device *dev)
3164 {
3165         int err;
3166
3167         err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE,
3168                 JS_LOBE_TEST_STATE);
3169
3170         return (err);
3171 }
3172
3173 static int smctr_make_8025_hdr(struct net_device *dev,
3174         MAC_HEADER *rmf, MAC_HEADER *tmf, __u16 ac_fc)
3175 {
3176         tmf->ac = MSB(ac_fc);                 /* msb is access control */
3177         tmf->fc = LSB(ac_fc);                 /* lsb is frame control */
3178
3179         tmf->sa[0] = dev->dev_addr[0];
3180         tmf->sa[1] = dev->dev_addr[1];
3181         tmf->sa[2] = dev->dev_addr[2];
3182         tmf->sa[3] = dev->dev_addr[3];
3183         tmf->sa[4] = dev->dev_addr[4];
3184         tmf->sa[5] = dev->dev_addr[5];
3185
3186         switch(tmf->vc)
3187         {
3188                 /* Send RQ_INIT to RPS */
3189                 case RQ_INIT:
3190                         tmf->da[0] = 0xc0;
3191                         tmf->da[1] = 0x00;
3192                         tmf->da[2] = 0x00;
3193                         tmf->da[3] = 0x00;
3194                         tmf->da[4] = 0x00;
3195                         tmf->da[5] = 0x02;
3196                         break;
3197
3198                 /* Send RPT_TX_FORWARD to CRS */
3199                 case RPT_TX_FORWARD:
3200                         tmf->da[0] = 0xc0;
3201                         tmf->da[1] = 0x00;
3202                         tmf->da[2] = 0x00;
3203                         tmf->da[3] = 0x00;
3204                         tmf->da[4] = 0x00;
3205                         tmf->da[5] = 0x10;
3206                         break;
3207
3208                 /* Everything else goes to sender */
3209                 default:
3210                         tmf->da[0] = rmf->sa[0];
3211                         tmf->da[1] = rmf->sa[1];
3212                         tmf->da[2] = rmf->sa[2];
3213                         tmf->da[3] = rmf->sa[3];
3214                         tmf->da[4] = rmf->sa[4];
3215                         tmf->da[5] = rmf->sa[5];
3216                         break;
3217         }
3218
3219         return (0);
3220 }
3221
3222 static int smctr_make_access_pri(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3223 {
3224         struct net_local *tp = netdev_priv(dev);
3225
3226         tsv->svi = AUTHORIZED_ACCESS_PRIORITY;
3227         tsv->svl = S_AUTHORIZED_ACCESS_PRIORITY;
3228
3229         tsv->svv[0] = MSB(tp->authorized_access_priority);
3230         tsv->svv[1] = LSB(tp->authorized_access_priority);
3231
3232         return (0);
3233 }
3234
3235 static int smctr_make_addr_mod(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3236 {
3237         tsv->svi = ADDRESS_MODIFER;
3238         tsv->svl = S_ADDRESS_MODIFER;
3239
3240         tsv->svv[0] = 0;
3241         tsv->svv[1] = 0;
3242
3243         return (0);
3244 }
3245
3246 static int smctr_make_auth_funct_class(struct net_device *dev,
3247         MAC_SUB_VECTOR *tsv)
3248 {
3249         struct net_local *tp = netdev_priv(dev);
3250
3251         tsv->svi = AUTHORIZED_FUNCTION_CLASS;
3252         tsv->svl = S_AUTHORIZED_FUNCTION_CLASS;
3253
3254         tsv->svv[0] = MSB(tp->authorized_function_classes);
3255         tsv->svv[1] = LSB(tp->authorized_function_classes);
3256
3257         return (0);
3258 }
3259
3260 static int smctr_make_corr(struct net_device *dev,
3261         MAC_SUB_VECTOR *tsv, __u16 correlator)
3262 {
3263         tsv->svi = CORRELATOR;
3264         tsv->svl = S_CORRELATOR;
3265
3266         tsv->svv[0] = MSB(correlator);
3267         tsv->svv[1] = LSB(correlator);
3268
3269         return (0);
3270 }
3271
3272 static int smctr_make_funct_addr(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3273 {
3274         struct net_local *tp = netdev_priv(dev);
3275
3276         smctr_get_functional_address(dev);
3277
3278         tsv->svi = FUNCTIONAL_ADDRESS;
3279         tsv->svl = S_FUNCTIONAL_ADDRESS;
3280
3281         tsv->svv[0] = MSB(tp->misc_command_data[0]);
3282         tsv->svv[1] = LSB(tp->misc_command_data[0]);
3283
3284         tsv->svv[2] = MSB(tp->misc_command_data[1]);
3285         tsv->svv[3] = LSB(tp->misc_command_data[1]);
3286
3287         return (0);
3288 }
3289
3290 static int smctr_make_group_addr(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3291 {
3292         struct net_local *tp = netdev_priv(dev);
3293
3294         smctr_get_group_address(dev);
3295
3296         tsv->svi = GROUP_ADDRESS;
3297         tsv->svl = S_GROUP_ADDRESS;
3298
3299         tsv->svv[0] = MSB(tp->misc_command_data[0]);
3300         tsv->svv[1] = LSB(tp->misc_command_data[0]);
3301
3302         tsv->svv[2] = MSB(tp->misc_command_data[1]);
3303         tsv->svv[3] = LSB(tp->misc_command_data[1]);
3304
3305         /* Set Group Address Sub-vector to all zeros if only the
3306          * Group Address/Functional Address Indicator is set.
3307          */
3308         if(tsv->svv[0] == 0x80 && tsv->svv[1] == 0x00
3309                 && tsv->svv[2] == 0x00 && tsv->svv[3] == 0x00)
3310                 tsv->svv[0] = 0x00;
3311
3312         return (0);
3313 }
3314
3315 static int smctr_make_phy_drop_num(struct net_device *dev,
3316         MAC_SUB_VECTOR *tsv)
3317 {
3318         struct net_local *tp = netdev_priv(dev);
3319
3320         smctr_get_physical_drop_number(dev);
3321
3322         tsv->svi = PHYSICAL_DROP;
3323         tsv->svl = S_PHYSICAL_DROP;
3324
3325         tsv->svv[0] = MSB(tp->misc_command_data[0]);
3326         tsv->svv[1] = LSB(tp->misc_command_data[0]);
3327
3328         tsv->svv[2] = MSB(tp->misc_command_data[1]);
3329         tsv->svv[3] = LSB(tp->misc_command_data[1]);
3330
3331         return (0);
3332 }
3333
3334 static int smctr_make_product_id(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3335 {
3336         int i;
3337
3338         tsv->svi = PRODUCT_INSTANCE_ID;
3339         tsv->svl = S_PRODUCT_INSTANCE_ID;
3340
3341         for(i = 0; i < 18; i++)
3342                 tsv->svv[i] = 0xF0;
3343
3344         return (0);
3345 }
3346
3347 static int smctr_make_station_id(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3348 {
3349         struct net_local *tp = netdev_priv(dev);
3350
3351         smctr_get_station_id(dev);
3352
3353         tsv->svi = STATION_IDENTIFER;
3354         tsv->svl = S_STATION_IDENTIFER;
3355
3356         tsv->svv[0] = MSB(tp->misc_command_data[0]);
3357         tsv->svv[1] = LSB(tp->misc_command_data[0]);
3358
3359         tsv->svv[2] = MSB(tp->misc_command_data[1]);
3360         tsv->svv[3] = LSB(tp->misc_command_data[1]);
3361
3362         tsv->svv[4] = MSB(tp->misc_command_data[2]);
3363         tsv->svv[5] = LSB(tp->misc_command_data[2]);
3364
3365         return (0);
3366 }
3367
3368 static int smctr_make_ring_station_status(struct net_device *dev,
3369         MAC_SUB_VECTOR * tsv)
3370 {
3371         tsv->svi = RING_STATION_STATUS;
3372         tsv->svl = S_RING_STATION_STATUS;
3373
3374         tsv->svv[0] = 0;
3375         tsv->svv[1] = 0;
3376         tsv->svv[2] = 0;
3377         tsv->svv[3] = 0;
3378         tsv->svv[4] = 0;
3379         tsv->svv[5] = 0;
3380
3381         return (0);
3382 }
3383
3384 static int smctr_make_ring_station_version(struct net_device *dev,
3385         MAC_SUB_VECTOR *tsv)
3386 {
3387         struct net_local *tp = netdev_priv(dev);
3388
3389         tsv->svi = RING_STATION_VERSION_NUMBER;
3390         tsv->svl = S_RING_STATION_VERSION_NUMBER;
3391
3392         tsv->svv[0] = 0xe2;            /* EBCDIC - S */
3393         tsv->svv[1] = 0xd4;            /* EBCDIC - M */
3394         tsv->svv[2] = 0xc3;            /* EBCDIC - C */
3395         tsv->svv[3] = 0x40;            /* EBCDIC -   */
3396         tsv->svv[4] = 0xe5;            /* EBCDIC - V */
3397         tsv->svv[5] = 0xF0 + (tp->microcode_version >> 4);
3398         tsv->svv[6] = 0xF0 + (tp->microcode_version & 0x0f);
3399         tsv->svv[7] = 0x40;            /* EBCDIC -   */
3400         tsv->svv[8] = 0xe7;            /* EBCDIC - X */
3401
3402         if(tp->extra_info & CHIP_REV_MASK)
3403                 tsv->svv[9] = 0xc5;    /* EBCDIC - E */
3404         else
3405                 tsv->svv[9] = 0xc4;    /* EBCDIC - D */
3406
3407         return (0);
3408 }
3409
3410 static int smctr_make_tx_status_code(struct net_device *dev,
3411         MAC_SUB_VECTOR *tsv, __u16 tx_fstatus)
3412 {
3413         tsv->svi = TRANSMIT_STATUS_CODE;
3414         tsv->svl = S_TRANSMIT_STATUS_CODE;
3415
3416         tsv->svv[0] = ((tx_fstatus & 0x0100 >> 6) | IBM_PASS_SOURCE_ADDR);
3417
3418         /* Stripped frame status of Transmitted Frame */
3419         tsv->svv[1] = tx_fstatus & 0xff;
3420
3421         return (0);
3422 }
3423
3424 static int smctr_make_upstream_neighbor_addr(struct net_device *dev,
3425         MAC_SUB_VECTOR *tsv)
3426 {
3427         struct net_local *tp = netdev_priv(dev);
3428
3429         smctr_get_upstream_neighbor_addr(dev);
3430
3431         tsv->svi = UPSTREAM_NEIGHBOR_ADDRESS;
3432         tsv->svl = S_UPSTREAM_NEIGHBOR_ADDRESS;
3433
3434         tsv->svv[0] = MSB(tp->misc_command_data[0]);
3435         tsv->svv[1] = LSB(tp->misc_command_data[0]);
3436
3437         tsv->svv[2] = MSB(tp->misc_command_data[1]);
3438         tsv->svv[3] = LSB(tp->misc_command_data[1]);
3439
3440         tsv->svv[4] = MSB(tp->misc_command_data[2]);
3441         tsv->svv[5] = LSB(tp->misc_command_data[2]);
3442
3443         return (0);
3444 }
3445
3446 static int smctr_make_wrap_data(struct net_device *dev, MAC_SUB_VECTOR *tsv)
3447 {
3448         tsv->svi = WRAP_DATA;
3449         tsv->svl = S_WRAP_DATA;
3450
3451         return (0);
3452 }
3453
3454 /*
3455  * Open/initialize the board. This is called sometime after
3456  * booting when the 'ifconfig' program is run.
3457  *
3458  * This routine should set everything up anew at each open, even
3459  * registers that "should" only need to be set once at boot, so that
3460  * there is non-reboot way to recover if something goes wrong.
3461  */
3462 static int smctr_open(struct net_device *dev)
3463 {
3464         int err;
3465
3466         if(smctr_debug > 10)
3467                 printk(KERN_DEBUG "%s: smctr_open\n", dev->name);
3468
3469         err = smctr_init_adapter(dev);
3470         if(err < 0)
3471                 return (err);
3472
3473         return (err);
3474 }
3475
3476 /* Interrupt driven open of Token card. */
3477 static int smctr_open_tr(struct net_device *dev)
3478 {
3479         struct net_local *tp = netdev_priv(dev);
3480         unsigned long flags;
3481         int err;
3482
3483         if(smctr_debug > 10)
3484                 printk(KERN_DEBUG "%s: smctr_open_tr\n", dev->name);
3485
3486         /* Now we can actually open the adapter. */
3487         if(tp->status == OPEN)
3488                 return (0);
3489         if(tp->status != INITIALIZED)
3490                 return (-1);
3491
3492         /* FIXME: it would work a lot better if we masked the irq sources
3493            on the card here, then we could skip the locking and poll nicely */
3494         spin_lock_irqsave(&tp->lock, flags);
3495         
3496         smctr_set_page(dev, (__u8 *)tp->ram_access);
3497
3498         if((err = smctr_issue_resume_rx_fcb_cmd(dev, (short)MAC_QUEUE)))
3499                 goto out;
3500
3501         if((err = smctr_issue_resume_rx_bdb_cmd(dev, (short)MAC_QUEUE)))
3502                 goto out;
3503
3504         if((err = smctr_issue_resume_rx_fcb_cmd(dev, (short)NON_MAC_QUEUE)))
3505                 goto out;
3506
3507         if((err = smctr_issue_resume_rx_bdb_cmd(dev, (short)NON_MAC_QUEUE)))
3508                 goto out;
3509
3510         tp->status = CLOSED;
3511
3512         /* Insert into the Ring or Enter Loopback Mode. */
3513         if((tp->mode_bits & LOOPING_MODE_MASK) == LOOPBACK_MODE_1)
3514         {
3515                 tp->status = CLOSED;
3516
3517                 if(!(err = smctr_issue_trc_loopback_cmd(dev)))
3518                 {
3519                         if(!(err = smctr_wait_cmd(dev)))
3520                                 tp->status = OPEN;
3521                 }
3522
3523                 smctr_status_chg(dev);
3524         }
3525         else
3526         {
3527                 if((tp->mode_bits & LOOPING_MODE_MASK) == LOOPBACK_MODE_2)
3528                 {
3529                         tp->status = CLOSED;
3530                         if(!(err = smctr_issue_tri_loopback_cmd(dev)))
3531                         {
3532                                 if(!(err = smctr_wait_cmd(dev)))
3533                                         tp->status = OPEN;
3534                         }
3535
3536                         smctr_status_chg(dev);
3537                 }
3538                 else
3539                 {
3540                         if((tp->mode_bits & LOOPING_MODE_MASK)
3541                                 == LOOPBACK_MODE_3)
3542                         {
3543                                 tp->status = CLOSED;
3544                                 if(!(err = smctr_lobe_media_test_cmd(dev)))
3545                                 {
3546                                         if(!(err = smctr_wait_cmd(dev)))
3547                                                 tp->status = OPEN;
3548                                 }
3549                                 smctr_status_chg(dev);
3550                         }
3551                         else
3552                         {
3553                                 if(!(err = smctr_lobe_media_test(dev)))
3554                                         err = smctr_issue_insert_cmd(dev);
3555                                 else
3556                                 {
3557                                         if(err == LOBE_MEDIA_TEST_FAILED)
3558                                                 printk(KERN_WARNING "%s: Lobe Media Test Failure - Check cable?\n", dev->name);
3559                                 }
3560                         }
3561                 }
3562         }
3563
3564 out:
3565         spin_unlock_irqrestore(&tp->lock, flags);
3566
3567         return (err);
3568 }
3569
3570 /* Check for a network adapter of this type, 
3571  * and return device structure if one exists.
3572  */
3573 struct net_device __init *smctr_probe(int unit)
3574 {
3575         struct net_device *dev = alloc_trdev(sizeof(struct net_local));
3576         static const unsigned ports[] = {
3577                 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0, 0x300,
3578                 0x320, 0x340, 0x360, 0x380, 0
3579         };
3580         const unsigned *port;
3581         int err = 0;
3582
3583         if (!dev)
3584                 return ERR_PTR(-ENOMEM);
3585
3586         if (unit >= 0) {
3587                 sprintf(dev->name, "tr%d", unit);
3588                 netdev_boot_setup_check(dev);
3589         }
3590
3591         if (dev->base_addr > 0x1ff)    /* Check a single specified location. */
3592                 err = smctr_probe1(dev, dev->base_addr);
3593         else if(dev->base_addr != 0)  /* Don't probe at all. */
3594                 err =-ENXIO;
3595         else {
3596                 for (port = ports; *port; port++) {
3597                         err = smctr_probe1(dev, *port);
3598                         if (!err)
3599                                 break;
3600                 }
3601         }
3602         if (err)
3603                 goto out;
3604         err = register_netdev(dev);
3605         if (err)
3606                 goto out1;
3607         return dev;
3608 out1:
3609 #ifdef CONFIG_MCA_LEGACY
3610         { struct net_local *tp = netdev_priv(dev);
3611           if (tp->slot_num)
3612                 mca_mark_as_unused(tp->slot_num);
3613         }
3614 #endif
3615         release_region(dev->base_addr, SMCTR_IO_EXTENT);
3616         free_irq(dev->irq, dev);
3617 out:
3618         free_netdev(dev);
3619         return ERR_PTR(err);
3620 }
3621
3622
3623 static int __init smctr_probe1(struct net_device *dev, int ioaddr)
3624 {
3625         static unsigned version_printed;
3626         struct net_local *tp = netdev_priv(dev);
3627         int err;
3628         __u32 *ram;
3629
3630         if(smctr_debug && version_printed++ == 0)
3631                 printk(version);
3632
3633         spin_lock_init(&tp->lock);
3634         dev->base_addr = ioaddr;
3635
3636         /* Actually detect an adapter now. */
3637         err = smctr_chk_isa(dev);
3638         if(err < 0)
3639         {
3640                 if ((err = smctr_chk_mca(dev)) < 0) {
3641                         err = -ENODEV;
3642                         goto out;
3643                 }
3644         }
3645
3646         tp = netdev_priv(dev);
3647         dev->mem_start = tp->ram_base;
3648         dev->mem_end = dev->mem_start + 0x10000;
3649         ram = (__u32 *)phys_to_virt(dev->mem_start);
3650         tp->ram_access = *(__u32 *)&ram;
3651         tp->status = NOT_INITIALIZED;
3652
3653         err = smctr_load_firmware(dev);
3654         if(err != UCODE_PRESENT && err != SUCCESS)
3655         {
3656                 printk(KERN_ERR "%s: Firmware load failed (%d)\n", dev->name, err);
3657                 err = -EIO;
3658                 goto out;
3659         }
3660
3661         /* Allow user to specify ring speed on module insert. */
3662         if(ringspeed == 4)
3663                 tp->media_type = MEDIA_UTP_4;
3664         else
3665                 tp->media_type = MEDIA_UTP_16;
3666
3667         printk(KERN_INFO "%s: %s %s at Io %#4x, Irq %d, Rom %#4x, Ram %#4x.\n",
3668                 dev->name, smctr_name, smctr_model,
3669                 (unsigned int)dev->base_addr,
3670                 dev->irq, tp->rom_base, tp->ram_base);
3671
3672         dev->open               = smctr_open;
3673         dev->stop               = smctr_close;
3674         dev->hard_start_xmit    = smctr_send_packet;
3675         dev->tx_timeout         = smctr_timeout;
3676         dev->watchdog_timeo     = HZ;
3677         dev->get_stats          = smctr_get_stats;
3678         dev->set_multicast_list = &smctr_set_multicast_list;
3679         return (0);
3680
3681 out:
3682         return err;
3683 }
3684
3685 static int smctr_process_rx_packet(MAC_HEADER *rmf, __u16 size,
3686         struct net_device *dev, __u16 rx_status)
3687 {
3688         struct net_local *tp = netdev_priv(dev);
3689         struct sk_buff *skb;
3690         __u16 rcode, correlator;
3691         int err = 0;
3692         __u8 xframe = 1;
3693
3694         rmf->vl = SWAP_BYTES(rmf->vl);
3695         if(rx_status & FCB_RX_STATUS_DA_MATCHED)
3696         {
3697                 switch(rmf->vc)
3698                 {
3699                         /* Received MAC Frames Processed by RS. */
3700                         case INIT:
3701                                 if((rcode = smctr_rcv_init(dev, rmf, &correlator)) == HARDWARE_FAILED)
3702                                 {
3703                                         return (rcode);
3704                                 }
3705
3706                                 if((err = smctr_send_rsp(dev, rmf, rcode,
3707                                         correlator)))
3708                                 {
3709                                         return (err);
3710                                 }
3711                                 break;
3712
3713                         case CHG_PARM:
3714                                 if((rcode = smctr_rcv_chg_param(dev, rmf,
3715                                         &correlator)) ==HARDWARE_FAILED)
3716                                 {
3717                                         return (rcode);
3718                                 }
3719
3720                                 if((err = smctr_send_rsp(dev, rmf, rcode,
3721                                         correlator)))
3722                                 {
3723                                         return (err);
3724                                 }
3725                                 break;
3726
3727                         case RQ_ADDR:
3728                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3729                                         rmf, &correlator)) != POSITIVE_ACK)
3730                                 {
3731                                         if(rcode == HARDWARE_FAILED)
3732                                                 return (rcode);
3733                                         else
3734                                                 return (smctr_send_rsp(dev, rmf,
3735                                                         rcode, correlator));
3736                                 }
3737
3738                                 if((err = smctr_send_rpt_addr(dev, rmf,
3739                                         correlator)))
3740                                 {
3741                                         return (err);
3742                                 }
3743                                 break;
3744
3745                         case RQ_ATTCH:
3746                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3747                                         rmf, &correlator)) != POSITIVE_ACK)
3748                                 {
3749                                         if(rcode == HARDWARE_FAILED)
3750                                                 return (rcode);
3751                                         else
3752                                                 return (smctr_send_rsp(dev, rmf,
3753                                                         rcode,
3754                                                         correlator));
3755                                 }
3756
3757                                 if((err = smctr_send_rpt_attch(dev, rmf,
3758                                         correlator)))
3759                                 {
3760                                         return (err);
3761                                 }
3762                                 break;
3763
3764                         case RQ_STATE:
3765                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3766                                         rmf, &correlator)) != POSITIVE_ACK)
3767                                 {
3768                                         if(rcode == HARDWARE_FAILED)
3769                                                 return (rcode);
3770                                         else
3771                                                 return (smctr_send_rsp(dev, rmf,
3772                                                         rcode,
3773                                                         correlator));
3774                                 }
3775
3776                                 if((err = smctr_send_rpt_state(dev, rmf,
3777                                         correlator)))
3778                                 {
3779                                         return (err);
3780                                 }
3781                                 break;
3782
3783                         case TX_FORWARD: {
3784                                 __u16 uninitialized_var(tx_fstatus);
3785
3786                                 if((rcode = smctr_rcv_tx_forward(dev, rmf))
3787                                         != POSITIVE_ACK)
3788                                 {
3789                                         if(rcode == HARDWARE_FAILED)
3790                                                 return (rcode);
3791                                         else
3792                                                 return (smctr_send_rsp(dev, rmf,
3793                                                         rcode,
3794                                                         correlator));
3795                                 }
3796
3797                                 if((err = smctr_send_tx_forward(dev, rmf,
3798                                         &tx_fstatus)) == HARDWARE_FAILED)
3799                                 {
3800                                         return (err);
3801                                 }
3802
3803                                 if(err == A_FRAME_WAS_FORWARDED)
3804                                 {
3805                                         if((err = smctr_send_rpt_tx_forward(dev,
3806                                                 rmf, tx_fstatus))
3807                                                 == HARDWARE_FAILED)
3808                                         {
3809                                                 return (err);
3810                                         }
3811                                 }
3812                                 break;
3813                         }
3814
3815                         /* Received MAC Frames Processed by CRS/REM/RPS. */
3816                         case RSP:
3817                         case RQ_INIT:
3818                         case RPT_NEW_MON:
3819                         case RPT_SUA_CHG:
3820                         case RPT_ACTIVE_ERR:
3821                         case RPT_NN_INCMP:
3822                         case RPT_ERROR:
3823                         case RPT_ATTCH:
3824                         case RPT_STATE:
3825                         case RPT_ADDR:
3826                                 break;
3827
3828                         /* Rcvd Att. MAC Frame (if RXATMAC set) or UNKNOWN */
3829                         default:
3830                                 xframe = 0;
3831                                 if(!(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES))
3832                                 {
3833                                         rcode = smctr_rcv_unknown(dev, rmf,
3834                                                 &correlator);
3835                                         if((err = smctr_send_rsp(dev, rmf,rcode,
3836                                                 correlator)))
3837                                         {
3838                                                 return (err);
3839                                         }
3840                                 }
3841
3842                                 break;
3843                 }
3844         }
3845         else
3846         {
3847                 /* 1. DA doesn't match (Promiscuous Mode).
3848                  * 2. Parse for Extended MAC Frame Type.
3849                  */
3850                 switch(rmf->vc)
3851                 {
3852                         case RSP:
3853                         case INIT:
3854                         case RQ_INIT:
3855                         case RQ_ADDR:
3856                         case RQ_ATTCH:
3857                         case RQ_STATE:
3858                         case CHG_PARM:
3859                         case RPT_ADDR:
3860                         case RPT_ERROR:
3861                         case RPT_ATTCH:
3862                         case RPT_STATE:
3863                         case RPT_NEW_MON:
3864                         case RPT_SUA_CHG:
3865                         case RPT_NN_INCMP:
3866                         case RPT_ACTIVE_ERR:
3867                                 break;
3868
3869                         default:
3870                                 xframe = 0;
3871                                 break;
3872                 }
3873         }
3874
3875         /* NOTE: UNKNOWN MAC frames will NOT be passed up unless
3876          * ACCEPT_ATT_MAC_FRAMES is set.
3877          */
3878         if(((tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
3879                 && (xframe == (__u8)0))
3880                 || ((tp->receive_mask & ACCEPT_EXT_MAC_FRAMES)
3881                 && (xframe == (__u8)1)))
3882         {
3883                 rmf->vl = SWAP_BYTES(rmf->vl);
3884
3885                 if (!(skb = dev_alloc_skb(size)))
3886                         return -ENOMEM;
3887                 skb->len = size;
3888
3889                 /* Slide data into a sleek skb. */
3890                 skb_put(skb, skb->len);
3891                 skb_copy_to_linear_data(skb, rmf, skb->len);
3892
3893                 /* Update Counters */
3894                 tp->MacStat.rx_packets++;
3895                 tp->MacStat.rx_bytes += skb->len;
3896
3897                 /* Kick the packet on up. */
3898                 skb->protocol = tr_type_trans(skb, dev);
3899                 netif_rx(skb);
3900                 dev->last_rx = jiffies;
3901                 err = 0;
3902         }
3903
3904         return (err);
3905 }
3906
3907 /* Adapter RAM test. Incremental word ODD boundary data test. */
3908 static int smctr_ram_memory_test(struct net_device *dev)
3909 {
3910         struct net_local *tp = netdev_priv(dev);
3911         __u16 page, pages_of_ram, start_pattern = 0, word_pattern = 0,
3912                 word_read = 0, err_word = 0, err_pattern = 0;
3913         unsigned int err_offset;
3914         __u32 j, pword;
3915         __u8 err = 0;
3916
3917         if(smctr_debug > 10)
3918                 printk(KERN_DEBUG "%s: smctr_ram_memory_test\n", dev->name);
3919
3920         start_pattern   = 0x0001;
3921         pages_of_ram    = tp->ram_size / tp->ram_usable;
3922         pword           = tp->ram_access;
3923
3924         /* Incremental word ODD boundary test. */
3925         for(page = 0; (page < pages_of_ram) && (~err);
3926                 page++, start_pattern += 0x8000)
3927         {
3928                 smctr_set_page(dev, (__u8 *)(tp->ram_access
3929                         + (page * tp->ram_usable * 1024) + 1));
3930                 word_pattern = start_pattern;
3931
3932                 for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1; j += 2)
3933                         *(__u16 *)(pword + j) = word_pattern++;
3934
3935                 word_pattern = start_pattern;
3936
3937                 for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1
3938                         && (~err); j += 2, word_pattern++)
3939                 {
3940                         word_read = *(__u16 *)(pword + j);
3941                         if(word_read != word_pattern)
3942                         {
3943                                 err             = (__u8)1;
3944                                 err_offset      = j;
3945                                 err_word        = word_read;
3946                                 err_pattern     = word_pattern;
3947                                 return (RAM_TEST_FAILED);
3948                         }
3949                 }
3950         }
3951
3952         /* Zero out memory. */
3953         for(page = 0; page < pages_of_ram && (~err); page++)
3954         {
3955                 smctr_set_page(dev, (__u8 *)(tp->ram_access
3956                         + (page * tp->ram_usable * 1024)));
3957                 word_pattern = 0;
3958
3959                 for(j = 0; j < (__u32)tp->ram_usable * 1024; j +=2)
3960                         *(__u16 *)(pword + j) = word_pattern;
3961
3962                 for(j =0; j < (__u32)tp->ram_usable * 1024
3963                         && (~err); j += 2)
3964                 {
3965                         word_read = *(__u16 *)(pword + j);
3966                         if(word_read != word_pattern)
3967                         {
3968                                 err             = (__u8)1;
3969                                 err_offset      = j;
3970                                 err_word        = word_read;
3971                                 err_pattern     = word_pattern;
3972                                 return (RAM_TEST_FAILED);
3973                         }
3974                 }
3975         }
3976
3977         smctr_set_page(dev, (__u8 *)tp->ram_access);
3978
3979         return (0);
3980 }
3981
3982 static int smctr_rcv_chg_param(struct net_device *dev, MAC_HEADER *rmf,
3983         __u16 *correlator)
3984 {
3985         MAC_SUB_VECTOR *rsv;
3986         signed short vlen;
3987         __u16 rcode = POSITIVE_ACK;
3988         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
3989
3990         /* This Frame can only come from a CRS */
3991         if((rmf->dc_sc & SC_MASK) != SC_CRS)
3992                 return(E_INAPPROPRIATE_SOURCE_CLASS);
3993
3994         /* Remove MVID Length from total length. */
3995         vlen = (signed short)rmf->vl - 4;
3996
3997         /* Point to First SVID */
3998         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
3999
4000         /* Search for Appropriate SVID's. */
4001         while((vlen > 0) && (rcode == POSITIVE_ACK))
4002         {
4003                 switch(rsv->svi)
4004                 {
4005                         case CORRELATOR:
4006                                 svectors |= F_CORRELATOR;
4007                                 rcode = smctr_set_corr(dev, rsv, correlator);
4008                                 break;
4009
4010                         case LOCAL_RING_NUMBER:
4011                                 svectors |= F_LOCAL_RING_NUMBER;
4012                                 rcode = smctr_set_local_ring_num(dev, rsv);
4013                                 break;
4014
4015                         case ASSIGN_PHYSICAL_DROP:
4016                                 svectors |= F_ASSIGN_PHYSICAL_DROP;
4017                                 rcode = smctr_set_phy_drop(dev, rsv);
4018                                 break;
4019
4020                         case ERROR_TIMER_VALUE:
4021                                 svectors |= F_ERROR_TIMER_VALUE;
4022                                 rcode = smctr_set_error_timer_value(dev, rsv);
4023                                 break;
4024
4025                         case AUTHORIZED_FUNCTION_CLASS:
4026                                 svectors |= F_AUTHORIZED_FUNCTION_CLASS;
4027                                 rcode = smctr_set_auth_funct_class(dev, rsv);
4028                                 break;
4029
4030                         case AUTHORIZED_ACCESS_PRIORITY:
4031                                 svectors |= F_AUTHORIZED_ACCESS_PRIORITY;
4032                                 rcode = smctr_set_auth_access_pri(dev, rsv);
4033                                 break;
4034
4035                         default:
4036                                 rcode = E_SUB_VECTOR_UNKNOWN;
4037                                 break;
4038                 }
4039
4040                 /* Let Sender Know if SUM of SV length's is
4041                  * larger then length in MVID length field
4042                  */
4043                 if((vlen -= rsv->svl) < 0)
4044                         rcode = E_VECTOR_LENGTH_ERROR;
4045
4046                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4047         }
4048
4049         if(rcode == POSITIVE_ACK)
4050         {
4051                 /* Let Sender Know if MVID length field
4052                  * is larger then SUM of SV length's
4053                  */
4054                 if(vlen != 0)
4055                         rcode = E_VECTOR_LENGTH_ERROR;
4056                 else
4057                 {
4058                         /* Let Sender Know if Expected SVID Missing */
4059                         if((svectors & R_CHG_PARM) ^ R_CHG_PARM)
4060                                 rcode = E_MISSING_SUB_VECTOR;
4061                 }
4062         }
4063
4064         return (rcode);
4065 }
4066
4067 static int smctr_rcv_init(struct net_device *dev, MAC_HEADER *rmf,
4068         __u16 *correlator)
4069 {
4070         MAC_SUB_VECTOR *rsv;
4071         signed short vlen;
4072         __u16 rcode = POSITIVE_ACK;
4073         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4074
4075         /* This Frame can only come from a RPS */
4076         if((rmf->dc_sc & SC_MASK) != SC_RPS)
4077                 return (E_INAPPROPRIATE_SOURCE_CLASS);
4078
4079         /* Remove MVID Length from total length. */
4080         vlen = (signed short)rmf->vl - 4;
4081
4082         /* Point to First SVID */
4083         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4084
4085         /* Search for Appropriate SVID's */
4086         while((vlen > 0) && (rcode == POSITIVE_ACK))
4087         {
4088                 switch(rsv->svi)
4089                 {
4090                         case CORRELATOR:
4091                                 svectors |= F_CORRELATOR;
4092                                 rcode = smctr_set_corr(dev, rsv, correlator);
4093                                 break;
4094
4095                         case LOCAL_RING_NUMBER:
4096                                 svectors |= F_LOCAL_RING_NUMBER;
4097                                 rcode = smctr_set_local_ring_num(dev, rsv);
4098                                 break;
4099
4100                         case ASSIGN_PHYSICAL_DROP:
4101                                 svectors |= F_ASSIGN_PHYSICAL_DROP;
4102                                 rcode = smctr_set_phy_drop(dev, rsv);
4103                                 break;
4104
4105                         case ERROR_TIMER_VALUE:
4106                                 svectors |= F_ERROR_TIMER_VALUE;
4107                                 rcode = smctr_set_error_timer_value(dev, rsv);
4108                                 break;
4109
4110                         default:
4111                                 rcode = E_SUB_VECTOR_UNKNOWN;
4112                                 break;
4113                 }
4114
4115                 /* Let Sender Know if SUM of SV length's is
4116                  * larger then length in MVID length field
4117                  */
4118                 if((vlen -= rsv->svl) < 0)
4119                         rcode = E_VECTOR_LENGTH_ERROR;
4120
4121                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4122         }
4123
4124         if(rcode == POSITIVE_ACK)
4125         {
4126                 /* Let Sender Know if MVID length field
4127                  * is larger then SUM of SV length's
4128                  */
4129                 if(vlen != 0)
4130                         rcode = E_VECTOR_LENGTH_ERROR;
4131                 else
4132                 {
4133                         /* Let Sender Know if Expected SV Missing */
4134                         if((svectors & R_INIT) ^ R_INIT)
4135                                 rcode = E_MISSING_SUB_VECTOR;
4136                 }
4137         }
4138
4139         return (rcode);
4140 }
4141
4142 static int smctr_rcv_tx_forward(struct net_device *dev, MAC_HEADER *rmf)
4143 {
4144         MAC_SUB_VECTOR *rsv;
4145         signed short vlen;
4146         __u16 rcode = POSITIVE_ACK;
4147         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4148
4149         /* This Frame can only come from a CRS */
4150         if((rmf->dc_sc & SC_MASK) != SC_CRS)
4151                 return (E_INAPPROPRIATE_SOURCE_CLASS);
4152
4153         /* Remove MVID Length from total length */
4154         vlen = (signed short)rmf->vl - 4;
4155
4156         /* Point to First SVID */
4157         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4158
4159         /* Search for Appropriate SVID's */
4160         while((vlen > 0) && (rcode == POSITIVE_ACK))
4161         {
4162                 switch(rsv->svi)
4163                 {
4164                         case FRAME_FORWARD:
4165                                 svectors |= F_FRAME_FORWARD;
4166                                 rcode = smctr_set_frame_forward(dev, rsv, 
4167                                         rmf->dc_sc);
4168                                 break;
4169
4170                         default:
4171                                 rcode = E_SUB_VECTOR_UNKNOWN;
4172                                 break;
4173                 }
4174
4175                 /* Let Sender Know if SUM of SV length's is
4176                  * larger then length in MVID length field
4177                  */
4178                 if((vlen -= rsv->svl) < 0)
4179                         rcode = E_VECTOR_LENGTH_ERROR;
4180
4181                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4182         }
4183
4184         if(rcode == POSITIVE_ACK)
4185         {
4186                 /* Let Sender Know if MVID length field
4187                  * is larger then SUM of SV length's
4188                  */
4189                 if(vlen != 0)
4190                         rcode = E_VECTOR_LENGTH_ERROR;
4191                 else
4192                 {
4193                         /* Let Sender Know if Expected SV Missing */
4194                         if((svectors & R_TX_FORWARD) ^ R_TX_FORWARD)
4195                                 rcode = E_MISSING_SUB_VECTOR;
4196                 }
4197         }
4198
4199         return (rcode);
4200 }
4201
4202 static int smctr_rcv_rq_addr_state_attch(struct net_device *dev,
4203         MAC_HEADER *rmf, __u16 *correlator)
4204 {
4205         MAC_SUB_VECTOR *rsv;
4206         signed short vlen;
4207         __u16 rcode = POSITIVE_ACK;
4208         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4209
4210         /* Remove MVID Length from total length */
4211         vlen = (signed short)rmf->vl - 4;
4212
4213         /* Point to First SVID */
4214         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4215
4216         /* Search for Appropriate SVID's */
4217         while((vlen > 0) && (rcode == POSITIVE_ACK))
4218         {
4219                 switch(rsv->svi)
4220                 {
4221                         case CORRELATOR:
4222                                 svectors |= F_CORRELATOR;
4223                                 rcode = smctr_set_corr(dev, rsv, correlator);
4224                                 break;
4225
4226                         default:
4227                                 rcode = E_SUB_VECTOR_UNKNOWN;
4228                                 break;
4229                 }
4230
4231                 /* Let Sender Know if SUM of SV length's is
4232                  * larger then length in MVID length field
4233                  */
4234                 if((vlen -= rsv->svl) < 0)
4235                         rcode = E_VECTOR_LENGTH_ERROR;
4236
4237                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4238         }
4239
4240         if(rcode == POSITIVE_ACK)
4241         {
4242                 /* Let Sender Know if MVID length field
4243                  * is larger then SUM of SV length's
4244                  */
4245                 if(vlen != 0)
4246                         rcode = E_VECTOR_LENGTH_ERROR;
4247                 else
4248                 {
4249                         /* Let Sender Know if Expected SVID Missing */
4250                         if((svectors & R_RQ_ATTCH_STATE_ADDR) 
4251                                 ^ R_RQ_ATTCH_STATE_ADDR)
4252                                 rcode = E_MISSING_SUB_VECTOR;
4253                         }
4254         }
4255
4256         return (rcode);
4257 }
4258
4259 static int smctr_rcv_unknown(struct net_device *dev, MAC_HEADER *rmf,
4260         __u16 *correlator)
4261 {
4262         MAC_SUB_VECTOR *rsv;
4263         signed short vlen;
4264
4265         *correlator = 0;
4266
4267         /* Remove MVID Length from total length */
4268         vlen = (signed short)rmf->vl - 4;
4269
4270         /* Point to First SVID */
4271         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4272
4273         /* Search for CORRELATOR for RSP to UNKNOWN */
4274         while((vlen > 0) && (*correlator == 0))
4275         {
4276                 switch(rsv->svi)
4277                 {
4278                         case CORRELATOR:
4279                                 smctr_set_corr(dev, rsv, correlator);
4280                                 break;
4281
4282                         default:
4283                                 break;
4284                 }
4285
4286                 vlen -= rsv->svl;
4287                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4288         }
4289
4290         return (E_UNRECOGNIZED_VECTOR_ID);
4291 }
4292
4293 /*
4294  * Reset the 825 NIC and exit w:
4295  * 1. The NIC reset cleared (non-reset state), halted and un-initialized.
4296  * 2. TINT masked.
4297  * 3. CBUSY masked.
4298  * 4. TINT clear.
4299  * 5. CBUSY clear.
4300  */
4301 static int smctr_reset_adapter(struct net_device *dev)
4302 {
4303         struct net_local *tp = netdev_priv(dev);
4304         int ioaddr = dev->base_addr;
4305
4306         /* Reseting the NIC will put it in a halted and un-initialized state. */        smctr_set_trc_reset(ioaddr);
4307         mdelay(200); /* ~2 ms */
4308
4309         smctr_clear_trc_reset(ioaddr);
4310         mdelay(200); /* ~2 ms */
4311
4312         /* Remove any latched interrupts that occurred prior to reseting the
4313          * adapter or possibily caused by line glitches due to the reset.
4314          */
4315         outb(tp->trc_mask | CSR_CLRTINT | CSR_CLRCBUSY, ioaddr + CSR);
4316
4317         return (0);
4318 }
4319
4320 static int smctr_restart_tx_chain(struct net_device *dev, short queue)
4321 {
4322         struct net_local *tp = netdev_priv(dev);
4323         int err = 0;
4324
4325         if(smctr_debug > 10)
4326                 printk(KERN_DEBUG "%s: smctr_restart_tx_chain\n", dev->name);
4327
4328         if(tp->num_tx_fcbs_used[queue] != 0
4329                 && tp->tx_queue_status[queue] == NOT_TRANSMITING)
4330         {
4331                 tp->tx_queue_status[queue] = TRANSMITING;
4332                 err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
4333         }
4334
4335         return (err);
4336 }
4337
4338 static int smctr_ring_status_chg(struct net_device *dev)
4339 {
4340         struct net_local *tp = netdev_priv(dev);
4341
4342         if(smctr_debug > 10)
4343                 printk(KERN_DEBUG "%s: smctr_ring_status_chg\n", dev->name);
4344
4345         /* Check for ring_status_flag: whenever MONITOR_STATE_BIT
4346          * Bit is set, check value of monitor_state, only then we
4347          * enable and start transmit/receive timeout (if and only
4348          * if it is MS_ACTIVE_MONITOR_STATE or MS_STANDBY_MONITOR_STATE)
4349          */
4350         if(tp->ring_status_flags == MONITOR_STATE_CHANGED)
4351         {
4352                 if((tp->monitor_state == MS_ACTIVE_MONITOR_STATE)
4353                         || (tp->monitor_state == MS_STANDBY_MONITOR_STATE))
4354                 {
4355                         tp->monitor_state_ready = 1;
4356                 }
4357                 else
4358                 {
4359                         /* if adapter is NOT in either active monitor
4360                          * or standby monitor state => Disable
4361                          * transmit/receive timeout.
4362                          */
4363                         tp->monitor_state_ready = 0;
4364
4365                         /* Ring speed problem, switching to auto mode. */
4366                         if(tp->monitor_state == MS_MONITOR_FSM_INACTIVE
4367                                 && !tp->cleanup)
4368                         {
4369                                 printk(KERN_INFO "%s: Incorrect ring speed switching.\n",
4370                                         dev->name);
4371                                 smctr_set_ring_speed(dev);
4372                         }
4373                 }
4374         }
4375
4376         if(!(tp->ring_status_flags & RING_STATUS_CHANGED))
4377                 return (0);
4378
4379         switch(tp->ring_status)
4380         {
4381                 case RING_RECOVERY:
4382                         printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
4383                         tp->current_ring_status |= RING_RECOVERY;
4384                         break;
4385
4386                 case SINGLE_STATION:
4387                         printk(KERN_INFO "%s: Single Statinon\n", dev->name);
4388                         tp->current_ring_status |= SINGLE_STATION;
4389                         break;
4390
4391                 case COUNTER_OVERFLOW:
4392                         printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
4393                         tp->current_ring_status |= COUNTER_OVERFLOW;
4394                         break;
4395
4396                 case REMOVE_RECEIVED:
4397                         printk(KERN_INFO "%s: Remove Received\n", dev->name);
4398                         tp->current_ring_status |= REMOVE_RECEIVED;
4399                         break;
4400
4401                 case AUTO_REMOVAL_ERROR:
4402                         printk(KERN_INFO "%s: Auto Remove Error\n", dev->name);
4403                         tp->current_ring_status |= AUTO_REMOVAL_ERROR;
4404                         break;
4405
4406                 case LOBE_WIRE_FAULT:
4407                         printk(KERN_INFO "%s: Lobe Wire Fault\n", dev->name);
4408                         tp->current_ring_status |= LOBE_WIRE_FAULT;
4409                         break;
4410
4411                 case TRANSMIT_BEACON:
4412                         printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
4413                         tp->current_ring_status |= TRANSMIT_BEACON;
4414                         break;
4415
4416                 case SOFT_ERROR:
4417                         printk(KERN_INFO "%s: Soft Error\n", dev->name);
4418                         tp->current_ring_status |= SOFT_ERROR;
4419                         break;
4420
4421                 case HARD_ERROR:
4422                         printk(KERN_INFO "%s: Hard Error\n", dev->name);
4423                         tp->current_ring_status |= HARD_ERROR;
4424                         break;
4425
4426                 case SIGNAL_LOSS:
4427                         printk(KERN_INFO "%s: Signal Loss\n", dev->name);
4428                         tp->current_ring_status |= SIGNAL_LOSS;
4429                         break;
4430
4431                 default:
4432                         printk(KERN_INFO "%s: Unknown ring status change\n",
4433                                 dev->name);
4434                         break;
4435         }
4436
4437         return (0);
4438 }
4439
4440 static int smctr_rx_frame(struct net_device *dev)
4441 {
4442         struct net_local *tp = netdev_priv(dev);
4443         __u16 queue, status, rx_size, err = 0;
4444         __u8 *pbuff;
4445
4446         if(smctr_debug > 10)
4447                 printk(KERN_DEBUG "%s: smctr_rx_frame\n", dev->name);
4448
4449         queue = tp->receive_queue_number;
4450
4451         while((status = tp->rx_fcb_curr[queue]->frame_status) != SUCCESS)
4452         {
4453                 err = HARDWARE_FAILED;
4454
4455                 if(((status & 0x007f) == 0)
4456                         || ((tp->receive_mask & ACCEPT_ERR_PACKETS) != 0))
4457                 {
4458                         /* frame length less the CRC (4 bytes) + FS (1 byte) */
4459                         rx_size = tp->rx_fcb_curr[queue]->frame_length - 5;
4460
4461                         pbuff = smctr_get_rx_pointer(dev, queue);
4462
4463                         smctr_set_page(dev, pbuff);
4464                         smctr_disable_16bit(dev);
4465
4466                         /* pbuff points to addr within one page */
4467                         pbuff = (__u8 *)PAGE_POINTER(pbuff);
4468
4469                         if(queue == NON_MAC_QUEUE)
4470                         {
4471                                 struct sk_buff *skb;
4472
4473                                 skb = dev_alloc_skb(rx_size);
4474                                 if (skb) {
4475                                         skb_put(skb, rx_size);
4476
4477                                         skb_copy_to_linear_data(skb, pbuff, rx_size);
4478
4479                                         /* Update Counters */
4480                                         tp->MacStat.rx_packets++;
4481                                         tp->MacStat.rx_bytes += skb->len;
4482
4483                                         /* Kick the packet on up. */
4484                                         skb->protocol = tr_type_trans(skb, dev);
4485                                         netif_rx(skb);
4486                                         dev->last_rx = jiffies;
4487                                 } else {
4488                                 }
4489                         }
4490                         else
4491                                 smctr_process_rx_packet((MAC_HEADER *)pbuff,
4492                                         rx_size, dev, status);
4493                 }
4494
4495                 smctr_enable_16bit(dev);
4496                 smctr_set_page(dev, (__u8 *)tp->ram_access);
4497                 smctr_update_rx_chain(dev, queue);
4498
4499                 if(err != SUCCESS)
4500                         break;
4501         }
4502
4503         return (err);
4504 }
4505
4506 static int smctr_send_dat(struct net_device *dev)
4507 {
4508         struct net_local *tp = netdev_priv(dev);
4509         unsigned int i, err;
4510         MAC_HEADER *tmf;
4511         FCBlock *fcb;
4512
4513         if(smctr_debug > 10)
4514                 printk(KERN_DEBUG "%s: smctr_send_dat\n", dev->name);
4515
4516         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE,
4517                 sizeof(MAC_HEADER))) == (FCBlock *)(-1L))
4518         {
4519                 return (OUT_OF_RESOURCES);
4520         }
4521
4522         /* Initialize DAT Data Fields. */
4523         tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4524         tmf->ac = MSB(AC_FC_DAT);
4525         tmf->fc = LSB(AC_FC_DAT);
4526
4527         for(i = 0; i < 6; i++)
4528         {
4529                 tmf->sa[i] = dev->dev_addr[i];
4530                 tmf->da[i] = dev->dev_addr[i];
4531
4532         }
4533
4534         tmf->vc        = DAT;
4535         tmf->dc_sc     = DC_RS | SC_RS;
4536         tmf->vl        = 4;
4537         tmf->vl        = SWAP_BYTES(tmf->vl);
4538
4539         /* Start Transmit. */
4540         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4541                 return (err);
4542
4543         /* Wait for Transmit to Complete */
4544         for(i = 0; i < 10000; i++)
4545         {
4546                 if(fcb->frame_status & FCB_COMMAND_DONE)
4547                         break;
4548                 mdelay(1);
4549         }
4550
4551         /* Check if GOOD frame Tx'ed. */
4552         if(!(fcb->frame_status &  FCB_COMMAND_DONE)
4553                 || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
4554         {
4555                 return (INITIALIZE_FAILED);
4556         }
4557
4558         /* De-allocated Tx FCB and Frame Buffer
4559          * The FCB must be de-allocated manually if executing with
4560          * interrupts disabled, other wise the ISR (LM_Service_Events)
4561          * will de-allocate it when the interrupt occurs.
4562          */
4563         tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4564         smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4565
4566         return (0);
4567 }
4568
4569 static void smctr_timeout(struct net_device *dev)
4570 {
4571         /*
4572          * If we get here, some higher level has decided we are broken.
4573          * There should really be a "kick me" function call instead.
4574          *
4575          * Resetting the token ring adapter takes a long time so just
4576          * fake transmission time and go on trying. Our own timeout
4577          * routine is in sktr_timer_chk()
4578          */
4579         dev->trans_start = jiffies;
4580         netif_wake_queue(dev);
4581 }
4582
4583 /*
4584  * Gets skb from system, queues it and checks if it can be sent
4585  */
4586 static int smctr_send_packet(struct sk_buff *skb, struct net_device *dev)
4587 {
4588         struct net_local *tp = netdev_priv(dev);
4589
4590         if(smctr_debug > 10)
4591                 printk(KERN_DEBUG "%s: smctr_send_packet\n", dev->name);
4592
4593         /*
4594          * Block a transmit overlap
4595          */
4596          
4597         netif_stop_queue(dev);
4598
4599         if(tp->QueueSkb == 0)
4600                 return (1);     /* Return with tbusy set: queue full */
4601
4602         tp->QueueSkb--;
4603         skb_queue_tail(&tp->SendSkbQueue, skb);
4604         smctr_hardware_send_packet(dev, tp);
4605         if(tp->QueueSkb > 0)
4606                 netif_wake_queue(dev);
4607                 
4608         return (0);
4609 }
4610
4611 static int smctr_send_lobe_media_test(struct net_device *dev)
4612 {
4613         struct net_local *tp = netdev_priv(dev);
4614         MAC_SUB_VECTOR *tsv;
4615         MAC_HEADER *tmf;
4616         FCBlock *fcb;
4617         __u32 i;
4618         int err;
4619
4620         if(smctr_debug > 15)
4621                 printk(KERN_DEBUG "%s: smctr_send_lobe_media_test\n", dev->name);
4622
4623         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(struct trh_hdr)
4624                 + S_WRAP_DATA + S_WRAP_DATA)) == (FCBlock *)(-1L))
4625         {
4626                 return (OUT_OF_RESOURCES);
4627         }
4628
4629         /* Initialize DAT Data Fields. */
4630         tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4631         tmf->ac = MSB(AC_FC_LOBE_MEDIA_TEST);
4632         tmf->fc = LSB(AC_FC_LOBE_MEDIA_TEST);
4633
4634         for(i = 0; i < 6; i++)
4635         {
4636                 tmf->da[i] = 0;
4637                 tmf->sa[i] = dev->dev_addr[i];
4638         }
4639
4640         tmf->vc        = LOBE_MEDIA_TEST;
4641         tmf->dc_sc     = DC_RS | SC_RS;
4642         tmf->vl        = 4;
4643
4644         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4645         smctr_make_wrap_data(dev, tsv);
4646         tmf->vl += tsv->svl;
4647
4648         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4649         smctr_make_wrap_data(dev, tsv);
4650         tmf->vl += tsv->svl;
4651
4652         /* Start Transmit. */
4653         tmf->vl = SWAP_BYTES(tmf->vl);
4654         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4655                 return (err);
4656
4657         /* Wait for Transmit to Complete. (10 ms). */
4658         for(i=0; i < 10000; i++)
4659         {
4660                 if(fcb->frame_status & FCB_COMMAND_DONE)
4661                         break;
4662                 mdelay(1);
4663         }
4664
4665         /* Check if GOOD frame Tx'ed */
4666         if(!(fcb->frame_status & FCB_COMMAND_DONE)
4667                 || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
4668         {
4669                 return (LOBE_MEDIA_TEST_FAILED);
4670         }
4671
4672         /* De-allocated Tx FCB and Frame Buffer
4673          * The FCB must be de-allocated manually if executing with
4674          * interrupts disabled, other wise the ISR (LM_Service_Events)
4675          * will de-allocate it when the interrupt occurs.
4676          */
4677         tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4678         smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4679
4680         return (0);
4681 }
4682
4683 static int smctr_send_rpt_addr(struct net_device *dev, MAC_HEADER *rmf,
4684         __u16 correlator)
4685 {
4686         MAC_HEADER *tmf;
4687         MAC_SUB_VECTOR *tsv;
4688         FCBlock *fcb;
4689
4690         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4691                 + S_CORRELATOR + S_PHYSICAL_DROP + S_UPSTREAM_NEIGHBOR_ADDRESS
4692                 + S_ADDRESS_MODIFER + S_GROUP_ADDRESS + S_FUNCTIONAL_ADDRESS))
4693                 == (FCBlock *)(-1L))
4694         {
4695                 return (0);
4696         }
4697
4698         tmf             = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4699         tmf->vc         = RPT_ADDR;
4700         tmf->dc_sc      = (rmf->dc_sc & SC_MASK) << 4;
4701         tmf->vl         = 4;
4702
4703         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ADDR);
4704
4705         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4706         smctr_make_corr(dev, tsv, correlator);
4707
4708         tmf->vl += tsv->svl;
4709         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4710         smctr_make_phy_drop_num(dev, tsv);
4711
4712         tmf->vl += tsv->svl;
4713         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4714         smctr_make_upstream_neighbor_addr(dev, tsv);
4715
4716         tmf->vl += tsv->svl;
4717         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4718         smctr_make_addr_mod(dev, tsv);
4719
4720         tmf->vl += tsv->svl;
4721         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4722         smctr_make_group_addr(dev, tsv);
4723
4724         tmf->vl += tsv->svl;
4725         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4726         smctr_make_funct_addr(dev, tsv);
4727
4728         tmf->vl += tsv->svl;
4729
4730         /* Subtract out MVID and MVL which is
4731          * include in both vl and MAC_HEADER
4732          */
4733 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4734         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4735 */
4736         tmf->vl = SWAP_BYTES(tmf->vl);
4737
4738         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4739 }
4740
4741 static int smctr_send_rpt_attch(struct net_device *dev, MAC_HEADER *rmf,
4742         __u16 correlator)
4743 {
4744         MAC_HEADER *tmf;
4745         MAC_SUB_VECTOR *tsv;
4746         FCBlock *fcb;
4747
4748         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4749                 + S_CORRELATOR + S_PRODUCT_INSTANCE_ID + S_FUNCTIONAL_ADDRESS
4750                 + S_AUTHORIZED_FUNCTION_CLASS + S_AUTHORIZED_ACCESS_PRIORITY))
4751                 == (FCBlock *)(-1L))
4752         {
4753                 return (0);
4754         }
4755
4756         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4757         tmf->vc    = RPT_ATTCH;
4758         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4759         tmf->vl    = 4;
4760
4761         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ATTCH);
4762
4763         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4764         smctr_make_corr(dev, tsv, correlator);
4765
4766         tmf->vl += tsv->svl;
4767         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4768         smctr_make_product_id(dev, tsv);
4769
4770         tmf->vl += tsv->svl;
4771         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4772         smctr_make_funct_addr(dev, tsv);
4773
4774         tmf->vl += tsv->svl;
4775         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4776         smctr_make_auth_funct_class(dev, tsv);
4777
4778         tmf->vl += tsv->svl;
4779         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4780         smctr_make_access_pri(dev, tsv);
4781
4782         tmf->vl += tsv->svl;
4783
4784         /* Subtract out MVID and MVL which is
4785          * include in both vl and MAC_HEADER
4786          */
4787 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4788         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4789 */
4790         tmf->vl = SWAP_BYTES(tmf->vl);
4791
4792         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4793 }
4794
4795 static int smctr_send_rpt_state(struct net_device *dev, MAC_HEADER *rmf,
4796         __u16 correlator)
4797 {
4798         MAC_HEADER *tmf;
4799         MAC_SUB_VECTOR *tsv;
4800         FCBlock *fcb;
4801
4802         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4803                 + S_CORRELATOR + S_RING_STATION_VERSION_NUMBER
4804                 + S_RING_STATION_STATUS + S_STATION_IDENTIFER))
4805                 == (FCBlock *)(-1L))
4806         {
4807                 return (0);
4808         }
4809
4810         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4811         tmf->vc    = RPT_STATE;
4812         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4813         tmf->vl    = 4;
4814
4815         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_STATE);
4816
4817         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4818         smctr_make_corr(dev, tsv, correlator);
4819
4820         tmf->vl += tsv->svl;
4821         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4822         smctr_make_ring_station_version(dev, tsv);
4823
4824         tmf->vl += tsv->svl;
4825         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4826         smctr_make_ring_station_status(dev, tsv);
4827
4828         tmf->vl += tsv->svl;
4829         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4830         smctr_make_station_id(dev, tsv);
4831
4832         tmf->vl += tsv->svl;
4833
4834         /* Subtract out MVID and MVL which is
4835          * include in both vl and MAC_HEADER
4836          */
4837 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4838         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4839 */
4840         tmf->vl = SWAP_BYTES(tmf->vl);
4841
4842         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4843 }
4844
4845 static int smctr_send_rpt_tx_forward(struct net_device *dev,
4846         MAC_HEADER *rmf, __u16 tx_fstatus)
4847 {
4848         MAC_HEADER *tmf;
4849         MAC_SUB_VECTOR *tsv;
4850         FCBlock *fcb;
4851
4852         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4853                 + S_TRANSMIT_STATUS_CODE)) == (FCBlock *)(-1L))
4854         {
4855                 return (0);
4856         }
4857
4858         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4859         tmf->vc    = RPT_TX_FORWARD;
4860         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4861         tmf->vl    = 4;
4862
4863         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_TX_FORWARD);
4864
4865         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4866         smctr_make_tx_status_code(dev, tsv, tx_fstatus);
4867
4868         tmf->vl += tsv->svl;
4869
4870         /* Subtract out MVID and MVL which is
4871          * include in both vl and MAC_HEADER
4872          */
4873 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4874         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4875 */
4876         tmf->vl = SWAP_BYTES(tmf->vl);
4877
4878         return(smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4879 }
4880
4881 static int smctr_send_rsp(struct net_device *dev, MAC_HEADER *rmf,
4882         __u16 rcode, __u16 correlator)
4883 {
4884         MAC_HEADER *tmf;
4885         MAC_SUB_VECTOR *tsv;
4886         FCBlock *fcb;
4887
4888         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4889                 + S_CORRELATOR + S_RESPONSE_CODE)) == (FCBlock *)(-1L))
4890         {
4891                 return (0);
4892         }
4893
4894         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4895         tmf->vc    = RSP;
4896         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4897         tmf->vl    = 4;
4898
4899         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RSP);
4900
4901         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4902         smctr_make_corr(dev, tsv, correlator);
4903
4904         return (0);
4905 }
4906
4907 static int smctr_send_rq_init(struct net_device *dev)
4908 {
4909         struct net_local *tp = netdev_priv(dev);
4910         MAC_HEADER *tmf;
4911         MAC_SUB_VECTOR *tsv;
4912         FCBlock *fcb;
4913         unsigned int i, count = 0;
4914         __u16 fstatus;
4915         int err;
4916
4917         do {
4918                 if(((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4919                         + S_PRODUCT_INSTANCE_ID + S_UPSTREAM_NEIGHBOR_ADDRESS
4920                         + S_RING_STATION_VERSION_NUMBER + S_ADDRESS_MODIFER))
4921                         == (FCBlock *)(-1L)))
4922                 {
4923                         return (0);
4924                 }
4925
4926                 tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4927                 tmf->vc    = RQ_INIT;
4928                 tmf->dc_sc = DC_RPS | SC_RS;
4929                 tmf->vl    = 4;
4930
4931                 smctr_make_8025_hdr(dev, NULL, tmf, AC_FC_RQ_INIT);
4932
4933                 tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4934                 smctr_make_product_id(dev, tsv);
4935
4936                 tmf->vl += tsv->svl;
4937                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4938                 smctr_make_upstream_neighbor_addr(dev, tsv);
4939
4940                 tmf->vl += tsv->svl;
4941                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4942                 smctr_make_ring_station_version(dev, tsv);
4943
4944                 tmf->vl += tsv->svl;
4945                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4946                 smctr_make_addr_mod(dev, tsv);
4947
4948                 tmf->vl += tsv->svl;
4949
4950                 /* Subtract out MVID and MVL which is
4951                  * include in both vl and MAC_HEADER
4952                  */
4953 /*              fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4954                 fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4955 */
4956                 tmf->vl = SWAP_BYTES(tmf->vl);
4957
4958                 if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4959                         return (err);
4960
4961                 /* Wait for Transmit to Complete */
4962                 for(i = 0; i < 10000; i++) 
4963                 {
4964                         if(fcb->frame_status & FCB_COMMAND_DONE)
4965                                 break;
4966                         mdelay(1);
4967                 }
4968
4969                 /* Check if GOOD frame Tx'ed */
4970                 fstatus = fcb->frame_status;
4971
4972                 if(!(fstatus & FCB_COMMAND_DONE))
4973                         return (HARDWARE_FAILED);
4974
4975                 if(!(fstatus & FCB_TX_STATUS_E))
4976                         count++;
4977
4978                 /* De-allocated Tx FCB and Frame Buffer
4979                  * The FCB must be de-allocated manually if executing with
4980                  * interrupts disabled, other wise the ISR (LM_Service_Events)
4981                  * will de-allocate it when the interrupt occurs.
4982                  */
4983                 tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4984                 smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4985         } while(count < 4 && ((fstatus & FCB_TX_AC_BITS) ^ FCB_TX_AC_BITS));
4986
4987         return (smctr_join_complete_state(dev));
4988 }
4989
4990 static int smctr_send_tx_forward(struct net_device *dev, MAC_HEADER *rmf,
4991         __u16 *tx_fstatus)
4992 {
4993         struct net_local *tp = netdev_priv(dev);
4994         FCBlock *fcb;
4995         unsigned int i;
4996         int err;
4997
4998         /* Check if this is the END POINT of the Transmit Forward Chain. */
4999         if(rmf->vl <= 18)
5000                 return (0);
5001
5002         /* Allocate Transmit FCB only by requesting 0 bytes
5003          * of data buffer.
5004          */
5005         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, 0)) == (FCBlock *)(-1L))
5006                 return (0);
5007
5008         /* Set pointer to Transmit Frame Buffer to the data
5009          * portion of the received TX Forward frame, making
5010          * sure to skip over the Vector Code (vc) and Vector
5011          * length (vl).
5012          */
5013         fcb->bdb_ptr->trc_data_block_ptr = TRC_POINTER((__u32)rmf 
5014                 + sizeof(MAC_HEADER) + 2);
5015         fcb->bdb_ptr->data_block_ptr     = (__u16 *)((__u32)rmf 
5016                 + sizeof(MAC_HEADER) + 2);
5017
5018         fcb->frame_length                = rmf->vl - 4 - 2;
5019         fcb->bdb_ptr->buffer_length      = rmf->vl - 4 - 2;
5020
5021         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
5022                 return (err);
5023
5024         /* Wait for Transmit to Complete */
5025         for(i = 0; i < 10000; i++) 
5026         {
5027                 if(fcb->frame_status & FCB_COMMAND_DONE)
5028                         break;
5029                 mdelay(1);
5030         }
5031
5032         /* Check if GOOD frame Tx'ed */
5033         if(!(fcb->frame_status & FCB_COMMAND_DONE))
5034         {
5035                 if((err = smctr_issue_resume_tx_fcb_cmd(dev, MAC_QUEUE)))
5036                         return (err);
5037
5038                 for(i = 0; i < 10000; i++) 
5039                 {
5040                         if(fcb->frame_status & FCB_COMMAND_DONE)
5041                                 break;
5042                         mdelay(1);
5043                 }
5044
5045                 if(!(fcb->frame_status & FCB_COMMAND_DONE))
5046                         return (HARDWARE_FAILED);
5047         }
5048
5049         *tx_fstatus = fcb->frame_status;
5050
5051         return (A_FRAME_WAS_FORWARDED);
5052 }
5053
5054 static int smctr_set_auth_access_pri(struct net_device *dev,
5055         MAC_SUB_VECTOR *rsv)
5056 {
5057         struct net_local *tp = netdev_priv(dev);
5058
5059         if(rsv->svl != S_AUTHORIZED_ACCESS_PRIORITY)
5060                 return (E_SUB_VECTOR_LENGTH_ERROR);
5061
5062         tp->authorized_access_priority = (rsv->svv[0] << 8 | rsv->svv[1]);
5063
5064         return (POSITIVE_ACK);
5065 }
5066
5067 static int smctr_set_auth_funct_class(struct net_device *dev,
5068         MAC_SUB_VECTOR *rsv)
5069 {
5070         struct net_local *tp = netdev_priv(dev);
5071
5072         if(rsv->svl != S_AUTHORIZED_FUNCTION_CLASS)
5073                 return (E_SUB_VECTOR_LENGTH_ERROR);
5074
5075         tp->authorized_function_classes = (rsv->svv[0] << 8 | rsv->svv[1]);
5076
5077         return (POSITIVE_ACK);
5078 }
5079
5080 static int smctr_set_corr(struct net_device *dev, MAC_SUB_VECTOR *rsv,
5081         __u16 *correlator)
5082 {
5083         if(rsv->svl != S_CORRELATOR)
5084                 return (E_SUB_VECTOR_LENGTH_ERROR);
5085
5086         *correlator = (rsv->svv[0] << 8 | rsv->svv[1]);
5087
5088         return (POSITIVE_ACK);
5089 }
5090
5091 static int smctr_set_error_timer_value(struct net_device *dev,
5092         MAC_SUB_VECTOR *rsv)
5093 {
5094         __u16 err_tval;
5095         int err;
5096
5097         if(rsv->svl != S_ERROR_TIMER_VALUE)
5098                 return (E_SUB_VECTOR_LENGTH_ERROR);
5099
5100         err_tval = (rsv->svv[0] << 8 | rsv->svv[1])*10;
5101
5102         smctr_issue_write_word_cmd(dev, RW_TER_THRESHOLD, &err_tval);
5103
5104         if((err = smctr_wait_cmd(dev)))
5105                 return (err);
5106
5107         return (POSITIVE_ACK);
5108 }
5109
5110 static int smctr_set_frame_forward(struct net_device *dev,
5111         MAC_SUB_VECTOR *rsv, __u8 dc_sc)
5112 {
5113         if((rsv->svl < 2) || (rsv->svl > S_FRAME_FORWARD))
5114                 return (E_SUB_VECTOR_LENGTH_ERROR);
5115
5116         if((dc_sc & DC_MASK) != DC_CRS)
5117         {
5118                 if(rsv->svl >= 2 && rsv->svl < 20)
5119                         return (E_TRANSMIT_FORWARD_INVALID);
5120
5121                 if((rsv->svv[0] != 0) || (rsv->svv[1] != 0))
5122                         return (E_TRANSMIT_FORWARD_INVALID);
5123         }
5124
5125         return (POSITIVE_ACK);
5126 }
5127
5128 static int smctr_set_local_ring_num(struct net_device *dev,
5129         MAC_SUB_VECTOR *rsv)
5130 {
5131         struct net_local *tp = netdev_priv(dev);
5132
5133         if(rsv->svl != S_LOCAL_RING_NUMBER)
5134                 return (E_SUB_VECTOR_LENGTH_ERROR);
5135
5136         if(tp->ptr_local_ring_num)
5137                 *(__u16 *)(tp->ptr_local_ring_num) 
5138                         = (rsv->svv[0] << 8 | rsv->svv[1]);
5139
5140         return (POSITIVE_ACK);
5141 }
5142
5143 static unsigned short smctr_set_ctrl_attention(struct net_device *dev)
5144 {
5145         struct net_local *tp = netdev_priv(dev);
5146         int ioaddr = dev->base_addr;
5147
5148         if(tp->bic_type == BIC_585_CHIP)
5149                 outb((tp->trc_mask | HWR_CA), ioaddr + HWR);
5150         else
5151         {
5152                 outb((tp->trc_mask | CSR_CA), ioaddr + CSR);
5153                 outb(tp->trc_mask, ioaddr + CSR);
5154         }
5155
5156         return (0);
5157 }
5158
5159 static void smctr_set_multicast_list(struct net_device *dev)
5160 {
5161         if(smctr_debug > 10)
5162                 printk(KERN_DEBUG "%s: smctr_set_multicast_list\n", dev->name);
5163
5164         return;
5165 }
5166
5167 static int smctr_set_page(struct net_device *dev, __u8 *buf)
5168 {
5169         struct net_local *tp = netdev_priv(dev);
5170         __u8 amask;
5171         __u32 tptr;
5172
5173         tptr = (__u32)buf - (__u32)tp->ram_access;
5174         amask = (__u8)((tptr & PR_PAGE_MASK) >> 8);
5175         outb(amask, dev->base_addr + PR);
5176
5177         return (0);
5178 }
5179
5180 static int smctr_set_phy_drop(struct net_device *dev, MAC_SUB_VECTOR *rsv)
5181 {
5182         int err;
5183
5184         if(rsv->svl != S_PHYSICAL_DROP)
5185                 return (E_SUB_VECTOR_LENGTH_ERROR);
5186
5187         smctr_issue_write_byte_cmd(dev, RW_PHYSICAL_DROP_NUMBER, &rsv->svv[0]);
5188         if((err = smctr_wait_cmd(dev)))
5189                 return (err);
5190
5191         return (POSITIVE_ACK);
5192 }
5193
5194 /* Reset the ring speed to the opposite of what it was. This auto-pilot
5195  * mode requires a complete reset and re-init of the adapter.
5196  */
5197 static int smctr_set_ring_speed(struct net_device *dev)
5198 {
5199         struct net_local *tp = netdev_priv(dev);
5200         int err;
5201
5202         if(tp->media_type == MEDIA_UTP_16)
5203                 tp->media_type = MEDIA_UTP_4;
5204         else
5205                 tp->media_type = MEDIA_UTP_16;
5206
5207         smctr_enable_16bit(dev);
5208
5209         /* Re-Initialize adapter's internal registers */
5210         smctr_reset_adapter(dev);
5211
5212         if((err = smctr_init_card_real(dev)))
5213                 return (err);
5214
5215         smctr_enable_bic_int(dev);
5216
5217         if((err = smctr_issue_enable_int_cmd(dev, TRC_INTERRUPT_ENABLE_MASK)))
5218                 return (err);
5219
5220         smctr_disable_16bit(dev);
5221
5222         return (0);
5223 }
5224
5225 static int smctr_set_rx_look_ahead(struct net_device *dev)
5226 {
5227         struct net_local *tp = netdev_priv(dev);
5228         __u16 sword, rword;
5229
5230         if(smctr_debug > 10)
5231                 printk(KERN_DEBUG "%s: smctr_set_rx_look_ahead_flag\n", dev->name);
5232
5233         tp->adapter_flags &= ~(FORCED_16BIT_MODE);
5234         tp->adapter_flags |= RX_VALID_LOOKAHEAD;
5235
5236         if(tp->adapter_bus == BUS_ISA16_TYPE)
5237         {
5238                 sword = *((__u16 *)(tp->ram_access));
5239                 *((__u16 *)(tp->ram_access)) = 0x1234;
5240
5241                 smctr_disable_16bit(dev);
5242                 rword = *((__u16 *)(tp->ram_access));
5243                 smctr_enable_16bit(dev);
5244
5245                 if(rword != 0x1234)
5246                         tp->adapter_flags |= FORCED_16BIT_MODE;
5247
5248                 *((__u16 *)(tp->ram_access)) = sword;
5249         }
5250
5251         return (0);
5252 }
5253
5254 static int smctr_set_trc_reset(int ioaddr)
5255 {
5256         __u8 r;
5257
5258         r = inb(ioaddr + MSR);
5259         outb(MSR_RST | r, ioaddr + MSR);
5260
5261         return (0);
5262 }
5263
5264 /*
5265  * This function can be called if the adapter is busy or not.
5266  */
5267 static int smctr_setup_single_cmd(struct net_device *dev,
5268         __u16 command, __u16 subcommand)
5269 {
5270         struct net_local *tp = netdev_priv(dev);
5271         unsigned int err;
5272
5273         if(smctr_debug > 10)
5274                 printk(KERN_DEBUG "%s: smctr_setup_single_cmd\n", dev->name);
5275
5276         if((err = smctr_wait_while_cbusy(dev)))
5277                 return (err);
5278
5279         if((err = (unsigned int)smctr_wait_cmd(dev)))
5280                 return (err);
5281
5282         tp->acb_head->cmd_done_status   = 0;
5283         tp->acb_head->cmd               = command;
5284         tp->acb_head->subcmd            = subcommand;
5285
5286         err = smctr_issue_resume_acb_cmd(dev);
5287
5288         return (err);
5289 }
5290
5291 /*
5292  * This function can not be called with the adapter busy.
5293  */
5294 static int smctr_setup_single_cmd_w_data(struct net_device *dev,
5295         __u16 command, __u16 subcommand)
5296 {
5297         struct net_local *tp = netdev_priv(dev);
5298
5299         tp->acb_head->cmd_done_status   = ACB_COMMAND_NOT_DONE;
5300         tp->acb_head->cmd               = command;
5301         tp->acb_head->subcmd            = subcommand;
5302         tp->acb_head->data_offset_lo
5303                 = (__u16)TRC_POINTER(tp->misc_command_data);
5304
5305         return(smctr_issue_resume_acb_cmd(dev));
5306 }
5307
5308 static char *smctr_malloc(struct net_device *dev, __u16 size)
5309 {
5310         struct net_local *tp = netdev_priv(dev);
5311         char *m;
5312
5313         m = (char *)(tp->ram_access + tp->sh_mem_used);
5314         tp->sh_mem_used += (__u32)size;
5315
5316         return (m);
5317 }
5318
5319 static int smctr_status_chg(struct net_device *dev)
5320 {
5321         struct net_local *tp = netdev_priv(dev);
5322
5323         if(smctr_debug > 10)
5324                 printk(KERN_DEBUG "%s: smctr_status_chg\n", dev->name);
5325
5326         switch(tp->status)
5327         {
5328                 case OPEN:
5329                         break;
5330
5331                 case CLOSED:
5332                         break;
5333
5334                 /* Interrupt driven open() completion. XXX */
5335                 case INITIALIZED:
5336                         tp->group_address_0 = 0;
5337                         tp->group_address[0] = 0;
5338                         tp->group_address[1] = 0;
5339                         tp->functional_address_0 = 0;
5340                         tp->functional_address[0] = 0;
5341                         tp->functional_address[1] = 0;
5342                         smctr_open_tr(dev);
5343                         break;
5344
5345                 default:
5346                         printk(KERN_INFO "%s: status change unknown %x\n",
5347                                 dev->name, tp->status);
5348                         break;
5349         }
5350
5351         return (0);
5352 }
5353
5354 static int smctr_trc_send_packet(struct net_device *dev, FCBlock *fcb,
5355         __u16 queue)
5356 {
5357         struct net_local *tp = netdev_priv(dev);
5358         int err = 0;
5359
5360         if(smctr_debug > 10)
5361                 printk(KERN_DEBUG "%s: smctr_trc_send_packet\n", dev->name);
5362
5363         fcb->info = FCB_CHAIN_END | FCB_ENABLE_TFS;
5364         if(tp->num_tx_fcbs[queue] != 1)
5365                 fcb->back_ptr->info = FCB_INTERRUPT_ENABLE | FCB_ENABLE_TFS;
5366
5367         if(tp->tx_queue_status[queue] == NOT_TRANSMITING)
5368         {
5369                 tp->tx_queue_status[queue] = TRANSMITING;
5370                 err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
5371         }
5372
5373         return (err);
5374 }
5375
5376 static __u16 smctr_tx_complete(struct net_device *dev, __u16 queue)
5377 {
5378         struct net_local *tp = netdev_priv(dev);
5379         __u16 status, err = 0;
5380         int cstatus;
5381
5382         if(smctr_debug > 10)
5383                 printk(KERN_DEBUG "%s: smctr_tx_complete\n", dev->name);
5384
5385         while((status = tp->tx_fcb_end[queue]->frame_status) != SUCCESS)
5386         {
5387                 if(status & 0x7e00 )
5388                 {
5389                         err = HARDWARE_FAILED;
5390                         break;
5391                 }
5392
5393                 if((err = smctr_update_tx_chain(dev, tp->tx_fcb_end[queue],
5394                         queue)) != SUCCESS)
5395                         break;
5396
5397                 smctr_disable_16bit(dev);
5398
5399                 if(tp->mode_bits & UMAC)
5400                 {
5401                         if(!(status & (FCB_TX_STATUS_AR1 | FCB_TX_STATUS_AR2)))
5402                                 cstatus = NO_SUCH_DESTINATION;
5403                         else
5404                         {
5405                                 if(!(status & (FCB_TX_STATUS_CR1 | FCB_TX_STATUS_CR2)))
5406                                         cstatus = DEST_OUT_OF_RESOURCES;
5407                                 else
5408                                 {
5409                                         if(status & FCB_TX_STATUS_E)
5410                                                 cstatus = MAX_COLLISIONS;
5411                                         else
5412                                                 cstatus = SUCCESS;
5413                                 }
5414                         }
5415                 }
5416                 else
5417                         cstatus = SUCCESS;
5418
5419                 if(queue == BUG_QUEUE)
5420                         err = SUCCESS;
5421
5422                 smctr_enable_16bit(dev);
5423                 if(err != SUCCESS)
5424                         break;
5425         }
5426
5427         return (err);
5428 }
5429
5430 static unsigned short smctr_tx_move_frame(struct net_device *dev,
5431         struct sk_buff *skb, __u8 *pbuff, unsigned int bytes)
5432 {
5433         struct net_local *tp = netdev_priv(dev);
5434         unsigned int ram_usable;
5435         __u32 flen, len, offset = 0;
5436         __u8 *frag, *page;
5437
5438         if(smctr_debug > 10)
5439                 printk(KERN_DEBUG "%s: smctr_tx_move_frame\n", dev->name);
5440
5441         ram_usable = ((unsigned int)tp->ram_usable) << 10;
5442         frag       = skb->data;
5443         flen       = skb->len;
5444
5445         while(flen > 0 && bytes > 0)
5446         {
5447                 smctr_set_page(dev, pbuff);
5448
5449                 offset = SMC_PAGE_OFFSET(pbuff);
5450
5451                 if(offset + flen > ram_usable)
5452                         len = ram_usable - offset;
5453                 else
5454                         len = flen;
5455
5456                 if(len > bytes)
5457                         len = bytes;
5458
5459                 page = (char *) (offset + tp->ram_access);
5460                 memcpy(page, frag, len);
5461
5462                 flen -=len;
5463                 bytes -= len;
5464                 frag += len;
5465                 pbuff += len;
5466         }
5467
5468         return (0);
5469 }
5470
5471 /* Update the error statistic counters for this adapter. */
5472 static int smctr_update_err_stats(struct net_device *dev)
5473 {
5474         struct net_local *tp = netdev_priv(dev);
5475         struct tr_statistics *tstat = &tp->MacStat;
5476
5477         if(tstat->internal_errors)
5478                 tstat->internal_errors
5479                         += *(tp->misc_command_data + 0) & 0x00ff;
5480
5481         if(tstat->line_errors)
5482                 tstat->line_errors += *(tp->misc_command_data + 0) >> 8;
5483
5484         if(tstat->A_C_errors)
5485                 tstat->A_C_errors += *(tp->misc_command_data + 1) & 0x00ff;
5486
5487         if(tstat->burst_errors)
5488                 tstat->burst_errors += *(tp->misc_command_data + 1) >> 8;
5489
5490         if(tstat->abort_delimiters)
5491                 tstat->abort_delimiters += *(tp->misc_command_data + 2) >> 8;
5492
5493         if(tstat->recv_congest_count)
5494                 tstat->recv_congest_count
5495                         += *(tp->misc_command_data + 3) & 0x00ff;
5496
5497         if(tstat->lost_frames)
5498                 tstat->lost_frames
5499                         += *(tp->misc_command_data + 3) >> 8;
5500
5501         if(tstat->frequency_errors)
5502                 tstat->frequency_errors += *(tp->misc_command_data + 4) & 0x00ff;
5503
5504         if(tstat->frame_copied_errors)
5505                  tstat->frame_copied_errors
5506                         += *(tp->misc_command_data + 4) >> 8;
5507
5508         if(tstat->token_errors)
5509                 tstat->token_errors += *(tp->misc_command_data + 5) >> 8;
5510
5511         return (0);
5512 }
5513
5514 static int smctr_update_rx_chain(struct net_device *dev, __u16 queue)
5515 {
5516         struct net_local *tp = netdev_priv(dev);
5517         FCBlock *fcb;
5518         BDBlock *bdb;
5519         __u16 size, len;
5520
5521         fcb = tp->rx_fcb_curr[queue];
5522         len = fcb->frame_length;
5523
5524         fcb->frame_status = 0;
5525         fcb->info = FCB_CHAIN_END;
5526         fcb->back_ptr->info = FCB_WARNING;
5527
5528         tp->rx_fcb_curr[queue] = tp->rx_fcb_curr[queue]->next_ptr;
5529
5530         /* update RX BDBs */
5531         size = (len >> RX_BDB_SIZE_SHIFT);
5532         if(len & RX_DATA_BUFFER_SIZE_MASK)
5533                 size += sizeof(BDBlock);
5534         size &= (~RX_BDB_SIZE_MASK);
5535
5536         /* check if wrap around */
5537         bdb = (BDBlock *)((__u32)(tp->rx_bdb_curr[queue]) + (__u32)(size));
5538         if((__u32)bdb >= (__u32)tp->rx_bdb_end[queue])
5539         {
5540                 bdb = (BDBlock *)((__u32)(tp->rx_bdb_head[queue])
5541                         + (__u32)(bdb) - (__u32)(tp->rx_bdb_end[queue]));
5542         }
5543
5544         bdb->back_ptr->info = BDB_CHAIN_END;
5545         tp->rx_bdb_curr[queue]->back_ptr->info = BDB_NOT_CHAIN_END;
5546         tp->rx_bdb_curr[queue] = bdb;
5547
5548         return (0);
5549 }
5550
5551 static int smctr_update_tx_chain(struct net_device *dev, FCBlock *fcb,
5552         __u16 queue)
5553 {
5554         struct net_local *tp = netdev_priv(dev);
5555
5556         if(smctr_debug > 20)
5557                 printk(KERN_DEBUG "smctr_update_tx_chain\n");
5558
5559         if(tp->num_tx_fcbs_used[queue] <= 0)
5560                 return (HARDWARE_FAILED);
5561         else
5562         {
5563                 if(tp->tx_buff_used[queue] < fcb->memory_alloc)
5564                 {
5565                         tp->tx_buff_used[queue] = 0;
5566                         return (HARDWARE_FAILED);
5567                 }
5568
5569                 tp->tx_buff_used[queue] -= fcb->memory_alloc;
5570
5571                 /* if all transmit buffer are cleared
5572                  * need to set the tx_buff_curr[] to tx_buff_head[]
5573                  * otherwise, tx buffer will be segregate and cannot
5574                  * accommodate and buffer greater than (curr - head) and
5575                  * (end - curr) since we do not allow wrap around allocation.
5576                  */
5577                 if(tp->tx_buff_used[queue] == 0)
5578                         tp->tx_buff_curr[queue] = tp->tx_buff_head[queue];
5579
5580                 tp->num_tx_fcbs_used[queue]--;
5581                 fcb->frame_status = 0;
5582                 tp->tx_fcb_end[queue] = fcb->next_ptr;
5583                 netif_wake_queue(dev);
5584                 return (0);
5585         }
5586 }
5587
5588 static int smctr_wait_cmd(struct net_device *dev)
5589 {
5590         struct net_local *tp = netdev_priv(dev);
5591         unsigned int loop_count = 0x20000;
5592
5593         if(smctr_debug > 10)
5594                 printk(KERN_DEBUG "%s: smctr_wait_cmd\n", dev->name);
5595
5596         while(loop_count)
5597         {
5598                 if(tp->acb_head->cmd_done_status & ACB_COMMAND_DONE)
5599                         break;
5600                 udelay(1);
5601                 loop_count--;
5602         }
5603
5604         if(loop_count == 0)
5605                 return(HARDWARE_FAILED);
5606
5607         if(tp->acb_head->cmd_done_status & 0xff)
5608                 return(HARDWARE_FAILED);
5609
5610         return (0);
5611 }
5612
5613 static int smctr_wait_while_cbusy(struct net_device *dev)
5614 {
5615         struct net_local *tp = netdev_priv(dev);
5616         unsigned int timeout = 0x20000;
5617         int ioaddr = dev->base_addr;
5618         __u8 r;
5619
5620         if(tp->bic_type == BIC_585_CHIP)
5621         {
5622                 while(timeout)
5623                 {
5624                         r = inb(ioaddr + HWR);
5625                         if((r & HWR_CBUSY) == 0)
5626                                 break;
5627                         timeout--;
5628                 }
5629         }
5630         else
5631         {
5632                 while(timeout)
5633                 {
5634                         r = inb(ioaddr + CSR);
5635                         if((r & CSR_CBUSY) == 0)
5636                                 break;
5637                         timeout--;
5638                 }
5639         }
5640
5641         if(timeout)
5642                 return (0);
5643         else
5644                 return (HARDWARE_FAILED);
5645 }
5646
5647 #ifdef MODULE
5648
5649 static struct net_device* dev_smctr[SMCTR_MAX_ADAPTERS];
5650 static int io[SMCTR_MAX_ADAPTERS];
5651 static int irq[SMCTR_MAX_ADAPTERS];
5652
5653 MODULE_LICENSE("GPL");
5654
5655 module_param_array(io, int, NULL, 0);
5656 module_param_array(irq, int, NULL, 0);
5657 module_param(ringspeed, int, 0);
5658
5659 static struct net_device * __init setup_card(int n)
5660 {
5661         struct net_device *dev = alloc_trdev(sizeof(struct net_local));
5662         int err;
5663         
5664         if (!dev)
5665                 return ERR_PTR(-ENOMEM);
5666
5667         dev->irq = irq[n];
5668         err = smctr_probe1(dev, io[n]);
5669         if (err) 
5670                 goto out;
5671                 
5672         err = register_netdev(dev);
5673         if (err)
5674                 goto out1;
5675         return dev;
5676  out1:
5677 #ifdef CONFIG_MCA_LEGACY
5678         { struct net_local *tp = netdev_priv(dev);
5679           if (tp->slot_num)
5680                 mca_mark_as_unused(tp->slot_num);
5681         }
5682 #endif
5683         release_region(dev->base_addr, SMCTR_IO_EXTENT);
5684         free_irq(dev->irq, dev);
5685 out:
5686         free_netdev(dev);
5687         return ERR_PTR(err);
5688 }
5689
5690 int __init init_module(void)
5691 {
5692         int i, found = 0;
5693         struct net_device *dev;
5694
5695         for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
5696                 dev = io[0]? setup_card(i) : smctr_probe(-1);
5697                 if (!IS_ERR(dev)) {
5698                         ++found;
5699                         dev_smctr[i] = dev;
5700                 }
5701         }
5702
5703         return found ? 0 : -ENODEV;
5704 }
5705
5706 void __exit cleanup_module(void)
5707 {
5708         int i;
5709
5710         for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
5711                 struct net_device *dev = dev_smctr[i];
5712
5713                 if (dev) {
5714
5715                         unregister_netdev(dev);
5716 #ifdef CONFIG_MCA_LEGACY
5717                         { struct net_local *tp = netdev_priv(dev);
5718                         if (tp->slot_num)
5719                                 mca_mark_as_unused(tp->slot_num);
5720                         }
5721 #endif
5722                         release_region(dev->base_addr, SMCTR_IO_EXTENT);
5723                         if (dev->irq)
5724                                 free_irq(dev->irq, dev);
5725
5726                         free_netdev(dev);
5727                 }
5728         }
5729 }
5730 #endif /* MODULE */