aic7xxx.seq 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  1. /*
  2. * Adaptec 274x/284x/294x device driver firmware for Linux and FreeBSD.
  3. *
  4. * Copyright (c) 1994-2001 Justin T. Gibbs.
  5. * Copyright (c) 2000-2001 Adaptec Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions, and the following disclaimer,
  13. * without modification.
  14. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  15. * substantially similar to the "NO WARRANTY" disclaimer below
  16. * ("Disclaimer") and any redistribution must be conditioned upon
  17. * including a substantially similar Disclaimer requirement for further
  18. * binary redistribution.
  19. * 3. Neither the names of the above-listed copyright holders nor the names
  20. * of any contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * Alternatively, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2 as published by the Free
  25. * Software Foundation.
  26. *
  27. * NO WARRANTY
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGES.
  39. *
  40. * $FreeBSD$
  41. */
  42. VERSION = "$Id: //depot/aic7xxx/aic7xxx/aic7xxx.seq#56 $"
  43. PATCH_ARG_LIST = "struct ahc_softc *ahc"
  44. PREFIX = "ahc_"
  45. #include "aic7xxx.reg"
  46. #include "scsi_message.h"
  47. /*
  48. * A few words on the waiting SCB list:
  49. * After starting the selection hardware, we check for reconnecting targets
  50. * as well as for our selection to complete just in case the reselection wins
  51. * bus arbitration. The problem with this is that we must keep track of the
  52. * SCB that we've already pulled from the QINFIFO and started the selection
  53. * on just in case the reselection wins so that we can retry the selection at
  54. * a later time. This problem cannot be resolved by holding a single entry
  55. * in scratch ram since a reconnecting target can request sense and this will
  56. * create yet another SCB waiting for selection. The solution used here is to
  57. * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
  58. * of SCBs that are awaiting selection. Since 0-0xfe are valid SCB indexes,
  59. * SCB_LIST_NULL is 0xff which is out of range. An entry is also added to
  60. * this list everytime a request sense occurs or after completing a non-tagged
  61. * command for which a second SCB has been queued. The sequencer will
  62. * automatically consume the entries.
  63. */
  64. bus_free_sel:
  65. /*
  66. * Turn off the selection hardware. We need to reset the
  67. * selection request in order to perform a new selection.
  68. */
  69. and SCSISEQ, TEMODE|ENSELI|ENRSELI|ENAUTOATNP;
  70. and SIMODE1, ~ENBUSFREE;
  71. poll_for_work:
  72. call clear_target_state;
  73. and SXFRCTL0, ~SPIOEN;
  74. if ((ahc->features & AHC_ULTRA2) != 0) {
  75. clr SCSIBUSL;
  76. }
  77. test SCSISEQ, ENSELO jnz poll_for_selection;
  78. if ((ahc->features & AHC_TWIN) != 0) {
  79. xor SBLKCTL,SELBUSB; /* Toggle to the other bus */
  80. test SCSISEQ, ENSELO jnz poll_for_selection;
  81. }
  82. cmp WAITING_SCBH,SCB_LIST_NULL jne start_waiting;
  83. poll_for_work_loop:
  84. if ((ahc->features & AHC_TWIN) != 0) {
  85. xor SBLKCTL,SELBUSB; /* Toggle to the other bus */
  86. }
  87. test SSTAT0, SELDO|SELDI jnz selection;
  88. test_queue:
  89. /* Has the driver posted any work for us? */
  90. BEGIN_CRITICAL;
  91. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  92. test QOFF_CTLSTA, SCB_AVAIL jz poll_for_work_loop;
  93. } else {
  94. mov A, QINPOS;
  95. cmp KERNEL_QINPOS, A je poll_for_work_loop;
  96. }
  97. mov ARG_1, NEXT_QUEUED_SCB;
  98. /*
  99. * We have at least one queued SCB now and we don't have any
  100. * SCBs in the list of SCBs awaiting selection. Allocate a
  101. * card SCB for the host's SCB and get to work on it.
  102. */
  103. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  104. mov ALLZEROS call get_free_or_disc_scb;
  105. } else {
  106. /* In the non-paging case, the SCBID == hardware SCB index */
  107. mov SCBPTR, ARG_1;
  108. }
  109. or SEQ_FLAGS2, SCB_DMA;
  110. END_CRITICAL;
  111. dma_queued_scb:
  112. /*
  113. * DMA the SCB from host ram into the current SCB location.
  114. */
  115. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  116. mov ARG_1 call dma_scb;
  117. /*
  118. * Check one last time to see if this SCB was canceled
  119. * before we completed the DMA operation. If it was,
  120. * the QINFIFO next pointer will not match our saved
  121. * value.
  122. */
  123. mov A, ARG_1;
  124. BEGIN_CRITICAL;
  125. cmp NEXT_QUEUED_SCB, A jne abort_qinscb;
  126. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  127. cmp SCB_TAG, A je . + 2;
  128. mvi SCB_MISMATCH call set_seqint;
  129. }
  130. mov NEXT_QUEUED_SCB, SCB_NEXT;
  131. mov SCB_NEXT,WAITING_SCBH;
  132. mov WAITING_SCBH, SCBPTR;
  133. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  134. mov NONE, SNSCB_QOFF;
  135. } else {
  136. inc QINPOS;
  137. }
  138. and SEQ_FLAGS2, ~SCB_DMA;
  139. END_CRITICAL;
  140. start_waiting:
  141. /*
  142. * Start the first entry on the waiting SCB list.
  143. */
  144. mov SCBPTR, WAITING_SCBH;
  145. call start_selection;
  146. poll_for_selection:
  147. /*
  148. * Twin channel devices cannot handle things like SELTO
  149. * interrupts on the "background" channel. So, while
  150. * selecting, keep polling the current channel until
  151. * either a selection or reselection occurs.
  152. */
  153. test SSTAT0, SELDO|SELDI jz poll_for_selection;
  154. selection:
  155. /*
  156. * We aren't expecting a bus free, so interrupt
  157. * the kernel driver if it happens.
  158. */
  159. mvi CLRSINT1,CLRBUSFREE;
  160. if ((ahc->features & AHC_DT) == 0) {
  161. or SIMODE1, ENBUSFREE;
  162. }
  163. /*
  164. * Guard against a bus free after (re)selection
  165. * but prior to enabling the busfree interrupt. SELDI
  166. * and SELDO will be cleared in that case.
  167. */
  168. test SSTAT0, SELDI|SELDO jz bus_free_sel;
  169. test SSTAT0,SELDO jnz select_out;
  170. select_in:
  171. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  172. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  173. test SSTAT0, TARGET jz initiator_reselect;
  174. }
  175. mvi CLRSINT0, CLRSELDI;
  176. /*
  177. * We've just been selected. Assert BSY and
  178. * setup the phase for receiving messages
  179. * from the target.
  180. */
  181. mvi SCSISIGO, P_MESGOUT|BSYO;
  182. /*
  183. * Setup the DMA for sending the identify and
  184. * command information.
  185. */
  186. mvi SEQ_FLAGS, CMDPHASE_PENDING;
  187. mov A, TQINPOS;
  188. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  189. mvi DINDEX, CCHADDR;
  190. mvi SHARED_DATA_ADDR call set_32byte_addr;
  191. mvi CCSCBCTL, CCSCBRESET;
  192. } else {
  193. mvi DINDEX, HADDR;
  194. mvi SHARED_DATA_ADDR call set_32byte_addr;
  195. mvi DFCNTRL, FIFORESET;
  196. }
  197. /* Initiator that selected us */
  198. and SAVED_SCSIID, SELID_MASK, SELID;
  199. /* The Target ID we were selected at */
  200. if ((ahc->features & AHC_MULTI_TID) != 0) {
  201. and A, OID, TARGIDIN;
  202. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  203. and A, OID, SCSIID_ULTRA2;
  204. } else {
  205. and A, OID, SCSIID;
  206. }
  207. or SAVED_SCSIID, A;
  208. if ((ahc->features & AHC_TWIN) != 0) {
  209. test SBLKCTL, SELBUSB jz . + 2;
  210. or SAVED_SCSIID, TWIN_CHNLB;
  211. }
  212. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  213. mov CCSCBRAM, SAVED_SCSIID;
  214. } else {
  215. mov DFDAT, SAVED_SCSIID;
  216. }
  217. /*
  218. * If ATN isn't asserted, the target isn't interested
  219. * in talking to us. Go directly to bus free.
  220. * XXX SCSI-1 may require us to assume lun 0 if
  221. * ATN is false.
  222. */
  223. test SCSISIGI, ATNI jz target_busfree;
  224. /*
  225. * Watch ATN closely now as we pull in messages from the
  226. * initiator. We follow the guidlines from section 6.5
  227. * of the SCSI-2 spec for what messages are allowed when.
  228. */
  229. call target_inb;
  230. /*
  231. * Our first message must be one of IDENTIFY, ABORT, or
  232. * BUS_DEVICE_RESET.
  233. */
  234. test DINDEX, MSG_IDENTIFYFLAG jz host_target_message_loop;
  235. /* Store for host */
  236. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  237. mov CCSCBRAM, DINDEX;
  238. } else {
  239. mov DFDAT, DINDEX;
  240. }
  241. and SAVED_LUN, MSG_IDENTIFY_LUNMASK, DINDEX;
  242. /* Remember for disconnection decision */
  243. test DINDEX, MSG_IDENTIFY_DISCFLAG jnz . + 2;
  244. /* XXX Honor per target settings too */
  245. or SEQ_FLAGS, NO_DISCONNECT;
  246. test SCSISIGI, ATNI jz ident_messages_done;
  247. call target_inb;
  248. /*
  249. * If this is a tagged request, the tagged message must
  250. * immediately follow the identify. We test for a valid
  251. * tag message by seeing if it is >= MSG_SIMPLE_Q_TAG and
  252. * < MSG_IGN_WIDE_RESIDUE.
  253. */
  254. add A, -MSG_SIMPLE_Q_TAG, DINDEX;
  255. jnc ident_messages_done_msg_pending;
  256. add A, -MSG_IGN_WIDE_RESIDUE, DINDEX;
  257. jc ident_messages_done_msg_pending;
  258. /* Store for host */
  259. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  260. mov CCSCBRAM, DINDEX;
  261. } else {
  262. mov DFDAT, DINDEX;
  263. }
  264. /*
  265. * If the initiator doesn't feel like providing a tag number,
  266. * we've got a failed selection and must transition to bus
  267. * free.
  268. */
  269. test SCSISIGI, ATNI jz target_busfree;
  270. /*
  271. * Store the tag for the host.
  272. */
  273. call target_inb;
  274. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  275. mov CCSCBRAM, DINDEX;
  276. } else {
  277. mov DFDAT, DINDEX;
  278. }
  279. mov INITIATOR_TAG, DINDEX;
  280. or SEQ_FLAGS, TARGET_CMD_IS_TAGGED;
  281. ident_messages_done:
  282. /* Terminate the ident list */
  283. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  284. mvi CCSCBRAM, SCB_LIST_NULL;
  285. } else {
  286. mvi DFDAT, SCB_LIST_NULL;
  287. }
  288. or SEQ_FLAGS, TARG_CMD_PENDING;
  289. test SEQ_FLAGS2, TARGET_MSG_PENDING
  290. jnz target_mesgout_pending;
  291. test SCSISIGI, ATNI jnz target_mesgout_continue;
  292. jmp target_ITloop;
  293. ident_messages_done_msg_pending:
  294. or SEQ_FLAGS2, TARGET_MSG_PENDING;
  295. jmp ident_messages_done;
  296. /*
  297. * Pushed message loop to allow the kernel to
  298. * run it's own target mode message state engine.
  299. */
  300. host_target_message_loop:
  301. mvi HOST_MSG_LOOP call set_seqint;
  302. cmp RETURN_1, EXIT_MSG_LOOP je target_ITloop;
  303. test SSTAT0, SPIORDY jz .;
  304. jmp host_target_message_loop;
  305. }
  306. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  307. /*
  308. * Reselection has been initiated by a target. Make a note that we've been
  309. * reselected, but haven't seen an IDENTIFY message from the target yet.
  310. */
  311. initiator_reselect:
  312. /* XXX test for and handle ONE BIT condition */
  313. or SXFRCTL0, SPIOEN|CLRSTCNT|CLRCHN;
  314. and SAVED_SCSIID, SELID_MASK, SELID;
  315. if ((ahc->features & AHC_ULTRA2) != 0) {
  316. and A, OID, SCSIID_ULTRA2;
  317. } else {
  318. and A, OID, SCSIID;
  319. }
  320. or SAVED_SCSIID, A;
  321. if ((ahc->features & AHC_TWIN) != 0) {
  322. test SBLKCTL, SELBUSB jz . + 2;
  323. or SAVED_SCSIID, TWIN_CHNLB;
  324. }
  325. mvi CLRSINT0, CLRSELDI;
  326. jmp ITloop;
  327. }
  328. abort_qinscb:
  329. call add_scb_to_free_list;
  330. jmp poll_for_work_loop;
  331. start_selection:
  332. /*
  333. * If bus reset interrupts have been disabled (from a previous
  334. * reset), re-enable them now. Resets are only of interest
  335. * when we have outstanding transactions, so we can safely
  336. * defer re-enabling the interrupt until, as an initiator,
  337. * we start sending out transactions again.
  338. */
  339. test SIMODE1, ENSCSIRST jnz . + 3;
  340. mvi CLRSINT1, CLRSCSIRSTI;
  341. or SIMODE1, ENSCSIRST;
  342. if ((ahc->features & AHC_TWIN) != 0) {
  343. and SINDEX,~SELBUSB,SBLKCTL;/* Clear channel select bit */
  344. test SCB_SCSIID, TWIN_CHNLB jz . + 2;
  345. or SINDEX, SELBUSB;
  346. mov SBLKCTL,SINDEX; /* select channel */
  347. }
  348. initialize_scsiid:
  349. if ((ahc->features & AHC_ULTRA2) != 0) {
  350. mov SCSIID_ULTRA2, SCB_SCSIID;
  351. } else if ((ahc->features & AHC_TWIN) != 0) {
  352. and SCSIID, TWIN_TID|OID, SCB_SCSIID;
  353. } else {
  354. mov SCSIID, SCB_SCSIID;
  355. }
  356. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  357. mov SINDEX, SCSISEQ_TEMPLATE;
  358. test SCB_CONTROL, TARGET_SCB jz . + 2;
  359. or SINDEX, TEMODE;
  360. mov SCSISEQ, SINDEX ret;
  361. } else {
  362. mov SCSISEQ, SCSISEQ_TEMPLATE ret;
  363. }
  364. /*
  365. * Initialize transfer settings with SCB provided settings.
  366. */
  367. set_transfer_settings:
  368. if ((ahc->features & AHC_ULTRA) != 0) {
  369. test SCB_CONTROL, ULTRAENB jz . + 2;
  370. or SXFRCTL0, FAST20;
  371. }
  372. /*
  373. * Initialize SCSIRATE with the appropriate value for this target.
  374. */
  375. if ((ahc->features & AHC_ULTRA2) != 0) {
  376. bmov SCSIRATE, SCB_SCSIRATE, 2 ret;
  377. } else {
  378. mov SCSIRATE, SCB_SCSIRATE ret;
  379. }
  380. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  381. /*
  382. * We carefully toggle SPIOEN to allow us to return the
  383. * message byte we receive so it can be checked prior to
  384. * driving REQ on the bus for the next byte.
  385. */
  386. target_inb:
  387. /*
  388. * Drive REQ on the bus by enabling SCSI PIO.
  389. */
  390. or SXFRCTL0, SPIOEN;
  391. /* Wait for the byte */
  392. test SSTAT0, SPIORDY jz .;
  393. /* Prevent our read from triggering another REQ */
  394. and SXFRCTL0, ~SPIOEN;
  395. /* Save latched contents */
  396. mov DINDEX, SCSIDATL ret;
  397. }
  398. /*
  399. * After the selection, remove this SCB from the "waiting SCB"
  400. * list. This is achieved by simply moving our "next" pointer into
  401. * WAITING_SCBH. Our next pointer will be set to null the next time this
  402. * SCB is used, so don't bother with it now.
  403. */
  404. select_out:
  405. /* Turn off the selection hardware */
  406. and SCSISEQ, TEMODE|ENSELI|ENRSELI|ENAUTOATNP, SCSISEQ;
  407. mov SCBPTR, WAITING_SCBH;
  408. mov WAITING_SCBH,SCB_NEXT;
  409. mov SAVED_SCSIID, SCB_SCSIID;
  410. and SAVED_LUN, LID, SCB_LUN;
  411. call set_transfer_settings;
  412. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  413. test SSTAT0, TARGET jz initiator_select;
  414. or SXFRCTL0, CLRSTCNT|CLRCHN;
  415. /*
  416. * Put tag in connonical location since not
  417. * all connections have an SCB.
  418. */
  419. mov INITIATOR_TAG, SCB_TARGET_ITAG;
  420. /*
  421. * We've just re-selected an initiator.
  422. * Assert BSY and setup the phase for
  423. * sending our identify messages.
  424. */
  425. mvi P_MESGIN|BSYO call change_phase;
  426. mvi CLRSINT0, CLRSELDO;
  427. /*
  428. * Start out with a simple identify message.
  429. */
  430. or SAVED_LUN, MSG_IDENTIFYFLAG call target_outb;
  431. /*
  432. * If we are the result of a tagged command, send
  433. * a simple Q tag and the tag id.
  434. */
  435. test SCB_CONTROL, TAG_ENB jz . + 3;
  436. mvi MSG_SIMPLE_Q_TAG call target_outb;
  437. mov SCB_TARGET_ITAG call target_outb;
  438. target_synccmd:
  439. /*
  440. * Now determine what phases the host wants us
  441. * to go through.
  442. */
  443. mov SEQ_FLAGS, SCB_TARGET_PHASES;
  444. test SCB_CONTROL, MK_MESSAGE jz target_ITloop;
  445. mvi P_MESGIN|BSYO call change_phase;
  446. jmp host_target_message_loop;
  447. target_ITloop:
  448. /*
  449. * Start honoring ATN signals now that
  450. * we properly identified ourselves.
  451. */
  452. test SCSISIGI, ATNI jnz target_mesgout;
  453. test SEQ_FLAGS, CMDPHASE_PENDING jnz target_cmdphase;
  454. test SEQ_FLAGS, DPHASE_PENDING jnz target_dphase;
  455. test SEQ_FLAGS, SPHASE_PENDING jnz target_sphase;
  456. /*
  457. * No more work to do. Either disconnect or not depending
  458. * on the state of NO_DISCONNECT.
  459. */
  460. test SEQ_FLAGS, NO_DISCONNECT jz target_disconnect;
  461. mvi TARG_IMMEDIATE_SCB, SCB_LIST_NULL;
  462. call complete_target_cmd;
  463. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  464. mov ALLZEROS call get_free_or_disc_scb;
  465. }
  466. cmp TARG_IMMEDIATE_SCB, SCB_LIST_NULL je .;
  467. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  468. mov TARG_IMMEDIATE_SCB call dma_scb;
  469. call set_transfer_settings;
  470. or SXFRCTL0, CLRSTCNT|CLRCHN;
  471. jmp target_synccmd;
  472. target_mesgout:
  473. mvi SCSISIGO, P_MESGOUT|BSYO;
  474. target_mesgout_continue:
  475. call target_inb;
  476. target_mesgout_pending:
  477. and SEQ_FLAGS2, ~TARGET_MSG_PENDING;
  478. /* Local Processing goes here... */
  479. jmp host_target_message_loop;
  480. target_disconnect:
  481. mvi P_MESGIN|BSYO call change_phase;
  482. test SEQ_FLAGS, DPHASE jz . + 2;
  483. mvi MSG_SAVEDATAPOINTER call target_outb;
  484. mvi MSG_DISCONNECT call target_outb;
  485. target_busfree_wait:
  486. /* Wait for preceding I/O session to complete. */
  487. test SCSISIGI, ACKI jnz .;
  488. target_busfree:
  489. and SIMODE1, ~ENBUSFREE;
  490. if ((ahc->features & AHC_ULTRA2) != 0) {
  491. clr SCSIBUSL;
  492. }
  493. clr SCSISIGO;
  494. mvi LASTPHASE, P_BUSFREE;
  495. call complete_target_cmd;
  496. jmp poll_for_work;
  497. target_cmdphase:
  498. /*
  499. * The target has dropped ATN (doesn't want to abort or BDR)
  500. * and we believe this selection to be valid. If the ring
  501. * buffer for new commands is full, return busy or queue full.
  502. */
  503. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  504. and A, HOST_TQINPOS, HS_MAILBOX;
  505. } else {
  506. mov A, KERNEL_TQINPOS;
  507. }
  508. cmp TQINPOS, A jne tqinfifo_has_space;
  509. mvi P_STATUS|BSYO call change_phase;
  510. test SEQ_FLAGS, TARGET_CMD_IS_TAGGED jz . + 3;
  511. mvi STATUS_QUEUE_FULL call target_outb;
  512. jmp target_busfree_wait;
  513. mvi STATUS_BUSY call target_outb;
  514. jmp target_busfree_wait;
  515. tqinfifo_has_space:
  516. mvi P_COMMAND|BSYO call change_phase;
  517. call target_inb;
  518. mov A, DINDEX;
  519. /* Store for host */
  520. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  521. mov CCSCBRAM, A;
  522. } else {
  523. mov DFDAT, A;
  524. }
  525. /*
  526. * Determine the number of bytes to read
  527. * based on the command group code via table lookup.
  528. * We reuse the first 8 bytes of the TARG_SCSIRATE
  529. * BIOS array for this table. Count is one less than
  530. * the total for the command since we've already fetched
  531. * the first byte.
  532. */
  533. shr A, CMD_GROUP_CODE_SHIFT;
  534. add SINDEX, CMDSIZE_TABLE, A;
  535. mov A, SINDIR;
  536. test A, 0xFF jz command_phase_done;
  537. or SXFRCTL0, SPIOEN;
  538. command_loop:
  539. test SSTAT0, SPIORDY jz .;
  540. cmp A, 1 jne . + 2;
  541. and SXFRCTL0, ~SPIOEN; /* Last Byte */
  542. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  543. mov CCSCBRAM, SCSIDATL;
  544. } else {
  545. mov DFDAT, SCSIDATL;
  546. }
  547. dec A;
  548. test A, 0xFF jnz command_loop;
  549. command_phase_done:
  550. and SEQ_FLAGS, ~CMDPHASE_PENDING;
  551. jmp target_ITloop;
  552. target_dphase:
  553. /*
  554. * Data phases on the bus are from the
  555. * perspective of the initiator. The dma
  556. * code looks at LASTPHASE to determine the
  557. * data direction of the DMA. Toggle it for
  558. * target transfers.
  559. */
  560. xor LASTPHASE, IOI, SCB_TARGET_DATA_DIR;
  561. or SCB_TARGET_DATA_DIR, BSYO call change_phase;
  562. jmp p_data;
  563. target_sphase:
  564. mvi P_STATUS|BSYO call change_phase;
  565. mvi LASTPHASE, P_STATUS;
  566. mov SCB_SCSI_STATUS call target_outb;
  567. /* XXX Watch for ATN or parity errors??? */
  568. mvi SCSISIGO, P_MESGIN|BSYO;
  569. /* MSG_CMDCMPLT is 0, but we can't do an immediate of 0 */
  570. mov ALLZEROS call target_outb;
  571. jmp target_busfree_wait;
  572. complete_target_cmd:
  573. test SEQ_FLAGS, TARG_CMD_PENDING jnz . + 2;
  574. mov SCB_TAG jmp complete_post;
  575. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  576. /* Set the valid byte */
  577. mvi CCSCBADDR, 24;
  578. mov CCSCBRAM, ALLONES;
  579. mvi CCHCNT, 28;
  580. or CCSCBCTL, CCSCBEN|CCSCBRESET;
  581. test CCSCBCTL, CCSCBDONE jz .;
  582. clr CCSCBCTL;
  583. } else {
  584. /* Set the valid byte */
  585. or DFCNTRL, FIFORESET;
  586. mvi DFWADDR, 3; /* Third 64bit word or byte 24 */
  587. mov DFDAT, ALLONES;
  588. mvi 28 call set_hcnt;
  589. or DFCNTRL, HDMAEN|FIFOFLUSH;
  590. call dma_finish;
  591. }
  592. inc TQINPOS;
  593. mvi INTSTAT,CMDCMPLT ret;
  594. }
  595. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  596. initiator_select:
  597. or SXFRCTL0, SPIOEN|CLRSTCNT|CLRCHN;
  598. /*
  599. * As soon as we get a successful selection, the target
  600. * should go into the message out phase since we have ATN
  601. * asserted.
  602. */
  603. mvi MSG_OUT, MSG_IDENTIFYFLAG;
  604. mvi SEQ_FLAGS, NO_CDB_SENT;
  605. mvi CLRSINT0, CLRSELDO;
  606. /*
  607. * Main loop for information transfer phases. Wait for the
  608. * target to assert REQ before checking MSG, C/D and I/O for
  609. * the bus phase.
  610. */
  611. mesgin_phasemis:
  612. ITloop:
  613. call phase_lock;
  614. mov A, LASTPHASE;
  615. test A, ~P_DATAIN jz p_data;
  616. cmp A,P_COMMAND je p_command;
  617. cmp A,P_MESGOUT je p_mesgout;
  618. cmp A,P_STATUS je p_status;
  619. cmp A,P_MESGIN je p_mesgin;
  620. mvi BAD_PHASE call set_seqint;
  621. jmp ITloop; /* Try reading the bus again. */
  622. await_busfree:
  623. and SIMODE1, ~ENBUSFREE;
  624. mov NONE, SCSIDATL; /* Ack the last byte */
  625. if ((ahc->features & AHC_ULTRA2) != 0) {
  626. clr SCSIBUSL; /* Prevent bit leakage durint SELTO */
  627. }
  628. and SXFRCTL0, ~SPIOEN;
  629. test SSTAT1,REQINIT|BUSFREE jz .;
  630. test SSTAT1, BUSFREE jnz poll_for_work;
  631. mvi MISSED_BUSFREE call set_seqint;
  632. }
  633. clear_target_state:
  634. /*
  635. * We assume that the kernel driver may reset us
  636. * at any time, even in the middle of a DMA, so
  637. * clear DFCNTRL too.
  638. */
  639. clr DFCNTRL;
  640. or SXFRCTL0, CLRSTCNT|CLRCHN;
  641. /*
  642. * We don't know the target we will connect to,
  643. * so default to narrow transfers to avoid
  644. * parity problems.
  645. */
  646. if ((ahc->features & AHC_ULTRA2) != 0) {
  647. bmov SCSIRATE, ALLZEROS, 2;
  648. } else {
  649. clr SCSIRATE;
  650. if ((ahc->features & AHC_ULTRA) != 0) {
  651. and SXFRCTL0, ~(FAST20);
  652. }
  653. }
  654. mvi LASTPHASE, P_BUSFREE;
  655. /* clear target specific flags */
  656. mvi SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT ret;
  657. sg_advance:
  658. clr A; /* add sizeof(struct scatter) */
  659. add SCB_RESIDUAL_SGPTR[0],SG_SIZEOF;
  660. adc SCB_RESIDUAL_SGPTR[1],A;
  661. adc SCB_RESIDUAL_SGPTR[2],A;
  662. adc SCB_RESIDUAL_SGPTR[3],A ret;
  663. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  664. disable_ccsgen:
  665. test CCSGCTL, CCSGEN jz return;
  666. test CCSGCTL, CCSGDONE jz .;
  667. disable_ccsgen_fetch_done:
  668. clr CCSGCTL;
  669. test CCSGCTL, CCSGEN jnz .;
  670. ret;
  671. idle_loop:
  672. /*
  673. * Do we need any more segments for this transfer?
  674. */
  675. test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jnz return;
  676. /* Did we just finish fetching segs? */
  677. cmp CCSGCTL, CCSGEN|CCSGDONE je idle_sgfetch_complete;
  678. /* Are we actively fetching segments? */
  679. test CCSGCTL, CCSGEN jnz return;
  680. /*
  681. * Do we have any prefetch left???
  682. */
  683. cmp CCSGADDR, SG_PREFETCH_CNT jne idle_sg_avail;
  684. /*
  685. * Need to fetch segments, but we can only do that
  686. * if the command channel is completely idle. Make
  687. * sure we don't have an SCB prefetch going on.
  688. */
  689. test CCSCBCTL, CCSCBEN jnz return;
  690. /*
  691. * We fetch a "cacheline aligned" and sized amount of data
  692. * so we don't end up referencing a non-existant page.
  693. * Cacheline aligned is in quotes because the kernel will
  694. * set the prefetch amount to a reasonable level if the
  695. * cacheline size is unknown.
  696. */
  697. mvi CCHCNT, SG_PREFETCH_CNT;
  698. and CCHADDR[0], SG_PREFETCH_ALIGN_MASK, SCB_RESIDUAL_SGPTR;
  699. bmov CCHADDR[1], SCB_RESIDUAL_SGPTR[1], 3;
  700. mvi CCSGCTL, CCSGEN|CCSGRESET ret;
  701. idle_sgfetch_complete:
  702. call disable_ccsgen_fetch_done;
  703. and CCSGADDR, SG_PREFETCH_ADDR_MASK, SCB_RESIDUAL_SGPTR;
  704. idle_sg_avail:
  705. if ((ahc->features & AHC_ULTRA2) != 0) {
  706. /* Does the hardware have space for another SG entry? */
  707. test DFSTATUS, PRELOAD_AVAIL jz return;
  708. bmov HADDR, CCSGRAM, 7;
  709. bmov SCB_RESIDUAL_DATACNT[3], CCSGRAM, 1;
  710. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  711. mov SCB_RESIDUAL_DATACNT[3] call set_hhaddr;
  712. }
  713. call sg_advance;
  714. mov SINDEX, SCB_RESIDUAL_SGPTR[0];
  715. test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
  716. or SINDEX, LAST_SEG;
  717. mov SG_CACHE_PRE, SINDEX;
  718. /* Load the segment */
  719. or DFCNTRL, PRELOADEN;
  720. }
  721. ret;
  722. }
  723. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0 && ahc->pci_cachesize != 0) {
  724. /*
  725. * Calculate the trailing portion of this S/G segment that cannot
  726. * be transferred using memory write and invalidate PCI transactions.
  727. * XXX Can we optimize this for PCI writes only???
  728. */
  729. calc_mwi_residual:
  730. /*
  731. * If the ending address is on a cacheline boundary,
  732. * there is no need for an extra segment.
  733. */
  734. mov A, HCNT[0];
  735. add A, A, HADDR[0];
  736. and A, CACHESIZE_MASK;
  737. test A, 0xFF jz return;
  738. /*
  739. * If the transfer is less than a cachline,
  740. * there is no need for an extra segment.
  741. */
  742. test HCNT[1], 0xFF jnz calc_mwi_residual_final;
  743. test HCNT[2], 0xFF jnz calc_mwi_residual_final;
  744. add NONE, INVERTED_CACHESIZE_MASK, HCNT[0];
  745. jnc return;
  746. calc_mwi_residual_final:
  747. mov MWI_RESIDUAL, A;
  748. not A;
  749. inc A;
  750. add HCNT[0], A;
  751. adc HCNT[1], -1;
  752. adc HCNT[2], -1 ret;
  753. }
  754. p_data:
  755. test SEQ_FLAGS,NOT_IDENTIFIED|NO_CDB_SENT jz p_data_allowed;
  756. mvi PROTO_VIOLATION call set_seqint;
  757. p_data_allowed:
  758. if ((ahc->features & AHC_ULTRA2) != 0) {
  759. mvi DMAPARAMS, PRELOADEN|SCSIEN|HDMAEN;
  760. } else {
  761. mvi DMAPARAMS, WIDEODD|SCSIEN|SDMAEN|HDMAEN|FIFORESET;
  762. }
  763. test LASTPHASE, IOI jnz . + 2;
  764. or DMAPARAMS, DIRECTION;
  765. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  766. /* We don't have any valid S/G elements */
  767. mvi CCSGADDR, SG_PREFETCH_CNT;
  768. }
  769. test SEQ_FLAGS, DPHASE jz data_phase_initialize;
  770. /*
  771. * If we re-enter the data phase after going through another
  772. * phase, our transfer location has almost certainly been
  773. * corrupted by the interveining, non-data, transfers. Ask
  774. * the host driver to fix us up based on the transfer residual.
  775. */
  776. mvi PDATA_REINIT call set_seqint;
  777. jmp data_phase_loop;
  778. data_phase_initialize:
  779. /* We have seen a data phase for the first time */
  780. or SEQ_FLAGS, DPHASE;
  781. /*
  782. * Initialize the DMA address and counter from the SCB.
  783. * Also set SCB_RESIDUAL_SGPTR, including the LAST_SEG
  784. * flag in the highest byte of the data count. We cannot
  785. * modify the saved values in the SCB until we see a save
  786. * data pointers message.
  787. */
  788. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  789. /* The lowest address byte must be loaded last. */
  790. mov SCB_DATACNT[3] call set_hhaddr;
  791. }
  792. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  793. bmov HADDR, SCB_DATAPTR, 7;
  794. bmov SCB_RESIDUAL_DATACNT[3], SCB_DATACNT[3], 5;
  795. } else {
  796. mvi DINDEX, HADDR;
  797. mvi SCB_DATAPTR call bcopy_7;
  798. mvi DINDEX, SCB_RESIDUAL_DATACNT + 3;
  799. mvi SCB_DATACNT + 3 call bcopy_5;
  800. }
  801. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0 && ahc->pci_cachesize != 0) {
  802. call calc_mwi_residual;
  803. }
  804. and SCB_RESIDUAL_SGPTR[0], ~SG_FULL_RESID;
  805. if ((ahc->features & AHC_ULTRA2) == 0) {
  806. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  807. bmov STCNT, HCNT, 3;
  808. } else {
  809. call set_stcnt_from_hcnt;
  810. }
  811. }
  812. data_phase_loop:
  813. /* Guard against overruns */
  814. test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz data_phase_inbounds;
  815. /*
  816. * Turn on `Bit Bucket' mode, wait until the target takes
  817. * us to another phase, and then notify the host.
  818. */
  819. and DMAPARAMS, DIRECTION;
  820. mov DFCNTRL, DMAPARAMS;
  821. or SXFRCTL1,BITBUCKET;
  822. if ((ahc->features & AHC_DT) == 0) {
  823. test SSTAT1,PHASEMIS jz .;
  824. } else {
  825. test SCSIPHASE, DATA_PHASE_MASK jnz .;
  826. }
  827. and SXFRCTL1, ~BITBUCKET;
  828. mvi DATA_OVERRUN call set_seqint;
  829. jmp ITloop;
  830. data_phase_inbounds:
  831. if ((ahc->features & AHC_ULTRA2) != 0) {
  832. mov SINDEX, SCB_RESIDUAL_SGPTR[0];
  833. test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz . + 2;
  834. or SINDEX, LAST_SEG;
  835. mov SG_CACHE_PRE, SINDEX;
  836. mov DFCNTRL, DMAPARAMS;
  837. ultra2_dma_loop:
  838. call idle_loop;
  839. /*
  840. * The transfer is complete if either the last segment
  841. * completes or the target changes phase.
  842. */
  843. test SG_CACHE_SHADOW, LAST_SEG_DONE jnz ultra2_dmafinish;
  844. if ((ahc->features & AHC_DT) == 0) {
  845. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  846. /*
  847. * As a target, we control the phases,
  848. * so ignore PHASEMIS.
  849. */
  850. test SSTAT0, TARGET jnz ultra2_dma_loop;
  851. }
  852. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  853. test SSTAT1,PHASEMIS jz ultra2_dma_loop;
  854. }
  855. } else {
  856. test DFCNTRL, SCSIEN jnz ultra2_dma_loop;
  857. }
  858. ultra2_dmafinish:
  859. /*
  860. * The transfer has terminated either due to a phase
  861. * change, and/or the completion of the last segment.
  862. * We have two goals here. Do as much other work
  863. * as possible while the data fifo drains on a read
  864. * and respond as quickly as possible to the standard
  865. * messages (save data pointers/disconnect and command
  866. * complete) that usually follow a data phase.
  867. */
  868. if ((ahc->bugs & AHC_AUTOFLUSH_BUG) != 0) {
  869. /*
  870. * On chips with broken auto-flush, start
  871. * the flushing process now. We'll poke
  872. * the chip from time to time to keep the
  873. * flush process going as we complete the
  874. * data phase.
  875. */
  876. or DFCNTRL, FIFOFLUSH;
  877. }
  878. /*
  879. * We assume that, even though data may still be
  880. * transferring to the host, that the SCSI side of
  881. * the DMA engine is now in a static state. This
  882. * allows us to update our notion of where we are
  883. * in this transfer.
  884. *
  885. * If, by chance, we stopped before being able
  886. * to fetch additional segments for this transfer,
  887. * yet the last S/G was completely exhausted,
  888. * call our idle loop until it is able to load
  889. * another segment. This will allow us to immediately
  890. * pickup on the next segment on the next data phase.
  891. *
  892. * If we happened to stop on the last segment, then
  893. * our residual information is still correct from
  894. * the idle loop and there is no need to perform
  895. * any fixups.
  896. */
  897. ultra2_ensure_sg:
  898. test SG_CACHE_SHADOW, LAST_SEG jz ultra2_shvalid;
  899. /* Record if we've consumed all S/G entries */
  900. test SSTAT2, SHVALID jnz residuals_correct;
  901. or SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
  902. jmp residuals_correct;
  903. ultra2_shvalid:
  904. test SSTAT2, SHVALID jnz sgptr_fixup;
  905. call idle_loop;
  906. jmp ultra2_ensure_sg;
  907. sgptr_fixup:
  908. /*
  909. * Fixup the residual next S/G pointer. The S/G preload
  910. * feature of the chip allows us to load two elements
  911. * in addition to the currently active element. We
  912. * store the bottom byte of the next S/G pointer in
  913. * the SG_CACEPTR register so we can restore the
  914. * correct value when the DMA completes. If the next
  915. * sg ptr value has advanced to the point where higher
  916. * bytes in the address have been affected, fix them
  917. * too.
  918. */
  919. test SG_CACHE_SHADOW, 0x80 jz sgptr_fixup_done;
  920. test SCB_RESIDUAL_SGPTR[0], 0x80 jnz sgptr_fixup_done;
  921. add SCB_RESIDUAL_SGPTR[1], -1;
  922. adc SCB_RESIDUAL_SGPTR[2], -1;
  923. adc SCB_RESIDUAL_SGPTR[3], -1;
  924. sgptr_fixup_done:
  925. and SCB_RESIDUAL_SGPTR[0], SG_ADDR_MASK, SG_CACHE_SHADOW;
  926. /* We are not the last seg */
  927. and SCB_RESIDUAL_DATACNT[3], ~SG_LAST_SEG;
  928. residuals_correct:
  929. /*
  930. * Go ahead and shut down the DMA engine now.
  931. * In the future, we'll want to handle end of
  932. * transfer messages prior to doing this, but this
  933. * requires similar restructuring for pre-ULTRA2
  934. * controllers.
  935. */
  936. test DMAPARAMS, DIRECTION jnz ultra2_fifoempty;
  937. ultra2_fifoflush:
  938. if ((ahc->features & AHC_DT) == 0) {
  939. if ((ahc->bugs & AHC_AUTOFLUSH_BUG) != 0) {
  940. /*
  941. * On Rev A of the aic7890, the autoflush
  942. * feature doesn't function correctly.
  943. * Perform an explicit manual flush. During
  944. * a manual flush, the FIFOEMP bit becomes
  945. * true every time the PCI FIFO empties
  946. * regardless of the state of the SCSI FIFO.
  947. * It can take up to 4 clock cycles for the
  948. * SCSI FIFO to get data into the PCI FIFO
  949. * and for FIFOEMP to de-assert. Here we
  950. * guard against this condition by making
  951. * sure the FIFOEMP bit stays on for 5 full
  952. * clock cycles.
  953. */
  954. or DFCNTRL, FIFOFLUSH;
  955. test DFSTATUS, FIFOEMP jz ultra2_fifoflush;
  956. test DFSTATUS, FIFOEMP jz ultra2_fifoflush;
  957. test DFSTATUS, FIFOEMP jz ultra2_fifoflush;
  958. test DFSTATUS, FIFOEMP jz ultra2_fifoflush;
  959. }
  960. test DFSTATUS, FIFOEMP jz ultra2_fifoflush;
  961. } else {
  962. /*
  963. * We enable the auto-ack feature on DT capable
  964. * controllers. This means that the controller may
  965. * have already transferred some overrun bytes into
  966. * the data FIFO and acked them on the bus. The only
  967. * way to detect this situation is to wait for
  968. * LAST_SEG_DONE to come true on a completed transfer
  969. * and then test to see if the data FIFO is non-empty.
  970. */
  971. test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL
  972. jz ultra2_wait_fifoemp;
  973. test SG_CACHE_SHADOW, LAST_SEG_DONE jz .;
  974. /*
  975. * FIFOEMP can lag LAST_SEG_DONE. Wait a few
  976. * clocks before calling this an overrun.
  977. */
  978. test DFSTATUS, FIFOEMP jnz ultra2_fifoempty;
  979. test DFSTATUS, FIFOEMP jnz ultra2_fifoempty;
  980. test DFSTATUS, FIFOEMP jnz ultra2_fifoempty;
  981. /* Overrun */
  982. jmp data_phase_loop;
  983. ultra2_wait_fifoemp:
  984. test DFSTATUS, FIFOEMP jz .;
  985. }
  986. ultra2_fifoempty:
  987. /* Don't clobber an inprogress host data transfer */
  988. test DFSTATUS, MREQPEND jnz ultra2_fifoempty;
  989. ultra2_dmahalt:
  990. and DFCNTRL, ~(SCSIEN|HDMAEN);
  991. test DFCNTRL, SCSIEN|HDMAEN jnz .;
  992. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  993. /*
  994. * Keep HHADDR cleared for future, 32bit addressed
  995. * only, DMA operations.
  996. *
  997. * Due to bayonette style S/G handling, our residual
  998. * data must be "fixed up" once the transfer is halted.
  999. * Here we fixup the HSHADDR stored in the high byte
  1000. * of the residual data cnt. By postponing the fixup,
  1001. * we can batch the clearing of HADDR with the fixup.
  1002. * If we halted on the last segment, the residual is
  1003. * already correct. If we are not on the last
  1004. * segment, copy the high address directly from HSHADDR.
  1005. * We don't need to worry about maintaining the
  1006. * SG_LAST_SEG flag as it will always be false in the
  1007. * case where an update is required.
  1008. */
  1009. or DSCOMMAND1, HADDLDSEL0;
  1010. test SG_CACHE_SHADOW, LAST_SEG jnz . + 2;
  1011. mov SCB_RESIDUAL_DATACNT[3], SHADDR;
  1012. clr HADDR;
  1013. and DSCOMMAND1, ~HADDLDSEL0;
  1014. }
  1015. } else {
  1016. /* If we are the last SG block, tell the hardware. */
  1017. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
  1018. && ahc->pci_cachesize != 0) {
  1019. test MWI_RESIDUAL, 0xFF jnz dma_mid_sg;
  1020. }
  1021. test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz dma_mid_sg;
  1022. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  1023. test SSTAT0, TARGET jz dma_last_sg;
  1024. if ((ahc->flags & AHC_TMODE_WIDEODD_BUG) != 0) {
  1025. test DMAPARAMS, DIRECTION jz dma_mid_sg;
  1026. }
  1027. }
  1028. dma_last_sg:
  1029. and DMAPARAMS, ~WIDEODD;
  1030. dma_mid_sg:
  1031. /* Start DMA data transfer. */
  1032. mov DFCNTRL, DMAPARAMS;
  1033. dma_loop:
  1034. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1035. call idle_loop;
  1036. }
  1037. test SSTAT0,DMADONE jnz dma_dmadone;
  1038. test SSTAT1,PHASEMIS jz dma_loop; /* ie. underrun */
  1039. dma_phasemis:
  1040. /*
  1041. * We will be "done" DMAing when the transfer count goes to
  1042. * zero, or the target changes the phase (in light of this,
  1043. * it makes sense that the DMA circuitry doesn't ACK when
  1044. * PHASEMIS is active). If we are doing a SCSI->Host transfer,
  1045. * the data FIFO should be flushed auto-magically on STCNT=0
  1046. * or a phase change, so just wait for FIFO empty status.
  1047. */
  1048. dma_checkfifo:
  1049. test DFCNTRL,DIRECTION jnz dma_fifoempty;
  1050. dma_fifoflush:
  1051. test DFSTATUS,FIFOEMP jz dma_fifoflush;
  1052. dma_fifoempty:
  1053. /* Don't clobber an inprogress host data transfer */
  1054. test DFSTATUS, MREQPEND jnz dma_fifoempty;
  1055. /*
  1056. * Now shut off the DMA and make sure that the DMA
  1057. * hardware has actually stopped. Touching the DMA
  1058. * counters, etc. while a DMA is active will result
  1059. * in an ILLSADDR exception.
  1060. */
  1061. dma_dmadone:
  1062. and DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
  1063. dma_halt:
  1064. /*
  1065. * Some revisions of the aic78XX have a problem where, if the
  1066. * data fifo is full, but the PCI input latch is not empty,
  1067. * HDMAEN cannot be cleared. The fix used here is to drain
  1068. * the prefetched but unused data from the data fifo until
  1069. * there is space for the input latch to drain.
  1070. */
  1071. if ((ahc->bugs & AHC_PCI_2_1_RETRY_BUG) != 0) {
  1072. mov NONE, DFDAT;
  1073. }
  1074. test DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz dma_halt;
  1075. /* See if we have completed this last segment */
  1076. test STCNT[0], 0xff jnz data_phase_finish;
  1077. test STCNT[1], 0xff jnz data_phase_finish;
  1078. test STCNT[2], 0xff jnz data_phase_finish;
  1079. /*
  1080. * Advance the scatter-gather pointers if needed
  1081. */
  1082. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
  1083. && ahc->pci_cachesize != 0) {
  1084. test MWI_RESIDUAL, 0xFF jz no_mwi_resid;
  1085. /*
  1086. * Reload HADDR from SHADDR and setup the
  1087. * count to be the size of our residual.
  1088. */
  1089. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1090. bmov HADDR, SHADDR, 4;
  1091. mov HCNT, MWI_RESIDUAL;
  1092. bmov HCNT[1], ALLZEROS, 2;
  1093. } else {
  1094. mvi DINDEX, HADDR;
  1095. mvi SHADDR call bcopy_4;
  1096. mov MWI_RESIDUAL call set_hcnt;
  1097. }
  1098. clr MWI_RESIDUAL;
  1099. jmp sg_load_done;
  1100. no_mwi_resid:
  1101. }
  1102. test SCB_RESIDUAL_DATACNT[3], SG_LAST_SEG jz sg_load;
  1103. or SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL;
  1104. jmp data_phase_finish;
  1105. sg_load:
  1106. /*
  1107. * Load the next SG element's data address and length
  1108. * into the DMA engine. If we don't have hardware
  1109. * to perform a prefetch, we'll have to fetch the
  1110. * segment from host memory first.
  1111. */
  1112. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1113. /* Wait for the idle loop to complete */
  1114. test CCSGCTL, CCSGEN jz . + 3;
  1115. call idle_loop;
  1116. test CCSGCTL, CCSGEN jnz . - 1;
  1117. bmov HADDR, CCSGRAM, 7;
  1118. /*
  1119. * Workaround for flaky external SCB RAM
  1120. * on certain aic7895 setups. It seems
  1121. * unable to handle direct transfers from
  1122. * S/G ram to certain SCB locations.
  1123. */
  1124. mov SINDEX, CCSGRAM;
  1125. mov SCB_RESIDUAL_DATACNT[3], SINDEX;
  1126. } else {
  1127. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  1128. mov ALLZEROS call set_hhaddr;
  1129. }
  1130. mvi DINDEX, HADDR;
  1131. mvi SCB_RESIDUAL_SGPTR call bcopy_4;
  1132. mvi SG_SIZEOF call set_hcnt;
  1133. or DFCNTRL, HDMAEN|DIRECTION|FIFORESET;
  1134. call dma_finish;
  1135. mvi DINDEX, HADDR;
  1136. call dfdat_in_7;
  1137. mov SCB_RESIDUAL_DATACNT[3], DFDAT;
  1138. }
  1139. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  1140. mov SCB_RESIDUAL_DATACNT[3] call set_hhaddr;
  1141. /*
  1142. * The lowest address byte must be loaded
  1143. * last as it triggers the computation of
  1144. * some items in the PCI block. The ULTRA2
  1145. * chips do this on PRELOAD.
  1146. */
  1147. mov HADDR, HADDR;
  1148. }
  1149. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
  1150. && ahc->pci_cachesize != 0) {
  1151. call calc_mwi_residual;
  1152. }
  1153. /* Point to the new next sg in memory */
  1154. call sg_advance;
  1155. sg_load_done:
  1156. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1157. bmov STCNT, HCNT, 3;
  1158. } else {
  1159. call set_stcnt_from_hcnt;
  1160. }
  1161. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  1162. test SSTAT0, TARGET jnz data_phase_loop;
  1163. }
  1164. }
  1165. data_phase_finish:
  1166. /*
  1167. * If the target has left us in data phase, loop through
  1168. * the dma code again. In the case of ULTRA2 adapters,
  1169. * we should only loop if there is a data overrun. For
  1170. * all other adapters, we'll loop after each S/G element
  1171. * is loaded as well as if there is an overrun.
  1172. */
  1173. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  1174. test SSTAT0, TARGET jnz data_phase_done;
  1175. }
  1176. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  1177. test SSTAT1, REQINIT jz .;
  1178. if ((ahc->features & AHC_DT) == 0) {
  1179. test SSTAT1,PHASEMIS jz data_phase_loop;
  1180. } else {
  1181. test SCSIPHASE, DATA_PHASE_MASK jnz data_phase_loop;
  1182. }
  1183. }
  1184. data_phase_done:
  1185. /*
  1186. * After a DMA finishes, save the SG and STCNT residuals back into
  1187. * the SCB. We use STCNT instead of HCNT, since it's a reflection
  1188. * of how many bytes were transferred on the SCSI (as opposed to the
  1189. * host) bus.
  1190. */
  1191. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1192. /* Kill off any pending prefetch */
  1193. call disable_ccsgen;
  1194. }
  1195. if ((ahc->features & AHC_ULTRA2) == 0) {
  1196. /*
  1197. * Clear the high address byte so that all other DMA
  1198. * operations, which use 32bit addressing, can assume
  1199. * HHADDR is 0.
  1200. */
  1201. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  1202. mov ALLZEROS call set_hhaddr;
  1203. }
  1204. }
  1205. /*
  1206. * Update our residual information before the information is
  1207. * lost by some other type of SCSI I/O (e.g. PIO). If we have
  1208. * transferred all data, no update is needed.
  1209. *
  1210. */
  1211. test SCB_RESIDUAL_SGPTR, SG_LIST_NULL jnz residual_update_done;
  1212. if ((ahc->bugs & AHC_PCI_MWI_BUG) != 0
  1213. && ahc->pci_cachesize != 0) {
  1214. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1215. test MWI_RESIDUAL, 0xFF jz bmov_resid;
  1216. }
  1217. mov A, MWI_RESIDUAL;
  1218. add SCB_RESIDUAL_DATACNT[0], A, STCNT[0];
  1219. clr A;
  1220. adc SCB_RESIDUAL_DATACNT[1], A, STCNT[1];
  1221. adc SCB_RESIDUAL_DATACNT[2], A, STCNT[2];
  1222. clr MWI_RESIDUAL;
  1223. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1224. jmp . + 2;
  1225. bmov_resid:
  1226. bmov SCB_RESIDUAL_DATACNT, STCNT, 3;
  1227. }
  1228. } else if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1229. bmov SCB_RESIDUAL_DATACNT, STCNT, 3;
  1230. } else {
  1231. mov SCB_RESIDUAL_DATACNT[0], STCNT[0];
  1232. mov SCB_RESIDUAL_DATACNT[1], STCNT[1];
  1233. mov SCB_RESIDUAL_DATACNT[2], STCNT[2];
  1234. }
  1235. residual_update_done:
  1236. /*
  1237. * Since we've been through a data phase, the SCB_RESID* fields
  1238. * are now initialized. Clear the full residual flag.
  1239. */
  1240. and SCB_SGPTR[0], ~SG_FULL_RESID;
  1241. if ((ahc->features & AHC_ULTRA2) != 0) {
  1242. /* Clear the channel in case we return to data phase later */
  1243. or SXFRCTL0, CLRSTCNT|CLRCHN;
  1244. or SXFRCTL0, CLRSTCNT|CLRCHN;
  1245. }
  1246. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  1247. test SEQ_FLAGS, DPHASE_PENDING jz ITloop;
  1248. and SEQ_FLAGS, ~DPHASE_PENDING;
  1249. /*
  1250. * For data-in phases, wait for any pending acks from the
  1251. * initiator before changing phase. We only need to
  1252. * send Ignore Wide Residue messages for data-in phases.
  1253. */
  1254. test DFCNTRL, DIRECTION jz target_ITloop;
  1255. test SSTAT1, REQINIT jnz .;
  1256. test SCB_LUN, SCB_XFERLEN_ODD jz target_ITloop;
  1257. test SCSIRATE, WIDEXFER jz target_ITloop;
  1258. /*
  1259. * Issue an Ignore Wide Residue Message.
  1260. */
  1261. mvi P_MESGIN|BSYO call change_phase;
  1262. mvi MSG_IGN_WIDE_RESIDUE call target_outb;
  1263. mvi 1 call target_outb;
  1264. jmp target_ITloop;
  1265. } else {
  1266. jmp ITloop;
  1267. }
  1268. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  1269. /*
  1270. * Command phase. Set up the DMA registers and let 'er rip.
  1271. */
  1272. p_command:
  1273. test SEQ_FLAGS, NOT_IDENTIFIED jz p_command_okay;
  1274. mvi PROTO_VIOLATION call set_seqint;
  1275. p_command_okay:
  1276. if ((ahc->features & AHC_ULTRA2) != 0) {
  1277. bmov HCNT[0], SCB_CDB_LEN, 1;
  1278. bmov HCNT[1], ALLZEROS, 2;
  1279. mvi SG_CACHE_PRE, LAST_SEG;
  1280. } else if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1281. bmov STCNT[0], SCB_CDB_LEN, 1;
  1282. bmov STCNT[1], ALLZEROS, 2;
  1283. } else {
  1284. mov STCNT[0], SCB_CDB_LEN;
  1285. clr STCNT[1];
  1286. clr STCNT[2];
  1287. }
  1288. add NONE, -13, SCB_CDB_LEN;
  1289. mvi SCB_CDB_STORE jnc p_command_embedded;
  1290. p_command_from_host:
  1291. if ((ahc->features & AHC_ULTRA2) != 0) {
  1292. bmov HADDR[0], SCB_CDB_PTR, 4;
  1293. mvi DFCNTRL, (PRELOADEN|SCSIEN|HDMAEN|DIRECTION);
  1294. } else {
  1295. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1296. bmov HADDR[0], SCB_CDB_PTR, 4;
  1297. bmov HCNT, STCNT, 3;
  1298. } else {
  1299. mvi DINDEX, HADDR;
  1300. mvi SCB_CDB_PTR call bcopy_4;
  1301. mov SCB_CDB_LEN call set_hcnt;
  1302. }
  1303. mvi DFCNTRL, (SCSIEN|SDMAEN|HDMAEN|DIRECTION|FIFORESET);
  1304. }
  1305. jmp p_command_xfer;
  1306. p_command_embedded:
  1307. /*
  1308. * The data fifo seems to require 4 byte aligned
  1309. * transfers from the sequencer. Force this to
  1310. * be the case by clearing HADDR[0] even though
  1311. * we aren't going to touch host memory.
  1312. */
  1313. clr HADDR[0];
  1314. if ((ahc->features & AHC_ULTRA2) != 0) {
  1315. mvi DFCNTRL, (PRELOADEN|SCSIEN|DIRECTION);
  1316. bmov DFDAT, SCB_CDB_STORE, 12;
  1317. } else if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1318. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1319. /*
  1320. * On the 7895 the data FIFO will
  1321. * get corrupted if you try to dump
  1322. * data from external SCB memory into
  1323. * the FIFO while it is enabled. So,
  1324. * fill the fifo and then enable SCSI
  1325. * transfers.
  1326. */
  1327. mvi DFCNTRL, (DIRECTION|FIFORESET);
  1328. } else {
  1329. mvi DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFORESET);
  1330. }
  1331. bmov DFDAT, SCB_CDB_STORE, 12;
  1332. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1333. mvi DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFOFLUSH);
  1334. } else {
  1335. or DFCNTRL, FIFOFLUSH;
  1336. }
  1337. } else {
  1338. mvi DFCNTRL, (SCSIEN|SDMAEN|DIRECTION|FIFORESET);
  1339. call copy_to_fifo_6;
  1340. call copy_to_fifo_6;
  1341. or DFCNTRL, FIFOFLUSH;
  1342. }
  1343. p_command_xfer:
  1344. and SEQ_FLAGS, ~NO_CDB_SENT;
  1345. if ((ahc->features & AHC_DT) == 0) {
  1346. test SSTAT0, SDONE jnz . + 2;
  1347. test SSTAT1, PHASEMIS jz . - 1;
  1348. /*
  1349. * Wait for our ACK to go-away on it's own
  1350. * instead of being killed by SCSIEN getting cleared.
  1351. */
  1352. test SCSISIGI, ACKI jnz .;
  1353. } else {
  1354. test DFCNTRL, SCSIEN jnz .;
  1355. }
  1356. test SSTAT0, SDONE jnz p_command_successful;
  1357. /*
  1358. * Don't allow a data phase if the command
  1359. * was not fully transferred.
  1360. */
  1361. or SEQ_FLAGS, NO_CDB_SENT;
  1362. p_command_successful:
  1363. and DFCNTRL, ~(SCSIEN|SDMAEN|HDMAEN);
  1364. test DFCNTRL, (SCSIEN|SDMAEN|HDMAEN) jnz .;
  1365. jmp ITloop;
  1366. /*
  1367. * Status phase. Wait for the data byte to appear, then read it
  1368. * and store it into the SCB.
  1369. */
  1370. p_status:
  1371. test SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
  1372. p_status_okay:
  1373. mov SCB_SCSI_STATUS, SCSIDATL;
  1374. or SCB_CONTROL, STATUS_RCVD;
  1375. jmp ITloop;
  1376. /*
  1377. * Message out phase. If MSG_OUT is MSG_IDENTIFYFLAG, build a full
  1378. * indentify message sequence and send it to the target. The host may
  1379. * override this behavior by setting the MK_MESSAGE bit in the SCB
  1380. * control byte. This will cause us to interrupt the host and allow
  1381. * it to handle the message phase completely on its own. If the bit
  1382. * associated with this target is set, we will also interrupt the host,
  1383. * thereby allowing it to send a message on the next selection regardless
  1384. * of the transaction being sent.
  1385. *
  1386. * If MSG_OUT is == HOST_MSG, also interrupt the host and take a message.
  1387. * This is done to allow the host to send messages outside of an identify
  1388. * sequence while protecting the seqencer from testing the MK_MESSAGE bit
  1389. * on an SCB that might not be for the current nexus. (For example, a
  1390. * BDR message in responce to a bad reselection would leave us pointed to
  1391. * an SCB that doesn't have anything to do with the current target).
  1392. *
  1393. * Otherwise, treat MSG_OUT as a 1 byte message to send (abort, abort tag,
  1394. * bus device reset).
  1395. *
  1396. * When there are no messages to send, MSG_OUT should be set to MSG_NOOP,
  1397. * in case the target decides to put us in this phase for some strange
  1398. * reason.
  1399. */
  1400. p_mesgout_retry:
  1401. /* Turn on ATN for the retry */
  1402. if ((ahc->features & AHC_DT) == 0) {
  1403. or SCSISIGO, ATNO, LASTPHASE;
  1404. } else {
  1405. mvi SCSISIGO, ATNO;
  1406. }
  1407. p_mesgout:
  1408. mov SINDEX, MSG_OUT;
  1409. cmp SINDEX, MSG_IDENTIFYFLAG jne p_mesgout_from_host;
  1410. test SCB_CONTROL,MK_MESSAGE jnz host_message_loop;
  1411. p_mesgout_identify:
  1412. or SINDEX, MSG_IDENTIFYFLAG|DISCENB, SAVED_LUN;
  1413. test SCB_CONTROL, DISCENB jnz . + 2;
  1414. and SINDEX, ~DISCENB;
  1415. /*
  1416. * Send a tag message if TAG_ENB is set in the SCB control block.
  1417. * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
  1418. */
  1419. p_mesgout_tag:
  1420. test SCB_CONTROL,TAG_ENB jz p_mesgout_onebyte;
  1421. mov SCSIDATL, SINDEX; /* Send the identify message */
  1422. call phase_lock;
  1423. cmp LASTPHASE, P_MESGOUT jne p_mesgout_done;
  1424. and SCSIDATL,TAG_ENB|SCB_TAG_TYPE,SCB_CONTROL;
  1425. call phase_lock;
  1426. cmp LASTPHASE, P_MESGOUT jne p_mesgout_done;
  1427. mov SCB_TAG jmp p_mesgout_onebyte;
  1428. /*
  1429. * Interrupt the driver, and allow it to handle this message
  1430. * phase and any required retries.
  1431. */
  1432. p_mesgout_from_host:
  1433. cmp SINDEX, HOST_MSG jne p_mesgout_onebyte;
  1434. jmp host_message_loop;
  1435. p_mesgout_onebyte:
  1436. mvi CLRSINT1, CLRATNO;
  1437. mov SCSIDATL, SINDEX;
  1438. /*
  1439. * If the next bus phase after ATN drops is message out, it means
  1440. * that the target is requesting that the last message(s) be resent.
  1441. */
  1442. call phase_lock;
  1443. cmp LASTPHASE, P_MESGOUT je p_mesgout_retry;
  1444. p_mesgout_done:
  1445. mvi CLRSINT1,CLRATNO; /* Be sure to turn ATNO off */
  1446. mov LAST_MSG, MSG_OUT;
  1447. mvi MSG_OUT, MSG_NOOP; /* No message left */
  1448. jmp ITloop;
  1449. /*
  1450. * Message in phase. Bytes are read using Automatic PIO mode.
  1451. */
  1452. p_mesgin:
  1453. mvi ACCUM call inb_first; /* read the 1st message byte */
  1454. test A,MSG_IDENTIFYFLAG jnz mesgin_identify;
  1455. cmp A,MSG_DISCONNECT je mesgin_disconnect;
  1456. cmp A,MSG_SAVEDATAPOINTER je mesgin_sdptrs;
  1457. cmp ALLZEROS,A je mesgin_complete;
  1458. cmp A,MSG_RESTOREPOINTERS je mesgin_rdptrs;
  1459. cmp A,MSG_IGN_WIDE_RESIDUE je mesgin_ign_wide_residue;
  1460. cmp A,MSG_NOOP je mesgin_done;
  1461. /*
  1462. * Pushed message loop to allow the kernel to
  1463. * run it's own message state engine. To avoid an
  1464. * extra nop instruction after signaling the kernel,
  1465. * we perform the phase_lock before checking to see
  1466. * if we should exit the loop and skip the phase_lock
  1467. * in the ITloop. Performing back to back phase_locks
  1468. * shouldn't hurt, but why do it twice...
  1469. */
  1470. host_message_loop:
  1471. mvi HOST_MSG_LOOP call set_seqint;
  1472. call phase_lock;
  1473. cmp RETURN_1, EXIT_MSG_LOOP je ITloop + 1;
  1474. jmp host_message_loop;
  1475. mesgin_ign_wide_residue:
  1476. if ((ahc->features & AHC_WIDE) != 0) {
  1477. test SCSIRATE, WIDEXFER jz mesgin_reject;
  1478. /* Pull the residue byte */
  1479. mvi ARG_1 call inb_next;
  1480. cmp ARG_1, 0x01 jne mesgin_reject;
  1481. test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz . + 2;
  1482. test SCB_LUN, SCB_XFERLEN_ODD jnz mesgin_done;
  1483. mvi IGN_WIDE_RES call set_seqint;
  1484. jmp mesgin_done;
  1485. }
  1486. mesgin_proto_violation:
  1487. mvi PROTO_VIOLATION call set_seqint;
  1488. jmp mesgin_done;
  1489. mesgin_reject:
  1490. mvi MSG_MESSAGE_REJECT call mk_mesg;
  1491. mesgin_done:
  1492. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  1493. jmp ITloop;
  1494. /*
  1495. * We received a "command complete" message. Put the SCB_TAG into the QOUTFIFO,
  1496. * and trigger a completion interrupt. Before doing so, check to see if there
  1497. * is a residual or the status byte is something other than STATUS_GOOD (0).
  1498. * In either of these conditions, we upload the SCB back to the host so it can
  1499. * process this information. In the case of a non zero status byte, we
  1500. * additionally interrupt the kernel driver synchronously, allowing it to
  1501. * decide if sense should be retrieved. If the kernel driver wishes to request
  1502. * sense, it will fill the kernel SCB with a request sense command, requeue
  1503. * it to the QINFIFO and tell us not to post to the QOUTFIFO by setting
  1504. * RETURN_1 to SEND_SENSE.
  1505. */
  1506. mesgin_complete:
  1507. /*
  1508. * If ATN is raised, we still want to give the target a message.
  1509. * Perhaps there was a parity error on this last message byte.
  1510. * Either way, the target should take us to message out phase
  1511. * and then attempt to complete the command again. We should use a
  1512. * critical section here to guard against a timeout triggering
  1513. * for this command and setting ATN while we are still processing
  1514. * the completion.
  1515. test SCSISIGI, ATNI jnz mesgin_done;
  1516. */
  1517. /*
  1518. * If we are identified and have successfully sent the CDB,
  1519. * any status will do. Optimize this fast path.
  1520. */
  1521. test SCB_CONTROL, STATUS_RCVD jz mesgin_proto_violation;
  1522. test SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT jz complete_accepted;
  1523. /*
  1524. * If the target never sent an identify message but instead went
  1525. * to mesgin to give an invalid message, let the host abort us.
  1526. */
  1527. test SEQ_FLAGS, NOT_IDENTIFIED jnz mesgin_proto_violation;
  1528. /*
  1529. * If we recevied good status but never successfully sent the
  1530. * cdb, abort the command.
  1531. */
  1532. test SCB_SCSI_STATUS,0xff jnz complete_accepted;
  1533. test SEQ_FLAGS, NO_CDB_SENT jnz mesgin_proto_violation;
  1534. complete_accepted:
  1535. /*
  1536. * See if we attempted to deliver a message but the target ingnored us.
  1537. */
  1538. test SCB_CONTROL, MK_MESSAGE jz . + 2;
  1539. mvi MKMSG_FAILED call set_seqint;
  1540. /*
  1541. * Check for residuals
  1542. */
  1543. test SCB_SGPTR, SG_LIST_NULL jnz check_status;/* No xfer */
  1544. test SCB_SGPTR, SG_FULL_RESID jnz upload_scb;/* Never xfered */
  1545. test SCB_RESIDUAL_SGPTR, SG_LIST_NULL jz upload_scb;
  1546. check_status:
  1547. test SCB_SCSI_STATUS,0xff jz complete; /* Good Status? */
  1548. upload_scb:
  1549. or SCB_SGPTR, SG_RESID_VALID;
  1550. mvi DMAPARAMS, FIFORESET;
  1551. mov SCB_TAG call dma_scb;
  1552. test SCB_SCSI_STATUS, 0xff jz complete; /* Just a residual? */
  1553. mvi BAD_STATUS call set_seqint; /* let driver know */
  1554. cmp RETURN_1, SEND_SENSE jne complete;
  1555. call add_scb_to_free_list;
  1556. jmp await_busfree;
  1557. complete:
  1558. mov SCB_TAG call complete_post;
  1559. jmp await_busfree;
  1560. }
  1561. complete_post:
  1562. /* Post the SCBID in SINDEX and issue an interrupt */
  1563. call add_scb_to_free_list;
  1564. mov ARG_1, SINDEX;
  1565. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  1566. mov A, SDSCB_QOFF;
  1567. } else {
  1568. mov A, QOUTPOS;
  1569. }
  1570. mvi QOUTFIFO_OFFSET call post_byte_setup;
  1571. mov ARG_1 call post_byte;
  1572. if ((ahc->features & AHC_QUEUE_REGS) == 0) {
  1573. inc QOUTPOS;
  1574. }
  1575. mvi INTSTAT,CMDCMPLT ret;
  1576. if ((ahc->flags & AHC_INITIATORROLE) != 0) {
  1577. /*
  1578. * Is it a disconnect message? Set a flag in the SCB to remind us
  1579. * and await the bus going free. If this is an untagged transaction
  1580. * store the SCB id for it in our untagged target table for lookup on
  1581. * a reselction.
  1582. */
  1583. mesgin_disconnect:
  1584. /*
  1585. * If ATN is raised, we still want to give the target a message.
  1586. * Perhaps there was a parity error on this last message byte
  1587. * or we want to abort this command. Either way, the target
  1588. * should take us to message out phase and then attempt to
  1589. * disconnect again.
  1590. * XXX - Wait for more testing.
  1591. test SCSISIGI, ATNI jnz mesgin_done;
  1592. */
  1593. test SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT
  1594. jnz mesgin_proto_violation;
  1595. or SCB_CONTROL,DISCONNECTED;
  1596. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1597. call add_scb_to_disc_list;
  1598. }
  1599. test SCB_CONTROL, TAG_ENB jnz await_busfree;
  1600. mov ARG_1, SCB_TAG;
  1601. and SAVED_LUN, LID, SCB_LUN;
  1602. mov SCB_SCSIID call set_busy_target;
  1603. jmp await_busfree;
  1604. /*
  1605. * Save data pointers message:
  1606. * Copying RAM values back to SCB, for Save Data Pointers message, but
  1607. * only if we've actually been into a data phase to change them. This
  1608. * protects against bogus data in scratch ram and the residual counts
  1609. * since they are only initialized when we go into data_in or data_out.
  1610. * Ack the message as soon as possible. For chips without S/G pipelining,
  1611. * we can only ack the message after SHADDR has been saved. On these
  1612. * chips, SHADDR increments with every bus transaction, even PIO.
  1613. */
  1614. mesgin_sdptrs:
  1615. if ((ahc->features & AHC_ULTRA2) != 0) {
  1616. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  1617. test SEQ_FLAGS, DPHASE jz ITloop;
  1618. } else {
  1619. test SEQ_FLAGS, DPHASE jz mesgin_done;
  1620. }
  1621. /*
  1622. * If we are asked to save our position at the end of the
  1623. * transfer, just mark us at the end rather than perform a
  1624. * full save.
  1625. */
  1626. test SCB_RESIDUAL_SGPTR[0], SG_LIST_NULL jz mesgin_sdptrs_full;
  1627. or SCB_SGPTR, SG_LIST_NULL;
  1628. if ((ahc->features & AHC_ULTRA2) != 0) {
  1629. jmp ITloop;
  1630. } else {
  1631. jmp mesgin_done;
  1632. }
  1633. mesgin_sdptrs_full:
  1634. /*
  1635. * The SCB_SGPTR becomes the next one we'll download,
  1636. * and the SCB_DATAPTR becomes the current SHADDR.
  1637. * Use the residual number since STCNT is corrupted by
  1638. * any message transfer.
  1639. */
  1640. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1641. bmov SCB_DATAPTR, SHADDR, 4;
  1642. if ((ahc->features & AHC_ULTRA2) == 0) {
  1643. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  1644. }
  1645. bmov SCB_DATACNT, SCB_RESIDUAL_DATACNT, 8;
  1646. } else {
  1647. mvi DINDEX, SCB_DATAPTR;
  1648. mvi SHADDR call bcopy_4;
  1649. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  1650. mvi SCB_RESIDUAL_DATACNT call bcopy_8;
  1651. }
  1652. jmp ITloop;
  1653. /*
  1654. * Restore pointers message? Data pointers are recopied from the
  1655. * SCB anytime we enter a data phase for the first time, so all
  1656. * we need to do is clear the DPHASE flag and let the data phase
  1657. * code do the rest. We also reset/reallocate the FIFO to make
  1658. * sure we have a clean start for the next data or command phase.
  1659. */
  1660. mesgin_rdptrs:
  1661. and SEQ_FLAGS, ~DPHASE; /*
  1662. * We'll reload them
  1663. * the next time through
  1664. * the dataphase.
  1665. */
  1666. or SXFRCTL0, CLRSTCNT|CLRCHN;
  1667. jmp mesgin_done;
  1668. /*
  1669. * Index into our Busy Target table. SINDEX and DINDEX are modified
  1670. * upon return. SCBPTR may be modified by this action.
  1671. */
  1672. set_busy_target:
  1673. shr DINDEX, 4, SINDEX;
  1674. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1675. mov SCBPTR, SAVED_LUN;
  1676. add DINDEX, SCB_64_BTT;
  1677. } else {
  1678. add DINDEX, BUSY_TARGETS;
  1679. }
  1680. mov DINDIR, ARG_1 ret;
  1681. /*
  1682. * Identify message? For a reconnecting target, this tells us the lun
  1683. * that the reconnection is for - find the correct SCB and switch to it,
  1684. * clearing the "disconnected" bit so we don't "find" it by accident later.
  1685. */
  1686. mesgin_identify:
  1687. /*
  1688. * Determine whether a target is using tagged or non-tagged
  1689. * transactions by first looking at the transaction stored in
  1690. * the busy target array. If there is no untagged transaction
  1691. * for this target or the transaction is for a different lun, then
  1692. * this must be a tagged transaction.
  1693. */
  1694. shr SINDEX, 4, SAVED_SCSIID;
  1695. and SAVED_LUN, MSG_IDENTIFY_LUNMASK, A;
  1696. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1697. add SINDEX, SCB_64_BTT;
  1698. mov SCBPTR, SAVED_LUN;
  1699. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1700. add NONE, -SCB_64_BTT, SINDEX;
  1701. jc . + 2;
  1702. mvi INTSTAT, OUT_OF_RANGE;
  1703. nop;
  1704. add NONE, -(SCB_64_BTT + 16), SINDEX;
  1705. jnc . + 2;
  1706. mvi INTSTAT, OUT_OF_RANGE;
  1707. nop;
  1708. }
  1709. } else {
  1710. add SINDEX, BUSY_TARGETS;
  1711. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1712. add NONE, -BUSY_TARGETS, SINDEX;
  1713. jc . + 2;
  1714. mvi INTSTAT, OUT_OF_RANGE;
  1715. nop;
  1716. add NONE, -(BUSY_TARGETS + 16), SINDEX;
  1717. jnc . + 2;
  1718. mvi INTSTAT, OUT_OF_RANGE;
  1719. nop;
  1720. }
  1721. }
  1722. mov ARG_1, SINDIR;
  1723. cmp ARG_1, SCB_LIST_NULL je snoop_tag;
  1724. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1725. mov ARG_1 call findSCB;
  1726. } else {
  1727. mov SCBPTR, ARG_1;
  1728. }
  1729. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1730. jmp setup_SCB_id_lun_okay;
  1731. } else {
  1732. /*
  1733. * We only allow one untagged command per-target
  1734. * at a time. So, if the lun doesn't match, look
  1735. * for a tag message.
  1736. */
  1737. and A, LID, SCB_LUN;
  1738. cmp SAVED_LUN, A je setup_SCB_id_lun_okay;
  1739. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1740. /*
  1741. * findSCB removes the SCB from the
  1742. * disconnected list, so we must replace
  1743. * it there should this SCB be for another
  1744. * lun.
  1745. */
  1746. call cleanup_scb;
  1747. }
  1748. }
  1749. /*
  1750. * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
  1751. * If we get one, we use the tag returned to find the proper
  1752. * SCB. With SCB paging, we must search for non-tagged
  1753. * transactions since the SCB may exist in any slot. If we're not
  1754. * using SCB paging, we can use the tag as the direct index to the
  1755. * SCB.
  1756. */
  1757. snoop_tag:
  1758. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1759. or SEQ_FLAGS, 0x80;
  1760. }
  1761. mov NONE,SCSIDATL; /* ACK Identify MSG */
  1762. call phase_lock;
  1763. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1764. or SEQ_FLAGS, 0x1;
  1765. }
  1766. cmp LASTPHASE, P_MESGIN jne not_found;
  1767. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1768. or SEQ_FLAGS, 0x2;
  1769. }
  1770. cmp SCSIBUSL,MSG_SIMPLE_Q_TAG jne not_found;
  1771. get_tag:
  1772. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1773. mvi ARG_1 call inb_next; /* tag value */
  1774. mov ARG_1 call findSCB;
  1775. } else {
  1776. mvi ARG_1 call inb_next; /* tag value */
  1777. mov SCBPTR, ARG_1;
  1778. }
  1779. /*
  1780. * Ensure that the SCB the tag points to is for
  1781. * an SCB transaction to the reconnecting target.
  1782. */
  1783. setup_SCB:
  1784. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1785. or SEQ_FLAGS, 0x4;
  1786. }
  1787. mov A, SCB_SCSIID;
  1788. cmp SAVED_SCSIID, A jne not_found_cleanup_scb;
  1789. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1790. or SEQ_FLAGS, 0x8;
  1791. }
  1792. setup_SCB_id_okay:
  1793. and A, LID, SCB_LUN;
  1794. cmp SAVED_LUN, A jne not_found_cleanup_scb;
  1795. setup_SCB_id_lun_okay:
  1796. if ((ahc->flags & AHC_SEQUENCER_DEBUG) != 0) {
  1797. or SEQ_FLAGS, 0x10;
  1798. }
  1799. test SCB_CONTROL,DISCONNECTED jz not_found_cleanup_scb;
  1800. and SCB_CONTROL,~DISCONNECTED;
  1801. test SCB_CONTROL, TAG_ENB jnz setup_SCB_tagged;
  1802. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1803. mov A, SCBPTR;
  1804. }
  1805. mvi ARG_1, SCB_LIST_NULL;
  1806. mov SAVED_SCSIID call set_busy_target;
  1807. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  1808. mov SCBPTR, A;
  1809. }
  1810. setup_SCB_tagged:
  1811. clr SEQ_FLAGS; /* make note of IDENTIFY */
  1812. call set_transfer_settings;
  1813. /* See if the host wants to send a message upon reconnection */
  1814. test SCB_CONTROL, MK_MESSAGE jz mesgin_done;
  1815. mvi HOST_MSG call mk_mesg;
  1816. jmp mesgin_done;
  1817. not_found_cleanup_scb:
  1818. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1819. call cleanup_scb;
  1820. }
  1821. not_found:
  1822. mvi NO_MATCH call set_seqint;
  1823. jmp mesgin_done;
  1824. mk_mesg:
  1825. if ((ahc->features & AHC_DT) == 0) {
  1826. or SCSISIGO, ATNO, LASTPHASE;
  1827. } else {
  1828. mvi SCSISIGO, ATNO;
  1829. }
  1830. mov MSG_OUT,SINDEX ret;
  1831. /*
  1832. * Functions to read data in Automatic PIO mode.
  1833. *
  1834. * According to Adaptec's documentation, an ACK is not sent on input from
  1835. * the target until SCSIDATL is read from. So we wait until SCSIDATL is
  1836. * latched (the usual way), then read the data byte directly off the bus
  1837. * using SCSIBUSL. When we have pulled the ATN line, or we just want to
  1838. * acknowledge the byte, then we do a dummy read from SCISDATL. The SCSI
  1839. * spec guarantees that the target will hold the data byte on the bus until
  1840. * we send our ACK.
  1841. *
  1842. * The assumption here is that these are called in a particular sequence,
  1843. * and that REQ is already set when inb_first is called. inb_{first,next}
  1844. * use the same calling convention as inb.
  1845. */
  1846. inb_next_wait_perr:
  1847. mvi PERR_DETECTED call set_seqint;
  1848. jmp inb_next_wait;
  1849. inb_next:
  1850. mov NONE,SCSIDATL; /*dummy read from latch to ACK*/
  1851. inb_next_wait:
  1852. /*
  1853. * If there is a parity error, wait for the kernel to
  1854. * see the interrupt and prepare our message response
  1855. * before continuing.
  1856. */
  1857. test SSTAT1, REQINIT jz inb_next_wait;
  1858. test SSTAT1, SCSIPERR jnz inb_next_wait_perr;
  1859. inb_next_check_phase:
  1860. and LASTPHASE, PHASE_MASK, SCSISIGI;
  1861. cmp LASTPHASE, P_MESGIN jne mesgin_phasemis;
  1862. inb_first:
  1863. mov DINDEX,SINDEX;
  1864. mov DINDIR,SCSIBUSL ret; /*read byte directly from bus*/
  1865. inb_last:
  1866. mov NONE,SCSIDATL ret; /*dummy read from latch to ACK*/
  1867. }
  1868. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  1869. /*
  1870. * Change to a new phase. If we are changing the state of the I/O signal,
  1871. * from out to in, wait an additional data release delay before continuing.
  1872. */
  1873. change_phase:
  1874. /* Wait for preceeding I/O session to complete. */
  1875. test SCSISIGI, ACKI jnz .;
  1876. /* Change the phase */
  1877. and DINDEX, IOI, SCSISIGI;
  1878. mov SCSISIGO, SINDEX;
  1879. and A, IOI, SINDEX;
  1880. /*
  1881. * If the data direction has changed, from
  1882. * out (initiator driving) to in (target driving),
  1883. * we must wait at least a data release delay plus
  1884. * the normal bus settle delay. [SCSI III SPI 10.11.0]
  1885. */
  1886. cmp DINDEX, A je change_phase_wait;
  1887. test SINDEX, IOI jz change_phase_wait;
  1888. call change_phase_wait;
  1889. change_phase_wait:
  1890. nop;
  1891. nop;
  1892. nop;
  1893. nop ret;
  1894. /*
  1895. * Send a byte to an initiator in Automatic PIO mode.
  1896. */
  1897. target_outb:
  1898. or SXFRCTL0, SPIOEN;
  1899. test SSTAT0, SPIORDY jz .;
  1900. mov SCSIDATL, SINDEX;
  1901. test SSTAT0, SPIORDY jz .;
  1902. and SXFRCTL0, ~SPIOEN ret;
  1903. }
  1904. /*
  1905. * Locate a disconnected SCB by SCBID. Upon return, SCBPTR and SINDEX will
  1906. * be set to the position of the SCB. If the SCB cannot be found locally,
  1907. * it will be paged in from host memory. RETURN_2 stores the address of the
  1908. * preceding SCB in the disconnected list which can be used to speed up
  1909. * removal of the found SCB from the disconnected list.
  1910. */
  1911. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  1912. BEGIN_CRITICAL;
  1913. findSCB:
  1914. mov A, SINDEX; /* Tag passed in SINDEX */
  1915. cmp DISCONNECTED_SCBH, SCB_LIST_NULL je findSCB_notFound;
  1916. mov SCBPTR, DISCONNECTED_SCBH; /* Initialize SCBPTR */
  1917. mvi ARG_2, SCB_LIST_NULL; /* Head of list */
  1918. jmp findSCB_loop;
  1919. findSCB_next:
  1920. cmp SCB_NEXT, SCB_LIST_NULL je findSCB_notFound;
  1921. mov ARG_2, SCBPTR;
  1922. mov SCBPTR,SCB_NEXT;
  1923. findSCB_loop:
  1924. cmp SCB_TAG, A jne findSCB_next;
  1925. rem_scb_from_disc_list:
  1926. cmp ARG_2, SCB_LIST_NULL je rHead;
  1927. mov DINDEX, SCB_NEXT;
  1928. mov SINDEX, SCBPTR;
  1929. mov SCBPTR, ARG_2;
  1930. mov SCB_NEXT, DINDEX;
  1931. mov SCBPTR, SINDEX ret;
  1932. rHead:
  1933. mov DISCONNECTED_SCBH,SCB_NEXT ret;
  1934. END_CRITICAL;
  1935. findSCB_notFound:
  1936. /*
  1937. * We didn't find it. Page in the SCB.
  1938. */
  1939. mov ARG_1, A; /* Save tag */
  1940. mov ALLZEROS call get_free_or_disc_scb;
  1941. mvi DMAPARAMS, HDMAEN|DIRECTION|FIFORESET;
  1942. mov ARG_1 jmp dma_scb;
  1943. }
  1944. /*
  1945. * Prepare the hardware to post a byte to host memory given an
  1946. * index of (A + (256 * SINDEX)) and a base address of SHARED_DATA_ADDR.
  1947. */
  1948. post_byte_setup:
  1949. mov ARG_2, SINDEX;
  1950. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1951. mvi DINDEX, CCHADDR;
  1952. mvi SHARED_DATA_ADDR call set_1byte_addr;
  1953. mvi CCHCNT, 1;
  1954. mvi CCSCBCTL, CCSCBRESET ret;
  1955. } else {
  1956. mvi DINDEX, HADDR;
  1957. mvi SHARED_DATA_ADDR call set_1byte_addr;
  1958. mvi 1 call set_hcnt;
  1959. mvi DFCNTRL, FIFORESET ret;
  1960. }
  1961. post_byte:
  1962. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  1963. bmov CCSCBRAM, SINDEX, 1;
  1964. or CCSCBCTL, CCSCBEN|CCSCBRESET;
  1965. test CCSCBCTL, CCSCBDONE jz .;
  1966. clr CCSCBCTL ret;
  1967. } else {
  1968. mov DFDAT, SINDEX;
  1969. or DFCNTRL, HDMAEN|FIFOFLUSH;
  1970. jmp dma_finish;
  1971. }
  1972. phase_lock_perr:
  1973. mvi PERR_DETECTED call set_seqint;
  1974. phase_lock:
  1975. /*
  1976. * If there is a parity error, wait for the kernel to
  1977. * see the interrupt and prepare our message response
  1978. * before continuing.
  1979. */
  1980. test SSTAT1, REQINIT jz phase_lock;
  1981. test SSTAT1, SCSIPERR jnz phase_lock_perr;
  1982. phase_lock_latch_phase:
  1983. if ((ahc->features & AHC_DT) == 0) {
  1984. and SCSISIGO, PHASE_MASK, SCSISIGI;
  1985. }
  1986. and LASTPHASE, PHASE_MASK, SCSISIGI ret;
  1987. if ((ahc->features & AHC_CMD_CHAN) == 0) {
  1988. set_hcnt:
  1989. mov HCNT[0], SINDEX;
  1990. clear_hcnt:
  1991. clr HCNT[1];
  1992. clr HCNT[2] ret;
  1993. set_stcnt_from_hcnt:
  1994. mov STCNT[0], HCNT[0];
  1995. mov STCNT[1], HCNT[1];
  1996. mov STCNT[2], HCNT[2] ret;
  1997. bcopy_8:
  1998. mov DINDIR, SINDIR;
  1999. bcopy_7:
  2000. mov DINDIR, SINDIR;
  2001. mov DINDIR, SINDIR;
  2002. bcopy_5:
  2003. mov DINDIR, SINDIR;
  2004. bcopy_4:
  2005. mov DINDIR, SINDIR;
  2006. bcopy_3:
  2007. mov DINDIR, SINDIR;
  2008. mov DINDIR, SINDIR;
  2009. mov DINDIR, SINDIR ret;
  2010. }
  2011. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  2012. /*
  2013. * Setup addr assuming that A is an index into
  2014. * an array of 32byte objects, SINDEX contains
  2015. * the base address of that array, and DINDEX
  2016. * contains the base address of the location
  2017. * to store the indexed address.
  2018. */
  2019. set_32byte_addr:
  2020. shr ARG_2, 3, A;
  2021. shl A, 5;
  2022. jmp set_1byte_addr;
  2023. }
  2024. /*
  2025. * Setup addr assuming that A is an index into
  2026. * an array of 64byte objects, SINDEX contains
  2027. * the base address of that array, and DINDEX
  2028. * contains the base address of the location
  2029. * to store the indexed address.
  2030. */
  2031. set_64byte_addr:
  2032. shr ARG_2, 2, A;
  2033. shl A, 6;
  2034. /*
  2035. * Setup addr assuming that A + (ARG_2 * 256) is an
  2036. * index into an array of 1byte objects, SINDEX contains
  2037. * the base address of that array, and DINDEX contains
  2038. * the base address of the location to store the computed
  2039. * address.
  2040. */
  2041. set_1byte_addr:
  2042. add DINDIR, A, SINDIR;
  2043. mov A, ARG_2;
  2044. adc DINDIR, A, SINDIR;
  2045. clr A;
  2046. adc DINDIR, A, SINDIR;
  2047. adc DINDIR, A, SINDIR ret;
  2048. /*
  2049. * Either post or fetch an SCB from host memory based on the
  2050. * DIRECTION bit in DMAPARAMS. The host SCB index is in SINDEX.
  2051. */
  2052. dma_scb:
  2053. mov A, SINDEX;
  2054. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  2055. mvi DINDEX, CCHADDR;
  2056. mvi HSCB_ADDR call set_64byte_addr;
  2057. mov CCSCBPTR, SCBPTR;
  2058. test DMAPARAMS, DIRECTION jz dma_scb_tohost;
  2059. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  2060. mvi CCHCNT, SCB_DOWNLOAD_SIZE_64;
  2061. } else {
  2062. mvi CCHCNT, SCB_DOWNLOAD_SIZE;
  2063. }
  2064. mvi CCSCBCTL, CCARREN|CCSCBEN|CCSCBDIR|CCSCBRESET;
  2065. cmp CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN|CCSCBDIR jne .;
  2066. jmp dma_scb_finish;
  2067. dma_scb_tohost:
  2068. mvi CCHCNT, SCB_UPLOAD_SIZE;
  2069. if ((ahc->features & AHC_ULTRA2) == 0) {
  2070. mvi CCSCBCTL, CCSCBRESET;
  2071. bmov CCSCBRAM, SCB_BASE, SCB_UPLOAD_SIZE;
  2072. or CCSCBCTL, CCSCBEN|CCSCBRESET;
  2073. test CCSCBCTL, CCSCBDONE jz .;
  2074. } else if ((ahc->bugs & AHC_SCBCHAN_UPLOAD_BUG) != 0) {
  2075. mvi CCSCBCTL, CCARREN|CCSCBRESET;
  2076. cmp CCSCBCTL, ARRDONE|CCARREN jne .;
  2077. mvi CCHCNT, SCB_UPLOAD_SIZE;
  2078. mvi CCSCBCTL, CCSCBEN|CCSCBRESET;
  2079. cmp CCSCBCTL, CCSCBDONE|CCSCBEN jne .;
  2080. } else {
  2081. mvi CCSCBCTL, CCARREN|CCSCBEN|CCSCBRESET;
  2082. cmp CCSCBCTL, CCSCBDONE|ARRDONE|CCARREN|CCSCBEN jne .;
  2083. }
  2084. dma_scb_finish:
  2085. clr CCSCBCTL;
  2086. test CCSCBCTL, CCARREN|CCSCBEN jnz .;
  2087. ret;
  2088. } else {
  2089. mvi DINDEX, HADDR;
  2090. mvi HSCB_ADDR call set_64byte_addr;
  2091. mvi SCB_DOWNLOAD_SIZE call set_hcnt;
  2092. mov DFCNTRL, DMAPARAMS;
  2093. test DMAPARAMS, DIRECTION jnz dma_scb_fromhost;
  2094. /* Fill it with the SCB data */
  2095. copy_scb_tofifo:
  2096. mvi SINDEX, SCB_BASE;
  2097. add A, SCB_DOWNLOAD_SIZE, SINDEX;
  2098. copy_scb_tofifo_loop:
  2099. call copy_to_fifo_8;
  2100. cmp SINDEX, A jne copy_scb_tofifo_loop;
  2101. or DFCNTRL, HDMAEN|FIFOFLUSH;
  2102. jmp dma_finish;
  2103. dma_scb_fromhost:
  2104. mvi DINDEX, SCB_BASE;
  2105. if ((ahc->bugs & AHC_PCI_2_1_RETRY_BUG) != 0) {
  2106. /*
  2107. * The PCI module will only issue a PCI
  2108. * retry if the data FIFO is empty. If the
  2109. * host disconnects in the middle of a
  2110. * transfer, we must empty the fifo of all
  2111. * available data to force the chip to
  2112. * continue the transfer. This does not
  2113. * happen for SCSI transfers as the SCSI module
  2114. * will drain the FIFO as data are made available.
  2115. * When the hang occurs, we know that a multiple
  2116. * of 8 bytes is in the FIFO because the PCI
  2117. * module has an 8 byte input latch that only
  2118. * dumps to the FIFO when HCNT == 0 or the
  2119. * latch is full.
  2120. */
  2121. clr A;
  2122. /* Wait for at least 8 bytes of data to arrive. */
  2123. dma_scb_hang_fifo:
  2124. test DFSTATUS, FIFOQWDEMP jnz dma_scb_hang_fifo;
  2125. dma_scb_hang_wait:
  2126. test DFSTATUS, MREQPEND jnz dma_scb_hang_wait;
  2127. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  2128. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  2129. test DFSTATUS, HDONE jnz dma_scb_hang_dma_done;
  2130. /*
  2131. * The PCI module no longer intends to perform
  2132. * a PCI transaction. Drain the fifo.
  2133. */
  2134. dma_scb_hang_dma_drain_fifo:
  2135. not A, HCNT;
  2136. add A, SCB_DOWNLOAD_SIZE+SCB_BASE+1;
  2137. and A, ~0x7;
  2138. mov DINDIR,DFDAT;
  2139. cmp DINDEX, A jne . - 1;
  2140. cmp DINDEX, SCB_DOWNLOAD_SIZE+SCB_BASE
  2141. je dma_finish_nowait;
  2142. /* Restore A as the lines left to transfer. */
  2143. add A, -SCB_BASE, DINDEX;
  2144. shr A, 3;
  2145. jmp dma_scb_hang_fifo;
  2146. dma_scb_hang_dma_done:
  2147. and DFCNTRL, ~HDMAEN;
  2148. test DFCNTRL, HDMAEN jnz .;
  2149. add SEQADDR0, A;
  2150. } else {
  2151. call dma_finish;
  2152. }
  2153. call dfdat_in_8;
  2154. call dfdat_in_8;
  2155. call dfdat_in_8;
  2156. dfdat_in_8:
  2157. mov DINDIR,DFDAT;
  2158. dfdat_in_7:
  2159. mov DINDIR,DFDAT;
  2160. mov DINDIR,DFDAT;
  2161. mov DINDIR,DFDAT;
  2162. mov DINDIR,DFDAT;
  2163. mov DINDIR,DFDAT;
  2164. dfdat_in_2:
  2165. mov DINDIR,DFDAT;
  2166. mov DINDIR,DFDAT ret;
  2167. }
  2168. copy_to_fifo_8:
  2169. mov DFDAT,SINDIR;
  2170. mov DFDAT,SINDIR;
  2171. copy_to_fifo_6:
  2172. mov DFDAT,SINDIR;
  2173. copy_to_fifo_5:
  2174. mov DFDAT,SINDIR;
  2175. copy_to_fifo_4:
  2176. mov DFDAT,SINDIR;
  2177. mov DFDAT,SINDIR;
  2178. mov DFDAT,SINDIR;
  2179. mov DFDAT,SINDIR ret;
  2180. /*
  2181. * Wait for DMA from host memory to data FIFO to complete, then disable
  2182. * DMA and wait for it to acknowledge that it's off.
  2183. */
  2184. dma_finish:
  2185. test DFSTATUS,HDONE jz dma_finish;
  2186. dma_finish_nowait:
  2187. /* Turn off DMA */
  2188. and DFCNTRL, ~HDMAEN;
  2189. test DFCNTRL, HDMAEN jnz .;
  2190. ret;
  2191. /*
  2192. * Restore an SCB that failed to match an incoming reselection
  2193. * to the correct/safe state. If the SCB is for a disconnected
  2194. * transaction, it must be returned to the disconnected list.
  2195. * If it is not in the disconnected state, it must be free.
  2196. */
  2197. cleanup_scb:
  2198. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  2199. test SCB_CONTROL,DISCONNECTED jnz add_scb_to_disc_list;
  2200. }
  2201. add_scb_to_free_list:
  2202. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  2203. BEGIN_CRITICAL;
  2204. mov SCB_NEXT, FREE_SCBH;
  2205. mvi SCB_TAG, SCB_LIST_NULL;
  2206. mov FREE_SCBH, SCBPTR ret;
  2207. END_CRITICAL;
  2208. } else {
  2209. mvi SCB_TAG, SCB_LIST_NULL ret;
  2210. }
  2211. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  2212. set_hhaddr:
  2213. or DSCOMMAND1, HADDLDSEL0;
  2214. and HADDR, SG_HIGH_ADDR_BITS, SINDEX;
  2215. and DSCOMMAND1, ~HADDLDSEL0 ret;
  2216. }
  2217. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  2218. get_free_or_disc_scb:
  2219. BEGIN_CRITICAL;
  2220. cmp FREE_SCBH, SCB_LIST_NULL jne dequeue_free_scb;
  2221. cmp DISCONNECTED_SCBH, SCB_LIST_NULL jne dequeue_disc_scb;
  2222. return_error:
  2223. mvi NO_FREE_SCB call set_seqint;
  2224. mvi SINDEX, SCB_LIST_NULL ret;
  2225. dequeue_disc_scb:
  2226. mov SCBPTR, DISCONNECTED_SCBH;
  2227. mov DISCONNECTED_SCBH, SCB_NEXT;
  2228. END_CRITICAL;
  2229. mvi DMAPARAMS, FIFORESET;
  2230. mov SCB_TAG jmp dma_scb;
  2231. BEGIN_CRITICAL;
  2232. dequeue_free_scb:
  2233. mov SCBPTR, FREE_SCBH;
  2234. mov FREE_SCBH, SCB_NEXT ret;
  2235. END_CRITICAL;
  2236. add_scb_to_disc_list:
  2237. /*
  2238. * Link this SCB into the DISCONNECTED list. This list holds the
  2239. * candidates for paging out an SCB if one is needed for a new command.
  2240. * Modifying the disconnected list is a critical(pause dissabled) section.
  2241. */
  2242. BEGIN_CRITICAL;
  2243. mov SCB_NEXT, DISCONNECTED_SCBH;
  2244. mov DISCONNECTED_SCBH, SCBPTR ret;
  2245. END_CRITICAL;
  2246. }
  2247. set_seqint:
  2248. mov INTSTAT, SINDEX;
  2249. nop;
  2250. return:
  2251. ret;