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