Pull sbs into release branch
[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 != 0)
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 == 0L)
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         SET_MODULE_OWNER(dev);
3587
3588         if (unit >= 0) {
3589                 sprintf(dev->name, "tr%d", unit);
3590                 netdev_boot_setup_check(dev);
3591         }
3592
3593         if (dev->base_addr > 0x1ff)    /* Check a single specified location. */
3594                 err = smctr_probe1(dev, dev->base_addr);
3595         else if(dev->base_addr != 0)  /* Don't probe at all. */
3596                 err =-ENXIO;
3597         else {
3598                 for (port = ports; *port; port++) {
3599                         err = smctr_probe1(dev, *port);
3600                         if (!err)
3601                                 break;
3602                 }
3603         }
3604         if (err)
3605                 goto out;
3606         err = register_netdev(dev);
3607         if (err)
3608                 goto out1;
3609         return dev;
3610 out1:
3611 #ifdef CONFIG_MCA_LEGACY
3612         { struct net_local *tp = netdev_priv(dev);
3613           if (tp->slot_num)
3614                 mca_mark_as_unused(tp->slot_num);
3615         }
3616 #endif
3617         release_region(dev->base_addr, SMCTR_IO_EXTENT);
3618         free_irq(dev->irq, dev);
3619 out:
3620         free_netdev(dev);
3621         return ERR_PTR(err);
3622 }
3623
3624
3625 static int __init smctr_probe1(struct net_device *dev, int ioaddr)
3626 {
3627         static unsigned version_printed;
3628         struct net_local *tp = netdev_priv(dev);
3629         int err;
3630         __u32 *ram;
3631
3632         if(smctr_debug && version_printed++ == 0)
3633                 printk(version);
3634
3635         spin_lock_init(&tp->lock);
3636         dev->base_addr = ioaddr;
3637
3638         /* Actually detect an adapter now. */
3639         err = smctr_chk_isa(dev);
3640         if(err < 0)
3641         {
3642                 if ((err = smctr_chk_mca(dev)) < 0) {
3643                         err = -ENODEV;
3644                         goto out;
3645                 }
3646         }
3647
3648         tp = netdev_priv(dev);
3649         dev->mem_start = tp->ram_base;
3650         dev->mem_end = dev->mem_start + 0x10000;
3651         ram = (__u32 *)phys_to_virt(dev->mem_start);
3652         tp->ram_access = *(__u32 *)&ram;
3653         tp->status = NOT_INITIALIZED;
3654
3655         err = smctr_load_firmware(dev);
3656         if(err != UCODE_PRESENT && err != SUCCESS)
3657         {
3658                 printk(KERN_ERR "%s: Firmware load failed (%d)\n", dev->name, err);
3659                 err = -EIO;
3660                 goto out;
3661         }
3662
3663         /* Allow user to specify ring speed on module insert. */
3664         if(ringspeed == 4)
3665                 tp->media_type = MEDIA_UTP_4;
3666         else
3667                 tp->media_type = MEDIA_UTP_16;
3668
3669         printk(KERN_INFO "%s: %s %s at Io %#4x, Irq %d, Rom %#4x, Ram %#4x.\n",
3670                 dev->name, smctr_name, smctr_model,
3671                 (unsigned int)dev->base_addr,
3672                 dev->irq, tp->rom_base, tp->ram_base);
3673
3674         dev->open               = smctr_open;
3675         dev->stop               = smctr_close;
3676         dev->hard_start_xmit    = smctr_send_packet;
3677         dev->tx_timeout         = smctr_timeout;
3678         dev->watchdog_timeo     = HZ;
3679         dev->get_stats          = smctr_get_stats;
3680         dev->set_multicast_list = &smctr_set_multicast_list;
3681         return (0);
3682
3683 out:
3684         return err;
3685 }
3686
3687 static int smctr_process_rx_packet(MAC_HEADER *rmf, __u16 size,
3688         struct net_device *dev, __u16 rx_status)
3689 {
3690         struct net_local *tp = netdev_priv(dev);
3691         struct sk_buff *skb;
3692         __u16 rcode, correlator;
3693         int err = 0;
3694         __u8 xframe = 1;
3695
3696         rmf->vl = SWAP_BYTES(rmf->vl);
3697         if(rx_status & FCB_RX_STATUS_DA_MATCHED)
3698         {
3699                 switch(rmf->vc)
3700                 {
3701                         /* Received MAC Frames Processed by RS. */
3702                         case INIT:
3703                                 if((rcode = smctr_rcv_init(dev, rmf, &correlator)) == HARDWARE_FAILED)
3704                                 {
3705                                         return (rcode);
3706                                 }
3707
3708                                 if((err = smctr_send_rsp(dev, rmf, rcode,
3709                                         correlator)))
3710                                 {
3711                                         return (err);
3712                                 }
3713                                 break;
3714
3715                         case CHG_PARM:
3716                                 if((rcode = smctr_rcv_chg_param(dev, rmf,
3717                                         &correlator)) ==HARDWARE_FAILED)
3718                                 {
3719                                         return (rcode);
3720                                 }
3721
3722                                 if((err = smctr_send_rsp(dev, rmf, rcode,
3723                                         correlator)))
3724                                 {
3725                                         return (err);
3726                                 }
3727                                 break;
3728
3729                         case RQ_ADDR:
3730                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3731                                         rmf, &correlator)) != POSITIVE_ACK)
3732                                 {
3733                                         if(rcode == HARDWARE_FAILED)
3734                                                 return (rcode);
3735                                         else
3736                                                 return (smctr_send_rsp(dev, rmf,
3737                                                         rcode, correlator));
3738                                 }
3739
3740                                 if((err = smctr_send_rpt_addr(dev, rmf,
3741                                         correlator)))
3742                                 {
3743                                         return (err);
3744                                 }
3745                                 break;
3746
3747                         case RQ_ATTCH:
3748                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3749                                         rmf, &correlator)) != POSITIVE_ACK)
3750                                 {
3751                                         if(rcode == HARDWARE_FAILED)
3752                                                 return (rcode);
3753                                         else
3754                                                 return (smctr_send_rsp(dev, rmf,
3755                                                         rcode,
3756                                                         correlator));
3757                                 }
3758
3759                                 if((err = smctr_send_rpt_attch(dev, rmf,
3760                                         correlator)))
3761                                 {
3762                                         return (err);
3763                                 }
3764                                 break;
3765
3766                         case RQ_STATE:
3767                                 if((rcode = smctr_rcv_rq_addr_state_attch(dev,
3768                                         rmf, &correlator)) != POSITIVE_ACK)
3769                                 {
3770                                         if(rcode == HARDWARE_FAILED)
3771                                                 return (rcode);
3772                                         else
3773                                                 return (smctr_send_rsp(dev, rmf,
3774                                                         rcode,
3775                                                         correlator));
3776                                 }
3777
3778                                 if((err = smctr_send_rpt_state(dev, rmf,
3779                                         correlator)))
3780                                 {
3781                                         return (err);
3782                                 }
3783                                 break;
3784
3785                         case TX_FORWARD: {
3786                                 __u16 uninitialized_var(tx_fstatus);
3787
3788                                 if((rcode = smctr_rcv_tx_forward(dev, rmf))
3789                                         != POSITIVE_ACK)
3790                                 {
3791                                         if(rcode == HARDWARE_FAILED)
3792                                                 return (rcode);
3793                                         else
3794                                                 return (smctr_send_rsp(dev, rmf,
3795                                                         rcode,
3796                                                         correlator));
3797                                 }
3798
3799                                 if((err = smctr_send_tx_forward(dev, rmf,
3800                                         &tx_fstatus)) == HARDWARE_FAILED)
3801                                 {
3802                                         return (err);
3803                                 }
3804
3805                                 if(err == A_FRAME_WAS_FORWARDED)
3806                                 {
3807                                         if((err = smctr_send_rpt_tx_forward(dev,
3808                                                 rmf, tx_fstatus))
3809                                                 == HARDWARE_FAILED)
3810                                         {
3811                                                 return (err);
3812                                         }
3813                                 }
3814                                 break;
3815                         }
3816
3817                         /* Received MAC Frames Processed by CRS/REM/RPS. */
3818                         case RSP:
3819                         case RQ_INIT:
3820                         case RPT_NEW_MON:
3821                         case RPT_SUA_CHG:
3822                         case RPT_ACTIVE_ERR:
3823                         case RPT_NN_INCMP:
3824                         case RPT_ERROR:
3825                         case RPT_ATTCH:
3826                         case RPT_STATE:
3827                         case RPT_ADDR:
3828                                 break;
3829
3830                         /* Rcvd Att. MAC Frame (if RXATMAC set) or UNKNOWN */
3831                         default:
3832                                 xframe = 0;
3833                                 if(!(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES))
3834                                 {
3835                                         rcode = smctr_rcv_unknown(dev, rmf,
3836                                                 &correlator);
3837                                         if((err = smctr_send_rsp(dev, rmf,rcode,
3838                                                 correlator)))
3839                                         {
3840                                                 return (err);
3841                                         }
3842                                 }
3843
3844                                 break;
3845                 }
3846         }
3847         else
3848         {
3849                 /* 1. DA doesn't match (Promiscuous Mode).
3850                  * 2. Parse for Extended MAC Frame Type.
3851                  */
3852                 switch(rmf->vc)
3853                 {
3854                         case RSP:
3855                         case INIT:
3856                         case RQ_INIT:
3857                         case RQ_ADDR:
3858                         case RQ_ATTCH:
3859                         case RQ_STATE:
3860                         case CHG_PARM:
3861                         case RPT_ADDR:
3862                         case RPT_ERROR:
3863                         case RPT_ATTCH:
3864                         case RPT_STATE:
3865                         case RPT_NEW_MON:
3866                         case RPT_SUA_CHG:
3867                         case RPT_NN_INCMP:
3868                         case RPT_ACTIVE_ERR:
3869                                 break;
3870
3871                         default:
3872                                 xframe = 0;
3873                                 break;
3874                 }
3875         }
3876
3877         /* NOTE: UNKNOWN MAC frames will NOT be passed up unless
3878          * ACCEPT_ATT_MAC_FRAMES is set.
3879          */
3880         if(((tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
3881                 && (xframe == (__u8)0))
3882                 || ((tp->receive_mask & ACCEPT_EXT_MAC_FRAMES)
3883                 && (xframe == (__u8)1)))
3884         {
3885                 rmf->vl = SWAP_BYTES(rmf->vl);
3886
3887                 if (!(skb = dev_alloc_skb(size)))
3888                         return -ENOMEM;
3889                 skb->len = size;
3890
3891                 /* Slide data into a sleek skb. */
3892                 skb_put(skb, skb->len);
3893                 skb_copy_to_linear_data(skb, rmf, skb->len);
3894
3895                 /* Update Counters */
3896                 tp->MacStat.rx_packets++;
3897                 tp->MacStat.rx_bytes += skb->len;
3898
3899                 /* Kick the packet on up. */
3900                 skb->protocol = tr_type_trans(skb, dev);
3901                 netif_rx(skb);
3902                 dev->last_rx = jiffies;
3903                 err = 0;
3904         }
3905
3906         return (err);
3907 }
3908
3909 /* Adapter RAM test. Incremental word ODD boundary data test. */
3910 static int smctr_ram_memory_test(struct net_device *dev)
3911 {
3912         struct net_local *tp = netdev_priv(dev);
3913         __u16 page, pages_of_ram, start_pattern = 0, word_pattern = 0,
3914                 word_read = 0, err_word = 0, err_pattern = 0;
3915         unsigned int err_offset;
3916         __u32 j, pword;
3917         __u8 err = 0;
3918
3919         if(smctr_debug > 10)
3920                 printk(KERN_DEBUG "%s: smctr_ram_memory_test\n", dev->name);
3921
3922         start_pattern   = 0x0001;
3923         pages_of_ram    = tp->ram_size / tp->ram_usable;
3924         pword           = tp->ram_access;
3925
3926         /* Incremental word ODD boundary test. */
3927         for(page = 0; (page < pages_of_ram) && (~err);
3928                 page++, start_pattern += 0x8000)
3929         {
3930                 smctr_set_page(dev, (__u8 *)(tp->ram_access
3931                         + (page * tp->ram_usable * 1024) + 1));
3932                 word_pattern = start_pattern;
3933
3934                 for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1; j += 2)
3935                         *(__u16 *)(pword + j) = word_pattern++;
3936
3937                 word_pattern = start_pattern;
3938
3939                 for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1
3940                         && (~err); j += 2, word_pattern++)
3941                 {
3942                         word_read = *(__u16 *)(pword + j);
3943                         if(word_read != word_pattern)
3944                         {
3945                                 err             = (__u8)1;
3946                                 err_offset      = j;
3947                                 err_word        = word_read;
3948                                 err_pattern     = word_pattern;
3949                                 return (RAM_TEST_FAILED);
3950                         }
3951                 }
3952         }
3953
3954         /* Zero out memory. */
3955         for(page = 0; page < pages_of_ram && (~err); page++)
3956         {
3957                 smctr_set_page(dev, (__u8 *)(tp->ram_access
3958                         + (page * tp->ram_usable * 1024)));
3959                 word_pattern = 0;
3960
3961                 for(j = 0; j < (__u32)tp->ram_usable * 1024; j +=2)
3962                         *(__u16 *)(pword + j) = word_pattern;
3963
3964                 for(j =0; j < (__u32)tp->ram_usable * 1024
3965                         && (~err); j += 2)
3966                 {
3967                         word_read = *(__u16 *)(pword + j);
3968                         if(word_read != word_pattern)
3969                         {
3970                                 err             = (__u8)1;
3971                                 err_offset      = j;
3972                                 err_word        = word_read;
3973                                 err_pattern     = word_pattern;
3974                                 return (RAM_TEST_FAILED);
3975                         }
3976                 }
3977         }
3978
3979         smctr_set_page(dev, (__u8 *)tp->ram_access);
3980
3981         return (0);
3982 }
3983
3984 static int smctr_rcv_chg_param(struct net_device *dev, MAC_HEADER *rmf,
3985         __u16 *correlator)
3986 {
3987         MAC_SUB_VECTOR *rsv;
3988         signed short vlen;
3989         __u16 rcode = POSITIVE_ACK;
3990         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
3991
3992         /* This Frame can only come from a CRS */
3993         if((rmf->dc_sc & SC_MASK) != SC_CRS)
3994                 return(E_INAPPROPRIATE_SOURCE_CLASS);
3995
3996         /* Remove MVID Length from total length. */
3997         vlen = (signed short)rmf->vl - 4;
3998
3999         /* Point to First SVID */
4000         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4001
4002         /* Search for Appropriate SVID's. */
4003         while((vlen > 0) && (rcode == POSITIVE_ACK))
4004         {
4005                 switch(rsv->svi)
4006                 {
4007                         case CORRELATOR:
4008                                 svectors |= F_CORRELATOR;
4009                                 rcode = smctr_set_corr(dev, rsv, correlator);
4010                                 break;
4011
4012                         case LOCAL_RING_NUMBER:
4013                                 svectors |= F_LOCAL_RING_NUMBER;
4014                                 rcode = smctr_set_local_ring_num(dev, rsv);
4015                                 break;
4016
4017                         case ASSIGN_PHYSICAL_DROP:
4018                                 svectors |= F_ASSIGN_PHYSICAL_DROP;
4019                                 rcode = smctr_set_phy_drop(dev, rsv);
4020                                 break;
4021
4022                         case ERROR_TIMER_VALUE:
4023                                 svectors |= F_ERROR_TIMER_VALUE;
4024                                 rcode = smctr_set_error_timer_value(dev, rsv);
4025                                 break;
4026
4027                         case AUTHORIZED_FUNCTION_CLASS:
4028                                 svectors |= F_AUTHORIZED_FUNCTION_CLASS;
4029                                 rcode = smctr_set_auth_funct_class(dev, rsv);
4030                                 break;
4031
4032                         case AUTHORIZED_ACCESS_PRIORITY:
4033                                 svectors |= F_AUTHORIZED_ACCESS_PRIORITY;
4034                                 rcode = smctr_set_auth_access_pri(dev, rsv);
4035                                 break;
4036
4037                         default:
4038                                 rcode = E_SUB_VECTOR_UNKNOWN;
4039                                 break;
4040                 }
4041
4042                 /* Let Sender Know if SUM of SV length's is
4043                  * larger then length in MVID length field
4044                  */
4045                 if((vlen -= rsv->svl) < 0)
4046                         rcode = E_VECTOR_LENGTH_ERROR;
4047
4048                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4049         }
4050
4051         if(rcode == POSITIVE_ACK)
4052         {
4053                 /* Let Sender Know if MVID length field
4054                  * is larger then SUM of SV length's
4055                  */
4056                 if(vlen != 0)
4057                         rcode = E_VECTOR_LENGTH_ERROR;
4058                 else
4059                 {
4060                         /* Let Sender Know if Expected SVID Missing */
4061                         if((svectors & R_CHG_PARM) ^ R_CHG_PARM)
4062                                 rcode = E_MISSING_SUB_VECTOR;
4063                 }
4064         }
4065
4066         return (rcode);
4067 }
4068
4069 static int smctr_rcv_init(struct net_device *dev, MAC_HEADER *rmf,
4070         __u16 *correlator)
4071 {
4072         MAC_SUB_VECTOR *rsv;
4073         signed short vlen;
4074         __u16 rcode = POSITIVE_ACK;
4075         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4076
4077         /* This Frame can only come from a RPS */
4078         if((rmf->dc_sc & SC_MASK) != SC_RPS)
4079                 return (E_INAPPROPRIATE_SOURCE_CLASS);
4080
4081         /* Remove MVID Length from total length. */
4082         vlen = (signed short)rmf->vl - 4;
4083
4084         /* Point to First SVID */
4085         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4086
4087         /* Search for Appropriate SVID's */
4088         while((vlen > 0) && (rcode == POSITIVE_ACK))
4089         {
4090                 switch(rsv->svi)
4091                 {
4092                         case CORRELATOR:
4093                                 svectors |= F_CORRELATOR;
4094                                 rcode = smctr_set_corr(dev, rsv, correlator);
4095                                 break;
4096
4097                         case LOCAL_RING_NUMBER:
4098                                 svectors |= F_LOCAL_RING_NUMBER;
4099                                 rcode = smctr_set_local_ring_num(dev, rsv);
4100                                 break;
4101
4102                         case ASSIGN_PHYSICAL_DROP:
4103                                 svectors |= F_ASSIGN_PHYSICAL_DROP;
4104                                 rcode = smctr_set_phy_drop(dev, rsv);
4105                                 break;
4106
4107                         case ERROR_TIMER_VALUE:
4108                                 svectors |= F_ERROR_TIMER_VALUE;
4109                                 rcode = smctr_set_error_timer_value(dev, rsv);
4110                                 break;
4111
4112                         default:
4113                                 rcode = E_SUB_VECTOR_UNKNOWN;
4114                                 break;
4115                 }
4116
4117                 /* Let Sender Know if SUM of SV length's is
4118                  * larger then length in MVID length field
4119                  */
4120                 if((vlen -= rsv->svl) < 0)
4121                         rcode = E_VECTOR_LENGTH_ERROR;
4122
4123                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4124         }
4125
4126         if(rcode == POSITIVE_ACK)
4127         {
4128                 /* Let Sender Know if MVID length field
4129                  * is larger then SUM of SV length's
4130                  */
4131                 if(vlen != 0)
4132                         rcode = E_VECTOR_LENGTH_ERROR;
4133                 else
4134                 {
4135                         /* Let Sender Know if Expected SV Missing */
4136                         if((svectors & R_INIT) ^ R_INIT)
4137                                 rcode = E_MISSING_SUB_VECTOR;
4138                 }
4139         }
4140
4141         return (rcode);
4142 }
4143
4144 static int smctr_rcv_tx_forward(struct net_device *dev, MAC_HEADER *rmf)
4145 {
4146         MAC_SUB_VECTOR *rsv;
4147         signed short vlen;
4148         __u16 rcode = POSITIVE_ACK;
4149         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4150
4151         /* This Frame can only come from a CRS */
4152         if((rmf->dc_sc & SC_MASK) != SC_CRS)
4153                 return (E_INAPPROPRIATE_SOURCE_CLASS);
4154
4155         /* Remove MVID Length from total length */
4156         vlen = (signed short)rmf->vl - 4;
4157
4158         /* Point to First SVID */
4159         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4160
4161         /* Search for Appropriate SVID's */
4162         while((vlen > 0) && (rcode == POSITIVE_ACK))
4163         {
4164                 switch(rsv->svi)
4165                 {
4166                         case FRAME_FORWARD:
4167                                 svectors |= F_FRAME_FORWARD;
4168                                 rcode = smctr_set_frame_forward(dev, rsv, 
4169                                         rmf->dc_sc);
4170                                 break;
4171
4172                         default:
4173                                 rcode = E_SUB_VECTOR_UNKNOWN;
4174                                 break;
4175                 }
4176
4177                 /* Let Sender Know if SUM of SV length's is
4178                  * larger then length in MVID length field
4179                  */
4180                 if((vlen -= rsv->svl) < 0)
4181                         rcode = E_VECTOR_LENGTH_ERROR;
4182
4183                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4184         }
4185
4186         if(rcode == POSITIVE_ACK)
4187         {
4188                 /* Let Sender Know if MVID length field
4189                  * is larger then SUM of SV length's
4190                  */
4191                 if(vlen != 0)
4192                         rcode = E_VECTOR_LENGTH_ERROR;
4193                 else
4194                 {
4195                         /* Let Sender Know if Expected SV Missing */
4196                         if((svectors & R_TX_FORWARD) ^ R_TX_FORWARD)
4197                                 rcode = E_MISSING_SUB_VECTOR;
4198                 }
4199         }
4200
4201         return (rcode);
4202 }
4203
4204 static int smctr_rcv_rq_addr_state_attch(struct net_device *dev,
4205         MAC_HEADER *rmf, __u16 *correlator)
4206 {
4207         MAC_SUB_VECTOR *rsv;
4208         signed short vlen;
4209         __u16 rcode = POSITIVE_ACK;
4210         unsigned int svectors = F_NO_SUB_VECTORS_FOUND;
4211
4212         /* Remove MVID Length from total length */
4213         vlen = (signed short)rmf->vl - 4;
4214
4215         /* Point to First SVID */
4216         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4217
4218         /* Search for Appropriate SVID's */
4219         while((vlen > 0) && (rcode == POSITIVE_ACK))
4220         {
4221                 switch(rsv->svi)
4222                 {
4223                         case CORRELATOR:
4224                                 svectors |= F_CORRELATOR;
4225                                 rcode = smctr_set_corr(dev, rsv, correlator);
4226                                 break;
4227
4228                         default:
4229                                 rcode = E_SUB_VECTOR_UNKNOWN;
4230                                 break;
4231                 }
4232
4233                 /* Let Sender Know if SUM of SV length's is
4234                  * larger then length in MVID length field
4235                  */
4236                 if((vlen -= rsv->svl) < 0)
4237                         rcode = E_VECTOR_LENGTH_ERROR;
4238
4239                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4240         }
4241
4242         if(rcode == POSITIVE_ACK)
4243         {
4244                 /* Let Sender Know if MVID length field
4245                  * is larger then SUM of SV length's
4246                  */
4247                 if(vlen != 0)
4248                         rcode = E_VECTOR_LENGTH_ERROR;
4249                 else
4250                 {
4251                         /* Let Sender Know if Expected SVID Missing */
4252                         if((svectors & R_RQ_ATTCH_STATE_ADDR) 
4253                                 ^ R_RQ_ATTCH_STATE_ADDR)
4254                                 rcode = E_MISSING_SUB_VECTOR;
4255                         }
4256         }
4257
4258         return (rcode);
4259 }
4260
4261 static int smctr_rcv_unknown(struct net_device *dev, MAC_HEADER *rmf,
4262         __u16 *correlator)
4263 {
4264         MAC_SUB_VECTOR *rsv;
4265         signed short vlen;
4266
4267         *correlator = 0;
4268
4269         /* Remove MVID Length from total length */
4270         vlen = (signed short)rmf->vl - 4;
4271
4272         /* Point to First SVID */
4273         rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));
4274
4275         /* Search for CORRELATOR for RSP to UNKNOWN */
4276         while((vlen > 0) && (*correlator == 0))
4277         {
4278                 switch(rsv->svi)
4279                 {
4280                         case CORRELATOR:
4281                                 smctr_set_corr(dev, rsv, correlator);
4282                                 break;
4283
4284                         default:
4285                                 break;
4286                 }
4287
4288                 vlen -= rsv->svl;
4289                 rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
4290         }
4291
4292         return (E_UNRECOGNIZED_VECTOR_ID);
4293 }
4294
4295 /*
4296  * Reset the 825 NIC and exit w:
4297  * 1. The NIC reset cleared (non-reset state), halted and un-initialized.
4298  * 2. TINT masked.
4299  * 3. CBUSY masked.
4300  * 4. TINT clear.
4301  * 5. CBUSY clear.
4302  */
4303 static int smctr_reset_adapter(struct net_device *dev)
4304 {
4305         struct net_local *tp = netdev_priv(dev);
4306         int ioaddr = dev->base_addr;
4307
4308         /* Reseting the NIC will put it in a halted and un-initialized state. */        smctr_set_trc_reset(ioaddr);
4309         mdelay(200); /* ~2 ms */
4310
4311         smctr_clear_trc_reset(ioaddr);
4312         mdelay(200); /* ~2 ms */
4313
4314         /* Remove any latched interrupts that occurred prior to reseting the
4315          * adapter or possibily caused by line glitches due to the reset.
4316          */
4317         outb(tp->trc_mask | CSR_CLRTINT | CSR_CLRCBUSY, ioaddr + CSR);
4318
4319         return (0);
4320 }
4321
4322 static int smctr_restart_tx_chain(struct net_device *dev, short queue)
4323 {
4324         struct net_local *tp = netdev_priv(dev);
4325         int err = 0;
4326
4327         if(smctr_debug > 10)
4328                 printk(KERN_DEBUG "%s: smctr_restart_tx_chain\n", dev->name);
4329
4330         if(tp->num_tx_fcbs_used[queue] != 0
4331                 && tp->tx_queue_status[queue] == NOT_TRANSMITING)
4332         {
4333                 tp->tx_queue_status[queue] = TRANSMITING;
4334                 err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
4335         }
4336
4337         return (err);
4338 }
4339
4340 static int smctr_ring_status_chg(struct net_device *dev)
4341 {
4342         struct net_local *tp = netdev_priv(dev);
4343
4344         if(smctr_debug > 10)
4345                 printk(KERN_DEBUG "%s: smctr_ring_status_chg\n", dev->name);
4346
4347         /* Check for ring_status_flag: whenever MONITOR_STATE_BIT
4348          * Bit is set, check value of monitor_state, only then we
4349          * enable and start transmit/receive timeout (if and only
4350          * if it is MS_ACTIVE_MONITOR_STATE or MS_STANDBY_MONITOR_STATE)
4351          */
4352         if(tp->ring_status_flags == MONITOR_STATE_CHANGED)
4353         {
4354                 if((tp->monitor_state == MS_ACTIVE_MONITOR_STATE)
4355                         || (tp->monitor_state == MS_STANDBY_MONITOR_STATE))
4356                 {
4357                         tp->monitor_state_ready = 1;
4358                 }
4359                 else
4360                 {
4361                         /* if adapter is NOT in either active monitor
4362                          * or standby monitor state => Disable
4363                          * transmit/receive timeout.
4364                          */
4365                         tp->monitor_state_ready = 0;
4366
4367                         /* Ring speed problem, switching to auto mode. */
4368                         if(tp->monitor_state == MS_MONITOR_FSM_INACTIVE
4369                                 && !tp->cleanup)
4370                         {
4371                                 printk(KERN_INFO "%s: Incorrect ring speed switching.\n",
4372                                         dev->name);
4373                                 smctr_set_ring_speed(dev);
4374                         }
4375                 }
4376         }
4377
4378         if(!(tp->ring_status_flags & RING_STATUS_CHANGED))
4379                 return (0);
4380
4381         switch(tp->ring_status)
4382         {
4383                 case RING_RECOVERY:
4384                         printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
4385                         tp->current_ring_status |= RING_RECOVERY;
4386                         break;
4387
4388                 case SINGLE_STATION:
4389                         printk(KERN_INFO "%s: Single Statinon\n", dev->name);
4390                         tp->current_ring_status |= SINGLE_STATION;
4391                         break;
4392
4393                 case COUNTER_OVERFLOW:
4394                         printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
4395                         tp->current_ring_status |= COUNTER_OVERFLOW;
4396                         break;
4397
4398                 case REMOVE_RECEIVED:
4399                         printk(KERN_INFO "%s: Remove Received\n", dev->name);
4400                         tp->current_ring_status |= REMOVE_RECEIVED;
4401                         break;
4402
4403                 case AUTO_REMOVAL_ERROR:
4404                         printk(KERN_INFO "%s: Auto Remove Error\n", dev->name);
4405                         tp->current_ring_status |= AUTO_REMOVAL_ERROR;
4406                         break;
4407
4408                 case LOBE_WIRE_FAULT:
4409                         printk(KERN_INFO "%s: Lobe Wire Fault\n", dev->name);
4410                         tp->current_ring_status |= LOBE_WIRE_FAULT;
4411                         break;
4412
4413                 case TRANSMIT_BEACON:
4414                         printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
4415                         tp->current_ring_status |= TRANSMIT_BEACON;
4416                         break;
4417
4418                 case SOFT_ERROR:
4419                         printk(KERN_INFO "%s: Soft Error\n", dev->name);
4420                         tp->current_ring_status |= SOFT_ERROR;
4421                         break;
4422
4423                 case HARD_ERROR:
4424                         printk(KERN_INFO "%s: Hard Error\n", dev->name);
4425                         tp->current_ring_status |= HARD_ERROR;
4426                         break;
4427
4428                 case SIGNAL_LOSS:
4429                         printk(KERN_INFO "%s: Signal Loss\n", dev->name);
4430                         tp->current_ring_status |= SIGNAL_LOSS;
4431                         break;
4432
4433                 default:
4434                         printk(KERN_INFO "%s: Unknown ring status change\n",
4435                                 dev->name);
4436                         break;
4437         }
4438
4439         return (0);
4440 }
4441
4442 static int smctr_rx_frame(struct net_device *dev)
4443 {
4444         struct net_local *tp = netdev_priv(dev);
4445         __u16 queue, status, rx_size, err = 0;
4446         __u8 *pbuff;
4447
4448         if(smctr_debug > 10)
4449                 printk(KERN_DEBUG "%s: smctr_rx_frame\n", dev->name);
4450
4451         queue = tp->receive_queue_number;
4452
4453         while((status = tp->rx_fcb_curr[queue]->frame_status) != SUCCESS)
4454         {
4455                 err = HARDWARE_FAILED;
4456
4457                 if(((status & 0x007f) == 0)
4458                         || ((tp->receive_mask & ACCEPT_ERR_PACKETS) != 0))
4459                 {
4460                         /* frame length less the CRC (4 bytes) + FS (1 byte) */
4461                         rx_size = tp->rx_fcb_curr[queue]->frame_length - 5;
4462
4463                         pbuff = smctr_get_rx_pointer(dev, queue);
4464
4465                         smctr_set_page(dev, pbuff);
4466                         smctr_disable_16bit(dev);
4467
4468                         /* pbuff points to addr within one page */
4469                         pbuff = (__u8 *)PAGE_POINTER(pbuff);
4470
4471                         if(queue == NON_MAC_QUEUE)
4472                         {
4473                                 struct sk_buff *skb;
4474
4475                                 skb = dev_alloc_skb(rx_size);
4476                                 if (skb) {
4477                                         skb_put(skb, rx_size);
4478
4479                                         skb_copy_to_linear_data(skb, pbuff, rx_size);
4480
4481                                         /* Update Counters */
4482                                         tp->MacStat.rx_packets++;
4483                                         tp->MacStat.rx_bytes += skb->len;
4484
4485                                         /* Kick the packet on up. */
4486                                         skb->protocol = tr_type_trans(skb, dev);
4487                                         netif_rx(skb);
4488                                         dev->last_rx = jiffies;
4489                                 } else {
4490                                 }
4491                         }
4492                         else
4493                                 smctr_process_rx_packet((MAC_HEADER *)pbuff,
4494                                         rx_size, dev, status);
4495                 }
4496
4497                 smctr_enable_16bit(dev);
4498                 smctr_set_page(dev, (__u8 *)tp->ram_access);
4499                 smctr_update_rx_chain(dev, queue);
4500
4501                 if(err != SUCCESS)
4502                         break;
4503         }
4504
4505         return (err);
4506 }
4507
4508 static int smctr_send_dat(struct net_device *dev)
4509 {
4510         struct net_local *tp = netdev_priv(dev);
4511         unsigned int i, err;
4512         MAC_HEADER *tmf;
4513         FCBlock *fcb;
4514
4515         if(smctr_debug > 10)
4516                 printk(KERN_DEBUG "%s: smctr_send_dat\n", dev->name);
4517
4518         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE,
4519                 sizeof(MAC_HEADER))) == (FCBlock *)(-1L))
4520         {
4521                 return (OUT_OF_RESOURCES);
4522         }
4523
4524         /* Initialize DAT Data Fields. */
4525         tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4526         tmf->ac = MSB(AC_FC_DAT);
4527         tmf->fc = LSB(AC_FC_DAT);
4528
4529         for(i = 0; i < 6; i++)
4530         {
4531                 tmf->sa[i] = dev->dev_addr[i];
4532                 tmf->da[i] = dev->dev_addr[i];
4533
4534         }
4535
4536         tmf->vc        = DAT;
4537         tmf->dc_sc     = DC_RS | SC_RS;
4538         tmf->vl        = 4;
4539         tmf->vl        = SWAP_BYTES(tmf->vl);
4540
4541         /* Start Transmit. */
4542         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4543                 return (err);
4544
4545         /* Wait for Transmit to Complete */
4546         for(i = 0; i < 10000; i++)
4547         {
4548                 if(fcb->frame_status & FCB_COMMAND_DONE)
4549                         break;
4550                 mdelay(1);
4551         }
4552
4553         /* Check if GOOD frame Tx'ed. */
4554         if(!(fcb->frame_status &  FCB_COMMAND_DONE)
4555                 || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
4556         {
4557                 return (INITIALIZE_FAILED);
4558         }
4559
4560         /* De-allocated Tx FCB and Frame Buffer
4561          * The FCB must be de-allocated manually if executing with
4562          * interrupts disabled, other wise the ISR (LM_Service_Events)
4563          * will de-allocate it when the interrupt occurs.
4564          */
4565         tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4566         smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4567
4568         return (0);
4569 }
4570
4571 static void smctr_timeout(struct net_device *dev)
4572 {
4573         /*
4574          * If we get here, some higher level has decided we are broken.
4575          * There should really be a "kick me" function call instead.
4576          *
4577          * Resetting the token ring adapter takes a long time so just
4578          * fake transmission time and go on trying. Our own timeout
4579          * routine is in sktr_timer_chk()
4580          */
4581         dev->trans_start = jiffies;
4582         netif_wake_queue(dev);
4583 }
4584
4585 /*
4586  * Gets skb from system, queues it and checks if it can be sent
4587  */
4588 static int smctr_send_packet(struct sk_buff *skb, struct net_device *dev)
4589 {
4590         struct net_local *tp = netdev_priv(dev);
4591
4592         if(smctr_debug > 10)
4593                 printk(KERN_DEBUG "%s: smctr_send_packet\n", dev->name);
4594
4595         /*
4596          * Block a transmit overlap
4597          */
4598          
4599         netif_stop_queue(dev);
4600
4601         if(tp->QueueSkb == 0)
4602                 return (1);     /* Return with tbusy set: queue full */
4603
4604         tp->QueueSkb--;
4605         skb_queue_tail(&tp->SendSkbQueue, skb);
4606         smctr_hardware_send_packet(dev, tp);
4607         if(tp->QueueSkb > 0)
4608                 netif_wake_queue(dev);
4609                 
4610         return (0);
4611 }
4612
4613 static int smctr_send_lobe_media_test(struct net_device *dev)
4614 {
4615         struct net_local *tp = netdev_priv(dev);
4616         MAC_SUB_VECTOR *tsv;
4617         MAC_HEADER *tmf;
4618         FCBlock *fcb;
4619         __u32 i;
4620         int err;
4621
4622         if(smctr_debug > 15)
4623                 printk(KERN_DEBUG "%s: smctr_send_lobe_media_test\n", dev->name);
4624
4625         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(struct trh_hdr)
4626                 + S_WRAP_DATA + S_WRAP_DATA)) == (FCBlock *)(-1L))
4627         {
4628                 return (OUT_OF_RESOURCES);
4629         }
4630
4631         /* Initialize DAT Data Fields. */
4632         tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4633         tmf->ac = MSB(AC_FC_LOBE_MEDIA_TEST);
4634         tmf->fc = LSB(AC_FC_LOBE_MEDIA_TEST);
4635
4636         for(i = 0; i < 6; i++)
4637         {
4638                 tmf->da[i] = 0;
4639                 tmf->sa[i] = dev->dev_addr[i];
4640         }
4641
4642         tmf->vc        = LOBE_MEDIA_TEST;
4643         tmf->dc_sc     = DC_RS | SC_RS;
4644         tmf->vl        = 4;
4645
4646         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4647         smctr_make_wrap_data(dev, tsv);
4648         tmf->vl += tsv->svl;
4649
4650         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4651         smctr_make_wrap_data(dev, tsv);
4652         tmf->vl += tsv->svl;
4653
4654         /* Start Transmit. */
4655         tmf->vl = SWAP_BYTES(tmf->vl);
4656         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4657                 return (err);
4658
4659         /* Wait for Transmit to Complete. (10 ms). */
4660         for(i=0; i < 10000; i++)
4661         {
4662                 if(fcb->frame_status & FCB_COMMAND_DONE)
4663                         break;
4664                 mdelay(1);
4665         }
4666
4667         /* Check if GOOD frame Tx'ed */
4668         if(!(fcb->frame_status & FCB_COMMAND_DONE)
4669                 || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
4670         {
4671                 return (LOBE_MEDIA_TEST_FAILED);
4672         }
4673
4674         /* De-allocated Tx FCB and Frame Buffer
4675          * The FCB must be de-allocated manually if executing with
4676          * interrupts disabled, other wise the ISR (LM_Service_Events)
4677          * will de-allocate it when the interrupt occurs.
4678          */
4679         tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4680         smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4681
4682         return (0);
4683 }
4684
4685 static int smctr_send_rpt_addr(struct net_device *dev, MAC_HEADER *rmf,
4686         __u16 correlator)
4687 {
4688         MAC_HEADER *tmf;
4689         MAC_SUB_VECTOR *tsv;
4690         FCBlock *fcb;
4691
4692         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4693                 + S_CORRELATOR + S_PHYSICAL_DROP + S_UPSTREAM_NEIGHBOR_ADDRESS
4694                 + S_ADDRESS_MODIFER + S_GROUP_ADDRESS + S_FUNCTIONAL_ADDRESS))
4695                 == (FCBlock *)(-1L))
4696         {
4697                 return (0);
4698         }
4699
4700         tmf             = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4701         tmf->vc         = RPT_ADDR;
4702         tmf->dc_sc      = (rmf->dc_sc & SC_MASK) << 4;
4703         tmf->vl         = 4;
4704
4705         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ADDR);
4706
4707         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4708         smctr_make_corr(dev, tsv, correlator);
4709
4710         tmf->vl += tsv->svl;
4711         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4712         smctr_make_phy_drop_num(dev, tsv);
4713
4714         tmf->vl += tsv->svl;
4715         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4716         smctr_make_upstream_neighbor_addr(dev, tsv);
4717
4718         tmf->vl += tsv->svl;
4719         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4720         smctr_make_addr_mod(dev, tsv);
4721
4722         tmf->vl += tsv->svl;
4723         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4724         smctr_make_group_addr(dev, tsv);
4725
4726         tmf->vl += tsv->svl;
4727         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4728         smctr_make_funct_addr(dev, tsv);
4729
4730         tmf->vl += tsv->svl;
4731
4732         /* Subtract out MVID and MVL which is
4733          * include in both vl and MAC_HEADER
4734          */
4735 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4736         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4737 */
4738         tmf->vl = SWAP_BYTES(tmf->vl);
4739
4740         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4741 }
4742
4743 static int smctr_send_rpt_attch(struct net_device *dev, MAC_HEADER *rmf,
4744         __u16 correlator)
4745 {
4746         MAC_HEADER *tmf;
4747         MAC_SUB_VECTOR *tsv;
4748         FCBlock *fcb;
4749
4750         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4751                 + S_CORRELATOR + S_PRODUCT_INSTANCE_ID + S_FUNCTIONAL_ADDRESS
4752                 + S_AUTHORIZED_FUNCTION_CLASS + S_AUTHORIZED_ACCESS_PRIORITY))
4753                 == (FCBlock *)(-1L))
4754         {
4755                 return (0);
4756         }
4757
4758         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4759         tmf->vc    = RPT_ATTCH;
4760         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4761         tmf->vl    = 4;
4762
4763         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ATTCH);
4764
4765         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4766         smctr_make_corr(dev, tsv, correlator);
4767
4768         tmf->vl += tsv->svl;
4769         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4770         smctr_make_product_id(dev, tsv);
4771
4772         tmf->vl += tsv->svl;
4773         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4774         smctr_make_funct_addr(dev, tsv);
4775
4776         tmf->vl += tsv->svl;
4777         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4778         smctr_make_auth_funct_class(dev, tsv);
4779
4780         tmf->vl += tsv->svl;
4781         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4782         smctr_make_access_pri(dev, tsv);
4783
4784         tmf->vl += tsv->svl;
4785
4786         /* Subtract out MVID and MVL which is
4787          * include in both vl and MAC_HEADER
4788          */
4789 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4790         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4791 */
4792         tmf->vl = SWAP_BYTES(tmf->vl);
4793
4794         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4795 }
4796
4797 static int smctr_send_rpt_state(struct net_device *dev, MAC_HEADER *rmf,
4798         __u16 correlator)
4799 {
4800         MAC_HEADER *tmf;
4801         MAC_SUB_VECTOR *tsv;
4802         FCBlock *fcb;
4803
4804         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4805                 + S_CORRELATOR + S_RING_STATION_VERSION_NUMBER
4806                 + S_RING_STATION_STATUS + S_STATION_IDENTIFER))
4807                 == (FCBlock *)(-1L))
4808         {
4809                 return (0);
4810         }
4811
4812         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4813         tmf->vc    = RPT_STATE;
4814         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4815         tmf->vl    = 4;
4816
4817         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_STATE);
4818
4819         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4820         smctr_make_corr(dev, tsv, correlator);
4821
4822         tmf->vl += tsv->svl;
4823         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4824         smctr_make_ring_station_version(dev, tsv);
4825
4826         tmf->vl += tsv->svl;
4827         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4828         smctr_make_ring_station_status(dev, tsv);
4829
4830         tmf->vl += tsv->svl;
4831         tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4832         smctr_make_station_id(dev, tsv);
4833
4834         tmf->vl += tsv->svl;
4835
4836         /* Subtract out MVID and MVL which is
4837          * include in both vl and MAC_HEADER
4838          */
4839 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4840         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4841 */
4842         tmf->vl = SWAP_BYTES(tmf->vl);
4843
4844         return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4845 }
4846
4847 static int smctr_send_rpt_tx_forward(struct net_device *dev,
4848         MAC_HEADER *rmf, __u16 tx_fstatus)
4849 {
4850         MAC_HEADER *tmf;
4851         MAC_SUB_VECTOR *tsv;
4852         FCBlock *fcb;
4853
4854         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4855                 + S_TRANSMIT_STATUS_CODE)) == (FCBlock *)(-1L))
4856         {
4857                 return (0);
4858         }
4859
4860         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4861         tmf->vc    = RPT_TX_FORWARD;
4862         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4863         tmf->vl    = 4;
4864
4865         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_TX_FORWARD);
4866
4867         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4868         smctr_make_tx_status_code(dev, tsv, tx_fstatus);
4869
4870         tmf->vl += tsv->svl;
4871
4872         /* Subtract out MVID and MVL which is
4873          * include in both vl and MAC_HEADER
4874          */
4875 /*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4876         fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4877 */
4878         tmf->vl = SWAP_BYTES(tmf->vl);
4879
4880         return(smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
4881 }
4882
4883 static int smctr_send_rsp(struct net_device *dev, MAC_HEADER *rmf,
4884         __u16 rcode, __u16 correlator)
4885 {
4886         MAC_HEADER *tmf;
4887         MAC_SUB_VECTOR *tsv;
4888         FCBlock *fcb;
4889
4890         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4891                 + S_CORRELATOR + S_RESPONSE_CODE)) == (FCBlock *)(-1L))
4892         {
4893                 return (0);
4894         }
4895
4896         tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4897         tmf->vc    = RSP;
4898         tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
4899         tmf->vl    = 4;
4900
4901         smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RSP);
4902
4903         tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4904         smctr_make_corr(dev, tsv, correlator);
4905
4906         return (0);
4907 }
4908
4909 static int smctr_send_rq_init(struct net_device *dev)
4910 {
4911         struct net_local *tp = netdev_priv(dev);
4912         MAC_HEADER *tmf;
4913         MAC_SUB_VECTOR *tsv;
4914         FCBlock *fcb;
4915         unsigned int i, count = 0;
4916         __u16 fstatus;
4917         int err;
4918
4919         do {
4920                 if(((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
4921                         + S_PRODUCT_INSTANCE_ID + S_UPSTREAM_NEIGHBOR_ADDRESS
4922                         + S_RING_STATION_VERSION_NUMBER + S_ADDRESS_MODIFER))
4923                         == (FCBlock *)(-1L)))
4924                 {
4925                         return (0);
4926                 }
4927
4928                 tmf        = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
4929                 tmf->vc    = RQ_INIT;
4930                 tmf->dc_sc = DC_RPS | SC_RS;
4931                 tmf->vl    = 4;
4932
4933                 smctr_make_8025_hdr(dev, NULL, tmf, AC_FC_RQ_INIT);
4934
4935                 tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
4936                 smctr_make_product_id(dev, tsv);
4937
4938                 tmf->vl += tsv->svl;
4939                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4940                 smctr_make_upstream_neighbor_addr(dev, tsv);
4941
4942                 tmf->vl += tsv->svl;
4943                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4944                 smctr_make_ring_station_version(dev, tsv);
4945
4946                 tmf->vl += tsv->svl;
4947                 tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
4948                 smctr_make_addr_mod(dev, tsv);
4949
4950                 tmf->vl += tsv->svl;
4951
4952                 /* Subtract out MVID and MVL which is
4953                  * include in both vl and MAC_HEADER
4954                  */
4955 /*              fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
4956                 fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
4957 */
4958                 tmf->vl = SWAP_BYTES(tmf->vl);
4959
4960                 if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
4961                         return (err);
4962
4963                 /* Wait for Transmit to Complete */
4964                 for(i = 0; i < 10000; i++) 
4965                 {
4966                         if(fcb->frame_status & FCB_COMMAND_DONE)
4967                                 break;
4968                         mdelay(1);
4969                 }
4970
4971                 /* Check if GOOD frame Tx'ed */
4972                 fstatus = fcb->frame_status;
4973
4974                 if(!(fstatus & FCB_COMMAND_DONE))
4975                         return (HARDWARE_FAILED);
4976
4977                 if(!(fstatus & FCB_TX_STATUS_E))
4978                         count++;
4979
4980                 /* De-allocated Tx FCB and Frame Buffer
4981                  * The FCB must be de-allocated manually if executing with
4982                  * interrupts disabled, other wise the ISR (LM_Service_Events)
4983                  * will de-allocate it when the interrupt occurs.
4984                  */
4985                 tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
4986                 smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
4987         } while(count < 4 && ((fstatus & FCB_TX_AC_BITS) ^ FCB_TX_AC_BITS));
4988
4989         return (smctr_join_complete_state(dev));
4990 }
4991
4992 static int smctr_send_tx_forward(struct net_device *dev, MAC_HEADER *rmf,
4993         __u16 *tx_fstatus)
4994 {
4995         struct net_local *tp = netdev_priv(dev);
4996         FCBlock *fcb;
4997         unsigned int i;
4998         int err;
4999
5000         /* Check if this is the END POINT of the Transmit Forward Chain. */
5001         if(rmf->vl <= 18)
5002                 return (0);
5003
5004         /* Allocate Transmit FCB only by requesting 0 bytes
5005          * of data buffer.
5006          */
5007         if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, 0)) == (FCBlock *)(-1L))
5008                 return (0);
5009
5010         /* Set pointer to Transmit Frame Buffer to the data
5011          * portion of the received TX Forward frame, making
5012          * sure to skip over the Vector Code (vc) and Vector
5013          * length (vl).
5014          */
5015         fcb->bdb_ptr->trc_data_block_ptr = TRC_POINTER((__u32)rmf 
5016                 + sizeof(MAC_HEADER) + 2);
5017         fcb->bdb_ptr->data_block_ptr     = (__u16 *)((__u32)rmf 
5018                 + sizeof(MAC_HEADER) + 2);
5019
5020         fcb->frame_length                = rmf->vl - 4 - 2;
5021         fcb->bdb_ptr->buffer_length      = rmf->vl - 4 - 2;
5022
5023         if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
5024                 return (err);
5025
5026         /* Wait for Transmit to Complete */
5027         for(i = 0; i < 10000; i++) 
5028         {
5029                 if(fcb->frame_status & FCB_COMMAND_DONE)
5030                         break;
5031                 mdelay(1);
5032         }
5033
5034         /* Check if GOOD frame Tx'ed */
5035         if(!(fcb->frame_status & FCB_COMMAND_DONE))
5036         {
5037                 if((err = smctr_issue_resume_tx_fcb_cmd(dev, MAC_QUEUE)))
5038                         return (err);
5039
5040                 for(i = 0; i < 10000; i++) 
5041                 {
5042                         if(fcb->frame_status & FCB_COMMAND_DONE)
5043                                 break;
5044                         mdelay(1);
5045                 }
5046
5047                 if(!(fcb->frame_status & FCB_COMMAND_DONE))
5048                         return (HARDWARE_FAILED);
5049         }
5050
5051         *tx_fstatus = fcb->frame_status;
5052
5053         return (A_FRAME_WAS_FORWARDED);
5054 }
5055
5056 static int smctr_set_auth_access_pri(struct net_device *dev,
5057         MAC_SUB_VECTOR *rsv)
5058 {
5059         struct net_local *tp = netdev_priv(dev);
5060
5061         if(rsv->svl != S_AUTHORIZED_ACCESS_PRIORITY)
5062                 return (E_SUB_VECTOR_LENGTH_ERROR);
5063
5064         tp->authorized_access_priority = (rsv->svv[0] << 8 | rsv->svv[1]);
5065
5066         return (POSITIVE_ACK);
5067 }
5068
5069 static int smctr_set_auth_funct_class(struct net_device *dev,
5070         MAC_SUB_VECTOR *rsv)
5071 {
5072         struct net_local *tp = netdev_priv(dev);
5073
5074         if(rsv->svl != S_AUTHORIZED_FUNCTION_CLASS)
5075                 return (E_SUB_VECTOR_LENGTH_ERROR);
5076
5077         tp->authorized_function_classes = (rsv->svv[0] << 8 | rsv->svv[1]);
5078
5079         return (POSITIVE_ACK);
5080 }
5081
5082 static int smctr_set_corr(struct net_device *dev, MAC_SUB_VECTOR *rsv,
5083         __u16 *correlator)
5084 {
5085         if(rsv->svl != S_CORRELATOR)
5086                 return (E_SUB_VECTOR_LENGTH_ERROR);
5087
5088         *correlator = (rsv->svv[0] << 8 | rsv->svv[1]);
5089
5090         return (POSITIVE_ACK);
5091 }
5092
5093 static int smctr_set_error_timer_value(struct net_device *dev,
5094         MAC_SUB_VECTOR *rsv)
5095 {
5096         __u16 err_tval;
5097         int err;
5098
5099         if(rsv->svl != S_ERROR_TIMER_VALUE)
5100                 return (E_SUB_VECTOR_LENGTH_ERROR);
5101
5102         err_tval = (rsv->svv[0] << 8 | rsv->svv[1])*10;
5103
5104         smctr_issue_write_word_cmd(dev, RW_TER_THRESHOLD, &err_tval);
5105
5106         if((err = smctr_wait_cmd(dev)))
5107                 return (err);
5108
5109         return (POSITIVE_ACK);
5110 }
5111
5112 static int smctr_set_frame_forward(struct net_device *dev,
5113         MAC_SUB_VECTOR *rsv, __u8 dc_sc)
5114 {
5115         if((rsv->svl < 2) || (rsv->svl > S_FRAME_FORWARD))
5116                 return (E_SUB_VECTOR_LENGTH_ERROR);
5117
5118         if((dc_sc & DC_MASK) != DC_CRS)
5119         {
5120                 if(rsv->svl >= 2 && rsv->svl < 20)
5121                         return (E_TRANSMIT_FORWARD_INVALID);
5122
5123                 if((rsv->svv[0] != 0) || (rsv->svv[1] != 0))
5124                         return (E_TRANSMIT_FORWARD_INVALID);
5125         }
5126
5127         return (POSITIVE_ACK);
5128 }
5129
5130 static int smctr_set_local_ring_num(struct net_device *dev,
5131         MAC_SUB_VECTOR *rsv)
5132 {
5133         struct net_local *tp = netdev_priv(dev);
5134
5135         if(rsv->svl != S_LOCAL_RING_NUMBER)
5136                 return (E_SUB_VECTOR_LENGTH_ERROR);
5137
5138         if(tp->ptr_local_ring_num)
5139                 *(__u16 *)(tp->ptr_local_ring_num) 
5140                         = (rsv->svv[0] << 8 | rsv->svv[1]);
5141
5142         return (POSITIVE_ACK);
5143 }
5144
5145 static unsigned short smctr_set_ctrl_attention(struct net_device *dev)
5146 {
5147         struct net_local *tp = netdev_priv(dev);
5148         int ioaddr = dev->base_addr;
5149
5150         if(tp->bic_type == BIC_585_CHIP)
5151                 outb((tp->trc_mask | HWR_CA), ioaddr + HWR);
5152         else
5153         {
5154                 outb((tp->trc_mask | CSR_CA), ioaddr + CSR);
5155                 outb(tp->trc_mask, ioaddr + CSR);
5156         }
5157
5158         return (0);
5159 }
5160
5161 static void smctr_set_multicast_list(struct net_device *dev)
5162 {
5163         if(smctr_debug > 10)
5164                 printk(KERN_DEBUG "%s: smctr_set_multicast_list\n", dev->name);
5165
5166         return;
5167 }
5168
5169 static int smctr_set_page(struct net_device *dev, __u8 *buf)
5170 {
5171         struct net_local *tp = netdev_priv(dev);
5172         __u8 amask;
5173         __u32 tptr;
5174
5175         tptr = (__u32)buf - (__u32)tp->ram_access;
5176         amask = (__u8)((tptr & PR_PAGE_MASK) >> 8);
5177         outb(amask, dev->base_addr + PR);
5178
5179         return (0);
5180 }
5181
5182 static int smctr_set_phy_drop(struct net_device *dev, MAC_SUB_VECTOR *rsv)
5183 {
5184         int err;
5185
5186         if(rsv->svl != S_PHYSICAL_DROP)
5187                 return (E_SUB_VECTOR_LENGTH_ERROR);
5188
5189         smctr_issue_write_byte_cmd(dev, RW_PHYSICAL_DROP_NUMBER, &rsv->svv[0]);
5190         if((err = smctr_wait_cmd(dev)))
5191                 return (err);
5192
5193         return (POSITIVE_ACK);
5194 }
5195
5196 /* Reset the ring speed to the opposite of what it was. This auto-pilot
5197  * mode requires a complete reset and re-init of the adapter.
5198  */
5199 static int smctr_set_ring_speed(struct net_device *dev)
5200 {
5201         struct net_local *tp = netdev_priv(dev);
5202         int err;
5203
5204         if(tp->media_type == MEDIA_UTP_16)
5205                 tp->media_type = MEDIA_UTP_4;
5206         else
5207                 tp->media_type = MEDIA_UTP_16;
5208
5209         smctr_enable_16bit(dev);
5210
5211         /* Re-Initialize adapter's internal registers */
5212         smctr_reset_adapter(dev);
5213
5214         if((err = smctr_init_card_real(dev)))
5215                 return (err);
5216
5217         smctr_enable_bic_int(dev);
5218
5219         if((err = smctr_issue_enable_int_cmd(dev, TRC_INTERRUPT_ENABLE_MASK)))
5220                 return (err);
5221
5222         smctr_disable_16bit(dev);
5223
5224         return (0);
5225 }
5226
5227 static int smctr_set_rx_look_ahead(struct net_device *dev)
5228 {
5229         struct net_local *tp = netdev_priv(dev);
5230         __u16 sword, rword;
5231
5232         if(smctr_debug > 10)
5233                 printk(KERN_DEBUG "%s: smctr_set_rx_look_ahead_flag\n", dev->name);
5234
5235         tp->adapter_flags &= ~(FORCED_16BIT_MODE);
5236         tp->adapter_flags |= RX_VALID_LOOKAHEAD;
5237
5238         if(tp->adapter_bus == BUS_ISA16_TYPE)
5239         {
5240                 sword = *((__u16 *)(tp->ram_access));
5241                 *((__u16 *)(tp->ram_access)) = 0x1234;
5242
5243                 smctr_disable_16bit(dev);
5244                 rword = *((__u16 *)(tp->ram_access));
5245                 smctr_enable_16bit(dev);
5246
5247                 if(rword != 0x1234)
5248                         tp->adapter_flags |= FORCED_16BIT_MODE;
5249
5250                 *((__u16 *)(tp->ram_access)) = sword;
5251         }
5252
5253         return (0);
5254 }
5255
5256 static int smctr_set_trc_reset(int ioaddr)
5257 {
5258         __u8 r;
5259
5260         r = inb(ioaddr + MSR);
5261         outb(MSR_RST | r, ioaddr + MSR);
5262
5263         return (0);
5264 }
5265
5266 /*
5267  * This function can be called if the adapter is busy or not.
5268  */
5269 static int smctr_setup_single_cmd(struct net_device *dev,
5270         __u16 command, __u16 subcommand)
5271 {
5272         struct net_local *tp = netdev_priv(dev);
5273         unsigned int err;
5274
5275         if(smctr_debug > 10)
5276                 printk(KERN_DEBUG "%s: smctr_setup_single_cmd\n", dev->name);
5277
5278         if((err = smctr_wait_while_cbusy(dev)))
5279                 return (err);
5280
5281         if((err = (unsigned int)smctr_wait_cmd(dev)))
5282                 return (err);
5283
5284         tp->acb_head->cmd_done_status   = 0;
5285         tp->acb_head->cmd               = command;
5286         tp->acb_head->subcmd            = subcommand;
5287
5288         err = smctr_issue_resume_acb_cmd(dev);
5289
5290         return (err);
5291 }
5292
5293 /*
5294  * This function can not be called with the adapter busy.
5295  */
5296 static int smctr_setup_single_cmd_w_data(struct net_device *dev,
5297         __u16 command, __u16 subcommand)
5298 {
5299         struct net_local *tp = netdev_priv(dev);
5300
5301         tp->acb_head->cmd_done_status   = ACB_COMMAND_NOT_DONE;
5302         tp->acb_head->cmd               = command;
5303         tp->acb_head->subcmd            = subcommand;
5304         tp->acb_head->data_offset_lo
5305                 = (__u16)TRC_POINTER(tp->misc_command_data);
5306
5307         return(smctr_issue_resume_acb_cmd(dev));
5308 }
5309
5310 static char *smctr_malloc(struct net_device *dev, __u16 size)
5311 {
5312         struct net_local *tp = netdev_priv(dev);
5313         char *m;
5314
5315         m = (char *)(tp->ram_access + tp->sh_mem_used);
5316         tp->sh_mem_used += (__u32)size;
5317
5318         return (m);
5319 }
5320
5321 static int smctr_status_chg(struct net_device *dev)
5322 {
5323         struct net_local *tp = netdev_priv(dev);
5324
5325         if(smctr_debug > 10)
5326                 printk(KERN_DEBUG "%s: smctr_status_chg\n", dev->name);
5327
5328         switch(tp->status)
5329         {
5330                 case OPEN:
5331                         break;
5332
5333                 case CLOSED:
5334                         break;
5335
5336                 /* Interrupt driven open() completion. XXX */
5337                 case INITIALIZED:
5338                         tp->group_address_0 = 0;
5339                         tp->group_address[0] = 0;
5340                         tp->group_address[1] = 0;
5341                         tp->functional_address_0 = 0;
5342                         tp->functional_address[0] = 0;
5343                         tp->functional_address[1] = 0;
5344                         smctr_open_tr(dev);
5345                         break;
5346
5347                 default:
5348                         printk(KERN_INFO "%s: status change unknown %x\n",
5349                                 dev->name, tp->status);
5350                         break;
5351         }
5352
5353         return (0);
5354 }
5355
5356 static int smctr_trc_send_packet(struct net_device *dev, FCBlock *fcb,
5357         __u16 queue)
5358 {
5359         struct net_local *tp = netdev_priv(dev);
5360         int err = 0;
5361
5362         if(smctr_debug > 10)
5363                 printk(KERN_DEBUG "%s: smctr_trc_send_packet\n", dev->name);
5364
5365         fcb->info = FCB_CHAIN_END | FCB_ENABLE_TFS;
5366         if(tp->num_tx_fcbs[queue] != 1)
5367                 fcb->back_ptr->info = FCB_INTERRUPT_ENABLE | FCB_ENABLE_TFS;
5368
5369         if(tp->tx_queue_status[queue] == NOT_TRANSMITING)
5370         {
5371                 tp->tx_queue_status[queue] = TRANSMITING;
5372                 err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
5373         }
5374
5375         return (err);
5376 }
5377
5378 static __u16 smctr_tx_complete(struct net_device *dev, __u16 queue)
5379 {
5380         struct net_local *tp = netdev_priv(dev);
5381         __u16 status, err = 0;
5382         int cstatus;
5383
5384         if(smctr_debug > 10)
5385                 printk(KERN_DEBUG "%s: smctr_tx_complete\n", dev->name);
5386
5387         while((status = tp->tx_fcb_end[queue]->frame_status) != SUCCESS)
5388         {
5389                 if(status & 0x7e00 )
5390                 {
5391                         err = HARDWARE_FAILED;
5392                         break;
5393                 }
5394
5395                 if((err = smctr_update_tx_chain(dev, tp->tx_fcb_end[queue],
5396                         queue)) != SUCCESS)
5397                         break;
5398
5399                 smctr_disable_16bit(dev);
5400
5401                 if(tp->mode_bits & UMAC)
5402                 {
5403                         if(!(status & (FCB_TX_STATUS_AR1 | FCB_TX_STATUS_AR2)))
5404                                 cstatus = NO_SUCH_DESTINATION;
5405                         else
5406                         {
5407                                 if(!(status & (FCB_TX_STATUS_CR1 | FCB_TX_STATUS_CR2)))
5408                                         cstatus = DEST_OUT_OF_RESOURCES;
5409                                 else
5410                                 {
5411                                         if(status & FCB_TX_STATUS_E)
5412                                                 cstatus = MAX_COLLISIONS;
5413                                         else
5414                                                 cstatus = SUCCESS;
5415                                 }
5416                         }
5417                 }
5418                 else
5419                         cstatus = SUCCESS;
5420
5421                 if(queue == BUG_QUEUE)
5422                         err = SUCCESS;
5423
5424                 smctr_enable_16bit(dev);
5425                 if(err != SUCCESS)
5426                         break;
5427         }
5428
5429         return (err);
5430 }
5431
5432 static unsigned short smctr_tx_move_frame(struct net_device *dev,
5433         struct sk_buff *skb, __u8 *pbuff, unsigned int bytes)
5434 {
5435         struct net_local *tp = netdev_priv(dev);
5436         unsigned int ram_usable;
5437         __u32 flen, len, offset = 0;
5438         __u8 *frag, *page;
5439
5440         if(smctr_debug > 10)
5441                 printk(KERN_DEBUG "%s: smctr_tx_move_frame\n", dev->name);
5442
5443         ram_usable = ((unsigned int)tp->ram_usable) << 10;
5444         frag       = skb->data;
5445         flen       = skb->len;
5446
5447         while(flen > 0 && bytes > 0)
5448         {
5449                 smctr_set_page(dev, pbuff);
5450
5451                 offset = SMC_PAGE_OFFSET(pbuff);
5452
5453                 if(offset + flen > ram_usable)
5454                         len = ram_usable - offset;
5455                 else
5456                         len = flen;
5457
5458                 if(len > bytes)
5459                         len = bytes;
5460
5461                 page = (char *) (offset + tp->ram_access);
5462                 memcpy(page, frag, len);
5463
5464                 flen -=len;
5465                 bytes -= len;
5466                 frag += len;
5467                 pbuff += len;
5468         }
5469
5470         return (0);
5471 }
5472
5473 /* Update the error statistic counters for this adapter. */
5474 static int smctr_update_err_stats(struct net_device *dev)
5475 {
5476         struct net_local *tp = netdev_priv(dev);
5477         struct tr_statistics *tstat = &tp->MacStat;
5478
5479         if(tstat->internal_errors)
5480                 tstat->internal_errors
5481                         += *(tp->misc_command_data + 0) & 0x00ff;
5482
5483         if(tstat->line_errors)
5484                 tstat->line_errors += *(tp->misc_command_data + 0) >> 8;
5485
5486         if(tstat->A_C_errors)
5487                 tstat->A_C_errors += *(tp->misc_command_data + 1) & 0x00ff;
5488
5489         if(tstat->burst_errors)
5490                 tstat->burst_errors += *(tp->misc_command_data + 1) >> 8;
5491
5492         if(tstat->abort_delimiters)
5493                 tstat->abort_delimiters += *(tp->misc_command_data + 2) >> 8;
5494
5495         if(tstat->recv_congest_count)
5496                 tstat->recv_congest_count
5497                         += *(tp->misc_command_data + 3) & 0x00ff;
5498
5499         if(tstat->lost_frames)
5500                 tstat->lost_frames
5501                         += *(tp->misc_command_data + 3) >> 8;
5502
5503         if(tstat->frequency_errors)
5504                 tstat->frequency_errors += *(tp->misc_command_data + 4) & 0x00ff;
5505
5506         if(tstat->frame_copied_errors)
5507                  tstat->frame_copied_errors
5508                         += *(tp->misc_command_data + 4) >> 8;
5509
5510         if(tstat->token_errors)
5511                 tstat->token_errors += *(tp->misc_command_data + 5) >> 8;
5512
5513         return (0);
5514 }
5515
5516 static int smctr_update_rx_chain(struct net_device *dev, __u16 queue)
5517 {
5518         struct net_local *tp = netdev_priv(dev);
5519         FCBlock *fcb;
5520         BDBlock *bdb;
5521         __u16 size, len;
5522
5523         fcb = tp->rx_fcb_curr[queue];
5524         len = fcb->frame_length;
5525
5526         fcb->frame_status = 0;
5527         fcb->info = FCB_CHAIN_END;
5528         fcb->back_ptr->info = FCB_WARNING;
5529
5530         tp->rx_fcb_curr[queue] = tp->rx_fcb_curr[queue]->next_ptr;
5531
5532         /* update RX BDBs */
5533         size = (len >> RX_BDB_SIZE_SHIFT);
5534         if(len & RX_DATA_BUFFER_SIZE_MASK)
5535                 size += sizeof(BDBlock);
5536         size &= (~RX_BDB_SIZE_MASK);
5537
5538         /* check if wrap around */
5539         bdb = (BDBlock *)((__u32)(tp->rx_bdb_curr[queue]) + (__u32)(size));
5540         if((__u32)bdb >= (__u32)tp->rx_bdb_end[queue])
5541         {
5542                 bdb = (BDBlock *)((__u32)(tp->rx_bdb_head[queue])
5543                         + (__u32)(bdb) - (__u32)(tp->rx_bdb_end[queue]));
5544         }
5545
5546         bdb->back_ptr->info = BDB_CHAIN_END;
5547         tp->rx_bdb_curr[queue]->back_ptr->info = BDB_NOT_CHAIN_END;
5548         tp->rx_bdb_curr[queue] = bdb;
5549
5550         return (0);
5551 }
5552
5553 static int smctr_update_tx_chain(struct net_device *dev, FCBlock *fcb,
5554         __u16 queue)
5555 {
5556         struct net_local *tp = netdev_priv(dev);
5557
5558         if(smctr_debug > 20)
5559                 printk(KERN_DEBUG "smctr_update_tx_chain\n");
5560
5561         if(tp->num_tx_fcbs_used[queue] <= 0)
5562                 return (HARDWARE_FAILED);
5563         else
5564         {
5565                 if(tp->tx_buff_used[queue] < fcb->memory_alloc)
5566                 {
5567                         tp->tx_buff_used[queue] = 0;
5568                         return (HARDWARE_FAILED);
5569                 }
5570
5571                 tp->tx_buff_used[queue] -= fcb->memory_alloc;
5572
5573                 /* if all transmit buffer are cleared
5574                  * need to set the tx_buff_curr[] to tx_buff_head[]
5575                  * otherwise, tx buffer will be segregate and cannot
5576                  * accommodate and buffer greater than (curr - head) and
5577                  * (end - curr) since we do not allow wrap around allocation.
5578                  */
5579                 if(tp->tx_buff_used[queue] == 0)
5580                         tp->tx_buff_curr[queue] = tp->tx_buff_head[queue];
5581
5582                 tp->num_tx_fcbs_used[queue]--;
5583                 fcb->frame_status = 0;
5584                 tp->tx_fcb_end[queue] = fcb->next_ptr;
5585                 netif_wake_queue(dev);
5586                 return (0);
5587         }
5588 }
5589
5590 static int smctr_wait_cmd(struct net_device *dev)
5591 {
5592         struct net_local *tp = netdev_priv(dev);
5593         unsigned int loop_count = 0x20000;
5594
5595         if(smctr_debug > 10)
5596                 printk(KERN_DEBUG "%s: smctr_wait_cmd\n", dev->name);
5597
5598         while(loop_count)
5599         {
5600                 if(tp->acb_head->cmd_done_status & ACB_COMMAND_DONE)
5601                         break;
5602                 udelay(1);
5603                 loop_count--;
5604         }
5605
5606         if(loop_count == 0)
5607                 return(HARDWARE_FAILED);
5608
5609         if(tp->acb_head->cmd_done_status & 0xff)
5610                 return(HARDWARE_FAILED);
5611
5612         return (0);
5613 }
5614
5615 static int smctr_wait_while_cbusy(struct net_device *dev)
5616 {
5617         struct net_local *tp = netdev_priv(dev);
5618         unsigned int timeout = 0x20000;
5619         int ioaddr = dev->base_addr;
5620         __u8 r;
5621
5622         if(tp->bic_type == BIC_585_CHIP)
5623         {
5624                 while(timeout)
5625                 {
5626                         r = inb(ioaddr + HWR);
5627                         if((r & HWR_CBUSY) == 0)
5628                                 break;
5629                         timeout--;
5630                 }
5631         }
5632         else
5633         {
5634                 while(timeout)
5635                 {
5636                         r = inb(ioaddr + CSR);
5637                         if((r & CSR_CBUSY) == 0)
5638                                 break;
5639                         timeout--;
5640                 }
5641         }
5642
5643         if(timeout)
5644                 return (0);
5645         else
5646                 return (HARDWARE_FAILED);
5647 }
5648
5649 #ifdef MODULE
5650
5651 static struct net_device* dev_smctr[SMCTR_MAX_ADAPTERS];
5652 static int io[SMCTR_MAX_ADAPTERS];
5653 static int irq[SMCTR_MAX_ADAPTERS];
5654
5655 MODULE_LICENSE("GPL");
5656
5657 module_param_array(io, int, NULL, 0);
5658 module_param_array(irq, int, NULL, 0);
5659 module_param(ringspeed, int, 0);
5660
5661 static struct net_device * __init setup_card(int n)
5662 {
5663         struct net_device *dev = alloc_trdev(sizeof(struct net_local));
5664         int err;
5665         
5666         if (!dev)
5667                 return ERR_PTR(-ENOMEM);
5668
5669         dev->irq = irq[n];
5670         err = smctr_probe1(dev, io[n]);
5671         if (err) 
5672                 goto out;
5673                 
5674         err = register_netdev(dev);
5675         if (err)
5676                 goto out1;
5677         return dev;
5678  out1:
5679 #ifdef CONFIG_MCA_LEGACY
5680         { struct net_local *tp = netdev_priv(dev);
5681           if (tp->slot_num)
5682                 mca_mark_as_unused(tp->slot_num);
5683         }
5684 #endif
5685         release_region(dev->base_addr, SMCTR_IO_EXTENT);
5686         free_irq(dev->irq, dev);
5687 out:
5688         free_netdev(dev);
5689         return ERR_PTR(err);
5690 }
5691
5692 int __init init_module(void)
5693 {
5694         int i, found = 0;
5695         struct net_device *dev;
5696
5697         for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
5698                 dev = io[0]? setup_card(i) : smctr_probe(-1);
5699                 if (!IS_ERR(dev)) {
5700                         ++found;
5701                         dev_smctr[i] = dev;
5702                 }
5703         }
5704
5705         return found ? 0 : -ENODEV;
5706 }
5707
5708 void __exit cleanup_module(void)
5709 {
5710         int i;
5711
5712         for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
5713                 struct net_device *dev = dev_smctr[i];
5714
5715                 if (dev) {
5716
5717                         unregister_netdev(dev);
5718 #ifdef CONFIG_MCA_LEGACY
5719                         { struct net_local *tp = netdev_priv(dev);
5720                         if (tp->slot_num)
5721                                 mca_mark_as_unused(tp->slot_num);
5722                         }
5723 #endif
5724                         release_region(dev->base_addr, SMCTR_IO_EXTENT);
5725                         if (dev->irq)
5726                                 free_irq(dev->irq, dev);
5727
5728                         free_netdev(dev);
5729                 }
5730         }
5731 }
5732 #endif /* MODULE */