1 /*
2 * tms380tr.c: A network driver library for Texas Instruments TMS380-based
3 * Token Ring Adapters.
4 *
5 * Originally sktr.c: Written 1997 by Christoph Goos
6 *
7 * A fine result of the Linux Systems Network Architecture Project.
8 * http://www.linux-sna.org
9 *
10 * This software may be used and distributed according to the terms
11 * of the GNU General Public License, incorporated herein by reference.
12 *
13 * The following modules are currently available for card support:
14 * - tmspci (Generic PCI card support)
15 * - abyss (Madge PCI support)
16 * - tmsisa (SysKonnect TR4/16 ISA)
17 *
18 * Sources:
19 * - The hardware related parts of this driver are take from
20 * the SysKonnect Token Ring driver for Windows NT.
21 * - I used the IBM Token Ring driver 'ibmtr.c' as a base for this
22 * driver, as well as the 'skeleton.c' driver by Donald Becker.
23 * - Also various other drivers in the linux source tree were taken
24 * as samples for some tasks.
25 * - TI TMS380 Second-Generation Token Ring User's Guide
26 * - TI datasheets for respective chips
27 * - David Hein at Texas Instruments
28 * - Various Madge employees
29 *
30 * Maintainer(s):
31 * JS Jay Schulist jschlst@samba.org
32 * CG Christoph Goos cgoos@syskonnect.de
33 * AF Adam Fritzler mid@auk.cx
34 * MLP Mike Phillips phillim@amtrak.com
35 * JF Jochen Friedrich jochen@scram.de
36 *
37 * Modification History:
38 * 29-Aug-97 CG Created
39 * 04-Apr-98 CG Fixed problems caused by tok_timer_check
40 * 10-Apr-98 CG Fixed lockups at cable disconnection
41 * 27-May-98 JS Formated to Linux Kernel Format
42 * 31-May-98 JS Hacked in PCI support
43 * 16-Jun-98 JS Modulized for multiple cards with one driver
44 * Sep-99 AF Renamed to tms380tr (supports more than SK's)
45 * 23-Sep-99 AF Added Compaq and Thomas-Conrad PCI support
46 * Fixed a bug causing double copies on PCI
47 * Fixed for new multicast stuff (2.2/2.3)
48 * 25-Sep-99 AF Uped TPL_NUM from 3 to 9
49 * Removed extraneous 'No free TPL'
50 * 22-Dec-99 AF Added Madge PCI Mk2 support and generalized
51 * parts of the initilization procedure.
52 * 30-Dec-99 AF Turned tms380tr into a library ala 8390.
53 * Madge support is provided in the abyss module
54 * Generic PCI support is in the tmspci module.
55 * 30-Nov-00 JF Updated PCI code to support IO MMU via
56 * pci_map_static(). Alpha uses this MMU for ISA
57 * as well.
58 * 14-Jan-01 JF Fix DMA on ifdown/ifup sequences. Some
59 * cleanup.
60 *
61 * To do:
62 * 1. Multi/Broadcast packet handling (this may have fixed itself)
63 * 2. Write a sktrisa module that includes the old ISA support (done)
64 * 3. Allow modules to load their own microcode
65 * 4. Speed up the BUD process -- freezing the kernel for 3+sec is
66 * quite unacceptable.
67 * 5. Still a few remaining stalls when the cable is unplugged.
68 */
69
70 #ifdef MODULE
71 static const char version[] = "tms380tr.c: v1.08 14/01/2001 by Christoph Goos, Adam Fritzler\n";
72 #endif
73
74 #include <linux/module.h>
75 #include <linux/version.h>
76
77 #include <linux/kernel.h>
78 #include <linux/sched.h>
79 #include <linux/types.h>
80 #include <linux/fcntl.h>
81 #include <linux/interrupt.h>
82 #include <linux/ptrace.h>
83 #include <linux/ioport.h>
84 #include <linux/in.h>
85 #include <linux/slab.h>
86 #include <linux/string.h>
87 #include <linux/time.h>
88 #include <asm/system.h>
89 #include <asm/bitops.h>
90 #include <asm/io.h>
91 #include <asm/dma.h>
92 #include <asm/irq.h>
93 #include <asm/uaccess.h>
94 #include <linux/errno.h>
95 #include <linux/init.h>
96 #include <linux/pci.h>
97 #include <linux/delay.h>
98
99 #include <linux/netdevice.h>
100 #include <linux/etherdevice.h>
101 #include <linux/skbuff.h>
102 #include <linux/trdevice.h>
103
104 #include "tms380tr.h" /* Our Stuff */
105 #include "tms380tr_microcode.h" /* TI microcode for COMMprocessor */
106
107 /* Use 0 for production, 1 for verification, 2 for debug, and
108 * 3 for very verbose debug.
109 */
110 #ifndef TMS380TR_DEBUG
111 #define TMS380TR_DEBUG 0
112 #endif
113 static unsigned int tms380tr_debug = TMS380TR_DEBUG;
114
115 /* Index to functions, as function prototypes.
116 * Alphabetical by function name.
117 */
118
119 /* "A" */
120 /* "B" */
121 static int tms380tr_bringup_diags(struct net_device *dev);
122 /* "C" */
123 static void tms380tr_cancel_tx_queue(struct net_local* tp);
124 static int tms380tr_chipset_init(struct net_device *dev);
125 static void tms380tr_chk_irq(struct net_device *dev);
126 static void tms380tr_chk_outstanding_cmds(struct net_device *dev);
127 static void tms380tr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr);
128 static unsigned char tms380tr_chk_ssb(struct net_local *tp, unsigned short IrqType);
129 int tms380tr_close(struct net_device *dev);
130 static void tms380tr_cmd_status_irq(struct net_device *dev);
131 /* "D" */
132 static void tms380tr_disable_interrupts(struct net_device *dev);
133 #if TMS380TR_DEBUG > 0
134 static void tms380tr_dump(unsigned char *Data, int length);
135 #endif
136 /* "E" */
137 static void tms380tr_enable_interrupts(struct net_device *dev);
138 static void tms380tr_exec_cmd(struct net_device *dev, unsigned short Command);
139 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue);
140 /* "F" */
141 /* "G" */
142 static struct net_device_stats *tms380tr_get_stats(struct net_device *dev);
143 /* "H" */
144 static void tms380tr_hardware_send_packet(struct net_device *dev,
145 struct net_local* tp);
146 /* "I" */
147 static int tms380tr_init_adapter(struct net_device *dev);
148 static int tms380tr_init_card(struct net_device *dev);
149 static void tms380tr_init_ipb(struct net_local *tp);
150 static void tms380tr_init_net_local(struct net_device *dev);
151 static void tms380tr_init_opb(struct net_device *dev);
152 void tms380tr_interrupt(int irq, void *dev_id, struct pt_regs *regs);
153 /* "M" */
154 /* "O" */
155 int tms380tr_open(struct net_device *dev);
156 static void tms380tr_open_adapter(struct net_device *dev);
157 /* "P" */
158 /* "R" */
159 static void tms380tr_rcv_status_irq(struct net_device *dev);
160 static int tms380tr_read_ptr(struct net_device *dev);
161 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
162 unsigned short Address, int Length);
163 static int tms380tr_reset_adapter(struct net_device *dev);
164 static void tms380tr_reset_interrupt(struct net_device *dev);
165 static void tms380tr_ring_status_irq(struct net_device *dev);
166 /* "S" */
167 static int tms380tr_send_packet(struct sk_buff *skb, struct net_device *dev);
168 static void tms380tr_set_multicast_list(struct net_device *dev);
169 static int tms380tr_set_mac_address(struct net_device *dev, void *addr);
170 /* "T" */
171 static void tms380tr_timer_chk(unsigned long data);
172 static void tms380tr_timer_end_wait(unsigned long data);
173 static void tms380tr_tx_status_irq(struct net_device *dev);
174 /* "U" */
175 static void tms380tr_update_rcv_stats(struct net_local *tp,
176 unsigned char DataPtr[], unsigned int Length);
177 /* "W" */
178 void tms380tr_wait(unsigned long time);
179 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status);
180 static void tms380tr_write_tpl_status(TPL *tpl, unsigned int Status);
181
182 #define SIFREADB(reg) (((struct net_local *)dev->priv)->sifreadb(dev, reg))
183 #define SIFWRITEB(val, reg) (((struct net_local *)dev->priv)->sifwriteb(dev, val, reg))
184 #define SIFREADW(reg) (((struct net_local *)dev->priv)->sifreadw(dev, reg))
185 #define SIFWRITEW(val, reg) (((struct net_local *)dev->priv)->sifwritew(dev, val, reg))
186
187
188
189 #if 0 /* TMS380TR_DEBUG > 0 */
190 static int madgemc_sifprobe(struct net_device *dev)
191 {
192 unsigned char old, chk1, chk2;
193
194 old = SIFREADB(SIFADR); /* Get the old SIFADR value */
195
196 chk1 = 0; /* Begin with check value 0 */
197 do {
198 madgemc_setregpage(dev, 0);
199 /* Write new SIFADR value */
200 SIFWRITEB(chk1, SIFADR);
201 chk2 = SIFREADB(SIFADR);
202 if (chk2 != chk1)
203 return -1;
204
205 madgemc_setregpage(dev, 1);
206 /* Read, invert and write */
207 chk2 = SIFREADB(SIFADD);
208 if (chk2 != chk1)
209 return -1;
210
211 madgemc_setregpage(dev, 0);
212 chk2 ^= 0x0FE;
213 SIFWRITEB(chk2, SIFADR);
214
215 /* Read, invert and compare */
216 madgemc_setregpage(dev, 1);
217 chk2 = SIFREADB(SIFADD);
218 madgemc_setregpage(dev, 0);
219 chk2 ^= 0x0FE;
220
221 if(chk1 != chk2)
222 return (-1); /* No adapter */
223 chk1 -= 2;
224 } while(chk1 != 0); /* Repeat 128 times (all byte values) */
225
226 madgemc_setregpage(dev, 0); /* sanity */
227 /* Restore the SIFADR value */
228 SIFWRITEB(old, SIFADR);
229
230 return (0);
231 }
232 #endif
233
234 /* Dummy function */
tms380tr_init_card(struct net_device * dev)235 static int __init tms380tr_init_card(struct net_device *dev)
236 {
237 if(tms380tr_debug > 3)
238 printk("%s: tms380tr_init_card\n", dev->name);
239
240 return (0);
241 }
242
243 /*
244 * Open/initialize the board. This is called sometime after
245 * booting when the 'ifconfig' program is run.
246 *
247 * This routine should set everything up anew at each open, even
248 * registers that "should" only need to be set once at boot, so that
249 * there is non-reboot way to recover if something goes wrong.
250 */
tms380tr_open(struct net_device * dev)251 int tms380tr_open(struct net_device *dev)
252 {
253 struct net_local *tp = (struct net_local *)dev->priv;
254 int err;
255
256 /* Reset the hardware here. Don't forget to set the station address. */
257
258 if(dev->dma > 0)
259 {
260 unsigned long flags=claim_dma_lock();
261 disable_dma(dev->dma);
262 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
263 enable_dma(dev->dma);
264 release_dma_lock(flags);
265 }
266
267 err = tms380tr_chipset_init(dev);
268 if(err)
269 {
270 printk(KERN_INFO "%s: Chipset initialization error\n",
271 dev->name);
272 return (-1);
273 }
274
275 init_timer(&tp->timer);
276 tp->timer.expires = jiffies + 30*HZ;
277 tp->timer.function = tms380tr_timer_end_wait;
278 tp->timer.data = (unsigned long)dev;
279 add_timer(&tp->timer);
280
281 printk(KERN_INFO "%s: Adapter RAM size: %dK\n",
282 dev->name, tms380tr_read_ptr(dev));
283
284 tms380tr_enable_interrupts(dev);
285 tms380tr_open_adapter(dev);
286
287 netif_start_queue(dev);
288
289 /* Wait for interrupt from hardware. If interrupt does not come,
290 * there will be a timeout from the timer.
291 */
292 tp->Sleeping = 1;
293 interruptible_sleep_on(&tp->wait_for_tok_int);
294 del_timer(&tp->timer);
295
296 /* If AdapterVirtOpenFlag is 1, the adapter is now open for use */
297 if(tp->AdapterVirtOpenFlag == 0)
298 {
299 tms380tr_disable_interrupts(dev);
300 return (-1);
301 }
302
303 tp->StartTime = jiffies;
304
305 /* Start function control timer */
306 tp->timer.expires = jiffies + 2*HZ;
307 tp->timer.function = tms380tr_timer_chk;
308 tp->timer.data = (unsigned long)dev;
309 add_timer(&tp->timer);
310
311 return (0);
312 }
313
314 /*
315 * Timeout function while waiting for event
316 */
tms380tr_timer_end_wait(unsigned long data)317 static void tms380tr_timer_end_wait(unsigned long data)
318 {
319 struct net_device *dev = (struct net_device*)data;
320 struct net_local *tp = (struct net_local *)dev->priv;
321
322 if(tp->Sleeping)
323 {
324 tp->Sleeping = 0;
325 wake_up_interruptible(&tp->wait_for_tok_int);
326 }
327
328 return;
329 }
330
331 /*
332 * Initialize the chipset
333 */
tms380tr_chipset_init(struct net_device * dev)334 static int tms380tr_chipset_init(struct net_device *dev)
335 {
336 struct net_local *tp = (struct net_local *)dev->priv;
337 int err;
338
339 tms380tr_init_ipb(tp);
340 tms380tr_init_opb(dev);
341 tms380tr_init_net_local(dev);
342
343 if(tms380tr_debug > 3)
344 printk(KERN_INFO "%s: Resetting adapter...\n", dev->name);
345 err = tms380tr_reset_adapter(dev);
346 if(err < 0)
347 return (-1);
348
349 if(tms380tr_debug > 3)
350 printk(KERN_INFO "%s: Bringup diags...\n", dev->name);
351 err = tms380tr_bringup_diags(dev);
352 if(err < 0)
353 return (-1);
354
355 if(tms380tr_debug > 3)
356 printk(KERN_INFO "%s: Init adapter...\n", dev->name);
357 err = tms380tr_init_adapter(dev);
358 if(err < 0)
359 return (-1);
360
361 if(tms380tr_debug > 3)
362 printk(KERN_INFO "%s: Done!\n", dev->name);
363 return (0);
364 }
365
366 /*
367 * Initializes the net_local structure.
368 */
tms380tr_init_net_local(struct net_device * dev)369 static void tms380tr_init_net_local(struct net_device *dev)
370 {
371 struct net_local *tp = (struct net_local *)dev->priv;
372 int i;
373 dma_addr_t dmabuf;
374
375 tp->scb.CMD = 0;
376 tp->scb.Parm[0] = 0;
377 tp->scb.Parm[1] = 0;
378
379 tp->ssb.STS = 0;
380 tp->ssb.Parm[0] = 0;
381 tp->ssb.Parm[1] = 0;
382 tp->ssb.Parm[2] = 0;
383
384 tp->CMDqueue = 0;
385
386 tp->AdapterOpenFlag = 0;
387 tp->AdapterVirtOpenFlag = 0;
388 tp->ScbInUse = 0;
389 tp->OpenCommandIssued = 0;
390 tp->ReOpenInProgress = 0;
391 tp->HaltInProgress = 0;
392 tp->TransmitHaltScheduled = 0;
393 tp->LobeWireFaultLogged = 0;
394 tp->LastOpenStatus = 0;
395 tp->MaxPacketSize = DEFAULT_PACKET_SIZE;
396
397 skb_queue_head_init(&tp->SendSkbQueue);
398 tp->QueueSkb = MAX_TX_QUEUE;
399
400 /* Create circular chain of transmit lists */
401 for (i = 0; i < TPL_NUM; i++)
402 {
403 tp->Tpl[i].NextTPLAddr = htonl(((char *)(&tp->Tpl[(i+1) % TPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
404 tp->Tpl[i].Status = 0;
405 tp->Tpl[i].FrameSize = 0;
406 tp->Tpl[i].FragList[0].DataCount = 0;
407 tp->Tpl[i].FragList[0].DataAddr = 0;
408 tp->Tpl[i].NextTPLPtr = &tp->Tpl[(i+1) % TPL_NUM];
409 tp->Tpl[i].MData = NULL;
410 tp->Tpl[i].TPLIndex = i;
411 tp->Tpl[i].DMABuff = 0;
412 tp->Tpl[i].BusyFlag = 0;
413 }
414
415 tp->TplFree = tp->TplBusy = &tp->Tpl[0];
416
417 /* Create circular chain of receive lists */
418 for (i = 0; i < RPL_NUM; i++)
419 {
420 tp->Rpl[i].NextRPLAddr = htonl(((char *)(&tp->Rpl[(i+1) % RPL_NUM]) - (char *)tp) + tp->dmabuffer); /* DMA buffer may be MMU driven */
421 tp->Rpl[i].Status = (RX_VALID | RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
422 tp->Rpl[i].FrameSize = 0;
423 tp->Rpl[i].FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
424
425 /* Alloc skb and point adapter to data area */
426 tp->Rpl[i].Skb = dev_alloc_skb(tp->MaxPacketSize);
427 tp->Rpl[i].DMABuff = 0;
428
429 /* skb == NULL ? then use local buffer */
430 if(tp->Rpl[i].Skb == NULL)
431 {
432 tp->Rpl[i].SkbStat = SKB_UNAVAILABLE;
433 tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
434 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
435 }
436 else /* SKB != NULL */
437 {
438 tp->Rpl[i].Skb->dev = dev;
439 skb_put(tp->Rpl[i].Skb, tp->MaxPacketSize);
440
441 /* data unreachable for DMA ? then use local buffer */
442 dmabuf = pci_map_single(tp->pdev, tp->Rpl[i].Skb->data, tp->MaxPacketSize, PCI_DMA_FROMDEVICE);
443 if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
444 {
445 tp->Rpl[i].SkbStat = SKB_DATA_COPY;
446 tp->Rpl[i].FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[i] - (char *)tp) + tp->dmabuffer);
447 tp->Rpl[i].MData = tp->LocalRxBuffers[i];
448 }
449 else /* DMA directly in skb->data */
450 {
451 tp->Rpl[i].SkbStat = SKB_DMA_DIRECT;
452 tp->Rpl[i].FragList[0].DataAddr = htonl(dmabuf);
453 tp->Rpl[i].MData = tp->Rpl[i].Skb->data;
454 tp->Rpl[i].DMABuff = dmabuf;
455 }
456 }
457
458 tp->Rpl[i].NextRPLPtr = &tp->Rpl[(i+1) % RPL_NUM];
459 tp->Rpl[i].RPLIndex = i;
460 }
461
462 tp->RplHead = &tp->Rpl[0];
463 tp->RplTail = &tp->Rpl[RPL_NUM-1];
464 tp->RplTail->Status = (RX_START_FRAME | RX_END_FRAME | RX_FRAME_IRQ);
465
466 return;
467 }
468
469 /*
470 * Initializes the initialisation parameter block.
471 */
tms380tr_init_ipb(struct net_local * tp)472 static void tms380tr_init_ipb(struct net_local *tp)
473 {
474 tp->ipb.Init_Options = BURST_MODE;
475 tp->ipb.CMD_Status_IV = 0;
476 tp->ipb.TX_IV = 0;
477 tp->ipb.RX_IV = 0;
478 tp->ipb.Ring_Status_IV = 0;
479 tp->ipb.SCB_Clear_IV = 0;
480 tp->ipb.Adapter_CHK_IV = 0;
481 tp->ipb.RX_Burst_Size = BURST_SIZE;
482 tp->ipb.TX_Burst_Size = BURST_SIZE;
483 tp->ipb.DMA_Abort_Thrhld = DMA_RETRIES;
484 tp->ipb.SCB_Addr = 0;
485 tp->ipb.SSB_Addr = 0;
486
487 return;
488 }
489
490 /*
491 * Initializes the open parameter block.
492 */
tms380tr_init_opb(struct net_device * dev)493 static void tms380tr_init_opb(struct net_device *dev)
494 {
495 struct net_local *tp;
496 unsigned long Addr;
497 unsigned short RplSize = RPL_SIZE;
498 unsigned short TplSize = TPL_SIZE;
499 unsigned short BufferSize = BUFFER_SIZE;
500 int i;
501
502 tp = (struct net_local *)dev->priv;
503
504 tp->ocpl.OPENOptions = 0;
505 tp->ocpl.OPENOptions |= ENABLE_FULL_DUPLEX_SELECTION;
506 tp->ocpl.FullDuplex = 0;
507 tp->ocpl.FullDuplex |= OPEN_FULL_DUPLEX_OFF;
508
509 /*
510 * Set node address
511 *
512 * We go ahead and put it in the OPB even though on
513 * most of the generic adapters this isn't required.
514 * Its simpler this way. -- ASF
515 */
516 for (i=0;i<6;i++)
517 tp->ocpl.NodeAddr[i] = ((unsigned char *)dev->dev_addr)[i];
518
519 tp->ocpl.GroupAddr = 0;
520 tp->ocpl.FunctAddr = 0;
521 tp->ocpl.RxListSize = cpu_to_be16((unsigned short)RplSize);
522 tp->ocpl.TxListSize = cpu_to_be16((unsigned short)TplSize);
523 tp->ocpl.BufSize = cpu_to_be16((unsigned short)BufferSize);
524 tp->ocpl.Reserved = 0;
525 tp->ocpl.TXBufMin = TX_BUF_MIN;
526 tp->ocpl.TXBufMax = TX_BUF_MAX;
527
528 Addr = htonl(((char *)tp->ProductID - (char *)tp) + tp->dmabuffer);
529
530 tp->ocpl.ProdIDAddr[0] = LOWORD(Addr);
531 tp->ocpl.ProdIDAddr[1] = HIWORD(Addr);
532
533 return;
534 }
535
536 /*
537 * Send OPEN command to adapter
538 */
tms380tr_open_adapter(struct net_device * dev)539 static void tms380tr_open_adapter(struct net_device *dev)
540 {
541 struct net_local *tp = (struct net_local *)dev->priv;
542
543 if(tp->OpenCommandIssued)
544 return;
545
546 tp->OpenCommandIssued = 1;
547 tms380tr_exec_cmd(dev, OC_OPEN);
548
549 return;
550 }
551
552 /*
553 * Clear the adapter's interrupt flag. Clear system interrupt enable
554 * (SINTEN): disable adapter to system interrupts.
555 */
tms380tr_disable_interrupts(struct net_device * dev)556 static void tms380tr_disable_interrupts(struct net_device *dev)
557 {
558 SIFWRITEB(0, SIFACL);
559
560 return;
561 }
562
563 /*
564 * Set the adapter's interrupt flag. Set system interrupt enable
565 * (SINTEN): enable adapter to system interrupts.
566 */
tms380tr_enable_interrupts(struct net_device * dev)567 static void tms380tr_enable_interrupts(struct net_device *dev)
568 {
569 SIFWRITEB(ACL_SINTEN, SIFACL);
570
571 return;
572 }
573
574 /*
575 * Put command in command queue, try to execute it.
576 */
tms380tr_exec_cmd(struct net_device * dev,unsigned short Command)577 static void tms380tr_exec_cmd(struct net_device *dev, unsigned short Command)
578 {
579 struct net_local *tp = (struct net_local *)dev->priv;
580
581 tp->CMDqueue |= Command;
582 tms380tr_chk_outstanding_cmds(dev);
583
584 return;
585 }
586
tms380tr_timeout(struct net_device * dev)587 static void tms380tr_timeout(struct net_device *dev)
588 {
589 /*
590 * If we get here, some higher level has decided we are broken.
591 * There should really be a "kick me" function call instead.
592 *
593 * Resetting the token ring adapter takes a long time so just
594 * fake transmission time and go on trying. Our own timeout
595 * routine is in tms380tr_timer_chk()
596 */
597 dev->trans_start = jiffies;
598 netif_wake_queue(dev);
599 }
600
601 /*
602 * Gets skb from system, queues it and checks if it can be sent
603 */
tms380tr_send_packet(struct sk_buff * skb,struct net_device * dev)604 static int tms380tr_send_packet(struct sk_buff *skb, struct net_device *dev)
605 {
606 struct net_local *tp = (struct net_local *)dev->priv;
607
608 /*
609 * Block transmits from overlapping.
610 */
611
612 netif_stop_queue(dev);
613
614 if(tp->QueueSkb == 0)
615 return (1); /* Return with tbusy set: queue full */
616
617 tp->QueueSkb--;
618 skb_queue_tail(&tp->SendSkbQueue, skb);
619 tms380tr_hardware_send_packet(dev, tp);
620 if(tp->QueueSkb > 0)
621 netif_wake_queue(dev);
622 return (0);
623 }
624
625 /*
626 * Move frames from internal skb queue into adapter tx queue
627 */
tms380tr_hardware_send_packet(struct net_device * dev,struct net_local * tp)628 static void tms380tr_hardware_send_packet(struct net_device *dev, struct net_local* tp)
629 {
630 TPL *tpl;
631 short length;
632 unsigned char *buf;
633 struct sk_buff *skb;
634 int i;
635 dma_addr_t dmabuf, newbuf;
636
637 for(;;)
638 {
639 /* Try to get a free TPL from the chain.
640 *
641 * NOTE: We *must* always leave one unused TPL in the chain,
642 * because otherwise the adapter might send frames twice.
643 */
644 if(tp->TplFree->NextTPLPtr->BusyFlag) /* No free TPL */
645 {
646 if (tms380tr_debug > 0)
647 printk(KERN_INFO "%s: No free TPL\n", dev->name);
648 return;
649 }
650
651 /* Send first buffer from queue */
652 skb = skb_dequeue(&tp->SendSkbQueue);
653 if(skb == NULL)
654 return;
655
656 tp->QueueSkb++;
657 dmabuf = 0;
658
659 /* Is buffer reachable for Busmaster-DMA? */
660
661 length = skb->len;
662 dmabuf = pci_map_single(tp->pdev, skb->data, length, PCI_DMA_TODEVICE);
663 if(tp->dmalimit && (dmabuf + length > tp->dmalimit))
664 {
665 /* Copy frame to local buffer */
666 pci_unmap_single(tp->pdev, dmabuf, length, PCI_DMA_TODEVICE);
667 dmabuf = 0;
668 i = tp->TplFree->TPLIndex;
669 buf = tp->LocalTxBuffers[i];
670 memcpy(buf, skb->data, length);
671 newbuf = ((char *)buf - (char *)tp) + tp->dmabuffer;
672 }
673 else
674 {
675 /* Send direct from skb->data */
676 newbuf = dmabuf;
677 buf = skb->data;
678 }
679 /* Source address in packet? */
680 tms380tr_chk_src_addr(buf, dev->dev_addr);
681 tp->LastSendTime = jiffies;
682 tpl = tp->TplFree; /* Get the "free" TPL */
683 tpl->BusyFlag = 1; /* Mark TPL as busy */
684 tp->TplFree = tpl->NextTPLPtr;
685
686 /* Save the skb for delayed return of skb to system */
687 tpl->Skb = skb;
688 tpl->DMABuff = dmabuf;
689 tpl->FragList[0].DataCount = cpu_to_be16((unsigned short)length);
690 tpl->FragList[0].DataAddr = htonl(newbuf);
691
692 /* Write the data length in the transmit list. */
693 tpl->FrameSize = cpu_to_be16((unsigned short)length);
694 tpl->MData = buf;
695
696 /* Transmit the frame and set the status values. */
697 tms380tr_write_tpl_status(tpl, TX_VALID | TX_START_FRAME
698 | TX_END_FRAME | TX_PASS_SRC_ADDR
699 | TX_FRAME_IRQ);
700
701 /* Let adapter send the frame. */
702 tms380tr_exec_sifcmd(dev, CMD_TX_VALID);
703 }
704
705 return;
706 }
707
708 /*
709 * Write the given value to the 'Status' field of the specified TPL.
710 * NOTE: This function should be used whenever the status of any TPL must be
711 * modified by the driver, because the compiler may otherwise change the
712 * order of instructions such that writing the TPL status may be executed at
713 * an undesireable time. When this function is used, the status is always
714 * written when the function is called.
715 */
tms380tr_write_tpl_status(TPL * tpl,unsigned int Status)716 static void tms380tr_write_tpl_status(TPL *tpl, unsigned int Status)
717 {
718 tpl->Status = Status;
719 }
720
tms380tr_chk_src_addr(unsigned char * frame,unsigned char * hw_addr)721 static void tms380tr_chk_src_addr(unsigned char *frame, unsigned char *hw_addr)
722 {
723 unsigned char SRBit;
724
725 if((((unsigned long)frame[8]) & ~0x80) != 0) /* Compare 4 bytes */
726 return;
727 if((unsigned short)frame[12] != 0) /* Compare 2 bytes */
728 return;
729
730 SRBit = frame[8] & 0x80;
731 memcpy(&frame[8], hw_addr, 6);
732 frame[8] |= SRBit;
733
734 return;
735 }
736
737 /*
738 * The timer routine: Check if adapter still open and working, reopen if not.
739 */
tms380tr_timer_chk(unsigned long data)740 static void tms380tr_timer_chk(unsigned long data)
741 {
742 struct net_device *dev = (struct net_device*)data;
743 struct net_local *tp = (struct net_local*)dev->priv;
744
745 if(tp->HaltInProgress)
746 return;
747
748 tms380tr_chk_outstanding_cmds(dev);
749 if(time_before(tp->LastSendTime + SEND_TIMEOUT, jiffies)
750 && (tp->QueueSkb < MAX_TX_QUEUE || tp->TplFree != tp->TplBusy))
751 {
752 /* Anything to send, but stalled to long */
753 tp->LastSendTime = jiffies;
754 tms380tr_exec_cmd(dev, OC_CLOSE); /* Does reopen automatically */
755 }
756
757 tp->timer.expires = jiffies + 2*HZ;
758 add_timer(&tp->timer);
759
760 if(tp->AdapterOpenFlag || tp->ReOpenInProgress)
761 return;
762 tp->ReOpenInProgress = 1;
763 tms380tr_open_adapter(dev);
764
765 return;
766 }
767
768 /*
769 * The typical workload of the driver: Handle the network interface interrupts.
770 */
tms380tr_interrupt(int irq,void * dev_id,struct pt_regs * regs)771 void tms380tr_interrupt(int irq, void *dev_id, struct pt_regs *regs)
772 {
773 struct net_device *dev = dev_id;
774 struct net_local *tp;
775 unsigned short irq_type;
776
777 if(dev == NULL) {
778 printk("%s: irq %d for unknown device.\n", dev->name, irq);
779 return;
780 }
781
782 tp = (struct net_local *)dev->priv;
783
784 irq_type = SIFREADW(SIFSTS);
785
786 while(irq_type & STS_SYSTEM_IRQ) {
787 irq_type &= STS_IRQ_MASK;
788
789 if(!tms380tr_chk_ssb(tp, irq_type)) {
790 printk(KERN_INFO "%s: DATA LATE occurred\n", dev->name);
791 break;
792 }
793
794 switch(irq_type) {
795 case STS_IRQ_RECEIVE_STATUS:
796 tms380tr_reset_interrupt(dev);
797 tms380tr_rcv_status_irq(dev);
798 break;
799
800 case STS_IRQ_TRANSMIT_STATUS:
801 /* Check if TRANSMIT.HALT command is complete */
802 if(tp->ssb.Parm[0] & COMMAND_COMPLETE) {
803 tp->TransmitCommandActive = 0;
804 tp->TransmitHaltScheduled = 0;
805
806 /* Issue a new transmit command. */
807 tms380tr_exec_cmd(dev, OC_TRANSMIT);
808 }
809
810 tms380tr_reset_interrupt(dev);
811 tms380tr_tx_status_irq(dev);
812 break;
813
814 case STS_IRQ_COMMAND_STATUS:
815 /* The SSB contains status of last command
816 * other than receive/transmit.
817 */
818 tms380tr_cmd_status_irq(dev);
819 break;
820
821 case STS_IRQ_SCB_CLEAR:
822 /* The SCB is free for another command. */
823 tp->ScbInUse = 0;
824 tms380tr_chk_outstanding_cmds(dev);
825 break;
826
827 case STS_IRQ_RING_STATUS:
828 tms380tr_ring_status_irq(dev);
829 break;
830
831 case STS_IRQ_ADAPTER_CHECK:
832 tms380tr_chk_irq(dev);
833 break;
834
835 case STS_IRQ_LLC_STATUS:
836 printk(KERN_DEBUG "tms380tr: unexpected LLC status IRQ\n");
837 break;
838
839 case STS_IRQ_TIMER:
840 printk(KERN_DEBUG "tms380tr: unexpected Timer IRQ\n");
841 break;
842
843 case STS_IRQ_RECEIVE_PENDING:
844 printk(KERN_DEBUG "tms380tr: unexpected Receive Pending IRQ\n");
845 break;
846
847 default:
848 printk(KERN_INFO "Unknown Token Ring IRQ (0x%04x)\n", irq_type);
849 break;
850 }
851
852 /* Reset system interrupt if not already done. */
853 if(irq_type != STS_IRQ_TRANSMIT_STATUS
854 && irq_type != STS_IRQ_RECEIVE_STATUS) {
855 tms380tr_reset_interrupt(dev);
856 }
857
858 irq_type = SIFREADW(SIFSTS);
859 }
860
861 return;
862 }
863
864 /*
865 * Reset the INTERRUPT SYSTEM bit and issue SSB CLEAR command.
866 */
tms380tr_reset_interrupt(struct net_device * dev)867 static void tms380tr_reset_interrupt(struct net_device *dev)
868 {
869 struct net_local *tp = (struct net_local *)dev->priv;
870 SSB *ssb = &tp->ssb;
871
872 /*
873 * [Workaround for "Data Late"]
874 * Set all fields of the SSB to well-defined values so we can
875 * check if the adapter has written the SSB.
876 */
877
878 ssb->STS = (unsigned short) -1;
879 ssb->Parm[0] = (unsigned short) -1;
880 ssb->Parm[1] = (unsigned short) -1;
881 ssb->Parm[2] = (unsigned short) -1;
882
883 /* Free SSB by issuing SSB_CLEAR command after reading IRQ code
884 * and clear STS_SYSTEM_IRQ bit: enable adapter for further interrupts.
885 */
886 tms380tr_exec_sifcmd(dev, CMD_SSB_CLEAR | CMD_CLEAR_SYSTEM_IRQ);
887
888 return;
889 }
890
891 /*
892 * Check if the SSB has actually been written by the adapter.
893 */
tms380tr_chk_ssb(struct net_local * tp,unsigned short IrqType)894 static unsigned char tms380tr_chk_ssb(struct net_local *tp, unsigned short IrqType)
895 {
896 SSB *ssb = &tp->ssb; /* The address of the SSB. */
897
898 /* C 0 1 2 INTERRUPT CODE
899 * - - - - --------------
900 * 1 1 1 1 TRANSMIT STATUS
901 * 1 1 1 1 RECEIVE STATUS
902 * 1 ? ? 0 COMMAND STATUS
903 * 0 0 0 0 SCB CLEAR
904 * 1 1 0 0 RING STATUS
905 * 0 0 0 0 ADAPTER CHECK
906 *
907 * 0 = SSB field not affected by interrupt
908 * 1 = SSB field is affected by interrupt
909 *
910 * C = SSB ADDRESS +0: COMMAND
911 * 0 = SSB ADDRESS +2: STATUS 0
912 * 1 = SSB ADDRESS +4: STATUS 1
913 * 2 = SSB ADDRESS +6: STATUS 2
914 */
915
916 /* Check if this interrupt does use the SSB. */
917
918 if(IrqType != STS_IRQ_TRANSMIT_STATUS
919 && IrqType != STS_IRQ_RECEIVE_STATUS
920 && IrqType != STS_IRQ_COMMAND_STATUS
921 && IrqType != STS_IRQ_RING_STATUS)
922 {
923 return (1); /* SSB not involved. */
924 }
925
926 /* Note: All fields of the SSB have been set to all ones (-1) after it
927 * has last been used by the software (see DriverIsr()).
928 *
929 * Check if the affected SSB fields are still unchanged.
930 */
931
932 if(ssb->STS == (unsigned short) -1)
933 return (0); /* Command field not yet available. */
934 if(IrqType == STS_IRQ_COMMAND_STATUS)
935 return (1); /* Status fields not always affected. */
936 if(ssb->Parm[0] == (unsigned short) -1)
937 return (0); /* Status 1 field not yet available. */
938 if(IrqType == STS_IRQ_RING_STATUS)
939 return (1); /* Status 2 & 3 fields not affected. */
940
941 /* Note: At this point, the interrupt is either TRANSMIT or RECEIVE. */
942 if(ssb->Parm[1] == (unsigned short) -1)
943 return (0); /* Status 2 field not yet available. */
944 if(ssb->Parm[2] == (unsigned short) -1)
945 return (0); /* Status 3 field not yet available. */
946
947 return (1); /* All SSB fields have been written by the adapter. */
948 }
949
950 /*
951 * Evaluates the command results status in the SSB status field.
952 */
tms380tr_cmd_status_irq(struct net_device * dev)953 static void tms380tr_cmd_status_irq(struct net_device *dev)
954 {
955 struct net_local *tp = (struct net_local *)dev->priv;
956 unsigned short ssb_cmd, ssb_parm_0;
957 unsigned short ssb_parm_1;
958 char *open_err = "Open error -";
959 char *code_err = "Open code -";
960
961 /* Copy the ssb values to local variables */
962 ssb_cmd = tp->ssb.STS;
963 ssb_parm_0 = tp->ssb.Parm[0];
964 ssb_parm_1 = tp->ssb.Parm[1];
965
966 if(ssb_cmd == OPEN)
967 {
968 tp->Sleeping = 0;
969 if(!tp->ReOpenInProgress)
970 wake_up_interruptible(&tp->wait_for_tok_int);
971
972 tp->OpenCommandIssued = 0;
973 tp->ScbInUse = 0;
974
975 if((ssb_parm_0 & 0x00FF) == GOOD_COMPLETION)
976 {
977 /* Success, the adapter is open. */
978 tp->LobeWireFaultLogged = 0;
979 tp->AdapterOpenFlag = 1;
980 tp->AdapterVirtOpenFlag = 1;
981 tp->TransmitCommandActive = 0;
982 tms380tr_exec_cmd(dev, OC_TRANSMIT);
983 tms380tr_exec_cmd(dev, OC_RECEIVE);
984
985 if(tp->ReOpenInProgress)
986 tp->ReOpenInProgress = 0;
987
988 return;
989 }
990 else /* The adapter did not open. */
991 {
992 if(ssb_parm_0 & NODE_ADDR_ERROR)
993 printk(KERN_INFO "%s: Node address error\n",
994 dev->name);
995 if(ssb_parm_0 & LIST_SIZE_ERROR)
996 printk(KERN_INFO "%s: List size error\n",
997 dev->name);
998 if(ssb_parm_0 & BUF_SIZE_ERROR)
999 printk(KERN_INFO "%s: Buffer size error\n",
1000 dev->name);
1001 if(ssb_parm_0 & TX_BUF_COUNT_ERROR)
1002 printk(KERN_INFO "%s: Tx buffer count error\n",
1003 dev->name);
1004 if(ssb_parm_0 & INVALID_OPEN_OPTION)
1005 printk(KERN_INFO "%s: Invalid open option\n",
1006 dev->name);
1007 if(ssb_parm_0 & OPEN_ERROR)
1008 {
1009 /* Show the open phase. */
1010 switch(ssb_parm_0 & OPEN_PHASES_MASK)
1011 {
1012 case LOBE_MEDIA_TEST:
1013 if(!tp->LobeWireFaultLogged)
1014 {
1015 tp->LobeWireFaultLogged = 1;
1016 printk(KERN_INFO "%s: %s Lobe wire fault (check cable !).\n", dev->name, open_err);
1017 }
1018 tp->ReOpenInProgress = 1;
1019 tp->AdapterOpenFlag = 0;
1020 tp->AdapterVirtOpenFlag = 1;
1021 tms380tr_open_adapter(dev);
1022 return;
1023
1024 case PHYSICAL_INSERTION:
1025 printk(KERN_INFO "%s: %s Physical insertion.\n", dev->name, open_err);
1026 break;
1027
1028 case ADDRESS_VERIFICATION:
1029 printk(KERN_INFO "%s: %s Address verification.\n", dev->name, open_err);
1030 break;
1031
1032 case PARTICIPATION_IN_RING_POLL:
1033 printk(KERN_INFO "%s: %s Participation in ring poll.\n", dev->name, open_err);
1034 break;
1035
1036 case REQUEST_INITIALISATION:
1037 printk(KERN_INFO "%s: %s Request initialisation.\n", dev->name, open_err);
1038 break;
1039
1040 case FULLDUPLEX_CHECK:
1041 printk(KERN_INFO "%s: %s Full duplex check.\n", dev->name, open_err);
1042 break;
1043
1044 default:
1045 printk(KERN_INFO "%s: %s Unknown open phase\n", dev->name, open_err);
1046 break;
1047 }
1048
1049 /* Show the open errors. */
1050 switch(ssb_parm_0 & OPEN_ERROR_CODES_MASK)
1051 {
1052 case OPEN_FUNCTION_FAILURE:
1053 printk(KERN_INFO "%s: %s OPEN_FUNCTION_FAILURE", dev->name, code_err);
1054 tp->LastOpenStatus =
1055 OPEN_FUNCTION_FAILURE;
1056 break;
1057
1058 case OPEN_SIGNAL_LOSS:
1059 printk(KERN_INFO "%s: %s OPEN_SIGNAL_LOSS\n", dev->name, code_err);
1060 tp->LastOpenStatus =
1061 OPEN_SIGNAL_LOSS;
1062 break;
1063
1064 case OPEN_TIMEOUT:
1065 printk(KERN_INFO "%s: %s OPEN_TIMEOUT\n", dev->name, code_err);
1066 tp->LastOpenStatus =
1067 OPEN_TIMEOUT;
1068 break;
1069
1070 case OPEN_RING_FAILURE:
1071 printk(KERN_INFO "%s: %s OPEN_RING_FAILURE\n", dev->name, code_err);
1072 tp->LastOpenStatus =
1073 OPEN_RING_FAILURE;
1074 break;
1075
1076 case OPEN_RING_BEACONING:
1077 printk(KERN_INFO "%s: %s OPEN_RING_BEACONING\n", dev->name, code_err);
1078 tp->LastOpenStatus =
1079 OPEN_RING_BEACONING;
1080 break;
1081
1082 case OPEN_DUPLICATE_NODEADDR:
1083 printk(KERN_INFO "%s: %s OPEN_DUPLICATE_NODEADDR\n", dev->name, code_err);
1084 tp->LastOpenStatus =
1085 OPEN_DUPLICATE_NODEADDR;
1086 break;
1087
1088 case OPEN_REQUEST_INIT:
1089 printk(KERN_INFO "%s: %s OPEN_REQUEST_INIT\n", dev->name, code_err);
1090 tp->LastOpenStatus =
1091 OPEN_REQUEST_INIT;
1092 break;
1093
1094 case OPEN_REMOVE_RECEIVED:
1095 printk(KERN_INFO "%s: %s OPEN_REMOVE_RECEIVED", dev->name, code_err);
1096 tp->LastOpenStatus =
1097 OPEN_REMOVE_RECEIVED;
1098 break;
1099
1100 case OPEN_FULLDUPLEX_SET:
1101 printk(KERN_INFO "%s: %s OPEN_FULLDUPLEX_SET\n", dev->name, code_err);
1102 tp->LastOpenStatus =
1103 OPEN_FULLDUPLEX_SET;
1104 break;
1105
1106 default:
1107 printk(KERN_INFO "%s: %s Unknown open err code", dev->name, code_err);
1108 tp->LastOpenStatus =
1109 OPEN_FUNCTION_FAILURE;
1110 break;
1111 }
1112 }
1113
1114 tp->AdapterOpenFlag = 0;
1115 tp->AdapterVirtOpenFlag = 0;
1116
1117 return;
1118 }
1119 }
1120 else
1121 {
1122 if(ssb_cmd != READ_ERROR_LOG)
1123 return;
1124
1125 /* Add values from the error log table to the MAC
1126 * statistics counters and update the errorlogtable
1127 * memory.
1128 */
1129 tp->MacStat.line_errors += tp->errorlogtable.Line_Error;
1130 tp->MacStat.burst_errors += tp->errorlogtable.Burst_Error;
1131 tp->MacStat.A_C_errors += tp->errorlogtable.ARI_FCI_Error;
1132 tp->MacStat.lost_frames += tp->errorlogtable.Lost_Frame_Error;
1133 tp->MacStat.recv_congest_count += tp->errorlogtable.Rx_Congest_Error;
1134 tp->MacStat.rx_errors += tp->errorlogtable.Rx_Congest_Error;
1135 tp->MacStat.frame_copied_errors += tp->errorlogtable.Frame_Copied_Error;
1136 tp->MacStat.token_errors += tp->errorlogtable.Token_Error;
1137 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Bus_Error;
1138 tp->MacStat.dummy1 += tp->errorlogtable.DMA_Parity_Error;
1139 tp->MacStat.abort_delimiters += tp->errorlogtable.AbortDelimeters;
1140 tp->MacStat.frequency_errors += tp->errorlogtable.Frequency_Error;
1141 tp->MacStat.internal_errors += tp->errorlogtable.Internal_Error;
1142 }
1143
1144 return;
1145 }
1146
1147 /*
1148 * The inverse routine to tms380tr_open().
1149 */
tms380tr_close(struct net_device * dev)1150 int tms380tr_close(struct net_device *dev)
1151 {
1152 struct net_local *tp = (struct net_local *)dev->priv;
1153 netif_stop_queue(dev);
1154
1155 del_timer(&tp->timer);
1156
1157 /* Flush the Tx and disable Rx here. */
1158
1159 tp->HaltInProgress = 1;
1160 tms380tr_exec_cmd(dev, OC_CLOSE);
1161 tp->timer.expires = jiffies + 1*HZ;
1162 tp->timer.function = tms380tr_timer_end_wait;
1163 tp->timer.data = (unsigned long)dev;
1164 add_timer(&tp->timer);
1165
1166 tms380tr_enable_interrupts(dev);
1167
1168 tp->Sleeping = 1;
1169 interruptible_sleep_on(&tp->wait_for_tok_int);
1170 tp->TransmitCommandActive = 0;
1171
1172 del_timer(&tp->timer);
1173 tms380tr_disable_interrupts(dev);
1174
1175 if(dev->dma > 0)
1176 {
1177 unsigned long flags=claim_dma_lock();
1178 disable_dma(dev->dma);
1179 release_dma_lock(flags);
1180 }
1181
1182 SIFWRITEW(0xFF00, SIFCMD);
1183 #if 0
1184 if(dev->dma > 0) /* what the? */
1185 SIFWRITEB(0xff, POSREG);
1186 #endif
1187 tms380tr_cancel_tx_queue(tp);
1188
1189 return (0);
1190 }
1191
1192 /*
1193 * Get the current statistics. This may be called with the card open
1194 * or closed.
1195 */
tms380tr_get_stats(struct net_device * dev)1196 static struct net_device_stats *tms380tr_get_stats(struct net_device *dev)
1197 {
1198 struct net_local *tp = (struct net_local *)dev->priv;
1199
1200 return ((struct net_device_stats *)&tp->MacStat);
1201 }
1202
1203 /*
1204 * Set or clear the multicast filter for this adapter.
1205 */
tms380tr_set_multicast_list(struct net_device * dev)1206 static void tms380tr_set_multicast_list(struct net_device *dev)
1207 {
1208 struct net_local *tp = (struct net_local *)dev->priv;
1209 unsigned int OpenOptions;
1210
1211 OpenOptions = tp->ocpl.OPENOptions &
1212 ~(PASS_ADAPTER_MAC_FRAMES
1213 | PASS_ATTENTION_FRAMES
1214 | PASS_BEACON_MAC_FRAMES
1215 | COPY_ALL_MAC_FRAMES
1216 | COPY_ALL_NON_MAC_FRAMES);
1217
1218 tp->ocpl.FunctAddr = 0;
1219
1220 if(dev->flags & IFF_PROMISC)
1221 /* Enable promiscuous mode */
1222 OpenOptions |= COPY_ALL_NON_MAC_FRAMES |
1223 COPY_ALL_MAC_FRAMES;
1224 else
1225 {
1226 if(dev->flags & IFF_ALLMULTI)
1227 {
1228 /* Disable promiscuous mode, use normal mode. */
1229 tp->ocpl.FunctAddr = 0xFFFFFFFF;
1230 }
1231 else
1232 {
1233 int i;
1234 struct dev_mc_list *mclist = dev->mc_list;
1235 for (i=0; i< dev->mc_count; i++)
1236 {
1237 ((char *)(&tp->ocpl.FunctAddr))[0] |=
1238 mclist->dmi_addr[2];
1239 ((char *)(&tp->ocpl.FunctAddr))[1] |=
1240 mclist->dmi_addr[3];
1241 ((char *)(&tp->ocpl.FunctAddr))[2] |=
1242 mclist->dmi_addr[4];
1243 ((char *)(&tp->ocpl.FunctAddr))[3] |=
1244 mclist->dmi_addr[5];
1245 mclist = mclist->next;
1246 }
1247 }
1248 tms380tr_exec_cmd(dev, OC_SET_FUNCT_ADDR);
1249 }
1250
1251 tp->ocpl.OPENOptions = OpenOptions;
1252 tms380tr_exec_cmd(dev, OC_MODIFY_OPEN_PARMS);
1253 return;
1254 }
1255
1256 /*
1257 * Wait for some time (microseconds)
1258 */
tms380tr_wait(unsigned long time)1259 void tms380tr_wait(unsigned long time)
1260 {
1261 #if 0
1262 long tmp;
1263
1264 tmp = jiffies + time/(1000000/HZ);
1265 do {
1266 current->state = TASK_INTERRUPTIBLE;
1267 tmp = schedule_timeout(tmp);
1268 } while(time_after(tmp, jiffies));
1269 #else
1270 udelay(time);
1271 #endif
1272 return;
1273 }
1274
1275 /*
1276 * Write a command value to the SIFCMD register
1277 */
tms380tr_exec_sifcmd(struct net_device * dev,unsigned int WriteValue)1278 static void tms380tr_exec_sifcmd(struct net_device *dev, unsigned int WriteValue)
1279 {
1280 unsigned short cmd;
1281 unsigned short SifStsValue;
1282 unsigned long loop_counter;
1283
1284 WriteValue = ((WriteValue ^ CMD_SYSTEM_IRQ) | CMD_INTERRUPT_ADAPTER);
1285 cmd = (unsigned short)WriteValue;
1286 loop_counter = 0,5 * 800000;
1287 do {
1288 SifStsValue = SIFREADW(SIFSTS);
1289 } while((SifStsValue & CMD_INTERRUPT_ADAPTER) && loop_counter--);
1290 SIFWRITEW(cmd, SIFCMD);
1291
1292 return;
1293 }
1294
1295 /*
1296 * Processes adapter hardware reset, halts adapter and downloads firmware,
1297 * clears the halt bit.
1298 */
tms380tr_reset_adapter(struct net_device * dev)1299 static int tms380tr_reset_adapter(struct net_device *dev)
1300 {
1301 struct net_local *tp = (struct net_local *)dev->priv;
1302 unsigned short *fw_ptr = (unsigned short *)&tms380tr_code;
1303 unsigned short count, c;
1304
1305 /* Hardware adapter reset */
1306 SIFWRITEW(ACL_ARESET, SIFACL);
1307 tms380tr_wait(40);
1308
1309 c = SIFREADW(SIFACL);
1310 tms380tr_wait(20);
1311
1312 if(dev->dma == 0) /* For PCI adapters */
1313 {
1314 c &= ~(ACL_NSELOUT0 | ACL_NSELOUT1); /* Clear bits */
1315 if(tp->setnselout)
1316 c |= (*tp->setnselout)(dev);
1317 }
1318
1319 /* In case a command is pending - forget it */
1320 tp->ScbInUse = 0;
1321
1322 c &= ~ACL_ARESET; /* Clear adapter reset bit */
1323 c |= ACL_CPHALT; /* Halt adapter CPU, allow download */
1324 c |= ACL_BOOT;
1325 c |= ACL_SINTEN;
1326 c &= ~ACL_PSDMAEN; /* Clear pseudo dma bit */
1327 SIFWRITEW(c, SIFACL);
1328 tms380tr_wait(40);
1329
1330 /* Download firmware via DIO interface: */
1331 do {
1332 /* Download first address part */
1333 SIFWRITEW(*fw_ptr, SIFADX);
1334 fw_ptr++;
1335
1336 /* Download second address part */
1337 SIFWRITEW(*fw_ptr, SIFADD);
1338 fw_ptr++;
1339
1340 if((count = *fw_ptr) != 0) /* Load loop counter */
1341 {
1342 fw_ptr++; /* Download block data */
1343 for(; count > 0; count--)
1344 {
1345 SIFWRITEW(*fw_ptr, SIFINC);
1346 fw_ptr++;
1347 }
1348 }
1349 else /* Stop, if last block downloaded */
1350 {
1351 c = SIFREADW(SIFACL);
1352 c &= (~ACL_CPHALT | ACL_SINTEN);
1353
1354 /* Clear CPHALT and start BUD */
1355 SIFWRITEW(c, SIFACL);
1356 return (1);
1357 }
1358 } while(count == 0);
1359
1360 printk(KERN_INFO "%s: Adapter Download Failed\n", dev->name);
1361 return (-1);
1362 }
1363
1364 /*
1365 * Starts bring up diagnostics of token ring adapter and evaluates
1366 * diagnostic results.
1367 */
tms380tr_bringup_diags(struct net_device * dev)1368 static int tms380tr_bringup_diags(struct net_device *dev)
1369 {
1370 int loop_cnt, retry_cnt;
1371 unsigned short Status;
1372
1373 tms380tr_wait(HALF_SECOND);
1374 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1375 tms380tr_wait(HALF_SECOND);
1376
1377 retry_cnt = BUD_MAX_RETRIES; /* maximal number of retrys */
1378
1379 do {
1380 retry_cnt--;
1381 if(tms380tr_debug > 3)
1382 printk(KERN_INFO "BUD-Status: ");
1383 loop_cnt = BUD_MAX_LOOPCNT; /* maximum: three seconds*/
1384 do { /* Inspect BUD results */
1385 loop_cnt--;
1386 tms380tr_wait(HALF_SECOND);
1387 Status = SIFREADW(SIFSTS);
1388 Status &= STS_MASK;
1389
1390 if(tms380tr_debug > 3)
1391 printk(KERN_INFO " %04X \n", Status);
1392 /* BUD successfully completed */
1393 if(Status == STS_INITIALIZE)
1394 return (1);
1395 /* Unrecoverable hardware error, BUD not completed? */
1396 } while((loop_cnt > 0) && ((Status & (STS_ERROR | STS_TEST))
1397 != (STS_ERROR | STS_TEST)));
1398
1399 /* Error preventing completion of BUD */
1400 if(retry_cnt > 0)
1401 {
1402 printk(KERN_INFO "%s: Adapter Software Reset.\n",
1403 dev->name);
1404 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1405 tms380tr_wait(HALF_SECOND);
1406 }
1407 } while(retry_cnt > 0);
1408
1409 Status = SIFREADW(SIFSTS);
1410
1411 printk(KERN_INFO "%s: Hardware error\n", dev->name);
1412 /* Hardware error occurred! */
1413 Status &= 0x001f;
1414 if (Status & 0x0010)
1415 printk(KERN_INFO "%s: BUD Error: Timeout\n", dev->name);
1416 else if ((Status & 0x000f) > 6)
1417 printk(KERN_INFO "%s: BUD Error: Illegal Failure\n", dev->name);
1418 else
1419 printk(KERN_INFO "%s: Bring Up Diagnostics Error (%04X) occurred\n", dev->name, Status & 0x000f);
1420
1421 return (-1);
1422 }
1423
1424 /*
1425 * Copy initialisation data to adapter memory, beginning at address
1426 * 1:0A00; Starting DMA test and evaluating result bits.
1427 */
tms380tr_init_adapter(struct net_device * dev)1428 static int tms380tr_init_adapter(struct net_device *dev)
1429 {
1430 struct net_local *tp = (struct net_local *)dev->priv;
1431
1432 const unsigned char SCB_Test[6] = {0x00, 0x00, 0xC1, 0xE2, 0xD4, 0x8B};
1433 const unsigned char SSB_Test[8] = {0xFF, 0xFF, 0xD1, 0xD7,
1434 0xC5, 0xD9, 0xC3, 0xD4};
1435 void *ptr = (void *)&tp->ipb;
1436 unsigned short *ipb_ptr = (unsigned short *)ptr;
1437 unsigned char *cb_ptr = (unsigned char *) &tp->scb;
1438 unsigned char *sb_ptr = (unsigned char *) &tp->ssb;
1439 unsigned short Status;
1440 int i, loop_cnt, retry_cnt;
1441
1442 /* Normalize: byte order low/high, word order high/low! (only IPB!) */
1443 tp->ipb.SCB_Addr = SWAPW(((char *)&tp->scb - (char *)tp) + tp->dmabuffer);
1444 tp->ipb.SSB_Addr = SWAPW(((char *)&tp->ssb - (char *)tp) + tp->dmabuffer);
1445
1446 if(tms380tr_debug > 3)
1447 {
1448 printk(KERN_INFO "%s: buffer (real): %lx\n", dev->name, (long) &tp->scb);
1449 printk(KERN_INFO "%s: buffer (virt): %lx\n", dev->name, (long) ((char *)&tp->scb - (char *)tp) + tp->dmabuffer);
1450 printk(KERN_INFO "%s: buffer (DMA) : %lx\n", dev->name, (long) tp->dmabuffer);
1451 printk(KERN_INFO "%s: buffer (tp) : %lx\n", dev->name, (long) tp);
1452 }
1453 /* Maximum: three initialization retries */
1454 retry_cnt = INIT_MAX_RETRIES;
1455
1456 do {
1457 retry_cnt--;
1458
1459 /* Transfer initialization block */
1460 SIFWRITEW(0x0001, SIFADX);
1461
1462 /* To address 0001:0A00 of adapter RAM */
1463 SIFWRITEW(0x0A00, SIFADD);
1464
1465 /* Write 11 words to adapter RAM */
1466 for(i = 0; i < 11; i++)
1467 SIFWRITEW(ipb_ptr[i], SIFINC);
1468
1469 /* Execute SCB adapter command */
1470 tms380tr_exec_sifcmd(dev, CMD_EXECUTE);
1471
1472 loop_cnt = INIT_MAX_LOOPCNT; /* Maximum: 11 seconds */
1473
1474 /* While remaining retries, no error and not completed */
1475 do {
1476 Status = 0;
1477 loop_cnt--;
1478 tms380tr_wait(HALF_SECOND);
1479
1480 /* Mask interesting status bits */
1481 Status = SIFREADW(SIFSTS);
1482 Status &= STS_MASK;
1483 } while(((Status &(STS_INITIALIZE | STS_ERROR | STS_TEST)) != 0)
1484 && ((Status & STS_ERROR) == 0) && (loop_cnt != 0));
1485
1486 if((Status & (STS_INITIALIZE | STS_ERROR | STS_TEST)) == 0)
1487 {
1488 /* Initialization completed without error */
1489 i = 0;
1490 do { /* Test if contents of SCB is valid */
1491 if(SCB_Test[i] != *(cb_ptr + i))
1492 {
1493 printk(KERN_INFO "%s: DMA failed\n", dev->name);
1494 /* DMA data error: wrong data in SCB */
1495 return (-1);
1496 }
1497 i++;
1498 } while(i < 6);
1499
1500 i = 0;
1501 do { /* Test if contents of SSB is valid */
1502 if(SSB_Test[i] != *(sb_ptr + i))
1503 /* DMA data error: wrong data in SSB */
1504 return (-1);
1505 i++;
1506 } while (i < 8);
1507
1508 return (1); /* Adapter successfully initialized */
1509 }
1510 else
1511 {
1512 if((Status & STS_ERROR) != 0)
1513 {
1514 /* Initialization error occurred */
1515 Status = SIFREADW(SIFSTS);
1516 Status &= STS_ERROR_MASK;
1517 /* ShowInitialisationErrorCode(Status); */
1518 printk(KERN_INFO "%s: Status error: %d\n", dev->name, Status);
1519 return (-1); /* Unrecoverable error */
1520 }
1521 else
1522 {
1523 if(retry_cnt > 0)
1524 {
1525 /* Reset adapter and try init again */
1526 tms380tr_exec_sifcmd(dev, EXEC_SOFT_RESET);
1527 tms380tr_wait(HALF_SECOND);
1528 }
1529 }
1530 }
1531 } while(retry_cnt > 0);
1532
1533 printk(KERN_INFO "%s: Retry exceeded\n", dev->name);
1534 return (-1);
1535 }
1536
1537 /*
1538 * Check for outstanding commands in command queue and tries to execute
1539 * command immediately. Corresponding command flag in command queue is cleared.
1540 */
tms380tr_chk_outstanding_cmds(struct net_device * dev)1541 static void tms380tr_chk_outstanding_cmds(struct net_device *dev)
1542 {
1543 struct net_local *tp = (struct net_local *)dev->priv;
1544 unsigned long Addr = 0;
1545
1546 if(tp->CMDqueue == 0)
1547 return; /* No command execution */
1548
1549 /* If SCB in use: no command */
1550 if(tp->ScbInUse == 1)
1551 return;
1552
1553 /* Check if adapter is opened, avoiding COMMAND_REJECT
1554 * interrupt by the adapter!
1555 */
1556 if(tp->AdapterOpenFlag == 0)
1557 {
1558 if(tp->CMDqueue & OC_OPEN)
1559 {
1560 /* Execute OPEN command */
1561 tp->CMDqueue ^= OC_OPEN;
1562
1563 Addr = htonl(((char *)&tp->ocpl - (char *)tp) + tp->dmabuffer);
1564 tp->scb.Parm[0] = LOWORD(Addr);
1565 tp->scb.Parm[1] = HIWORD(Addr);
1566 tp->scb.CMD = OPEN;
1567 }
1568 else
1569 /* No OPEN command queued, but adapter closed. Note:
1570 * We'll try to re-open the adapter in DriverPoll()
1571 */
1572 return; /* No adapter command issued */
1573 }
1574 else
1575 {
1576 /* Adapter is open; evaluate command queue: try to execute
1577 * outstanding commands (depending on priority!) CLOSE
1578 * command queued
1579 */
1580 if(tp->CMDqueue & OC_CLOSE)
1581 {
1582 tp->CMDqueue ^= OC_CLOSE;
1583 tp->AdapterOpenFlag = 0;
1584 tp->scb.Parm[0] = 0; /* Parm[0], Parm[1] are ignored */
1585 tp->scb.Parm[1] = 0; /* but should be set to zero! */
1586 tp->scb.CMD = CLOSE;
1587 if(!tp->HaltInProgress)
1588 tp->CMDqueue |= OC_OPEN; /* re-open adapter */
1589 else
1590 tp->CMDqueue = 0; /* no more commands */
1591 }
1592 else
1593 {
1594 if(tp->CMDqueue & OC_RECEIVE)
1595 {
1596 tp->CMDqueue ^= OC_RECEIVE;
1597 Addr = htonl(((char *)tp->RplHead - (char *)tp) + tp->dmabuffer);
1598 tp->scb.Parm[0] = LOWORD(Addr);
1599 tp->scb.Parm[1] = HIWORD(Addr);
1600 tp->scb.CMD = RECEIVE;
1601 }
1602 else
1603 {
1604 if(tp->CMDqueue & OC_TRANSMIT_HALT)
1605 {
1606 /* NOTE: TRANSMIT.HALT must be checked
1607 * before TRANSMIT.
1608 */
1609 tp->CMDqueue ^= OC_TRANSMIT_HALT;
1610 tp->scb.CMD = TRANSMIT_HALT;
1611
1612 /* Parm[0] and Parm[1] are ignored
1613 * but should be set to zero!
1614 */
1615 tp->scb.Parm[0] = 0;
1616 tp->scb.Parm[1] = 0;
1617 }
1618 else
1619 {
1620 if(tp->CMDqueue & OC_TRANSMIT)
1621 {
1622 /* NOTE: TRANSMIT must be
1623 * checked after TRANSMIT.HALT
1624 */
1625 if(tp->TransmitCommandActive)
1626 {
1627 if(!tp->TransmitHaltScheduled)
1628 {
1629 tp->TransmitHaltScheduled = 1;
1630 tms380tr_exec_cmd(dev, OC_TRANSMIT_HALT) ;
1631 }
1632 tp->TransmitCommandActive = 0;
1633 return;
1634 }
1635
1636 tp->CMDqueue ^= OC_TRANSMIT;
1637 tms380tr_cancel_tx_queue(tp);
1638 Addr = htonl(((char *)tp->TplBusy - (char *)tp) + tp->dmabuffer);
1639 tp->scb.Parm[0] = LOWORD(Addr);
1640 tp->scb.Parm[1] = HIWORD(Addr);
1641 tp->scb.CMD = TRANSMIT;
1642 tp->TransmitCommandActive = 1;
1643 }
1644 else
1645 {
1646 if(tp->CMDqueue & OC_MODIFY_OPEN_PARMS)
1647 {
1648 tp->CMDqueue ^= OC_MODIFY_OPEN_PARMS;
1649 tp->scb.Parm[0] = tp->ocpl.OPENOptions; /* new OPEN options*/
1650 tp->scb.Parm[0] |= ENABLE_FULL_DUPLEX_SELECTION;
1651 tp->scb.Parm[1] = 0; /* is ignored but should be zero */
1652 tp->scb.CMD = MODIFY_OPEN_PARMS;
1653 }
1654 else
1655 {
1656 if(tp->CMDqueue & OC_SET_FUNCT_ADDR)
1657 {
1658 tp->CMDqueue ^= OC_SET_FUNCT_ADDR;
1659 tp->scb.Parm[0] = LOWORD(tp->ocpl.FunctAddr);
1660 tp->scb.Parm[1] = HIWORD(tp->ocpl.FunctAddr);
1661 tp->scb.CMD = SET_FUNCT_ADDR;
1662 }
1663 else
1664 {
1665 if(tp->CMDqueue & OC_SET_GROUP_ADDR)
1666 {
1667 tp->CMDqueue ^= OC_SET_GROUP_ADDR;
1668 tp->scb.Parm[0] = LOWORD(tp->ocpl.GroupAddr);
1669 tp->scb.Parm[1] = HIWORD(tp->ocpl.GroupAddr);
1670 tp->scb.CMD = SET_GROUP_ADDR;
1671 }
1672 else
1673 {
1674 if(tp->CMDqueue & OC_READ_ERROR_LOG)
1675 {
1676 tp->CMDqueue ^= OC_READ_ERROR_LOG;
1677 Addr = htonl(((char *)&tp->errorlogtable - (char *)tp) + tp->dmabuffer);
1678 tp->scb.Parm[0] = LOWORD(Addr);
1679 tp->scb.Parm[1] = HIWORD(Addr);
1680 tp->scb.CMD = READ_ERROR_LOG;
1681 }
1682 else
1683 {
1684 printk(KERN_WARNING "CheckForOutstandingCommand: unknown Command\n");
1685 tp->CMDqueue = 0;
1686 return;
1687 }
1688 }
1689 }
1690 }
1691 }
1692 }
1693 }
1694 }
1695 }
1696
1697 tp->ScbInUse = 1; /* Set semaphore: SCB in use. */
1698
1699 /* Execute SCB and generate IRQ when done. */
1700 tms380tr_exec_sifcmd(dev, CMD_EXECUTE | CMD_SCB_REQUEST);
1701
1702 return;
1703 }
1704
1705 /*
1706 * IRQ conditions: signal loss on the ring, transmit or receive of beacon
1707 * frames (disabled if bit 1 of OPEN option is set); report error MAC
1708 * frame transmit (disabled if bit 2 of OPEN option is set); open or short
1709 * circuit fault on the lobe is detected; remove MAC frame received;
1710 * error counter overflow (255); opened adapter is the only station in ring.
1711 * After some of the IRQs the adapter is closed!
1712 */
tms380tr_ring_status_irq(struct net_device * dev)1713 static void tms380tr_ring_status_irq(struct net_device *dev)
1714 {
1715 struct net_local *tp = (struct net_local *)dev->priv;
1716
1717 tp->CurrentRingStatus = be16_to_cpu((unsigned short)tp->ssb.Parm[0]);
1718
1719 /* First: fill up statistics */
1720 if(tp->ssb.Parm[0] & SIGNAL_LOSS)
1721 {
1722 printk(KERN_INFO "%s: Signal Loss\n", dev->name);
1723 tp->MacStat.line_errors++;
1724 }
1725
1726 /* Adapter is closed, but initialized */
1727 if(tp->ssb.Parm[0] & LOBE_WIRE_FAULT)
1728 {
1729 printk(KERN_INFO "%s: Lobe Wire Fault, Reopen Adapter\n",
1730 dev->name);
1731 tp->MacStat.line_errors++;
1732 }
1733
1734 if(tp->ssb.Parm[0] & RING_RECOVERY)
1735 printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
1736
1737 /* Counter overflow: read error log */
1738 if(tp->ssb.Parm[0] & COUNTER_OVERFLOW)
1739 {
1740 printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
1741 tms380tr_exec_cmd(dev, OC_READ_ERROR_LOG);
1742 }
1743
1744 /* Adapter is closed, but initialized */
1745 if(tp->ssb.Parm[0] & REMOVE_RECEIVED)
1746 printk(KERN_INFO "%s: Remove Received, Reopen Adapter\n",
1747 dev->name);
1748
1749 /* Adapter is closed, but initialized */
1750 if(tp->ssb.Parm[0] & AUTO_REMOVAL_ERROR)
1751 printk(KERN_INFO "%s: Auto Removal Error, Reopen Adapter\n",
1752 dev->name);
1753
1754 if(tp->ssb.Parm[0] & HARD_ERROR)
1755 printk(KERN_INFO "%s: Hard Error\n", dev->name);
1756
1757 if(tp->ssb.Parm[0] & SOFT_ERROR)
1758 printk(KERN_INFO "%s: Soft Error\n", dev->name);
1759
1760 if(tp->ssb.Parm[0] & TRANSMIT_BEACON)
1761 printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
1762
1763 if(tp->ssb.Parm[0] & SINGLE_STATION)
1764 printk(KERN_INFO "%s: Single Station\n", dev->name);
1765
1766 /* Check if adapter has been closed */
1767 if(tp->ssb.Parm[0] & ADAPTER_CLOSED)
1768 {
1769 printk(KERN_INFO "%s: Adapter closed (Reopening),"
1770 "QueueSkb %d, CurrentRingStat %x\n",
1771 dev->name, tp->QueueSkb, tp->CurrentRingStatus);
1772 tp->AdapterOpenFlag = 0;
1773 tms380tr_open_adapter(dev);
1774 }
1775
1776 return;
1777 }
1778
1779 /*
1780 * Issued if adapter has encountered an unrecoverable hardware
1781 * or software error.
1782 */
tms380tr_chk_irq(struct net_device * dev)1783 static void tms380tr_chk_irq(struct net_device *dev)
1784 {
1785 int i;
1786 unsigned short AdapterCheckBlock[4];
1787 struct net_local *tp = (struct net_local *)dev->priv;
1788
1789 tp->AdapterOpenFlag = 0; /* Adapter closed now */
1790
1791 /* Page number of adapter memory */
1792 SIFWRITEW(0x0001, SIFADX);
1793 /* Address offset */
1794 SIFWRITEW(CHECKADDR, SIFADR);
1795
1796 /* Reading 8 byte adapter check block. */
1797 for(i = 0; i < 4; i++)
1798 AdapterCheckBlock[i] = SIFREADW(SIFINC);
1799
1800 if(tms380tr_debug > 3)
1801 {
1802 printk("%s: AdapterCheckBlock: ", dev->name);
1803 for (i = 0; i < 4; i++)
1804 printk("%04X", AdapterCheckBlock[i]);
1805 printk("\n");
1806 }
1807
1808 switch(AdapterCheckBlock[0])
1809 {
1810 case DIO_PARITY:
1811 printk(KERN_INFO "%s: DIO parity error\n", dev->name);
1812 break;
1813
1814 case DMA_READ_ABORT:
1815 printk(KERN_INFO "%s DMA read operation aborted:\n",
1816 dev->name);
1817 switch (AdapterCheckBlock[1])
1818 {
1819 case 0:
1820 printk(KERN_INFO "Timeout\n");
1821 printk(KERN_INFO "Address: %04X %04X\n",
1822 AdapterCheckBlock[2],
1823 AdapterCheckBlock[3]);
1824 break;
1825
1826 case 1:
1827 printk(KERN_INFO "Parity error\n");
1828 printk(KERN_INFO "Address: %04X %04X\n",
1829 AdapterCheckBlock[2],
1830 AdapterCheckBlock[3]);
1831 break;
1832
1833 case 2:
1834 printk(KERN_INFO "Bus error\n");
1835 printk(KERN_INFO "Address: %04X %04X\n",
1836 AdapterCheckBlock[2],
1837 AdapterCheckBlock[3]);
1838 break;
1839
1840 default:
1841 printk(KERN_INFO "Unknown error.\n");
1842 break;
1843 }
1844 break;
1845
1846 case DMA_WRITE_ABORT:
1847 printk(KERN_INFO "%s: DMA write operation aborted: \n",
1848 dev->name);
1849 switch (AdapterCheckBlock[1])
1850 {
1851 case 0:
1852 printk(KERN_INFO "Timeout\n");
1853 printk(KERN_INFO "Address: %04X %04X\n",
1854 AdapterCheckBlock[2],
1855 AdapterCheckBlock[3]);
1856 break;
1857
1858 case 1:
1859 printk(KERN_INFO "Parity error\n");
1860 printk(KERN_INFO "Address: %04X %04X\n",
1861 AdapterCheckBlock[2],
1862 AdapterCheckBlock[3]);
1863 break;
1864
1865 case 2:
1866 printk(KERN_INFO "Bus error\n");
1867 printk(KERN_INFO "Address: %04X %04X\n",
1868 AdapterCheckBlock[2],
1869 AdapterCheckBlock[3]);
1870 break;
1871
1872 default:
1873 printk(KERN_INFO "Unknown error.\n");
1874 break;
1875 }
1876 break;
1877
1878 case ILLEGAL_OP_CODE:
1879 printk("%s: Illegal operation code in firmware\n",
1880 dev->name);
1881 /* Parm[0-3]: adapter internal register R13-R15 */
1882 break;
1883
1884 case PARITY_ERRORS:
1885 printk("%s: Adapter internal bus parity error\n",
1886 dev->name);
1887 /* Parm[0-3]: adapter internal register R13-R15 */
1888 break;
1889
1890 case RAM_DATA_ERROR:
1891 printk("%s: RAM data error\n", dev->name);
1892 /* Parm[0-1]: MSW/LSW address of RAM location. */
1893 break;
1894
1895 case RAM_PARITY_ERROR:
1896 printk("%s: RAM parity error\n", dev->name);
1897 /* Parm[0-1]: MSW/LSW address of RAM location. */
1898 break;
1899
1900 case RING_UNDERRUN:
1901 printk("%s: Internal DMA underrun detected\n",
1902 dev->name);
1903 break;
1904
1905 case INVALID_IRQ:
1906 printk("%s: Unrecognized interrupt detected\n",
1907 dev->name);
1908 /* Parm[0-3]: adapter internal register R13-R15 */
1909 break;
1910
1911 case INVALID_ERROR_IRQ:
1912 printk("%s: Unrecognized error interrupt detected\n",
1913 dev->name);
1914 /* Parm[0-3]: adapter internal register R13-R15 */
1915 break;
1916
1917 case INVALID_XOP:
1918 printk("%s: Unrecognized XOP request detected\n",
1919 dev->name);
1920 /* Parm[0-3]: adapter internal register R13-R15 */
1921 break;
1922
1923 default:
1924 printk("%s: Unknown status", dev->name);
1925 break;
1926 }
1927
1928 if(tms380tr_chipset_init(dev) == 1)
1929 {
1930 /* Restart of firmware successful */
1931 tp->AdapterOpenFlag = 1;
1932 }
1933
1934 return;
1935 }
1936
1937 /*
1938 * Internal adapter pointer to RAM data are copied from adapter into
1939 * host system.
1940 */
tms380tr_read_ptr(struct net_device * dev)1941 static int tms380tr_read_ptr(struct net_device *dev)
1942 {
1943 struct net_local *tp = (struct net_local *)dev->priv;
1944 unsigned short adapterram;
1945
1946 tms380tr_read_ram(dev, (unsigned char *)&tp->intptrs.BurnedInAddrPtr,
1947 ADAPTER_INT_PTRS, 16);
1948 tms380tr_read_ram(dev, (unsigned char *)&adapterram,
1949 cpu_to_be16((unsigned short)tp->intptrs.AdapterRAMPtr), 2);
1950 return be16_to_cpu(adapterram);
1951 }
1952
1953 /*
1954 * Reads a number of bytes from adapter to system memory.
1955 */
tms380tr_read_ram(struct net_device * dev,unsigned char * Data,unsigned short Address,int Length)1956 static void tms380tr_read_ram(struct net_device *dev, unsigned char *Data,
1957 unsigned short Address, int Length)
1958 {
1959 int i;
1960 unsigned short old_sifadx, old_sifadr, InWord;
1961
1962 /* Save the current values */
1963 old_sifadx = SIFREADW(SIFADX);
1964 old_sifadr = SIFREADW(SIFADR);
1965
1966 /* Page number of adapter memory */
1967 SIFWRITEW(0x0001, SIFADX);
1968 /* Address offset in adapter RAM */
1969 SIFWRITEW(Address, SIFADR);
1970
1971 /* Copy len byte from adapter memory to system data area. */
1972 i = 0;
1973 for(;;)
1974 {
1975 InWord = SIFREADW(SIFINC);
1976
1977 *(Data + i) = HIBYTE(InWord); /* Write first byte */
1978 if(++i == Length) /* All is done break */
1979 break;
1980
1981 *(Data + i) = LOBYTE(InWord); /* Write second byte */
1982 if (++i == Length) /* All is done break */
1983 break;
1984 }
1985
1986 /* Restore original values */
1987 SIFWRITEW(old_sifadx, SIFADX);
1988 SIFWRITEW(old_sifadr, SIFADR);
1989
1990 return;
1991 }
1992
1993 /*
1994 * Cancel all queued packets in the transmission queue.
1995 */
tms380tr_cancel_tx_queue(struct net_local * tp)1996 static void tms380tr_cancel_tx_queue(struct net_local* tp)
1997 {
1998 TPL *tpl;
1999 struct sk_buff *skb;
2000
2001 /*
2002 * NOTE: There must not be an active TRANSMIT command pending, when
2003 * this function is called.
2004 */
2005 if(tp->TransmitCommandActive)
2006 return;
2007
2008 for(;;)
2009 {
2010 tpl = tp->TplBusy;
2011 if(!tpl->BusyFlag)
2012 break;
2013 /* "Remove" TPL from busy list. */
2014 tp->TplBusy = tpl->NextTPLPtr;
2015 tms380tr_write_tpl_status(tpl, 0); /* Clear VALID bit */
2016 tpl->BusyFlag = 0; /* "free" TPL */
2017
2018 printk(KERN_INFO "Cancel tx (%08lXh).\n", (unsigned long)tpl);
2019 if (tpl->DMABuff)
2020 pci_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, PCI_DMA_TODEVICE);
2021 dev_kfree_skb_any(tpl->Skb);
2022 }
2023
2024 for(;;)
2025 {
2026 skb = skb_dequeue(&tp->SendSkbQueue);
2027 if(skb == NULL)
2028 break;
2029 tp->QueueSkb++;
2030 dev_kfree_skb_any(skb);
2031 }
2032
2033 return;
2034 }
2035
2036 /*
2037 * This function is called whenever a transmit interrupt is generated by the
2038 * adapter. For a command complete interrupt, it is checked if we have to
2039 * issue a new transmit command or not.
2040 */
tms380tr_tx_status_irq(struct net_device * dev)2041 static void tms380tr_tx_status_irq(struct net_device *dev)
2042 {
2043 struct net_local *tp = (struct net_local *)dev->priv;
2044 unsigned char HighByte, HighAc, LowAc;
2045 TPL *tpl;
2046
2047 /* NOTE: At this point the SSB from TRANSMIT STATUS is no longer
2048 * available, because the CLEAR SSB command has already been issued.
2049 *
2050 * Process all complete transmissions.
2051 */
2052
2053 for(;;)
2054 {
2055 tpl = tp->TplBusy;
2056 if(!tpl->BusyFlag || (tpl->Status
2057 & (TX_VALID | TX_FRAME_COMPLETE))
2058 != TX_FRAME_COMPLETE)
2059 {
2060 break;
2061 }
2062
2063 /* "Remove" TPL from busy list. */
2064 tp->TplBusy = tpl->NextTPLPtr ;
2065
2066 /* Check the transmit status field only for directed frames*/
2067 if(DIRECTED_FRAME(tpl) && (tpl->Status & TX_ERROR) == 0)
2068 {
2069 HighByte = GET_TRANSMIT_STATUS_HIGH_BYTE(tpl->Status);
2070 HighAc = GET_FRAME_STATUS_HIGH_AC(HighByte);
2071 LowAc = GET_FRAME_STATUS_LOW_AC(HighByte);
2072
2073 if((HighAc != LowAc) || (HighAc == AC_NOT_RECOGNIZED))
2074 {
2075 printk(KERN_INFO "%s: (DA=%08lX not recognized)",
2076 dev->name,
2077 *(unsigned long *)&tpl->MData[2+2]);
2078 }
2079 else
2080 {
2081 if(tms380tr_debug > 3)
2082 printk("%s: Directed frame tx'd\n",
2083 dev->name);
2084 }
2085 }
2086 else
2087 {
2088 if(!DIRECTED_FRAME(tpl))
2089 {
2090 if(tms380tr_debug > 3)
2091 printk("%s: Broadcast frame tx'd\n",
2092 dev->name);
2093 }
2094 }
2095
2096 tp->MacStat.tx_packets++;
2097 if (tpl->DMABuff)
2098 pci_unmap_single(tp->pdev, tpl->DMABuff, tpl->Skb->len, PCI_DMA_TODEVICE);
2099 dev_kfree_skb_irq(tpl->Skb);
2100 tpl->BusyFlag = 0; /* "free" TPL */
2101 }
2102
2103 netif_wake_queue(dev);
2104 if(tp->QueueSkb < MAX_TX_QUEUE)
2105 tms380tr_hardware_send_packet(dev, tp);
2106 return;
2107 }
2108
2109 /*
2110 * Called if a frame receive interrupt is generated by the adapter.
2111 * Check if the frame is valid and indicate it to system.
2112 */
tms380tr_rcv_status_irq(struct net_device * dev)2113 static void tms380tr_rcv_status_irq(struct net_device *dev)
2114 {
2115 struct net_local *tp = (struct net_local *)dev->priv;
2116 unsigned char *ReceiveDataPtr;
2117 struct sk_buff *skb;
2118 unsigned int Length, Length2;
2119 RPL *rpl;
2120 RPL *SaveHead;
2121 dma_addr_t dmabuf;
2122
2123 /* NOTE: At this point the SSB from RECEIVE STATUS is no longer
2124 * available, because the CLEAR SSB command has already been issued.
2125 *
2126 * Process all complete receives.
2127 */
2128
2129 for(;;)
2130 {
2131 rpl = tp->RplHead;
2132 if(rpl->Status & RX_VALID)
2133 break; /* RPL still in use by adapter */
2134
2135 /* Forward RPLHead pointer to next list. */
2136 SaveHead = tp->RplHead;
2137 tp->RplHead = rpl->NextRPLPtr;
2138
2139 /* Get the frame size (Byte swap for Intel).
2140 * Do this early (see workaround comment below)
2141 */
2142 Length = be16_to_cpu((unsigned short)rpl->FrameSize);
2143
2144 /* Check if the Frame_Start, Frame_End and
2145 * Frame_Complete bits are set.
2146 */
2147 if((rpl->Status & VALID_SINGLE_BUFFER_FRAME)
2148 == VALID_SINGLE_BUFFER_FRAME)
2149 {
2150 ReceiveDataPtr = rpl->MData;
2151
2152 /* Workaround for delayed write of FrameSize on ISA
2153 * (FrameSize is false but valid-bit is reset)
2154 * Frame size is set to zero when the RPL is freed.
2155 * Length2 is there because there have also been
2156 * cases where the FrameSize was partially written
2157 */
2158 Length2 = be16_to_cpu((unsigned short)rpl->FrameSize);
2159
2160 if(Length == 0 || Length != Length2)
2161 {
2162 tp->RplHead = SaveHead;
2163 break; /* Return to tms380tr_interrupt */
2164 }
2165 tms380tr_update_rcv_stats(tp,ReceiveDataPtr,Length);
2166
2167 if(tms380tr_debug > 3)
2168 printk("%s: Packet Length %04X (%d)\n",
2169 dev->name, Length, Length);
2170
2171 /* Indicate the received frame to system the
2172 * adapter does the Source-Routing padding for
2173 * us. See: OpenOptions in tms380tr_init_opb()
2174 */
2175 skb = rpl->Skb;
2176 if(rpl->SkbStat == SKB_UNAVAILABLE)
2177 {
2178 /* Try again to allocate skb */
2179 skb = dev_alloc_skb(tp->MaxPacketSize);
2180 if(skb == NULL)
2181 {
2182 /* Update Stats ?? */
2183 }
2184 else
2185 {
2186 skb->dev = dev;
2187 skb_put(skb, tp->MaxPacketSize);
2188 rpl->SkbStat = SKB_DATA_COPY;
2189 ReceiveDataPtr = rpl->MData;
2190 }
2191 }
2192
2193 if(skb && (rpl->SkbStat == SKB_DATA_COPY
2194 || rpl->SkbStat == SKB_DMA_DIRECT))
2195 {
2196 if(rpl->SkbStat == SKB_DATA_COPY)
2197 memcpy(skb->data, ReceiveDataPtr, Length);
2198
2199 /* Deliver frame to system */
2200 rpl->Skb = NULL;
2201 skb_trim(skb,Length);
2202 skb->protocol = tr_type_trans(skb,dev);
2203 netif_rx(skb);
2204 dev->last_rx = jiffies;
2205 }
2206 }
2207 else /* Invalid frame */
2208 {
2209 if(rpl->Skb != NULL)
2210 dev_kfree_skb_irq(rpl->Skb);
2211
2212 /* Skip list. */
2213 if(rpl->Status & RX_START_FRAME)
2214 /* Frame start bit is set -> overflow. */
2215 tp->MacStat.rx_errors++;
2216 }
2217 if (rpl->DMABuff)
2218 pci_unmap_single(tp->pdev, rpl->DMABuff, tp->MaxPacketSize, PCI_DMA_TODEVICE);
2219 rpl->DMABuff = 0;
2220
2221 /* Allocate new skb for rpl */
2222 rpl->Skb = dev_alloc_skb(tp->MaxPacketSize);
2223 /* skb == NULL ? then use local buffer */
2224 if(rpl->Skb == NULL)
2225 {
2226 rpl->SkbStat = SKB_UNAVAILABLE;
2227 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2228 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2229 }
2230 else /* skb != NULL */
2231 {
2232 rpl->Skb->dev = dev;
2233 skb_put(rpl->Skb, tp->MaxPacketSize);
2234
2235 /* Data unreachable for DMA ? then use local buffer */
2236 dmabuf = pci_map_single(tp->pdev, rpl->Skb->data, tp->MaxPacketSize, PCI_DMA_FROMDEVICE);
2237 if(tp->dmalimit && (dmabuf + tp->MaxPacketSize > tp->dmalimit))
2238 {
2239 rpl->SkbStat = SKB_DATA_COPY;
2240 rpl->FragList[0].DataAddr = htonl(((char *)tp->LocalRxBuffers[rpl->RPLIndex] - (char *)tp) + tp->dmabuffer);
2241 rpl->MData = tp->LocalRxBuffers[rpl->RPLIndex];
2242 }
2243 else
2244 {
2245 /* DMA directly in skb->data */
2246 rpl->SkbStat = SKB_DMA_DIRECT;
2247 rpl->FragList[0].DataAddr = htonl(dmabuf);
2248 rpl->MData = rpl->Skb->data;
2249 rpl->DMABuff = dmabuf;
2250 }
2251 }
2252
2253 rpl->FragList[0].DataCount = cpu_to_be16((unsigned short)tp->MaxPacketSize);
2254 rpl->FrameSize = 0;
2255
2256 /* Pass the last RPL back to the adapter */
2257 tp->RplTail->FrameSize = 0;
2258
2259 /* Reset the CSTAT field in the list. */
2260 tms380tr_write_rpl_status(tp->RplTail, RX_VALID | RX_FRAME_IRQ);
2261
2262 /* Current RPL becomes last one in list. */
2263 tp->RplTail = tp->RplTail->NextRPLPtr;
2264
2265 /* Inform adapter about RPL valid. */
2266 tms380tr_exec_sifcmd(dev, CMD_RX_VALID);
2267 }
2268
2269 return;
2270 }
2271
2272 /*
2273 * This function should be used whenever the status of any RPL must be
2274 * modified by the driver, because the compiler may otherwise change the
2275 * order of instructions such that writing the RPL status may be executed
2276 * at an undesireable time. When this function is used, the status is
2277 * always written when the function is called.
2278 */
tms380tr_write_rpl_status(RPL * rpl,unsigned int Status)2279 static void tms380tr_write_rpl_status(RPL *rpl, unsigned int Status)
2280 {
2281 rpl->Status = Status;
2282
2283 return;
2284 }
2285
2286 /*
2287 * The function updates the statistic counters in mac->MacStat.
2288 * It differtiates between directed and broadcast/multicast ( ==functional)
2289 * frames.
2290 */
tms380tr_update_rcv_stats(struct net_local * tp,unsigned char DataPtr[],unsigned int Length)2291 static void tms380tr_update_rcv_stats(struct net_local *tp, unsigned char DataPtr[],
2292 unsigned int Length)
2293 {
2294 tp->MacStat.rx_packets++;
2295 tp->MacStat.rx_bytes += Length;
2296
2297 /* Test functional bit */
2298 if(DataPtr[2] & GROUP_BIT)
2299 tp->MacStat.multicast++;
2300
2301 return;
2302 }
2303
tms380tr_set_mac_address(struct net_device * dev,void * addr)2304 static int tms380tr_set_mac_address(struct net_device *dev, void *addr)
2305 {
2306 struct net_local *tp = (struct net_local *)dev->priv;
2307 struct sockaddr *saddr = addr;
2308
2309 if (tp->AdapterOpenFlag || tp->AdapterVirtOpenFlag) {
2310 printk(KERN_WARNING "%s: Cannot set MAC/LAA address while card is open\n", dev->name);
2311 return -EIO;
2312 }
2313 memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
2314 return 0;
2315 }
2316
2317 #if TMS380TR_DEBUG > 0
2318 /*
2319 * Dump Packet (data)
2320 */
tms380tr_dump(unsigned char * Data,int length)2321 static void tms380tr_dump(unsigned char *Data, int length)
2322 {
2323 int i, j;
2324
2325 for (i = 0, j = 0; i < length / 8; i++, j += 8)
2326 {
2327 printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\n",
2328 Data[j+0],Data[j+1],Data[j+2],Data[j+3],
2329 Data[j+4],Data[j+5],Data[j+6],Data[j+7]);
2330 }
2331
2332 return;
2333 }
2334 #endif
2335
tmsdev_term(struct net_device * dev)2336 void tmsdev_term(struct net_device *dev)
2337 {
2338 struct net_local *tp;
2339
2340 tp = (struct net_local *) dev->priv;
2341 pci_unmap_single(tp->pdev, tp->dmabuffer, sizeof(struct net_local),
2342 PCI_DMA_BIDIRECTIONAL);
2343 kfree(dev->priv);
2344 }
2345
tmsdev_init(struct net_device * dev,unsigned long dmalimit,struct pci_dev * pdev)2346 int tmsdev_init(struct net_device *dev, unsigned long dmalimit,
2347 struct pci_dev *pdev)
2348 {
2349 if (dev->priv == NULL)
2350 {
2351 struct net_local *tms_local;
2352 dma_addr_t buffer;
2353
2354 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL | GFP_DMA);
2355 if (dev->priv == NULL)
2356 {
2357 printk("%s: Out of memory for DMA\n",
2358 dev->name);
2359 return -ENOMEM;
2360 }
2361 memset(dev->priv, 0, sizeof(struct net_local));
2362 tms_local = (struct net_local *)dev->priv;
2363 init_waitqueue_head(&tms_local->wait_for_tok_int);
2364 tms_local->dmalimit = dmalimit;
2365 tms_local->pdev = pdev;
2366 buffer = pci_map_single(pdev, (void *)tms_local,
2367 sizeof(struct net_local), PCI_DMA_BIDIRECTIONAL);
2368 if (buffer + sizeof(struct net_local) > dmalimit)
2369 {
2370 printk("%s: Memory not accessible for DMA\n",
2371 dev->name);
2372 tmsdev_term(dev);
2373 return -ENOMEM;
2374 }
2375 tms_local->dmabuffer = buffer;
2376 }
2377
2378 /* These can be overridden by the card driver if needed */
2379 dev->init = tms380tr_init_card;
2380 dev->open = tms380tr_open;
2381 dev->stop = tms380tr_close;
2382 dev->do_ioctl = NULL;
2383 dev->hard_start_xmit = tms380tr_send_packet;
2384 dev->tx_timeout = tms380tr_timeout;
2385 dev->watchdog_timeo = HZ;
2386 dev->get_stats = tms380tr_get_stats;
2387 dev->set_multicast_list = &tms380tr_set_multicast_list;
2388 dev->set_mac_address = tms380tr_set_mac_address;
2389
2390 return 0;
2391 }
2392
2393 #ifdef MODULE
2394
2395 EXPORT_SYMBOL(tms380tr_open);
2396 EXPORT_SYMBOL(tms380tr_close);
2397 EXPORT_SYMBOL(tms380tr_interrupt);
2398 EXPORT_SYMBOL(tmsdev_init);
2399 EXPORT_SYMBOL(tmsdev_term);
2400 EXPORT_SYMBOL(tms380tr_wait);
2401
2402 struct module *TMS380_module = NULL;
2403
init_module(void)2404 int init_module(void)
2405 {
2406 printk("%s", version);
2407
2408 TMS380_module = &__this_module;
2409 return 0;
2410 }
2411
cleanup_module(void)2412 void cleanup_module(void)
2413 {
2414 TMS380_module = NULL;
2415 }
2416 #endif
2417
2418 MODULE_LICENSE("GPL");
2419
2420
2421 /*
2422 * Local variables:
2423 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c tms380tr.c"
2424 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c tms380tr.c"
2425 * c-set-style "K&R"
2426 * c-indent-level: 8
2427 * c-basic-offset: 8
2428 * tab-width: 8
2429 * End:
2430 */
2431