i2lib.c 65 KB

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