Linux 2.6.34-rc3
[pohmelfs.git] / drivers / char / ip2 / i2lib.c
blob0d10b89218ed1da1818f47c643705f99d9dd3b55
1 /*******************************************************************************
3 * (c) 1999 by Computone Corporation
5 ********************************************************************************
8 * PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
9 * serial I/O controllers.
11 * DESCRIPTION: High-level interface code for the device driver. Uses the
12 * Extremely Low Level Interface Support (i2ellis.c). Provides an
13 * interface to the standard loadware, to support drivers or
14 * application code. (This is included source code, not a separate
15 * compilation module.)
17 *******************************************************************************/
18 //------------------------------------------------------------------------------
19 // Note on Strategy:
20 // Once the board has been initialized, it will interrupt us when:
21 // 1) It has something in the fifo for us to read (incoming data, flow control
22 // packets, or whatever).
23 // 2) It has stripped whatever we have sent last time in the FIFO (and
24 // consequently is ready for more).
26 // Note also that the buffer sizes declared in i2lib.h are VERY SMALL. This
27 // worsens performance considerably, but is done so that a great many channels
28 // might use only a little memory.
29 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 // Revision History:
34 // 0.00 - 4/16/91 --- First Draft
35 // 0.01 - 4/29/91 --- 1st beta release
36 // 0.02 - 6/14/91 --- Changes to allow small model compilation
37 // 0.03 - 6/17/91 MAG Break reporting protected from interrupts routines with
38 // in-line asm added for moving data to/from ring buffers,
39 // replacing a variety of methods used previously.
40 // 0.04 - 6/21/91 MAG Initial flow-control packets not queued until
41 // i2_enable_interrupts time. Former versions would enqueue
42 // them at i2_init_channel time, before we knew how many
43 // channels were supposed to exist!
44 // 0.05 - 10/12/91 MAG Major changes: works through the ellis.c routines now;
45 // supports new 16-bit protocol and expandable boards.
46 // - 10/24/91 MAG Most changes in place and stable.
47 // 0.06 - 2/20/92 MAG Format of CMD_HOTACK corrected: the command takes no
48 // argument.
49 // 0.07 -- 3/11/92 MAG Support added to store special packet types at interrupt
50 // level (mostly responses to specific commands.)
51 // 0.08 -- 3/30/92 MAG Support added for STAT_MODEM packet
52 // 0.09 -- 6/24/93 MAG i2Link... needed to update number of boards BEFORE
53 // turning on the interrupt.
54 // 0.10 -- 6/25/93 MAG To avoid gruesome death from a bad board, we sanity check
55 // some incoming.
57 // 1.1 - 12/25/96 AKM Linux version.
58 // - 10/09/98 DMC Revised Linux version.
59 //------------------------------------------------------------------------------
61 //************
62 //* Includes *
63 //************
65 #include <linux/sched.h>
66 #include "i2lib.h"
69 //***********************
70 //* Function Prototypes *
71 //***********************
72 static void i2QueueNeeds(i2eBordStrPtr, i2ChanStrPtr, int);
73 static i2ChanStrPtr i2DeQueueNeeds(i2eBordStrPtr, int );
74 static void i2StripFifo(i2eBordStrPtr);
75 static void i2StuffFifoBypass(i2eBordStrPtr);
76 static void i2StuffFifoFlow(i2eBordStrPtr);
77 static void i2StuffFifoInline(i2eBordStrPtr);
78 static int i2RetryFlushOutput(i2ChanStrPtr);
80 // Not a documented part of the library routines (careful...) but the Diagnostic
81 // i2diag.c finds them useful to help the throughput in certain limited
82 // single-threaded operations.
83 static void iiSendPendingMail(i2eBordStrPtr);
84 static void serviceOutgoingFifo(i2eBordStrPtr);
86 // Functions defined in ip2.c as part of interrupt handling
87 static void do_input(struct work_struct *);
88 static void do_status(struct work_struct *);
90 //***************
91 //* Debug Data *
92 //***************
93 #ifdef DEBUG_FIFO
95 unsigned char DBGBuf[0x4000];
96 unsigned short I = 0;
98 static void
99 WriteDBGBuf(char *s, unsigned char *src, unsigned short n )
101 char *p = src;
103 // XXX: We need a spin lock here if we ever use this again
105 while (*s) { // copy label
106 DBGBuf[I] = *s++;
107 I = I++ & 0x3fff;
109 while (n--) { // copy data
110 DBGBuf[I] = *p++;
111 I = I++ & 0x3fff;
115 static void
116 fatality(i2eBordStrPtr pB )
118 int i;
120 for (i=0;i<sizeof(DBGBuf);i++) {
121 if ((i%16) == 0)
122 printk("\n%4x:",i);
123 printk("%02x ",DBGBuf[i]);
125 printk("\n");
126 for (i=0;i<sizeof(DBGBuf);i++) {
127 if ((i%16) == 0)
128 printk("\n%4x:",i);
129 if (DBGBuf[i] >= ' ' && DBGBuf[i] <= '~') {
130 printk(" %c ",DBGBuf[i]);
131 } else {
132 printk(" . ");
135 printk("\n");
136 printk("Last index %x\n",I);
138 #endif /* DEBUG_FIFO */
140 //********
141 //* Code *
142 //********
144 static inline int
145 i2Validate ( i2ChanStrPtr pCh )
147 //ip2trace(pCh->port_index, ITRC_VERIFY,ITRC_ENTER,2,pCh->validity,
148 // (CHANNEL_MAGIC | CHANNEL_SUPPORT));
149 return ((pCh->validity & (CHANNEL_MAGIC_BITS | CHANNEL_SUPPORT))
150 == (CHANNEL_MAGIC | CHANNEL_SUPPORT));
153 static void iiSendPendingMail_t(unsigned long data)
155 i2eBordStrPtr pB = (i2eBordStrPtr)data;
157 iiSendPendingMail(pB);
160 //******************************************************************************
161 // Function: iiSendPendingMail(pB)
162 // Parameters: Pointer to a board structure
163 // Returns: Nothing
165 // Description:
166 // If any outgoing mail bits are set and there is outgoing mailbox is empty,
167 // send the mail and clear the bits.
168 //******************************************************************************
169 static void
170 iiSendPendingMail(i2eBordStrPtr pB)
172 if (pB->i2eOutMailWaiting && (!pB->i2eWaitingForEmptyFifo) )
174 if (iiTrySendMail(pB, pB->i2eOutMailWaiting))
176 /* If we were already waiting for fifo to empty,
177 * or just sent MB_OUT_STUFFED, then we are
178 * still waiting for it to empty, until we should
179 * receive an MB_IN_STRIPPED from the board.
181 pB->i2eWaitingForEmptyFifo |=
182 (pB->i2eOutMailWaiting & MB_OUT_STUFFED);
183 pB->i2eOutMailWaiting = 0;
184 pB->SendPendingRetry = 0;
185 } else {
186 /* The only time we hit this area is when "iiTrySendMail" has
187 failed. That only occurs when the outbound mailbox is
188 still busy with the last message. We take a short breather
189 to let the board catch up with itself and then try again.
190 16 Retries is the limit - then we got a borked board.
191 /\/\|=mhw=|\/\/ */
193 if( ++pB->SendPendingRetry < 16 ) {
194 setup_timer(&pB->SendPendingTimer,
195 iiSendPendingMail_t, (unsigned long)pB);
196 mod_timer(&pB->SendPendingTimer, jiffies + 1);
197 } else {
198 printk( KERN_ERR "IP2: iiSendPendingMail unable to queue outbound mail\n" );
204 //******************************************************************************
205 // Function: i2InitChannels(pB, nChannels, pCh)
206 // Parameters: Pointer to Ellis Board structure
207 // Number of channels to initialize
208 // Pointer to first element in an array of channel structures
209 // Returns: Success or failure
211 // Description:
213 // This function patches pointers, back-pointers, and initializes all the
214 // elements in the channel structure array.
216 // This should be run after the board structure is initialized, through having
217 // loaded the standard loadware (otherwise it complains).
219 // In any case, it must be done before any serious work begins initializing the
220 // irq's or sending commands...
222 //******************************************************************************
223 static int
224 i2InitChannels ( i2eBordStrPtr pB, int nChannels, i2ChanStrPtr pCh)
226 int index, stuffIndex;
227 i2ChanStrPtr *ppCh;
229 if (pB->i2eValid != I2E_MAGIC) {
230 I2_COMPLETE(pB, I2EE_BADMAGIC);
232 if (pB->i2eState != II_STATE_STDLOADED) {
233 I2_COMPLETE(pB, I2EE_BADSTATE);
236 rwlock_init(&pB->read_fifo_spinlock);
237 rwlock_init(&pB->write_fifo_spinlock);
238 rwlock_init(&pB->Dbuf_spinlock);
239 rwlock_init(&pB->Bbuf_spinlock);
240 rwlock_init(&pB->Fbuf_spinlock);
242 // NO LOCK needed yet - this is init
244 pB->i2eChannelPtr = pCh;
245 pB->i2eChannelCnt = nChannels;
247 pB->i2Fbuf_strip = pB->i2Fbuf_stuff = 0;
248 pB->i2Dbuf_strip = pB->i2Dbuf_stuff = 0;
249 pB->i2Bbuf_strip = pB->i2Bbuf_stuff = 0;
251 pB->SendPendingRetry = 0;
253 memset ( pCh, 0, sizeof (i2ChanStr) * nChannels );
255 for (index = stuffIndex = 0, ppCh = (i2ChanStrPtr *)(pB->i2Fbuf);
256 nChannels && index < ABS_MOST_PORTS;
257 index++)
259 if ( !(pB->i2eChannelMap[index >> 4] & (1 << (index & 0xf)) ) ) {
260 continue;
262 rwlock_init(&pCh->Ibuf_spinlock);
263 rwlock_init(&pCh->Obuf_spinlock);
264 rwlock_init(&pCh->Cbuf_spinlock);
265 rwlock_init(&pCh->Pbuf_spinlock);
266 // NO LOCK needed yet - this is init
267 // Set up validity flag according to support level
268 if (pB->i2eGoodMap[index >> 4] & (1 << (index & 0xf)) ) {
269 pCh->validity = CHANNEL_MAGIC | CHANNEL_SUPPORT;
270 } else {
271 pCh->validity = CHANNEL_MAGIC;
273 pCh->pMyBord = pB; /* Back-pointer */
275 // Prepare an outgoing flow-control packet to send as soon as the chance
276 // occurs.
277 if ( pCh->validity & CHANNEL_SUPPORT ) {
278 pCh->infl.hd.i2sChannel = index;
279 pCh->infl.hd.i2sCount = 5;
280 pCh->infl.hd.i2sType = PTYPE_BYPASS;
281 pCh->infl.fcmd = 37;
282 pCh->infl.asof = 0;
283 pCh->infl.room = IBUF_SIZE - 1;
285 pCh->whenSendFlow = (IBUF_SIZE/5)*4; // when 80% full
287 // The following is similar to calling i2QueueNeeds, except that this
288 // is done in longhand, since we are setting up initial conditions on
289 // many channels at once.
290 pCh->channelNeeds = NEED_FLOW; // Since starting from scratch
291 pCh->sinceLastFlow = 0; // No bytes received since last flow
292 // control packet was queued
293 stuffIndex++;
294 *ppCh++ = pCh; // List this channel as needing
295 // initial flow control packet sent
298 // Don't allow anything to be sent until the status packets come in from
299 // the board.
301 pCh->outfl.asof = 0;
302 pCh->outfl.room = 0;
304 // Initialize all the ring buffers
306 pCh->Ibuf_stuff = pCh->Ibuf_strip = 0;
307 pCh->Obuf_stuff = pCh->Obuf_strip = 0;
308 pCh->Cbuf_stuff = pCh->Cbuf_strip = 0;
310 memset( &pCh->icount, 0, sizeof (struct async_icount) );
311 pCh->hotKeyIn = HOT_CLEAR;
312 pCh->channelOptions = 0;
313 pCh->bookMarks = 0;
314 init_waitqueue_head(&pCh->pBookmarkWait);
316 init_waitqueue_head(&pCh->open_wait);
317 init_waitqueue_head(&pCh->close_wait);
318 init_waitqueue_head(&pCh->delta_msr_wait);
320 // Set base and divisor so default custom rate is 9600
321 pCh->BaudBase = 921600; // MAX for ST654, changed after we get
322 pCh->BaudDivisor = 96; // the boxids (UART types) later
324 pCh->dataSetIn = 0;
325 pCh->dataSetOut = 0;
327 pCh->wopen = 0;
328 pCh->throttled = 0;
330 pCh->speed = CBR_9600;
332 pCh->flags = 0;
334 pCh->ClosingDelay = 5*HZ/10;
335 pCh->ClosingWaitTime = 30*HZ;
337 // Initialize task queue objects
338 INIT_WORK(&pCh->tqueue_input, do_input);
339 INIT_WORK(&pCh->tqueue_status, do_status);
341 #ifdef IP2DEBUG_TRACE
342 pCh->trace = ip2trace;
343 #endif
345 ++pCh;
346 --nChannels;
348 // No need to check for wrap here; this is initialization.
349 pB->i2Fbuf_stuff = stuffIndex;
350 I2_COMPLETE(pB, I2EE_GOOD);
354 //******************************************************************************
355 // Function: i2DeQueueNeeds(pB, type)
356 // Parameters: Pointer to a board structure
357 // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
358 // Returns:
359 // Pointer to a channel structure
361 // Description: Returns pointer struct of next channel that needs service of
362 // the type specified. Otherwise returns a NULL reference.
364 //******************************************************************************
365 static i2ChanStrPtr
366 i2DeQueueNeeds(i2eBordStrPtr pB, int type)
368 unsigned short queueIndex;
369 unsigned long flags;
371 i2ChanStrPtr pCh = NULL;
373 switch(type) {
375 case NEED_INLINE:
377 write_lock_irqsave(&pB->Dbuf_spinlock, flags);
378 if ( pB->i2Dbuf_stuff != pB->i2Dbuf_strip)
380 queueIndex = pB->i2Dbuf_strip;
381 pCh = pB->i2Dbuf[queueIndex];
382 queueIndex++;
383 if (queueIndex >= CH_QUEUE_SIZE) {
384 queueIndex = 0;
386 pB->i2Dbuf_strip = queueIndex;
387 pCh->channelNeeds &= ~NEED_INLINE;
389 write_unlock_irqrestore(&pB->Dbuf_spinlock, flags);
390 break;
392 case NEED_BYPASS:
394 write_lock_irqsave(&pB->Bbuf_spinlock, flags);
395 if (pB->i2Bbuf_stuff != pB->i2Bbuf_strip)
397 queueIndex = pB->i2Bbuf_strip;
398 pCh = pB->i2Bbuf[queueIndex];
399 queueIndex++;
400 if (queueIndex >= CH_QUEUE_SIZE) {
401 queueIndex = 0;
403 pB->i2Bbuf_strip = queueIndex;
404 pCh->channelNeeds &= ~NEED_BYPASS;
406 write_unlock_irqrestore(&pB->Bbuf_spinlock, flags);
407 break;
409 case NEED_FLOW:
411 write_lock_irqsave(&pB->Fbuf_spinlock, flags);
412 if (pB->i2Fbuf_stuff != pB->i2Fbuf_strip)
414 queueIndex = pB->i2Fbuf_strip;
415 pCh = pB->i2Fbuf[queueIndex];
416 queueIndex++;
417 if (queueIndex >= CH_QUEUE_SIZE) {
418 queueIndex = 0;
420 pB->i2Fbuf_strip = queueIndex;
421 pCh->channelNeeds &= ~NEED_FLOW;
423 write_unlock_irqrestore(&pB->Fbuf_spinlock, flags);
424 break;
425 default:
426 printk(KERN_ERR "i2DeQueueNeeds called with bad type:%x\n",type);
427 break;
429 return pCh;
432 //******************************************************************************
433 // Function: i2QueueNeeds(pB, pCh, type)
434 // Parameters: Pointer to a board structure
435 // Pointer to a channel structure
436 // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
437 // Returns: Nothing
439 // Description:
440 // For each type of need selected, if the given channel is not already in the
441 // queue, adds it, and sets the flag indicating it is in the queue.
442 //******************************************************************************
443 static void
444 i2QueueNeeds(i2eBordStrPtr pB, i2ChanStrPtr pCh, int type)
446 unsigned short queueIndex;
447 unsigned long flags;
449 // We turn off all the interrupts during this brief process, since the
450 // interrupt-level code might want to put things on the queue as well.
452 switch (type) {
454 case NEED_INLINE:
456 write_lock_irqsave(&pB->Dbuf_spinlock, flags);
457 if ( !(pCh->channelNeeds & NEED_INLINE) )
459 pCh->channelNeeds |= NEED_INLINE;
460 queueIndex = pB->i2Dbuf_stuff;
461 pB->i2Dbuf[queueIndex++] = pCh;
462 if (queueIndex >= CH_QUEUE_SIZE)
463 queueIndex = 0;
464 pB->i2Dbuf_stuff = queueIndex;
466 write_unlock_irqrestore(&pB->Dbuf_spinlock, flags);
467 break;
469 case NEED_BYPASS:
471 write_lock_irqsave(&pB->Bbuf_spinlock, flags);
472 if ((type & NEED_BYPASS) && !(pCh->channelNeeds & NEED_BYPASS))
474 pCh->channelNeeds |= NEED_BYPASS;
475 queueIndex = pB->i2Bbuf_stuff;
476 pB->i2Bbuf[queueIndex++] = pCh;
477 if (queueIndex >= CH_QUEUE_SIZE)
478 queueIndex = 0;
479 pB->i2Bbuf_stuff = queueIndex;
481 write_unlock_irqrestore(&pB->Bbuf_spinlock, flags);
482 break;
484 case NEED_FLOW:
486 write_lock_irqsave(&pB->Fbuf_spinlock, flags);
487 if ((type & NEED_FLOW) && !(pCh->channelNeeds & NEED_FLOW))
489 pCh->channelNeeds |= NEED_FLOW;
490 queueIndex = pB->i2Fbuf_stuff;
491 pB->i2Fbuf[queueIndex++] = pCh;
492 if (queueIndex >= CH_QUEUE_SIZE)
493 queueIndex = 0;
494 pB->i2Fbuf_stuff = queueIndex;
496 write_unlock_irqrestore(&pB->Fbuf_spinlock, flags);
497 break;
499 case NEED_CREDIT:
500 pCh->channelNeeds |= NEED_CREDIT;
501 break;
502 default:
503 printk(KERN_ERR "i2QueueNeeds called with bad type:%x\n",type);
504 break;
506 return;
509 //******************************************************************************
510 // Function: i2QueueCommands(type, pCh, timeout, nCommands, pCs,...)
511 // Parameters: type - PTYPE_BYPASS or PTYPE_INLINE
512 // pointer to the channel structure
513 // maximum period to wait
514 // number of commands (n)
515 // n commands
516 // Returns: Number of commands sent, or -1 for error
518 // get board lock before calling
520 // Description:
521 // Queues up some commands to be sent to a channel. To send possibly several
522 // bypass or inline commands to the given channel. The timeout parameter
523 // indicates how many HUNDREDTHS OF SECONDS to wait until there is room:
524 // 0 = return immediately if no room, -ive = wait forever, +ive = number of
525 // 1/100 seconds to wait. Return values:
526 // -1 Some kind of nasty error: bad channel structure or invalid arguments.
527 // 0 No room to send all the commands
528 // (+) Number of commands sent
529 //******************************************************************************
530 static int
531 i2QueueCommands(int type, i2ChanStrPtr pCh, int timeout, int nCommands,
532 cmdSyntaxPtr pCs0,...)
534 int totalsize = 0;
535 int blocksize;
536 int lastended;
537 cmdSyntaxPtr *ppCs;
538 cmdSyntaxPtr pCs;
539 int count;
540 int flag;
541 i2eBordStrPtr pB;
543 unsigned short maxBlock;
544 unsigned short maxBuff;
545 short bufroom;
546 unsigned short stuffIndex;
547 unsigned char *pBuf;
548 unsigned char *pInsert;
549 unsigned char *pDest, *pSource;
550 unsigned short channel;
551 int cnt;
552 unsigned long flags = 0;
553 rwlock_t *lock_var_p = NULL;
555 // Make sure the channel exists, otherwise do nothing
556 if ( !i2Validate ( pCh ) ) {
557 return -1;
560 ip2trace (CHANN, ITRC_QUEUE, ITRC_ENTER, 0 );
562 pB = pCh->pMyBord;
564 // Board must also exist, and THE INTERRUPT COMMAND ALREADY SENT
565 if (pB->i2eValid != I2E_MAGIC || pB->i2eUsingIrq == I2_IRQ_UNDEFINED)
566 return -2;
567 // If the board has gone fatal, return bad, and also hit the trap routine if
568 // it exists.
569 if (pB->i2eFatal) {
570 if ( pB->i2eFatalTrap ) {
571 (*(pB)->i2eFatalTrap)(pB);
573 return -3;
575 // Set up some variables, Which buffers are we using? How big are they?
576 switch(type)
578 case PTYPE_INLINE:
579 flag = INL;
580 maxBlock = MAX_OBUF_BLOCK;
581 maxBuff = OBUF_SIZE;
582 pBuf = pCh->Obuf;
583 break;
584 case PTYPE_BYPASS:
585 flag = BYP;
586 maxBlock = MAX_CBUF_BLOCK;
587 maxBuff = CBUF_SIZE;
588 pBuf = pCh->Cbuf;
589 break;
590 default:
591 return -4;
593 // Determine the total size required for all the commands
594 totalsize = blocksize = sizeof(i2CmdHeader);
595 lastended = 0;
596 ppCs = &pCs0;
597 for ( count = nCommands; count; count--, ppCs++)
599 pCs = *ppCs;
600 cnt = pCs->length;
601 // Will a new block be needed for this one?
602 // Two possible reasons: too
603 // big or previous command has to be at the end of a packet.
604 if ((blocksize + cnt > maxBlock) || lastended) {
605 blocksize = sizeof(i2CmdHeader);
606 totalsize += sizeof(i2CmdHeader);
608 totalsize += cnt;
609 blocksize += cnt;
611 // If this command had to end a block, then we will make sure to
612 // account for it should there be any more blocks.
613 lastended = pCs->flags & END;
615 for (;;) {
616 // Make sure any pending flush commands go out before we add more data.
617 if ( !( pCh->flush_flags && i2RetryFlushOutput( pCh ) ) ) {
618 // How much room (this time through) ?
619 switch(type) {
620 case PTYPE_INLINE:
621 lock_var_p = &pCh->Obuf_spinlock;
622 write_lock_irqsave(lock_var_p, flags);
623 stuffIndex = pCh->Obuf_stuff;
624 bufroom = pCh->Obuf_strip - stuffIndex;
625 break;
626 case PTYPE_BYPASS:
627 lock_var_p = &pCh->Cbuf_spinlock;
628 write_lock_irqsave(lock_var_p, flags);
629 stuffIndex = pCh->Cbuf_stuff;
630 bufroom = pCh->Cbuf_strip - stuffIndex;
631 break;
632 default:
633 return -5;
635 if (--bufroom < 0) {
636 bufroom += maxBuff;
639 ip2trace (CHANN, ITRC_QUEUE, 2, 1, bufroom );
641 // Check for overflow
642 if (totalsize <= bufroom) {
643 // Normal Expected path - We still hold LOCK
644 break; /* from for()- Enough room: goto proceed */
646 ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
647 write_unlock_irqrestore(lock_var_p, flags);
648 } else
649 ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
651 /* Prepare to wait for buffers to empty */
652 serviceOutgoingFifo(pB); // Dump what we got
654 if (timeout == 0) {
655 return 0; // Tired of waiting
657 if (timeout > 0)
658 timeout--; // So negative values == forever
660 if (!in_interrupt()) {
661 schedule_timeout_interruptible(1); // short nap
662 } else {
663 // we cannot sched/sleep in interrupt silly
664 return 0;
666 if (signal_pending(current)) {
667 return 0; // Wake up! Time to die!!!
670 ip2trace (CHANN, ITRC_QUEUE, 4, 0 );
672 } // end of for(;;)
674 // At this point we have room and the lock - stick them in.
675 channel = pCh->infl.hd.i2sChannel;
676 pInsert = &pBuf[stuffIndex]; // Pointer to start of packet
677 pDest = CMD_OF(pInsert); // Pointer to start of command
679 // When we start counting, the block is the size of the header
680 for (blocksize = sizeof(i2CmdHeader), count = nCommands,
681 lastended = 0, ppCs = &pCs0;
682 count;
683 count--, ppCs++)
685 pCs = *ppCs; // Points to command protocol structure
687 // If this is a bookmark request command, post the fact that a bookmark
688 // request is pending. NOTE THIS TRICK ONLY WORKS BECAUSE CMD_BMARK_REQ
689 // has no parameters! The more general solution would be to reference
690 // pCs->cmd[0].
691 if (pCs == CMD_BMARK_REQ) {
692 pCh->bookMarks++;
694 ip2trace (CHANN, ITRC_DRAIN, 30, 1, pCh->bookMarks );
697 cnt = pCs->length;
699 // If this command would put us over the maximum block size or
700 // if the last command had to be at the end of a block, we end
701 // the existing block here and start a new one.
702 if ((blocksize + cnt > maxBlock) || lastended) {
704 ip2trace (CHANN, ITRC_QUEUE, 5, 0 );
706 PTYPE_OF(pInsert) = type;
707 CHANNEL_OF(pInsert) = channel;
708 // count here does not include the header
709 CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
710 stuffIndex += blocksize;
711 if(stuffIndex >= maxBuff) {
712 stuffIndex = 0;
713 pInsert = pBuf;
715 pInsert = &pBuf[stuffIndex]; // Pointer to start of next pkt
716 pDest = CMD_OF(pInsert);
717 blocksize = sizeof(i2CmdHeader);
719 // Now we know there is room for this one in the current block
721 blocksize += cnt; // Total bytes in this command
722 pSource = pCs->cmd; // Copy the command into the buffer
723 while (cnt--) {
724 *pDest++ = *pSource++;
726 // If this command had to end a block, then we will make sure to account
727 // for it should there be any more blocks.
728 lastended = pCs->flags & END;
729 } // end for
730 // Clean up the final block by writing header, etc
732 PTYPE_OF(pInsert) = type;
733 CHANNEL_OF(pInsert) = channel;
734 // count here does not include the header
735 CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
736 stuffIndex += blocksize;
737 if(stuffIndex >= maxBuff) {
738 stuffIndex = 0;
739 pInsert = pBuf;
741 // Updates the index, and post the need for service. When adding these to
742 // the queue of channels, we turn off the interrupt while doing so,
743 // because at interrupt level we might want to push a channel back to the
744 // end of the queue.
745 switch(type)
747 case PTYPE_INLINE:
748 pCh->Obuf_stuff = stuffIndex; // Store buffer pointer
749 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
751 pB->debugInlineQueued++;
752 // Add the channel pointer to list of channels needing service (first
753 // come...), if it's not already there.
754 i2QueueNeeds(pB, pCh, NEED_INLINE);
755 break;
757 case PTYPE_BYPASS:
758 pCh->Cbuf_stuff = stuffIndex; // Store buffer pointer
759 write_unlock_irqrestore(&pCh->Cbuf_spinlock, flags);
761 pB->debugBypassQueued++;
762 // Add the channel pointer to list of channels needing service (first
763 // come...), if it's not already there.
764 i2QueueNeeds(pB, pCh, NEED_BYPASS);
765 break;
768 ip2trace (CHANN, ITRC_QUEUE, ITRC_RETURN, 1, nCommands );
770 return nCommands; // Good status: number of commands sent
773 //******************************************************************************
774 // Function: i2GetStatus(pCh,resetBits)
775 // Parameters: Pointer to a channel structure
776 // Bit map of status bits to clear
777 // Returns: Bit map of current status bits
779 // Description:
780 // Returns the state of data set signals, and whether a break has been received,
781 // (see i2lib.h for bit-mapped result). resetBits is a bit-map of any status
782 // bits to be cleared: I2_BRK, I2_PAR, I2_FRA, I2_OVR,... These are cleared
783 // AFTER the condition is passed. If pCh does not point to a valid channel,
784 // returns -1 (which would be impossible otherwise.
785 //******************************************************************************
786 static int
787 i2GetStatus(i2ChanStrPtr pCh, int resetBits)
789 unsigned short status;
790 i2eBordStrPtr pB;
792 ip2trace (CHANN, ITRC_STATUS, ITRC_ENTER, 2, pCh->dataSetIn, resetBits );
794 // Make sure the channel exists, otherwise do nothing */
795 if ( !i2Validate ( pCh ) )
796 return -1;
798 pB = pCh->pMyBord;
800 status = pCh->dataSetIn;
802 // Clear any specified error bits: but note that only actual error bits can
803 // be cleared, regardless of the value passed.
804 if (resetBits)
806 pCh->dataSetIn &= ~(resetBits & (I2_BRK | I2_PAR | I2_FRA | I2_OVR));
807 pCh->dataSetIn &= ~(I2_DDCD | I2_DCTS | I2_DDSR | I2_DRI);
810 ip2trace (CHANN, ITRC_STATUS, ITRC_RETURN, 1, pCh->dataSetIn );
812 return status;
815 //******************************************************************************
816 // Function: i2Input(pChpDest,count)
817 // Parameters: Pointer to a channel structure
818 // Pointer to data buffer
819 // Number of bytes to read
820 // Returns: Number of bytes read, or -1 for error
822 // Description:
823 // Strips data from the input buffer and writes it to pDest. If there is a
824 // collosal blunder, (invalid structure pointers or the like), returns -1.
825 // Otherwise, returns the number of bytes read.
826 //******************************************************************************
827 static int
828 i2Input(i2ChanStrPtr pCh)
830 int amountToMove;
831 unsigned short stripIndex;
832 int count;
833 unsigned long flags = 0;
835 ip2trace (CHANN, ITRC_INPUT, ITRC_ENTER, 0);
837 // Ensure channel structure seems real
838 if ( !i2Validate( pCh ) ) {
839 count = -1;
840 goto i2Input_exit;
842 write_lock_irqsave(&pCh->Ibuf_spinlock, flags);
844 // initialize some accelerators and private copies
845 stripIndex = pCh->Ibuf_strip;
847 count = pCh->Ibuf_stuff - stripIndex;
849 // If buffer is empty or requested data count was 0, (trivial case) return
850 // without any further thought.
851 if ( count == 0 ) {
852 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
853 goto i2Input_exit;
855 // Adjust for buffer wrap
856 if ( count < 0 ) {
857 count += IBUF_SIZE;
859 // Don't give more than can be taken by the line discipline
860 amountToMove = pCh->pTTY->receive_room;
861 if (count > amountToMove) {
862 count = amountToMove;
864 // How much could we copy without a wrap?
865 amountToMove = IBUF_SIZE - stripIndex;
867 if (amountToMove > count) {
868 amountToMove = count;
870 // Move the first block
871 pCh->pTTY->ldisc->ops->receive_buf( pCh->pTTY,
872 &(pCh->Ibuf[stripIndex]), NULL, amountToMove );
873 // If we needed to wrap, do the second data move
874 if (count > amountToMove) {
875 pCh->pTTY->ldisc->ops->receive_buf( pCh->pTTY,
876 pCh->Ibuf, NULL, count - amountToMove );
878 // Bump and wrap the stripIndex all at once by the amount of data read. This
879 // method is good regardless of whether the data was in one or two pieces.
880 stripIndex += count;
881 if (stripIndex >= IBUF_SIZE) {
882 stripIndex -= IBUF_SIZE;
884 pCh->Ibuf_strip = stripIndex;
886 // Update our flow control information and possibly queue ourselves to send
887 // it, depending on how much data has been stripped since the last time a
888 // packet was sent.
889 pCh->infl.asof += count;
891 if ((pCh->sinceLastFlow += count) >= pCh->whenSendFlow) {
892 pCh->sinceLastFlow -= pCh->whenSendFlow;
893 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
894 i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
895 } else {
896 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
899 i2Input_exit:
901 ip2trace (CHANN, ITRC_INPUT, ITRC_RETURN, 1, count);
903 return count;
906 //******************************************************************************
907 // Function: i2InputFlush(pCh)
908 // Parameters: Pointer to a channel structure
909 // Returns: Number of bytes stripped, or -1 for error
911 // Description:
912 // Strips any data from the input buffer. If there is a collosal blunder,
913 // (invalid structure pointers or the like), returns -1. Otherwise, returns the
914 // number of bytes stripped.
915 //******************************************************************************
916 static int
917 i2InputFlush(i2ChanStrPtr pCh)
919 int count;
920 unsigned long flags;
922 // Ensure channel structure seems real
923 if ( !i2Validate ( pCh ) )
924 return -1;
926 ip2trace (CHANN, ITRC_INPUT, 10, 0);
928 write_lock_irqsave(&pCh->Ibuf_spinlock, flags);
929 count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
931 // Adjust for buffer wrap
932 if (count < 0) {
933 count += IBUF_SIZE;
936 // Expedient way to zero out the buffer
937 pCh->Ibuf_strip = pCh->Ibuf_stuff;
940 // Update our flow control information and possibly queue ourselves to send
941 // it, depending on how much data has been stripped since the last time a
942 // packet was sent.
944 pCh->infl.asof += count;
946 if ( (pCh->sinceLastFlow += count) >= pCh->whenSendFlow )
948 pCh->sinceLastFlow -= pCh->whenSendFlow;
949 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
950 i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
951 } else {
952 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
955 ip2trace (CHANN, ITRC_INPUT, 19, 1, count);
957 return count;
960 //******************************************************************************
961 // Function: i2InputAvailable(pCh)
962 // Parameters: Pointer to a channel structure
963 // Returns: Number of bytes available, or -1 for error
965 // Description:
966 // If there is a collosal blunder, (invalid structure pointers or the like),
967 // returns -1. Otherwise, returns the number of bytes stripped. Otherwise,
968 // returns the number of bytes available in the buffer.
969 //******************************************************************************
970 #if 0
971 static int
972 i2InputAvailable(i2ChanStrPtr pCh)
974 int count;
976 // Ensure channel structure seems real
977 if ( !i2Validate ( pCh ) ) return -1;
980 // initialize some accelerators and private copies
981 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
982 count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
983 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
985 // Adjust for buffer wrap
986 if (count < 0)
988 count += IBUF_SIZE;
991 return count;
993 #endif
995 //******************************************************************************
996 // Function: i2Output(pCh, pSource, count)
997 // Parameters: Pointer to channel structure
998 // Pointer to source data
999 // Number of bytes to send
1000 // Returns: Number of bytes sent, or -1 for error
1002 // Description:
1003 // Queues the data at pSource to be sent as data packets to the board. If there
1004 // is a collosal blunder, (invalid structure pointers or the like), returns -1.
1005 // Otherwise, returns the number of bytes written. What if there is not enough
1006 // room for all the data? If pCh->channelOptions & CO_NBLOCK_WRITE is set, then
1007 // we transfer as many characters as we can now, then return. If this bit is
1008 // clear (default), routine will spin along until all the data is buffered.
1009 // Should this occur, the 1-ms delay routine is called while waiting to avoid
1010 // applications that one cannot break out of.
1011 //******************************************************************************
1012 static int
1013 i2Output(i2ChanStrPtr pCh, const char *pSource, int count)
1015 i2eBordStrPtr pB;
1016 unsigned char *pInsert;
1017 int amountToMove;
1018 int countOriginal = count;
1019 unsigned short channel;
1020 unsigned short stuffIndex;
1021 unsigned long flags;
1023 int bailout = 10;
1025 ip2trace (CHANN, ITRC_OUTPUT, ITRC_ENTER, 2, count, 0 );
1027 // Ensure channel structure seems real
1028 if ( !i2Validate ( pCh ) )
1029 return -1;
1031 // initialize some accelerators and private copies
1032 pB = pCh->pMyBord;
1033 channel = pCh->infl.hd.i2sChannel;
1035 // If the board has gone fatal, return bad, and also hit the trap routine if
1036 // it exists.
1037 if (pB->i2eFatal) {
1038 if (pB->i2eFatalTrap) {
1039 (*(pB)->i2eFatalTrap)(pB);
1041 return -1;
1043 // Proceed as though we would do everything
1044 while ( count > 0 ) {
1046 // How much room in output buffer is there?
1047 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1048 amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
1049 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1050 if (amountToMove < 0) {
1051 amountToMove += OBUF_SIZE;
1053 // Subtract off the headers size and see how much room there is for real
1054 // data. If this is negative, we will discover later.
1055 amountToMove -= sizeof (i2DataHeader);
1057 // Don't move more (now) than can go in a single packet
1058 if ( amountToMove > (int)(MAX_OBUF_BLOCK - sizeof(i2DataHeader)) ) {
1059 amountToMove = MAX_OBUF_BLOCK - sizeof(i2DataHeader);
1061 // Don't move more than the count we were given
1062 if (amountToMove > count) {
1063 amountToMove = count;
1065 // Now we know how much we must move: NB because the ring buffers have
1066 // an overflow area at the end, we needn't worry about wrapping in the
1067 // middle of a packet.
1069 // Small WINDOW here with no LOCK but I can't call Flush with LOCK
1070 // We would be flushing (or ending flush) anyway
1072 ip2trace (CHANN, ITRC_OUTPUT, 10, 1, amountToMove );
1074 if ( !(pCh->flush_flags && i2RetryFlushOutput(pCh) )
1075 && amountToMove > 0 )
1077 write_lock_irqsave(&pCh->Obuf_spinlock, flags);
1078 stuffIndex = pCh->Obuf_stuff;
1080 // Had room to move some data: don't know whether the block size,
1081 // buffer space, or what was the limiting factor...
1082 pInsert = &(pCh->Obuf[stuffIndex]);
1084 // Set up the header
1085 CHANNEL_OF(pInsert) = channel;
1086 PTYPE_OF(pInsert) = PTYPE_DATA;
1087 TAG_OF(pInsert) = 0;
1088 ID_OF(pInsert) = ID_ORDINARY_DATA;
1089 DATA_COUNT_OF(pInsert) = amountToMove;
1091 // Move the data
1092 memcpy( (char*)(DATA_OF(pInsert)), pSource, amountToMove );
1093 // Adjust pointers and indices
1094 pSource += amountToMove;
1095 pCh->Obuf_char_count += amountToMove;
1096 stuffIndex += amountToMove + sizeof(i2DataHeader);
1097 count -= amountToMove;
1099 if (stuffIndex >= OBUF_SIZE) {
1100 stuffIndex = 0;
1102 pCh->Obuf_stuff = stuffIndex;
1104 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1106 ip2trace (CHANN, ITRC_OUTPUT, 13, 1, stuffIndex );
1108 } else {
1110 // Cannot move data
1111 // becuz we need to stuff a flush
1112 // or amount to move is <= 0
1114 ip2trace(CHANN, ITRC_OUTPUT, 14, 3,
1115 amountToMove, pB->i2eFifoRemains,
1116 pB->i2eWaitingForEmptyFifo );
1118 // Put this channel back on queue
1119 // this ultimatly gets more data or wakes write output
1120 i2QueueNeeds(pB, pCh, NEED_INLINE);
1122 if ( pB->i2eWaitingForEmptyFifo ) {
1124 ip2trace (CHANN, ITRC_OUTPUT, 16, 0 );
1126 // or schedule
1127 if (!in_interrupt()) {
1129 ip2trace (CHANN, ITRC_OUTPUT, 61, 0 );
1131 schedule_timeout_interruptible(2);
1132 if (signal_pending(current)) {
1133 break;
1135 continue;
1136 } else {
1138 ip2trace (CHANN, ITRC_OUTPUT, 62, 0 );
1140 // let interrupt in = WAS restore_flags()
1141 // We hold no lock nor is irq off anymore???
1143 break;
1145 break; // from while(count)
1147 else if ( pB->i2eFifoRemains < 32 && !pB->i2eTxMailEmpty ( pB ) )
1149 ip2trace (CHANN, ITRC_OUTPUT, 19, 2,
1150 pB->i2eFifoRemains,
1151 pB->i2eTxMailEmpty );
1153 break; // from while(count)
1154 } else if ( pCh->channelNeeds & NEED_CREDIT ) {
1156 ip2trace (CHANN, ITRC_OUTPUT, 22, 0 );
1158 break; // from while(count)
1159 } else if ( --bailout) {
1161 // Try to throw more things (maybe not us) in the fifo if we're
1162 // not already waiting for it.
1164 ip2trace (CHANN, ITRC_OUTPUT, 20, 0 );
1166 serviceOutgoingFifo(pB);
1167 //break; CONTINUE;
1168 } else {
1169 ip2trace (CHANN, ITRC_OUTPUT, 21, 3,
1170 pB->i2eFifoRemains,
1171 pB->i2eOutMailWaiting,
1172 pB->i2eWaitingForEmptyFifo );
1174 break; // from while(count)
1177 } // End of while(count)
1179 i2QueueNeeds(pB, pCh, NEED_INLINE);
1181 // We drop through either when the count expires, or when there is some
1182 // count left, but there was a non-blocking write.
1183 if (countOriginal > count) {
1185 ip2trace (CHANN, ITRC_OUTPUT, 17, 2, countOriginal, count );
1187 serviceOutgoingFifo( pB );
1190 ip2trace (CHANN, ITRC_OUTPUT, ITRC_RETURN, 2, countOriginal, count );
1192 return countOriginal - count;
1195 //******************************************************************************
1196 // Function: i2FlushOutput(pCh)
1197 // Parameters: Pointer to a channel structure
1198 // Returns: Nothing
1200 // Description:
1201 // Sends bypass command to start flushing (waiting possibly forever until there
1202 // is room), then sends inline command to stop flushing output, (again waiting
1203 // possibly forever).
1204 //******************************************************************************
1205 static inline void
1206 i2FlushOutput(i2ChanStrPtr pCh)
1209 ip2trace (CHANN, ITRC_FLUSH, 1, 1, pCh->flush_flags );
1211 if (pCh->flush_flags)
1212 return;
1214 if ( 1 != i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
1215 pCh->flush_flags = STARTFL_FLAG; // Failed - flag for later
1217 ip2trace (CHANN, ITRC_FLUSH, 2, 0 );
1219 } else if ( 1 != i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL) ) {
1220 pCh->flush_flags = STOPFL_FLAG; // Failed - flag for later
1222 ip2trace (CHANN, ITRC_FLUSH, 3, 0 );
1226 static int
1227 i2RetryFlushOutput(i2ChanStrPtr pCh)
1229 int old_flags = pCh->flush_flags;
1231 ip2trace (CHANN, ITRC_FLUSH, 14, 1, old_flags );
1233 pCh->flush_flags = 0; // Clear flag so we can avoid recursion
1234 // and queue the commands
1236 if ( old_flags & STARTFL_FLAG ) {
1237 if ( 1 == i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
1238 old_flags = STOPFL_FLAG; //Success - send stop flush
1239 } else {
1240 old_flags = STARTFL_FLAG; //Failure - Flag for retry later
1243 ip2trace (CHANN, ITRC_FLUSH, 15, 1, old_flags );
1246 if ( old_flags & STOPFL_FLAG ) {
1247 if (1 == i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL)) {
1248 old_flags = 0; // Success - clear flags
1251 ip2trace (CHANN, ITRC_FLUSH, 16, 1, old_flags );
1253 pCh->flush_flags = old_flags;
1255 ip2trace (CHANN, ITRC_FLUSH, 17, 1, old_flags );
1257 return old_flags;
1260 //******************************************************************************
1261 // Function: i2DrainOutput(pCh,timeout)
1262 // Parameters: Pointer to a channel structure
1263 // Maximum period to wait
1264 // Returns: ?
1266 // Description:
1267 // Uses the bookmark request command to ask the board to send a bookmark back as
1268 // soon as all the data is completely sent.
1269 //******************************************************************************
1270 static void
1271 i2DrainWakeup(unsigned long d)
1273 i2ChanStrPtr pCh = (i2ChanStrPtr)d;
1275 ip2trace (CHANN, ITRC_DRAIN, 10, 1, pCh->BookmarkTimer.expires );
1277 pCh->BookmarkTimer.expires = 0;
1278 wake_up_interruptible( &pCh->pBookmarkWait );
1281 static void
1282 i2DrainOutput(i2ChanStrPtr pCh, int timeout)
1284 wait_queue_t wait;
1285 i2eBordStrPtr pB;
1287 ip2trace (CHANN, ITRC_DRAIN, ITRC_ENTER, 1, pCh->BookmarkTimer.expires);
1289 pB = pCh->pMyBord;
1290 // If the board has gone fatal, return bad,
1291 // and also hit the trap routine if it exists.
1292 if (pB->i2eFatal) {
1293 if (pB->i2eFatalTrap) {
1294 (*(pB)->i2eFatalTrap)(pB);
1296 return;
1298 if ((timeout > 0) && (pCh->BookmarkTimer.expires == 0 )) {
1299 // One per customer (channel)
1300 setup_timer(&pCh->BookmarkTimer, i2DrainWakeup,
1301 (unsigned long)pCh);
1303 ip2trace (CHANN, ITRC_DRAIN, 1, 1, pCh->BookmarkTimer.expires );
1305 mod_timer(&pCh->BookmarkTimer, jiffies + timeout);
1308 i2QueueCommands( PTYPE_INLINE, pCh, -1, 1, CMD_BMARK_REQ );
1310 init_waitqueue_entry(&wait, current);
1311 add_wait_queue(&(pCh->pBookmarkWait), &wait);
1312 set_current_state( TASK_INTERRUPTIBLE );
1314 serviceOutgoingFifo( pB );
1316 schedule(); // Now we take our interruptible sleep on
1318 // Clean up the queue
1319 set_current_state( TASK_RUNNING );
1320 remove_wait_queue(&(pCh->pBookmarkWait), &wait);
1322 // if expires == 0 then timer poped, then do not need to del_timer
1323 if ((timeout > 0) && pCh->BookmarkTimer.expires &&
1324 time_before(jiffies, pCh->BookmarkTimer.expires)) {
1325 del_timer( &(pCh->BookmarkTimer) );
1326 pCh->BookmarkTimer.expires = 0;
1328 ip2trace (CHANN, ITRC_DRAIN, 3, 1, pCh->BookmarkTimer.expires );
1331 ip2trace (CHANN, ITRC_DRAIN, ITRC_RETURN, 1, pCh->BookmarkTimer.expires );
1332 return;
1335 //******************************************************************************
1336 // Function: i2OutputFree(pCh)
1337 // Parameters: Pointer to a channel structure
1338 // Returns: Space in output buffer
1340 // Description:
1341 // Returns -1 if very gross error. Otherwise returns the amount of bytes still
1342 // free in the output buffer.
1343 //******************************************************************************
1344 static int
1345 i2OutputFree(i2ChanStrPtr pCh)
1347 int amountToMove;
1348 unsigned long flags;
1350 // Ensure channel structure seems real
1351 if ( !i2Validate ( pCh ) ) {
1352 return -1;
1354 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1355 amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
1356 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1358 if (amountToMove < 0) {
1359 amountToMove += OBUF_SIZE;
1361 // If this is negative, we will discover later
1362 amountToMove -= sizeof(i2DataHeader);
1364 return (amountToMove < 0) ? 0 : amountToMove;
1366 static void
1368 ip2_owake( PTTY tp)
1370 i2ChanStrPtr pCh;
1372 if (tp == NULL) return;
1374 pCh = tp->driver_data;
1376 ip2trace (CHANN, ITRC_SICMD, 10, 2, tp->flags,
1377 (1 << TTY_DO_WRITE_WAKEUP) );
1379 tty_wakeup(tp);
1382 static inline void
1383 set_baud_params(i2eBordStrPtr pB)
1385 int i,j;
1386 i2ChanStrPtr *pCh;
1388 pCh = (i2ChanStrPtr *) pB->i2eChannelPtr;
1390 for (i = 0; i < ABS_MAX_BOXES; i++) {
1391 if (pB->channelBtypes.bid_value[i]) {
1392 if (BID_HAS_654(pB->channelBtypes.bid_value[i])) {
1393 for (j = 0; j < ABS_BIGGEST_BOX; j++) {
1394 if (pCh[i*16+j] == NULL)
1395 break;
1396 (pCh[i*16+j])->BaudBase = 921600; // MAX for ST654
1397 (pCh[i*16+j])->BaudDivisor = 96;
1399 } else { // has cirrus cd1400
1400 for (j = 0; j < ABS_BIGGEST_BOX; j++) {
1401 if (pCh[i*16+j] == NULL)
1402 break;
1403 (pCh[i*16+j])->BaudBase = 115200; // MAX for CD1400
1404 (pCh[i*16+j])->BaudDivisor = 12;
1411 //******************************************************************************
1412 // Function: i2StripFifo(pB)
1413 // Parameters: Pointer to a board structure
1414 // Returns: ?
1416 // Description:
1417 // Strips all the available data from the incoming FIFO, identifies the type of
1418 // packet, and either buffers the data or does what needs to be done.
1420 // Note there is no overflow checking here: if the board sends more data than it
1421 // ought to, we will not detect it here, but blindly overflow...
1422 //******************************************************************************
1424 // A buffer for reading in blocks for unknown channels
1425 static unsigned char junkBuffer[IBUF_SIZE];
1427 // A buffer to read in a status packet. Because of the size of the count field
1428 // for these things, the maximum packet size must be less than MAX_CMD_PACK_SIZE
1429 static unsigned char cmdBuffer[MAX_CMD_PACK_SIZE + 4];
1431 // This table changes the bit order from MSR order given by STAT_MODEM packet to
1432 // status bits used in our library.
1433 static char xlatDss[16] = {
1434 0 | 0 | 0 | 0 ,
1435 0 | 0 | 0 | I2_CTS ,
1436 0 | 0 | I2_DSR | 0 ,
1437 0 | 0 | I2_DSR | I2_CTS ,
1438 0 | I2_RI | 0 | 0 ,
1439 0 | I2_RI | 0 | I2_CTS ,
1440 0 | I2_RI | I2_DSR | 0 ,
1441 0 | I2_RI | I2_DSR | I2_CTS ,
1442 I2_DCD | 0 | 0 | 0 ,
1443 I2_DCD | 0 | 0 | I2_CTS ,
1444 I2_DCD | 0 | I2_DSR | 0 ,
1445 I2_DCD | 0 | I2_DSR | I2_CTS ,
1446 I2_DCD | I2_RI | 0 | 0 ,
1447 I2_DCD | I2_RI | 0 | I2_CTS ,
1448 I2_DCD | I2_RI | I2_DSR | 0 ,
1449 I2_DCD | I2_RI | I2_DSR | I2_CTS };
1451 static inline void
1452 i2StripFifo(i2eBordStrPtr pB)
1454 i2ChanStrPtr pCh;
1455 int channel;
1456 int count;
1457 unsigned short stuffIndex;
1458 int amountToRead;
1459 unsigned char *pc, *pcLimit;
1460 unsigned char uc;
1461 unsigned char dss_change;
1462 unsigned long bflags,cflags;
1464 // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_ENTER, 0 );
1466 while (I2_HAS_INPUT(pB)) {
1467 // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 2, 0 );
1469 // Process packet from fifo a one atomic unit
1470 write_lock_irqsave(&pB->read_fifo_spinlock, bflags);
1472 // The first word (or two bytes) will have channel number and type of
1473 // packet, possibly other information
1474 pB->i2eLeadoffWord[0] = iiReadWord(pB);
1476 switch(PTYPE_OF(pB->i2eLeadoffWord))
1478 case PTYPE_DATA:
1479 pB->got_input = 1;
1481 // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 3, 0 );
1483 channel = CHANNEL_OF(pB->i2eLeadoffWord); /* Store channel */
1484 count = iiReadWord(pB); /* Count is in the next word */
1486 // NEW: Check the count for sanity! Should the hardware fail, our death
1487 // is more pleasant. While an oversize channel is acceptable (just more
1488 // than the driver supports), an over-length count clearly means we are
1489 // sick!
1490 if ( ((unsigned int)count) > IBUF_SIZE ) {
1491 pB->i2eFatal = 2;
1492 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1493 bflags);
1494 return; /* Bail out ASAP */
1496 // Channel is illegally big ?
1497 if ((channel >= pB->i2eChannelCnt) ||
1498 (NULL==(pCh = ((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])))
1500 iiReadBuf(pB, junkBuffer, count);
1501 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1502 bflags);
1503 break; /* From switch: ready for next packet */
1506 // Channel should be valid, then
1508 // If this is a hot-key, merely post its receipt for now. These are
1509 // always supposed to be 1-byte packets, so we won't even check the
1510 // count. Also we will post an acknowledgement to the board so that
1511 // more data can be forthcoming. Note that we are not trying to use
1512 // these sequences in this driver, merely to robustly ignore them.
1513 if(ID_OF(pB->i2eLeadoffWord) == ID_HOT_KEY)
1515 pCh->hotKeyIn = iiReadWord(pB) & 0xff;
1516 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1517 bflags);
1518 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_HOTACK);
1519 break; /* From the switch: ready for next packet */
1522 // Normal data! We crudely assume there is room for the data in our
1523 // buffer because the board wouldn't have exceeded his credit limit.
1524 write_lock_irqsave(&pCh->Ibuf_spinlock, cflags);
1525 // We have 2 locks now
1526 stuffIndex = pCh->Ibuf_stuff;
1527 amountToRead = IBUF_SIZE - stuffIndex;
1528 if (amountToRead > count)
1529 amountToRead = count;
1531 // stuffIndex would have been already adjusted so there would
1532 // always be room for at least one, and count is always at least
1533 // one.
1535 iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
1536 pCh->icount.rx += amountToRead;
1538 // Update the stuffIndex by the amount of data moved. Note we could
1539 // never ask for more data than would just fit. However, we might
1540 // have read in one more byte than we wanted because the read
1541 // rounds up to even bytes. If this byte is on the end of the
1542 // packet, and is padding, we ignore it. If the byte is part of
1543 // the actual data, we need to move it.
1545 stuffIndex += amountToRead;
1547 if (stuffIndex >= IBUF_SIZE) {
1548 if ((amountToRead & 1) && (count > amountToRead)) {
1549 pCh->Ibuf[0] = pCh->Ibuf[IBUF_SIZE];
1550 amountToRead++;
1551 stuffIndex = 1;
1552 } else {
1553 stuffIndex = 0;
1557 // If there is anything left over, read it as well
1558 if (count > amountToRead) {
1559 amountToRead = count - amountToRead;
1560 iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
1561 pCh->icount.rx += amountToRead;
1562 stuffIndex += amountToRead;
1565 // Update stuff index
1566 pCh->Ibuf_stuff = stuffIndex;
1567 write_unlock_irqrestore(&pCh->Ibuf_spinlock, cflags);
1568 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1569 bflags);
1571 #ifdef USE_IQ
1572 schedule_work(&pCh->tqueue_input);
1573 #else
1574 do_input(&pCh->tqueue_input);
1575 #endif
1577 // Note we do not need to maintain any flow-control credits at this
1578 // time: if we were to increment .asof and decrement .room, there
1579 // would be no net effect. Instead, when we strip data, we will
1580 // increment .asof and leave .room unchanged.
1582 break; // From switch: ready for next packet
1584 case PTYPE_STATUS:
1585 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 4, 0 );
1587 count = CMD_COUNT_OF(pB->i2eLeadoffWord);
1589 iiReadBuf(pB, cmdBuffer, count);
1590 // We can release early with buffer grab
1591 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1592 bflags);
1594 pc = cmdBuffer;
1595 pcLimit = &(cmdBuffer[count]);
1597 while (pc < pcLimit) {
1598 channel = *pc++;
1600 ip2trace (channel, ITRC_SFIFO, 7, 2, channel, *pc );
1602 /* check for valid channel */
1603 if (channel < pB->i2eChannelCnt
1605 (pCh = (((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])) != NULL
1608 dss_change = 0;
1610 switch (uc = *pc++)
1612 /* Breaks and modem signals are easy: just update status */
1613 case STAT_CTS_UP:
1614 if ( !(pCh->dataSetIn & I2_CTS) )
1616 pCh->dataSetIn |= I2_DCTS;
1617 pCh->icount.cts++;
1618 dss_change = 1;
1620 pCh->dataSetIn |= I2_CTS;
1621 break;
1623 case STAT_CTS_DN:
1624 if ( pCh->dataSetIn & I2_CTS )
1626 pCh->dataSetIn |= I2_DCTS;
1627 pCh->icount.cts++;
1628 dss_change = 1;
1630 pCh->dataSetIn &= ~I2_CTS;
1631 break;
1633 case STAT_DCD_UP:
1634 ip2trace (channel, ITRC_MODEM, 1, 1, pCh->dataSetIn );
1636 if ( !(pCh->dataSetIn & I2_DCD) )
1638 ip2trace (CHANN, ITRC_MODEM, 2, 0 );
1639 pCh->dataSetIn |= I2_DDCD;
1640 pCh->icount.dcd++;
1641 dss_change = 1;
1643 pCh->dataSetIn |= I2_DCD;
1645 ip2trace (channel, ITRC_MODEM, 3, 1, pCh->dataSetIn );
1646 break;
1648 case STAT_DCD_DN:
1649 ip2trace (channel, ITRC_MODEM, 4, 1, pCh->dataSetIn );
1650 if ( pCh->dataSetIn & I2_DCD )
1652 ip2trace (channel, ITRC_MODEM, 5, 0 );
1653 pCh->dataSetIn |= I2_DDCD;
1654 pCh->icount.dcd++;
1655 dss_change = 1;
1657 pCh->dataSetIn &= ~I2_DCD;
1659 ip2trace (channel, ITRC_MODEM, 6, 1, pCh->dataSetIn );
1660 break;
1662 case STAT_DSR_UP:
1663 if ( !(pCh->dataSetIn & I2_DSR) )
1665 pCh->dataSetIn |= I2_DDSR;
1666 pCh->icount.dsr++;
1667 dss_change = 1;
1669 pCh->dataSetIn |= I2_DSR;
1670 break;
1672 case STAT_DSR_DN:
1673 if ( pCh->dataSetIn & I2_DSR )
1675 pCh->dataSetIn |= I2_DDSR;
1676 pCh->icount.dsr++;
1677 dss_change = 1;
1679 pCh->dataSetIn &= ~I2_DSR;
1680 break;
1682 case STAT_RI_UP:
1683 if ( !(pCh->dataSetIn & I2_RI) )
1685 pCh->dataSetIn |= I2_DRI;
1686 pCh->icount.rng++;
1687 dss_change = 1;
1689 pCh->dataSetIn |= I2_RI ;
1690 break;
1692 case STAT_RI_DN:
1693 // to be compat with serial.c
1694 //if ( pCh->dataSetIn & I2_RI )
1696 // pCh->dataSetIn |= I2_DRI;
1697 // pCh->icount.rng++;
1698 // dss_change = 1;
1700 pCh->dataSetIn &= ~I2_RI ;
1701 break;
1703 case STAT_BRK_DET:
1704 pCh->dataSetIn |= I2_BRK;
1705 pCh->icount.brk++;
1706 dss_change = 1;
1707 break;
1709 // Bookmarks? one less request we're waiting for
1710 case STAT_BMARK:
1711 pCh->bookMarks--;
1712 if (pCh->bookMarks <= 0 ) {
1713 pCh->bookMarks = 0;
1714 wake_up_interruptible( &pCh->pBookmarkWait );
1716 ip2trace (channel, ITRC_DRAIN, 20, 1, pCh->BookmarkTimer.expires );
1718 break;
1720 // Flow control packets? Update the new credits, and if
1721 // someone was waiting for output, queue him up again.
1722 case STAT_FLOW:
1723 pCh->outfl.room =
1724 ((flowStatPtr)pc)->room -
1725 (pCh->outfl.asof - ((flowStatPtr)pc)->asof);
1727 ip2trace (channel, ITRC_STFLW, 1, 1, pCh->outfl.room );
1729 if (pCh->channelNeeds & NEED_CREDIT)
1731 ip2trace (channel, ITRC_STFLW, 2, 1, pCh->channelNeeds);
1733 pCh->channelNeeds &= ~NEED_CREDIT;
1734 i2QueueNeeds(pB, pCh, NEED_INLINE);
1735 if ( pCh->pTTY )
1736 ip2_owake(pCh->pTTY);
1739 ip2trace (channel, ITRC_STFLW, 3, 1, pCh->channelNeeds);
1741 pc += sizeof(flowStat);
1742 break;
1744 /* Special packets: */
1745 /* Just copy the information into the channel structure */
1747 case STAT_STATUS:
1749 pCh->channelStatus = *((debugStatPtr)pc);
1750 pc += sizeof(debugStat);
1751 break;
1753 case STAT_TXCNT:
1755 pCh->channelTcount = *((cntStatPtr)pc);
1756 pc += sizeof(cntStat);
1757 break;
1759 case STAT_RXCNT:
1761 pCh->channelRcount = *((cntStatPtr)pc);
1762 pc += sizeof(cntStat);
1763 break;
1765 case STAT_BOXIDS:
1766 pB->channelBtypes = *((bidStatPtr)pc);
1767 pc += sizeof(bidStat);
1768 set_baud_params(pB);
1769 break;
1771 case STAT_HWFAIL:
1772 i2QueueCommands (PTYPE_INLINE, pCh, 0, 1, CMD_HW_TEST);
1773 pCh->channelFail = *((failStatPtr)pc);
1774 pc += sizeof(failStat);
1775 break;
1777 /* No explicit match? then
1778 * Might be an error packet...
1780 default:
1781 switch (uc & STAT_MOD_ERROR)
1783 case STAT_ERROR:
1784 if (uc & STAT_E_PARITY) {
1785 pCh->dataSetIn |= I2_PAR;
1786 pCh->icount.parity++;
1788 if (uc & STAT_E_FRAMING){
1789 pCh->dataSetIn |= I2_FRA;
1790 pCh->icount.frame++;
1792 if (uc & STAT_E_OVERRUN){
1793 pCh->dataSetIn |= I2_OVR;
1794 pCh->icount.overrun++;
1796 break;
1798 case STAT_MODEM:
1799 // the answer to DSS_NOW request (not change)
1800 pCh->dataSetIn = (pCh->dataSetIn
1801 & ~(I2_RI | I2_CTS | I2_DCD | I2_DSR) )
1802 | xlatDss[uc & 0xf];
1803 wake_up_interruptible ( &pCh->dss_now_wait );
1804 default:
1805 break;
1807 } /* End of switch on status type */
1808 if (dss_change) {
1809 #ifdef USE_IQ
1810 schedule_work(&pCh->tqueue_status);
1811 #else
1812 do_status(&pCh->tqueue_status);
1813 #endif
1816 else /* Or else, channel is invalid */
1818 // Even though the channel is invalid, we must test the
1819 // status to see how much additional data it has (to be
1820 // skipped)
1821 switch (*pc++)
1823 case STAT_FLOW:
1824 pc += 4; /* Skip the data */
1825 break;
1827 default:
1828 break;
1831 } // End of while (there is still some status packet left)
1832 break;
1834 default: // Neither packet? should be impossible
1835 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 5, 1,
1836 PTYPE_OF(pB->i2eLeadoffWord) );
1837 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1838 bflags);
1840 break;
1841 } // End of switch on type of packets
1842 } /*while(board I2_HAS_INPUT)*/
1844 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_RETURN, 0 );
1846 // Send acknowledgement to the board even if there was no data!
1847 pB->i2eOutMailWaiting |= MB_IN_STRIPPED;
1848 return;
1851 //******************************************************************************
1852 // Function: i2Write2Fifo(pB,address,count)
1853 // Parameters: Pointer to a board structure, source address, byte count
1854 // Returns: bytes written
1856 // Description:
1857 // Writes count bytes to board io address(implied) from source
1858 // Adjusts count, leaves reserve for next time around bypass cmds
1859 //******************************************************************************
1860 static int
1861 i2Write2Fifo(i2eBordStrPtr pB, unsigned char *source, int count,int reserve)
1863 int rc = 0;
1864 unsigned long flags;
1865 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1866 if (!pB->i2eWaitingForEmptyFifo) {
1867 if (pB->i2eFifoRemains > (count+reserve)) {
1868 pB->i2eFifoRemains -= count;
1869 iiWriteBuf(pB, source, count);
1870 pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
1871 rc = count;
1874 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1875 return rc;
1877 //******************************************************************************
1878 // Function: i2StuffFifoBypass(pB)
1879 // Parameters: Pointer to a board structure
1880 // Returns: Nothing
1882 // Description:
1883 // Stuffs as many bypass commands into the fifo as possible. This is simpler
1884 // than stuffing data or inline commands to fifo, since we do not have
1885 // flow-control to deal with.
1886 //******************************************************************************
1887 static inline void
1888 i2StuffFifoBypass(i2eBordStrPtr pB)
1890 i2ChanStrPtr pCh;
1891 unsigned char *pRemove;
1892 unsigned short stripIndex;
1893 unsigned short packetSize;
1894 unsigned short paddedSize;
1895 unsigned short notClogged = 1;
1896 unsigned long flags;
1898 int bailout = 1000;
1900 // Continue processing so long as there are entries, or there is room in the
1901 // fifo. Each entry represents a channel with something to do.
1902 while ( --bailout && notClogged &&
1903 (NULL != (pCh = i2DeQueueNeeds(pB,NEED_BYPASS))))
1905 write_lock_irqsave(&pCh->Cbuf_spinlock, flags);
1906 stripIndex = pCh->Cbuf_strip;
1908 // as long as there are packets for this channel...
1910 while (stripIndex != pCh->Cbuf_stuff) {
1911 pRemove = &(pCh->Cbuf[stripIndex]);
1912 packetSize = CMD_COUNT_OF(pRemove) + sizeof(i2CmdHeader);
1913 paddedSize = roundup(packetSize, 2);
1915 if (paddedSize > 0) {
1916 if ( 0 == i2Write2Fifo(pB, pRemove, paddedSize,0)) {
1917 notClogged = 0; /* fifo full */
1918 i2QueueNeeds(pB, pCh, NEED_BYPASS); // Put back on queue
1919 break; // Break from the channel
1922 #ifdef DEBUG_FIFO
1923 WriteDBGBuf("BYPS", pRemove, paddedSize);
1924 #endif /* DEBUG_FIFO */
1925 pB->debugBypassCount++;
1927 pRemove += packetSize;
1928 stripIndex += packetSize;
1929 if (stripIndex >= CBUF_SIZE) {
1930 stripIndex = 0;
1931 pRemove = pCh->Cbuf;
1934 // Done with this channel. Move to next, removing this one from
1935 // the queue of channels if we cleaned it out (i.e., didn't get clogged.
1936 pCh->Cbuf_strip = stripIndex;
1937 write_unlock_irqrestore(&pCh->Cbuf_spinlock, flags);
1938 } // Either clogged or finished all the work
1940 #ifdef IP2DEBUG_TRACE
1941 if ( !bailout ) {
1942 ip2trace (ITRC_NO_PORT, ITRC_ERROR, 1, 0 );
1944 #endif
1947 //******************************************************************************
1948 // Function: i2StuffFifoFlow(pB)
1949 // Parameters: Pointer to a board structure
1950 // Returns: Nothing
1952 // Description:
1953 // Stuffs as many flow control packets into the fifo as possible. This is easier
1954 // even than doing normal bypass commands, because there is always at most one
1955 // packet, already assembled, for each channel.
1956 //******************************************************************************
1957 static inline void
1958 i2StuffFifoFlow(i2eBordStrPtr pB)
1960 i2ChanStrPtr pCh;
1961 unsigned short paddedSize = roundup(sizeof(flowIn), 2);
1963 ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_ENTER, 2,
1964 pB->i2eFifoRemains, paddedSize );
1966 // Continue processing so long as there are entries, or there is room in the
1967 // fifo. Each entry represents a channel with something to do.
1968 while ( (NULL != (pCh = i2DeQueueNeeds(pB,NEED_FLOW)))) {
1969 pB->debugFlowCount++;
1971 // NO Chan LOCK needed ???
1972 if ( 0 == i2Write2Fifo(pB,(unsigned char *)&(pCh->infl),paddedSize,0)) {
1973 break;
1975 #ifdef DEBUG_FIFO
1976 WriteDBGBuf("FLOW",(unsigned char *) &(pCh->infl), paddedSize);
1977 #endif /* DEBUG_FIFO */
1979 } // Either clogged or finished all the work
1981 ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_RETURN, 0 );
1984 //******************************************************************************
1985 // Function: i2StuffFifoInline(pB)
1986 // Parameters: Pointer to a board structure
1987 // Returns: Nothing
1989 // Description:
1990 // Stuffs as much data and inline commands into the fifo as possible. This is
1991 // the most complex fifo-stuffing operation, since there if now the channel
1992 // flow-control issue to deal with.
1993 //******************************************************************************
1994 static inline void
1995 i2StuffFifoInline(i2eBordStrPtr pB)
1997 i2ChanStrPtr pCh;
1998 unsigned char *pRemove;
1999 unsigned short stripIndex;
2000 unsigned short packetSize;
2001 unsigned short paddedSize;
2002 unsigned short notClogged = 1;
2003 unsigned short flowsize;
2004 unsigned long flags;
2006 int bailout = 1000;
2007 int bailout2;
2009 ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_ENTER, 3, pB->i2eFifoRemains,
2010 pB->i2Dbuf_strip, pB->i2Dbuf_stuff );
2012 // Continue processing so long as there are entries, or there is room in the
2013 // fifo. Each entry represents a channel with something to do.
2014 while ( --bailout && notClogged &&
2015 (NULL != (pCh = i2DeQueueNeeds(pB,NEED_INLINE))) )
2017 write_lock_irqsave(&pCh->Obuf_spinlock, flags);
2018 stripIndex = pCh->Obuf_strip;
2020 ip2trace (CHANN, ITRC_SICMD, 3, 2, stripIndex, pCh->Obuf_stuff );
2022 // as long as there are packets for this channel...
2023 bailout2 = 1000;
2024 while ( --bailout2 && stripIndex != pCh->Obuf_stuff) {
2025 pRemove = &(pCh->Obuf[stripIndex]);
2027 // Must determine whether this be a data or command packet to
2028 // calculate correctly the header size and the amount of
2029 // flow-control credit this type of packet will use.
2030 if (PTYPE_OF(pRemove) == PTYPE_DATA) {
2031 flowsize = DATA_COUNT_OF(pRemove);
2032 packetSize = flowsize + sizeof(i2DataHeader);
2033 } else {
2034 flowsize = CMD_COUNT_OF(pRemove);
2035 packetSize = flowsize + sizeof(i2CmdHeader);
2037 flowsize = CREDIT_USAGE(flowsize);
2038 paddedSize = roundup(packetSize, 2);
2040 ip2trace (CHANN, ITRC_SICMD, 4, 2, pB->i2eFifoRemains, paddedSize );
2042 // If we don't have enough credits from the board to send the data,
2043 // flag the channel that we are waiting for flow control credit, and
2044 // break out. This will clean up this channel and remove us from the
2045 // queue of hot things to do.
2047 ip2trace (CHANN, ITRC_SICMD, 5, 2, pCh->outfl.room, flowsize );
2049 if (pCh->outfl.room <= flowsize) {
2050 // Do Not have the credits to send this packet.
2051 i2QueueNeeds(pB, pCh, NEED_CREDIT);
2052 notClogged = 0;
2053 break; // So to do next channel
2055 if ( (paddedSize > 0)
2056 && ( 0 == i2Write2Fifo(pB, pRemove, paddedSize, 128))) {
2057 // Do Not have room in fifo to send this packet.
2058 notClogged = 0;
2059 i2QueueNeeds(pB, pCh, NEED_INLINE);
2060 break; // Break from the channel
2062 #ifdef DEBUG_FIFO
2063 WriteDBGBuf("DATA", pRemove, paddedSize);
2064 #endif /* DEBUG_FIFO */
2065 pB->debugInlineCount++;
2067 pCh->icount.tx += flowsize;
2068 // Update current credits
2069 pCh->outfl.room -= flowsize;
2070 pCh->outfl.asof += flowsize;
2071 if (PTYPE_OF(pRemove) == PTYPE_DATA) {
2072 pCh->Obuf_char_count -= DATA_COUNT_OF(pRemove);
2074 pRemove += packetSize;
2075 stripIndex += packetSize;
2077 ip2trace (CHANN, ITRC_SICMD, 6, 2, stripIndex, pCh->Obuf_strip);
2079 if (stripIndex >= OBUF_SIZE) {
2080 stripIndex = 0;
2081 pRemove = pCh->Obuf;
2083 ip2trace (CHANN, ITRC_SICMD, 7, 1, stripIndex );
2086 } /* while */
2087 if ( !bailout2 ) {
2088 ip2trace (CHANN, ITRC_ERROR, 3, 0 );
2090 // Done with this channel. Move to next, removing this one from the
2091 // queue of channels if we cleaned it out (i.e., didn't get clogged.
2092 pCh->Obuf_strip = stripIndex;
2093 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
2094 if ( notClogged )
2097 ip2trace (CHANN, ITRC_SICMD, 8, 0 );
2099 if ( pCh->pTTY ) {
2100 ip2_owake(pCh->pTTY);
2103 } // Either clogged or finished all the work
2105 if ( !bailout ) {
2106 ip2trace (ITRC_NO_PORT, ITRC_ERROR, 4, 0 );
2109 ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_RETURN, 1,pB->i2Dbuf_strip);
2112 //******************************************************************************
2113 // Function: serviceOutgoingFifo(pB)
2114 // Parameters: Pointer to a board structure
2115 // Returns: Nothing
2117 // Description:
2118 // Helper routine to put data in the outgoing fifo, if we aren't already waiting
2119 // for something to be there. If the fifo has only room for a very little data,
2120 // go head and hit the board with a mailbox hit immediately. Otherwise, it will
2121 // have to happen later in the interrupt processing. Since this routine may be
2122 // called both at interrupt and foreground time, we must turn off interrupts
2123 // during the entire process.
2124 //******************************************************************************
2125 static void
2126 serviceOutgoingFifo(i2eBordStrPtr pB)
2128 // If we aren't currently waiting for the board to empty our fifo, service
2129 // everything that is pending, in priority order (especially, Bypass before
2130 // Inline).
2131 if ( ! pB->i2eWaitingForEmptyFifo )
2133 i2StuffFifoFlow(pB);
2134 i2StuffFifoBypass(pB);
2135 i2StuffFifoInline(pB);
2137 iiSendPendingMail(pB);
2141 //******************************************************************************
2142 // Function: i2ServiceBoard(pB)
2143 // Parameters: Pointer to a board structure
2144 // Returns: Nothing
2146 // Description:
2147 // Normally this is called from interrupt level, but there is deliberately
2148 // nothing in here specific to being called from interrupt level. All the
2149 // hardware-specific, interrupt-specific things happen at the outer levels.
2151 // For example, a timer interrupt could drive this routine for some sort of
2152 // polled operation. The only requirement is that the programmer deal with any
2153 // atomiticity/concurrency issues that result.
2155 // This routine responds to the board's having sent mailbox information to the
2156 // host (which would normally cause an interrupt). This routine reads the
2157 // incoming mailbox. If there is no data in it, this board did not create the
2158 // interrupt and/or has nothing to be done to it. (Except, if we have been
2159 // waiting to write mailbox data to it, we may do so.
2161 // Based on the value in the mailbox, we may take various actions.
2163 // No checking here of pB validity: after all, it shouldn't have been called by
2164 // the handler unless pB were on the list.
2165 //******************************************************************************
2166 static inline int
2167 i2ServiceBoard ( i2eBordStrPtr pB )
2169 unsigned inmail;
2170 unsigned long flags;
2173 /* This should be atomic because of the way we are called... */
2174 if (NO_MAIL_HERE == ( inmail = pB->i2eStartMail ) ) {
2175 inmail = iiGetMail(pB);
2177 pB->i2eStartMail = NO_MAIL_HERE;
2179 ip2trace (ITRC_NO_PORT, ITRC_INTR, 2, 1, inmail );
2181 if (inmail != NO_MAIL_HERE) {
2182 // If the board has gone fatal, nothing to do but hit a bit that will
2183 // alert foreground tasks to protest!
2184 if ( inmail & MB_FATAL_ERROR ) {
2185 pB->i2eFatal = 1;
2186 goto exit_i2ServiceBoard;
2189 /* Assuming no fatal condition, we proceed to do work */
2190 if ( inmail & MB_IN_STUFFED ) {
2191 pB->i2eFifoInInts++;
2192 i2StripFifo(pB); /* There might be incoming packets */
2195 if (inmail & MB_OUT_STRIPPED) {
2196 pB->i2eFifoOutInts++;
2197 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
2198 pB->i2eFifoRemains = pB->i2eFifoSize;
2199 pB->i2eWaitingForEmptyFifo = 0;
2200 write_unlock_irqrestore(&pB->write_fifo_spinlock,
2201 flags);
2203 ip2trace (ITRC_NO_PORT, ITRC_INTR, 30, 1, pB->i2eFifoRemains );
2206 serviceOutgoingFifo(pB);
2209 ip2trace (ITRC_NO_PORT, ITRC_INTR, 8, 0 );
2211 exit_i2ServiceBoard:
2213 return 0;