NCR53C9x.c 102 KB

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