NCR53C9x.c 102 KB

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