PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/char/ip2/i2lib.c

https://bitbucket.org/evzijst/gittest
C | 2219 lines | 1324 code | 338 blank | 557 comment | 236 complexity | a60aea5df7e3c384238c5cd0c46ed4aa MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*******************************************************************************
  2. *
  3. * (c) 1999 by Computone Corporation
  4. *
  5. ********************************************************************************
  6. *
  7. *
  8. * PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
  9. * serial I/O controllers.
  10. *
  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.)
  16. *
  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).
  25. //
  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. //------------------------------------------------------------------------------
  30. //------------------------------------------------------------------------------
  31. // Revision History:
  32. //
  33. // 0.00 - 4/16/91 --- First Draft
  34. // 0.01 - 4/29/91 --- 1st beta release
  35. // 0.02 - 6/14/91 --- Changes to allow small model compilation
  36. // 0.03 - 6/17/91 MAG Break reporting protected from interrupts routines with
  37. // in-line asm added for moving data to/from ring buffers,
  38. // replacing a variety of methods used previously.
  39. // 0.04 - 6/21/91 MAG Initial flow-control packets not queued until
  40. // i2_enable_interrupts time. Former versions would enqueue
  41. // them at i2_init_channel time, before we knew how many
  42. // channels were supposed to exist!
  43. // 0.05 - 10/12/91 MAG Major changes: works through the ellis.c routines now;
  44. // supports new 16-bit protocol and expandable boards.
  45. // - 10/24/91 MAG Most changes in place and stable.
  46. // 0.06 - 2/20/92 MAG Format of CMD_HOTACK corrected: the command takes no
  47. // argument.
  48. // 0.07 -- 3/11/92 MAG Support added to store special packet types at interrupt
  49. // level (mostly responses to specific commands.)
  50. // 0.08 -- 3/30/92 MAG Support added for STAT_MODEM packet
  51. // 0.09 -- 6/24/93 MAG i2Link... needed to update number of boards BEFORE
  52. // turning on the interrupt.
  53. // 0.10 -- 6/25/93 MAG To avoid gruesome death from a bad board, we sanity check
  54. // some incoming.
  55. //
  56. // 1.1 - 12/25/96 AKM Linux version.
  57. // - 10/09/98 DMC Revised Linux version.
  58. //------------------------------------------------------------------------------
  59. //************
  60. //* Includes *
  61. //************
  62. #include <linux/sched.h>
  63. #include "i2lib.h"
  64. //***********************
  65. //* Function Prototypes *
  66. //***********************
  67. static void i2QueueNeeds(i2eBordStrPtr, i2ChanStrPtr, int);
  68. static i2ChanStrPtr i2DeQueueNeeds(i2eBordStrPtr, int );
  69. static void i2StripFifo(i2eBordStrPtr);
  70. static void i2StuffFifoBypass(i2eBordStrPtr);
  71. static void i2StuffFifoFlow(i2eBordStrPtr);
  72. static void i2StuffFifoInline(i2eBordStrPtr);
  73. static int i2RetryFlushOutput(i2ChanStrPtr);
  74. // Not a documented part of the library routines (careful...) but the Diagnostic
  75. // i2diag.c finds them useful to help the throughput in certain limited
  76. // single-threaded operations.
  77. static void iiSendPendingMail(i2eBordStrPtr);
  78. static void serviceOutgoingFifo(i2eBordStrPtr);
  79. // Functions defined in ip2.c as part of interrupt handling
  80. static void do_input(void *);
  81. static void do_status(void *);
  82. //***************
  83. //* Debug Data *
  84. //***************
  85. #ifdef DEBUG_FIFO
  86. unsigned char DBGBuf[0x4000];
  87. unsigned short I = 0;
  88. static void
  89. WriteDBGBuf(char *s, unsigned char *src, unsigned short n )
  90. {
  91. char *p = src;
  92. // XXX: We need a spin lock here if we ever use this again
  93. while (*s) { // copy label
  94. DBGBuf[I] = *s++;
  95. I = I++ & 0x3fff;
  96. }
  97. while (n--) { // copy data
  98. DBGBuf[I] = *p++;
  99. I = I++ & 0x3fff;
  100. }
  101. }
  102. static void
  103. fatality(i2eBordStrPtr pB )
  104. {
  105. int i;
  106. for (i=0;i<sizeof(DBGBuf);i++) {
  107. if ((i%16) == 0)
  108. printk("\n%4x:",i);
  109. printk("%02x ",DBGBuf[i]);
  110. }
  111. printk("\n");
  112. for (i=0;i<sizeof(DBGBuf);i++) {
  113. if ((i%16) == 0)
  114. printk("\n%4x:",i);
  115. if (DBGBuf[i] >= ' ' && DBGBuf[i] <= '~') {
  116. printk(" %c ",DBGBuf[i]);
  117. } else {
  118. printk(" . ");
  119. }
  120. }
  121. printk("\n");
  122. printk("Last index %x\n",I);
  123. }
  124. #endif /* DEBUG_FIFO */
  125. //********
  126. //* Code *
  127. //********
  128. static inline int
  129. i2Validate ( i2ChanStrPtr pCh )
  130. {
  131. //ip2trace(pCh->port_index, ITRC_VERIFY,ITRC_ENTER,2,pCh->validity,
  132. // (CHANNEL_MAGIC | CHANNEL_SUPPORT));
  133. return ((pCh->validity & (CHANNEL_MAGIC_BITS | CHANNEL_SUPPORT))
  134. == (CHANNEL_MAGIC | CHANNEL_SUPPORT));
  135. }
  136. //******************************************************************************
  137. // Function: iiSendPendingMail(pB)
  138. // Parameters: Pointer to a board structure
  139. // Returns: Nothing
  140. //
  141. // Description:
  142. // If any outgoing mail bits are set and there is outgoing mailbox is empty,
  143. // send the mail and clear the bits.
  144. //******************************************************************************
  145. static inline void
  146. iiSendPendingMail(i2eBordStrPtr pB)
  147. {
  148. if (pB->i2eOutMailWaiting && (!pB->i2eWaitingForEmptyFifo) )
  149. {
  150. if (iiTrySendMail(pB, pB->i2eOutMailWaiting))
  151. {
  152. /* If we were already waiting for fifo to empty,
  153. * or just sent MB_OUT_STUFFED, then we are
  154. * still waiting for it to empty, until we should
  155. * receive an MB_IN_STRIPPED from the board.
  156. */
  157. pB->i2eWaitingForEmptyFifo |=
  158. (pB->i2eOutMailWaiting & MB_OUT_STUFFED);
  159. pB->i2eOutMailWaiting = 0;
  160. pB->SendPendingRetry = 0;
  161. } else {
  162. /* The only time we hit this area is when "iiTrySendMail" has
  163. failed. That only occurs when the outbound mailbox is
  164. still busy with the last message. We take a short breather
  165. to let the board catch up with itself and then try again.
  166. 16 Retries is the limit - then we got a borked board.
  167. /\/\|=mhw=|\/\/ */
  168. if( ++pB->SendPendingRetry < 16 ) {
  169. init_timer( &(pB->SendPendingTimer) );
  170. pB->SendPendingTimer.expires = jiffies + 1;
  171. pB->SendPendingTimer.function = (void*)(unsigned long)iiSendPendingMail;
  172. pB->SendPendingTimer.data = (unsigned long)pB;
  173. add_timer( &(pB->SendPendingTimer) );
  174. } else {
  175. printk( KERN_ERR "IP2: iiSendPendingMail unable to queue outbound mail\n" );
  176. }
  177. }
  178. }
  179. }
  180. //******************************************************************************
  181. // Function: i2InitChannels(pB, nChannels, pCh)
  182. // Parameters: Pointer to Ellis Board structure
  183. // Number of channels to initialize
  184. // Pointer to first element in an array of channel structures
  185. // Returns: Success or failure
  186. //
  187. // Description:
  188. //
  189. // This function patches pointers, back-pointers, and initializes all the
  190. // elements in the channel structure array.
  191. //
  192. // This should be run after the board structure is initialized, through having
  193. // loaded the standard loadware (otherwise it complains).
  194. //
  195. // In any case, it must be done before any serious work begins initializing the
  196. // irq's or sending commands...
  197. //
  198. //******************************************************************************
  199. static int
  200. i2InitChannels ( i2eBordStrPtr pB, int nChannels, i2ChanStrPtr pCh)
  201. {
  202. int index, stuffIndex;
  203. i2ChanStrPtr *ppCh;
  204. if (pB->i2eValid != I2E_MAGIC) {
  205. COMPLETE(pB, I2EE_BADMAGIC);
  206. }
  207. if (pB->i2eState != II_STATE_STDLOADED) {
  208. COMPLETE(pB, I2EE_BADSTATE);
  209. }
  210. LOCK_INIT(&pB->read_fifo_spinlock);
  211. LOCK_INIT(&pB->write_fifo_spinlock);
  212. LOCK_INIT(&pB->Dbuf_spinlock);
  213. LOCK_INIT(&pB->Bbuf_spinlock);
  214. LOCK_INIT(&pB->Fbuf_spinlock);
  215. // NO LOCK needed yet - this is init
  216. pB->i2eChannelPtr = pCh;
  217. pB->i2eChannelCnt = nChannels;
  218. pB->i2Fbuf_strip = pB->i2Fbuf_stuff = 0;
  219. pB->i2Dbuf_strip = pB->i2Dbuf_stuff = 0;
  220. pB->i2Bbuf_strip = pB->i2Bbuf_stuff = 0;
  221. pB->SendPendingRetry = 0;
  222. memset ( pCh, 0, sizeof (i2ChanStr) * nChannels );
  223. for (index = stuffIndex = 0, ppCh = (i2ChanStrPtr *)(pB->i2Fbuf);
  224. nChannels && index < ABS_MOST_PORTS;
  225. index++)
  226. {
  227. if ( !(pB->i2eChannelMap[index >> 4] & (1 << (index & 0xf)) ) ) {
  228. continue;
  229. }
  230. LOCK_INIT(&pCh->Ibuf_spinlock);
  231. LOCK_INIT(&pCh->Obuf_spinlock);
  232. LOCK_INIT(&pCh->Cbuf_spinlock);
  233. LOCK_INIT(&pCh->Pbuf_spinlock);
  234. // NO LOCK needed yet - this is init
  235. // Set up validity flag according to support level
  236. if (pB->i2eGoodMap[index >> 4] & (1 << (index & 0xf)) ) {
  237. pCh->validity = CHANNEL_MAGIC | CHANNEL_SUPPORT;
  238. } else {
  239. pCh->validity = CHANNEL_MAGIC;
  240. }
  241. pCh->pMyBord = pB; /* Back-pointer */
  242. // Prepare an outgoing flow-control packet to send as soon as the chance
  243. // occurs.
  244. if ( pCh->validity & CHANNEL_SUPPORT ) {
  245. pCh->infl.hd.i2sChannel = index;
  246. pCh->infl.hd.i2sCount = 5;
  247. pCh->infl.hd.i2sType = PTYPE_BYPASS;
  248. pCh->infl.fcmd = 37;
  249. pCh->infl.asof = 0;
  250. pCh->infl.room = IBUF_SIZE - 1;
  251. pCh->whenSendFlow = (IBUF_SIZE/5)*4; // when 80% full
  252. // The following is similar to calling i2QueueNeeds, except that this
  253. // is done in longhand, since we are setting up initial conditions on
  254. // many channels at once.
  255. pCh->channelNeeds = NEED_FLOW; // Since starting from scratch
  256. pCh->sinceLastFlow = 0; // No bytes received since last flow
  257. // control packet was queued
  258. stuffIndex++;
  259. *ppCh++ = pCh; // List this channel as needing
  260. // initial flow control packet sent
  261. }
  262. // Don't allow anything to be sent until the status packets come in from
  263. // the board.
  264. pCh->outfl.asof = 0;
  265. pCh->outfl.room = 0;
  266. // Initialize all the ring buffers
  267. pCh->Ibuf_stuff = pCh->Ibuf_strip = 0;
  268. pCh->Obuf_stuff = pCh->Obuf_strip = 0;
  269. pCh->Cbuf_stuff = pCh->Cbuf_strip = 0;
  270. memset( &pCh->icount, 0, sizeof (struct async_icount) );
  271. pCh->hotKeyIn = HOT_CLEAR;
  272. pCh->channelOptions = 0;
  273. pCh->bookMarks = 0;
  274. init_waitqueue_head(&pCh->pBookmarkWait);
  275. init_waitqueue_head(&pCh->open_wait);
  276. init_waitqueue_head(&pCh->close_wait);
  277. init_waitqueue_head(&pCh->delta_msr_wait);
  278. // Set base and divisor so default custom rate is 9600
  279. pCh->BaudBase = 921600; // MAX for ST654, changed after we get
  280. pCh->BaudDivisor = 96; // the boxids (UART types) later
  281. pCh->dataSetIn = 0;
  282. pCh->dataSetOut = 0;
  283. pCh->wopen = 0;
  284. pCh->throttled = 0;
  285. pCh->speed = CBR_9600;
  286. pCh->flags = 0;
  287. pCh->ClosingDelay = 5*HZ/10;
  288. pCh->ClosingWaitTime = 30*HZ;
  289. // Initialize task queue objects
  290. INIT_WORK(&pCh->tqueue_input, do_input, pCh);
  291. INIT_WORK(&pCh->tqueue_status, do_status, pCh);
  292. #ifdef IP2DEBUG_TRACE
  293. pCh->trace = ip2trace;
  294. #endif
  295. ++pCh;
  296. --nChannels;
  297. }
  298. // No need to check for wrap here; this is initialization.
  299. pB->i2Fbuf_stuff = stuffIndex;
  300. COMPLETE(pB, I2EE_GOOD);
  301. }
  302. //******************************************************************************
  303. // Function: i2DeQueueNeeds(pB, type)
  304. // Parameters: Pointer to a board structure
  305. // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
  306. // Returns:
  307. // Pointer to a channel structure
  308. //
  309. // Description: Returns pointer struct of next channel that needs service of
  310. // the type specified. Otherwise returns a NULL reference.
  311. //
  312. //******************************************************************************
  313. static i2ChanStrPtr
  314. i2DeQueueNeeds(i2eBordStrPtr pB, int type)
  315. {
  316. unsigned short queueIndex;
  317. unsigned long flags;
  318. i2ChanStrPtr pCh = NULL;
  319. switch(type) {
  320. case NEED_INLINE:
  321. WRITE_LOCK_IRQSAVE(&pB->Dbuf_spinlock,flags);
  322. if ( pB->i2Dbuf_stuff != pB->i2Dbuf_strip)
  323. {
  324. queueIndex = pB->i2Dbuf_strip;
  325. pCh = pB->i2Dbuf[queueIndex];
  326. queueIndex++;
  327. if (queueIndex >= CH_QUEUE_SIZE) {
  328. queueIndex = 0;
  329. }
  330. pB->i2Dbuf_strip = queueIndex;
  331. pCh->channelNeeds &= ~NEED_INLINE;
  332. }
  333. WRITE_UNLOCK_IRQRESTORE(&pB->Dbuf_spinlock,flags);
  334. break;
  335. case NEED_BYPASS:
  336. WRITE_LOCK_IRQSAVE(&pB->Bbuf_spinlock,flags);
  337. if (pB->i2Bbuf_stuff != pB->i2Bbuf_strip)
  338. {
  339. queueIndex = pB->i2Bbuf_strip;
  340. pCh = pB->i2Bbuf[queueIndex];
  341. queueIndex++;
  342. if (queueIndex >= CH_QUEUE_SIZE) {
  343. queueIndex = 0;
  344. }
  345. pB->i2Bbuf_strip = queueIndex;
  346. pCh->channelNeeds &= ~NEED_BYPASS;
  347. }
  348. WRITE_UNLOCK_IRQRESTORE(&pB->Bbuf_spinlock,flags);
  349. break;
  350. case NEED_FLOW:
  351. WRITE_LOCK_IRQSAVE(&pB->Fbuf_spinlock,flags);
  352. if (pB->i2Fbuf_stuff != pB->i2Fbuf_strip)
  353. {
  354. queueIndex = pB->i2Fbuf_strip;
  355. pCh = pB->i2Fbuf[queueIndex];
  356. queueIndex++;
  357. if (queueIndex >= CH_QUEUE_SIZE) {
  358. queueIndex = 0;
  359. }
  360. pB->i2Fbuf_strip = queueIndex;
  361. pCh->channelNeeds &= ~NEED_FLOW;
  362. }
  363. WRITE_UNLOCK_IRQRESTORE(&pB->Fbuf_spinlock,flags);
  364. break;
  365. default:
  366. printk(KERN_ERR "i2DeQueueNeeds called with bad type:%x\n",type);
  367. break;
  368. }
  369. return pCh;
  370. }
  371. //******************************************************************************
  372. // Function: i2QueueNeeds(pB, pCh, type)
  373. // Parameters: Pointer to a board structure
  374. // Pointer to a channel structure
  375. // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
  376. // Returns: Nothing
  377. //
  378. // Description:
  379. // For each type of need selected, if the given channel is not already in the
  380. // queue, adds it, and sets the flag indicating it is in the queue.
  381. //******************************************************************************
  382. static void
  383. i2QueueNeeds(i2eBordStrPtr pB, i2ChanStrPtr pCh, int type)
  384. {
  385. unsigned short queueIndex;
  386. unsigned long flags;
  387. // We turn off all the interrupts during this brief process, since the
  388. // interrupt-level code might want to put things on the queue as well.
  389. switch (type) {
  390. case NEED_INLINE:
  391. WRITE_LOCK_IRQSAVE(&pB->Dbuf_spinlock,flags);
  392. if ( !(pCh->channelNeeds & NEED_INLINE) )
  393. {
  394. pCh->channelNeeds |= NEED_INLINE;
  395. queueIndex = pB->i2Dbuf_stuff;
  396. pB->i2Dbuf[queueIndex++] = pCh;
  397. if (queueIndex >= CH_QUEUE_SIZE)
  398. queueIndex = 0;
  399. pB->i2Dbuf_stuff = queueIndex;
  400. }
  401. WRITE_UNLOCK_IRQRESTORE(&pB->Dbuf_spinlock,flags);
  402. break;
  403. case NEED_BYPASS:
  404. WRITE_LOCK_IRQSAVE(&pB->Bbuf_spinlock,flags);
  405. if ((type & NEED_BYPASS) && !(pCh->channelNeeds & NEED_BYPASS))
  406. {
  407. pCh->channelNeeds |= NEED_BYPASS;
  408. queueIndex = pB->i2Bbuf_stuff;
  409. pB->i2Bbuf[queueIndex++] = pCh;
  410. if (queueIndex >= CH_QUEUE_SIZE)
  411. queueIndex = 0;
  412. pB->i2Bbuf_stuff = queueIndex;
  413. }
  414. WRITE_UNLOCK_IRQRESTORE(&pB->Bbuf_spinlock,flags);
  415. break;
  416. case NEED_FLOW:
  417. WRITE_LOCK_IRQSAVE(&pB->Fbuf_spinlock,flags);
  418. if ((type & NEED_FLOW) && !(pCh->channelNeeds & NEED_FLOW))
  419. {
  420. pCh->channelNeeds |= NEED_FLOW;
  421. queueIndex = pB->i2Fbuf_stuff;
  422. pB->i2Fbuf[queueIndex++] = pCh;
  423. if (queueIndex >= CH_QUEUE_SIZE)
  424. queueIndex = 0;
  425. pB->i2Fbuf_stuff = queueIndex;
  426. }
  427. WRITE_UNLOCK_IRQRESTORE(&pB->Fbuf_spinlock,flags);
  428. break;
  429. case NEED_CREDIT:
  430. pCh->channelNeeds |= NEED_CREDIT;
  431. break;
  432. default:
  433. printk(KERN_ERR "i2QueueNeeds called with bad type:%x\n",type);
  434. break;
  435. }
  436. return;
  437. }
  438. //******************************************************************************
  439. // Function: i2QueueCommands(type, pCh, timeout, nCommands, pCs,...)
  440. // Parameters: type - PTYPE_BYPASS or PTYPE_INLINE
  441. // pointer to the channel structure
  442. // maximum period to wait
  443. // number of commands (n)
  444. // n commands
  445. // Returns: Number of commands sent, or -1 for error
  446. //
  447. // get board lock before calling
  448. //
  449. // Description:
  450. // Queues up some commands to be sent to a channel. To send possibly several
  451. // bypass or inline commands to the given channel. The timeout parameter
  452. // indicates how many HUNDREDTHS OF SECONDS to wait until there is room:
  453. // 0 = return immediately if no room, -ive = wait forever, +ive = number of
  454. // 1/100 seconds to wait. Return values:
  455. // -1 Some kind of nasty error: bad channel structure or invalid arguments.
  456. // 0 No room to send all the commands
  457. // (+) Number of commands sent
  458. //******************************************************************************
  459. static int
  460. i2QueueCommands(int type, i2ChanStrPtr pCh, int timeout, int nCommands,
  461. cmdSyntaxPtr pCs0,...)
  462. {
  463. int totalsize = 0;
  464. int blocksize;
  465. int lastended;
  466. cmdSyntaxPtr *ppCs;
  467. cmdSyntaxPtr pCs;
  468. int count;
  469. int flag;
  470. i2eBordStrPtr pB;
  471. unsigned short maxBlock;
  472. unsigned short maxBuff;
  473. short bufroom;
  474. unsigned short stuffIndex;
  475. unsigned char *pBuf;
  476. unsigned char *pInsert;
  477. unsigned char *pDest, *pSource;
  478. unsigned short channel;
  479. int cnt;
  480. unsigned long flags = 0;
  481. rwlock_t *lock_var_p = NULL;
  482. // Make sure the channel exists, otherwise do nothing
  483. if ( !i2Validate ( pCh ) ) {
  484. return -1;
  485. }
  486. ip2trace (CHANN, ITRC_QUEUE, ITRC_ENTER, 0 );
  487. pB = pCh->pMyBord;
  488. // Board must also exist, and THE INTERRUPT COMMAND ALREADY SENT
  489. if (pB->i2eValid != I2E_MAGIC || pB->i2eUsingIrq == IRQ_UNDEFINED) {
  490. return -2;
  491. }
  492. // If the board has gone fatal, return bad, and also hit the trap routine if
  493. // it exists.
  494. if (pB->i2eFatal) {
  495. if ( pB->i2eFatalTrap ) {
  496. (*(pB)->i2eFatalTrap)(pB);
  497. }
  498. return -3;
  499. }
  500. // Set up some variables, Which buffers are we using? How big are they?
  501. switch(type)
  502. {
  503. case PTYPE_INLINE:
  504. flag = INL;
  505. maxBlock = MAX_OBUF_BLOCK;
  506. maxBuff = OBUF_SIZE;
  507. pBuf = pCh->Obuf;
  508. break;
  509. case PTYPE_BYPASS:
  510. flag = BYP;
  511. maxBlock = MAX_CBUF_BLOCK;
  512. maxBuff = CBUF_SIZE;
  513. pBuf = pCh->Cbuf;
  514. break;
  515. default:
  516. return -4;
  517. }
  518. // Determine the total size required for all the commands
  519. totalsize = blocksize = sizeof(i2CmdHeader);
  520. lastended = 0;
  521. ppCs = &pCs0;
  522. for ( count = nCommands; count; count--, ppCs++)
  523. {
  524. pCs = *ppCs;
  525. cnt = pCs->length;
  526. // Will a new block be needed for this one?
  527. // Two possible reasons: too
  528. // big or previous command has to be at the end of a packet.
  529. if ((blocksize + cnt > maxBlock) || lastended) {
  530. blocksize = sizeof(i2CmdHeader);
  531. totalsize += sizeof(i2CmdHeader);
  532. }
  533. totalsize += cnt;
  534. blocksize += cnt;
  535. // If this command had to end a block, then we will make sure to
  536. // account for it should there be any more blocks.
  537. lastended = pCs->flags & END;
  538. }
  539. for (;;) {
  540. // Make sure any pending flush commands go out before we add more data.
  541. if ( !( pCh->flush_flags && i2RetryFlushOutput( pCh ) ) ) {
  542. // How much room (this time through) ?
  543. switch(type) {
  544. case PTYPE_INLINE:
  545. lock_var_p = &pCh->Obuf_spinlock;
  546. WRITE_LOCK_IRQSAVE(lock_var_p,flags);
  547. stuffIndex = pCh->Obuf_stuff;
  548. bufroom = pCh->Obuf_strip - stuffIndex;
  549. break;
  550. case PTYPE_BYPASS:
  551. lock_var_p = &pCh->Cbuf_spinlock;
  552. WRITE_LOCK_IRQSAVE(lock_var_p,flags);
  553. stuffIndex = pCh->Cbuf_stuff;
  554. bufroom = pCh->Cbuf_strip - stuffIndex;
  555. break;
  556. default:
  557. return -5;
  558. }
  559. if (--bufroom < 0) {
  560. bufroom += maxBuff;
  561. }
  562. ip2trace (CHANN, ITRC_QUEUE, 2, 1, bufroom );
  563. // Check for overflow
  564. if (totalsize <= bufroom) {
  565. // Normal Expected path - We still hold LOCK
  566. break; /* from for()- Enough room: goto proceed */
  567. }
  568. }
  569. ip2trace (CHANN, ITRC_QUEUE, 3, 1, totalsize );
  570. // Prepare to wait for buffers to empty
  571. WRITE_UNLOCK_IRQRESTORE(lock_var_p,flags);
  572. serviceOutgoingFifo(pB); // Dump what we got
  573. if (timeout == 0) {
  574. return 0; // Tired of waiting
  575. }
  576. if (timeout > 0)
  577. timeout--; // So negative values == forever
  578. if (!in_interrupt()) {
  579. current->state = TASK_INTERRUPTIBLE;
  580. schedule_timeout(1); // short nap
  581. } else {
  582. // we cannot sched/sleep in interrrupt silly
  583. return 0;
  584. }
  585. if (signal_pending(current)) {
  586. return 0; // Wake up! Time to die!!!
  587. }
  588. ip2trace (CHANN, ITRC_QUEUE, 4, 0 );
  589. } // end of for(;;)
  590. // At this point we have room and the lock - stick them in.
  591. channel = pCh->infl.hd.i2sChannel;
  592. pInsert = &pBuf[stuffIndex]; // Pointer to start of packet
  593. pDest = CMD_OF(pInsert); // Pointer to start of command
  594. // When we start counting, the block is the size of the header
  595. for (blocksize = sizeof(i2CmdHeader), count = nCommands,
  596. lastended = 0, ppCs = &pCs0;
  597. count;
  598. count--, ppCs++)
  599. {
  600. pCs = *ppCs; // Points to command protocol structure
  601. // If this is a bookmark request command, post the fact that a bookmark
  602. // request is pending. NOTE THIS TRICK ONLY WORKS BECAUSE CMD_BMARK_REQ
  603. // has no parameters! The more general solution would be to reference
  604. // pCs->cmd[0].
  605. if (pCs == CMD_BMARK_REQ) {
  606. pCh->bookMarks++;
  607. ip2trace (CHANN, ITRC_DRAIN, 30, 1, pCh->bookMarks );
  608. }
  609. cnt = pCs->length;
  610. // If this command would put us over the maximum block size or
  611. // if the last command had to be at the end of a block, we end
  612. // the existing block here and start a new one.
  613. if ((blocksize + cnt > maxBlock) || lastended) {
  614. ip2trace (CHANN, ITRC_QUEUE, 5, 0 );
  615. PTYPE_OF(pInsert) = type;
  616. CHANNEL_OF(pInsert) = channel;
  617. // count here does not include the header
  618. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  619. stuffIndex += blocksize;
  620. if(stuffIndex >= maxBuff) {
  621. stuffIndex = 0;
  622. pInsert = pBuf;
  623. }
  624. pInsert = &pBuf[stuffIndex]; // Pointer to start of next pkt
  625. pDest = CMD_OF(pInsert);
  626. blocksize = sizeof(i2CmdHeader);
  627. }
  628. // Now we know there is room for this one in the current block
  629. blocksize += cnt; // Total bytes in this command
  630. pSource = pCs->cmd; // Copy the command into the buffer
  631. while (cnt--) {
  632. *pDest++ = *pSource++;
  633. }
  634. // If this command had to end a block, then we will make sure to account
  635. // for it should there be any more blocks.
  636. lastended = pCs->flags & END;
  637. } // end for
  638. // Clean up the final block by writing header, etc
  639. PTYPE_OF(pInsert) = type;
  640. CHANNEL_OF(pInsert) = channel;
  641. // count here does not include the header
  642. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  643. stuffIndex += blocksize;
  644. if(stuffIndex >= maxBuff) {
  645. stuffIndex = 0;
  646. pInsert = pBuf;
  647. }
  648. // Updates the index, and post the need for service. When adding these to
  649. // the queue of channels, we turn off the interrupt while doing so,
  650. // because at interrupt level we might want to push a channel back to the
  651. // end of the queue.
  652. switch(type)
  653. {
  654. case PTYPE_INLINE:
  655. pCh->Obuf_stuff = stuffIndex; // Store buffer pointer
  656. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  657. pB->debugInlineQueued++;
  658. // Add the channel pointer to list of channels needing service (first
  659. // come...), if it's not already there.
  660. i2QueueNeeds(pB, pCh, NEED_INLINE);
  661. break;
  662. case PTYPE_BYPASS:
  663. pCh->Cbuf_stuff = stuffIndex; // Store buffer pointer
  664. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  665. pB->debugBypassQueued++;
  666. // Add the channel pointer to list of channels needing service (first
  667. // come...), if it's not already there.
  668. i2QueueNeeds(pB, pCh, NEED_BYPASS);
  669. break;
  670. }
  671. ip2trace (CHANN, ITRC_QUEUE, ITRC_RETURN, 1, nCommands );
  672. return nCommands; // Good status: number of commands sent
  673. }
  674. //******************************************************************************
  675. // Function: i2GetStatus(pCh,resetBits)
  676. // Parameters: Pointer to a channel structure
  677. // Bit map of status bits to clear
  678. // Returns: Bit map of current status bits
  679. //
  680. // Description:
  681. // Returns the state of data set signals, and whether a break has been received,
  682. // (see i2lib.h for bit-mapped result). resetBits is a bit-map of any status
  683. // bits to be cleared: I2_BRK, I2_PAR, I2_FRA, I2_OVR,... These are cleared
  684. // AFTER the condition is passed. If pCh does not point to a valid channel,
  685. // returns -1 (which would be impossible otherwise.
  686. //******************************************************************************
  687. static int
  688. i2GetStatus(i2ChanStrPtr pCh, int resetBits)
  689. {
  690. unsigned short status;
  691. i2eBordStrPtr pB;
  692. ip2trace (CHANN, ITRC_STATUS, ITRC_ENTER, 2, pCh->dataSetIn, resetBits );
  693. // Make sure the channel exists, otherwise do nothing */
  694. if ( !i2Validate ( pCh ) )
  695. return -1;
  696. pB = pCh->pMyBord;
  697. status = pCh->dataSetIn;
  698. // Clear any specified error bits: but note that only actual error bits can
  699. // be cleared, regardless of the value passed.
  700. if (resetBits)
  701. {
  702. pCh->dataSetIn &= ~(resetBits & (I2_BRK | I2_PAR | I2_FRA | I2_OVR));
  703. pCh->dataSetIn &= ~(I2_DDCD | I2_DCTS | I2_DDSR | I2_DRI);
  704. }
  705. ip2trace (CHANN, ITRC_STATUS, ITRC_RETURN, 1, pCh->dataSetIn );
  706. return status;
  707. }
  708. //******************************************************************************
  709. // Function: i2Input(pChpDest,count)
  710. // Parameters: Pointer to a channel structure
  711. // Pointer to data buffer
  712. // Number of bytes to read
  713. // Returns: Number of bytes read, or -1 for error
  714. //
  715. // Description:
  716. // Strips data from the input buffer and writes it to pDest. If there is a
  717. // collosal blunder, (invalid structure pointers or the like), returns -1.
  718. // Otherwise, returns the number of bytes read.
  719. //******************************************************************************
  720. static int
  721. i2Input(i2ChanStrPtr pCh)
  722. {
  723. int amountToMove;
  724. unsigned short stripIndex;
  725. int count;
  726. unsigned long flags = 0;
  727. ip2trace (CHANN, ITRC_INPUT, ITRC_ENTER, 0);
  728. // Ensure channel structure seems real
  729. if ( !i2Validate( pCh ) ) {
  730. count = -1;
  731. goto i2Input_exit;
  732. }
  733. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  734. // initialize some accelerators and private copies
  735. stripIndex = pCh->Ibuf_strip;
  736. count = pCh->Ibuf_stuff - stripIndex;
  737. // If buffer is empty or requested data count was 0, (trivial case) return
  738. // without any further thought.
  739. if ( count == 0 ) {
  740. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  741. goto i2Input_exit;
  742. }
  743. // Adjust for buffer wrap
  744. if ( count < 0 ) {
  745. count += IBUF_SIZE;
  746. }
  747. // Don't give more than can be taken by the line discipline
  748. amountToMove = pCh->pTTY->ldisc.receive_room( pCh->pTTY );
  749. if (count > amountToMove) {
  750. count = amountToMove;
  751. }
  752. // How much could we copy without a wrap?
  753. amountToMove = IBUF_SIZE - stripIndex;
  754. if (amountToMove > count) {
  755. amountToMove = count;
  756. }
  757. // Move the first block
  758. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  759. &(pCh->Ibuf[stripIndex]), NULL, amountToMove );
  760. // If we needed to wrap, do the second data move
  761. if (count > amountToMove) {
  762. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  763. pCh->Ibuf, NULL, count - amountToMove );
  764. }
  765. // Bump and wrap the stripIndex all at once by the amount of data read. This
  766. // method is good regardless of whether the data was in one or two pieces.
  767. stripIndex += count;
  768. if (stripIndex >= IBUF_SIZE) {
  769. stripIndex -= IBUF_SIZE;
  770. }
  771. pCh->Ibuf_strip = stripIndex;
  772. // Update our flow control information and possibly queue ourselves to send
  773. // it, depending on how much data has been stripped since the last time a
  774. // packet was sent.
  775. pCh->infl.asof += count;
  776. if ((pCh->sinceLastFlow += count) >= pCh->whenSendFlow) {
  777. pCh->sinceLastFlow -= pCh->whenSendFlow;
  778. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  779. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  780. } else {
  781. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  782. }
  783. i2Input_exit:
  784. ip2trace (CHANN, ITRC_INPUT, ITRC_RETURN, 1, count);
  785. return count;
  786. }
  787. //******************************************************************************
  788. // Function: i2InputFlush(pCh)
  789. // Parameters: Pointer to a channel structure
  790. // Returns: Number of bytes stripped, or -1 for error
  791. //
  792. // Description:
  793. // Strips any data from the input buffer. If there is a collosal blunder,
  794. // (invalid structure pointers or the like), returns -1. Otherwise, returns the
  795. // number of bytes stripped.
  796. //******************************************************************************
  797. static int
  798. i2InputFlush(i2ChanStrPtr pCh)
  799. {
  800. int count;
  801. unsigned long flags;
  802. // Ensure channel structure seems real
  803. if ( !i2Validate ( pCh ) )
  804. return -1;
  805. ip2trace (CHANN, ITRC_INPUT, 10, 0);
  806. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  807. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  808. // Adjust for buffer wrap
  809. if (count < 0) {
  810. count += IBUF_SIZE;
  811. }
  812. // Expedient way to zero out the buffer
  813. pCh->Ibuf_strip = pCh->Ibuf_stuff;
  814. // Update our flow control information and possibly queue ourselves to send
  815. // it, depending on how much data has been stripped since the last time a
  816. // packet was sent.
  817. pCh->infl.asof += count;
  818. if ( (pCh->sinceLastFlow += count) >= pCh->whenSendFlow )
  819. {
  820. pCh->sinceLastFlow -= pCh->whenSendFlow;
  821. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  822. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  823. } else {
  824. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  825. }
  826. ip2trace (CHANN, ITRC_INPUT, 19, 1, count);
  827. return count;
  828. }
  829. //******************************************************************************
  830. // Function: i2InputAvailable(pCh)
  831. // Parameters: Pointer to a channel structure
  832. // Returns: Number of bytes available, or -1 for error
  833. //
  834. // Description:
  835. // If there is a collosal blunder, (invalid structure pointers or the like),
  836. // returns -1. Otherwise, returns the number of bytes stripped. Otherwise,
  837. // returns the number of bytes available in the buffer.
  838. //******************************************************************************
  839. #if 0
  840. static int
  841. i2InputAvailable(i2ChanStrPtr pCh)
  842. {
  843. int count;
  844. // Ensure channel structure seems real
  845. if ( !i2Validate ( pCh ) ) return -1;
  846. // initialize some accelerators and private copies
  847. READ_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  848. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  849. READ_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  850. // Adjust for buffer wrap
  851. if (count < 0)
  852. {
  853. count += IBUF_SIZE;
  854. }
  855. return count;
  856. }
  857. #endif
  858. //******************************************************************************
  859. // Function: i2Output(pCh, pSource, count)
  860. // Parameters: Pointer to channel structure
  861. // Pointer to source data
  862. // Number of bytes to send
  863. // Returns: Number of bytes sent, or -1 for error
  864. //
  865. // Description:
  866. // Queues the data at pSource to be sent as data packets to the board. If there
  867. // is a collosal blunder, (invalid structure pointers or the like), returns -1.
  868. // Otherwise, returns the number of bytes written. What if there is not enough
  869. // room for all the data? If pCh->channelOptions & CO_NBLOCK_WRITE is set, then
  870. // we transfer as many characters as we can now, then return. If this bit is
  871. // clear (default), routine will spin along until all the data is buffered.
  872. // Should this occur, the 1-ms delay routine is called while waiting to avoid
  873. // applications that one cannot break out of.
  874. //******************************************************************************
  875. static int
  876. i2Output(i2ChanStrPtr pCh, const char *pSource, int count, int user )
  877. {
  878. i2eBordStrPtr pB;
  879. unsigned char *pInsert;
  880. int amountToMove;
  881. int countOriginal = count;
  882. unsigned short channel;
  883. unsigned short stuffIndex;
  884. unsigned long flags;
  885. int rc = 0;
  886. int bailout = 10;
  887. ip2trace (CHANN, ITRC_OUTPUT, ITRC_ENTER, 2, count, user );
  888. // Ensure channel structure seems real
  889. if ( !i2Validate ( pCh ) )
  890. return -1;
  891. // initialize some accelerators and private copies
  892. pB = pCh->pMyBord;
  893. channel = pCh->infl.hd.i2sChannel;
  894. // If the board has gone fatal, return bad, and also hit the trap routine if
  895. // it exists.
  896. if (pB->i2eFatal) {
  897. if (pB->i2eFatalTrap) {
  898. (*(pB)->i2eFatalTrap)(pB);
  899. }
  900. return -1;
  901. }
  902. // Proceed as though we would do everything
  903. while ( count > 0 ) {
  904. // How much room in output buffer is there?
  905. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  906. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  907. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  908. if (amountToMove < 0) {
  909. amountToMove += OBUF_SIZE;
  910. }
  911. // Subtract off the headers size and see how much room there is for real
  912. // data. If this is negative, we will discover later.
  913. amountToMove -= sizeof (i2DataHeader);
  914. // Don't move more (now) than can go in a single packet
  915. if ( amountToMove > (int)(MAX_OBUF_BLOCK - sizeof(i2DataHeader)) ) {
  916. amountToMove = MAX_OBUF_BLOCK - sizeof(i2DataHeader);
  917. }
  918. // Don't move more than the count we were given
  919. if (amountToMove > count) {
  920. amountToMove = count;
  921. }
  922. // Now we know how much we must move: NB because the ring buffers have
  923. // an overflow area at the end, we needn't worry about wrapping in the
  924. // middle of a packet.
  925. // Small WINDOW here with no LOCK but I can't call Flush with LOCK
  926. // We would be flushing (or ending flush) anyway
  927. ip2trace (CHANN, ITRC_OUTPUT, 10, 1, amountToMove );
  928. if ( !(pCh->flush_flags && i2RetryFlushOutput(pCh) )
  929. && amountToMove > 0 )
  930. {
  931. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  932. stuffIndex = pCh->Obuf_stuff;
  933. // Had room to move some data: don't know whether the block size,
  934. // buffer space, or what was the limiting factor...
  935. pInsert = &(pCh->Obuf[stuffIndex]);
  936. // Set up the header
  937. CHANNEL_OF(pInsert) = channel;
  938. PTYPE_OF(pInsert) = PTYPE_DATA;
  939. TAG_OF(pInsert) = 0;
  940. ID_OF(pInsert) = ID_ORDINARY_DATA;
  941. DATA_COUNT_OF(pInsert) = amountToMove;
  942. // Move the data
  943. if ( user ) {
  944. rc = copy_from_user((char*)(DATA_OF(pInsert)), pSource,
  945. amountToMove );
  946. } else {
  947. memcpy( (char*)(DATA_OF(pInsert)), pSource, amountToMove );
  948. }
  949. // Adjust pointers and indices
  950. pSource += amountToMove;
  951. pCh->Obuf_char_count += amountToMove;
  952. stuffIndex += amountToMove + sizeof(i2DataHeader);
  953. count -= amountToMove;
  954. if (stuffIndex >= OBUF_SIZE) {
  955. stuffIndex = 0;
  956. }
  957. pCh->Obuf_stuff = stuffIndex;
  958. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  959. ip2trace (CHANN, ITRC_OUTPUT, 13, 1, stuffIndex );
  960. } else {
  961. // Cannot move data
  962. // becuz we need to stuff a flush
  963. // or amount to move is <= 0
  964. ip2trace(CHANN, ITRC_OUTPUT, 14, 3,
  965. amountToMove, pB->i2eFifoRemains,
  966. pB->i2eWaitingForEmptyFifo );
  967. // Put this channel back on queue
  968. // this ultimatly gets more data or wakes write output
  969. i2QueueNeeds(pB, pCh, NEED_INLINE);
  970. if ( pB->i2eWaitingForEmptyFifo ) {
  971. ip2trace (CHANN, ITRC_OUTPUT, 16, 0 );
  972. // or schedule
  973. if (!in_interrupt()) {
  974. ip2trace (CHANN, ITRC_OUTPUT, 61, 0 );
  975. current->state = TASK_INTERRUPTIBLE;
  976. schedule_timeout(2);
  977. if (signal_pending(current)) {
  978. break;
  979. }
  980. continue;
  981. } else {
  982. ip2trace (CHANN, ITRC_OUTPUT, 62, 0 );
  983. // let interrupt in = WAS restore_flags()
  984. // We hold no lock nor is irq off anymore???
  985. break;
  986. }
  987. break; // from while(count)
  988. }
  989. else if ( pB->i2eFifoRemains < 32 && !pB->i2eTxMailEmpty ( pB ) )
  990. {
  991. ip2trace (CHANN, ITRC_OUTPUT, 19, 2,
  992. pB->i2eFifoRemains,
  993. pB->i2eTxMailEmpty );
  994. break; // from while(count)
  995. } else if ( pCh->channelNeeds & NEED_CREDIT ) {
  996. ip2trace (CHANN, ITRC_OUTPUT, 22, 0 );
  997. break; // from while(count)
  998. } else if ( --bailout) {
  999. // Try to throw more things (maybe not us) in the fifo if we're
  1000. // not already waiting for it.
  1001. ip2trace (CHANN, ITRC_OUTPUT, 20, 0 );
  1002. serviceOutgoingFifo(pB);
  1003. //break; CONTINUE;
  1004. } else {
  1005. ip2trace (CHANN, ITRC_OUTPUT, 21, 3,
  1006. pB->i2eFifoRemains,
  1007. pB->i2eOutMailWaiting,
  1008. pB->i2eWaitingForEmptyFifo );
  1009. break; // from while(count)
  1010. }
  1011. }
  1012. } // End of while(count)
  1013. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1014. // We drop through either when the count expires, or when there is some
  1015. // count left, but there was a non-blocking write.
  1016. if (countOriginal > count) {
  1017. ip2trace (CHANN, ITRC_OUTPUT, 17, 2, countOriginal, count );
  1018. serviceOutgoingFifo( pB );
  1019. }
  1020. ip2trace (CHANN, ITRC_OUTPUT, ITRC_RETURN, 2, countOriginal, count );
  1021. return countOriginal - count;
  1022. }
  1023. //******************************************************************************
  1024. // Function: i2FlushOutput(pCh)
  1025. // Parameters: Pointer to a channel structure
  1026. // Returns: Nothing
  1027. //
  1028. // Description:
  1029. // Sends bypass command to start flushing (waiting possibly forever until there
  1030. // is room), then sends inline command to stop flushing output, (again waiting
  1031. // possibly forever).
  1032. //******************************************************************************
  1033. static inline void
  1034. i2FlushOutput(i2ChanStrPtr pCh)
  1035. {
  1036. ip2trace (CHANN, ITRC_FLUSH, 1, 1, pCh->flush_flags );
  1037. if (pCh->flush_flags)
  1038. return;
  1039. if ( 1 != i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1040. pCh->flush_flags = STARTFL_FLAG; // Failed - flag for later
  1041. ip2trace (CHANN, ITRC_FLUSH, 2, 0 );
  1042. } else if ( 1 != i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL) ) {
  1043. pCh->flush_flags = STOPFL_FLAG; // Failed - flag for later
  1044. ip2trace (CHANN, ITRC_FLUSH, 3, 0 );
  1045. }
  1046. }
  1047. static int
  1048. i2RetryFlushOutput(i2ChanStrPtr pCh)
  1049. {
  1050. int old_flags = pCh->flush_flags;
  1051. ip2trace (CHANN, ITRC_FLUSH, 14, 1, old_flags );
  1052. pCh->flush_flags = 0; // Clear flag so we can avoid recursion
  1053. // and queue the commands
  1054. if ( old_flags & STARTFL_FLAG ) {
  1055. if ( 1 == i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1056. old_flags = STOPFL_FLAG; //Success - send stop flush
  1057. } else {
  1058. old_flags = STARTFL_FLAG; //Failure - Flag for retry later
  1059. }
  1060. ip2trace (CHANN, ITRC_FLUSH, 15, 1, old_flags );
  1061. }
  1062. if ( old_flags & STOPFL_FLAG ) {
  1063. if (1 == i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL)) {
  1064. old_flags = 0; // Success - clear flags
  1065. }
  1066. ip2trace (CHANN, ITRC_FLUSH, 16, 1, old_flags );
  1067. }
  1068. pCh->flush_flags = old_flags;
  1069. ip2trace (CHANN, ITRC_FLUSH, 17, 1, old_flags );
  1070. return old_flags;
  1071. }
  1072. //******************************************************************************
  1073. // Function: i2DrainOutput(pCh,timeout)
  1074. // Parameters: Pointer to a channel structure
  1075. // Maximum period to wait
  1076. // Returns: ?
  1077. //
  1078. // Description:
  1079. // Uses the bookmark request command to ask the board to send a bookmark back as
  1080. // soon as all the data is completely sent.
  1081. //******************************************************************************
  1082. static void
  1083. i2DrainWakeup(i2ChanStrPtr pCh)
  1084. {
  1085. ip2trace (CHANN, ITRC_DRAIN, 10, 1, pCh->BookmarkTimer.expires );
  1086. pCh->BookmarkTimer.expires = 0;
  1087. wake_up_interruptible( &pCh->pBookmarkWait );
  1088. }
  1089. static void
  1090. i2DrainOutput(i2ChanStrPtr pCh, int timeout)
  1091. {
  1092. wait_queue_t wait;
  1093. i2eBordStrPtr pB;
  1094. ip2trace (CHANN, ITRC_DRAIN, ITRC_ENTER, 1, pCh->BookmarkTimer.expires);
  1095. pB = pCh->pMyBord;
  1096. // If the board has gone fatal, return bad,
  1097. // and also hit the trap routine if it exists.
  1098. if (pB->i2eFatal) {
  1099. if (pB->i2eFatalTrap) {
  1100. (*(pB)->i2eFatalTrap)(pB);
  1101. }
  1102. return;
  1103. }
  1104. if ((timeout > 0) && (pCh->BookmarkTimer.expires == 0 )) {
  1105. // One per customer (channel)
  1106. init_timer( &(pCh->BookmarkTimer) );
  1107. pCh->BookmarkTimer.expires = jiffies + timeout;
  1108. pCh->BookmarkTimer.function = (void*)(unsigned long)i2DrainWakeup;
  1109. pCh->BookmarkTimer.data = (unsigned long)pCh;
  1110. ip2trace (CHANN, ITRC_DRAIN, 1, 1, pCh->BookmarkTimer.expires );
  1111. add_timer( &(pCh->BookmarkTimer) );
  1112. }
  1113. i2QueueCommands( PTYPE_INLINE, pCh, -1, 1, CMD_BMARK_REQ );
  1114. init_waitqueue_entry(&wait, current);
  1115. add_wait_queue(&(pCh->pBookmarkWait), &wait);
  1116. set_current_state( TASK_INTERRUPTIBLE );
  1117. serviceOutgoingFifo( pB );
  1118. schedule(); // Now we take our interruptible sleep on
  1119. // Clean up the queue
  1120. set_current_state( TASK_RUNNING );
  1121. remove_wait_queue(&(pCh->pBookmarkWait), &wait);
  1122. // if expires == 0 then timer poped, then do not need to del_timer
  1123. if ((timeout > 0) && pCh->BookmarkTimer.expires &&
  1124. time_before(jiffies, pCh->BookmarkTimer.expires)) {
  1125. del_timer( &(pCh->BookmarkTimer) );
  1126. pCh->BookmarkTimer.expires = 0;
  1127. ip2trace (CHANN, ITRC_DRAIN, 3, 1, pCh->BookmarkTimer.expires );
  1128. }
  1129. ip2trace (CHANN, ITRC_DRAIN, ITRC_RETURN, 1, pCh->BookmarkTimer.expires );
  1130. return;
  1131. }
  1132. //******************************************************************************
  1133. // Function: i2OutputFree(pCh)
  1134. // Parameters: Pointer to a channel structure
  1135. // Returns: Space in output buffer
  1136. //
  1137. // Description:
  1138. // Returns -1 if very gross error. Otherwise returns the amount of bytes still
  1139. // free in the output buffer.
  1140. //******************************************************************************
  1141. static int
  1142. i2OutputFree(i2ChanStrPtr pCh)
  1143. {
  1144. int amountToMove;
  1145. unsigned long flags;
  1146. // Ensure channel structure seems real
  1147. if ( !i2Validate ( pCh ) ) {
  1148. return -1;
  1149. }
  1150. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1151. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  1152. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1153. if (amountToMove < 0) {
  1154. amountToMove += OBUF_SIZE;
  1155. }
  1156. // If this is negative, we will discover later
  1157. amountToMove -= sizeof(i2DataHeader);
  1158. return (amountToMove < 0) ? 0 : amountToMove;
  1159. }
  1160. static void
  1161. ip2_owake( PTTY tp)
  1162. {
  1163. i2ChanStrPtr pCh;
  1164. if (tp == NULL) return;
  1165. pCh = tp->driver_data;
  1166. ip2trace (CHANN, ITRC_SICMD, 10, 2, tp->flags,
  1167. (1 << TTY_DO_WRITE_WAKEUP) );
  1168. wake_up_interruptible ( &tp->write_wait );
  1169. if ( ( tp->flags & (1 << TTY_DO_WRITE_WAKEUP) )
  1170. && tp->ldisc.write_wakeup )
  1171. {
  1172. (tp->ldisc.write_wakeup) ( tp );
  1173. ip2trace (CHANN, ITRC_SICMD, 11, 0 );
  1174. }
  1175. }
  1176. static inline void
  1177. set_baud_params(i2eBordStrPtr pB)
  1178. {
  1179. int i,j;
  1180. i2ChanStrPtr *pCh;
  1181. pCh = (i2ChanStrPtr *) pB->i2eChannelPtr;
  1182. for (i = 0; i < ABS_MAX_BOXES; i++) {
  1183. if (pB->channelBtypes.bid_value[i]) {
  1184. if (BID_HAS_654(pB->channelBtypes.bid_value[i])) {
  1185. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1186. if (pCh[i*16+j] == NULL)
  1187. break;
  1188. (pCh[i*16+j])->BaudBase = 921600; // MAX for ST654
  1189. (pCh[i*16+j])->BaudDivisor = 96;
  1190. }
  1191. } else { // has cirrus cd1400
  1192. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1193. if (pCh[i*16+j] == NULL)
  1194. break;
  1195. (pCh[i*16+j])->BaudBase = 115200; // MAX for CD1400
  1196. (pCh[i*16+j])->BaudDivisor = 12;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. //******************************************************************************
  1203. // Function: i2StripFifo(pB)
  1204. // Parameters: Pointer to a board structure
  1205. // Returns: ?
  1206. //
  1207. // Description:
  1208. // Strips all the available data from the incoming FIFO, identifies the type of
  1209. // packet, and either buffers the data or does what needs to be done.
  1210. //
  1211. // Note there is no overflow checking here: if the board sends more data than it
  1212. // ought to, we will not detect it here, but blindly overflow...
  1213. //******************************************************************************
  1214. // A buffer for reading in blocks for unknown channels
  1215. static unsigned char junkBuffer[IBUF_SIZE];
  1216. // A buffer to read in a status packet. Because of the size of the count field
  1217. // for these things, the maximum packet size must be less than MAX_CMD_PACK_SIZE
  1218. static unsigned char cmdBuffer[MAX_CMD_PACK_SIZE + 4];
  1219. // This table changes the bit order from MSR order given by STAT_MODEM packet to
  1220. // status bits used in our library.
  1221. static char xlatDss[16] = {
  1222. 0 | 0 | 0 | 0 ,
  1223. 0 | 0 | 0 | I2_CTS ,
  1224. 0 | 0 | I2_DSR | 0 ,
  1225. 0 | 0 | I2_DSR | I2_CTS ,
  1226. 0 | I2_RI | 0 | 0 ,
  1227. 0 | I2_RI | 0 | I2_CTS ,
  1228. 0 | I2_RI | I2_DSR | 0 ,
  1229. 0 | I2_RI | I2_DSR | I2_CTS ,
  1230. I2_DCD | 0 | 0 | 0 ,
  1231. I2_DCD | 0 | 0 | I2_CTS ,
  1232. I2_DCD | 0 | I2_DSR | 0 ,
  1233. I2_DCD | 0 | I2_DSR | I2_CTS ,
  1234. I2_DCD | I2_RI | 0 | 0 ,
  1235. I2_DCD | I2_RI | 0 | I2_CTS ,
  1236. I2_DCD | I2_RI | I2_DSR | 0 ,
  1237. I2_DCD | I2_RI | I2_DSR | I2_CTS };
  1238. static inline void
  1239. i2StripFifo(i2eBordStrPtr pB)
  1240. {
  1241. i2ChanStrPtr pCh;
  1242. int channel;
  1243. int count;
  1244. unsigned short stuffIndex;
  1245. int amountToRead;
  1246. unsigned char *pc, *pcLimit;
  1247. unsigned char uc;
  1248. unsigned char dss_change;
  1249. unsigned long bflags,cflags;
  1250. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_ENTER, 0 );
  1251. while (HAS_INPUT(pB)) {
  1252. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 2, 0 );
  1253. // Process packet from fifo a one atomic unit
  1254. WRITE_LOCK_IRQSAVE(&pB->read_fifo_spinlock,bflags);
  1255. // The first word (or two bytes) will have channel number and type of
  1256. // packet, possibly other information
  1257. pB->i2eLeadoffWord[0] = iiReadWord(pB);
  1258. switch(PTYPE_OF(pB->i2eLeadoffWord))
  1259. {
  1260. case PTYPE_DATA:
  1261. pB->got_input = 1;
  1262. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 3, 0 );
  1263. channel = CHANNEL_OF(pB->i2eLeadoffWord); /* Store channel */
  1264. count = iiReadWord(pB); /* Count is in the next word */
  1265. // NEW: Check the count for sanity! Should the hardware fail, our death
  1266. // is more pleasant. While an oversize channel is acceptable (just more
  1267. // than the driver supports), an over-length count clearly means we are
  1268. // sick!
  1269. if ( ((unsigned int)count) > IBUF_SIZE ) {
  1270. pB->i2eFatal = 2;
  1271. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1272. return; /* Bail out ASAP */
  1273. }
  1274. // Channel is illegally big ?
  1275. if ((channel >= pB->i2eChannelCnt) ||
  1276. (NULL==(pCh = ((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])))
  1277. {
  1278. iiReadBuf(pB, junkBuffer, count);
  1279. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1280. break; /* From switch: ready for next packet */
  1281. }
  1282. // Channel should be valid, then
  1283. // If this is a hot-key, merely post its receipt for now. These are
  1284. // always supposed to be 1-byte packets, so we won't even check the
  1285. // count. Also we will post an acknowledgement to the board so that
  1286. // more data can be forthcoming. Note that we are not trying to use
  1287. // these sequences in this driver, merely to robustly ignore them.
  1288. if(ID_OF(pB->i2eLeadoffWord) == ID_HOT_KEY)
  1289. {
  1290. pCh->hotKeyIn = iiReadWord(pB) & 0xff;
  1291. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1292. i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_HOTACK);
  1293. break; /* From the switch: ready for next packet */
  1294. }
  1295. // Normal data! We crudely assume there is room for the data in our
  1296. // buffer because the board wouldn't have exceeded his credit limit.
  1297. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,cflags);
  1298. // We have 2 locks now
  1299. stuffIndex = pCh->Ibuf_stuff;
  1300. amountToRead = IBUF_SIZE - stuffIndex;
  1301. if (amountToRead > count)
  1302. amountToRead = count;
  1303. // stuffIndex would have been already adjusted so there would
  1304. // always be room for at least one, and count is always at least
  1305. // one.
  1306. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1307. pCh->icount.rx += amountToRead;
  1308. // Update the stuffIndex by the amount of data moved. Note we could
  1309. // never ask for more data than would just fit. However, we might
  1310. // have read in one more byte than we wanted because the read
  1311. // rounds up to even bytes. If this byte is on the end of the
  1312. // packet, and is padding, we ignore it. If the byte is part of
  1313. // the actual data, we need to move it.
  1314. stuffIndex += amountToRead;
  1315. if (stuffIndex >= IBUF_SIZE) {
  1316. if ((amountToRead & 1) && (count > amountToRead)) {
  1317. pCh->Ibuf[0] = pCh->Ibuf[IBUF_SIZE];
  1318. amountToRead++;
  1319. stuffIndex = 1;
  1320. } else {
  1321. stuffIndex = 0;
  1322. }
  1323. }
  1324. // If there is anything left over, read it as well
  1325. if (count > amountToRead) {
  1326. amountToRead = count - amountToRead;
  1327. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1328. pCh->icount.rx += amountToRead;
  1329. stuffIndex += amountToRead;
  1330. }
  1331. // Update stuff index
  1332. pCh->Ibuf_stuff = stuffIndex;
  1333. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,cflags);
  1334. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1335. #ifdef USE_IQ
  1336. schedule_work(&pCh->tqueue_input);
  1337. #else
  1338. do_input(pCh);
  1339. #endif
  1340. // Note we do not need to maintain any flow-control credits at this
  1341. // time: if we were to increment .asof and decrement .room, there
  1342. // would be no net effect. Instead, when we strip data, we will
  1343. // increment .asof and leave .room unchanged.
  1344. break; // From switch: ready for next packet
  1345. case PTYPE_STATUS:
  1346. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 4, 0 );
  1347. count = CMD_COUNT_OF(pB->i2eLeadoffWord);
  1348. iiReadBuf(pB, cmdBuffer, count);
  1349. // We can release early with buffer grab
  1350. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1351. pc = cmdBuffer;
  1352. pcLimit = &(cmdBuffer[count]);
  1353. while (pc < pcLimit) {
  1354. channel = *pc++;
  1355. ip2trace (channel, ITRC_SFIFO, 7, 2, channel, *pc );
  1356. /* check for valid channel */
  1357. if (channel < pB->i2eChannelCnt
  1358. &&
  1359. (pCh = (((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])) != NULL
  1360. )
  1361. {
  1362. dss_change = 0;
  1363. switch (uc = *pc++)
  1364. {
  1365. /* Breaks and modem signals are easy: just update status */
  1366. case STAT_CTS_UP:
  1367. if ( !(pCh->dataSetIn & I2_CTS) )
  1368. {
  1369. pCh->dataSetIn |= I2_DCTS;
  1370. pCh->icount.cts++;
  1371. dss_change = 1;
  1372. }
  1373. pCh->dataSetIn |= I2_CTS;
  1374. break;
  1375. case STAT_CTS_DN:
  1376. if ( pCh->dataSetIn & I2_CTS )
  1377. {
  1378. pCh->dataSetIn |= I2_DCTS;
  1379. pCh->icount.cts++;
  1380. dss_change = 1;
  1381. }
  1382. pCh->dataSetIn &= ~I2_CTS;
  1383. break;
  1384. case STAT_DCD_UP:
  1385. ip2trace (channel, ITRC_MODEM, 1, 1, pCh->dataSetIn );
  1386. if ( !(pCh->dataSetIn & I2_DCD) )
  1387. {
  1388. ip2trace (CHANN, ITRC_MODEM, 2, 0 );
  1389. pCh->dataSetIn |= I2_DDCD;
  1390. pCh->icount.dcd++;
  1391. dss_change = 1;
  1392. }
  1393. pCh->dataSetIn |= I2_DCD;
  1394. ip2trace (channel, ITRC_MODEM, 3, 1, pCh->dataSetIn );
  1395. break;
  1396. case STAT_DCD_DN:
  1397. ip2trace (channel, ITRC_MODEM, 4, 1, pCh->dataSetIn );
  1398. if ( pCh->dataSetIn & I2_DCD )
  1399. {
  1400. ip2trace (channel, ITRC_MODEM, 5, 0 );
  1401. pCh->dataSetIn |= I2_DDCD;
  1402. pCh->icount.dcd++;
  1403. dss_change = 1;
  1404. }
  1405. pCh->dataSetIn &= ~I2_DCD;
  1406. ip2trace (channel, ITRC_MODEM, 6, 1, pCh->dataSetIn );
  1407. break;
  1408. case STAT_DSR_UP:
  1409. if ( !(pCh->dataSetIn & I2_DSR) )
  1410. {
  1411. pCh->dataSetIn |= I2_DDSR;
  1412. pCh->icount.dsr++;
  1413. dss_change = 1;
  1414. }
  1415. pCh->dataSetIn |= I2_DSR;
  1416. break;
  1417. case STAT_DSR_DN:
  1418. if ( pCh->dataSetIn & I2_DSR )
  1419. {
  1420. pCh->dataSetIn |= I2_DDSR;
  1421. pCh->icount.dsr++;
  1422. dss_change = 1;
  1423. }
  1424. pCh->dataSetIn &= ~I2_DSR;
  1425. break;
  1426. case STAT_RI_UP:
  1427. if ( !(pCh->dataSetIn & I2_RI) )
  1428. {
  1429. pCh->dataSetIn |= I2_DRI;
  1430. pCh->icount.rng++;
  1431. dss_change = 1;
  1432. }
  1433. pCh->dataSetIn |= I2_RI ;
  1434. break;
  1435. case STAT_RI_DN:
  1436. // to be compat with serial.c
  1437. //if ( pCh->dataSetIn & I2_RI )
  1438. //{
  1439. // pCh->dataSetIn |= I2_DRI;
  1440. // pCh->icount.rng++;
  1441. // dss_change = 1;
  1442. //}
  1443. pCh->dataSetIn &= ~I2_RI ;
  1444. break;
  1445. case STAT_BRK_DET:
  1446. pCh->dataSetIn |= I2_BRK;
  1447. pCh->icount.brk++;
  1448. dss_change = 1;
  1449. break;
  1450. // Bookmarks? one less request we're waiting for
  1451. case STAT_BMARK:
  1452. pCh->bookMarks--;
  1453. if (pCh->bookMarks <= 0 ) {
  1454. pCh->bookMarks = 0;
  1455. wake_up_interruptible( &pCh->pBookmarkWait );
  1456. ip2trace (channel, ITRC_DRAIN, 20, 1, pCh->BookmarkTimer.expires );
  1457. }
  1458. break;
  1459. // Flow control packets? Update the new credits, and if
  1460. // someone was waiting for output, queue him up again.
  1461. case STAT_FLOW:
  1462. pCh->outfl.room =
  1463. ((flowStatPtr)pc)->room -
  1464. (pCh->outfl.asof - ((flowStatPtr)pc)->asof);
  1465. ip2trace (channel, ITRC_STFLW, 1, 1, pCh->outfl.room );
  1466. if (pCh->channelNeeds & NEED_CREDIT)
  1467. {
  1468. ip2trace (channel, ITRC_STFLW, 2, 1, pCh->channelNeeds);
  1469. pCh->channelNeeds &= ~NEED_CREDIT;
  1470. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1471. if ( pCh->pTTY )
  1472. ip2_owake(pCh->pTTY);
  1473. }
  1474. ip2trace (channel, ITRC_STFLW, 3, 1, pCh->channelNeeds);
  1475. pc += sizeof(flowStat);
  1476. break;
  1477. /* Special packets: */
  1478. /* Just copy the information into the channel structure */
  1479. case STAT_STATUS:
  1480. pCh->channelStatus = *((debugStatPtr)pc);
  1481. pc += sizeof(debugStat);
  1482. break;
  1483. case STAT_TXCNT:
  1484. pCh->channelTcount = *((cntStatPtr)pc);
  1485. pc += sizeof(cntStat);
  1486. break;
  1487. case STAT_RXCNT:
  1488. pCh->channelRcount = *((cntStatPtr)pc);
  1489. pc += sizeof(cntStat);
  1490. break;
  1491. case STAT_BOXIDS:
  1492. pB->channelBtypes = *((bidStatPtr)pc);
  1493. pc += sizeof(bidStat);
  1494. set_baud_params(pB);
  1495. break;
  1496. case STAT_HWFAIL:
  1497. i2QueueCommands (PTYPE_INLINE, pCh, 0, 1, CMD_HW_TEST);
  1498. pCh->channelFail = *((failStatPtr)pc);
  1499. pc += sizeof(failStat);
  1500. break;
  1501. /* No explicit match? then
  1502. * Might be an error packet...
  1503. */
  1504. default:
  1505. switch (uc & STAT_MOD_ERROR)
  1506. {
  1507. case STAT_ERROR:
  1508. if (uc & STAT_E_PARITY) {
  1509. pCh->dataSetIn |= I2_PAR;
  1510. pCh->icount.parity++;
  1511. }
  1512. if (uc & STAT_E_FRAMING){
  1513. pCh->dataSetIn |= I2_FRA;
  1514. pCh->icount.frame++;
  1515. }
  1516. if (uc & STAT_E_OVERRUN){
  1517. pCh->dataSetIn |= I2_OVR;
  1518. pCh->icount.overrun++;
  1519. }
  1520. break;
  1521. case STAT_MODEM:
  1522. // the answer to DSS_NOW request (not change)
  1523. pCh->dataSetIn = (pCh->dataSetIn
  1524. & ~(I2_RI | I2_CTS | I2_DCD | I2_DSR) )
  1525. | xlatDss[uc & 0xf];
  1526. wake_up_interruptible ( &pCh->dss_now_wait );
  1527. default:
  1528. break;
  1529. }
  1530. } /* End of switch on status type */
  1531. if (dss_change) {
  1532. #ifdef USE_IQ
  1533. schedule_work(&pCh->tqueue_status);
  1534. #else
  1535. do_status(pCh);
  1536. #endif
  1537. }
  1538. }
  1539. else /* Or else, channel is invalid */
  1540. {
  1541. // Even though the channel is invalid, we must test the
  1542. // status to see how much additional data it has (to be
  1543. // skipped)
  1544. switch (*pc++)
  1545. {
  1546. case STAT_FLOW:
  1547. pc += 4; /* Skip the data */
  1548. break;
  1549. default:
  1550. break;
  1551. }
  1552. }
  1553. } // End of while (there is still some status packet left)
  1554. break;
  1555. default: // Neither packet? should be impossible
  1556. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 5, 1,
  1557. PTYPE_OF(pB->i2eLeadoffWord) );
  1558. break;
  1559. } // End of switch on type of packets
  1560. } //while(board HAS_INPUT)
  1561. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_RETURN, 0 );
  1562. // Send acknowledgement to the board even if there was no data!
  1563. pB->i2eOutMailWaiting |= MB_IN_STRIPPED;
  1564. return;
  1565. }
  1566. //******************************************************************************
  1567. // Function: i2Write2Fifo(pB,address,count)
  1568. // Parameters: Pointer to a board structure, source address, byte count
  1569. // Returns: bytes written
  1570. //
  1571. // Description:
  1572. // Writes count bytes to board io address(implied) from source
  1573. // Adjusts count, leaves reserve for next time around bypass cmds
  1574. //******************************************************************************
  1575. static int
  1576. i2Write2Fifo(i2eBordStrPtr pB, unsigned char *source, int count,int reserve)
  1577. {
  1578. int rc = 0;
  1579. unsigned long flags;
  1580. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1581. if (!pB->i2eWaitingForEmptyFifo) {
  1582. if (pB->i2eFifoRemains > (count+reserve)) {
  1583. pB->i2eFifoRemains -= count;
  1584. iiWriteBuf(pB, source, count);
  1585. pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
  1586. rc = count;
  1587. }
  1588. }
  1589. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1590. return rc;
  1591. }
  1592. //******************************************************************************
  1593. // Function: i2StuffFifoBypass(pB)
  1594. // Parameters: Pointer to a board structure
  1595. // Returns: Nothing
  1596. //
  1597. // Description:
  1598. // Stuffs as many bypass commands into the fifo as possible. This is simpler
  1599. // than stuffing data or inline commands to fifo, since we do not have
  1600. // flow-control to deal with.
  1601. //******************************************************************************
  1602. static inline void
  1603. i2StuffFifoBypass(i2eBordStrPtr pB)
  1604. {
  1605. i2ChanStrPtr pCh;
  1606. unsigned char *pRemove;
  1607. unsigned short stripIndex;
  1608. unsigned short packetSize;
  1609. unsigned short paddedSize;
  1610. unsigned short notClogged = 1;
  1611. unsigned long flags;
  1612. int bailout = 1000;
  1613. // Continue processing so long as there are entries, or there is room in the
  1614. // fifo. Each entry represents a channel with something to do.
  1615. while ( --bailout && notClogged &&
  1616. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_BYPASS))))
  1617. {
  1618. WRITE_LOCK_IRQSAVE(&pCh->Cbuf_spinlock,flags);
  1619. stripIndex = pCh->Cbuf_strip;
  1620. // as long as there are packets for this channel...
  1621. while (stripIndex != pCh->Cbuf_stuff) {
  1622. pRemove = &(pCh->Cbuf[stripIndex]);
  1623. packetSize = CMD_COUNT_OF(pRemove) + sizeof(i2CmdHeader);
  1624. paddedSize = ROUNDUP(packetSize);
  1625. if (paddedSize > 0) {
  1626. if ( 0 == i2Write2Fifo(pB, pRemove, paddedSize,0)) {
  1627. notClogged = 0; /* fifo full */
  1628. i2QueueNeeds(pB, pCh, NEED_BYPASS); // Put back on queue
  1629. break; // Break from the channel
  1630. }
  1631. }
  1632. #ifdef DEBUG_FIFO
  1633. WriteDBGBuf("BYPS", pRemove, paddedSize);
  1634. #endif /* DEBUG_FIFO */
  1635. pB->debugBypassCount++;
  1636. pRemove += packetSize;
  1637. stripIndex += packetSize;
  1638. if (stripIndex >= CBUF_SIZE) {
  1639. stripIndex = 0;
  1640. pRemove = pCh->Cbuf;
  1641. }
  1642. }
  1643. // Done with this channel. Move to next, removing this one from
  1644. // the queue of channels if we cleaned it out (i.e., didn't get clogged.
  1645. pCh->Cbuf_strip = stripIndex;
  1646. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  1647. } // Either clogged or finished all the work
  1648. #ifdef IP2DEBUG_TRACE
  1649. if ( !bailout ) {
  1650. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 1, 0 );
  1651. }
  1652. #endif
  1653. }
  1654. //******************************************************************************
  1655. // Function: i2StuffFifoFlow(pB)
  1656. // Parameters: Pointer to a board structure
  1657. // Returns: Nothing
  1658. //
  1659. // Description:
  1660. // Stuffs as many flow control packets into the fifo as possible. This is easier
  1661. // even than doing normal bypass commands, because there is always at most one
  1662. // packet, already assembled, for each channel.
  1663. //******************************************************************************
  1664. static inline void
  1665. i2StuffFifoFlow(i2eBordStrPtr pB)
  1666. {
  1667. i2ChanStrPtr pCh;
  1668. unsigned short paddedSize = ROUNDUP(sizeof(flowIn));
  1669. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_ENTER, 2,
  1670. pB->i2eFifoRemains, paddedSize );
  1671. // Continue processing so long as there are entries, or there is room in the
  1672. // fifo. Each entry represents a channel with something to do.
  1673. while ( (NULL != (pCh = i2DeQueueNeeds(pB,NEED_FLOW)))) {
  1674. pB->debugFlowCount++;
  1675. // NO Chan LOCK needed ???
  1676. if ( 0 == i2Write2Fifo(pB,(unsigned char *)&(pCh->infl),paddedSize,0)) {
  1677. break;
  1678. }
  1679. #ifdef DEBUG_FIFO
  1680. WriteDBGBuf("FLOW",(unsigned char *) &(pCh->infl), paddedSize);
  1681. #endif /* DEBUG_FIFO */
  1682. } // Either clogged or finished all the work
  1683. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_RETURN, 0 );
  1684. }
  1685. //******************************************************************************
  1686. // Function: i2StuffFifoInline(pB)
  1687. // Parameters: Pointer to a board structure
  1688. // Returns: Nothing
  1689. //
  1690. // Description:
  1691. // Stuffs as much data and inline commands into the fifo as possible. This is
  1692. // the most complex fifo-stuffing operation, since there if now the channel
  1693. // flow-control issue to deal with.
  1694. //******************************************************************************
  1695. static inline void
  1696. i2StuffFifoInline(i2eBordStrPtr pB)
  1697. {
  1698. i2ChanStrPtr pCh;
  1699. unsigned char *pRemove;
  1700. unsigned short stripIndex;
  1701. unsigned short packetSize;
  1702. unsigned short paddedSize;
  1703. unsigned short notClogged = 1;
  1704. unsigned short flowsize;
  1705. unsigned long flags;
  1706. int bailout = 1000;
  1707. int bailout2;
  1708. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_ENTER, 3, pB->i2eFifoRemains,
  1709. pB->i2Dbuf_strip, pB->i2Dbuf_stuff );
  1710. // Continue processing so long as there are entries, or there is room in the
  1711. // fifo. Each entry represents a channel with something to do.
  1712. while ( --bailout && notClogged &&
  1713. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_INLINE))) )
  1714. {
  1715. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1716. stripIndex = pCh->Obuf_strip;
  1717. ip2trace (CHANN, ITRC_SICMD, 3, 2, stripIndex, pCh->Obuf_stuff );
  1718. // as long as there are packets for this channel...
  1719. bailout2 = 1000;
  1720. while ( --bailout2 && stripIndex != pCh->Obuf_stuff) {
  1721. pRemove = &(pCh->Obuf[stripIndex]);
  1722. // Must determine whether this be a data or command packet to
  1723. // calculate correctly the header size and the amount of
  1724. // flow-control credit this type of packet will use.
  1725. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1726. flowsize = DATA_COUNT_OF(pRemove);
  1727. packetSize = flowsize + sizeof(i2DataHeader);
  1728. } else {
  1729. flowsize = CMD_COUNT_OF(pRemove);
  1730. packetSize = flowsize + sizeof(i2CmdHeader);
  1731. }
  1732. flowsize = CREDIT_USAGE(flowsize);
  1733. paddedSize = ROUNDUP(packetSize);
  1734. ip2trace (CHANN, ITRC_SICMD, 4, 2, pB->i2eFifoRemains, paddedSize );
  1735. // If we don't have enough credits from the board to send the data,
  1736. // flag the channel that we are waiting for flow control credit, and
  1737. // break out. This will clean up this channel and remove us from the
  1738. // queue of hot things to do.
  1739. ip2trace (CHANN, ITRC_SICMD, 5, 2, pCh->outfl.room, flowsize );
  1740. if (pCh->outfl.room <= flowsize) {
  1741. // Do Not have the credits to send this packet.
  1742. i2QueueNeeds(pB, pCh, NEED_CREDIT);
  1743. notClogged = 0;
  1744. break; // So to do next channel
  1745. }
  1746. if ( (paddedSize > 0)
  1747. && ( 0 == i2Write2Fifo(pB, pRemove, paddedSize, 128))) {
  1748. // Do Not have room in fifo to send this packet.
  1749. notClogged = 0;
  1750. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1751. break; // Break from the channel
  1752. }
  1753. #ifdef DEBUG_FIFO
  1754. WriteDBGBuf("DATA", pRemove, paddedSize);
  1755. #endif /* DEBUG_FIFO */
  1756. pB->debugInlineCount++;
  1757. pCh->icount.tx += flowsize;
  1758. // Update current credits
  1759. pCh->outfl.room -= flowsize;
  1760. pCh->outfl.asof += flowsize;
  1761. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1762. pCh->Obuf_char_count -= DATA_COUNT_OF(pRemove);
  1763. }
  1764. pRemove += packetSize;
  1765. stripIndex += packetSize;
  1766. ip2trace (CHANN, ITRC_SICMD, 6, 2, stripIndex, pCh->Obuf_strip);
  1767. if (stripIndex >= OBUF_SIZE) {
  1768. stripIndex = 0;
  1769. pRemove = pCh->Obuf;
  1770. ip2trace (CHANN, ITRC_SICMD, 7, 1, stripIndex );
  1771. }
  1772. } /* while */
  1773. if ( !bailout2 ) {
  1774. ip2trace (CHANN, ITRC_ERROR, 3, 0 );
  1775. }
  1776. // Done with this channel. Move to next, removing this one from the
  1777. // queue of channels if we cleaned it out (i.e., didn't get clogged.
  1778. pCh->Obuf_strip = stripIndex;
  1779. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1780. if ( notClogged )
  1781. {
  1782. ip2trace (CHANN, ITRC_SICMD, 8, 0 );
  1783. if ( pCh->pTTY ) {
  1784. ip2_owake(pCh->pTTY);
  1785. }
  1786. }
  1787. } // Either clogged or finished all the work
  1788. if ( !bailout ) {
  1789. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 4, 0 );
  1790. }
  1791. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_RETURN, 1,pB->i2Dbuf_strip);
  1792. }
  1793. //******************************************************************************
  1794. // Function: serviceOutgoingFifo(pB)
  1795. // Parameters: Pointer to a board structure
  1796. // Returns: Nothing
  1797. //
  1798. // Description:
  1799. // Helper routine to put data in the outgoing fifo, if we aren't already waiting
  1800. // for something to be there. If the fifo has only room for a very little data,
  1801. // go head and hit the board with a mailbox hit immediately. Otherwise, it will
  1802. // have to happen later in the interrupt processing. Since this routine may be
  1803. // called both at interrupt and foreground time, we must turn off interrupts
  1804. // during the entire process.
  1805. //******************************************************************************
  1806. static void
  1807. serviceOutgoingFifo(i2eBordStrPtr pB)
  1808. {
  1809. // If we aren't currently waiting for the board to empty our fifo, service
  1810. // everything that is pending, in priority order (especially, Bypass before
  1811. // Inline).
  1812. if ( ! pB->i2eWaitingForEmptyFifo )
  1813. {
  1814. i2StuffFifoFlow(pB);
  1815. i2StuffFifoBypass(pB);
  1816. i2StuffFifoInline(pB);
  1817. iiSendPendingMail(pB);
  1818. }
  1819. }
  1820. //******************************************************************************
  1821. // Function: i2ServiceBoard(pB)
  1822. // Parameters: Pointer to a board structure
  1823. // Returns: Nothing
  1824. //
  1825. // Description:
  1826. // Normally this is called from interrupt level, but there is deliberately
  1827. // nothing in here specific to being called from interrupt level. All the
  1828. // hardware-specific, interrupt-specific things happen at the outer levels.
  1829. //
  1830. // For example, a timer interrupt could drive this routine for some sort of
  1831. // polled operation. The only requirement is that the programmer deal with any
  1832. // atomiticity/concurrency issues that result.
  1833. //
  1834. // This routine responds to the board's having sent mailbox information to the
  1835. // host (which would normally cause an interrupt). This routine reads the
  1836. // incoming mailbox. If there is no data in it, this board did not create the
  1837. // interrupt and/or has nothing to be done to it. (Except, if we have been
  1838. // waiting to write mailbox data to it, we may do so.
  1839. //
  1840. // Based on the value in the mailbox, we may take various actions.
  1841. //
  1842. // No checking here of pB validity: after all, it shouldn't have been called by
  1843. // the handler unless pB were on the list.
  1844. //******************************************************************************
  1845. static inline int
  1846. i2ServiceBoard ( i2eBordStrPtr pB )
  1847. {
  1848. unsigned inmail;
  1849. unsigned long flags;
  1850. /* This should be atomic because of the way we are called... */
  1851. if (NO_MAIL_HERE == ( inmail = pB->i2eStartMail ) ) {
  1852. inmail = iiGetMail(pB);
  1853. }
  1854. pB->i2eStartMail = NO_MAIL_HERE;
  1855. ip2trace (ITRC_NO_PORT, ITRC_INTR, 2, 1, inmail );
  1856. if (inmail != NO_MAIL_HERE) {
  1857. // If the board has gone fatal, nothing to do but hit a bit that will
  1858. // alert foreground tasks to protest!
  1859. if ( inmail & MB_FATAL_ERROR ) {
  1860. pB->i2eFatal = 1;
  1861. goto exit_i2ServiceBoard;
  1862. }
  1863. /* Assuming no fatal condition, we proceed to do work */
  1864. if ( inmail & MB_IN_STUFFED ) {
  1865. pB->i2eFifoInInts++;
  1866. i2StripFifo(pB); /* There might be incoming packets */
  1867. }
  1868. if (inmail & MB_OUT_STRIPPED) {
  1869. pB->i2eFifoOutInts++;
  1870. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1871. pB->i2eFifoRemains = pB->i2eFifoSize;
  1872. pB->i2eWaitingForEmptyFifo = 0;
  1873. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1874. ip2trace (ITRC_NO_PORT, ITRC_INTR, 30, 1, pB->i2eFifoRemains );
  1875. }
  1876. serviceOutgoingFifo(pB);
  1877. }
  1878. ip2trace (ITRC_NO_PORT, ITRC_INTR, 8, 0 );
  1879. exit_i2ServiceBoard:
  1880. return 0;
  1881. }