NCR53C9x.c 102 KB

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