aic7xxx.seq 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * Adaptec 274x/284x/294x device driver firmware for Linux and FreeBSD.
  3. *
  4. * Copyright (c) 1994-1999 Justin Gibbs.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification, immediately at the beginning of the file.
  13. * 2. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * Where this Software is combined with software released under the terms of
  17. * the GNU General Public License (GPL) and the terms of the GPL would require the
  18. * combined work to also be released under the terms of the GPL, the terms
  19. * and conditions of this License will apply in addition to those of the
  20. * GPL with the exception of any terms or conditions of this License that
  21. * conflict with, or are expressly prohibited by, the GPL.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * $Id: aic7xxx.seq,v 1.77 1998/06/28 02:58:57 gibbs Exp $
  36. */
  37. #include "aic7xxx.reg"
  38. #include "scsi_message.h"
  39. /*
  40. * A few words on the waiting SCB list:
  41. * After starting the selection hardware, we check for reconnecting targets
  42. * as well as for our selection to complete just in case the reselection wins
  43. * bus arbitration. The problem with this is that we must keep track of the
  44. * SCB that we've already pulled from the QINFIFO and started the selection
  45. * on just in case the reselection wins so that we can retry the selection at
  46. * a later time. This problem cannot be resolved by holding a single entry
  47. * in scratch ram since a reconnecting target can request sense and this will
  48. * create yet another SCB waiting for selection. The solution used here is to
  49. * use byte 27 of the SCB as a pseudo-next pointer and to thread a list
  50. * of SCBs that are awaiting selection. Since 0-0xfe are valid SCB indexes,
  51. * SCB_LIST_NULL is 0xff which is out of range. An entry is also added to
  52. * this list every time a request sense occurs or after completing a non-tagged
  53. * command for which a second SCB has been queued. The sequencer will
  54. * automatically consume the entries.
  55. */
  56. reset:
  57. clr SCSISIGO; /* De-assert BSY */
  58. and SXFRCTL1, ~BITBUCKET;
  59. /* Always allow reselection */
  60. mvi SCSISEQ, ENRSELI|ENAUTOATNP;
  61. if ((p->features & AHC_CMD_CHAN) != 0) {
  62. /* Ensure that no DMA operations are in progress */
  63. clr CCSGCTL;
  64. clr CCSCBCTL;
  65. }
  66. call clear_target_state;
  67. poll_for_work:
  68. and SXFRCTL0, ~SPIOEN;
  69. if ((p->features & AHC_QUEUE_REGS) == 0) {
  70. mov A, QINPOS;
  71. }
  72. poll_for_work_loop:
  73. if ((p->features & AHC_QUEUE_REGS) == 0) {
  74. and SEQCTL, ~PAUSEDIS;
  75. }
  76. test SSTAT0, SELDO|SELDI jnz selection;
  77. test SCSISEQ, ENSELO jnz poll_for_work;
  78. if ((p->features & AHC_TWIN) != 0) {
  79. /*
  80. * Twin channel devices cannot handle things like SELTO
  81. * interrupts on the "background" channel. So, if we
  82. * are selecting, keep polling the current channel util
  83. * either a selection or reselection occurs.
  84. */
  85. xor SBLKCTL,SELBUSB; /* Toggle to the other bus */
  86. test SSTAT0, SELDO|SELDI jnz selection;
  87. test SCSISEQ, ENSELO jnz poll_for_work;
  88. xor SBLKCTL,SELBUSB; /* Toggle back */
  89. }
  90. cmp WAITING_SCBH,SCB_LIST_NULL jne start_waiting;
  91. test_queue:
  92. /* Has the driver posted any work for us? */
  93. if ((p->features & AHC_QUEUE_REGS) != 0) {
  94. test QOFF_CTLSTA, SCB_AVAIL jz poll_for_work_loop;
  95. mov NONE, SNSCB_QOFF;
  96. inc QINPOS;
  97. } else {
  98. or SEQCTL, PAUSEDIS;
  99. cmp KERNEL_QINPOS, A je poll_for_work_loop;
  100. inc QINPOS;
  101. and SEQCTL, ~PAUSEDIS;
  102. }
  103. /*
  104. * We have at least one queued SCB now and we don't have any
  105. * SCBs in the list of SCBs awaiting selection. If we have
  106. * any SCBs available for use, pull the tag from the QINFIFO
  107. * and get to work on it.
  108. */
  109. if ((p->flags & AHC_PAGESCBS) != 0) {
  110. mov ALLZEROS call get_free_or_disc_scb;
  111. }
  112. dequeue_scb:
  113. add A, -1, QINPOS;
  114. mvi QINFIFO_OFFSET call fetch_byte;
  115. if ((p->flags & AHC_PAGESCBS) == 0) {
  116. /* In the non-paging case, the SCBID == hardware SCB index */
  117. mov SCBPTR, RETURN_2;
  118. }
  119. dma_queued_scb:
  120. /*
  121. * DMA the SCB from host ram into the current SCB location.
  122. */
  123. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  124. mov RETURN_2 call dma_scb;
  125. /*
  126. * Preset the residual fields in case we never go through a data phase.
  127. * This isn't done by the host so we can avoid a DMA to clear these
  128. * fields for the normal case of I/O that completes without underrun
  129. * or overrun conditions.
  130. */
  131. if ((p->features & AHC_CMD_CHAN) != 0) {
  132. bmov SCB_RESID_DCNT, SCB_DATACNT, 3;
  133. } else {
  134. mov SCB_RESID_DCNT[0],SCB_DATACNT[0];
  135. mov SCB_RESID_DCNT[1],SCB_DATACNT[1];
  136. mov SCB_RESID_DCNT[2],SCB_DATACNT[2];
  137. }
  138. mov SCB_RESID_SGCNT, SCB_SGCOUNT;
  139. start_scb:
  140. /*
  141. * Place us on the waiting list in case our selection
  142. * doesn't win during bus arbitration.
  143. */
  144. mov SCB_NEXT,WAITING_SCBH;
  145. mov WAITING_SCBH, SCBPTR;
  146. start_waiting:
  147. /*
  148. * Pull the first entry off of the waiting SCB list.
  149. */
  150. mov SCBPTR, WAITING_SCBH;
  151. call start_selection;
  152. jmp poll_for_work;
  153. start_selection:
  154. if ((p->features & AHC_TWIN) != 0) {
  155. and SINDEX,~SELBUSB,SBLKCTL;/* Clear channel select bit */
  156. and A,SELBUSB,SCB_TCL; /* Get new channel bit */
  157. or SINDEX,A;
  158. mov SBLKCTL,SINDEX; /* select channel */
  159. }
  160. initialize_scsiid:
  161. if ((p->features & AHC_ULTRA2) != 0) {
  162. and A, TID, SCB_TCL; /* Get target ID */
  163. and SCSIID_ULTRA2, OID; /* Clear old target */
  164. or SCSIID_ULTRA2, A;
  165. } else {
  166. and A, TID, SCB_TCL; /* Get target ID */
  167. and SCSIID, OID; /* Clear old target */
  168. or SCSIID, A;
  169. }
  170. mov SCSIDATL, ALLZEROS; /* clear out the latched */
  171. /* data register, this */
  172. /* fixes a bug on some */
  173. /* controllers where the */
  174. /* last byte written to */
  175. /* this register can leak */
  176. /* onto the data bus at */
  177. /* bad times, such as during */
  178. /* selection timeouts */
  179. mvi SCSISEQ, ENSELO|ENAUTOATNO|ENRSELI|ENAUTOATNP ret;
  180. /*
  181. * Initialize Ultra mode setting and clear the SCSI channel.
  182. * SINDEX should contain any additional bit's the client wants
  183. * set in SXFRCTL0.
  184. */
  185. initialize_channel:
  186. or SXFRCTL0, CLRSTCNT|CLRCHN, SINDEX;
  187. if ((p->features & AHC_ULTRA) != 0) {
  188. ultra:
  189. mvi SINDEX, ULTRA_ENB+1;
  190. test SAVED_TCL, 0x80 jnz ultra_2; /* Target ID > 7 */
  191. dec SINDEX;
  192. ultra_2:
  193. mov FUNCTION1,SAVED_TCL;
  194. mov A,FUNCTION1;
  195. test SINDIR, A jz ndx_dtr;
  196. or SXFRCTL0, FAST20;
  197. }
  198. /*
  199. * Initialize SCSIRATE with the appropriate value for this target.
  200. * The SCSIRATE settings for each target are stored in an array
  201. * based at TARG_SCSIRATE.
  202. */
  203. ndx_dtr:
  204. shr A,4,SAVED_TCL;
  205. if ((p->features & AHC_TWIN) != 0) {
  206. test SBLKCTL,SELBUSB jz ndx_dtr_2;
  207. or SAVED_TCL, SELBUSB;
  208. or A,0x08; /* Channel B entries add 8 */
  209. ndx_dtr_2:
  210. }
  211. if ((p->features & AHC_ULTRA2) != 0) {
  212. add SINDEX, TARG_OFFSET, A;
  213. mov SCSIOFFSET, SINDIR;
  214. }
  215. add SINDEX,TARG_SCSIRATE,A;
  216. mov SCSIRATE,SINDIR ret;
  217. selection:
  218. test SSTAT0,SELDO jnz select_out;
  219. /*
  220. * Reselection has been initiated by a target. Make a note that we've been
  221. * reselected, but haven't seen an IDENTIFY message from the target yet.
  222. */
  223. initiator_reselect:
  224. mvi CLRSINT0, CLRSELDI;
  225. /* XXX test for and handle ONE BIT condition */
  226. and SAVED_TCL, SELID_MASK, SELID;
  227. mvi CLRSINT1,CLRBUSFREE;
  228. or SIMODE1, ENBUSFREE; /*
  229. * We aren't expecting a
  230. * bus free, so interrupt
  231. * the kernel driver if it
  232. * happens.
  233. */
  234. mvi SPIOEN call initialize_channel;
  235. mvi MSG_OUT, MSG_NOOP; /* No message to send */
  236. jmp ITloop;
  237. /*
  238. * After the selection, remove this SCB from the "waiting SCB"
  239. * list. This is achieved by simply moving our "next" pointer into
  240. * WAITING_SCBH. Our next pointer will be set to null the next time this
  241. * SCB is used, so don't bother with it now.
  242. */
  243. select_out:
  244. /* Turn off the selection hardware */
  245. mvi SCSISEQ, ENRSELI|ENAUTOATNP; /*
  246. * ATN on parity errors
  247. * for "in" phases
  248. */
  249. mvi CLRSINT0, CLRSELDO;
  250. mov SCBPTR, WAITING_SCBH;
  251. mov WAITING_SCBH,SCB_NEXT;
  252. mov SAVED_TCL, SCB_TCL;
  253. mvi CLRSINT1,CLRBUSFREE;
  254. or SIMODE1, ENBUSFREE; /*
  255. * We aren't expecting a
  256. * bus free, so interrupt
  257. * the kernel driver if it
  258. * happens.
  259. */
  260. mvi SPIOEN call initialize_channel;
  261. /*
  262. * As soon as we get a successful selection, the target should go
  263. * into the message out phase since we have ATN asserted.
  264. */
  265. mvi MSG_OUT, MSG_IDENTIFYFLAG;
  266. or SEQ_FLAGS, IDENTIFY_SEEN;
  267. /*
  268. * Main loop for information transfer phases. Wait for the target
  269. * to assert REQ before checking MSG, C/D and I/O for the bus phase.
  270. */
  271. ITloop:
  272. call phase_lock;
  273. mov A, LASTPHASE;
  274. test A, ~P_DATAIN jz p_data;
  275. cmp A,P_COMMAND je p_command;
  276. cmp A,P_MESGOUT je p_mesgout;
  277. cmp A,P_STATUS je p_status;
  278. cmp A,P_MESGIN je p_mesgin;
  279. mvi INTSTAT,BAD_PHASE; /* unknown phase - signal driver */
  280. jmp ITloop; /* Try reading the bus again. */
  281. await_busfree:
  282. and SIMODE1, ~ENBUSFREE;
  283. call clear_target_state;
  284. mov NONE, SCSIDATL; /* Ack the last byte */
  285. and SXFRCTL0, ~SPIOEN;
  286. test SSTAT1,REQINIT|BUSFREE jz .;
  287. test SSTAT1, BUSFREE jnz poll_for_work;
  288. mvi INTSTAT, BAD_PHASE;
  289. clear_target_state:
  290. /*
  291. * We assume that the kernel driver may reset us
  292. * at any time, even in the middle of a DMA, so
  293. * clear DFCNTRL too.
  294. */
  295. clr DFCNTRL;
  296. /*
  297. * We don't know the target we will connect to,
  298. * so default to narrow transfers to avoid
  299. * parity problems.
  300. */
  301. if ((p->features & AHC_ULTRA2) != 0) {
  302. bmov SCSIRATE, ALLZEROS, 2;
  303. } else {
  304. clr SCSIRATE;
  305. and SXFRCTL0, ~(FAST20);
  306. }
  307. mvi LASTPHASE, P_BUSFREE;
  308. /* clear target specific flags */
  309. clr SEQ_FLAGS ret;
  310. data_phase_reinit:
  311. /*
  312. * If we re-enter the data phase after going through another phase, the
  313. * STCNT may have been cleared, so restore it from the residual field.
  314. * On Ultra2, we have to put it into the HCNT field because we have to
  315. * drop the data down into the shadow layer via the preload ability.
  316. */
  317. if ((p->features & AHC_ULTRA2) != 0) {
  318. bmov HADDR, SHADDR, 4;
  319. bmov HCNT, SCB_RESID_DCNT, 3;
  320. }
  321. if ((p->chip & AHC_CHIPID_MASK) == AHC_AIC7895) {
  322. bmov STCNT, SCB_RESID_DCNT, 3;
  323. }
  324. if ((p->features & AHC_CMD_CHAN) == 0) {
  325. mvi DINDEX, STCNT;
  326. mvi SCB_RESID_DCNT call bcopy_3;
  327. }
  328. jmp data_phase_loop;
  329. p_data:
  330. if ((p->features & AHC_ULTRA2) != 0) {
  331. mvi DMAPARAMS, PRELOADEN|SCSIEN|HDMAEN;
  332. } else {
  333. mvi DMAPARAMS, WIDEODD|SCSIEN|SDMAEN|HDMAEN|FIFORESET;
  334. }
  335. test LASTPHASE, IOI jnz . + 2;
  336. or DMAPARAMS, DIRECTION;
  337. call assert; /*
  338. * Ensure entering a data
  339. * phase is okay - seen identify, etc.
  340. */
  341. if ((p->features & AHC_CMD_CHAN) != 0) {
  342. mvi CCSGADDR, CCSGADDR_MAX;
  343. }
  344. test SEQ_FLAGS, DPHASE jnz data_phase_reinit;
  345. or SEQ_FLAGS, DPHASE; /* we've seen a data phase */
  346. /*
  347. * Initialize the DMA address and counter from the SCB.
  348. * Also set SG_COUNT and SG_NEXT in memory since we cannot
  349. * modify the values in the SCB itself until we see a
  350. * save data pointers message.
  351. */
  352. if ((p->features & AHC_CMD_CHAN) != 0) {
  353. bmov HADDR, SCB_DATAPTR, 7;
  354. bmov SG_COUNT, SCB_SGCOUNT, 5;
  355. if ((p->features & AHC_ULTRA2) == 0) {
  356. bmov STCNT, HCNT, 3;
  357. }
  358. } else {
  359. mvi DINDEX, HADDR;
  360. mvi SCB_DATAPTR call bcopy_7;
  361. call set_stcnt_from_hcnt;
  362. mvi DINDEX, SG_COUNT;
  363. mvi SCB_SGCOUNT call bcopy_5;
  364. }
  365. data_phase_loop:
  366. /* Guard against overruns */
  367. test SG_COUNT, 0xff jnz data_phase_inbounds;
  368. /*
  369. * Turn on 'Bit Bucket' mode, set the transfer count to
  370. * 16meg and let the target run until it changes phase.
  371. * When the transfer completes, notify the host that we
  372. * had an overrun.
  373. */
  374. or SXFRCTL1,BITBUCKET;
  375. and DMAPARAMS, ~(HDMAEN|SDMAEN);
  376. if ((p->features & AHC_ULTRA2) != 0) {
  377. bmov HCNT, ALLONES, 3;
  378. }
  379. if ((p->chip & AHC_CHIPID_MASK) == AHC_AIC7895) {
  380. bmov STCNT, ALLONES, 3;
  381. }
  382. if ((p->features & AHC_CMD_CHAN) == 0) {
  383. mvi STCNT[0], 0xFF;
  384. mvi STCNT[1], 0xFF;
  385. mvi STCNT[2], 0xFF;
  386. }
  387. data_phase_inbounds:
  388. /* If we are the last SG block, tell the hardware. */
  389. if ((p->features & AHC_ULTRA2) != 0) {
  390. shl A, 2, SG_COUNT;
  391. cmp SG_COUNT,0x01 jne data_phase_wideodd;
  392. or A, LAST_SEG;
  393. } else {
  394. cmp SG_COUNT,0x01 jne data_phase_wideodd;
  395. and DMAPARAMS, ~WIDEODD;
  396. }
  397. data_phase_wideodd:
  398. if ((p->features & AHC_ULTRA2) != 0) {
  399. mov SG_CACHEPTR, A;
  400. mov DFCNTRL, DMAPARAMS; /* start the operation */
  401. test SXFRCTL1, BITBUCKET jnz data_phase_overrun;
  402. u2_preload_wait:
  403. test SSTAT1, PHASEMIS jnz u2_phasemis;
  404. test DFSTATUS, PRELOAD_AVAIL jz u2_preload_wait;
  405. } else {
  406. mov DMAPARAMS call dma;
  407. data_phase_dma_done:
  408. /* Go tell the host about any overruns */
  409. test SXFRCTL1,BITBUCKET jnz data_phase_overrun;
  410. /* Exit if we had an underrun. dma clears SINDEX in this case. */
  411. test SINDEX,0xff jz data_phase_finish;
  412. }
  413. /*
  414. * Advance the scatter-gather pointers
  415. */
  416. sg_advance:
  417. if ((p->features & AHC_ULTRA2) != 0) {
  418. cmp SG_COUNT, 0x01 je u2_data_phase_finish;
  419. } else {
  420. dec SG_COUNT;
  421. test SG_COUNT, 0xff jz data_phase_finish;
  422. }
  423. if ((p->features & AHC_CMD_CHAN) != 0) {
  424. /*
  425. * Do we have any prefetch left???
  426. */
  427. cmp CCSGADDR, CCSGADDR_MAX jne prefetch_avail;
  428. /*
  429. * Fetch MIN(CCSGADDR_MAX, (SG_COUNT * 8)) bytes.
  430. */
  431. add A, -(CCSGRAM_MAXSEGS + 1), SG_COUNT;
  432. mvi A, CCSGADDR_MAX;
  433. jc . + 2;
  434. shl A, 3, SG_COUNT;
  435. mov CCHCNT, A;
  436. bmov CCHADDR, SG_NEXT, 4;
  437. mvi CCSGCTL, CCSGEN|CCSGRESET;
  438. test CCSGCTL, CCSGDONE jz .;
  439. and CCSGCTL, ~CCSGEN;
  440. test CCSGCTL, CCSGEN jnz .;
  441. mvi CCSGCTL, CCSGRESET;
  442. prefetch_avail:
  443. bmov HADDR, CCSGRAM, 8;
  444. if ((p->features & AHC_ULTRA2) == 0) {
  445. bmov STCNT, HCNT, 3;
  446. } else {
  447. dec SG_COUNT;
  448. }
  449. } else {
  450. mvi DINDEX, HADDR;
  451. mvi SG_NEXT call bcopy_4;
  452. mvi HCNT[0],SG_SIZEOF;
  453. clr HCNT[1];
  454. clr HCNT[2];
  455. or DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
  456. call dma_finish;
  457. /*
  458. * Copy data from FIFO into SCB data pointer and data count.
  459. * This assumes that the SG segments are of the form:
  460. * struct ahc_dma_seg {
  461. * u_int32_t addr; four bytes, little-endian order
  462. * u_int32_t len; four bytes, little endian order
  463. * };
  464. */
  465. mvi DINDEX, HADDR;
  466. call dfdat_in_7;
  467. call set_stcnt_from_hcnt;
  468. }
  469. /* Advance the SG pointer */
  470. clr A; /* add sizeof(struct scatter) */
  471. add SG_NEXT[0],SG_SIZEOF;
  472. adc SG_NEXT[1],A;
  473. if ((p->features & AHC_ULTRA2) != 0) {
  474. jmp data_phase_loop;
  475. } else {
  476. test SSTAT1, REQINIT jz .;
  477. test SSTAT1,PHASEMIS jz data_phase_loop;
  478. }
  479. /*
  480. * We've loaded all of our segments into the preload layer. Now, we simply
  481. * have to wait for it to finish or for us to get a phasemis. And, since
  482. * we'll get a phasemis if we do finish, all we really need to do is wait
  483. * for a phasemis then check if we did actually complete all the segments.
  484. */
  485. if ((p->features & AHC_ULTRA2) != 0) {
  486. u2_data_phase_finish:
  487. test SSTAT1, PHASEMIS jnz u2_phasemis;
  488. test SG_CACHEPTR, LAST_SEG_DONE jz u2_data_phase_finish;
  489. clr SG_COUNT;
  490. test SSTAT1, REQINIT jz .;
  491. test SSTAT1, PHASEMIS jz data_phase_loop;
  492. u2_phasemis:
  493. call ultra2_dmafinish;
  494. test SG_CACHEPTR, LAST_SEG_DONE jnz data_phase_finish;
  495. test SSTAT2, SHVALID jnz u2_fixup_residual;
  496. mvi INTSTAT, SEQ_SG_FIXUP;
  497. jmp data_phase_finish;
  498. u2_fixup_residual:
  499. shr ARG_1, 2, SG_CACHEPTR;
  500. u2_phasemis_loop:
  501. and A, 0x3f, SG_COUNT;
  502. cmp ARG_1, A je data_phase_finish;
  503. /*
  504. * Subtract SG_SIZEOF from the SG_NEXT pointer and add 1 to the SG_COUNT
  505. */
  506. clr A;
  507. add SG_NEXT[0], -SG_SIZEOF;
  508. adc SG_NEXT[1], 0xff;
  509. inc SG_COUNT;
  510. jmp u2_phasemis_loop;
  511. }
  512. data_phase_finish:
  513. /*
  514. * After a DMA finishes, save the SG and STCNT residuals back into the SCB
  515. * We use STCNT instead of HCNT, since it's a reflection of how many bytes
  516. * were transferred on the SCSI (as opposed to the host) bus.
  517. */
  518. if ((p->features & AHC_CMD_CHAN) != 0) {
  519. bmov SCB_RESID_DCNT, STCNT, 3;
  520. mov SCB_RESID_SGCNT, SG_COUNT;
  521. if ((p->features & AHC_ULTRA2) != 0) {
  522. or SXFRCTL0, CLRSTCNT|CLRCHN;
  523. }
  524. } else {
  525. mov SCB_RESID_DCNT[0],STCNT[0];
  526. mov SCB_RESID_DCNT[1],STCNT[1];
  527. mov SCB_RESID_DCNT[2],STCNT[2];
  528. mov SCB_RESID_SGCNT, SG_COUNT;
  529. }
  530. jmp ITloop;
  531. data_phase_overrun:
  532. /*
  533. * Turn off BITBUCKET mode and notify the host
  534. */
  535. if ((p->features & AHC_ULTRA2) != 0) {
  536. /*
  537. * Wait for the target to quit transferring data on the SCSI bus
  538. */
  539. test SSTAT1, PHASEMIS jz .;
  540. call ultra2_dmafinish;
  541. }
  542. and SXFRCTL1, ~BITBUCKET;
  543. mvi INTSTAT,DATA_OVERRUN;
  544. jmp ITloop;
  545. /*
  546. * Actually turn off the DMA hardware, save our current position into the
  547. * proper residual variables, wait for the next REQ signal, then jump to
  548. * the ITloop. Jumping to the ITloop ensures that if we happen to get
  549. * brought into the data phase again (or are still in it after our last
  550. * segment) that we will properly signal an overrun to the kernel.
  551. */
  552. if ((p->features & AHC_ULTRA2) != 0) {
  553. ultra2_dmafinish:
  554. test DFCNTRL, DIRECTION jnz ultra2_dmahalt;
  555. and DFCNTRL, ~SCSIEN;
  556. test DFCNTRL, SCSIEN jnz .;
  557. if ((p->bugs & AHC_BUG_AUTOFLUSH) != 0) {
  558. or DFCNTRL, FIFOFLUSH;
  559. }
  560. ultra2_dmafifoflush:
  561. if ((p->bugs & AHC_BUG_AUTOFLUSH) != 0) {
  562. /*
  563. * hardware bug alert! This needless set of jumps
  564. * works around a glitch in the silicon. When the
  565. * PCI DMA fifo goes empty, but there is still SCSI
  566. * data to be flushed into the PCI DMA fifo (and from
  567. * there on into main memory), the FIFOEMP bit will
  568. * come on between the time when the PCI DMA buffer
  569. * went empty and the next bit of data is copied from
  570. * the SCSI fifo into the PCI fifo. It should only
  571. * come on when both FIFOs (meaning the entire FIFO
  572. * chain) are empty. Since it can take up to 4 cycles
  573. * for new data to be copied from the SCSI fifo into
  574. * the PCI fifo, testing for FIFOEMP status for 4
  575. * extra times gives the needed time for any
  576. * remaining SCSI fifo data to be put in the PCI fifo
  577. * before we declare it *truly* empty.
  578. */
  579. test DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
  580. test DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
  581. test DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
  582. test DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
  583. }
  584. test DFSTATUS, FIFOEMP jz ultra2_dmafifoflush;
  585. test DFSTATUS, MREQPEND jnz .;
  586. ultra2_dmahalt:
  587. and DFCNTRL, ~(HDMAEN|SCSIEN);
  588. test DFCNTRL, (HDMAEN|SCSIEN) jnz .;
  589. ret;
  590. }
  591. /*
  592. * Command phase. Set up the DMA registers and let 'er rip.
  593. */
  594. p_command:
  595. call assert;
  596. /*
  597. * Load HADDR and HCNT.
  598. */
  599. if ((p->features & AHC_CMD_CHAN) != 0) {
  600. bmov HADDR, SCB_CMDPTR, 5;
  601. bmov HCNT[1], ALLZEROS, 2;
  602. if ((p->features & AHC_ULTRA2) == 0) {
  603. bmov STCNT, HCNT, 3;
  604. }
  605. } else {
  606. mvi DINDEX, HADDR;
  607. mvi SCB_CMDPTR call bcopy_5;
  608. clr HCNT[1];
  609. clr HCNT[2];
  610. call set_stcnt_from_hcnt;
  611. }
  612. if ((p->features & AHC_ULTRA2) == 0) {
  613. mvi (SCSIEN|SDMAEN|HDMAEN|DIRECTION|FIFORESET) call dma;
  614. } else {
  615. mvi DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN|DIRECTION);
  616. test SSTAT0, SDONE jnz .;
  617. p_command_dma_loop:
  618. test SSTAT0, SDONE jnz p_command_ultra2_dma_done;
  619. test SSTAT1,PHASEMIS jz p_command_dma_loop; /* ie. underrun */
  620. p_command_ultra2_dma_done:
  621. test SCSISIGI, REQI jz p_command_ultra2_shutdown;
  622. test SSTAT1, (PHASEMIS|REQINIT) jz p_command_ultra2_dma_done;
  623. p_command_ultra2_shutdown:
  624. and DFCNTRL, ~(HDMAEN|SCSIEN);
  625. test DFCNTRL, (HDMAEN|SCSIEN) jnz .;
  626. or SXFRCTL0, CLRSTCNT|CLRCHN;
  627. }
  628. jmp ITloop;
  629. /*
  630. * Status phase. Wait for the data byte to appear, then read it
  631. * and store it into the SCB.
  632. */
  633. p_status:
  634. call assert;
  635. mov SCB_TARGET_STATUS, SCSIDATL;
  636. jmp ITloop;
  637. /*
  638. * Message out phase. If MSG_OUT is 0x80, build I full indentify message
  639. * sequence and send it to the target. In addition, if the MK_MESSAGE bit
  640. * is set in the SCB_CONTROL byte, interrupt the host and allow it to send
  641. * it's own message.
  642. *
  643. * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
  644. * This is done to allow the hsot to send messages outside of an identify
  645. * sequence while protecting the seqencer from testing the MK_MESSAGE bit
  646. * on an SCB that might not be for the current nexus. (For example, a
  647. * BDR message in response to a bad reselection would leave us pointed to
  648. * an SCB that doesn't have anything to do with the current target).
  649. * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
  650. * bus device reset).
  651. *
  652. * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
  653. * in case the target decides to put us in this phase for some strange
  654. * reason.
  655. */
  656. p_mesgout_retry:
  657. or SCSISIGO,ATNO,LASTPHASE;/* turn on ATN for the retry */
  658. p_mesgout:
  659. mov SINDEX, MSG_OUT;
  660. cmp SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
  661. p_mesgout_identify:
  662. if ((p->features & AHC_WIDE) != 0) {
  663. and SINDEX,0xf,SCB_TCL; /* lun */
  664. } else {
  665. and SINDEX,0x7,SCB_TCL; /* lun */
  666. }
  667. and A,DISCENB,SCB_CONTROL; /* mask off disconnect privilege */
  668. or SINDEX,A; /* or in disconnect privilege */
  669. or SINDEX,MSG_IDENTIFYFLAG;
  670. p_mesgout_mk_message:
  671. test SCB_CONTROL,MK_MESSAGE jz p_mesgout_tag;
  672. mov SCSIDATL, SINDEX; /* Send the last byte */
  673. jmp p_mesgout_from_host + 1;/* Skip HOST_MSG test */
  674. /*
  675. * Send a tag message if TAG_ENB is set in the SCB control block.
  676. * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
  677. */
  678. p_mesgout_tag:
  679. test SCB_CONTROL,TAG_ENB jz p_mesgout_onebyte;
  680. mov SCSIDATL, SINDEX; /* Send the identify message */
  681. call phase_lock;
  682. cmp LASTPHASE, P_MESGOUT jne p_mesgout_done;
  683. and SCSIDATL,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
  684. call phase_lock;
  685. cmp LASTPHASE, P_MESGOUT jne p_mesgout_done;
  686. mov SCB_TAG jmp p_mesgout_onebyte;
  687. /*
  688. * Interrupt the driver, and allow it to send a message
  689. * if it asks.
  690. */
  691. p_mesgout_from_host:
  692. cmp SINDEX, HOST_MSG jne p_mesgout_onebyte;
  693. mvi INTSTAT,AWAITING_MSG;
  694. nop;
  695. /*
  696. * Did the host detect a phase change?
  697. */
  698. cmp RETURN_1, MSGOUT_PHASEMIS je p_mesgout_done;
  699. p_mesgout_onebyte:
  700. mvi CLRSINT1, CLRATNO;
  701. mov SCSIDATL, SINDEX;
  702. /*
  703. * If the next bus phase after ATN drops is a message out, it means
  704. * that the target is requesting that the last message(s) be resent.
  705. */
  706. call phase_lock;
  707. cmp LASTPHASE, P_MESGOUT je p_mesgout_retry;
  708. p_mesgout_done:
  709. mvi CLRSINT1,CLRATNO; /* Be sure to turn ATNO off */
  710. mov LAST_MSG, MSG_OUT;
  711. cmp MSG_OUT, MSG_IDENTIFYFLAG jne . + 2;
  712. and SCB_CONTROL, ~MK_MESSAGE;
  713. mvi MSG_OUT, MSG_NOOP; /* No message left */
  714. jmp ITloop;
  715. /*
  716. * Message in phase. Bytes are read using Automatic PIO mode.
  717. */
  718. p_mesgin:
  719. mvi ACCUM call inb_first; /* read the 1st message byte */
  720. test A,MSG_IDENTIFYFLAG jnz mesgin_identify;
  721. cmp A,MSG_DISCONNECT je mesgin_disconnect;
  722. cmp A,MSG_SAVEDATAPOINTER je mesgin_sdptrs;
  723. cmp ALLZEROS,A je mesgin_complete;
  724. cmp A,MSG_RESTOREPOINTERS je mesgin_rdptrs;
  725. cmp A,MSG_EXTENDED je mesgin_extended;
  726. cmp A,MSG_MESSAGE_REJECT je mesgin_reject;
  727. cmp A,MSG_NOOP je mesgin_done;
  728. cmp A,MSG_IGN_WIDE_RESIDUE je mesgin_wide_residue;
  729. rej_mesgin:
  730. /*
  731. * We have no idea what this message in is, so we issue a message reject
  732. * and hope for the best. In any case, rejection should be a rare
  733. * occurrence - signal the driver when it happens.
  734. */
  735. mvi INTSTAT,SEND_REJECT; /* let driver know */
  736. mvi MSG_MESSAGE_REJECT call mk_mesg;
  737. mesgin_done:
  738. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  739. jmp ITloop;
  740. mesgin_complete:
  741. /*
  742. * We got a "command complete" message, so put the SCB_TAG into the QOUTFIFO,
  743. * and trigger a completion interrupt. Before doing so, check to see if there
  744. * is a residual or the status byte is something other than STATUS_GOOD (0).
  745. * In either of these conditions, we upload the SCB back to the host so it can
  746. * process this information. In the case of a non zero status byte, we
  747. * additionally interrupt the kernel driver synchronously, allowing it to
  748. * decide if sense should be retrieved. If the kernel driver wishes to request
  749. * sense, it will fill the kernel SCB with a request sense command and set
  750. * RETURN_1 to SEND_SENSE. If RETURN_1 is set to SEND_SENSE we redownload
  751. * the SCB, and process it as the next command by adding it to the waiting list.
  752. * If the kernel driver does not wish to request sense, it need only clear
  753. * RETURN_1, and the command is allowed to complete normally. We don't bother
  754. * to post to the QOUTFIFO in the error cases since it would require extra
  755. * work in the kernel driver to ensure that the entry was removed before the
  756. * command complete code tried processing it.
  757. */
  758. /*
  759. * First check for residuals
  760. */
  761. test SCB_RESID_SGCNT,0xff jnz upload_scb;
  762. test SCB_TARGET_STATUS,0xff jz complete; /* Good Status? */
  763. upload_scb:
  764. mvi DMAPARAMS, FIFORESET;
  765. mov SCB_TAG call dma_scb;
  766. check_status:
  767. test SCB_TARGET_STATUS,0xff jz complete; /* Just a residual? */
  768. mvi INTSTAT,BAD_STATUS; /* let driver know */
  769. nop;
  770. cmp RETURN_1, SEND_SENSE jne complete;
  771. /* This SCB becomes the next to execute as it will retrieve sense */
  772. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  773. mov SCB_TAG call dma_scb;
  774. add_to_waiting_list:
  775. mov SCB_NEXT,WAITING_SCBH;
  776. mov WAITING_SCBH, SCBPTR;
  777. /*
  778. * Prepare our selection hardware before the busfree so we have a
  779. * high probability of winning arbitration.
  780. */
  781. call start_selection;
  782. jmp await_busfree;
  783. complete:
  784. /* If we are untagged, clear our address up in host ram */
  785. test SCB_CONTROL, TAG_ENB jnz complete_post;
  786. mov A, SAVED_TCL;
  787. mvi UNTAGGEDSCB_OFFSET call post_byte_setup;
  788. mvi SCB_LIST_NULL call post_byte;
  789. complete_post:
  790. /* Post the SCB and issue an interrupt */
  791. if ((p->features & AHC_QUEUE_REGS) != 0) {
  792. mov A, SDSCB_QOFF;
  793. } else {
  794. mov A, QOUTPOS;
  795. }
  796. mvi QOUTFIFO_OFFSET call post_byte_setup;
  797. mov SCB_TAG call post_byte;
  798. if ((p->features & AHC_QUEUE_REGS) == 0) {
  799. inc QOUTPOS;
  800. }
  801. mvi INTSTAT,CMDCMPLT;
  802. add_to_free_list:
  803. call add_scb_to_free_list;
  804. jmp await_busfree;
  805. /*
  806. * Is it an extended message? Copy the message to our message buffer and
  807. * notify the host. The host will tell us whether to reject this message,
  808. * respond to it with the message that the host placed in our message buffer,
  809. * or simply to do nothing.
  810. */
  811. mesgin_extended:
  812. mvi INTSTAT,EXTENDED_MSG; /* let driver know */
  813. jmp ITloop;
  814. /*
  815. * Is it a disconnect message? Set a flag in the SCB to remind us
  816. * and await the bus going free.
  817. */
  818. mesgin_disconnect:
  819. or SCB_CONTROL,DISCONNECTED;
  820. call add_scb_to_disc_list;
  821. jmp await_busfree;
  822. /*
  823. * Save data pointers message:
  824. * Copying RAM values back to SCB, for Save Data Pointers message, but
  825. * only if we've actually been into a data phase to change them. This
  826. * protects against bogus data in scratch ram and the residual counts
  827. * since they are only initialized when we go into data_in or data_out.
  828. */
  829. mesgin_sdptrs:
  830. test SEQ_FLAGS, DPHASE jz mesgin_done;
  831. /*
  832. * The SCB SGPTR becomes the next one we'll download,
  833. * and the SCB DATAPTR becomes the current SHADDR.
  834. * Use the residual number since STCNT is corrupted by
  835. * any message transfer.
  836. */
  837. if ((p->features & AHC_CMD_CHAN) != 0) {
  838. bmov SCB_SGCOUNT, SG_COUNT, 5;
  839. bmov SCB_DATAPTR, SHADDR, 4;
  840. bmov SCB_DATACNT, SCB_RESID_DCNT, 3;
  841. } else {
  842. mvi DINDEX, SCB_SGCOUNT;
  843. mvi SG_COUNT call bcopy_5;
  844. mvi DINDEX, SCB_DATAPTR;
  845. mvi SHADDR call bcopy_4;
  846. mvi SCB_RESID_DCNT call bcopy_3;
  847. }
  848. jmp mesgin_done;
  849. /*
  850. * Restore pointers message? Data pointers are recopied from the
  851. * SCB anytime we enter a data phase for the first time, so all
  852. * we need to do is clear the DPHASE flag and let the data phase
  853. * code do the rest.
  854. */
  855. mesgin_rdptrs:
  856. and SEQ_FLAGS, ~DPHASE; /*
  857. * We'll reload them
  858. * the next time through
  859. * the dataphase.
  860. */
  861. jmp mesgin_done;
  862. /*
  863. * Identify message? For a reconnecting target, this tells us the lun
  864. * that the reconnection is for - find the correct SCB and switch to it,
  865. * clearing the "disconnected" bit so we don't "find" it by accident later.
  866. */
  867. mesgin_identify:
  868. if ((p->features & AHC_WIDE) != 0) {
  869. and A,0x0f; /* lun in lower four bits */
  870. } else {
  871. and A,0x07; /* lun in lower three bits */
  872. }
  873. or SAVED_TCL,A; /* SAVED_TCL should be complete now */
  874. mvi ARG_2, SCB_LIST_NULL; /* SCBID of prev SCB in disc List */
  875. call get_untagged_SCBID;
  876. cmp ARG_1, SCB_LIST_NULL je snoop_tag;
  877. if ((p->flags & AHC_PAGESCBS) != 0) {
  878. test SEQ_FLAGS, SCBPTR_VALID jz use_retrieveSCB;
  879. }
  880. /*
  881. * If the SCB was found in the disconnected list (as is
  882. * always the case in non-paging scenarios), SCBPTR is already
  883. * set to the correct SCB. So, simply setup the SCB and get
  884. * on with things.
  885. */
  886. mov SCBPTR call rem_scb_from_disc_list;
  887. jmp setup_SCB;
  888. /*
  889. * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
  890. * If we get one, we use the tag returned to find the proper
  891. * SCB. With SCB paging, this requires using search for both tagged
  892. * and non-tagged transactions since the SCB may exist in any slot.
  893. * If we're not using SCB paging, we can use the tag as the direct
  894. * index to the SCB.
  895. */
  896. snoop_tag:
  897. mov NONE,SCSIDATL; /* ACK Identify MSG */
  898. snoop_tag_loop:
  899. call phase_lock;
  900. cmp LASTPHASE, P_MESGIN jne not_found;
  901. cmp SCSIBUSL,MSG_SIMPLE_Q_TAG jne not_found;
  902. get_tag:
  903. mvi ARG_1 call inb_next; /* tag value */
  904. use_retrieveSCB:
  905. call retrieveSCB;
  906. setup_SCB:
  907. mov A, SAVED_TCL;
  908. cmp SCB_TCL, A jne not_found_cleanup_scb;
  909. test SCB_CONTROL,DISCONNECTED jz not_found_cleanup_scb;
  910. and SCB_CONTROL,~DISCONNECTED;
  911. or SEQ_FLAGS,IDENTIFY_SEEN; /* make note of IDENTIFY */
  912. /* See if the host wants to send a message upon reconnection */
  913. test SCB_CONTROL, MK_MESSAGE jz mesgin_done;
  914. and SCB_CONTROL, ~MK_MESSAGE;
  915. mvi HOST_MSG call mk_mesg;
  916. jmp mesgin_done;
  917. not_found_cleanup_scb:
  918. test SCB_CONTROL, DISCONNECTED jz . + 3;
  919. call add_scb_to_disc_list;
  920. jmp not_found;
  921. call add_scb_to_free_list;
  922. not_found:
  923. mvi INTSTAT, NO_MATCH;
  924. mvi MSG_BUS_DEV_RESET call mk_mesg;
  925. jmp mesgin_done;
  926. /*
  927. * Message reject? Let the kernel driver handle this. If we have an
  928. * outstanding WDTR or SDTR negotiation, assume that it's a response from
  929. * the target selecting 8bit or asynchronous transfer, otherwise just ignore
  930. * it since we have no clue what it pertains to.
  931. */
  932. mesgin_reject:
  933. mvi INTSTAT, REJECT_MSG;
  934. jmp mesgin_done;
  935. /*
  936. * Wide Residue. We handle the simple cases, but pass of the one hard case
  937. * to the kernel (when the residue byte happened to cause us to advance our
  938. * sg element array, so we know have to back that advance out).
  939. */
  940. mesgin_wide_residue:
  941. mvi ARG_1 call inb_next; /* ACK the wide_residue and get */
  942. /* the size byte */
  943. /*
  944. * In order for this to be reliable, we have to do all sorts of horrible
  945. * magic in terms of resetting the datafifo and reloading the shadow layer
  946. * with the correct new values (so that a subsequent save data pointers
  947. * message will do the right thing). We let the kernel do that work.
  948. */
  949. mvi INTSTAT, WIDE_RESIDUE;
  950. jmp mesgin_done;
  951. /*
  952. * [ ADD MORE MESSAGE HANDLING HERE ]
  953. */
  954. /*
  955. * Locking the driver out, build a one-byte message passed in SINDEX
  956. * if there is no active message already. SINDEX is returned intact.
  957. */
  958. mk_mesg:
  959. or SCSISIGO,ATNO,LASTPHASE;/* turn on ATNO */
  960. mov MSG_OUT,SINDEX ret;
  961. /*
  962. * Functions to read data in Automatic PIO mode.
  963. *
  964. * According to Adaptec's documentation, an ACK is not sent on input from
  965. * the target until SCSIDATL is read from. So we wait until SCSIDATL is
  966. * latched (the usual way), then read the data byte directly off the bus
  967. * using SCSIBUSL. When we have pulled the ATN line, or we just want to
  968. * acknowledge the byte, then we do a dummy read from SCISDATL. The SCSI
  969. * spec guarantees that the target will hold the data byte on the bus until
  970. * we send our ACK.
  971. *
  972. * The assumption here is that these are called in a particular sequence,
  973. * and that REQ is already set when inb_first is called. inb_{first,next}
  974. * use the same calling convention as inb.
  975. */
  976. inb_next:
  977. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  978. inb_next_wait:
  979. /*
  980. * If there is a parity error, wait for the kernel to
  981. * see the interrupt and prepare our message response
  982. * before continuing.
  983. */
  984. test SSTAT1, REQINIT jz inb_next_wait;
  985. test SSTAT1, SCSIPERR jnz .;
  986. and LASTPHASE, PHASE_MASK, SCSISIGI;
  987. cmp LASTPHASE, P_MESGIN jne mesgin_phasemis;
  988. inb_first:
  989. mov DINDEX,SINDEX;
  990. mov DINDIR,SCSIBUSL ret; /*read byte directly from bus*/
  991. inb_last:
  992. mov NONE,SCSIDATL ret; /*dummy read from latch to ACK*/
  993. mesgin_phasemis:
  994. /*
  995. * We expected to receive another byte, but the target changed phase
  996. */
  997. mvi INTSTAT, MSGIN_PHASEMIS;
  998. jmp ITloop;
  999. /*
  1000. * DMA data transfer. HADDR and HCNT must be loaded first, and
  1001. * SINDEX should contain the value to load DFCNTRL with - 0x3d for
  1002. * host->scsi, or 0x39 for scsi->host. The SCSI channel is cleared
  1003. * during initialization.
  1004. */
  1005. if ((p->features & AHC_ULTRA2) == 0) {
  1006. dma:
  1007. mov DFCNTRL,SINDEX;
  1008. dma_loop:
  1009. test SSTAT0,DMADONE jnz dma_dmadone;
  1010. test SSTAT1,PHASEMIS jz dma_loop; /* ie. underrun */
  1011. dma_phasemis:
  1012. test SSTAT0,SDONE jnz dma_checkfifo;
  1013. mov SINDEX,ALLZEROS; /* Notify caller of phasemiss */
  1014. /*
  1015. * We will be "done" DMAing when the transfer count goes to zero, or
  1016. * the target changes the phase (in light of this, it makes sense that
  1017. * the DMA circuitry doesn't ACK when PHASEMIS is active). If we are
  1018. * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
  1019. * magically on STCNT=0 or a phase change, so just wait for FIFO empty
  1020. * status.
  1021. */
  1022. dma_checkfifo:
  1023. test DFCNTRL,DIRECTION jnz dma_fifoempty;
  1024. dma_fifoflush:
  1025. test DFSTATUS,FIFOEMP jz dma_fifoflush;
  1026. dma_fifoempty:
  1027. /* Don't clobber an inprogress host data transfer */
  1028. test DFSTATUS, MREQPEND jnz dma_fifoempty;
  1029. /*
  1030. * Now shut the DMA enables off and make sure that the DMA enables are
  1031. * actually off first lest we get an ILLSADDR.
  1032. */
  1033. dma_dmadone:
  1034. cmp LASTPHASE, P_COMMAND je dma_await_nreq;
  1035. test SCSIRATE, 0x0f jnz dma_shutdown;
  1036. dma_await_nreq:
  1037. test SCSISIGI, REQI jz dma_shutdown;
  1038. test SSTAT1, (PHASEMIS|REQINIT) jz dma_await_nreq;
  1039. dma_shutdown:
  1040. and DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
  1041. dma_halt:
  1042. /*
  1043. * Some revisions of the aic7880 have a problem where, if the
  1044. * data fifo is full, but the PCI input latch is not empty,
  1045. * HDMAEN cannot be cleared. The fix used here is to attempt
  1046. * to drain the data fifo until there is space for the input
  1047. * latch to drain and HDMAEN de-asserts.
  1048. */
  1049. if ((p->bugs & AHC_BUG_PCI_2_1_RETRY) != 0) {
  1050. mov NONE, DFDAT;
  1051. }
  1052. test DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz dma_halt;
  1053. }
  1054. return:
  1055. ret;
  1056. /*
  1057. * Assert that if we've been reselected, then we've seen an IDENTIFY
  1058. * message.
  1059. */
  1060. assert:
  1061. test SEQ_FLAGS,IDENTIFY_SEEN jnz return; /* seen IDENTIFY? */
  1062. mvi INTSTAT,NO_IDENT ret; /* no - tell the kernel */
  1063. /*
  1064. * Locate a disconnected SCB either by SAVED_TCL (ARG_1 is SCB_LIST_NULL)
  1065. * or by the SCBID ARG_1. The search begins at the SCB index passed in
  1066. * via SINDEX which is an SCB that must be on the disconnected list. If
  1067. * the SCB cannot be found, SINDEX will be SCB_LIST_NULL, otherwise, SCBPTR
  1068. * is set to the proper SCB.
  1069. */
  1070. findSCB:
  1071. mov SCBPTR,SINDEX; /* Initialize SCBPTR */
  1072. cmp ARG_1, SCB_LIST_NULL jne findSCB_by_SCBID;
  1073. mov A, SAVED_TCL;
  1074. mvi SCB_TCL jmp findSCB_loop; /* &SCB_TCL -> SINDEX */
  1075. findSCB_by_SCBID:
  1076. mov A, ARG_1; /* Tag passed in ARG_1 */
  1077. mvi SCB_TAG jmp findSCB_loop; /* &SCB_TAG -> SINDEX */
  1078. findSCB_next:
  1079. mov ARG_2, SCBPTR;
  1080. cmp SCB_NEXT, SCB_LIST_NULL je notFound;
  1081. mov SCBPTR,SCB_NEXT;
  1082. dec SINDEX; /* Last comparison moved us too far */
  1083. findSCB_loop:
  1084. cmp SINDIR, A jne findSCB_next;
  1085. mov SINDEX, SCBPTR ret;
  1086. notFound:
  1087. mvi SINDEX, SCB_LIST_NULL ret;
  1088. /*
  1089. * Retrieve an SCB by SCBID first searching the disconnected list falling
  1090. * back to DMA'ing the SCB down from the host. This routine assumes that
  1091. * ARG_1 is the SCBID of interest and that SINDEX is the position in the
  1092. * disconnected list to start the search from. If SINDEX is SCB_LIST_NULL,
  1093. * we go directly to the host for the SCB.
  1094. */
  1095. retrieveSCB:
  1096. test SEQ_FLAGS, SCBPTR_VALID jz retrieve_from_host;
  1097. mov SCBPTR call findSCB; /* Continue the search */
  1098. cmp SINDEX, SCB_LIST_NULL je retrieve_from_host;
  1099. /*
  1100. * This routine expects SINDEX to contain the index of the SCB to be
  1101. * removed, SCBPTR to be pointing to that SCB, and ARG_2 to be the
  1102. * SCBID of the SCB just previous to this one in the list or SCB_LIST_NULL
  1103. * if it is at the head.
  1104. */
  1105. rem_scb_from_disc_list:
  1106. /* Remove this SCB from the disconnection list */
  1107. cmp ARG_2, SCB_LIST_NULL je rHead;
  1108. mov DINDEX, SCB_NEXT;
  1109. mov SCBPTR, ARG_2;
  1110. mov SCB_NEXT, DINDEX;
  1111. mov SCBPTR, SINDEX ret;
  1112. rHead:
  1113. mov DISCONNECTED_SCBH,SCB_NEXT ret;
  1114. retrieve_from_host:
  1115. /*
  1116. * We didn't find it. Pull an SCB and DMA down the one we want.
  1117. * We should never get here in the non-paging case.
  1118. */
  1119. mov ALLZEROS call get_free_or_disc_scb;
  1120. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  1121. /* Jump instead of call as we want to return anyway */
  1122. mov ARG_1 jmp dma_scb;
  1123. /*
  1124. * Determine whether a target is using tagged or non-tagged transactions
  1125. * by first looking for a matching transaction based on the TCL and if
  1126. * that fails, looking up this device in the host's untagged SCB array.
  1127. * The TCL to search for is assumed to be in SAVED_TCL. The value is
  1128. * returned in ARG_1 (SCB_LIST_NULL for tagged, SCBID for non-tagged).
  1129. * The SCBPTR_VALID bit is set in SEQ_FLAGS if we found the information
  1130. * in an SCB instead of having to go to the host.
  1131. */
  1132. get_untagged_SCBID:
  1133. cmp DISCONNECTED_SCBH, SCB_LIST_NULL je get_SCBID_from_host;
  1134. mvi ARG_1, SCB_LIST_NULL;
  1135. mov DISCONNECTED_SCBH call findSCB;
  1136. cmp SINDEX, SCB_LIST_NULL je get_SCBID_from_host;
  1137. or SEQ_FLAGS, SCBPTR_VALID;/* Was in disconnected list */
  1138. test SCB_CONTROL, TAG_ENB jnz . + 2;
  1139. mov ARG_1, SCB_TAG ret;
  1140. mvi ARG_1, SCB_LIST_NULL ret;
  1141. /*
  1142. * Fetch a byte from host memory given an index of (A + (256 * SINDEX))
  1143. * and a base address of SCBID_ADDR. The byte is returned in RETURN_2.
  1144. */
  1145. fetch_byte:
  1146. mov ARG_2, SINDEX;
  1147. if ((p->features & AHC_CMD_CHAN) != 0) {
  1148. mvi DINDEX, CCHADDR;
  1149. mvi SCBID_ADDR call set_1byte_addr;
  1150. mvi CCHCNT, 1;
  1151. mvi CCSGCTL, CCSGEN|CCSGRESET;
  1152. test CCSGCTL, CCSGDONE jz .;
  1153. mvi CCSGCTL, CCSGRESET;
  1154. bmov RETURN_2, CCSGRAM, 1 ret;
  1155. } else {
  1156. mvi DINDEX, HADDR;
  1157. mvi SCBID_ADDR call set_1byte_addr;
  1158. mvi HCNT[0], 1;
  1159. clr HCNT[1];
  1160. clr HCNT[2];
  1161. mvi DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
  1162. call dma_finish;
  1163. mov RETURN_2, DFDAT ret;
  1164. }
  1165. /*
  1166. * Prepare the hardware to post a byte to host memory given an
  1167. * index of (A + (256 * SINDEX)) and a base address of SCBID_ADDR.
  1168. */
  1169. post_byte_setup:
  1170. mov ARG_2, SINDEX;
  1171. if ((p->features & AHC_CMD_CHAN) != 0) {
  1172. mvi DINDEX, CCHADDR;
  1173. mvi SCBID_ADDR call set_1byte_addr;
  1174. mvi CCHCNT, 1;
  1175. mvi CCSCBCTL, CCSCBRESET ret;
  1176. } else {
  1177. mvi DINDEX, HADDR;
  1178. mvi SCBID_ADDR call set_1byte_addr;
  1179. mvi HCNT[0], 1;
  1180. clr HCNT[1];
  1181. clr HCNT[2];
  1182. mvi DFCNTRL, FIFORESET ret;
  1183. }
  1184. post_byte:
  1185. if ((p->features & AHC_CMD_CHAN) != 0) {
  1186. bmov CCSCBRAM, SINDEX, 1;
  1187. or CCSCBCTL, CCSCBEN|CCSCBRESET;
  1188. test CCSCBCTL, CCSCBDONE jz .;
  1189. clr CCSCBCTL ret;
  1190. } else {
  1191. mov DFDAT, SINDEX;
  1192. or DFCNTRL, HDMAEN|FIFOFLUSH;
  1193. jmp dma_finish;
  1194. }
  1195. get_SCBID_from_host:
  1196. mov A, SAVED_TCL;
  1197. mvi UNTAGGEDSCB_OFFSET call fetch_byte;
  1198. mov RETURN_1, RETURN_2 ret;
  1199. phase_lock:
  1200. test SSTAT1, REQINIT jz phase_lock;
  1201. test SSTAT1, SCSIPERR jnz phase_lock;
  1202. and SCSISIGO, PHASE_MASK, SCSISIGI;
  1203. and LASTPHASE, PHASE_MASK, SCSISIGI ret;
  1204. if ((p->features & AHC_CMD_CHAN) == 0) {
  1205. set_stcnt_from_hcnt:
  1206. mov STCNT[0], HCNT[0];
  1207. mov STCNT[1], HCNT[1];
  1208. mov STCNT[2], HCNT[2] ret;
  1209. bcopy_7:
  1210. mov DINDIR, SINDIR;
  1211. mov DINDIR, SINDIR;
  1212. bcopy_5:
  1213. mov DINDIR, SINDIR;
  1214. bcopy_4:
  1215. mov DINDIR, SINDIR;
  1216. bcopy_3:
  1217. mov DINDIR, SINDIR;
  1218. mov DINDIR, SINDIR;
  1219. mov DINDIR, SINDIR ret;
  1220. }
  1221. /*
  1222. * Setup addr assuming that A is an index into
  1223. * an array of 32byte objects, SINDEX contains
  1224. * the base address of that array, and DINDEX
  1225. * contains the base address of the location
  1226. * to store the indexed address.
  1227. */
  1228. set_32byte_addr:
  1229. shr ARG_2, 3, A;
  1230. shl A, 5;
  1231. /*
  1232. * Setup addr assuming that A + (ARG_1 * 256) is an
  1233. * index into an array of 1byte objects, SINDEX contains
  1234. * the base address of that array, and DINDEX contains
  1235. * the base address of the location to store the computed
  1236. * address.
  1237. */
  1238. set_1byte_addr:
  1239. add DINDIR, A, SINDIR;
  1240. mov A, ARG_2;
  1241. adc DINDIR, A, SINDIR;
  1242. clr A;
  1243. adc DINDIR, A, SINDIR;
  1244. adc DINDIR, A, SINDIR ret;
  1245. /*
  1246. * Either post or fetch and SCB from host memory based on the
  1247. * DIRECTION bit in DMAPARAMS. The host SCB index is in SINDEX.
  1248. */
  1249. dma_scb:
  1250. mov A, SINDEX;
  1251. if ((p->features & AHC_CMD_CHAN) != 0) {
  1252. mvi DINDEX, CCHADDR;
  1253. mvi HSCB_ADDR call set_32byte_addr;
  1254. mov CCSCBPTR, SCBPTR;
  1255. mvi CCHCNT, 32;
  1256. test DMAPARAMS, DIRECTION jz dma_scb_tohost;
  1257. mvi CCSCBCTL, CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET;
  1258. cmp CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN|CCSCBDIR jne .;
  1259. jmp dma_scb_finish;
  1260. dma_scb_tohost:
  1261. if ((p->features & AHC_ULTRA2) == 0) {
  1262. mvi CCSCBCTL, CCSCBRESET;
  1263. bmov CCSCBRAM, SCB_CONTROL, 32;
  1264. or CCSCBCTL, CCSCBEN|CCSCBRESET;
  1265. test CCSCBCTL, CCSCBDONE jz .;
  1266. }
  1267. if ((p->features & AHC_ULTRA2) != 0) {
  1268. if ((p->bugs & AHC_BUG_SCBCHAN_UPLOAD) != 0) {
  1269. mvi CCSCBCTL, CCARREN|CCSCBRESET;
  1270. cmp CCSCBCTL, ARRDONE|CCARREN jne .;
  1271. mvi CCHCNT, 32;
  1272. mvi CCSCBCTL, CCSCBEN|CCSCBRESET;
  1273. cmp CCSCBCTL, CCSCBDONE|CCSCBEN jne .;
  1274. } else {
  1275. mvi CCSCBCTL, CCARREN|CCSCBEN|CCSCBRESET;
  1276. cmp CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN jne .;
  1277. }
  1278. }
  1279. dma_scb_finish:
  1280. clr CCSCBCTL;
  1281. test CCSCBCTL, CCARREN|CCSCBEN jnz .;
  1282. ret;
  1283. }
  1284. if ((p->features & AHC_CMD_CHAN) == 0) {
  1285. mvi DINDEX, HADDR;
  1286. mvi HSCB_ADDR call set_32byte_addr;
  1287. mvi HCNT[0], 32;
  1288. clr HCNT[1];
  1289. clr HCNT[2];
  1290. mov DFCNTRL, DMAPARAMS;
  1291. test DMAPARAMS, DIRECTION jnz dma_scb_fromhost;
  1292. /* Fill it with the SCB data */
  1293. copy_scb_tofifo:
  1294. mvi SINDEX, SCB_CONTROL;
  1295. add A, 32, SINDEX;
  1296. copy_scb_tofifo_loop:
  1297. mov DFDAT,SINDIR;
  1298. mov DFDAT,SINDIR;
  1299. mov DFDAT,SINDIR;
  1300. mov DFDAT,SINDIR;
  1301. mov DFDAT,SINDIR;
  1302. mov DFDAT,SINDIR;
  1303. mov DFDAT,SINDIR;
  1304. mov DFDAT,SINDIR;
  1305. cmp SINDEX, A jne copy_scb_tofifo_loop;
  1306. or DFCNTRL, HDMAEN|FIFOFLUSH;
  1307. jmp dma_finish;
  1308. dma_scb_fromhost:
  1309. mvi DINDEX, SCB_CONTROL;
  1310. if ((p->bugs & AHC_BUG_PCI_2_1_RETRY) != 0) {
  1311. /*
  1312. * Set the A to -24. It it hits 0, then we let
  1313. * our code fall through to dfdat_in_8 to complete
  1314. * the last of the copy.
  1315. *
  1316. * Also, things happen 8 bytes at a time in this
  1317. * case, so we may need to drain the fifo at most
  1318. * 3 times to keep things flowing
  1319. */
  1320. mvi A, -24;
  1321. dma_scb_hang_fifo:
  1322. /* Wait for the first bit of data to hit the fifo */
  1323. test DFSTATUS, FIFOEMP jnz .;
  1324. dma_scb_hang_wait:
  1325. /* OK, now they've started to transfer into the fifo,
  1326. * so wait for them to stop trying to transfer any
  1327. * more data.
  1328. */
  1329. test DFSTATUS, MREQPEND jnz .;
  1330. /*
  1331. * OK, they started, then they stopped, now see if they
  1332. * managed to complete the job before stopping. Try
  1333. * it multiple times to give the chip a few cycles to
  1334. * set the flag if it did complete.
  1335. */
  1336. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  1337. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  1338. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  1339. /*
  1340. * Too bad, the chip didn't complete the DMA, but there
  1341. * aren't any more memory requests pending, so that
  1342. * means it stopped part way through and hung. That's
  1343. * our bug, so now we drain what data there is in the
  1344. * fifo in order to get things going again.
  1345. */
  1346. dma_scb_hang_empty_fifo:
  1347. call dfdat_in_8;
  1348. add A, 8;
  1349. add SINDEX, A, HCNT;
  1350. /*
  1351. * If there are another 8 bytes of data waiting in the
  1352. * fifo, then the carry bit will be set as a result
  1353. * of the above add command (unless A is non-negative,
  1354. * in which case the carry bit won't be set).
  1355. */
  1356. jc dma_scb_hang_empty_fifo;
  1357. /*
  1358. * We've emptied the fifo now, but we wouldn't have got
  1359. * here if the memory transfer hadn't stopped part way
  1360. * through, so go back up to the beginning of the
  1361. * loop and start over. When it succeeds in getting
  1362. * all the data down, HDONE will be set and we'll
  1363. * jump to the code just below here.
  1364. */
  1365. jmp dma_scb_hang_fifo;
  1366. dma_scb_hang_dma_done:
  1367. and DFCNTRL, ~HDMAEN;
  1368. test DFCNTRL, HDMAEN jnz .;
  1369. call dfdat_in_8;
  1370. add A, 8;
  1371. cmp A, 8 jne . - 2;
  1372. ret;
  1373. } else {
  1374. call dma_finish;
  1375. call dfdat_in_8;
  1376. call dfdat_in_8;
  1377. call dfdat_in_8;
  1378. }
  1379. dfdat_in_8:
  1380. mov DINDIR,DFDAT;
  1381. dfdat_in_7:
  1382. mov DINDIR,DFDAT;
  1383. mov DINDIR,DFDAT;
  1384. mov DINDIR,DFDAT;
  1385. mov DINDIR,DFDAT;
  1386. mov DINDIR,DFDAT;
  1387. mov DINDIR,DFDAT;
  1388. mov DINDIR,DFDAT ret;
  1389. }
  1390. /*
  1391. * Wait for DMA from host memory to data FIFO to complete, then disable
  1392. * DMA and wait for it to acknowledge that it's off.
  1393. */
  1394. if ((p->features & AHC_CMD_CHAN) == 0) {
  1395. dma_finish:
  1396. test DFSTATUS,HDONE jz dma_finish;
  1397. /* Turn off DMA */
  1398. and DFCNTRL, ~HDMAEN;
  1399. test DFCNTRL, HDMAEN jnz .;
  1400. ret;
  1401. }
  1402. add_scb_to_free_list:
  1403. if ((p->flags & AHC_PAGESCBS) != 0) {
  1404. mov SCB_NEXT, FREE_SCBH;
  1405. mov FREE_SCBH, SCBPTR;
  1406. }
  1407. mvi SCB_TAG, SCB_LIST_NULL ret;
  1408. if ((p->flags & AHC_PAGESCBS) != 0) {
  1409. get_free_or_disc_scb:
  1410. cmp FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb;
  1411. cmp DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb;
  1412. return_error:
  1413. mvi SINDEX, SCB_LIST_NULL ret;
  1414. dequeue_disc_scb:
  1415. mov SCBPTR, DISCONNECTED_SCBH;
  1416. dma_up_scb:
  1417. mvi DMAPARAMS, FIFORESET;
  1418. mov SCB_TAG call dma_scb;
  1419. unlink_disc_scb:
  1420. mov DISCONNECTED_SCBH, SCB_NEXT ret;
  1421. dequeue_free_scb:
  1422. mov SCBPTR, FREE_SCBH;
  1423. mov FREE_SCBH, SCB_NEXT ret;
  1424. }
  1425. add_scb_to_disc_list:
  1426. /*
  1427. * Link this SCB into the DISCONNECTED list. This list holds the
  1428. * candidates for paging out an SCB if one is needed for a new command.
  1429. * Modifying the disconnected list is a critical(pause dissabled) section.
  1430. */
  1431. mov SCB_NEXT, DISCONNECTED_SCBH;
  1432. mov DISCONNECTED_SCBH, SCBPTR ret;