NCR53C9x.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644
  1. /* NCR53C9x.c: Generic SCSI driver code for NCR53C9x chips.
  2. *
  3. * Originally esp.c : EnhancedScsiProcessor Sun SCSI driver code.
  4. *
  5. * Copyright (C) 1995, 1998 David S. Miller (davem@caip.rutgers.edu)
  6. *
  7. * Most DMA dependencies put in driver specific files by
  8. * Jesper Skov (jskov@cygnus.co.uk)
  9. *
  10. * Set up to use esp_read/esp_write (preprocessor macros in NCR53c9x.h) by
  11. * Tymm Twillman (tymm@coe.missouri.edu)
  12. */
  13. /* TODO:
  14. *
  15. * 1) Maybe disable parity checking in config register one for SCSI1
  16. * targets. (Gilmore says parity error on the SBus can lock up
  17. * old sun4c's)
  18. * 2) Add support for DMA2 pipelining.
  19. * 3) Add tagged queueing.
  20. * 4) Maybe change use of "esp" to something more "NCR"'ish.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/types.h>
  26. #include <linux/string.h>
  27. #include <linux/slab.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/stat.h>
  32. #include <linux/init.h>
  33. #include "scsi.h"
  34. #include <scsi/scsi_host.h>
  35. #include "NCR53C9x.h"
  36. #include <asm/system.h>
  37. #include <asm/ptrace.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. /* Command phase enumeration. */
  42. enum {
  43. not_issued = 0x00, /* Still in the issue_SC queue. */
  44. /* Various forms of selecting a target. */
  45. #define in_slct_mask 0x10
  46. in_slct_norm = 0x10, /* ESP is arbitrating, normal selection */
  47. in_slct_stop = 0x11, /* ESP will select, then stop with IRQ */
  48. in_slct_msg = 0x12, /* select, then send a message */
  49. in_slct_tag = 0x13, /* select and send tagged queue msg */
  50. in_slct_sneg = 0x14, /* select and acquire sync capabilities */
  51. /* Any post selection activity. */
  52. #define in_phases_mask 0x20
  53. in_datain = 0x20, /* Data is transferring from the bus */
  54. in_dataout = 0x21, /* Data is transferring to the bus */
  55. in_data_done = 0x22, /* Last DMA data operation done (maybe) */
  56. in_msgin = 0x23, /* Eating message from target */
  57. in_msgincont = 0x24, /* Eating more msg bytes from target */
  58. in_msgindone = 0x25, /* Decide what to do with what we got */
  59. in_msgout = 0x26, /* Sending message to target */
  60. in_msgoutdone = 0x27, /* Done sending msg out */
  61. in_cmdbegin = 0x28, /* Sending cmd after abnormal selection */
  62. in_cmdend = 0x29, /* Done sending slow cmd */
  63. in_status = 0x2a, /* Was in status phase, finishing cmd */
  64. in_freeing = 0x2b, /* freeing the bus for cmd cmplt or disc */
  65. in_the_dark = 0x2c, /* Don't know what bus phase we are in */
  66. /* Special states, ie. not normal bus transitions... */
  67. #define in_spec_mask 0x80
  68. in_abortone = 0x80, /* Aborting one command currently */
  69. in_abortall = 0x81, /* Blowing away all commands we have */
  70. in_resetdev = 0x82, /* SCSI target reset in progress */
  71. in_resetbus = 0x83, /* SCSI bus reset in progress */
  72. in_tgterror = 0x84, /* Target did something stupid */
  73. };
  74. enum {
  75. /* Zero has special meaning, see skipahead[12]. */
  76. /*0*/ do_never,
  77. /*1*/ do_phase_determine,
  78. /*2*/ do_reset_bus,
  79. /*3*/ do_reset_complete,
  80. /*4*/ do_work_bus,
  81. /*5*/ do_intr_end
  82. };
  83. /* The master ring of all esp hosts we are managing in this driver. */
  84. static struct NCR_ESP *espchain;
  85. int nesps = 0, esps_in_use = 0, esps_running = 0;
  86. irqreturn_t esp_intr(int irq, void *dev_id);
  87. /* Debugging routines */
  88. static struct esp_cmdstrings {
  89. unchar cmdchar;
  90. char *text;
  91. } esp_cmd_strings[] = {
  92. /* Miscellaneous */
  93. { ESP_CMD_NULL, "ESP_NOP", },
  94. { ESP_CMD_FLUSH, "FIFO_FLUSH", },
  95. { ESP_CMD_RC, "RSTESP", },
  96. { ESP_CMD_RS, "RSTSCSI", },
  97. /* Disconnected State Group */
  98. { ESP_CMD_RSEL, "RESLCTSEQ", },
  99. { ESP_CMD_SEL, "SLCTNATN", },
  100. { ESP_CMD_SELA, "SLCTATN", },
  101. { ESP_CMD_SELAS, "SLCTATNSTOP", },
  102. { ESP_CMD_ESEL, "ENSLCTRESEL", },
  103. { ESP_CMD_DSEL, "DISSELRESEL", },
  104. { ESP_CMD_SA3, "SLCTATN3", },
  105. { ESP_CMD_RSEL3, "RESLCTSEQ", },
  106. /* Target State Group */
  107. { ESP_CMD_SMSG, "SNDMSG", },
  108. { ESP_CMD_SSTAT, "SNDSTATUS", },
  109. { ESP_CMD_SDATA, "SNDDATA", },
  110. { ESP_CMD_DSEQ, "DISCSEQ", },
  111. { ESP_CMD_TSEQ, "TERMSEQ", },
  112. { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
  113. { ESP_CMD_DCNCT, "DISC", },
  114. { ESP_CMD_RMSG, "RCVMSG", },
  115. { ESP_CMD_RCMD, "RCVCMD", },
  116. { ESP_CMD_RDATA, "RCVDATA", },
  117. { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
  118. /* Initiator State Group */
  119. { ESP_CMD_TI, "TRANSINFO", },
  120. { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
  121. { ESP_CMD_MOK, "MSGACCEPTED", },
  122. { ESP_CMD_TPAD, "TPAD", },
  123. { ESP_CMD_SATN, "SATN", },
  124. { ESP_CMD_RATN, "RATN", },
  125. };
  126. #define NUM_ESP_COMMANDS ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
  127. /* Print textual representation of an ESP command */
  128. static inline void esp_print_cmd(unchar espcmd)
  129. {
  130. unchar dma_bit = espcmd & ESP_CMD_DMA;
  131. int i;
  132. espcmd &= ~dma_bit;
  133. for(i=0; i<NUM_ESP_COMMANDS; i++)
  134. if(esp_cmd_strings[i].cmdchar == espcmd)
  135. break;
  136. if(i==NUM_ESP_COMMANDS)
  137. printk("ESP_Unknown");
  138. else
  139. printk("%s%s", esp_cmd_strings[i].text,
  140. ((dma_bit) ? "+DMA" : ""));
  141. }
  142. /* Print the status register's value */
  143. static inline void esp_print_statreg(unchar statreg)
  144. {
  145. unchar phase;
  146. printk("STATUS<");
  147. phase = statreg & ESP_STAT_PMASK;
  148. printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
  149. (phase == ESP_DIP ? "DATA-IN" :
  150. (phase == ESP_CMDP ? "COMMAND" :
  151. (phase == ESP_STATP ? "STATUS" :
  152. (phase == ESP_MOP ? "MSG-OUT" :
  153. (phase == ESP_MIP ? "MSG_IN" :
  154. "unknown")))))));
  155. if(statreg & ESP_STAT_TDONE)
  156. printk("TRANS_DONE,");
  157. if(statreg & ESP_STAT_TCNT)
  158. printk("TCOUNT_ZERO,");
  159. if(statreg & ESP_STAT_PERR)
  160. printk("P_ERROR,");
  161. if(statreg & ESP_STAT_SPAM)
  162. printk("SPAM,");
  163. if(statreg & ESP_STAT_INTR)
  164. printk("IRQ,");
  165. printk(">");
  166. }
  167. /* Print the interrupt register's value */
  168. static inline void esp_print_ireg(unchar intreg)
  169. {
  170. printk("INTREG< ");
  171. if(intreg & ESP_INTR_S)
  172. printk("SLCT_NATN ");
  173. if(intreg & ESP_INTR_SATN)
  174. printk("SLCT_ATN ");
  175. if(intreg & ESP_INTR_RSEL)
  176. printk("RSLCT ");
  177. if(intreg & ESP_INTR_FDONE)
  178. printk("FDONE ");
  179. if(intreg & ESP_INTR_BSERV)
  180. printk("BSERV ");
  181. if(intreg & ESP_INTR_DC)
  182. printk("DISCNCT ");
  183. if(intreg & ESP_INTR_IC)
  184. printk("ILL_CMD ");
  185. if(intreg & ESP_INTR_SR)
  186. printk("SCSI_BUS_RESET ");
  187. printk(">");
  188. }
  189. /* Print the sequence step registers contents */
  190. static inline void esp_print_seqreg(unchar stepreg)
  191. {
  192. stepreg &= ESP_STEP_VBITS;
  193. printk("STEP<%s>",
  194. (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
  195. (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
  196. (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
  197. (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
  198. (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
  199. "UNKNOWN"))))));
  200. }
  201. static char *phase_string(int phase)
  202. {
  203. switch(phase) {
  204. case not_issued:
  205. return "UNISSUED";
  206. case in_slct_norm:
  207. return "SLCTNORM";
  208. case in_slct_stop:
  209. return "SLCTSTOP";
  210. case in_slct_msg:
  211. return "SLCTMSG";
  212. case in_slct_tag:
  213. return "SLCTTAG";
  214. case in_slct_sneg:
  215. return "SLCTSNEG";
  216. case in_datain:
  217. return "DATAIN";
  218. case in_dataout:
  219. return "DATAOUT";
  220. case in_data_done:
  221. return "DATADONE";
  222. case in_msgin:
  223. return "MSGIN";
  224. case in_msgincont:
  225. return "MSGINCONT";
  226. case in_msgindone:
  227. return "MSGINDONE";
  228. case in_msgout:
  229. return "MSGOUT";
  230. case in_msgoutdone:
  231. return "MSGOUTDONE";
  232. case in_cmdbegin:
  233. return "CMDBEGIN";
  234. case in_cmdend:
  235. return "CMDEND";
  236. case in_status:
  237. return "STATUS";
  238. case in_freeing:
  239. return "FREEING";
  240. case in_the_dark:
  241. return "CLUELESS";
  242. case in_abortone:
  243. return "ABORTONE";
  244. case in_abortall:
  245. return "ABORTALL";
  246. case in_resetdev:
  247. return "RESETDEV";
  248. case in_resetbus:
  249. return "RESETBUS";
  250. case in_tgterror:
  251. return "TGTERROR";
  252. default:
  253. return "UNKNOWN";
  254. };
  255. }
  256. #ifdef DEBUG_STATE_MACHINE
  257. static inline void esp_advance_phase(Scsi_Cmnd *s, int newphase)
  258. {
  259. ESPLOG(("<%s>", phase_string(newphase)));
  260. s->SCp.sent_command = s->SCp.phase;
  261. s->SCp.phase = newphase;
  262. }
  263. #else
  264. #define esp_advance_phase(__s, __newphase) \
  265. (__s)->SCp.sent_command = (__s)->SCp.phase; \
  266. (__s)->SCp.phase = (__newphase);
  267. #endif
  268. #ifdef DEBUG_ESP_CMDS
  269. static inline void esp_cmd(struct NCR_ESP *esp, struct ESP_regs *eregs,
  270. unchar cmd)
  271. {
  272. esp->espcmdlog[esp->espcmdent] = cmd;
  273. esp->espcmdent = (esp->espcmdent + 1) & 31;
  274. esp_write(eregs->esp_cmnd, cmd);
  275. }
  276. #else
  277. #define esp_cmd(__esp, __eregs, __cmd) esp_write((__eregs)->esp_cmnd, (__cmd))
  278. #endif
  279. /* How we use the various Linux SCSI data structures for operation.
  280. *
  281. * struct scsi_cmnd:
  282. *
  283. * We keep track of the syncronous capabilities of a target
  284. * in the device member, using sync_min_period and
  285. * sync_max_offset. These are the values we directly write
  286. * into the ESP registers while running a command. If offset
  287. * is zero the ESP will use asynchronous transfers.
  288. * If the borken flag is set we assume we shouldn't even bother
  289. * trying to negotiate for synchronous transfer as this target
  290. * is really stupid. If we notice the target is dropping the
  291. * bus, and we have been allowing it to disconnect, we clear
  292. * the disconnect flag.
  293. */
  294. /* Manipulation of the ESP command queues. Thanks to the aha152x driver
  295. * and its author, Juergen E. Fischer, for the methods used here.
  296. * Note that these are per-ESP queues, not global queues like
  297. * the aha152x driver uses.
  298. */
  299. static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
  300. {
  301. Scsi_Cmnd *end;
  302. new_SC->host_scribble = (unsigned char *) NULL;
  303. if(!*SC)
  304. *SC = new_SC;
  305. else {
  306. for(end=*SC;end->host_scribble;end=(Scsi_Cmnd *)end->host_scribble)
  307. ;
  308. end->host_scribble = (unsigned char *) new_SC;
  309. }
  310. }
  311. static inline void prepend_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
  312. {
  313. new_SC->host_scribble = (unsigned char *) *SC;
  314. *SC = new_SC;
  315. }
  316. static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd **SC)
  317. {
  318. Scsi_Cmnd *ptr;
  319. ptr = *SC;
  320. if(ptr)
  321. *SC = (Scsi_Cmnd *) (*SC)->host_scribble;
  322. return ptr;
  323. }
  324. static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, int target, int lun)
  325. {
  326. Scsi_Cmnd *ptr, *prev;
  327. for(ptr = *SC, prev = NULL;
  328. ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
  329. prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble)
  330. ;
  331. if(ptr) {
  332. if(prev)
  333. prev->host_scribble=ptr->host_scribble;
  334. else
  335. *SC=(Scsi_Cmnd *)ptr->host_scribble;
  336. }
  337. return ptr;
  338. }
  339. /* Resetting various pieces of the ESP scsi driver chipset */
  340. /* Reset the ESP chip, _not_ the SCSI bus. */
  341. static void esp_reset_esp(struct NCR_ESP *esp, struct ESP_regs *eregs)
  342. {
  343. int family_code, version, i;
  344. volatile int trash;
  345. /* Now reset the ESP chip */
  346. esp_cmd(esp, eregs, ESP_CMD_RC);
  347. esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
  348. if(esp->erev == fast)
  349. esp_write(eregs->esp_cfg2, ESP_CONFIG2_FENAB);
  350. esp_cmd(esp, eregs, ESP_CMD_NULL | ESP_CMD_DMA);
  351. /* This is the only point at which it is reliable to read
  352. * the ID-code for a fast ESP chip variant.
  353. */
  354. esp->max_period = ((35 * esp->ccycle) / 1000);
  355. if(esp->erev == fast) {
  356. char *erev2string[] = {
  357. "Emulex FAS236",
  358. "Emulex FPESP100A",
  359. "fast",
  360. "QLogic FAS366",
  361. "Emulex FAS216",
  362. "Symbios Logic 53CF9x-2",
  363. "unknown!"
  364. };
  365. version = esp_read(eregs->esp_uid);
  366. family_code = (version & 0xf8) >> 3;
  367. if(family_code == 0x02) {
  368. if ((version & 7) == 2)
  369. esp->erev = fas216;
  370. else
  371. esp->erev = fas236;
  372. } else if(family_code == 0x0a)
  373. esp->erev = fas366; /* Version is usually '5'. */
  374. else if(family_code == 0x00) {
  375. if ((version & 7) == 2)
  376. esp->erev = fas100a; /* NCR53C9X */
  377. else
  378. esp->erev = espunknown;
  379. } else if(family_code == 0x14) {
  380. if ((version & 7) == 2)
  381. esp->erev = fsc;
  382. else
  383. esp->erev = espunknown;
  384. } else if(family_code == 0x00) {
  385. if ((version & 7) == 2)
  386. esp->erev = fas100a; /* NCR53C9X */
  387. else
  388. esp->erev = espunknown;
  389. } else
  390. esp->erev = espunknown;
  391. ESPLOG(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
  392. esp->esp_id, erev2string[esp->erev - fas236],
  393. family_code, (version & 7)));
  394. esp->min_period = ((4 * esp->ccycle) / 1000);
  395. } else {
  396. esp->min_period = ((5 * esp->ccycle) / 1000);
  397. }
  398. /* Reload the configuration registers */
  399. esp_write(eregs->esp_cfact, esp->cfact);
  400. esp->prev_stp = 0;
  401. esp_write(eregs->esp_stp, 0);
  402. esp->prev_soff = 0;
  403. esp_write(eregs->esp_soff, 0);
  404. esp_write(eregs->esp_timeo, esp->neg_defp);
  405. esp->max_period = (esp->max_period + 3)>>2;
  406. esp->min_period = (esp->min_period + 3)>>2;
  407. esp_write(eregs->esp_cfg1, esp->config1);
  408. switch(esp->erev) {
  409. case esp100:
  410. /* nothing to do */
  411. break;
  412. case esp100a:
  413. esp_write(eregs->esp_cfg2, esp->config2);
  414. break;
  415. case esp236:
  416. /* Slow 236 */
  417. esp_write(eregs->esp_cfg2, esp->config2);
  418. esp->prev_cfg3 = esp->config3[0];
  419. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  420. break;
  421. case fas366:
  422. panic("esp: FAS366 support not present, please notify "
  423. "jongk@cs.utwente.nl");
  424. break;
  425. case fas216:
  426. case fas236:
  427. case fsc:
  428. /* Fast ESP variants */
  429. esp_write(eregs->esp_cfg2, esp->config2);
  430. for(i=0; i<8; i++)
  431. esp->config3[i] |= ESP_CONFIG3_FCLK;
  432. esp->prev_cfg3 = esp->config3[0];
  433. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  434. if(esp->diff)
  435. esp->radelay = 0;
  436. else
  437. esp->radelay = 16;
  438. /* Different timeout constant for these chips */
  439. esp->neg_defp =
  440. FSC_NEG_DEFP(esp->cfreq,
  441. (esp->cfact == ESP_CCF_F0 ?
  442. ESP_CCF_F7 + 1 : esp->cfact));
  443. esp_write(eregs->esp_timeo, esp->neg_defp);
  444. /* Enable Active Negotiation if possible */
  445. if((esp->erev == fsc) && !esp->diff)
  446. esp_write(eregs->esp_cfg4, ESP_CONFIG4_EAN);
  447. break;
  448. case fas100a:
  449. /* Fast 100a */
  450. esp_write(eregs->esp_cfg2, esp->config2);
  451. for(i=0; i<8; i++)
  452. esp->config3[i] |= ESP_CONFIG3_FCLOCK;
  453. esp->prev_cfg3 = esp->config3[0];
  454. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  455. esp->radelay = 32;
  456. break;
  457. default:
  458. panic("esp: what could it be... I wonder...");
  459. break;
  460. };
  461. /* Eat any bitrot in the chip */
  462. trash = esp_read(eregs->esp_intrpt);
  463. udelay(100);
  464. }
  465. /* This places the ESP into a known state at boot time. */
  466. void esp_bootup_reset(struct NCR_ESP *esp, struct ESP_regs *eregs)
  467. {
  468. volatile unchar trash;
  469. /* Reset the DMA */
  470. if(esp->dma_reset)
  471. esp->dma_reset(esp);
  472. /* Reset the ESP */
  473. esp_reset_esp(esp, eregs);
  474. /* Reset the SCSI bus, but tell ESP not to generate an irq */
  475. esp_write(eregs->esp_cfg1, (esp_read(eregs->esp_cfg1) | ESP_CONFIG1_SRRDISAB));
  476. esp_cmd(esp, eregs, ESP_CMD_RS);
  477. udelay(400);
  478. esp_write(eregs->esp_cfg1, esp->config1);
  479. /* Eat any bitrot in the chip and we are done... */
  480. trash = esp_read(eregs->esp_intrpt);
  481. }
  482. /* Allocate structure and insert basic data such as SCSI chip frequency
  483. * data and a pointer to the device
  484. */
  485. struct NCR_ESP* esp_allocate(struct scsi_host_template *tpnt, void *esp_dev)
  486. {
  487. struct NCR_ESP *esp, *elink;
  488. struct Scsi_Host *esp_host;
  489. esp_host = scsi_register(tpnt, sizeof(struct NCR_ESP));
  490. if(!esp_host)
  491. panic("Cannot register ESP SCSI host");
  492. esp = (struct NCR_ESP *) esp_host->hostdata;
  493. if(!esp)
  494. panic("No esp in hostdata");
  495. esp->ehost = esp_host;
  496. esp->edev = esp_dev;
  497. esp->esp_id = nesps++;
  498. /* Set bitshift value (only used on Amiga with multiple ESPs) */
  499. esp->shift = 2;
  500. /* Put into the chain of esp chips detected */
  501. if(espchain) {
  502. elink = espchain;
  503. while(elink->next) elink = elink->next;
  504. elink->next = esp;
  505. } else {
  506. espchain = esp;
  507. }
  508. esp->next = NULL;
  509. return esp;
  510. }
  511. void esp_deallocate(struct NCR_ESP *esp)
  512. {
  513. struct NCR_ESP *elink;
  514. if(espchain == esp) {
  515. espchain = NULL;
  516. } else {
  517. for(elink = espchain; elink && (elink->next != esp); elink = elink->next);
  518. if(elink)
  519. elink->next = esp->next;
  520. }
  521. nesps--;
  522. }
  523. /* Complete initialization of ESP structure and device
  524. * Caller must have initialized appropriate parts of the ESP structure
  525. * between the call to esp_allocate and this function.
  526. */
  527. void esp_initialize(struct NCR_ESP *esp)
  528. {
  529. struct ESP_regs *eregs = esp->eregs;
  530. unsigned int fmhz;
  531. unchar ccf;
  532. int i;
  533. /* Check out the clock properties of the chip. */
  534. /* This is getting messy but it has to be done
  535. * correctly or else you get weird behavior all
  536. * over the place. We are trying to basically
  537. * figure out three pieces of information.
  538. *
  539. * a) Clock Conversion Factor
  540. *
  541. * This is a representation of the input
  542. * crystal clock frequency going into the
  543. * ESP on this machine. Any operation whose
  544. * timing is longer than 400ns depends on this
  545. * value being correct. For example, you'll
  546. * get blips for arbitration/selection during
  547. * high load or with multiple targets if this
  548. * is not set correctly.
  549. *
  550. * b) Selection Time-Out
  551. *
  552. * The ESP isn't very bright and will arbitrate
  553. * for the bus and try to select a target
  554. * forever if you let it. This value tells
  555. * the ESP when it has taken too long to
  556. * negotiate and that it should interrupt
  557. * the CPU so we can see what happened.
  558. * The value is computed as follows (from
  559. * NCR/Symbios chip docs).
  560. *
  561. * (Time Out Period) * (Input Clock)
  562. * STO = ----------------------------------
  563. * (8192) * (Clock Conversion Factor)
  564. *
  565. * You usually want the time out period to be
  566. * around 250ms, I think we'll set it a little
  567. * bit higher to account for fully loaded SCSI
  568. * bus's and slow devices that don't respond so
  569. * quickly to selection attempts. (yeah, I know
  570. * this is out of spec. but there is a lot of
  571. * buggy pieces of firmware out there so bite me)
  572. *
  573. * c) Imperical constants for synchronous offset
  574. * and transfer period register values
  575. *
  576. * This entails the smallest and largest sync
  577. * period we could ever handle on this ESP.
  578. */
  579. fmhz = esp->cfreq;
  580. if(fmhz <= (5000000))
  581. ccf = 0;
  582. else
  583. ccf = (((5000000 - 1) + (fmhz))/(5000000));
  584. if(!ccf || ccf > 8) {
  585. /* If we can't find anything reasonable,
  586. * just assume 20MHZ. This is the clock
  587. * frequency of the older sun4c's where I've
  588. * been unable to find the clock-frequency
  589. * PROM property. All other machines provide
  590. * useful values it seems.
  591. */
  592. ccf = ESP_CCF_F4;
  593. fmhz = (20000000);
  594. }
  595. if(ccf==(ESP_CCF_F7+1))
  596. esp->cfact = ESP_CCF_F0;
  597. else if(ccf == ESP_CCF_NEVER)
  598. esp->cfact = ESP_CCF_F2;
  599. else
  600. esp->cfact = ccf;
  601. esp->cfreq = fmhz;
  602. esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
  603. esp->ctick = ESP_TICK(ccf, esp->ccycle);
  604. esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
  605. esp->sync_defp = SYNC_DEFP_SLOW;
  606. printk("SCSI ID %d Clk %dMHz CCF=%d TOut %d ",
  607. esp->scsi_id, (esp->cfreq / 1000000),
  608. ccf, (int) esp->neg_defp);
  609. /* Fill in ehost data */
  610. esp->ehost->base = (unsigned long)eregs;
  611. esp->ehost->this_id = esp->scsi_id;
  612. esp->ehost->irq = esp->irq;
  613. /* SCSI id mask */
  614. esp->scsi_id_mask = (1 << esp->scsi_id);
  615. /* Probe the revision of this esp */
  616. esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
  617. esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
  618. esp_write(eregs->esp_cfg2, esp->config2);
  619. if((esp_read(eregs->esp_cfg2) & ~(ESP_CONFIG2_MAGIC)) !=
  620. (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
  621. printk("NCR53C90(esp100)\n");
  622. esp->erev = esp100;
  623. } else {
  624. esp->config2 = 0;
  625. esp_write(eregs->esp_cfg2, 0);
  626. esp_write(eregs->esp_cfg3, 5);
  627. if(esp_read(eregs->esp_cfg3) != 5) {
  628. printk("NCR53C90A(esp100a)\n");
  629. esp->erev = esp100a;
  630. } else {
  631. int target;
  632. for(target=0; target<8; target++)
  633. esp->config3[target] = 0;
  634. esp->prev_cfg3 = 0;
  635. esp_write(eregs->esp_cfg3, 0);
  636. if(ccf > ESP_CCF_F5) {
  637. printk("NCR53C9XF(espfast)\n");
  638. esp->erev = fast;
  639. esp->sync_defp = SYNC_DEFP_FAST;
  640. } else {
  641. printk("NCR53C9x(esp236)\n");
  642. esp->erev = esp236;
  643. }
  644. }
  645. }
  646. /* Initialize the command queues */
  647. esp->current_SC = NULL;
  648. esp->disconnected_SC = NULL;
  649. esp->issue_SC = NULL;
  650. /* Clear the state machines. */
  651. esp->targets_present = 0;
  652. esp->resetting_bus = 0;
  653. esp->snip = 0;
  654. init_waitqueue_head(&esp->reset_queue);
  655. esp->fas_premature_intr_workaround = 0;
  656. for(i = 0; i < 32; i++)
  657. esp->espcmdlog[i] = 0;
  658. esp->espcmdent = 0;
  659. for(i = 0; i < 16; i++) {
  660. esp->cur_msgout[i] = 0;
  661. esp->cur_msgin[i] = 0;
  662. }
  663. esp->prevmsgout = esp->prevmsgin = 0;
  664. esp->msgout_len = esp->msgin_len = 0;
  665. /* Clear the one behind caches to hold unmatchable values. */
  666. esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
  667. /* Reset the thing before we try anything... */
  668. esp_bootup_reset(esp, eregs);
  669. esps_in_use++;
  670. }
  671. /* The info function will return whatever useful
  672. * information the developer sees fit. If not provided, then
  673. * the name field will be used instead.
  674. */
  675. const char *esp_info(struct Scsi_Host *host)
  676. {
  677. struct NCR_ESP *esp;
  678. esp = (struct NCR_ESP *) host->hostdata;
  679. switch(esp->erev) {
  680. case esp100:
  681. return "ESP100 (NCR53C90)";
  682. case esp100a:
  683. return "ESP100A (NCR53C90A)";
  684. case esp236:
  685. return "ESP236 (NCR53C9x)";
  686. case fas216:
  687. return "Emulex FAS216";
  688. case fas236:
  689. return "Emulex FAS236";
  690. case fas366:
  691. return "QLogic FAS366";
  692. case fas100a:
  693. return "FPESP100A";
  694. case fsc:
  695. return "Symbios Logic 53CF9x-2";
  696. default:
  697. panic("Bogon ESP revision");
  698. };
  699. }
  700. /* From Wolfgang Stanglmeier's NCR scsi driver. */
  701. struct info_str
  702. {
  703. char *buffer;
  704. int length;
  705. int offset;
  706. int pos;
  707. };
  708. static void copy_mem_info(struct info_str *info, char *data, int len)
  709. {
  710. if (info->pos + len > info->length)
  711. len = info->length - info->pos;
  712. if (info->pos + len < info->offset) {
  713. info->pos += len;
  714. return;
  715. }
  716. if (info->pos < info->offset) {
  717. data += (info->offset - info->pos);
  718. len -= (info->offset - info->pos);
  719. }
  720. if (len > 0) {
  721. memcpy(info->buffer + info->pos, data, len);
  722. info->pos += len;
  723. }
  724. }
  725. static int copy_info(struct info_str *info, char *fmt, ...)
  726. {
  727. va_list args;
  728. char buf[81];
  729. int len;
  730. va_start(args, fmt);
  731. len = vsprintf(buf, fmt, args);
  732. va_end(args);
  733. copy_mem_info(info, buf, len);
  734. return len;
  735. }
  736. static int esp_host_info(struct NCR_ESP *esp, char *ptr, off_t offset, int len)
  737. {
  738. struct scsi_device *sdev;
  739. struct info_str info;
  740. int i;
  741. info.buffer = ptr;
  742. info.length = len;
  743. info.offset = offset;
  744. info.pos = 0;
  745. copy_info(&info, "ESP Host Adapter:\n");
  746. copy_info(&info, "\tESP Model\t\t");
  747. switch(esp->erev) {
  748. case esp100:
  749. copy_info(&info, "ESP100 (NCR53C90)\n");
  750. break;
  751. case esp100a:
  752. copy_info(&info, "ESP100A (NCR53C90A)\n");
  753. break;
  754. case esp236:
  755. copy_info(&info, "ESP236 (NCR53C9x)\n");
  756. break;
  757. case fas216:
  758. copy_info(&info, "Emulex FAS216\n");
  759. break;
  760. case fas236:
  761. copy_info(&info, "Emulex FAS236\n");
  762. break;
  763. case fas100a:
  764. copy_info(&info, "FPESP100A\n");
  765. break;
  766. case fast:
  767. copy_info(&info, "Generic FAST\n");
  768. break;
  769. case fas366:
  770. copy_info(&info, "QLogic FAS366\n");
  771. break;
  772. case fsc:
  773. copy_info(&info, "Symbios Logic 53C9x-2\n");
  774. break;
  775. case espunknown:
  776. default:
  777. copy_info(&info, "Unknown!\n");
  778. break;
  779. };
  780. copy_info(&info, "\tLive Targets\t\t[ ");
  781. for(i = 0; i < 15; i++) {
  782. if(esp->targets_present & (1 << i))
  783. copy_info(&info, "%d ", i);
  784. }
  785. copy_info(&info, "]\n\n");
  786. /* Now describe the state of each existing target. */
  787. copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\n");
  788. shost_for_each_device(sdev, esp->ehost) {
  789. struct esp_device *esp_dev = sdev->hostdata;
  790. uint id = sdev->id;
  791. if (!(esp->targets_present & (1 << id)))
  792. continue;
  793. copy_info(&info, "%d\t\t", id);
  794. copy_info(&info, "%08lx\t", esp->config3[id]);
  795. copy_info(&info, "[%02lx,%02lx]\t\t\t",
  796. esp_dev->sync_max_offset,
  797. esp_dev->sync_min_period);
  798. copy_info(&info, "%s\n", esp_dev->disconnect ? "yes" : "no");
  799. }
  800. return info.pos > info.offset? info.pos - info.offset : 0;
  801. }
  802. /* ESP proc filesystem code. */
  803. int esp_proc_info(struct Scsi_Host *shost, char *buffer, char **start, off_t offset, int length,
  804. int inout)
  805. {
  806. struct NCR_ESP *esp = (struct NCR_ESP *)shost->hostdata;
  807. if(inout)
  808. return -EINVAL; /* not yet */
  809. if(start)
  810. *start = buffer;
  811. return esp_host_info(esp, buffer, offset, length);
  812. }
  813. static void esp_get_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  814. {
  815. if(sp->use_sg == 0) {
  816. sp->SCp.this_residual = sp->request_bufflen;
  817. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  818. sp->SCp.buffers_residual = 0;
  819. if (esp->dma_mmu_get_scsi_one)
  820. esp->dma_mmu_get_scsi_one(esp, sp);
  821. else
  822. sp->SCp.ptr =
  823. (char *) virt_to_phys(sp->request_buffer);
  824. } else {
  825. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  826. sp->SCp.buffers_residual = sp->use_sg - 1;
  827. sp->SCp.this_residual = sp->SCp.buffer->length;
  828. if (esp->dma_mmu_get_scsi_sgl)
  829. esp->dma_mmu_get_scsi_sgl(esp, sp);
  830. else
  831. sp->SCp.ptr =
  832. (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
  833. }
  834. }
  835. static void esp_release_dmabufs(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  836. {
  837. if(sp->use_sg == 0) {
  838. if (esp->dma_mmu_release_scsi_one)
  839. esp->dma_mmu_release_scsi_one(esp, sp);
  840. } else {
  841. if (esp->dma_mmu_release_scsi_sgl)
  842. esp->dma_mmu_release_scsi_sgl(esp, sp);
  843. }
  844. }
  845. static void esp_restore_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  846. {
  847. struct esp_pointers *ep = &esp->data_pointers[scmd_id(sp)];
  848. sp->SCp.ptr = ep->saved_ptr;
  849. sp->SCp.buffer = ep->saved_buffer;
  850. sp->SCp.this_residual = ep->saved_this_residual;
  851. sp->SCp.buffers_residual = ep->saved_buffers_residual;
  852. }
  853. static void esp_save_pointers(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  854. {
  855. struct esp_pointers *ep = &esp->data_pointers[scmd_id(sp)];
  856. ep->saved_ptr = sp->SCp.ptr;
  857. ep->saved_buffer = sp->SCp.buffer;
  858. ep->saved_this_residual = sp->SCp.this_residual;
  859. ep->saved_buffers_residual = sp->SCp.buffers_residual;
  860. }
  861. /* Some rules:
  862. *
  863. * 1) Never ever panic while something is live on the bus.
  864. * If there is to be any chance of syncing the disks this
  865. * rule is to be obeyed.
  866. *
  867. * 2) Any target that causes a foul condition will no longer
  868. * have synchronous transfers done to it, no questions
  869. * asked.
  870. *
  871. * 3) Keep register accesses to a minimum. Think about some
  872. * day when we have Xbus machines this is running on and
  873. * the ESP chip is on the other end of the machine on a
  874. * different board from the cpu where this is running.
  875. */
  876. /* Fire off a command. We assume the bus is free and that the only
  877. * case where we could see an interrupt is where we have disconnected
  878. * commands active and they are trying to reselect us.
  879. */
  880. static inline void esp_check_cmd(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  881. {
  882. switch(sp->cmd_len) {
  883. case 6:
  884. case 10:
  885. case 12:
  886. esp->esp_slowcmd = 0;
  887. break;
  888. default:
  889. esp->esp_slowcmd = 1;
  890. esp->esp_scmdleft = sp->cmd_len;
  891. esp->esp_scmdp = &sp->cmnd[0];
  892. break;
  893. };
  894. }
  895. static inline void build_sync_nego_msg(struct NCR_ESP *esp, int period, int offset)
  896. {
  897. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  898. esp->cur_msgout[1] = 3;
  899. esp->cur_msgout[2] = EXTENDED_SDTR;
  900. esp->cur_msgout[3] = period;
  901. esp->cur_msgout[4] = offset;
  902. esp->msgout_len = 5;
  903. }
  904. static void esp_exec_cmd(struct NCR_ESP *esp)
  905. {
  906. struct ESP_regs *eregs = esp->eregs;
  907. struct esp_device *esp_dev;
  908. Scsi_Cmnd *SCptr;
  909. struct scsi_device *SDptr;
  910. volatile unchar *cmdp = esp->esp_command;
  911. unsigned char the_esp_command;
  912. int lun, target;
  913. int i;
  914. /* Hold off if we have disconnected commands and
  915. * an IRQ is showing...
  916. */
  917. if(esp->disconnected_SC && esp->dma_irq_p(esp))
  918. return;
  919. /* Grab first member of the issue queue. */
  920. SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
  921. /* Safe to panic here because current_SC is null. */
  922. if(!SCptr)
  923. panic("esp: esp_exec_cmd and issue queue is NULL");
  924. SDptr = SCptr->device;
  925. esp_dev = SDptr->hostdata;
  926. lun = SCptr->device->lun;
  927. target = SCptr->device->id;
  928. esp->snip = 0;
  929. esp->msgout_len = 0;
  930. /* Send it out whole, or piece by piece? The ESP
  931. * only knows how to automatically send out 6, 10,
  932. * and 12 byte commands. I used to think that the
  933. * Linux SCSI code would never throw anything other
  934. * than that to us, but then again there is the
  935. * SCSI generic driver which can send us anything.
  936. */
  937. esp_check_cmd(esp, SCptr);
  938. /* If arbitration/selection is successful, the ESP will leave
  939. * ATN asserted, causing the target to go into message out
  940. * phase. The ESP will feed the target the identify and then
  941. * the target can only legally go to one of command,
  942. * datain/out, status, or message in phase, or stay in message
  943. * out phase (should we be trying to send a sync negotiation
  944. * message after the identify). It is not allowed to drop
  945. * BSY, but some buggy targets do and we check for this
  946. * condition in the selection complete code. Most of the time
  947. * we'll make the command bytes available to the ESP and it
  948. * will not interrupt us until it finishes command phase, we
  949. * cannot do this for command sizes the ESP does not
  950. * understand and in this case we'll get interrupted right
  951. * when the target goes into command phase.
  952. *
  953. * It is absolutely _illegal_ in the presence of SCSI-2 devices
  954. * to use the ESP select w/o ATN command. When SCSI-2 devices are
  955. * present on the bus we _must_ always go straight to message out
  956. * phase with an identify message for the target. Being that
  957. * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
  958. * selections should not confuse SCSI-1 we hope.
  959. */
  960. if(esp_dev->sync) {
  961. /* this targets sync is known */
  962. #ifdef CONFIG_SCSI_MAC_ESP
  963. do_sync_known:
  964. #endif
  965. if(esp_dev->disconnect)
  966. *cmdp++ = IDENTIFY(1, lun);
  967. else
  968. *cmdp++ = IDENTIFY(0, lun);
  969. if(esp->esp_slowcmd) {
  970. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  971. esp_advance_phase(SCptr, in_slct_stop);
  972. } else {
  973. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  974. esp_advance_phase(SCptr, in_slct_norm);
  975. }
  976. } else if(!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
  977. /* After the bootup SCSI code sends both the
  978. * TEST_UNIT_READY and INQUIRY commands we want
  979. * to at least attempt allowing the device to
  980. * disconnect.
  981. */
  982. ESPMISC(("esp: Selecting device for first time. target=%d "
  983. "lun=%d\n", target, SCptr->device->lun));
  984. if(!SDptr->borken && !esp_dev->disconnect)
  985. esp_dev->disconnect = 1;
  986. *cmdp++ = IDENTIFY(0, lun);
  987. esp->prevmsgout = NOP;
  988. esp_advance_phase(SCptr, in_slct_norm);
  989. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  990. /* Take no chances... */
  991. esp_dev->sync_max_offset = 0;
  992. esp_dev->sync_min_period = 0;
  993. } else {
  994. int toshiba_cdrom_hwbug_wkaround = 0;
  995. #ifdef CONFIG_SCSI_MAC_ESP
  996. /* Never allow synchronous transfers (disconnect OK) on
  997. * Macintosh. Well, maybe later when we figured out how to
  998. * do DMA on the machines that support it ...
  999. */
  1000. esp_dev->disconnect = 1;
  1001. esp_dev->sync_max_offset = 0;
  1002. esp_dev->sync_min_period = 0;
  1003. esp_dev->sync = 1;
  1004. esp->snip = 0;
  1005. goto do_sync_known;
  1006. #endif
  1007. /* We've talked to this guy before,
  1008. * but never negotiated. Let's try
  1009. * sync negotiation.
  1010. */
  1011. if(!SDptr->borken) {
  1012. if((SDptr->type == TYPE_ROM) &&
  1013. (!strncmp(SDptr->vendor, "TOSHIBA", 7))) {
  1014. /* Nice try sucker... */
  1015. ESPMISC(("esp%d: Disabling sync for buggy "
  1016. "Toshiba CDROM.\n", esp->esp_id));
  1017. toshiba_cdrom_hwbug_wkaround = 1;
  1018. build_sync_nego_msg(esp, 0, 0);
  1019. } else {
  1020. build_sync_nego_msg(esp, esp->sync_defp, 15);
  1021. }
  1022. } else {
  1023. build_sync_nego_msg(esp, 0, 0);
  1024. }
  1025. esp_dev->sync = 1;
  1026. esp->snip = 1;
  1027. /* A fix for broken SCSI1 targets, when they disconnect
  1028. * they lock up the bus and confuse ESP. So disallow
  1029. * disconnects for SCSI1 targets for now until we
  1030. * find a better fix.
  1031. *
  1032. * Addendum: This is funny, I figured out what was going
  1033. * on. The blotzed SCSI1 target would disconnect,
  1034. * one of the other SCSI2 targets or both would be
  1035. * disconnected as well. The SCSI1 target would
  1036. * stay disconnected long enough that we start
  1037. * up a command on one of the SCSI2 targets. As
  1038. * the ESP is arbitrating for the bus the SCSI1
  1039. * target begins to arbitrate as well to reselect
  1040. * the ESP. The SCSI1 target refuses to drop it's
  1041. * ID bit on the data bus even though the ESP is
  1042. * at ID 7 and is the obvious winner for any
  1043. * arbitration. The ESP is a poor sport and refuses
  1044. * to lose arbitration, it will continue indefinitely
  1045. * trying to arbitrate for the bus and can only be
  1046. * stopped via a chip reset or SCSI bus reset.
  1047. * Therefore _no_ disconnects for SCSI1 targets
  1048. * thank you very much. ;-)
  1049. */
  1050. if(((SDptr->scsi_level < 3) && (SDptr->type != TYPE_TAPE)) ||
  1051. toshiba_cdrom_hwbug_wkaround || SDptr->borken) {
  1052. ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
  1053. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  1054. esp_dev->disconnect = 0;
  1055. *cmdp++ = IDENTIFY(0, lun);
  1056. } else {
  1057. *cmdp++ = IDENTIFY(1, lun);
  1058. }
  1059. /* ESP fifo is only so big...
  1060. * Make this look like a slow command.
  1061. */
  1062. esp->esp_slowcmd = 1;
  1063. esp->esp_scmdleft = SCptr->cmd_len;
  1064. esp->esp_scmdp = &SCptr->cmnd[0];
  1065. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1066. esp_advance_phase(SCptr, in_slct_msg);
  1067. }
  1068. if(!esp->esp_slowcmd)
  1069. for(i = 0; i < SCptr->cmd_len; i++)
  1070. *cmdp++ = SCptr->cmnd[i];
  1071. esp_write(eregs->esp_busid, (target & 7));
  1072. if (esp->prev_soff != esp_dev->sync_max_offset ||
  1073. esp->prev_stp != esp_dev->sync_min_period ||
  1074. (esp->erev > esp100a &&
  1075. esp->prev_cfg3 != esp->config3[target])) {
  1076. esp->prev_soff = esp_dev->sync_max_offset;
  1077. esp_write(eregs->esp_soff, esp->prev_soff);
  1078. esp->prev_stp = esp_dev->sync_min_period;
  1079. esp_write(eregs->esp_stp, esp->prev_stp);
  1080. if(esp->erev > esp100a) {
  1081. esp->prev_cfg3 = esp->config3[target];
  1082. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  1083. }
  1084. }
  1085. i = (cmdp - esp->esp_command);
  1086. /* Set up the DMA and ESP counters */
  1087. if(esp->do_pio_cmds){
  1088. int j = 0;
  1089. /*
  1090. * XXX MSch:
  1091. *
  1092. * It seems this is required, at least to clean up
  1093. * after failed commands when using PIO mode ...
  1094. */
  1095. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  1096. for(;j<i;j++)
  1097. esp_write(eregs->esp_fdata, esp->esp_command[j]);
  1098. the_esp_command &= ~ESP_CMD_DMA;
  1099. /* Tell ESP to "go". */
  1100. esp_cmd(esp, eregs, the_esp_command);
  1101. } else {
  1102. /* Set up the ESP counters */
  1103. esp_write(eregs->esp_tclow, i);
  1104. esp_write(eregs->esp_tcmed, 0);
  1105. esp->dma_init_write(esp, esp->esp_command_dvma, i);
  1106. /* Tell ESP to "go". */
  1107. esp_cmd(esp, eregs, the_esp_command);
  1108. }
  1109. }
  1110. /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
  1111. int esp_queue(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  1112. {
  1113. struct NCR_ESP *esp;
  1114. /* Set up func ptr and initial driver cmd-phase. */
  1115. SCpnt->scsi_done = done;
  1116. SCpnt->SCp.phase = not_issued;
  1117. esp = (struct NCR_ESP *) SCpnt->device->host->hostdata;
  1118. if(esp->dma_led_on)
  1119. esp->dma_led_on(esp);
  1120. /* We use the scratch area. */
  1121. ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->lun));
  1122. ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->lun));
  1123. esp_get_dmabufs(esp, SCpnt);
  1124. esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
  1125. SCpnt->SCp.Status = CHECK_CONDITION;
  1126. SCpnt->SCp.Message = 0xff;
  1127. SCpnt->SCp.sent_command = 0;
  1128. /* Place into our queue. */
  1129. if(SCpnt->cmnd[0] == REQUEST_SENSE) {
  1130. ESPQUEUE(("RQSENSE\n"));
  1131. prepend_SC(&esp->issue_SC, SCpnt);
  1132. } else {
  1133. ESPQUEUE(("\n"));
  1134. append_SC(&esp->issue_SC, SCpnt);
  1135. }
  1136. /* Run it now if we can. */
  1137. if(!esp->current_SC && !esp->resetting_bus)
  1138. esp_exec_cmd(esp);
  1139. return 0;
  1140. }
  1141. /* Dump driver state. */
  1142. static void esp_dump_cmd(Scsi_Cmnd *SCptr)
  1143. {
  1144. ESPLOG(("[tgt<%02x> lun<%02x> "
  1145. "pphase<%s> cphase<%s>]",
  1146. SCptr->device->id, SCptr->device->lun,
  1147. phase_string(SCptr->SCp.sent_command),
  1148. phase_string(SCptr->SCp.phase)));
  1149. }
  1150. static void esp_dump_state(struct NCR_ESP *esp,
  1151. struct ESP_regs *eregs)
  1152. {
  1153. Scsi_Cmnd *SCptr = esp->current_SC;
  1154. #ifdef DEBUG_ESP_CMDS
  1155. int i;
  1156. #endif
  1157. ESPLOG(("esp%d: dumping state\n", esp->esp_id));
  1158. /* Print DMA status */
  1159. esp->dma_dump_state(esp);
  1160. ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1161. esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
  1162. ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1163. esp->esp_id, esp_read(eregs->esp_status), esp_read(eregs->esp_sstep),
  1164. esp_read(eregs->esp_intrpt)));
  1165. #ifdef DEBUG_ESP_CMDS
  1166. printk("esp%d: last ESP cmds [", esp->esp_id);
  1167. i = (esp->espcmdent - 1) & 31;
  1168. printk("<");
  1169. esp_print_cmd(esp->espcmdlog[i]);
  1170. printk(">");
  1171. i = (i - 1) & 31;
  1172. printk("<");
  1173. esp_print_cmd(esp->espcmdlog[i]);
  1174. printk(">");
  1175. i = (i - 1) & 31;
  1176. printk("<");
  1177. esp_print_cmd(esp->espcmdlog[i]);
  1178. printk(">");
  1179. i = (i - 1) & 31;
  1180. printk("<");
  1181. esp_print_cmd(esp->espcmdlog[i]);
  1182. printk(">");
  1183. printk("]\n");
  1184. #endif /* (DEBUG_ESP_CMDS) */
  1185. if(SCptr) {
  1186. ESPLOG(("esp%d: current command ", esp->esp_id));
  1187. esp_dump_cmd(SCptr);
  1188. }
  1189. ESPLOG(("\n"));
  1190. SCptr = esp->disconnected_SC;
  1191. ESPLOG(("esp%d: disconnected ", esp->esp_id));
  1192. while(SCptr) {
  1193. esp_dump_cmd(SCptr);
  1194. SCptr = (Scsi_Cmnd *) SCptr->host_scribble;
  1195. }
  1196. ESPLOG(("\n"));
  1197. }
  1198. /* Abort a command. The host_lock is acquired by caller. */
  1199. int esp_abort(Scsi_Cmnd *SCptr)
  1200. {
  1201. struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
  1202. struct ESP_regs *eregs = esp->eregs;
  1203. int don;
  1204. ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
  1205. esp_dump_state(esp, eregs);
  1206. /* Wheee, if this is the current command on the bus, the
  1207. * best we can do is assert ATN and wait for msgout phase.
  1208. * This should even fix a hung SCSI bus when we lose state
  1209. * in the driver and timeout because the eventual phase change
  1210. * will cause the ESP to (eventually) give an interrupt.
  1211. */
  1212. if(esp->current_SC == SCptr) {
  1213. esp->cur_msgout[0] = ABORT;
  1214. esp->msgout_len = 1;
  1215. esp->msgout_ctr = 0;
  1216. esp_cmd(esp, eregs, ESP_CMD_SATN);
  1217. return SUCCESS;
  1218. }
  1219. /* If it is still in the issue queue then we can safely
  1220. * call the completion routine and report abort success.
  1221. */
  1222. don = esp->dma_ports_p(esp);
  1223. if(don) {
  1224. esp->dma_ints_off(esp);
  1225. synchronize_irq(esp->irq);
  1226. }
  1227. if(esp->issue_SC) {
  1228. Scsi_Cmnd **prev, *this;
  1229. for(prev = (&esp->issue_SC), this = esp->issue_SC;
  1230. this;
  1231. prev = (Scsi_Cmnd **) &(this->host_scribble),
  1232. this = (Scsi_Cmnd *) this->host_scribble) {
  1233. if(this == SCptr) {
  1234. *prev = (Scsi_Cmnd *) this->host_scribble;
  1235. this->host_scribble = NULL;
  1236. esp_release_dmabufs(esp, this);
  1237. this->result = DID_ABORT << 16;
  1238. this->done(this);
  1239. if(don)
  1240. esp->dma_ints_on(esp);
  1241. return SUCCESS;
  1242. }
  1243. }
  1244. }
  1245. /* Yuck, the command to abort is disconnected, it is not
  1246. * worth trying to abort it now if something else is live
  1247. * on the bus at this time. So, we let the SCSI code wait
  1248. * a little bit and try again later.
  1249. */
  1250. if(esp->current_SC) {
  1251. if(don)
  1252. esp->dma_ints_on(esp);
  1253. return FAILED;
  1254. }
  1255. /* It's disconnected, we have to reconnect to re-establish
  1256. * the nexus and tell the device to abort. However, we really
  1257. * cannot 'reconnect' per se. Don't try to be fancy, just
  1258. * indicate failure, which causes our caller to reset the whole
  1259. * bus.
  1260. */
  1261. if(don)
  1262. esp->dma_ints_on(esp);
  1263. return FAILED;
  1264. }
  1265. /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
  1266. * arrived indicating the end of the SCSI bus reset. Our job
  1267. * is to clean out the command queues and begin re-execution
  1268. * of SCSI commands once more.
  1269. */
  1270. static int esp_finish_reset(struct NCR_ESP *esp,
  1271. struct ESP_regs *eregs)
  1272. {
  1273. Scsi_Cmnd *sp = esp->current_SC;
  1274. /* Clean up currently executing command, if any. */
  1275. if (sp != NULL) {
  1276. esp_release_dmabufs(esp, sp);
  1277. sp->result = (DID_RESET << 16);
  1278. sp->scsi_done(sp);
  1279. esp->current_SC = NULL;
  1280. }
  1281. /* Clean up disconnected queue, they have been invalidated
  1282. * by the bus reset.
  1283. */
  1284. if (esp->disconnected_SC) {
  1285. while((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
  1286. esp_release_dmabufs(esp, sp);
  1287. sp->result = (DID_RESET << 16);
  1288. sp->scsi_done(sp);
  1289. }
  1290. }
  1291. /* SCSI bus reset is complete. */
  1292. esp->resetting_bus = 0;
  1293. wake_up(&esp->reset_queue);
  1294. /* Ok, now it is safe to get commands going once more. */
  1295. if(esp->issue_SC)
  1296. esp_exec_cmd(esp);
  1297. return do_intr_end;
  1298. }
  1299. static int esp_do_resetbus(struct NCR_ESP *esp,
  1300. struct ESP_regs *eregs)
  1301. {
  1302. ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
  1303. esp->resetting_bus = 1;
  1304. esp_cmd(esp, eregs, ESP_CMD_RS);
  1305. return do_intr_end;
  1306. }
  1307. /* Reset ESP chip, reset hanging bus, then kill active and
  1308. * disconnected commands for targets without soft reset.
  1309. *
  1310. * The host_lock is acquired by caller.
  1311. */
  1312. int esp_reset(Scsi_Cmnd *SCptr)
  1313. {
  1314. struct NCR_ESP *esp = (struct NCR_ESP *) SCptr->device->host->hostdata;
  1315. spin_lock_irq(esp->ehost->host_lock);
  1316. (void) esp_do_resetbus(esp, esp->eregs);
  1317. spin_unlock_irq(esp->ehost->host_lock);
  1318. wait_event(esp->reset_queue, (esp->resetting_bus == 0));
  1319. return SUCCESS;
  1320. }
  1321. /* Internal ESP done function. */
  1322. static void esp_done(struct NCR_ESP *esp, int error)
  1323. {
  1324. Scsi_Cmnd *done_SC;
  1325. if(esp->current_SC) {
  1326. done_SC = esp->current_SC;
  1327. esp->current_SC = NULL;
  1328. esp_release_dmabufs(esp, done_SC);
  1329. done_SC->result = error;
  1330. done_SC->scsi_done(done_SC);
  1331. /* Bus is free, issue any commands in the queue. */
  1332. if(esp->issue_SC && !esp->current_SC)
  1333. esp_exec_cmd(esp);
  1334. } else {
  1335. /* Panic is safe as current_SC is null so we may still
  1336. * be able to accept more commands to sync disk buffers.
  1337. */
  1338. ESPLOG(("panicing\n"));
  1339. panic("esp: done() called with NULL esp->current_SC");
  1340. }
  1341. }
  1342. /* Wheee, ESP interrupt engine. */
  1343. /* Forward declarations. */
  1344. static int esp_do_phase_determine(struct NCR_ESP *esp,
  1345. struct ESP_regs *eregs);
  1346. static int esp_do_data_finale(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1347. static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1348. static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1349. static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1350. static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1351. static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1352. static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs);
  1353. #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
  1354. #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
  1355. /* We try to avoid some interrupts by jumping ahead and see if the ESP
  1356. * has gotten far enough yet. Hence the following.
  1357. */
  1358. static inline int skipahead1(struct NCR_ESP *esp, struct ESP_regs *eregs,
  1359. Scsi_Cmnd *scp, int prev_phase, int new_phase)
  1360. {
  1361. if(scp->SCp.sent_command != prev_phase)
  1362. return 0;
  1363. if(esp->dma_irq_p(esp)) {
  1364. /* Yes, we are able to save an interrupt. */
  1365. esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
  1366. esp->ireg = esp_read(eregs->esp_intrpt);
  1367. if(!(esp->ireg & ESP_INTR_SR))
  1368. return 0;
  1369. else
  1370. return do_reset_complete;
  1371. }
  1372. /* Ho hum, target is taking forever... */
  1373. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1374. return do_intr_end;
  1375. }
  1376. static inline int skipahead2(struct NCR_ESP *esp,
  1377. struct ESP_regs *eregs,
  1378. Scsi_Cmnd *scp, int prev_phase1, int prev_phase2,
  1379. int new_phase)
  1380. {
  1381. if(scp->SCp.sent_command != prev_phase1 &&
  1382. scp->SCp.sent_command != prev_phase2)
  1383. return 0;
  1384. if(esp->dma_irq_p(esp)) {
  1385. /* Yes, we are able to save an interrupt. */
  1386. esp->sreg = (esp_read(eregs->esp_status) & ~(ESP_STAT_INTR));
  1387. esp->ireg = esp_read(eregs->esp_intrpt);
  1388. if(!(esp->ireg & ESP_INTR_SR))
  1389. return 0;
  1390. else
  1391. return do_reset_complete;
  1392. }
  1393. /* Ho hum, target is taking forever... */
  1394. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1395. return do_intr_end;
  1396. }
  1397. /* Misc. esp helper macros. */
  1398. #define esp_setcount(__eregs, __cnt) \
  1399. esp_write((__eregs)->esp_tclow, ((__cnt) & 0xff)); \
  1400. esp_write((__eregs)->esp_tcmed, (((__cnt) >> 8) & 0xff))
  1401. #define esp_getcount(__eregs) \
  1402. ((esp_read((__eregs)->esp_tclow)&0xff) | \
  1403. ((esp_read((__eregs)->esp_tcmed)&0xff) << 8))
  1404. #define fcount(__esp, __eregs) \
  1405. (esp_read((__eregs)->esp_fflags) & ESP_FF_FBYTES)
  1406. #define fnzero(__esp, __eregs) \
  1407. (esp_read((__eregs)->esp_fflags) & ESP_FF_ONOTZERO)
  1408. /* XXX speculative nops unnecessary when continuing amidst a data phase
  1409. * XXX even on esp100!!! another case of flooding the bus with I/O reg
  1410. * XXX writes...
  1411. */
  1412. #define esp_maybe_nop(__esp, __eregs) \
  1413. if((__esp)->erev == esp100) \
  1414. esp_cmd((__esp), (__eregs), ESP_CMD_NULL)
  1415. #define sreg_to_dataphase(__sreg) \
  1416. ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
  1417. /* The ESP100 when in synchronous data phase, can mistake a long final
  1418. * REQ pulse from the target as an extra byte, it places whatever is on
  1419. * the data lines into the fifo. For now, we will assume when this
  1420. * happens that the target is a bit quirky and we don't want to
  1421. * be talking synchronously to it anyways. Regardless, we need to
  1422. * tell the ESP to eat the extraneous byte so that we can proceed
  1423. * to the next phase.
  1424. */
  1425. static inline int esp100_sync_hwbug(struct NCR_ESP *esp, struct ESP_regs *eregs,
  1426. Scsi_Cmnd *sp, int fifocnt)
  1427. {
  1428. /* Do not touch this piece of code. */
  1429. if((!(esp->erev == esp100)) ||
  1430. (!(sreg_datainp((esp->sreg = esp_read(eregs->esp_status))) && !fifocnt) &&
  1431. !(sreg_dataoutp(esp->sreg) && !fnzero(esp, eregs)))) {
  1432. if(sp->SCp.phase == in_dataout)
  1433. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  1434. return 0;
  1435. } else {
  1436. /* Async mode for this guy. */
  1437. build_sync_nego_msg(esp, 0, 0);
  1438. /* Ack the bogus byte, but set ATN first. */
  1439. esp_cmd(esp, eregs, ESP_CMD_SATN);
  1440. esp_cmd(esp, eregs, ESP_CMD_MOK);
  1441. return 1;
  1442. }
  1443. }
  1444. /* This closes the window during a selection with a reselect pending, because
  1445. * we use DMA for the selection process the FIFO should hold the correct
  1446. * contents if we get reselected during this process. So we just need to
  1447. * ack the possible illegal cmd interrupt pending on the esp100.
  1448. */
  1449. static inline int esp100_reconnect_hwbug(struct NCR_ESP *esp,
  1450. struct ESP_regs *eregs)
  1451. {
  1452. volatile unchar junk;
  1453. if(esp->erev != esp100)
  1454. return 0;
  1455. junk = esp_read(eregs->esp_intrpt);
  1456. if(junk & ESP_INTR_SR)
  1457. return 1;
  1458. return 0;
  1459. }
  1460. /* This verifies the BUSID bits during a reselection so that we know which
  1461. * target is talking to us.
  1462. */
  1463. static inline int reconnect_target(struct NCR_ESP *esp, struct ESP_regs *eregs)
  1464. {
  1465. int it, me = esp->scsi_id_mask, targ = 0;
  1466. if(2 != fcount(esp, eregs))
  1467. return -1;
  1468. it = esp_read(eregs->esp_fdata);
  1469. if(!(it & me))
  1470. return -1;
  1471. it &= ~me;
  1472. if(it & (it - 1))
  1473. return -1;
  1474. while(!(it & 1))
  1475. targ++, it >>= 1;
  1476. return targ;
  1477. }
  1478. /* This verifies the identify from the target so that we know which lun is
  1479. * being reconnected.
  1480. */
  1481. static inline int reconnect_lun(struct NCR_ESP *esp, struct ESP_regs *eregs)
  1482. {
  1483. int lun;
  1484. if((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
  1485. return -1;
  1486. lun = esp_read(eregs->esp_fdata);
  1487. /* Yes, you read this correctly. We report lun of zero
  1488. * if we see parity error. ESP reports parity error for
  1489. * the lun byte, and this is the only way to hope to recover
  1490. * because the target is connected.
  1491. */
  1492. if(esp->sreg & ESP_STAT_PERR)
  1493. return 0;
  1494. /* Check for illegal bits being set in the lun. */
  1495. if((lun & 0x40) || !(lun & 0x80))
  1496. return -1;
  1497. return lun & 7;
  1498. }
  1499. /* This puts the driver in a state where it can revitalize a command that
  1500. * is being continued due to reselection.
  1501. */
  1502. static inline void esp_connect(struct NCR_ESP *esp, struct ESP_regs *eregs,
  1503. Scsi_Cmnd *sp)
  1504. {
  1505. struct scsi_device *dp = sp->device;
  1506. struct esp_device *esp_dev = dp->hostdata;
  1507. if(esp->prev_soff != esp_dev->sync_max_offset ||
  1508. esp->prev_stp != esp_dev->sync_min_period ||
  1509. (esp->erev > esp100a &&
  1510. esp->prev_cfg3 != esp->config3[scmd_id(sp)])) {
  1511. esp->prev_soff = esp_dev->sync_max_offset;
  1512. esp_write(eregs->esp_soff, esp->prev_soff);
  1513. esp->prev_stp = esp_dev->sync_min_period;
  1514. esp_write(eregs->esp_stp, esp->prev_stp);
  1515. if(esp->erev > esp100a) {
  1516. esp->prev_cfg3 = esp->config3[scmd_id(sp)];
  1517. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  1518. }
  1519. }
  1520. esp->current_SC = sp;
  1521. }
  1522. /* This will place the current working command back into the issue queue
  1523. * if we are to receive a reselection amidst a selection attempt.
  1524. */
  1525. static inline void esp_reconnect(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  1526. {
  1527. if(!esp->disconnected_SC)
  1528. ESPLOG(("esp%d: Weird, being reselected but disconnected "
  1529. "command queue is empty.\n", esp->esp_id));
  1530. esp->snip = 0;
  1531. esp->current_SC = NULL;
  1532. sp->SCp.phase = not_issued;
  1533. append_SC(&esp->issue_SC, sp);
  1534. }
  1535. /* Begin message in phase. */
  1536. static int esp_do_msgin(struct NCR_ESP *esp, struct ESP_regs *eregs)
  1537. {
  1538. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  1539. esp_maybe_nop(esp, eregs);
  1540. esp_cmd(esp, eregs, ESP_CMD_TI);
  1541. esp->msgin_len = 1;
  1542. esp->msgin_ctr = 0;
  1543. esp_advance_phase(esp->current_SC, in_msgindone);
  1544. return do_work_bus;
  1545. }
  1546. static inline void advance_sg(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  1547. {
  1548. ++sp->SCp.buffer;
  1549. --sp->SCp.buffers_residual;
  1550. sp->SCp.this_residual = sp->SCp.buffer->length;
  1551. if (esp->dma_advance_sg)
  1552. esp->dma_advance_sg (sp);
  1553. else
  1554. sp->SCp.ptr = (char *) virt_to_phys((page_address(sp->SCp.buffer->page) + sp->SCp.buffer->offset));
  1555. }
  1556. /* Please note that the way I've coded these routines is that I _always_
  1557. * check for a disconnect during any and all information transfer
  1558. * phases. The SCSI standard states that the target _can_ cause a BUS
  1559. * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
  1560. * that during information transfer phases the target controls every
  1561. * change in phase, the only thing the initiator can do is "ask" for
  1562. * a message out phase by driving ATN true. The target can, and sometimes
  1563. * will, completely ignore this request so we cannot assume anything when
  1564. * we try to force a message out phase to abort/reset a target. Most of
  1565. * the time the target will eventually be nice and go to message out, so
  1566. * we may have to hold on to our state about what we want to tell the target
  1567. * for some period of time.
  1568. */
  1569. /* I think I have things working here correctly. Even partial transfers
  1570. * within a buffer or sub-buffer should not upset us at all no matter
  1571. * how bad the target and/or ESP fucks things up.
  1572. */
  1573. static int esp_do_data(struct NCR_ESP *esp, struct ESP_regs *eregs)
  1574. {
  1575. Scsi_Cmnd *SCptr = esp->current_SC;
  1576. int thisphase, hmuch;
  1577. ESPDATA(("esp_do_data: "));
  1578. esp_maybe_nop(esp, eregs);
  1579. thisphase = sreg_to_dataphase(esp->sreg);
  1580. esp_advance_phase(SCptr, thisphase);
  1581. ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
  1582. hmuch = esp->dma_can_transfer(esp, SCptr);
  1583. /*
  1584. * XXX MSch: cater for PIO transfer here; PIO used if hmuch == 0
  1585. */
  1586. if (hmuch) { /* DMA */
  1587. /*
  1588. * DMA
  1589. */
  1590. ESPDATA(("hmuch<%d> ", hmuch));
  1591. esp->current_transfer_size = hmuch;
  1592. esp_setcount(eregs, (esp->fas_premature_intr_workaround ?
  1593. (hmuch + 0x40) : hmuch));
  1594. esp->dma_setup(esp, (__u32)((unsigned long)SCptr->SCp.ptr),
  1595. hmuch, (thisphase == in_datain));
  1596. ESPDATA(("DMA|TI --> do_intr_end\n"));
  1597. esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
  1598. return do_intr_end;
  1599. /*
  1600. * end DMA
  1601. */
  1602. } else {
  1603. /*
  1604. * PIO
  1605. */
  1606. int oldphase, i = 0; /* or where we left off last time ?? esp->current_data ?? */
  1607. int fifocnt = 0;
  1608. unsigned char *p = phys_to_virt((unsigned long)SCptr->SCp.ptr);
  1609. oldphase = esp_read(eregs->esp_status) & ESP_STAT_PMASK;
  1610. /*
  1611. * polled transfer; ugly, can we make this happen in a DRQ
  1612. * interrupt handler ??
  1613. * requires keeping track of state information in host or
  1614. * command struct!
  1615. * Problem: I've never seen a DRQ happen on Mac, not even
  1616. * with ESP_CMD_DMA ...
  1617. */
  1618. /* figure out how much needs to be transferred */
  1619. hmuch = SCptr->SCp.this_residual;
  1620. ESPDATA(("hmuch<%d> pio ", hmuch));
  1621. esp->current_transfer_size = hmuch;
  1622. /* tell the ESP ... */
  1623. esp_setcount(eregs, hmuch);
  1624. /* loop */
  1625. while (hmuch) {
  1626. int j, fifo_stuck = 0, newphase;
  1627. unsigned long timeout;
  1628. #if 0
  1629. unsigned long flags;
  1630. #endif
  1631. #if 0
  1632. if ( i % 10 )
  1633. ESPDATA(("\r"));
  1634. else
  1635. ESPDATA(( /*"\n"*/ "\r"));
  1636. #endif
  1637. #if 0
  1638. local_irq_save(flags);
  1639. #endif
  1640. if(thisphase == in_datain) {
  1641. /* 'go' ... */
  1642. esp_cmd(esp, eregs, ESP_CMD_TI);
  1643. /* wait for data */
  1644. timeout = 1000000;
  1645. while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
  1646. udelay(2);
  1647. if (timeout == 0)
  1648. printk("DRQ datain timeout! \n");
  1649. newphase = esp->sreg & ESP_STAT_PMASK;
  1650. /* see how much we got ... */
  1651. fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
  1652. if (!fifocnt)
  1653. fifo_stuck++;
  1654. else
  1655. fifo_stuck = 0;
  1656. ESPDATA(("\rgot %d st %x ph %x", fifocnt, esp->sreg, newphase));
  1657. /* read fifo */
  1658. for(j=0;j<fifocnt;j++)
  1659. p[i++] = esp_read(eregs->esp_fdata);
  1660. ESPDATA(("(%d) ", i));
  1661. /* how many to go ?? */
  1662. hmuch -= fifocnt;
  1663. /* break if status phase !! */
  1664. if(newphase == ESP_STATP) {
  1665. /* clear int. */
  1666. esp->ireg = esp_read(eregs->esp_intrpt);
  1667. break;
  1668. }
  1669. } else {
  1670. #define MAX_FIFO 8
  1671. /* how much will fit ? */
  1672. int this_count = MAX_FIFO - fifocnt;
  1673. if (this_count > hmuch)
  1674. this_count = hmuch;
  1675. /* fill fifo */
  1676. for(j=0;j<this_count;j++)
  1677. esp_write(eregs->esp_fdata, p[i++]);
  1678. /* how many left if this goes out ?? */
  1679. hmuch -= this_count;
  1680. /* 'go' ... */
  1681. esp_cmd(esp, eregs, ESP_CMD_TI);
  1682. /* wait for 'got it' */
  1683. timeout = 1000000;
  1684. while (!((esp->sreg=esp_read(eregs->esp_status)) & ESP_STAT_INTR) && --timeout)
  1685. udelay(2);
  1686. if (timeout == 0)
  1687. printk("DRQ dataout timeout! \n");
  1688. newphase = esp->sreg & ESP_STAT_PMASK;
  1689. /* need to check how much was sent ?? */
  1690. fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
  1691. ESPDATA(("\rsent %d st %x ph %x", this_count - fifocnt, esp->sreg, newphase));
  1692. ESPDATA(("(%d) ", i));
  1693. /* break if status phase !! */
  1694. if(newphase == ESP_STATP) {
  1695. /* clear int. */
  1696. esp->ireg = esp_read(eregs->esp_intrpt);
  1697. break;
  1698. }
  1699. }
  1700. /* clear int. */
  1701. esp->ireg = esp_read(eregs->esp_intrpt);
  1702. ESPDATA(("ir %x ... ", esp->ireg));
  1703. if (hmuch == 0)
  1704. ESPDATA(("done! \n"));
  1705. #if 0
  1706. local_irq_restore(flags);
  1707. #endif
  1708. /* check new bus phase */
  1709. if (newphase != oldphase && i < esp->current_transfer_size) {
  1710. /* something happened; disconnect ?? */
  1711. ESPDATA(("phase change, dropped out with %d done ... ", i));
  1712. break;
  1713. }
  1714. /* check int. status */
  1715. if (esp->ireg & ESP_INTR_DC) {
  1716. /* disconnect */
  1717. ESPDATA(("disconnect; %d transferred ... ", i));
  1718. break;
  1719. } else if (esp->ireg & ESP_INTR_FDONE) {
  1720. /* function done */
  1721. ESPDATA(("function done; %d transferred ... ", i));
  1722. break;
  1723. }
  1724. /* XXX fixme: bail out on stall */
  1725. if (fifo_stuck > 10) {
  1726. /* we're stuck */
  1727. ESPDATA(("fifo stall; %d transferred ... ", i));
  1728. break;
  1729. }
  1730. }
  1731. ESPDATA(("\n"));
  1732. /* check successful completion ?? */
  1733. if (thisphase == in_dataout)
  1734. hmuch += fifocnt; /* stuck?? adjust data pointer ...*/
  1735. /* tell do_data_finale how much was transferred */
  1736. esp->current_transfer_size -= hmuch;
  1737. /* still not completely sure on this one ... */
  1738. return /*do_intr_end*/ do_work_bus /*do_phase_determine*/ ;
  1739. /*
  1740. * end PIO
  1741. */
  1742. }
  1743. return do_intr_end;
  1744. }
  1745. /* See how successful the data transfer was. */
  1746. static int esp_do_data_finale(struct NCR_ESP *esp,
  1747. struct ESP_regs *eregs)
  1748. {
  1749. Scsi_Cmnd *SCptr = esp->current_SC;
  1750. struct esp_device *esp_dev = SCptr->device->hostdata;
  1751. int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
  1752. if(esp->dma_led_off)
  1753. esp->dma_led_off(esp);
  1754. ESPDATA(("esp_do_data_finale: "));
  1755. if(SCptr->SCp.phase == in_datain) {
  1756. if(esp->sreg & ESP_STAT_PERR) {
  1757. /* Yuck, parity error. The ESP asserts ATN
  1758. * so that we can go to message out phase
  1759. * immediately and inform the target that
  1760. * something bad happened.
  1761. */
  1762. ESPLOG(("esp%d: data bad parity detected.\n",
  1763. esp->esp_id));
  1764. esp->cur_msgout[0] = INITIATOR_ERROR;
  1765. esp->msgout_len = 1;
  1766. }
  1767. if(esp->dma_drain)
  1768. esp->dma_drain(esp);
  1769. }
  1770. if(esp->dma_invalidate)
  1771. esp->dma_invalidate(esp);
  1772. /* This could happen for the above parity error case. */
  1773. if(!(esp->ireg == ESP_INTR_BSERV)) {
  1774. /* Please go to msgout phase, please please please... */
  1775. ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
  1776. esp->esp_id));
  1777. return esp_do_phase_determine(esp, eregs);
  1778. }
  1779. /* Check for partial transfers and other horrible events. */
  1780. fifocnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
  1781. ecount = esp_getcount(eregs);
  1782. if(esp->fas_premature_intr_workaround)
  1783. ecount -= 0x40;
  1784. bytes_sent = esp->current_transfer_size;
  1785. ESPDATA(("trans_sz=%d, ", bytes_sent));
  1786. if(!(esp->sreg & ESP_STAT_TCNT))
  1787. bytes_sent -= ecount;
  1788. if(SCptr->SCp.phase == in_dataout)
  1789. bytes_sent -= fifocnt;
  1790. ESPDATA(("bytes_sent=%d (ecount=%d, fifocnt=%d), ", bytes_sent,
  1791. ecount, fifocnt));
  1792. /* If we were in synchronous mode, check for peculiarities. */
  1793. if(esp_dev->sync_max_offset)
  1794. bogus_data = esp100_sync_hwbug(esp, eregs, SCptr, fifocnt);
  1795. else
  1796. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  1797. /* Until we are sure of what has happened, we are certainly
  1798. * in the dark.
  1799. */
  1800. esp_advance_phase(SCptr, in_the_dark);
  1801. /* Check for premature interrupt condition. Can happen on FAS2x6
  1802. * chips. QLogic recommends a workaround by overprogramming the
  1803. * transfer counters, but this makes doing scatter-gather impossible.
  1804. * Until there is a way to disable scatter-gather for a single target,
  1805. * and not only for the entire host adapter as it is now, the workaround
  1806. * is way to expensive performance wise.
  1807. * Instead, it turns out that when this happens the target has disconnected
  1808. * already but it doesn't show in the interrupt register. Compensate for
  1809. * that here to try and avoid a SCSI bus reset.
  1810. */
  1811. if(!esp->fas_premature_intr_workaround && (fifocnt == 1) &&
  1812. sreg_dataoutp(esp->sreg)) {
  1813. ESPLOG(("esp%d: Premature interrupt, enabling workaround\n",
  1814. esp->esp_id));
  1815. #if 0
  1816. /* Disable scatter-gather operations, they are not possible
  1817. * when using this workaround.
  1818. */
  1819. esp->ehost->sg_tablesize = 0;
  1820. esp->ehost->use_clustering = ENABLE_CLUSTERING;
  1821. esp->fas_premature_intr_workaround = 1;
  1822. bytes_sent = 0;
  1823. if(SCptr->use_sg) {
  1824. ESPLOG(("esp%d: Aborting scatter-gather operation\n",
  1825. esp->esp_id));
  1826. esp->cur_msgout[0] = ABORT;
  1827. esp->msgout_len = 1;
  1828. esp->msgout_ctr = 0;
  1829. esp_cmd(esp, eregs, ESP_CMD_SATN);
  1830. esp_setcount(eregs, 0xffff);
  1831. esp_cmd(esp, eregs, ESP_CMD_NULL);
  1832. esp_cmd(esp, eregs, ESP_CMD_TPAD | ESP_CMD_DMA);
  1833. return do_intr_end;
  1834. }
  1835. #else
  1836. /* Just set the disconnected bit. That's what appears to
  1837. * happen anyway. The state machine will pick it up when
  1838. * we return.
  1839. */
  1840. esp->ireg |= ESP_INTR_DC;
  1841. #endif
  1842. }
  1843. if(bytes_sent < 0) {
  1844. /* I've seen this happen due to lost state in this
  1845. * driver. No idea why it happened, but allowing
  1846. * this value to be negative caused things to
  1847. * lock up. This allows greater chance of recovery.
  1848. * In fact every time I've seen this, it has been
  1849. * a driver bug without question.
  1850. */
  1851. ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
  1852. ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
  1853. esp->esp_id,
  1854. esp->current_transfer_size, fifocnt, ecount));
  1855. ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
  1856. esp->esp_id,
  1857. SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
  1858. ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
  1859. SCptr->device->id));
  1860. SCptr->device->borken = 1;
  1861. esp_dev->sync = 0;
  1862. bytes_sent = 0;
  1863. }
  1864. /* Update the state of our transfer. */
  1865. SCptr->SCp.ptr += bytes_sent;
  1866. SCptr->SCp.this_residual -= bytes_sent;
  1867. if(SCptr->SCp.this_residual < 0) {
  1868. /* shit */
  1869. ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
  1870. SCptr->SCp.this_residual = 0;
  1871. }
  1872. /* Maybe continue. */
  1873. if(!bogus_data) {
  1874. ESPDATA(("!bogus_data, "));
  1875. /* NO MATTER WHAT, we advance the scatterlist,
  1876. * if the target should decide to disconnect
  1877. * in between scatter chunks (which is common)
  1878. * we could die horribly! I used to have the sg
  1879. * advance occur only if we are going back into
  1880. * (or are staying in) a data phase, you can
  1881. * imagine the hell I went through trying to
  1882. * figure this out.
  1883. */
  1884. if(!SCptr->SCp.this_residual && SCptr->SCp.buffers_residual)
  1885. advance_sg(esp, SCptr);
  1886. #ifdef DEBUG_ESP_DATA
  1887. if(sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
  1888. ESPDATA(("to more data\n"));
  1889. } else {
  1890. ESPDATA(("to new phase\n"));
  1891. }
  1892. #endif
  1893. return esp_do_phase_determine(esp, eregs);
  1894. }
  1895. /* Bogus data, just wait for next interrupt. */
  1896. ESPLOG(("esp%d: bogus_data during end of data phase\n",
  1897. esp->esp_id));
  1898. return do_intr_end;
  1899. }
  1900. /* We received a non-good status return at the end of
  1901. * running a SCSI command. This is used to decide if
  1902. * we should clear our synchronous transfer state for
  1903. * such a device when that happens.
  1904. *
  1905. * The idea is that when spinning up a disk or rewinding
  1906. * a tape, we don't want to go into a loop re-negotiating
  1907. * synchronous capabilities over and over.
  1908. */
  1909. static int esp_should_clear_sync(Scsi_Cmnd *sp)
  1910. {
  1911. unchar cmd = sp->cmnd[0];
  1912. /* These cases are for spinning up a disk and
  1913. * waiting for that spinup to complete.
  1914. */
  1915. if(cmd == START_STOP)
  1916. return 0;
  1917. if(cmd == TEST_UNIT_READY)
  1918. return 0;
  1919. /* One more special case for SCSI tape drives,
  1920. * this is what is used to probe the device for
  1921. * completion of a rewind or tape load operation.
  1922. */
  1923. if(sp->device->type == TYPE_TAPE && cmd == MODE_SENSE)
  1924. return 0;
  1925. return 1;
  1926. }
  1927. /* Either a command is completing or a target is dropping off the bus
  1928. * to continue the command in the background so we can do other work.
  1929. */
  1930. static int esp_do_freebus(struct NCR_ESP *esp, struct ESP_regs *eregs)
  1931. {
  1932. Scsi_Cmnd *SCptr = esp->current_SC;
  1933. int rval;
  1934. rval = skipahead2(esp, eregs, SCptr, in_status, in_msgindone, in_freeing);
  1935. if(rval)
  1936. return rval;
  1937. if(esp->ireg != ESP_INTR_DC) {
  1938. ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
  1939. return do_reset_bus; /* target will not drop BSY... */
  1940. }
  1941. esp->msgout_len = 0;
  1942. esp->prevmsgout = NOP;
  1943. if(esp->prevmsgin == COMMAND_COMPLETE) {
  1944. struct esp_device *esp_dev = SCptr->device->hostdata;
  1945. /* Normal end of nexus. */
  1946. if(esp->disconnected_SC)
  1947. esp_cmd(esp, eregs, ESP_CMD_ESEL);
  1948. if(SCptr->SCp.Status != GOOD &&
  1949. SCptr->SCp.Status != CONDITION_GOOD &&
  1950. ((1<<scmd_id(SCptr)) & esp->targets_present) &&
  1951. esp_dev->sync && esp_dev->sync_max_offset) {
  1952. /* SCSI standard says that the synchronous capabilities
  1953. * should be renegotiated at this point. Most likely
  1954. * we are about to request sense from this target
  1955. * in which case we want to avoid using sync
  1956. * transfers until we are sure of the current target
  1957. * state.
  1958. */
  1959. ESPMISC(("esp: Status <%d> for target %d lun %d\n",
  1960. SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
  1961. /* But don't do this when spinning up a disk at
  1962. * boot time while we poll for completion as it
  1963. * fills up the console with messages. Also, tapes
  1964. * can report not ready many times right after
  1965. * loading up a tape.
  1966. */
  1967. if(esp_should_clear_sync(SCptr) != 0)
  1968. esp_dev->sync = 0;
  1969. }
  1970. ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  1971. esp_done(esp, ((SCptr->SCp.Status & 0xff) |
  1972. ((SCptr->SCp.Message & 0xff)<<8) |
  1973. (DID_OK << 16)));
  1974. } else if(esp->prevmsgin == DISCONNECT) {
  1975. /* Normal disconnect. */
  1976. esp_cmd(esp, eregs, ESP_CMD_ESEL);
  1977. ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  1978. append_SC(&esp->disconnected_SC, SCptr);
  1979. esp->current_SC = NULL;
  1980. if(esp->issue_SC)
  1981. esp_exec_cmd(esp);
  1982. } else {
  1983. /* Driver bug, we do not expect a disconnect here
  1984. * and should not have advanced the state engine
  1985. * to in_freeing.
  1986. */
  1987. ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
  1988. esp->esp_id));
  1989. return do_reset_bus;
  1990. }
  1991. return do_intr_end;
  1992. }
  1993. /* When a reselect occurs, and we cannot find the command to
  1994. * reconnect to in our queues, we do this.
  1995. */
  1996. static int esp_bad_reconnect(struct NCR_ESP *esp)
  1997. {
  1998. Scsi_Cmnd *sp;
  1999. ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
  2000. esp->esp_id));
  2001. ESPLOG(("QUEUE DUMP\n"));
  2002. sp = esp->issue_SC;
  2003. ESPLOG(("esp%d: issue_SC[", esp->esp_id));
  2004. while(sp) {
  2005. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2006. sp = (Scsi_Cmnd *) sp->host_scribble;
  2007. }
  2008. ESPLOG(("]\n"));
  2009. sp = esp->current_SC;
  2010. ESPLOG(("esp%d: current_SC[", esp->esp_id));
  2011. while(sp) {
  2012. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2013. sp = (Scsi_Cmnd *) sp->host_scribble;
  2014. }
  2015. ESPLOG(("]\n"));
  2016. sp = esp->disconnected_SC;
  2017. ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
  2018. while(sp) {
  2019. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2020. sp = (Scsi_Cmnd *) sp->host_scribble;
  2021. }
  2022. ESPLOG(("]\n"));
  2023. return do_reset_bus;
  2024. }
  2025. /* Do the needy when a target tries to reconnect to us. */
  2026. static int esp_do_reconnect(struct NCR_ESP *esp,
  2027. struct ESP_regs *eregs)
  2028. {
  2029. int lun, target;
  2030. Scsi_Cmnd *SCptr;
  2031. /* Check for all bogus conditions first. */
  2032. target = reconnect_target(esp, eregs);
  2033. if(target < 0) {
  2034. ESPDISC(("bad bus bits\n"));
  2035. return do_reset_bus;
  2036. }
  2037. lun = reconnect_lun(esp, eregs);
  2038. if(lun < 0) {
  2039. ESPDISC(("target=%2x, bad identify msg\n", target));
  2040. return do_reset_bus;
  2041. }
  2042. /* Things look ok... */
  2043. ESPDISC(("R<%02x,%02x>", target, lun));
  2044. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2045. if(esp100_reconnect_hwbug(esp, eregs))
  2046. return do_reset_bus;
  2047. esp_cmd(esp, eregs, ESP_CMD_NULL);
  2048. SCptr = remove_SC(&esp->disconnected_SC, (unchar) target, (unchar) lun);
  2049. if(!SCptr)
  2050. return esp_bad_reconnect(esp);
  2051. esp_connect(esp, eregs, SCptr);
  2052. esp_cmd(esp, eregs, ESP_CMD_MOK);
  2053. /* Reconnect implies a restore pointers operation. */
  2054. esp_restore_pointers(esp, SCptr);
  2055. esp->snip = 0;
  2056. esp_advance_phase(SCptr, in_the_dark);
  2057. return do_intr_end;
  2058. }
  2059. /* End of NEXUS (hopefully), pick up status + message byte then leave if
  2060. * all goes well.
  2061. */
  2062. static int esp_do_status(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2063. {
  2064. Scsi_Cmnd *SCptr = esp->current_SC;
  2065. int intr, rval;
  2066. rval = skipahead1(esp, eregs, SCptr, in_the_dark, in_status);
  2067. if(rval)
  2068. return rval;
  2069. intr = esp->ireg;
  2070. ESPSTAT(("esp_do_status: "));
  2071. if(intr != ESP_INTR_DC) {
  2072. int message_out = 0; /* for parity problems */
  2073. /* Ack the message. */
  2074. ESPSTAT(("ack msg, "));
  2075. esp_cmd(esp, eregs, ESP_CMD_MOK);
  2076. if(esp->dma_poll)
  2077. esp->dma_poll(esp, (unsigned char *) esp->esp_command);
  2078. ESPSTAT(("got something, "));
  2079. /* ESP chimes in with one of
  2080. *
  2081. * 1) function done interrupt:
  2082. * both status and message in bytes
  2083. * are available
  2084. *
  2085. * 2) bus service interrupt:
  2086. * only status byte was acquired
  2087. *
  2088. * 3) Anything else:
  2089. * can't happen, but we test for it
  2090. * anyways
  2091. *
  2092. * ALSO: If bad parity was detected on either
  2093. * the status _or_ the message byte then
  2094. * the ESP has asserted ATN on the bus
  2095. * and we must therefore wait for the
  2096. * next phase change.
  2097. */
  2098. if(intr & ESP_INTR_FDONE) {
  2099. /* We got it all, hallejulia. */
  2100. ESPSTAT(("got both, "));
  2101. SCptr->SCp.Status = esp->esp_command[0];
  2102. SCptr->SCp.Message = esp->esp_command[1];
  2103. esp->prevmsgin = SCptr->SCp.Message;
  2104. esp->cur_msgin[0] = SCptr->SCp.Message;
  2105. if(esp->sreg & ESP_STAT_PERR) {
  2106. /* There was bad parity for the
  2107. * message byte, the status byte
  2108. * was ok.
  2109. */
  2110. message_out = MSG_PARITY_ERROR;
  2111. }
  2112. } else if(intr == ESP_INTR_BSERV) {
  2113. /* Only got status byte. */
  2114. ESPLOG(("esp%d: got status only, ", esp->esp_id));
  2115. if(!(esp->sreg & ESP_STAT_PERR)) {
  2116. SCptr->SCp.Status = esp->esp_command[0];
  2117. SCptr->SCp.Message = 0xff;
  2118. } else {
  2119. /* The status byte had bad parity.
  2120. * we leave the scsi_pointer Status
  2121. * field alone as we set it to a default
  2122. * of CHECK_CONDITION in esp_queue.
  2123. */
  2124. message_out = INITIATOR_ERROR;
  2125. }
  2126. } else {
  2127. /* This shouldn't happen ever. */
  2128. ESPSTAT(("got bolixed\n"));
  2129. esp_advance_phase(SCptr, in_the_dark);
  2130. return esp_do_phase_determine(esp, eregs);
  2131. }
  2132. if(!message_out) {
  2133. ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
  2134. SCptr->SCp.Message));
  2135. if(SCptr->SCp.Message == COMMAND_COMPLETE) {
  2136. ESPSTAT(("and was COMMAND_COMPLETE\n"));
  2137. esp_advance_phase(SCptr, in_freeing);
  2138. return esp_do_freebus(esp, eregs);
  2139. } else {
  2140. ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
  2141. esp->esp_id));
  2142. esp->msgin_len = esp->msgin_ctr = 1;
  2143. esp_advance_phase(SCptr, in_msgindone);
  2144. return esp_do_msgindone(esp, eregs);
  2145. }
  2146. } else {
  2147. /* With luck we'll be able to let the target
  2148. * know that bad parity happened, it will know
  2149. * which byte caused the problems and send it
  2150. * again. For the case where the status byte
  2151. * receives bad parity, I do not believe most
  2152. * targets recover very well. We'll see.
  2153. */
  2154. ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
  2155. esp->esp_id, message_out));
  2156. esp->cur_msgout[0] = message_out;
  2157. esp->msgout_len = esp->msgout_ctr = 1;
  2158. esp_advance_phase(SCptr, in_the_dark);
  2159. return esp_do_phase_determine(esp, eregs);
  2160. }
  2161. } else {
  2162. /* If we disconnect now, all hell breaks loose. */
  2163. ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
  2164. esp_advance_phase(SCptr, in_the_dark);
  2165. return esp_do_phase_determine(esp, eregs);
  2166. }
  2167. }
  2168. static int esp_enter_status(struct NCR_ESP *esp,
  2169. struct ESP_regs *eregs)
  2170. {
  2171. unchar thecmd = ESP_CMD_ICCSEQ;
  2172. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2173. if(esp->do_pio_cmds) {
  2174. esp_advance_phase(esp->current_SC, in_status);
  2175. esp_cmd(esp, eregs, thecmd);
  2176. while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
  2177. esp->esp_command[0] = esp_read(eregs->esp_fdata);
  2178. while(!(esp_read(esp->eregs->esp_status) & ESP_STAT_INTR));
  2179. esp->esp_command[1] = esp_read(eregs->esp_fdata);
  2180. } else {
  2181. esp->esp_command[0] = esp->esp_command[1] = 0xff;
  2182. esp_write(eregs->esp_tclow, 2);
  2183. esp_write(eregs->esp_tcmed, 0);
  2184. esp->dma_init_read(esp, esp->esp_command_dvma, 2);
  2185. thecmd |= ESP_CMD_DMA;
  2186. esp_cmd(esp, eregs, thecmd);
  2187. esp_advance_phase(esp->current_SC, in_status);
  2188. }
  2189. return esp_do_status(esp, eregs);
  2190. }
  2191. static int esp_disconnect_amidst_phases(struct NCR_ESP *esp,
  2192. struct ESP_regs *eregs)
  2193. {
  2194. Scsi_Cmnd *sp = esp->current_SC;
  2195. struct esp_device *esp_dev = sp->device->hostdata;
  2196. /* This means real problems if we see this
  2197. * here. Unless we were actually trying
  2198. * to force the device to abort/reset.
  2199. */
  2200. ESPLOG(("esp%d: Disconnect amidst phases, ", esp->esp_id));
  2201. ESPLOG(("pphase<%s> cphase<%s>, ",
  2202. phase_string(sp->SCp.phase),
  2203. phase_string(sp->SCp.sent_command)));
  2204. if(esp->disconnected_SC)
  2205. esp_cmd(esp, eregs, ESP_CMD_ESEL);
  2206. switch(esp->cur_msgout[0]) {
  2207. default:
  2208. /* We didn't expect this to happen at all. */
  2209. ESPLOG(("device is bolixed\n"));
  2210. esp_advance_phase(sp, in_tgterror);
  2211. esp_done(esp, (DID_ERROR << 16));
  2212. break;
  2213. case BUS_DEVICE_RESET:
  2214. ESPLOG(("device reset successful\n"));
  2215. esp_dev->sync_max_offset = 0;
  2216. esp_dev->sync_min_period = 0;
  2217. esp_dev->sync = 0;
  2218. esp_advance_phase(sp, in_resetdev);
  2219. esp_done(esp, (DID_RESET << 16));
  2220. break;
  2221. case ABORT:
  2222. ESPLOG(("device abort successful\n"));
  2223. esp_advance_phase(sp, in_abortone);
  2224. esp_done(esp, (DID_ABORT << 16));
  2225. break;
  2226. };
  2227. return do_intr_end;
  2228. }
  2229. static int esp_enter_msgout(struct NCR_ESP *esp,
  2230. struct ESP_regs *eregs)
  2231. {
  2232. esp_advance_phase(esp->current_SC, in_msgout);
  2233. return esp_do_msgout(esp, eregs);
  2234. }
  2235. static int esp_enter_msgin(struct NCR_ESP *esp,
  2236. struct ESP_regs *eregs)
  2237. {
  2238. esp_advance_phase(esp->current_SC, in_msgin);
  2239. return esp_do_msgin(esp, eregs);
  2240. }
  2241. static int esp_enter_cmd(struct NCR_ESP *esp,
  2242. struct ESP_regs *eregs)
  2243. {
  2244. esp_advance_phase(esp->current_SC, in_cmdbegin);
  2245. return esp_do_cmdbegin(esp, eregs);
  2246. }
  2247. static int esp_enter_badphase(struct NCR_ESP *esp,
  2248. struct ESP_regs *eregs)
  2249. {
  2250. ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
  2251. esp->sreg & ESP_STAT_PMASK));
  2252. return do_reset_bus;
  2253. }
  2254. typedef int (*espfunc_t)(struct NCR_ESP *,
  2255. struct ESP_regs *);
  2256. static espfunc_t phase_vector[] = {
  2257. esp_do_data, /* ESP_DOP */
  2258. esp_do_data, /* ESP_DIP */
  2259. esp_enter_cmd, /* ESP_CMDP */
  2260. esp_enter_status, /* ESP_STATP */
  2261. esp_enter_badphase, /* ESP_STAT_PMSG */
  2262. esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
  2263. esp_enter_msgout, /* ESP_MOP */
  2264. esp_enter_msgin, /* ESP_MIP */
  2265. };
  2266. /* The target has control of the bus and we have to see where it has
  2267. * taken us.
  2268. */
  2269. static int esp_do_phase_determine(struct NCR_ESP *esp,
  2270. struct ESP_regs *eregs)
  2271. {
  2272. if ((esp->ireg & ESP_INTR_DC) != 0)
  2273. return esp_disconnect_amidst_phases(esp, eregs);
  2274. return phase_vector[esp->sreg & ESP_STAT_PMASK](esp, eregs);
  2275. }
  2276. /* First interrupt after exec'ing a cmd comes here. */
  2277. static int esp_select_complete(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2278. {
  2279. Scsi_Cmnd *SCptr = esp->current_SC;
  2280. struct esp_device *esp_dev = SCptr->device->hostdata;
  2281. int cmd_bytes_sent, fcnt;
  2282. fcnt = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES);
  2283. cmd_bytes_sent = esp->dma_bytes_sent(esp, fcnt);
  2284. if(esp->dma_invalidate)
  2285. esp->dma_invalidate(esp);
  2286. /* Let's check to see if a reselect happened
  2287. * while we we're trying to select. This must
  2288. * be checked first.
  2289. */
  2290. if(esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
  2291. esp_reconnect(esp, SCptr);
  2292. return esp_do_reconnect(esp, eregs);
  2293. }
  2294. /* Looks like things worked, we should see a bus service &
  2295. * a function complete interrupt at this point. Note we
  2296. * are doing a direct comparison because we don't want to
  2297. * be fooled into thinking selection was successful if
  2298. * ESP_INTR_DC is set, see below.
  2299. */
  2300. if(esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
  2301. /* target speaks... */
  2302. esp->targets_present |= (1<<scmd_id(SCptr));
  2303. /* What if the target ignores the sdtr? */
  2304. if(esp->snip)
  2305. esp_dev->sync = 1;
  2306. /* See how far, if at all, we got in getting
  2307. * the information out to the target.
  2308. */
  2309. switch(esp->seqreg) {
  2310. default:
  2311. case ESP_STEP_ASEL:
  2312. /* Arbitration won, target selected, but
  2313. * we are in some phase which is not command
  2314. * phase nor is it message out phase.
  2315. *
  2316. * XXX We've confused the target, obviously.
  2317. * XXX So clear it's state, but we also end
  2318. * XXX up clearing everyone elses. That isn't
  2319. * XXX so nice. I'd like to just reset this
  2320. * XXX target, but if I cannot even get it's
  2321. * XXX attention and finish selection to talk
  2322. * XXX to it, there is not much more I can do.
  2323. * XXX If we have a loaded bus we're going to
  2324. * XXX spend the next second or so renegotiating
  2325. * XXX for synchronous transfers.
  2326. */
  2327. ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
  2328. esp->esp_id, SCptr->device->id));
  2329. case ESP_STEP_SID:
  2330. /* Arbitration won, target selected, went
  2331. * to message out phase, sent one message
  2332. * byte, then we stopped. ATN is asserted
  2333. * on the SCSI bus and the target is still
  2334. * there hanging on. This is a legal
  2335. * sequence step if we gave the ESP a select
  2336. * and stop command.
  2337. *
  2338. * XXX See above, I could set the borken flag
  2339. * XXX in the device struct and retry the
  2340. * XXX command. But would that help for
  2341. * XXX tagged capable targets?
  2342. */
  2343. case ESP_STEP_NCMD:
  2344. /* Arbitration won, target selected, maybe
  2345. * sent the one message byte in message out
  2346. * phase, but we did not go to command phase
  2347. * in the end. Actually, we could have sent
  2348. * only some of the message bytes if we tried
  2349. * to send out the entire identify and tag
  2350. * message using ESP_CMD_SA3.
  2351. */
  2352. cmd_bytes_sent = 0;
  2353. break;
  2354. case ESP_STEP_PPC:
  2355. /* No, not the powerPC pinhead. Arbitration
  2356. * won, all message bytes sent if we went to
  2357. * message out phase, went to command phase
  2358. * but only part of the command was sent.
  2359. *
  2360. * XXX I've seen this, but usually in conjunction
  2361. * XXX with a gross error which appears to have
  2362. * XXX occurred between the time I told the
  2363. * XXX ESP to arbitrate and when I got the
  2364. * XXX interrupt. Could I have misloaded the
  2365. * XXX command bytes into the fifo? Actually,
  2366. * XXX I most likely missed a phase, and therefore
  2367. * XXX went into never never land and didn't even
  2368. * XXX know it. That was the old driver though.
  2369. * XXX What is even more peculiar is that the ESP
  2370. * XXX showed the proper function complete and
  2371. * XXX bus service bits in the interrupt register.
  2372. */
  2373. case ESP_STEP_FINI4:
  2374. case ESP_STEP_FINI5:
  2375. case ESP_STEP_FINI6:
  2376. case ESP_STEP_FINI7:
  2377. /* Account for the identify message */
  2378. if(SCptr->SCp.phase == in_slct_norm)
  2379. cmd_bytes_sent -= 1;
  2380. };
  2381. esp_cmd(esp, eregs, ESP_CMD_NULL);
  2382. /* Be careful, we could really get fucked during synchronous
  2383. * data transfers if we try to flush the fifo now.
  2384. */
  2385. if(!fcnt && /* Fifo is empty and... */
  2386. /* either we are not doing synchronous transfers or... */
  2387. (!esp_dev->sync_max_offset ||
  2388. /* We are not going into data in phase. */
  2389. ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
  2390. esp_cmd(esp, eregs, ESP_CMD_FLUSH); /* flush is safe */
  2391. /* See how far we got if this is not a slow command. */
  2392. if(!esp->esp_slowcmd) {
  2393. if(cmd_bytes_sent < 0)
  2394. cmd_bytes_sent = 0;
  2395. if(cmd_bytes_sent != SCptr->cmd_len) {
  2396. /* Crapola, mark it as a slowcmd
  2397. * so that we have some chance of
  2398. * keeping the command alive with
  2399. * good luck.
  2400. *
  2401. * XXX Actually, if we didn't send it all
  2402. * XXX this means either we didn't set things
  2403. * XXX up properly (driver bug) or the target
  2404. * XXX or the ESP detected parity on one of
  2405. * XXX the command bytes. This makes much
  2406. * XXX more sense, and therefore this code
  2407. * XXX should be changed to send out a
  2408. * XXX parity error message or if the status
  2409. * XXX register shows no parity error then
  2410. * XXX just expect the target to bring the
  2411. * XXX bus into message in phase so that it
  2412. * XXX can send us the parity error message.
  2413. * XXX SCSI sucks...
  2414. */
  2415. esp->esp_slowcmd = 1;
  2416. esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
  2417. esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
  2418. }
  2419. }
  2420. /* Now figure out where we went. */
  2421. esp_advance_phase(SCptr, in_the_dark);
  2422. return esp_do_phase_determine(esp, eregs);
  2423. }
  2424. /* Did the target even make it? */
  2425. if(esp->ireg == ESP_INTR_DC) {
  2426. /* wheee... nobody there or they didn't like
  2427. * what we told it to do, clean up.
  2428. */
  2429. /* If anyone is off the bus, but working on
  2430. * a command in the background for us, tell
  2431. * the ESP to listen for them.
  2432. */
  2433. if(esp->disconnected_SC)
  2434. esp_cmd(esp, eregs, ESP_CMD_ESEL);
  2435. if(((1<<SCptr->device->id) & esp->targets_present) &&
  2436. esp->seqreg && esp->cur_msgout[0] == EXTENDED_MESSAGE &&
  2437. (SCptr->SCp.phase == in_slct_msg ||
  2438. SCptr->SCp.phase == in_slct_stop)) {
  2439. /* shit */
  2440. esp->snip = 0;
  2441. ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
  2442. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  2443. esp_dev->sync_max_offset = 0;
  2444. esp_dev->sync_min_period = 0;
  2445. esp_dev->sync = 1; /* so we don't negotiate again */
  2446. /* Run the command again, this time though we
  2447. * won't try to negotiate for synchronous transfers.
  2448. *
  2449. * XXX I'd like to do something like send an
  2450. * XXX INITIATOR_ERROR or ABORT message to the
  2451. * XXX target to tell it, "Sorry I confused you,
  2452. * XXX please come back and I will be nicer next
  2453. * XXX time". But that requires having the target
  2454. * XXX on the bus, and it has dropped BSY on us.
  2455. */
  2456. esp->current_SC = NULL;
  2457. esp_advance_phase(SCptr, not_issued);
  2458. prepend_SC(&esp->issue_SC, SCptr);
  2459. esp_exec_cmd(esp);
  2460. return do_intr_end;
  2461. }
  2462. /* Ok, this is normal, this is what we see during boot
  2463. * or whenever when we are scanning the bus for targets.
  2464. * But first make sure that is really what is happening.
  2465. */
  2466. if(((1<<SCptr->device->id) & esp->targets_present)) {
  2467. ESPLOG(("esp%d: Warning, live target %d not responding to "
  2468. "selection.\n", esp->esp_id, SCptr->device->id));
  2469. /* This _CAN_ happen. The SCSI standard states that
  2470. * the target is to _not_ respond to selection if
  2471. * _it_ detects bad parity on the bus for any reason.
  2472. * Therefore, we assume that if we've talked successfully
  2473. * to this target before, bad parity is the problem.
  2474. */
  2475. esp_done(esp, (DID_PARITY << 16));
  2476. } else {
  2477. /* Else, there really isn't anyone there. */
  2478. ESPMISC(("esp: selection failure, maybe nobody there?\n"));
  2479. ESPMISC(("esp: target %d lun %d\n",
  2480. SCptr->device->id, SCptr->device->lun));
  2481. esp_done(esp, (DID_BAD_TARGET << 16));
  2482. }
  2483. return do_intr_end;
  2484. }
  2485. ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
  2486. printk("esp%d: Currently -- ", esp->esp_id);
  2487. esp_print_ireg(esp->ireg);
  2488. printk(" ");
  2489. esp_print_statreg(esp->sreg);
  2490. printk(" ");
  2491. esp_print_seqreg(esp->seqreg);
  2492. printk("\n");
  2493. printk("esp%d: New -- ", esp->esp_id);
  2494. esp->sreg = esp_read(eregs->esp_status);
  2495. esp->seqreg = esp_read(eregs->esp_sstep);
  2496. esp->ireg = esp_read(eregs->esp_intrpt);
  2497. esp_print_ireg(esp->ireg);
  2498. printk(" ");
  2499. esp_print_statreg(esp->sreg);
  2500. printk(" ");
  2501. esp_print_seqreg(esp->seqreg);
  2502. printk("\n");
  2503. ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
  2504. return do_reset_bus; /* ugh... */
  2505. }
  2506. /* Continue reading bytes for msgin phase. */
  2507. static int esp_do_msgincont(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2508. {
  2509. if(esp->ireg & ESP_INTR_BSERV) {
  2510. /* in the right phase too? */
  2511. if((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
  2512. /* phew... */
  2513. esp_cmd(esp, eregs, ESP_CMD_TI);
  2514. esp_advance_phase(esp->current_SC, in_msgindone);
  2515. return do_intr_end;
  2516. }
  2517. /* We changed phase but ESP shows bus service,
  2518. * in this case it is most likely that we, the
  2519. * hacker who has been up for 20hrs straight
  2520. * staring at the screen, drowned in coffee
  2521. * smelling like retched cigarette ashes
  2522. * have miscoded something..... so, try to
  2523. * recover as best we can.
  2524. */
  2525. ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
  2526. }
  2527. esp_advance_phase(esp->current_SC, in_the_dark);
  2528. return do_phase_determine;
  2529. }
  2530. static int check_singlebyte_msg(struct NCR_ESP *esp,
  2531. struct ESP_regs *eregs)
  2532. {
  2533. esp->prevmsgin = esp->cur_msgin[0];
  2534. if(esp->cur_msgin[0] & 0x80) {
  2535. /* wheee... */
  2536. ESPLOG(("esp%d: target sends identify amidst phases\n",
  2537. esp->esp_id));
  2538. esp_advance_phase(esp->current_SC, in_the_dark);
  2539. return 0;
  2540. } else if(((esp->cur_msgin[0] & 0xf0) == 0x20) ||
  2541. (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
  2542. esp->msgin_len = 2;
  2543. esp_advance_phase(esp->current_SC, in_msgincont);
  2544. return 0;
  2545. }
  2546. esp_advance_phase(esp->current_SC, in_the_dark);
  2547. switch(esp->cur_msgin[0]) {
  2548. default:
  2549. /* We don't want to hear about it. */
  2550. ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
  2551. esp->cur_msgin[0]));
  2552. return MESSAGE_REJECT;
  2553. case NOP:
  2554. ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
  2555. esp->current_SC->device->id));
  2556. return 0;
  2557. case RESTORE_POINTERS:
  2558. /* In this case we might also have to backup the
  2559. * "slow command" pointer. It is rare to get such
  2560. * a save/restore pointer sequence so early in the
  2561. * bus transition sequences, but cover it.
  2562. */
  2563. if(esp->esp_slowcmd) {
  2564. esp->esp_scmdleft = esp->current_SC->cmd_len;
  2565. esp->esp_scmdp = &esp->current_SC->cmnd[0];
  2566. }
  2567. esp_restore_pointers(esp, esp->current_SC);
  2568. return 0;
  2569. case SAVE_POINTERS:
  2570. esp_save_pointers(esp, esp->current_SC);
  2571. return 0;
  2572. case COMMAND_COMPLETE:
  2573. case DISCONNECT:
  2574. /* Freeing the bus, let it go. */
  2575. esp->current_SC->SCp.phase = in_freeing;
  2576. return 0;
  2577. case MESSAGE_REJECT:
  2578. ESPMISC(("msg reject, "));
  2579. if(esp->prevmsgout == EXTENDED_MESSAGE) {
  2580. struct esp_device *esp_dev = esp->current_SC->device->hostdata;
  2581. /* Doesn't look like this target can
  2582. * do synchronous or WIDE transfers.
  2583. */
  2584. ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
  2585. esp_dev->sync = 1;
  2586. esp_dev->wide = 1;
  2587. esp_dev->sync_min_period = 0;
  2588. esp_dev->sync_max_offset = 0;
  2589. return 0;
  2590. } else {
  2591. ESPMISC(("not sync nego, sending ABORT\n"));
  2592. return ABORT;
  2593. }
  2594. };
  2595. }
  2596. /* Target negotiates for synchronous transfers before we do, this
  2597. * is legal although very strange. What is even funnier is that
  2598. * the SCSI2 standard specifically recommends against targets doing
  2599. * this because so many initiators cannot cope with this occurring.
  2600. */
  2601. static int target_with_ants_in_pants(struct NCR_ESP *esp,
  2602. Scsi_Cmnd *SCptr,
  2603. struct esp_device *esp_dev)
  2604. {
  2605. if(esp_dev->sync || SCptr->device->borken) {
  2606. /* sorry, no can do */
  2607. ESPSDTR(("forcing to async, "));
  2608. build_sync_nego_msg(esp, 0, 0);
  2609. esp_dev->sync = 1;
  2610. esp->snip = 1;
  2611. ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
  2612. esp_advance_phase(SCptr, in_the_dark);
  2613. return EXTENDED_MESSAGE;
  2614. }
  2615. /* Ok, we'll check them out... */
  2616. return 0;
  2617. }
  2618. static void sync_report(struct NCR_ESP *esp)
  2619. {
  2620. int msg3, msg4;
  2621. char *type;
  2622. msg3 = esp->cur_msgin[3];
  2623. msg4 = esp->cur_msgin[4];
  2624. if(msg4) {
  2625. int hz = 1000000000 / (msg3 * 4);
  2626. int integer = hz / 1000000;
  2627. int fraction = (hz - (integer * 1000000)) / 10000;
  2628. if((msg3 * 4) < 200) {
  2629. type = "FAST";
  2630. } else {
  2631. type = "synchronous";
  2632. }
  2633. /* Do not transform this back into one big printk
  2634. * again, it triggers a bug in our sparc64-gcc272
  2635. * sibling call optimization. -DaveM
  2636. */
  2637. ESPLOG((KERN_INFO "esp%d: target %d ",
  2638. esp->esp_id, esp->current_SC->device->id));
  2639. ESPLOG(("[period %dns offset %d %d.%02dMHz ",
  2640. (int) msg3 * 4, (int) msg4,
  2641. integer, fraction));
  2642. ESPLOG(("%s SCSI%s]\n", type,
  2643. (((msg3 * 4) < 200) ? "-II" : "")));
  2644. } else {
  2645. ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
  2646. esp->esp_id, esp->current_SC->device->id));
  2647. }
  2648. }
  2649. static int check_multibyte_msg(struct NCR_ESP *esp,
  2650. struct ESP_regs *eregs)
  2651. {
  2652. Scsi_Cmnd *SCptr = esp->current_SC;
  2653. struct esp_device *esp_dev = SCptr->device->hostdata;
  2654. unchar regval = 0;
  2655. int message_out = 0;
  2656. ESPSDTR(("chk multibyte msg: "));
  2657. if(esp->cur_msgin[2] == EXTENDED_SDTR) {
  2658. int period = esp->cur_msgin[3];
  2659. int offset = esp->cur_msgin[4];
  2660. ESPSDTR(("is sync nego response, "));
  2661. if(!esp->snip) {
  2662. int rval;
  2663. /* Target negotiates first! */
  2664. ESPSDTR(("target jumps the gun, "));
  2665. message_out = EXTENDED_MESSAGE; /* we must respond */
  2666. rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
  2667. if(rval)
  2668. return rval;
  2669. }
  2670. ESPSDTR(("examining sdtr, "));
  2671. /* Offset cannot be larger than ESP fifo size. */
  2672. if(offset > 15) {
  2673. ESPSDTR(("offset too big %2x, ", offset));
  2674. offset = 15;
  2675. ESPSDTR(("sending back new offset\n"));
  2676. build_sync_nego_msg(esp, period, offset);
  2677. return EXTENDED_MESSAGE;
  2678. }
  2679. if(offset && period > esp->max_period) {
  2680. /* Yeee, async for this slow device. */
  2681. ESPSDTR(("period too long %2x, ", period));
  2682. build_sync_nego_msg(esp, 0, 0);
  2683. ESPSDTR(("hoping for msgout\n"));
  2684. esp_advance_phase(esp->current_SC, in_the_dark);
  2685. return EXTENDED_MESSAGE;
  2686. } else if (offset && period < esp->min_period) {
  2687. ESPSDTR(("period too short %2x, ", period));
  2688. period = esp->min_period;
  2689. if(esp->erev > esp236)
  2690. regval = 4;
  2691. else
  2692. regval = 5;
  2693. } else if(offset) {
  2694. int tmp;
  2695. ESPSDTR(("period is ok, "));
  2696. tmp = esp->ccycle / 1000;
  2697. regval = (((period << 2) + tmp - 1) / tmp);
  2698. if(regval && (esp->erev > esp236)) {
  2699. if(period >= 50)
  2700. regval--;
  2701. }
  2702. }
  2703. if(offset) {
  2704. unchar bit;
  2705. esp_dev->sync_min_period = (regval & 0x1f);
  2706. esp_dev->sync_max_offset = (offset | esp->radelay);
  2707. if(esp->erev > esp236) {
  2708. if(esp->erev == fas100a)
  2709. bit = ESP_CONFIG3_FAST;
  2710. else
  2711. bit = ESP_CONFIG3_FSCSI;
  2712. if(period < 50)
  2713. esp->config3[SCptr->device->id] |= bit;
  2714. else
  2715. esp->config3[SCptr->device->id] &= ~bit;
  2716. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  2717. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  2718. }
  2719. esp->prev_soff = esp_dev->sync_min_period;
  2720. esp_write(eregs->esp_soff, esp->prev_soff);
  2721. esp->prev_stp = esp_dev->sync_max_offset;
  2722. esp_write(eregs->esp_stp, esp->prev_stp);
  2723. ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
  2724. esp_dev->sync_max_offset,
  2725. esp_dev->sync_min_period,
  2726. esp->config3[scmd_id(SCptr)]));
  2727. esp->snip = 0;
  2728. } else if(esp_dev->sync_max_offset) {
  2729. unchar bit;
  2730. /* back to async mode */
  2731. ESPSDTR(("unaccaptable sync nego, forcing async\n"));
  2732. esp_dev->sync_max_offset = 0;
  2733. esp_dev->sync_min_period = 0;
  2734. esp->prev_soff = 0;
  2735. esp_write(eregs->esp_soff, 0);
  2736. esp->prev_stp = 0;
  2737. esp_write(eregs->esp_stp, 0);
  2738. if(esp->erev > esp236) {
  2739. if(esp->erev == fas100a)
  2740. bit = ESP_CONFIG3_FAST;
  2741. else
  2742. bit = ESP_CONFIG3_FSCSI;
  2743. esp->config3[SCptr->device->id] &= ~bit;
  2744. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  2745. esp_write(eregs->esp_cfg3, esp->prev_cfg3);
  2746. }
  2747. }
  2748. sync_report(esp);
  2749. ESPSDTR(("chk multibyte msg: sync is known, "));
  2750. esp_dev->sync = 1;
  2751. if(message_out) {
  2752. ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
  2753. esp->esp_id));
  2754. build_sync_nego_msg(esp, period, offset);
  2755. esp_advance_phase(SCptr, in_the_dark);
  2756. return EXTENDED_MESSAGE;
  2757. }
  2758. ESPSDTR(("returning zero\n"));
  2759. esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
  2760. return 0;
  2761. } else if(esp->cur_msgin[2] == EXTENDED_WDTR) {
  2762. ESPLOG(("esp%d: AIEEE wide msg received\n", esp->esp_id));
  2763. message_out = MESSAGE_REJECT;
  2764. } else if(esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
  2765. ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
  2766. message_out = MESSAGE_REJECT;
  2767. }
  2768. esp_advance_phase(SCptr, in_the_dark);
  2769. return message_out;
  2770. }
  2771. static int esp_do_msgindone(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2772. {
  2773. Scsi_Cmnd *SCptr = esp->current_SC;
  2774. int message_out = 0, it = 0, rval;
  2775. rval = skipahead1(esp, eregs, SCptr, in_msgin, in_msgindone);
  2776. if(rval)
  2777. return rval;
  2778. if(SCptr->SCp.sent_command != in_status) {
  2779. if(!(esp->ireg & ESP_INTR_DC)) {
  2780. if(esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
  2781. message_out = MSG_PARITY_ERROR;
  2782. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2783. } else if((it = (esp_read(eregs->esp_fflags) & ESP_FF_FBYTES))!=1) {
  2784. /* We certainly dropped the ball somewhere. */
  2785. message_out = INITIATOR_ERROR;
  2786. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2787. } else if(!esp->msgin_len) {
  2788. it = esp_read(eregs->esp_fdata);
  2789. esp_advance_phase(SCptr, in_msgincont);
  2790. } else {
  2791. /* it is ok and we want it */
  2792. it = esp->cur_msgin[esp->msgin_ctr] =
  2793. esp_read(eregs->esp_fdata);
  2794. esp->msgin_ctr++;
  2795. }
  2796. } else {
  2797. esp_advance_phase(SCptr, in_the_dark);
  2798. return do_work_bus;
  2799. }
  2800. } else {
  2801. it = esp->cur_msgin[0];
  2802. }
  2803. if(!message_out && esp->msgin_len) {
  2804. if(esp->msgin_ctr < esp->msgin_len) {
  2805. esp_advance_phase(SCptr, in_msgincont);
  2806. } else if(esp->msgin_len == 1) {
  2807. message_out = check_singlebyte_msg(esp, eregs);
  2808. } else if(esp->msgin_len == 2) {
  2809. if(esp->cur_msgin[0] == EXTENDED_MESSAGE) {
  2810. if((it+2) >= 15) {
  2811. message_out = MESSAGE_REJECT;
  2812. } else {
  2813. esp->msgin_len = (it + 2);
  2814. esp_advance_phase(SCptr, in_msgincont);
  2815. }
  2816. } else {
  2817. message_out = MESSAGE_REJECT; /* foo on you */
  2818. }
  2819. } else {
  2820. message_out = check_multibyte_msg(esp, eregs);
  2821. }
  2822. }
  2823. if(message_out < 0) {
  2824. return -message_out;
  2825. } else if(message_out) {
  2826. if(((message_out != 1) &&
  2827. ((message_out < 0x20) || (message_out & 0x80))))
  2828. esp->msgout_len = 1;
  2829. esp->cur_msgout[0] = message_out;
  2830. esp_cmd(esp, eregs, ESP_CMD_SATN);
  2831. esp_advance_phase(SCptr, in_the_dark);
  2832. esp->msgin_len = 0;
  2833. }
  2834. esp->sreg = esp_read(eregs->esp_status);
  2835. esp->sreg &= ~(ESP_STAT_INTR);
  2836. if((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
  2837. esp_cmd(esp, eregs, ESP_CMD_MOK);
  2838. if((SCptr->SCp.sent_command == in_msgindone) &&
  2839. (SCptr->SCp.phase == in_freeing))
  2840. return esp_do_freebus(esp, eregs);
  2841. return do_intr_end;
  2842. }
  2843. static int esp_do_cmdbegin(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2844. {
  2845. unsigned char tmp;
  2846. Scsi_Cmnd *SCptr = esp->current_SC;
  2847. esp_advance_phase(SCptr, in_cmdend);
  2848. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2849. tmp = *esp->esp_scmdp++;
  2850. esp->esp_scmdleft--;
  2851. esp_write(eregs->esp_fdata, tmp);
  2852. esp_cmd(esp, eregs, ESP_CMD_TI);
  2853. return do_intr_end;
  2854. }
  2855. static int esp_do_cmddone(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2856. {
  2857. esp_cmd(esp, eregs, ESP_CMD_NULL);
  2858. if(esp->ireg & ESP_INTR_BSERV) {
  2859. esp_advance_phase(esp->current_SC, in_the_dark);
  2860. return esp_do_phase_determine(esp, eregs);
  2861. }
  2862. ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
  2863. esp->esp_id));
  2864. return do_reset_bus;
  2865. }
  2866. static int esp_do_msgout(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2867. {
  2868. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2869. switch(esp->msgout_len) {
  2870. case 1:
  2871. esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
  2872. esp_cmd(esp, eregs, ESP_CMD_TI);
  2873. break;
  2874. case 2:
  2875. if(esp->do_pio_cmds){
  2876. esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
  2877. esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
  2878. esp_cmd(esp, eregs, ESP_CMD_TI);
  2879. } else {
  2880. esp->esp_command[0] = esp->cur_msgout[0];
  2881. esp->esp_command[1] = esp->cur_msgout[1];
  2882. esp->dma_setup(esp, esp->esp_command_dvma, 2, 0);
  2883. esp_setcount(eregs, 2);
  2884. esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
  2885. }
  2886. break;
  2887. case 4:
  2888. esp->snip = 1;
  2889. if(esp->do_pio_cmds){
  2890. esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
  2891. esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
  2892. esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
  2893. esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
  2894. esp_cmd(esp, eregs, ESP_CMD_TI);
  2895. } else {
  2896. esp->esp_command[0] = esp->cur_msgout[0];
  2897. esp->esp_command[1] = esp->cur_msgout[1];
  2898. esp->esp_command[2] = esp->cur_msgout[2];
  2899. esp->esp_command[3] = esp->cur_msgout[3];
  2900. esp->dma_setup(esp, esp->esp_command_dvma, 4, 0);
  2901. esp_setcount(eregs, 4);
  2902. esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
  2903. }
  2904. break;
  2905. case 5:
  2906. esp->snip = 1;
  2907. if(esp->do_pio_cmds){
  2908. esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
  2909. esp_write(eregs->esp_fdata, esp->cur_msgout[1]);
  2910. esp_write(eregs->esp_fdata, esp->cur_msgout[2]);
  2911. esp_write(eregs->esp_fdata, esp->cur_msgout[3]);
  2912. esp_write(eregs->esp_fdata, esp->cur_msgout[4]);
  2913. esp_cmd(esp, eregs, ESP_CMD_TI);
  2914. } else {
  2915. esp->esp_command[0] = esp->cur_msgout[0];
  2916. esp->esp_command[1] = esp->cur_msgout[1];
  2917. esp->esp_command[2] = esp->cur_msgout[2];
  2918. esp->esp_command[3] = esp->cur_msgout[3];
  2919. esp->esp_command[4] = esp->cur_msgout[4];
  2920. esp->dma_setup(esp, esp->esp_command_dvma, 5, 0);
  2921. esp_setcount(eregs, 5);
  2922. esp_cmd(esp, eregs, ESP_CMD_DMA | ESP_CMD_TI);
  2923. }
  2924. break;
  2925. default:
  2926. /* whoops */
  2927. ESPMISC(("bogus msgout sending NOP\n"));
  2928. esp->cur_msgout[0] = NOP;
  2929. esp_write(eregs->esp_fdata, esp->cur_msgout[0]);
  2930. esp->msgout_len = 1;
  2931. esp_cmd(esp, eregs, ESP_CMD_TI);
  2932. break;
  2933. }
  2934. esp_advance_phase(esp->current_SC, in_msgoutdone);
  2935. return do_intr_end;
  2936. }
  2937. static int esp_do_msgoutdone(struct NCR_ESP *esp,
  2938. struct ESP_regs *eregs)
  2939. {
  2940. if((esp->msgout_len > 1) && esp->dma_barrier)
  2941. esp->dma_barrier(esp);
  2942. if(!(esp->ireg & ESP_INTR_DC)) {
  2943. esp_cmd(esp, eregs, ESP_CMD_NULL);
  2944. switch(esp->sreg & ESP_STAT_PMASK) {
  2945. case ESP_MOP:
  2946. /* whoops, parity error */
  2947. ESPLOG(("esp%d: still in msgout, parity error assumed\n",
  2948. esp->esp_id));
  2949. if(esp->msgout_len > 1)
  2950. esp_cmd(esp, eregs, ESP_CMD_SATN);
  2951. esp_advance_phase(esp->current_SC, in_msgout);
  2952. return do_work_bus;
  2953. case ESP_DIP:
  2954. break;
  2955. default:
  2956. if(!fcount(esp, eregs) &&
  2957. !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
  2958. esp_cmd(esp, eregs, ESP_CMD_FLUSH);
  2959. break;
  2960. };
  2961. }
  2962. /* If we sent out a synchronous negotiation message, update
  2963. * our state.
  2964. */
  2965. if(esp->cur_msgout[2] == EXTENDED_MESSAGE &&
  2966. esp->cur_msgout[4] == EXTENDED_SDTR) {
  2967. esp->snip = 1; /* anal retentiveness... */
  2968. }
  2969. esp->prevmsgout = esp->cur_msgout[0];
  2970. esp->msgout_len = 0;
  2971. esp_advance_phase(esp->current_SC, in_the_dark);
  2972. return esp_do_phase_determine(esp, eregs);
  2973. }
  2974. static int esp_bus_unexpected(struct NCR_ESP *esp, struct ESP_regs *eregs)
  2975. {
  2976. ESPLOG(("esp%d: command in weird state %2x\n",
  2977. esp->esp_id, esp->current_SC->SCp.phase));
  2978. return do_reset_bus;
  2979. }
  2980. static espfunc_t bus_vector[] = {
  2981. esp_do_data_finale,
  2982. esp_do_data_finale,
  2983. esp_bus_unexpected,
  2984. esp_do_msgin,
  2985. esp_do_msgincont,
  2986. esp_do_msgindone,
  2987. esp_do_msgout,
  2988. esp_do_msgoutdone,
  2989. esp_do_cmdbegin,
  2990. esp_do_cmddone,
  2991. esp_do_status,
  2992. esp_do_freebus,
  2993. esp_do_phase_determine,
  2994. esp_bus_unexpected,
  2995. esp_bus_unexpected,
  2996. esp_bus_unexpected,
  2997. };
  2998. /* This is the second tier in our dual-level SCSI state machine. */
  2999. static int esp_work_bus(struct NCR_ESP *esp, struct ESP_regs *eregs)
  3000. {
  3001. Scsi_Cmnd *SCptr = esp->current_SC;
  3002. unsigned int phase;
  3003. ESPBUS(("esp_work_bus: "));
  3004. if(!SCptr) {
  3005. ESPBUS(("reconnect\n"));
  3006. return esp_do_reconnect(esp, eregs);
  3007. }
  3008. phase = SCptr->SCp.phase;
  3009. if ((phase & 0xf0) == in_phases_mask)
  3010. return bus_vector[(phase & 0x0f)](esp, eregs);
  3011. else if((phase & 0xf0) == in_slct_mask)
  3012. return esp_select_complete(esp, eregs);
  3013. else
  3014. return esp_bus_unexpected(esp, eregs);
  3015. }
  3016. static espfunc_t isvc_vector[] = {
  3017. NULL,
  3018. esp_do_phase_determine,
  3019. esp_do_resetbus,
  3020. esp_finish_reset,
  3021. esp_work_bus
  3022. };
  3023. /* Main interrupt handler for an esp adapter. */
  3024. void esp_handle(struct NCR_ESP *esp)
  3025. {
  3026. struct ESP_regs *eregs;
  3027. Scsi_Cmnd *SCptr;
  3028. int what_next = do_intr_end;
  3029. eregs = esp->eregs;
  3030. SCptr = esp->current_SC;
  3031. if(esp->dma_irq_entry)
  3032. esp->dma_irq_entry(esp);
  3033. /* Check for errors. */
  3034. esp->sreg = esp_read(eregs->esp_status);
  3035. esp->sreg &= (~ESP_STAT_INTR);
  3036. esp->seqreg = (esp_read(eregs->esp_sstep) & ESP_STEP_VBITS);
  3037. esp->ireg = esp_read(eregs->esp_intrpt); /* Unlatch intr and stat regs */
  3038. ESPIRQ(("handle_irq: [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  3039. esp->sreg, esp->seqreg, esp->ireg));
  3040. if(esp->sreg & (ESP_STAT_SPAM)) {
  3041. /* Gross error, could be due to one of:
  3042. *
  3043. * - top of fifo overwritten, could be because
  3044. * we tried to do a synchronous transfer with
  3045. * an offset greater than ESP fifo size
  3046. *
  3047. * - top of command register overwritten
  3048. *
  3049. * - DMA setup to go in one direction, SCSI
  3050. * bus points in the other, whoops
  3051. *
  3052. * - weird phase change during asynchronous
  3053. * data phase while we are initiator
  3054. */
  3055. ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
  3056. /* If a command is live on the bus we cannot safely
  3057. * reset the bus, so we'll just let the pieces fall
  3058. * where they may. Here we are hoping that the
  3059. * target will be able to cleanly go away soon
  3060. * so we can safely reset things.
  3061. */
  3062. if(!SCptr) {
  3063. ESPLOG(("esp%d: No current cmd during gross error, "
  3064. "resetting bus\n", esp->esp_id));
  3065. what_next = do_reset_bus;
  3066. goto state_machine;
  3067. }
  3068. }
  3069. /* No current cmd is only valid at this point when there are
  3070. * commands off the bus or we are trying a reset.
  3071. */
  3072. if(!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
  3073. /* Panic is safe, since current_SC is null. */
  3074. ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
  3075. panic("esp_handle: current_SC == penguin within interrupt!");
  3076. }
  3077. if(esp->ireg & (ESP_INTR_IC)) {
  3078. /* Illegal command fed to ESP. Outside of obvious
  3079. * software bugs that could cause this, there is
  3080. * a condition with ESP100 where we can confuse the
  3081. * ESP into an erroneous illegal command interrupt
  3082. * because it does not scrape the FIFO properly
  3083. * for reselection. See esp100_reconnect_hwbug()
  3084. * to see how we try very hard to avoid this.
  3085. */
  3086. ESPLOG(("esp%d: invalid command\n", esp->esp_id));
  3087. esp_dump_state(esp, eregs);
  3088. if(SCptr) {
  3089. /* Devices with very buggy firmware can drop BSY
  3090. * during a scatter list interrupt when using sync
  3091. * mode transfers. We continue the transfer as
  3092. * expected, the target drops the bus, the ESP
  3093. * gets confused, and we get a illegal command
  3094. * interrupt because the bus is in the disconnected
  3095. * state now and ESP_CMD_TI is only allowed when
  3096. * a nexus is alive on the bus.
  3097. */
  3098. ESPLOG(("esp%d: Forcing async and disabling disconnect for "
  3099. "target %d\n", esp->esp_id, SCptr->device->id));
  3100. SCptr->device->borken = 1; /* foo on you */
  3101. }
  3102. what_next = do_reset_bus;
  3103. } else if(!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
  3104. int phase;
  3105. if(SCptr) {
  3106. phase = SCptr->SCp.phase;
  3107. if(phase & in_phases_mask) {
  3108. what_next = esp_work_bus(esp, eregs);
  3109. } else if(phase & in_slct_mask) {
  3110. what_next = esp_select_complete(esp, eregs);
  3111. } else {
  3112. ESPLOG(("esp%d: interrupt for no good reason...\n",
  3113. esp->esp_id));
  3114. what_next = do_intr_end;
  3115. }
  3116. } else {
  3117. ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
  3118. esp->esp_id));
  3119. what_next = do_reset_bus;
  3120. }
  3121. } else if(esp->ireg & ESP_INTR_SR) {
  3122. ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
  3123. what_next = do_reset_complete;
  3124. } else if(esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
  3125. ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
  3126. esp->esp_id));
  3127. what_next = do_reset_bus;
  3128. } else if(esp->ireg & ESP_INTR_RSEL) {
  3129. if(!SCptr) {
  3130. /* This is ok. */
  3131. what_next = esp_do_reconnect(esp, eregs);
  3132. } else if(SCptr->SCp.phase & in_slct_mask) {
  3133. /* Only selection code knows how to clean
  3134. * up properly.
  3135. */
  3136. ESPDISC(("Reselected during selection attempt\n"));
  3137. what_next = esp_select_complete(esp, eregs);
  3138. } else {
  3139. ESPLOG(("esp%d: Reselected while bus is busy\n",
  3140. esp->esp_id));
  3141. what_next = do_reset_bus;
  3142. }
  3143. }
  3144. /* This is tier-one in our dual level SCSI state machine. */
  3145. state_machine:
  3146. while(what_next != do_intr_end) {
  3147. if (what_next >= do_phase_determine &&
  3148. what_next < do_intr_end)
  3149. what_next = isvc_vector[what_next](esp, eregs);
  3150. else {
  3151. /* state is completely lost ;-( */
  3152. ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
  3153. esp->esp_id));
  3154. what_next = do_reset_bus;
  3155. }
  3156. }
  3157. if(esp->dma_irq_exit)
  3158. esp->dma_irq_exit(esp);
  3159. }
  3160. #ifndef CONFIG_SMP
  3161. irqreturn_t esp_intr(int irq, void *dev_id)
  3162. {
  3163. struct NCR_ESP *esp;
  3164. unsigned long flags;
  3165. int again;
  3166. struct Scsi_Host *dev = dev_id;
  3167. /* Handle all ESP interrupts showing at this IRQ level. */
  3168. spin_lock_irqsave(dev->host_lock, flags);
  3169. repeat:
  3170. again = 0;
  3171. for_each_esp(esp) {
  3172. #ifndef __mips__
  3173. if(((esp)->irq & 0xff) == irq) {
  3174. #endif
  3175. if(esp->dma_irq_p(esp)) {
  3176. again = 1;
  3177. esp->dma_ints_off(esp);
  3178. ESPIRQ(("I%d(", esp->esp_id));
  3179. esp_handle(esp);
  3180. ESPIRQ((")"));
  3181. esp->dma_ints_on(esp);
  3182. }
  3183. #ifndef __mips__
  3184. }
  3185. #endif
  3186. }
  3187. if(again)
  3188. goto repeat;
  3189. spin_unlock_irqrestore(dev->host_lock, flags);
  3190. return IRQ_HANDLED;
  3191. }
  3192. #else
  3193. /* For SMP we only service one ESP on the list list at our IRQ level! */
  3194. irqreturn_t esp_intr(int irq, void *dev_id)
  3195. {
  3196. struct NCR_ESP *esp;
  3197. unsigned long flags;
  3198. struct Scsi_Host *dev = dev_id;
  3199. /* Handle all ESP interrupts showing at this IRQ level. */
  3200. spin_lock_irqsave(dev->host_lock, flags);
  3201. for_each_esp(esp) {
  3202. if(((esp)->irq & 0xf) == irq) {
  3203. if(esp->dma_irq_p(esp)) {
  3204. esp->dma_ints_off(esp);
  3205. ESPIRQ(("I[%d:%d](",
  3206. smp_processor_id(), esp->esp_id));
  3207. esp_handle(esp);
  3208. ESPIRQ((")"));
  3209. esp->dma_ints_on(esp);
  3210. goto out;
  3211. }
  3212. }
  3213. }
  3214. out:
  3215. spin_unlock_irqrestore(dev->host_lock, flags);
  3216. return IRQ_HANDLED;
  3217. }
  3218. #endif
  3219. int esp_slave_alloc(struct scsi_device *SDptr)
  3220. {
  3221. struct esp_device *esp_dev =
  3222. kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
  3223. if (!esp_dev)
  3224. return -ENOMEM;
  3225. memset(esp_dev, 0, sizeof(struct esp_device));
  3226. SDptr->hostdata = esp_dev;
  3227. return 0;
  3228. }
  3229. void esp_slave_destroy(struct scsi_device *SDptr)
  3230. {
  3231. struct NCR_ESP *esp = (struct NCR_ESP *) SDptr->host->hostdata;
  3232. esp->targets_present &= ~(1 << sdev_id(SDptr));
  3233. kfree(SDptr->hostdata);
  3234. SDptr->hostdata = NULL;
  3235. }
  3236. #ifdef MODULE
  3237. int init_module(void) { return 0; }
  3238. void cleanup_module(void) {}
  3239. void esp_release(void)
  3240. {
  3241. esps_in_use--;
  3242. esps_running = esps_in_use;
  3243. }
  3244. #endif
  3245. EXPORT_SYMBOL(esp_abort);
  3246. EXPORT_SYMBOL(esp_allocate);
  3247. EXPORT_SYMBOL(esp_deallocate);
  3248. EXPORT_SYMBOL(esp_initialize);
  3249. EXPORT_SYMBOL(esp_intr);
  3250. EXPORT_SYMBOL(esp_queue);
  3251. EXPORT_SYMBOL(esp_reset);
  3252. EXPORT_SYMBOL(esp_slave_alloc);
  3253. EXPORT_SYMBOL(esp_slave_destroy);
  3254. EXPORT_SYMBOL(esps_in_use);
  3255. MODULE_LICENSE("GPL");