esp.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394
  1. /* esp.c: ESP Sun SCSI driver.
  2. *
  3. * Copyright (C) 1995, 1998, 2006 David S. Miller (davem@davemloft.net)
  4. */
  5. /* TODO:
  6. *
  7. * 1) Maybe disable parity checking in config register one for SCSI1
  8. * targets. (Gilmore says parity error on the SBus can lock up
  9. * old sun4c's)
  10. * 2) Add support for DMA2 pipelining.
  11. * 3) Add tagged queueing.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/delay.h>
  15. #include <linux/types.h>
  16. #include <linux/string.h>
  17. #include <linux/slab.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/stat.h>
  21. #include <linux/init.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include "esp.h"
  26. #include <asm/sbus.h>
  27. #include <asm/dma.h>
  28. #include <asm/system.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/oplib.h>
  32. #include <asm/io.h>
  33. #include <asm/irq.h>
  34. #ifndef __sparc_v9__
  35. #include <asm/machines.h>
  36. #include <asm/idprom.h>
  37. #endif
  38. #include <scsi/scsi.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_eh.h>
  42. #include <scsi/scsi_host.h>
  43. #include <scsi/scsi_tcq.h>
  44. #define DRV_VERSION "1.101"
  45. #define DEBUG_ESP
  46. /* #define DEBUG_ESP_HME */
  47. /* #define DEBUG_ESP_DATA */
  48. /* #define DEBUG_ESP_QUEUE */
  49. /* #define DEBUG_ESP_DISCONNECT */
  50. /* #define DEBUG_ESP_STATUS */
  51. /* #define DEBUG_ESP_PHASES */
  52. /* #define DEBUG_ESP_WORKBUS */
  53. /* #define DEBUG_STATE_MACHINE */
  54. /* #define DEBUG_ESP_CMDS */
  55. /* #define DEBUG_ESP_IRQS */
  56. /* #define DEBUG_SDTR */
  57. /* #define DEBUG_ESP_SG */
  58. /* Use the following to sprinkle debugging messages in a way which
  59. * suits you if combinations of the above become too verbose when
  60. * trying to track down a specific problem.
  61. */
  62. /* #define DEBUG_ESP_MISC */
  63. #if defined(DEBUG_ESP)
  64. #define ESPLOG(foo) printk foo
  65. #else
  66. #define ESPLOG(foo)
  67. #endif /* (DEBUG_ESP) */
  68. #if defined(DEBUG_ESP_HME)
  69. #define ESPHME(foo) printk foo
  70. #else
  71. #define ESPHME(foo)
  72. #endif
  73. #if defined(DEBUG_ESP_DATA)
  74. #define ESPDATA(foo) printk foo
  75. #else
  76. #define ESPDATA(foo)
  77. #endif
  78. #if defined(DEBUG_ESP_QUEUE)
  79. #define ESPQUEUE(foo) printk foo
  80. #else
  81. #define ESPQUEUE(foo)
  82. #endif
  83. #if defined(DEBUG_ESP_DISCONNECT)
  84. #define ESPDISC(foo) printk foo
  85. #else
  86. #define ESPDISC(foo)
  87. #endif
  88. #if defined(DEBUG_ESP_STATUS)
  89. #define ESPSTAT(foo) printk foo
  90. #else
  91. #define ESPSTAT(foo)
  92. #endif
  93. #if defined(DEBUG_ESP_PHASES)
  94. #define ESPPHASE(foo) printk foo
  95. #else
  96. #define ESPPHASE(foo)
  97. #endif
  98. #if defined(DEBUG_ESP_WORKBUS)
  99. #define ESPBUS(foo) printk foo
  100. #else
  101. #define ESPBUS(foo)
  102. #endif
  103. #if defined(DEBUG_ESP_IRQS)
  104. #define ESPIRQ(foo) printk foo
  105. #else
  106. #define ESPIRQ(foo)
  107. #endif
  108. #if defined(DEBUG_SDTR)
  109. #define ESPSDTR(foo) printk foo
  110. #else
  111. #define ESPSDTR(foo)
  112. #endif
  113. #if defined(DEBUG_ESP_MISC)
  114. #define ESPMISC(foo) printk foo
  115. #else
  116. #define ESPMISC(foo)
  117. #endif
  118. /* Command phase enumeration. */
  119. enum {
  120. not_issued = 0x00, /* Still in the issue_SC queue. */
  121. /* Various forms of selecting a target. */
  122. #define in_slct_mask 0x10
  123. in_slct_norm = 0x10, /* ESP is arbitrating, normal selection */
  124. in_slct_stop = 0x11, /* ESP will select, then stop with IRQ */
  125. in_slct_msg = 0x12, /* select, then send a message */
  126. in_slct_tag = 0x13, /* select and send tagged queue msg */
  127. in_slct_sneg = 0x14, /* select and acquire sync capabilities */
  128. /* Any post selection activity. */
  129. #define in_phases_mask 0x20
  130. in_datain = 0x20, /* Data is transferring from the bus */
  131. in_dataout = 0x21, /* Data is transferring to the bus */
  132. in_data_done = 0x22, /* Last DMA data operation done (maybe) */
  133. in_msgin = 0x23, /* Eating message from target */
  134. in_msgincont = 0x24, /* Eating more msg bytes from target */
  135. in_msgindone = 0x25, /* Decide what to do with what we got */
  136. in_msgout = 0x26, /* Sending message to target */
  137. in_msgoutdone = 0x27, /* Done sending msg out */
  138. in_cmdbegin = 0x28, /* Sending cmd after abnormal selection */
  139. in_cmdend = 0x29, /* Done sending slow cmd */
  140. in_status = 0x2a, /* Was in status phase, finishing cmd */
  141. in_freeing = 0x2b, /* freeing the bus for cmd cmplt or disc */
  142. in_the_dark = 0x2c, /* Don't know what bus phase we are in */
  143. /* Special states, ie. not normal bus transitions... */
  144. #define in_spec_mask 0x80
  145. in_abortone = 0x80, /* Aborting one command currently */
  146. in_abortall = 0x81, /* Blowing away all commands we have */
  147. in_resetdev = 0x82, /* SCSI target reset in progress */
  148. in_resetbus = 0x83, /* SCSI bus reset in progress */
  149. in_tgterror = 0x84, /* Target did something stupid */
  150. };
  151. enum {
  152. /* Zero has special meaning, see skipahead[12]. */
  153. /*0*/ do_never,
  154. /*1*/ do_phase_determine,
  155. /*2*/ do_reset_bus,
  156. /*3*/ do_reset_complete,
  157. /*4*/ do_work_bus,
  158. /*5*/ do_intr_end
  159. };
  160. /* Forward declarations. */
  161. static irqreturn_t esp_intr(int irq, void *dev_id);
  162. /* Debugging routines */
  163. struct esp_cmdstrings {
  164. u8 cmdchar;
  165. char *text;
  166. } esp_cmd_strings[] = {
  167. /* Miscellaneous */
  168. { ESP_CMD_NULL, "ESP_NOP", },
  169. { ESP_CMD_FLUSH, "FIFO_FLUSH", },
  170. { ESP_CMD_RC, "RSTESP", },
  171. { ESP_CMD_RS, "RSTSCSI", },
  172. /* Disconnected State Group */
  173. { ESP_CMD_RSEL, "RESLCTSEQ", },
  174. { ESP_CMD_SEL, "SLCTNATN", },
  175. { ESP_CMD_SELA, "SLCTATN", },
  176. { ESP_CMD_SELAS, "SLCTATNSTOP", },
  177. { ESP_CMD_ESEL, "ENSLCTRESEL", },
  178. { ESP_CMD_DSEL, "DISSELRESEL", },
  179. { ESP_CMD_SA3, "SLCTATN3", },
  180. { ESP_CMD_RSEL3, "RESLCTSEQ", },
  181. /* Target State Group */
  182. { ESP_CMD_SMSG, "SNDMSG", },
  183. { ESP_CMD_SSTAT, "SNDSTATUS", },
  184. { ESP_CMD_SDATA, "SNDDATA", },
  185. { ESP_CMD_DSEQ, "DISCSEQ", },
  186. { ESP_CMD_TSEQ, "TERMSEQ", },
  187. { ESP_CMD_TCCSEQ, "TRGTCMDCOMPSEQ", },
  188. { ESP_CMD_DCNCT, "DISC", },
  189. { ESP_CMD_RMSG, "RCVMSG", },
  190. { ESP_CMD_RCMD, "RCVCMD", },
  191. { ESP_CMD_RDATA, "RCVDATA", },
  192. { ESP_CMD_RCSEQ, "RCVCMDSEQ", },
  193. /* Initiator State Group */
  194. { ESP_CMD_TI, "TRANSINFO", },
  195. { ESP_CMD_ICCSEQ, "INICMDSEQCOMP", },
  196. { ESP_CMD_MOK, "MSGACCEPTED", },
  197. { ESP_CMD_TPAD, "TPAD", },
  198. { ESP_CMD_SATN, "SATN", },
  199. { ESP_CMD_RATN, "RATN", },
  200. };
  201. #define NUM_ESP_COMMANDS ((sizeof(esp_cmd_strings)) / (sizeof(struct esp_cmdstrings)))
  202. /* Print textual representation of an ESP command */
  203. static inline void esp_print_cmd(u8 espcmd)
  204. {
  205. u8 dma_bit = espcmd & ESP_CMD_DMA;
  206. int i;
  207. espcmd &= ~dma_bit;
  208. for (i = 0; i < NUM_ESP_COMMANDS; i++)
  209. if (esp_cmd_strings[i].cmdchar == espcmd)
  210. break;
  211. if (i == NUM_ESP_COMMANDS)
  212. printk("ESP_Unknown");
  213. else
  214. printk("%s%s", esp_cmd_strings[i].text,
  215. ((dma_bit) ? "+DMA" : ""));
  216. }
  217. /* Print the status register's value */
  218. static inline void esp_print_statreg(u8 statreg)
  219. {
  220. u8 phase;
  221. printk("STATUS<");
  222. phase = statreg & ESP_STAT_PMASK;
  223. printk("%s,", (phase == ESP_DOP ? "DATA-OUT" :
  224. (phase == ESP_DIP ? "DATA-IN" :
  225. (phase == ESP_CMDP ? "COMMAND" :
  226. (phase == ESP_STATP ? "STATUS" :
  227. (phase == ESP_MOP ? "MSG-OUT" :
  228. (phase == ESP_MIP ? "MSG_IN" :
  229. "unknown")))))));
  230. if (statreg & ESP_STAT_TDONE)
  231. printk("TRANS_DONE,");
  232. if (statreg & ESP_STAT_TCNT)
  233. printk("TCOUNT_ZERO,");
  234. if (statreg & ESP_STAT_PERR)
  235. printk("P_ERROR,");
  236. if (statreg & ESP_STAT_SPAM)
  237. printk("SPAM,");
  238. if (statreg & ESP_STAT_INTR)
  239. printk("IRQ,");
  240. printk(">");
  241. }
  242. /* Print the interrupt register's value */
  243. static inline void esp_print_ireg(u8 intreg)
  244. {
  245. printk("INTREG< ");
  246. if (intreg & ESP_INTR_S)
  247. printk("SLCT_NATN ");
  248. if (intreg & ESP_INTR_SATN)
  249. printk("SLCT_ATN ");
  250. if (intreg & ESP_INTR_RSEL)
  251. printk("RSLCT ");
  252. if (intreg & ESP_INTR_FDONE)
  253. printk("FDONE ");
  254. if (intreg & ESP_INTR_BSERV)
  255. printk("BSERV ");
  256. if (intreg & ESP_INTR_DC)
  257. printk("DISCNCT ");
  258. if (intreg & ESP_INTR_IC)
  259. printk("ILL_CMD ");
  260. if (intreg & ESP_INTR_SR)
  261. printk("SCSI_BUS_RESET ");
  262. printk(">");
  263. }
  264. /* Print the sequence step registers contents */
  265. static inline void esp_print_seqreg(u8 stepreg)
  266. {
  267. stepreg &= ESP_STEP_VBITS;
  268. printk("STEP<%s>",
  269. (stepreg == ESP_STEP_ASEL ? "SLCT_ARB_CMPLT" :
  270. (stepreg == ESP_STEP_SID ? "1BYTE_MSG_SENT" :
  271. (stepreg == ESP_STEP_NCMD ? "NOT_IN_CMD_PHASE" :
  272. (stepreg == ESP_STEP_PPC ? "CMD_BYTES_LOST" :
  273. (stepreg == ESP_STEP_FINI4 ? "CMD_SENT_OK" :
  274. "UNKNOWN"))))));
  275. }
  276. static char *phase_string(int phase)
  277. {
  278. switch (phase) {
  279. case not_issued:
  280. return "UNISSUED";
  281. case in_slct_norm:
  282. return "SLCTNORM";
  283. case in_slct_stop:
  284. return "SLCTSTOP";
  285. case in_slct_msg:
  286. return "SLCTMSG";
  287. case in_slct_tag:
  288. return "SLCTTAG";
  289. case in_slct_sneg:
  290. return "SLCTSNEG";
  291. case in_datain:
  292. return "DATAIN";
  293. case in_dataout:
  294. return "DATAOUT";
  295. case in_data_done:
  296. return "DATADONE";
  297. case in_msgin:
  298. return "MSGIN";
  299. case in_msgincont:
  300. return "MSGINCONT";
  301. case in_msgindone:
  302. return "MSGINDONE";
  303. case in_msgout:
  304. return "MSGOUT";
  305. case in_msgoutdone:
  306. return "MSGOUTDONE";
  307. case in_cmdbegin:
  308. return "CMDBEGIN";
  309. case in_cmdend:
  310. return "CMDEND";
  311. case in_status:
  312. return "STATUS";
  313. case in_freeing:
  314. return "FREEING";
  315. case in_the_dark:
  316. return "CLUELESS";
  317. case in_abortone:
  318. return "ABORTONE";
  319. case in_abortall:
  320. return "ABORTALL";
  321. case in_resetdev:
  322. return "RESETDEV";
  323. case in_resetbus:
  324. return "RESETBUS";
  325. case in_tgterror:
  326. return "TGTERROR";
  327. default:
  328. return "UNKNOWN";
  329. };
  330. }
  331. #ifdef DEBUG_STATE_MACHINE
  332. static inline void esp_advance_phase(struct scsi_cmnd *s, int newphase)
  333. {
  334. ESPLOG(("<%s>", phase_string(newphase)));
  335. s->SCp.sent_command = s->SCp.phase;
  336. s->SCp.phase = newphase;
  337. }
  338. #else
  339. #define esp_advance_phase(__s, __newphase) \
  340. (__s)->SCp.sent_command = (__s)->SCp.phase; \
  341. (__s)->SCp.phase = (__newphase);
  342. #endif
  343. #ifdef DEBUG_ESP_CMDS
  344. static inline void esp_cmd(struct esp *esp, u8 cmd)
  345. {
  346. esp->espcmdlog[esp->espcmdent] = cmd;
  347. esp->espcmdent = (esp->espcmdent + 1) & 31;
  348. sbus_writeb(cmd, esp->eregs + ESP_CMD);
  349. }
  350. #else
  351. #define esp_cmd(__esp, __cmd) \
  352. sbus_writeb((__cmd), ((__esp)->eregs) + ESP_CMD)
  353. #endif
  354. #define ESP_INTSOFF(__dregs) \
  355. sbus_writel(sbus_readl((__dregs)+DMA_CSR)&~(DMA_INT_ENAB), (__dregs)+DMA_CSR)
  356. #define ESP_INTSON(__dregs) \
  357. sbus_writel(sbus_readl((__dregs)+DMA_CSR)|DMA_INT_ENAB, (__dregs)+DMA_CSR)
  358. #define ESP_IRQ_P(__dregs) \
  359. (sbus_readl((__dregs)+DMA_CSR) & (DMA_HNDL_INTR|DMA_HNDL_ERROR))
  360. /* How we use the various Linux SCSI data structures for operation.
  361. *
  362. * struct scsi_cmnd:
  363. *
  364. * We keep track of the synchronous capabilities of a target
  365. * in the device member, using sync_min_period and
  366. * sync_max_offset. These are the values we directly write
  367. * into the ESP registers while running a command. If offset
  368. * is zero the ESP will use asynchronous transfers.
  369. * If the borken flag is set we assume we shouldn't even bother
  370. * trying to negotiate for synchronous transfer as this target
  371. * is really stupid. If we notice the target is dropping the
  372. * bus, and we have been allowing it to disconnect, we clear
  373. * the disconnect flag.
  374. */
  375. /* Manipulation of the ESP command queues. Thanks to the aha152x driver
  376. * and its author, Juergen E. Fischer, for the methods used here.
  377. * Note that these are per-ESP queues, not global queues like
  378. * the aha152x driver uses.
  379. */
  380. static inline void append_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
  381. {
  382. struct scsi_cmnd *end;
  383. new_SC->host_scribble = (unsigned char *) NULL;
  384. if (!*SC)
  385. *SC = new_SC;
  386. else {
  387. for (end=*SC;end->host_scribble;end=(struct scsi_cmnd *)end->host_scribble)
  388. ;
  389. end->host_scribble = (unsigned char *) new_SC;
  390. }
  391. }
  392. static inline void prepend_SC(struct scsi_cmnd **SC, struct scsi_cmnd *new_SC)
  393. {
  394. new_SC->host_scribble = (unsigned char *) *SC;
  395. *SC = new_SC;
  396. }
  397. static inline struct scsi_cmnd *remove_first_SC(struct scsi_cmnd **SC)
  398. {
  399. struct scsi_cmnd *ptr;
  400. ptr = *SC;
  401. if (ptr)
  402. *SC = (struct scsi_cmnd *) (*SC)->host_scribble;
  403. return ptr;
  404. }
  405. static inline struct scsi_cmnd *remove_SC(struct scsi_cmnd **SC, int target, int lun)
  406. {
  407. struct scsi_cmnd *ptr, *prev;
  408. for (ptr = *SC, prev = NULL;
  409. ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
  410. prev = ptr, ptr = (struct scsi_cmnd *) ptr->host_scribble)
  411. ;
  412. if (ptr) {
  413. if (prev)
  414. prev->host_scribble=ptr->host_scribble;
  415. else
  416. *SC=(struct scsi_cmnd *)ptr->host_scribble;
  417. }
  418. return ptr;
  419. }
  420. /* Resetting various pieces of the ESP scsi driver chipset/buses. */
  421. static void esp_reset_dma(struct esp *esp)
  422. {
  423. int can_do_burst16, can_do_burst32, can_do_burst64;
  424. int can_do_sbus64;
  425. u32 tmp;
  426. can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
  427. can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
  428. can_do_burst64 = 0;
  429. can_do_sbus64 = 0;
  430. if (sbus_can_dma_64bit(esp->sdev))
  431. can_do_sbus64 = 1;
  432. if (sbus_can_burst64(esp->sdev))
  433. can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
  434. /* Punt the DVMA into a known state. */
  435. if (esp->dma->revision != dvmahme) {
  436. tmp = sbus_readl(esp->dregs + DMA_CSR);
  437. sbus_writel(tmp | DMA_RST_SCSI, esp->dregs + DMA_CSR);
  438. sbus_writel(tmp & ~DMA_RST_SCSI, esp->dregs + DMA_CSR);
  439. }
  440. switch (esp->dma->revision) {
  441. case dvmahme:
  442. /* This is the HME DVMA gate array. */
  443. sbus_writel(DMA_RESET_FAS366, esp->dregs + DMA_CSR);
  444. sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
  445. esp->prev_hme_dmacsr = (DMA_PARITY_OFF|DMA_2CLKS|DMA_SCSI_DISAB|DMA_INT_ENAB);
  446. esp->prev_hme_dmacsr &= ~(DMA_ENABLE|DMA_ST_WRITE|DMA_BRST_SZ);
  447. if (can_do_burst64)
  448. esp->prev_hme_dmacsr |= DMA_BRST64;
  449. else if (can_do_burst32)
  450. esp->prev_hme_dmacsr |= DMA_BRST32;
  451. if (can_do_sbus64) {
  452. esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
  453. sbus_set_sbus64(esp->sdev, esp->bursts);
  454. }
  455. /* This chip is horrible. */
  456. while (sbus_readl(esp->dregs + DMA_CSR) & DMA_PEND_READ)
  457. udelay(1);
  458. sbus_writel(0, esp->dregs + DMA_CSR);
  459. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  460. /* This is necessary to avoid having the SCSI channel
  461. * engine lock up on us.
  462. */
  463. sbus_writel(0, esp->dregs + DMA_ADDR);
  464. break;
  465. case dvmarev2:
  466. /* This is the gate array found in the sun4m
  467. * NCR SBUS I/O subsystem.
  468. */
  469. if (esp->erev != esp100) {
  470. tmp = sbus_readl(esp->dregs + DMA_CSR);
  471. sbus_writel(tmp | DMA_3CLKS, esp->dregs + DMA_CSR);
  472. }
  473. break;
  474. case dvmarev3:
  475. tmp = sbus_readl(esp->dregs + DMA_CSR);
  476. tmp &= ~DMA_3CLKS;
  477. tmp |= DMA_2CLKS;
  478. if (can_do_burst32) {
  479. tmp &= ~DMA_BRST_SZ;
  480. tmp |= DMA_BRST32;
  481. }
  482. sbus_writel(tmp, esp->dregs + DMA_CSR);
  483. break;
  484. case dvmaesc1:
  485. /* This is the DMA unit found on SCSI/Ether cards. */
  486. tmp = sbus_readl(esp->dregs + DMA_CSR);
  487. tmp |= DMA_ADD_ENABLE;
  488. tmp &= ~DMA_BCNT_ENAB;
  489. if (!can_do_burst32 && can_do_burst16) {
  490. tmp |= DMA_ESC_BURST;
  491. } else {
  492. tmp &= ~(DMA_ESC_BURST);
  493. }
  494. sbus_writel(tmp, esp->dregs + DMA_CSR);
  495. break;
  496. default:
  497. break;
  498. };
  499. ESP_INTSON(esp->dregs);
  500. }
  501. /* Reset the ESP chip, _not_ the SCSI bus. */
  502. static void __init esp_reset_esp(struct esp *esp)
  503. {
  504. u8 family_code, version;
  505. int i;
  506. /* Now reset the ESP chip */
  507. esp_cmd(esp, ESP_CMD_RC);
  508. esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  509. esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  510. /* Reload the configuration registers */
  511. sbus_writeb(esp->cfact, esp->eregs + ESP_CFACT);
  512. esp->prev_stp = 0;
  513. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  514. esp->prev_soff = 0;
  515. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  516. sbus_writeb(esp->neg_defp, esp->eregs + ESP_TIMEO);
  517. /* This is the only point at which it is reliable to read
  518. * the ID-code for a fast ESP chip variants.
  519. */
  520. esp->max_period = ((35 * esp->ccycle) / 1000);
  521. if (esp->erev == fast) {
  522. version = sbus_readb(esp->eregs + ESP_UID);
  523. family_code = (version & 0xf8) >> 3;
  524. if (family_code == 0x02)
  525. esp->erev = fas236;
  526. else if (family_code == 0x0a)
  527. esp->erev = fashme; /* Version is usually '5'. */
  528. else
  529. esp->erev = fas100a;
  530. ESPMISC(("esp%d: FAST chip is %s (family=%d, version=%d)\n",
  531. esp->esp_id,
  532. (esp->erev == fas236) ? "fas236" :
  533. ((esp->erev == fas100a) ? "fas100a" :
  534. "fasHME"), family_code, (version & 7)));
  535. esp->min_period = ((4 * esp->ccycle) / 1000);
  536. } else {
  537. esp->min_period = ((5 * esp->ccycle) / 1000);
  538. }
  539. esp->max_period = (esp->max_period + 3)>>2;
  540. esp->min_period = (esp->min_period + 3)>>2;
  541. sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
  542. switch (esp->erev) {
  543. case esp100:
  544. /* nothing to do */
  545. break;
  546. case esp100a:
  547. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  548. break;
  549. case esp236:
  550. /* Slow 236 */
  551. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  552. esp->prev_cfg3 = esp->config3[0];
  553. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  554. break;
  555. case fashme:
  556. esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
  557. /* fallthrough... */
  558. case fas236:
  559. /* Fast 236 or HME */
  560. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  561. for (i = 0; i < 16; i++) {
  562. if (esp->erev == fashme) {
  563. u8 cfg3;
  564. cfg3 = ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
  565. if (esp->scsi_id >= 8)
  566. cfg3 |= ESP_CONFIG3_IDBIT3;
  567. esp->config3[i] |= cfg3;
  568. } else {
  569. esp->config3[i] |= ESP_CONFIG3_FCLK;
  570. }
  571. }
  572. esp->prev_cfg3 = esp->config3[0];
  573. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  574. if (esp->erev == fashme) {
  575. esp->radelay = 80;
  576. } else {
  577. if (esp->diff)
  578. esp->radelay = 0;
  579. else
  580. esp->radelay = 96;
  581. }
  582. break;
  583. case fas100a:
  584. /* Fast 100a */
  585. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  586. for (i = 0; i < 16; i++)
  587. esp->config3[i] |= ESP_CONFIG3_FCLOCK;
  588. esp->prev_cfg3 = esp->config3[0];
  589. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  590. esp->radelay = 32;
  591. break;
  592. default:
  593. panic("esp: what could it be... I wonder...");
  594. break;
  595. };
  596. /* Eat any bitrot in the chip */
  597. sbus_readb(esp->eregs + ESP_INTRPT);
  598. udelay(100);
  599. }
  600. /* This places the ESP into a known state at boot time. */
  601. static void __init esp_bootup_reset(struct esp *esp)
  602. {
  603. u8 tmp;
  604. /* Reset the DMA */
  605. esp_reset_dma(esp);
  606. /* Reset the ESP */
  607. esp_reset_esp(esp);
  608. /* Reset the SCSI bus, but tell ESP not to generate an irq */
  609. tmp = sbus_readb(esp->eregs + ESP_CFG1);
  610. tmp |= ESP_CONFIG1_SRRDISAB;
  611. sbus_writeb(tmp, esp->eregs + ESP_CFG1);
  612. esp_cmd(esp, ESP_CMD_RS);
  613. udelay(400);
  614. sbus_writeb(esp->config1, esp->eregs + ESP_CFG1);
  615. /* Eat any bitrot in the chip and we are done... */
  616. sbus_readb(esp->eregs + ESP_INTRPT);
  617. }
  618. static int __init esp_find_dvma(struct esp *esp, struct sbus_dev *dma_sdev)
  619. {
  620. struct sbus_dev *sdev = esp->sdev;
  621. struct sbus_dma *dma;
  622. if (dma_sdev != NULL) {
  623. for_each_dvma(dma) {
  624. if (dma->sdev == dma_sdev)
  625. break;
  626. }
  627. } else {
  628. for_each_dvma(dma) {
  629. /* If allocated already, can't use it. */
  630. if (dma->allocated)
  631. continue;
  632. if (dma->sdev == NULL)
  633. break;
  634. /* If bus + slot are the same and it has the
  635. * correct OBP name, it's ours.
  636. */
  637. if (sdev->bus == dma->sdev->bus &&
  638. sdev->slot == dma->sdev->slot &&
  639. (!strcmp(dma->sdev->prom_name, "dma") ||
  640. !strcmp(dma->sdev->prom_name, "espdma")))
  641. break;
  642. }
  643. }
  644. /* If we don't know how to handle the dvma,
  645. * do not use this device.
  646. */
  647. if (dma == NULL) {
  648. printk("Cannot find dvma for ESP%d's SCSI\n", esp->esp_id);
  649. return -1;
  650. }
  651. if (dma->allocated) {
  652. printk("esp%d: can't use my espdma\n", esp->esp_id);
  653. return -1;
  654. }
  655. dma->allocated = 1;
  656. esp->dma = dma;
  657. esp->dregs = dma->regs;
  658. return 0;
  659. }
  660. static int __init esp_map_regs(struct esp *esp, int hme)
  661. {
  662. struct sbus_dev *sdev = esp->sdev;
  663. struct resource *res;
  664. /* On HME, two reg sets exist, first is DVMA,
  665. * second is ESP registers.
  666. */
  667. if (hme)
  668. res = &sdev->resource[1];
  669. else
  670. res = &sdev->resource[0];
  671. esp->eregs = sbus_ioremap(res, 0, ESP_REG_SIZE, "ESP Registers");
  672. if (esp->eregs == 0)
  673. return -1;
  674. return 0;
  675. }
  676. static int __init esp_map_cmdarea(struct esp *esp)
  677. {
  678. struct sbus_dev *sdev = esp->sdev;
  679. esp->esp_command = sbus_alloc_consistent(sdev, 16,
  680. &esp->esp_command_dvma);
  681. if (esp->esp_command == NULL ||
  682. esp->esp_command_dvma == 0)
  683. return -1;
  684. return 0;
  685. }
  686. static int __init esp_register_irq(struct esp *esp)
  687. {
  688. esp->ehost->irq = esp->irq = esp->sdev->irqs[0];
  689. /* We used to try various overly-clever things to
  690. * reduce the interrupt processing overhead on
  691. * sun4c/sun4m when multiple ESP's shared the
  692. * same IRQ. It was too complex and messy to
  693. * sanely maintain.
  694. */
  695. if (request_irq(esp->ehost->irq, esp_intr,
  696. IRQF_SHARED, "ESP SCSI", esp)) {
  697. printk("esp%d: Cannot acquire irq line\n",
  698. esp->esp_id);
  699. return -1;
  700. }
  701. printk("esp%d: IRQ %d ", esp->esp_id,
  702. esp->ehost->irq);
  703. return 0;
  704. }
  705. static void __init esp_get_scsi_id(struct esp *esp)
  706. {
  707. struct sbus_dev *sdev = esp->sdev;
  708. struct device_node *dp = sdev->ofdev.node;
  709. esp->scsi_id = of_getintprop_default(dp,
  710. "initiator-id",
  711. -1);
  712. if (esp->scsi_id == -1)
  713. esp->scsi_id = of_getintprop_default(dp,
  714. "scsi-initiator-id",
  715. -1);
  716. if (esp->scsi_id == -1)
  717. esp->scsi_id = (sdev->bus == NULL) ? 7 :
  718. of_getintprop_default(sdev->bus->ofdev.node,
  719. "scsi-initiator-id",
  720. 7);
  721. esp->ehost->this_id = esp->scsi_id;
  722. esp->scsi_id_mask = (1 << esp->scsi_id);
  723. }
  724. static void __init esp_get_clock_params(struct esp *esp)
  725. {
  726. struct sbus_dev *sdev = esp->sdev;
  727. int prom_node = esp->prom_node;
  728. int sbus_prom_node;
  729. unsigned int fmhz;
  730. u8 ccf;
  731. if (sdev != NULL && sdev->bus != NULL)
  732. sbus_prom_node = sdev->bus->prom_node;
  733. else
  734. sbus_prom_node = 0;
  735. /* This is getting messy but it has to be done
  736. * correctly or else you get weird behavior all
  737. * over the place. We are trying to basically
  738. * figure out three pieces of information.
  739. *
  740. * a) Clock Conversion Factor
  741. *
  742. * This is a representation of the input
  743. * crystal clock frequency going into the
  744. * ESP on this machine. Any operation whose
  745. * timing is longer than 400ns depends on this
  746. * value being correct. For example, you'll
  747. * get blips for arbitration/selection during
  748. * high load or with multiple targets if this
  749. * is not set correctly.
  750. *
  751. * b) Selection Time-Out
  752. *
  753. * The ESP isn't very bright and will arbitrate
  754. * for the bus and try to select a target
  755. * forever if you let it. This value tells
  756. * the ESP when it has taken too long to
  757. * negotiate and that it should interrupt
  758. * the CPU so we can see what happened.
  759. * The value is computed as follows (from
  760. * NCR/Symbios chip docs).
  761. *
  762. * (Time Out Period) * (Input Clock)
  763. * STO = ----------------------------------
  764. * (8192) * (Clock Conversion Factor)
  765. *
  766. * You usually want the time out period to be
  767. * around 250ms, I think we'll set it a little
  768. * bit higher to account for fully loaded SCSI
  769. * bus's and slow devices that don't respond so
  770. * quickly to selection attempts. (yeah, I know
  771. * this is out of spec. but there is a lot of
  772. * buggy pieces of firmware out there so bite me)
  773. *
  774. * c) Imperical constants for synchronous offset
  775. * and transfer period register values
  776. *
  777. * This entails the smallest and largest sync
  778. * period we could ever handle on this ESP.
  779. */
  780. fmhz = prom_getintdefault(prom_node, "clock-frequency", -1);
  781. if (fmhz == -1)
  782. fmhz = (!sbus_prom_node) ? 0 :
  783. prom_getintdefault(sbus_prom_node, "clock-frequency", -1);
  784. if (fmhz <= (5000000))
  785. ccf = 0;
  786. else
  787. ccf = (((5000000 - 1) + (fmhz))/(5000000));
  788. if (!ccf || ccf > 8) {
  789. /* If we can't find anything reasonable,
  790. * just assume 20MHZ. This is the clock
  791. * frequency of the older sun4c's where I've
  792. * been unable to find the clock-frequency
  793. * PROM property. All other machines provide
  794. * useful values it seems.
  795. */
  796. ccf = ESP_CCF_F4;
  797. fmhz = (20000000);
  798. }
  799. if (ccf == (ESP_CCF_F7 + 1))
  800. esp->cfact = ESP_CCF_F0;
  801. else if (ccf == ESP_CCF_NEVER)
  802. esp->cfact = ESP_CCF_F2;
  803. else
  804. esp->cfact = ccf;
  805. esp->raw_cfact = ccf;
  806. esp->cfreq = fmhz;
  807. esp->ccycle = ESP_MHZ_TO_CYCLE(fmhz);
  808. esp->ctick = ESP_TICK(ccf, esp->ccycle);
  809. esp->neg_defp = ESP_NEG_DEFP(fmhz, ccf);
  810. esp->sync_defp = SYNC_DEFP_SLOW;
  811. printk("SCSI ID %d Clk %dMHz CCYC=%d CCF=%d TOut %d ",
  812. esp->scsi_id, (fmhz / 1000000),
  813. (int)esp->ccycle, (int)ccf, (int) esp->neg_defp);
  814. }
  815. static void __init esp_get_bursts(struct esp *esp, struct sbus_dev *dma)
  816. {
  817. struct sbus_dev *sdev = esp->sdev;
  818. u8 bursts;
  819. bursts = prom_getintdefault(esp->prom_node, "burst-sizes", 0xff);
  820. if (dma) {
  821. u8 tmp = prom_getintdefault(dma->prom_node,
  822. "burst-sizes", 0xff);
  823. if (tmp != 0xff)
  824. bursts &= tmp;
  825. }
  826. if (sdev->bus) {
  827. u8 tmp = prom_getintdefault(sdev->bus->prom_node,
  828. "burst-sizes", 0xff);
  829. if (tmp != 0xff)
  830. bursts &= tmp;
  831. }
  832. if (bursts == 0xff ||
  833. (bursts & DMA_BURST16) == 0 ||
  834. (bursts & DMA_BURST32) == 0)
  835. bursts = (DMA_BURST32 - 1);
  836. esp->bursts = bursts;
  837. }
  838. static void __init esp_get_revision(struct esp *esp)
  839. {
  840. u8 tmp;
  841. esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
  842. esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
  843. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  844. tmp = sbus_readb(esp->eregs + ESP_CFG2);
  845. tmp &= ~ESP_CONFIG2_MAGIC;
  846. if (tmp != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
  847. /* If what we write to cfg2 does not come back, cfg2
  848. * is not implemented, therefore this must be a plain
  849. * esp100.
  850. */
  851. esp->erev = esp100;
  852. printk("NCR53C90(esp100)\n");
  853. } else {
  854. esp->config2 = 0;
  855. esp->prev_cfg3 = esp->config3[0] = 5;
  856. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  857. sbus_writeb(0, esp->eregs + ESP_CFG3);
  858. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  859. tmp = sbus_readb(esp->eregs + ESP_CFG3);
  860. if (tmp != 5) {
  861. /* The cfg2 register is implemented, however
  862. * cfg3 is not, must be esp100a.
  863. */
  864. esp->erev = esp100a;
  865. printk("NCR53C90A(esp100a)\n");
  866. } else {
  867. int target;
  868. for (target = 0; target < 16; target++)
  869. esp->config3[target] = 0;
  870. esp->prev_cfg3 = 0;
  871. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  872. /* All of cfg{1,2,3} implemented, must be one of
  873. * the fas variants, figure out which one.
  874. */
  875. if (esp->raw_cfact > ESP_CCF_F5) {
  876. esp->erev = fast;
  877. esp->sync_defp = SYNC_DEFP_FAST;
  878. printk("NCR53C9XF(espfast)\n");
  879. } else {
  880. esp->erev = esp236;
  881. printk("NCR53C9x(esp236)\n");
  882. }
  883. esp->config2 = 0;
  884. sbus_writeb(esp->config2, esp->eregs + ESP_CFG2);
  885. }
  886. }
  887. }
  888. static void __init esp_init_swstate(struct esp *esp)
  889. {
  890. int i;
  891. /* Command queues... */
  892. esp->current_SC = NULL;
  893. esp->disconnected_SC = NULL;
  894. esp->issue_SC = NULL;
  895. /* Target and current command state... */
  896. esp->targets_present = 0;
  897. esp->resetting_bus = 0;
  898. esp->snip = 0;
  899. init_waitqueue_head(&esp->reset_queue);
  900. /* Debugging... */
  901. for(i = 0; i < 32; i++)
  902. esp->espcmdlog[i] = 0;
  903. esp->espcmdent = 0;
  904. /* MSG phase state... */
  905. for(i = 0; i < 16; i++) {
  906. esp->cur_msgout[i] = 0;
  907. esp->cur_msgin[i] = 0;
  908. }
  909. esp->prevmsgout = esp->prevmsgin = 0;
  910. esp->msgout_len = esp->msgin_len = 0;
  911. /* Clear the one behind caches to hold unmatchable values. */
  912. esp->prev_soff = esp->prev_stp = esp->prev_cfg3 = 0xff;
  913. esp->prev_hme_dmacsr = 0xffffffff;
  914. }
  915. static int __init detect_one_esp(struct scsi_host_template *tpnt,
  916. struct device *dev,
  917. struct sbus_dev *esp_dev,
  918. struct sbus_dev *espdma,
  919. struct sbus_bus *sbus,
  920. int hme)
  921. {
  922. static int instance;
  923. struct Scsi_Host *esp_host = scsi_host_alloc(tpnt, sizeof(struct esp));
  924. struct esp *esp;
  925. if (!esp_host)
  926. return -ENOMEM;
  927. if (hme)
  928. esp_host->max_id = 16;
  929. esp = (struct esp *) esp_host->hostdata;
  930. esp->ehost = esp_host;
  931. esp->sdev = esp_dev;
  932. esp->esp_id = instance;
  933. esp->prom_node = esp_dev->prom_node;
  934. prom_getstring(esp->prom_node, "name", esp->prom_name,
  935. sizeof(esp->prom_name));
  936. if (esp_find_dvma(esp, espdma) < 0)
  937. goto fail_unlink;
  938. if (esp_map_regs(esp, hme) < 0) {
  939. printk("ESP registers unmappable");
  940. goto fail_dvma_release;
  941. }
  942. if (esp_map_cmdarea(esp) < 0) {
  943. printk("ESP DVMA transport area unmappable");
  944. goto fail_unmap_regs;
  945. }
  946. if (esp_register_irq(esp) < 0)
  947. goto fail_unmap_cmdarea;
  948. esp_get_scsi_id(esp);
  949. esp->diff = prom_getbool(esp->prom_node, "differential");
  950. if (esp->diff)
  951. printk("Differential ");
  952. esp_get_clock_params(esp);
  953. esp_get_bursts(esp, espdma);
  954. esp_get_revision(esp);
  955. esp_init_swstate(esp);
  956. esp_bootup_reset(esp);
  957. if (scsi_add_host(esp_host, dev))
  958. goto fail_free_irq;
  959. dev_set_drvdata(&esp_dev->ofdev.dev, esp);
  960. scsi_scan_host(esp_host);
  961. instance++;
  962. return 0;
  963. fail_free_irq:
  964. free_irq(esp->ehost->irq, esp);
  965. fail_unmap_cmdarea:
  966. sbus_free_consistent(esp->sdev, 16,
  967. (void *) esp->esp_command,
  968. esp->esp_command_dvma);
  969. fail_unmap_regs:
  970. sbus_iounmap(esp->eregs, ESP_REG_SIZE);
  971. fail_dvma_release:
  972. esp->dma->allocated = 0;
  973. fail_unlink:
  974. scsi_host_put(esp_host);
  975. return -1;
  976. }
  977. /* Detecting ESP chips on the machine. This is the simple and easy
  978. * version.
  979. */
  980. static int __devexit esp_remove_common(struct esp *esp)
  981. {
  982. unsigned int irq = esp->ehost->irq;
  983. scsi_remove_host(esp->ehost);
  984. ESP_INTSOFF(esp->dregs);
  985. #if 0
  986. esp_reset_dma(esp);
  987. esp_reset_esp(esp);
  988. #endif
  989. free_irq(irq, esp);
  990. sbus_free_consistent(esp->sdev, 16,
  991. (void *) esp->esp_command, esp->esp_command_dvma);
  992. sbus_iounmap(esp->eregs, ESP_REG_SIZE);
  993. esp->dma->allocated = 0;
  994. scsi_host_put(esp->ehost);
  995. return 0;
  996. }
  997. #ifdef CONFIG_SUN4
  998. #include <asm/sun4paddr.h>
  999. static struct sbus_dev sun4_esp_dev;
  1000. static int __init esp_sun4_probe(struct scsi_host_template *tpnt)
  1001. {
  1002. if (sun4_esp_physaddr) {
  1003. memset(&sun4_esp_dev, 0, sizeof(sun4_esp_dev));
  1004. sun4_esp_dev.reg_addrs[0].phys_addr = sun4_esp_physaddr;
  1005. sun4_esp_dev.irqs[0] = 4;
  1006. sun4_esp_dev.resource[0].start = sun4_esp_physaddr;
  1007. sun4_esp_dev.resource[0].end =
  1008. sun4_esp_physaddr + ESP_REG_SIZE - 1;
  1009. sun4_esp_dev.resource[0].flags = IORESOURCE_IO;
  1010. return detect_one_esp(tpnt, NULL,
  1011. &sun4_esp_dev, NULL, NULL, 0);
  1012. }
  1013. return 0;
  1014. }
  1015. static int __devexit esp_sun4_remove(void)
  1016. {
  1017. struct of_device *dev = &sun4_esp_dev.ofdev;
  1018. struct esp *esp = dev_get_drvdata(&dev->dev);
  1019. return esp_remove_common(esp);
  1020. }
  1021. #else /* !CONFIG_SUN4 */
  1022. static int __devinit esp_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  1023. {
  1024. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  1025. struct device_node *dp = dev->node;
  1026. struct sbus_dev *dma_sdev = NULL;
  1027. int hme = 0;
  1028. if (dp->parent &&
  1029. (!strcmp(dp->parent->name, "espdma") ||
  1030. !strcmp(dp->parent->name, "dma")))
  1031. dma_sdev = sdev->parent;
  1032. else if (!strcmp(dp->name, "SUNW,fas")) {
  1033. dma_sdev = sdev;
  1034. hme = 1;
  1035. }
  1036. return detect_one_esp(match->data, &dev->dev,
  1037. sdev, dma_sdev, sdev->bus, hme);
  1038. }
  1039. static int __devexit esp_sbus_remove(struct of_device *dev)
  1040. {
  1041. struct esp *esp = dev_get_drvdata(&dev->dev);
  1042. return esp_remove_common(esp);
  1043. }
  1044. #endif /* !CONFIG_SUN4 */
  1045. /* The info function will return whatever useful
  1046. * information the developer sees fit. If not provided, then
  1047. * the name field will be used instead.
  1048. */
  1049. static const char *esp_info(struct Scsi_Host *host)
  1050. {
  1051. struct esp *esp;
  1052. esp = (struct esp *) host->hostdata;
  1053. switch (esp->erev) {
  1054. case esp100:
  1055. return "Sparc ESP100 (NCR53C90)";
  1056. case esp100a:
  1057. return "Sparc ESP100A (NCR53C90A)";
  1058. case esp236:
  1059. return "Sparc ESP236";
  1060. case fas236:
  1061. return "Sparc ESP236-FAST";
  1062. case fashme:
  1063. return "Sparc ESP366-HME";
  1064. case fas100a:
  1065. return "Sparc ESP100A-FAST";
  1066. default:
  1067. return "Bogon ESP revision";
  1068. };
  1069. }
  1070. /* From Wolfgang Stanglmeier's NCR scsi driver. */
  1071. struct info_str
  1072. {
  1073. char *buffer;
  1074. int length;
  1075. int offset;
  1076. int pos;
  1077. };
  1078. static void copy_mem_info(struct info_str *info, char *data, int len)
  1079. {
  1080. if (info->pos + len > info->length)
  1081. len = info->length - info->pos;
  1082. if (info->pos + len < info->offset) {
  1083. info->pos += len;
  1084. return;
  1085. }
  1086. if (info->pos < info->offset) {
  1087. data += (info->offset - info->pos);
  1088. len -= (info->offset - info->pos);
  1089. }
  1090. if (len > 0) {
  1091. memcpy(info->buffer + info->pos, data, len);
  1092. info->pos += len;
  1093. }
  1094. }
  1095. static int copy_info(struct info_str *info, char *fmt, ...)
  1096. {
  1097. va_list args;
  1098. char buf[81];
  1099. int len;
  1100. va_start(args, fmt);
  1101. len = vsprintf(buf, fmt, args);
  1102. va_end(args);
  1103. copy_mem_info(info, buf, len);
  1104. return len;
  1105. }
  1106. static int esp_host_info(struct esp *esp, char *ptr, off_t offset, int len)
  1107. {
  1108. struct scsi_device *sdev;
  1109. struct info_str info;
  1110. int i;
  1111. info.buffer = ptr;
  1112. info.length = len;
  1113. info.offset = offset;
  1114. info.pos = 0;
  1115. copy_info(&info, "Sparc ESP Host Adapter:\n");
  1116. copy_info(&info, "\tPROM node\t\t%08x\n", (unsigned int) esp->prom_node);
  1117. copy_info(&info, "\tPROM name\t\t%s\n", esp->prom_name);
  1118. copy_info(&info, "\tESP Model\t\t");
  1119. switch (esp->erev) {
  1120. case esp100:
  1121. copy_info(&info, "ESP100\n");
  1122. break;
  1123. case esp100a:
  1124. copy_info(&info, "ESP100A\n");
  1125. break;
  1126. case esp236:
  1127. copy_info(&info, "ESP236\n");
  1128. break;
  1129. case fas236:
  1130. copy_info(&info, "FAS236\n");
  1131. break;
  1132. case fas100a:
  1133. copy_info(&info, "FAS100A\n");
  1134. break;
  1135. case fast:
  1136. copy_info(&info, "FAST\n");
  1137. break;
  1138. case fashme:
  1139. copy_info(&info, "Happy Meal FAS\n");
  1140. break;
  1141. case espunknown:
  1142. default:
  1143. copy_info(&info, "Unknown!\n");
  1144. break;
  1145. };
  1146. copy_info(&info, "\tDMA Revision\t\t");
  1147. switch (esp->dma->revision) {
  1148. case dvmarev0:
  1149. copy_info(&info, "Rev 0\n");
  1150. break;
  1151. case dvmaesc1:
  1152. copy_info(&info, "ESC Rev 1\n");
  1153. break;
  1154. case dvmarev1:
  1155. copy_info(&info, "Rev 1\n");
  1156. break;
  1157. case dvmarev2:
  1158. copy_info(&info, "Rev 2\n");
  1159. break;
  1160. case dvmarev3:
  1161. copy_info(&info, "Rev 3\n");
  1162. break;
  1163. case dvmarevplus:
  1164. copy_info(&info, "Rev 1+\n");
  1165. break;
  1166. case dvmahme:
  1167. copy_info(&info, "Rev HME/FAS\n");
  1168. break;
  1169. default:
  1170. copy_info(&info, "Unknown!\n");
  1171. break;
  1172. };
  1173. copy_info(&info, "\tLive Targets\t\t[ ");
  1174. for (i = 0; i < 15; i++) {
  1175. if (esp->targets_present & (1 << i))
  1176. copy_info(&info, "%d ", i);
  1177. }
  1178. copy_info(&info, "]\n\n");
  1179. /* Now describe the state of each existing target. */
  1180. copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\tWide\n");
  1181. shost_for_each_device(sdev, esp->ehost) {
  1182. struct esp_device *esp_dev = sdev->hostdata;
  1183. uint id = sdev->id;
  1184. if (!(esp->targets_present & (1 << id)))
  1185. continue;
  1186. copy_info(&info, "%d\t\t", id);
  1187. copy_info(&info, "%08lx\t", esp->config3[id]);
  1188. copy_info(&info, "[%02lx,%02lx]\t\t\t",
  1189. esp_dev->sync_max_offset,
  1190. esp_dev->sync_min_period);
  1191. copy_info(&info, "%s\t\t",
  1192. esp_dev->disconnect ? "yes" : "no");
  1193. copy_info(&info, "%s\n",
  1194. (esp->config3[id] & ESP_CONFIG3_EWIDE) ? "yes" : "no");
  1195. }
  1196. return info.pos > info.offset? info.pos - info.offset : 0;
  1197. }
  1198. /* ESP proc filesystem code. */
  1199. static int esp_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1200. int length, int inout)
  1201. {
  1202. struct esp *esp = (struct esp *) host->hostdata;
  1203. if (inout)
  1204. return -EINVAL; /* not yet */
  1205. if (start)
  1206. *start = buffer;
  1207. return esp_host_info(esp, buffer, offset, length);
  1208. }
  1209. static void esp_get_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
  1210. {
  1211. if (sp->use_sg == 0) {
  1212. sp->SCp.this_residual = sp->request_bufflen;
  1213. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  1214. sp->SCp.buffers_residual = 0;
  1215. if (sp->request_bufflen) {
  1216. sp->SCp.have_data_in = sbus_map_single(esp->sdev, sp->SCp.buffer,
  1217. sp->SCp.this_residual,
  1218. sp->sc_data_direction);
  1219. sp->SCp.ptr = (char *) ((unsigned long)sp->SCp.have_data_in);
  1220. } else {
  1221. sp->SCp.ptr = NULL;
  1222. }
  1223. } else {
  1224. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  1225. sp->SCp.buffers_residual = sbus_map_sg(esp->sdev,
  1226. sp->SCp.buffer,
  1227. sp->use_sg,
  1228. sp->sc_data_direction);
  1229. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  1230. sp->SCp.ptr = (char *) ((unsigned long)sg_dma_address(sp->SCp.buffer));
  1231. }
  1232. }
  1233. static void esp_release_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
  1234. {
  1235. if (sp->use_sg) {
  1236. sbus_unmap_sg(esp->sdev, sp->request_buffer, sp->use_sg,
  1237. sp->sc_data_direction);
  1238. } else if (sp->request_bufflen) {
  1239. sbus_unmap_single(esp->sdev,
  1240. sp->SCp.have_data_in,
  1241. sp->request_bufflen,
  1242. sp->sc_data_direction);
  1243. }
  1244. }
  1245. static void esp_restore_pointers(struct esp *esp, struct scsi_cmnd *sp)
  1246. {
  1247. struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
  1248. sp->SCp.ptr = ep->saved_ptr;
  1249. sp->SCp.buffer = ep->saved_buffer;
  1250. sp->SCp.this_residual = ep->saved_this_residual;
  1251. sp->SCp.buffers_residual = ep->saved_buffers_residual;
  1252. }
  1253. static void esp_save_pointers(struct esp *esp, struct scsi_cmnd *sp)
  1254. {
  1255. struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
  1256. ep->saved_ptr = sp->SCp.ptr;
  1257. ep->saved_buffer = sp->SCp.buffer;
  1258. ep->saved_this_residual = sp->SCp.this_residual;
  1259. ep->saved_buffers_residual = sp->SCp.buffers_residual;
  1260. }
  1261. /* Some rules:
  1262. *
  1263. * 1) Never ever panic while something is live on the bus.
  1264. * If there is to be any chance of syncing the disks this
  1265. * rule is to be obeyed.
  1266. *
  1267. * 2) Any target that causes a foul condition will no longer
  1268. * have synchronous transfers done to it, no questions
  1269. * asked.
  1270. *
  1271. * 3) Keep register accesses to a minimum. Think about some
  1272. * day when we have Xbus machines this is running on and
  1273. * the ESP chip is on the other end of the machine on a
  1274. * different board from the cpu where this is running.
  1275. */
  1276. /* Fire off a command. We assume the bus is free and that the only
  1277. * case where we could see an interrupt is where we have disconnected
  1278. * commands active and they are trying to reselect us.
  1279. */
  1280. static inline void esp_check_cmd(struct esp *esp, struct scsi_cmnd *sp)
  1281. {
  1282. switch (sp->cmd_len) {
  1283. case 6:
  1284. case 10:
  1285. case 12:
  1286. esp->esp_slowcmd = 0;
  1287. break;
  1288. default:
  1289. esp->esp_slowcmd = 1;
  1290. esp->esp_scmdleft = sp->cmd_len;
  1291. esp->esp_scmdp = &sp->cmnd[0];
  1292. break;
  1293. };
  1294. }
  1295. static inline void build_sync_nego_msg(struct esp *esp, int period, int offset)
  1296. {
  1297. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1298. esp->cur_msgout[1] = 3;
  1299. esp->cur_msgout[2] = EXTENDED_SDTR;
  1300. esp->cur_msgout[3] = period;
  1301. esp->cur_msgout[4] = offset;
  1302. esp->msgout_len = 5;
  1303. }
  1304. /* SIZE is in bits, currently HME only supports 16 bit wide transfers. */
  1305. static inline void build_wide_nego_msg(struct esp *esp, int size)
  1306. {
  1307. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1308. esp->cur_msgout[1] = 2;
  1309. esp->cur_msgout[2] = EXTENDED_WDTR;
  1310. switch (size) {
  1311. case 32:
  1312. esp->cur_msgout[3] = 2;
  1313. break;
  1314. case 16:
  1315. esp->cur_msgout[3] = 1;
  1316. break;
  1317. case 8:
  1318. default:
  1319. esp->cur_msgout[3] = 0;
  1320. break;
  1321. };
  1322. esp->msgout_len = 4;
  1323. }
  1324. static void esp_exec_cmd(struct esp *esp)
  1325. {
  1326. struct scsi_cmnd *SCptr;
  1327. struct scsi_device *SDptr;
  1328. struct esp_device *esp_dev;
  1329. volatile u8 *cmdp = esp->esp_command;
  1330. u8 the_esp_command;
  1331. int lun, target;
  1332. int i;
  1333. /* Hold off if we have disconnected commands and
  1334. * an IRQ is showing...
  1335. */
  1336. if (esp->disconnected_SC && ESP_IRQ_P(esp->dregs))
  1337. return;
  1338. /* Grab first member of the issue queue. */
  1339. SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
  1340. /* Safe to panic here because current_SC is null. */
  1341. if (!SCptr)
  1342. panic("esp: esp_exec_cmd and issue queue is NULL");
  1343. SDptr = SCptr->device;
  1344. esp_dev = SDptr->hostdata;
  1345. lun = SCptr->device->lun;
  1346. target = SCptr->device->id;
  1347. esp->snip = 0;
  1348. esp->msgout_len = 0;
  1349. /* Send it out whole, or piece by piece? The ESP
  1350. * only knows how to automatically send out 6, 10,
  1351. * and 12 byte commands. I used to think that the
  1352. * Linux SCSI code would never throw anything other
  1353. * than that to us, but then again there is the
  1354. * SCSI generic driver which can send us anything.
  1355. */
  1356. esp_check_cmd(esp, SCptr);
  1357. /* If arbitration/selection is successful, the ESP will leave
  1358. * ATN asserted, causing the target to go into message out
  1359. * phase. The ESP will feed the target the identify and then
  1360. * the target can only legally go to one of command,
  1361. * datain/out, status, or message in phase, or stay in message
  1362. * out phase (should we be trying to send a sync negotiation
  1363. * message after the identify). It is not allowed to drop
  1364. * BSY, but some buggy targets do and we check for this
  1365. * condition in the selection complete code. Most of the time
  1366. * we'll make the command bytes available to the ESP and it
  1367. * will not interrupt us until it finishes command phase, we
  1368. * cannot do this for command sizes the ESP does not
  1369. * understand and in this case we'll get interrupted right
  1370. * when the target goes into command phase.
  1371. *
  1372. * It is absolutely _illegal_ in the presence of SCSI-2 devices
  1373. * to use the ESP select w/o ATN command. When SCSI-2 devices are
  1374. * present on the bus we _must_ always go straight to message out
  1375. * phase with an identify message for the target. Being that
  1376. * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
  1377. * selections should not confuse SCSI-1 we hope.
  1378. */
  1379. if (esp_dev->sync) {
  1380. /* this targets sync is known */
  1381. #ifndef __sparc_v9__
  1382. do_sync_known:
  1383. #endif
  1384. if (esp_dev->disconnect)
  1385. *cmdp++ = IDENTIFY(1, lun);
  1386. else
  1387. *cmdp++ = IDENTIFY(0, lun);
  1388. if (esp->esp_slowcmd) {
  1389. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1390. esp_advance_phase(SCptr, in_slct_stop);
  1391. } else {
  1392. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1393. esp_advance_phase(SCptr, in_slct_norm);
  1394. }
  1395. } else if (!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
  1396. /* After the bootup SCSI code sends both the
  1397. * TEST_UNIT_READY and INQUIRY commands we want
  1398. * to at least attempt allowing the device to
  1399. * disconnect.
  1400. */
  1401. ESPMISC(("esp: Selecting device for first time. target=%d "
  1402. "lun=%d\n", target, SCptr->device->lun));
  1403. if (!SDptr->borken && !esp_dev->disconnect)
  1404. esp_dev->disconnect = 1;
  1405. *cmdp++ = IDENTIFY(0, lun);
  1406. esp->prevmsgout = NOP;
  1407. esp_advance_phase(SCptr, in_slct_norm);
  1408. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1409. /* Take no chances... */
  1410. esp_dev->sync_max_offset = 0;
  1411. esp_dev->sync_min_period = 0;
  1412. } else {
  1413. /* Sorry, I have had way too many problems with
  1414. * various CDROM devices on ESP. -DaveM
  1415. */
  1416. int cdrom_hwbug_wkaround = 0;
  1417. #ifndef __sparc_v9__
  1418. /* Never allow disconnects or synchronous transfers on
  1419. * SparcStation1 and SparcStation1+. Allowing those
  1420. * to be enabled seems to lockup the machine completely.
  1421. */
  1422. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
  1423. (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
  1424. /* But we are nice and allow tapes and removable
  1425. * disks (but not CDROMs) to disconnect.
  1426. */
  1427. if(SDptr->type == TYPE_TAPE ||
  1428. (SDptr->type != TYPE_ROM && SDptr->removable))
  1429. esp_dev->disconnect = 1;
  1430. else
  1431. esp_dev->disconnect = 0;
  1432. esp_dev->sync_max_offset = 0;
  1433. esp_dev->sync_min_period = 0;
  1434. esp_dev->sync = 1;
  1435. esp->snip = 0;
  1436. goto do_sync_known;
  1437. }
  1438. #endif /* !(__sparc_v9__) */
  1439. /* We've talked to this guy before,
  1440. * but never negotiated. Let's try,
  1441. * need to attempt WIDE first, before
  1442. * sync nego, as per SCSI 2 standard.
  1443. */
  1444. if (esp->erev == fashme && !esp_dev->wide) {
  1445. if (!SDptr->borken &&
  1446. SDptr->type != TYPE_ROM &&
  1447. SDptr->removable == 0) {
  1448. build_wide_nego_msg(esp, 16);
  1449. esp_dev->wide = 1;
  1450. esp->wnip = 1;
  1451. goto after_nego_msg_built;
  1452. } else {
  1453. esp_dev->wide = 1;
  1454. /* Fall through and try sync. */
  1455. }
  1456. }
  1457. if (!SDptr->borken) {
  1458. if ((SDptr->type == TYPE_ROM)) {
  1459. /* Nice try sucker... */
  1460. ESPMISC(("esp%d: Disabling sync for buggy "
  1461. "CDROM.\n", esp->esp_id));
  1462. cdrom_hwbug_wkaround = 1;
  1463. build_sync_nego_msg(esp, 0, 0);
  1464. } else if (SDptr->removable != 0) {
  1465. ESPMISC(("esp%d: Not negotiating sync/wide but "
  1466. "allowing disconnect for removable media.\n",
  1467. esp->esp_id));
  1468. build_sync_nego_msg(esp, 0, 0);
  1469. } else {
  1470. build_sync_nego_msg(esp, esp->sync_defp, 15);
  1471. }
  1472. } else {
  1473. build_sync_nego_msg(esp, 0, 0);
  1474. }
  1475. esp_dev->sync = 1;
  1476. esp->snip = 1;
  1477. after_nego_msg_built:
  1478. /* A fix for broken SCSI1 targets, when they disconnect
  1479. * they lock up the bus and confuse ESP. So disallow
  1480. * disconnects for SCSI1 targets for now until we
  1481. * find a better fix.
  1482. *
  1483. * Addendum: This is funny, I figured out what was going
  1484. * on. The blotzed SCSI1 target would disconnect,
  1485. * one of the other SCSI2 targets or both would be
  1486. * disconnected as well. The SCSI1 target would
  1487. * stay disconnected long enough that we start
  1488. * up a command on one of the SCSI2 targets. As
  1489. * the ESP is arbitrating for the bus the SCSI1
  1490. * target begins to arbitrate as well to reselect
  1491. * the ESP. The SCSI1 target refuses to drop it's
  1492. * ID bit on the data bus even though the ESP is
  1493. * at ID 7 and is the obvious winner for any
  1494. * arbitration. The ESP is a poor sport and refuses
  1495. * to lose arbitration, it will continue indefinitely
  1496. * trying to arbitrate for the bus and can only be
  1497. * stopped via a chip reset or SCSI bus reset.
  1498. * Therefore _no_ disconnects for SCSI1 targets
  1499. * thank you very much. ;-)
  1500. */
  1501. if(((SDptr->scsi_level < 3) &&
  1502. (SDptr->type != TYPE_TAPE) &&
  1503. SDptr->removable == 0) ||
  1504. cdrom_hwbug_wkaround || SDptr->borken) {
  1505. ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
  1506. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  1507. esp_dev->disconnect = 0;
  1508. *cmdp++ = IDENTIFY(0, lun);
  1509. } else {
  1510. *cmdp++ = IDENTIFY(1, lun);
  1511. }
  1512. /* ESP fifo is only so big...
  1513. * Make this look like a slow command.
  1514. */
  1515. esp->esp_slowcmd = 1;
  1516. esp->esp_scmdleft = SCptr->cmd_len;
  1517. esp->esp_scmdp = &SCptr->cmnd[0];
  1518. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1519. esp_advance_phase(SCptr, in_slct_msg);
  1520. }
  1521. if (!esp->esp_slowcmd)
  1522. for (i = 0; i < SCptr->cmd_len; i++)
  1523. *cmdp++ = SCptr->cmnd[i];
  1524. /* HME sucks... */
  1525. if (esp->erev == fashme)
  1526. sbus_writeb((target & 0xf) | (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT),
  1527. esp->eregs + ESP_BUSID);
  1528. else
  1529. sbus_writeb(target & 7, esp->eregs + ESP_BUSID);
  1530. if (esp->prev_soff != esp_dev->sync_max_offset ||
  1531. esp->prev_stp != esp_dev->sync_min_period ||
  1532. (esp->erev > esp100a &&
  1533. esp->prev_cfg3 != esp->config3[target])) {
  1534. esp->prev_soff = esp_dev->sync_max_offset;
  1535. esp->prev_stp = esp_dev->sync_min_period;
  1536. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  1537. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  1538. if (esp->erev > esp100a) {
  1539. esp->prev_cfg3 = esp->config3[target];
  1540. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  1541. }
  1542. }
  1543. i = (cmdp - esp->esp_command);
  1544. if (esp->erev == fashme) {
  1545. esp_cmd(esp, ESP_CMD_FLUSH); /* Grrr! */
  1546. /* Set up the DMA and HME counters */
  1547. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1548. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1549. sbus_writeb(0, esp->eregs + FAS_RLO);
  1550. sbus_writeb(0, esp->eregs + FAS_RHI);
  1551. esp_cmd(esp, the_esp_command);
  1552. /* Talk about touchy hardware... */
  1553. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  1554. (DMA_SCSI_DISAB | DMA_ENABLE)) &
  1555. ~(DMA_ST_WRITE));
  1556. sbus_writel(16, esp->dregs + DMA_COUNT);
  1557. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1558. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  1559. } else {
  1560. u32 tmp;
  1561. /* Set up the DMA and ESP counters */
  1562. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1563. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1564. tmp = sbus_readl(esp->dregs + DMA_CSR);
  1565. tmp &= ~DMA_ST_WRITE;
  1566. tmp |= DMA_ENABLE;
  1567. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1568. if (esp->dma->revision == dvmaesc1) {
  1569. if (i) /* Workaround ESC gate array SBUS rerun bug. */
  1570. sbus_writel(PAGE_SIZE, esp->dregs + DMA_COUNT);
  1571. }
  1572. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1573. /* Tell ESP to "go". */
  1574. esp_cmd(esp, the_esp_command);
  1575. }
  1576. }
  1577. /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
  1578. static int esp_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  1579. {
  1580. struct esp *esp;
  1581. /* Set up func ptr and initial driver cmd-phase. */
  1582. SCpnt->scsi_done = done;
  1583. SCpnt->SCp.phase = not_issued;
  1584. /* We use the scratch area. */
  1585. ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->device->lun));
  1586. ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->device->lun));
  1587. esp = (struct esp *) SCpnt->device->host->hostdata;
  1588. esp_get_dmabufs(esp, SCpnt);
  1589. esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
  1590. SCpnt->SCp.Status = CHECK_CONDITION;
  1591. SCpnt->SCp.Message = 0xff;
  1592. SCpnt->SCp.sent_command = 0;
  1593. /* Place into our queue. */
  1594. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1595. ESPQUEUE(("RQSENSE\n"));
  1596. prepend_SC(&esp->issue_SC, SCpnt);
  1597. } else {
  1598. ESPQUEUE(("\n"));
  1599. append_SC(&esp->issue_SC, SCpnt);
  1600. }
  1601. /* Run it now if we can. */
  1602. if (!esp->current_SC && !esp->resetting_bus)
  1603. esp_exec_cmd(esp);
  1604. return 0;
  1605. }
  1606. /* Dump driver state. */
  1607. static void esp_dump_cmd(struct scsi_cmnd *SCptr)
  1608. {
  1609. ESPLOG(("[tgt<%02x> lun<%02x> "
  1610. "pphase<%s> cphase<%s>]",
  1611. SCptr->device->id, SCptr->device->lun,
  1612. phase_string(SCptr->SCp.sent_command),
  1613. phase_string(SCptr->SCp.phase)));
  1614. }
  1615. static void esp_dump_state(struct esp *esp)
  1616. {
  1617. struct scsi_cmnd *SCptr = esp->current_SC;
  1618. #ifdef DEBUG_ESP_CMDS
  1619. int i;
  1620. #endif
  1621. ESPLOG(("esp%d: dumping state\n", esp->esp_id));
  1622. ESPLOG(("esp%d: dma -- cond_reg<%08x> addr<%08x>\n",
  1623. esp->esp_id,
  1624. sbus_readl(esp->dregs + DMA_CSR),
  1625. sbus_readl(esp->dregs + DMA_ADDR)));
  1626. ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1627. esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
  1628. ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1629. esp->esp_id,
  1630. sbus_readb(esp->eregs + ESP_STATUS),
  1631. sbus_readb(esp->eregs + ESP_SSTEP),
  1632. sbus_readb(esp->eregs + ESP_INTRPT)));
  1633. #ifdef DEBUG_ESP_CMDS
  1634. printk("esp%d: last ESP cmds [", esp->esp_id);
  1635. i = (esp->espcmdent - 1) & 31;
  1636. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1637. i = (i - 1) & 31;
  1638. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1639. i = (i - 1) & 31;
  1640. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1641. i = (i - 1) & 31;
  1642. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1643. printk("]\n");
  1644. #endif /* (DEBUG_ESP_CMDS) */
  1645. if (SCptr) {
  1646. ESPLOG(("esp%d: current command ", esp->esp_id));
  1647. esp_dump_cmd(SCptr);
  1648. }
  1649. ESPLOG(("\n"));
  1650. SCptr = esp->disconnected_SC;
  1651. ESPLOG(("esp%d: disconnected ", esp->esp_id));
  1652. while (SCptr) {
  1653. esp_dump_cmd(SCptr);
  1654. SCptr = (struct scsi_cmnd *) SCptr->host_scribble;
  1655. }
  1656. ESPLOG(("\n"));
  1657. }
  1658. /* Abort a command. The host_lock is acquired by caller. */
  1659. static int esp_abort(struct scsi_cmnd *SCptr)
  1660. {
  1661. struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
  1662. int don;
  1663. ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
  1664. esp_dump_state(esp);
  1665. /* Wheee, if this is the current command on the bus, the
  1666. * best we can do is assert ATN and wait for msgout phase.
  1667. * This should even fix a hung SCSI bus when we lose state
  1668. * in the driver and timeout because the eventual phase change
  1669. * will cause the ESP to (eventually) give an interrupt.
  1670. */
  1671. if (esp->current_SC == SCptr) {
  1672. esp->cur_msgout[0] = ABORT;
  1673. esp->msgout_len = 1;
  1674. esp->msgout_ctr = 0;
  1675. esp_cmd(esp, ESP_CMD_SATN);
  1676. return SUCCESS;
  1677. }
  1678. /* If it is still in the issue queue then we can safely
  1679. * call the completion routine and report abort success.
  1680. */
  1681. don = (sbus_readl(esp->dregs + DMA_CSR) & DMA_INT_ENAB);
  1682. if (don) {
  1683. ESP_INTSOFF(esp->dregs);
  1684. }
  1685. if (esp->issue_SC) {
  1686. struct scsi_cmnd **prev, *this;
  1687. for (prev = (&esp->issue_SC), this = esp->issue_SC;
  1688. this != NULL;
  1689. prev = (struct scsi_cmnd **) &(this->host_scribble),
  1690. this = (struct scsi_cmnd *) this->host_scribble) {
  1691. if (this == SCptr) {
  1692. *prev = (struct scsi_cmnd *) this->host_scribble;
  1693. this->host_scribble = NULL;
  1694. esp_release_dmabufs(esp, this);
  1695. this->result = DID_ABORT << 16;
  1696. this->scsi_done(this);
  1697. if (don)
  1698. ESP_INTSON(esp->dregs);
  1699. return SUCCESS;
  1700. }
  1701. }
  1702. }
  1703. /* Yuck, the command to abort is disconnected, it is not
  1704. * worth trying to abort it now if something else is live
  1705. * on the bus at this time. So, we let the SCSI code wait
  1706. * a little bit and try again later.
  1707. */
  1708. if (esp->current_SC) {
  1709. if (don)
  1710. ESP_INTSON(esp->dregs);
  1711. return FAILED;
  1712. }
  1713. /* It's disconnected, we have to reconnect to re-establish
  1714. * the nexus and tell the device to abort. However, we really
  1715. * cannot 'reconnect' per se. Don't try to be fancy, just
  1716. * indicate failure, which causes our caller to reset the whole
  1717. * bus.
  1718. */
  1719. if (don)
  1720. ESP_INTSON(esp->dregs);
  1721. return FAILED;
  1722. }
  1723. /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
  1724. * arrived indicating the end of the SCSI bus reset. Our job
  1725. * is to clean out the command queues and begin re-execution
  1726. * of SCSI commands once more.
  1727. */
  1728. static int esp_finish_reset(struct esp *esp)
  1729. {
  1730. struct scsi_cmnd *sp = esp->current_SC;
  1731. /* Clean up currently executing command, if any. */
  1732. if (sp != NULL) {
  1733. esp->current_SC = NULL;
  1734. esp_release_dmabufs(esp, sp);
  1735. sp->result = (DID_RESET << 16);
  1736. sp->scsi_done(sp);
  1737. }
  1738. /* Clean up disconnected queue, they have been invalidated
  1739. * by the bus reset.
  1740. */
  1741. if (esp->disconnected_SC) {
  1742. while ((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
  1743. esp_release_dmabufs(esp, sp);
  1744. sp->result = (DID_RESET << 16);
  1745. sp->scsi_done(sp);
  1746. }
  1747. }
  1748. /* SCSI bus reset is complete. */
  1749. esp->resetting_bus = 0;
  1750. wake_up(&esp->reset_queue);
  1751. /* Ok, now it is safe to get commands going once more. */
  1752. if (esp->issue_SC)
  1753. esp_exec_cmd(esp);
  1754. return do_intr_end;
  1755. }
  1756. static int esp_do_resetbus(struct esp *esp)
  1757. {
  1758. ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
  1759. esp->resetting_bus = 1;
  1760. esp_cmd(esp, ESP_CMD_RS);
  1761. return do_intr_end;
  1762. }
  1763. /* Reset ESP chip, reset hanging bus, then kill active and
  1764. * disconnected commands for targets without soft reset.
  1765. *
  1766. * The host_lock is acquired by caller.
  1767. */
  1768. static int esp_reset(struct scsi_cmnd *SCptr)
  1769. {
  1770. struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
  1771. spin_lock_irq(esp->ehost->host_lock);
  1772. (void) esp_do_resetbus(esp);
  1773. spin_unlock_irq(esp->ehost->host_lock);
  1774. wait_event(esp->reset_queue, (esp->resetting_bus == 0));
  1775. return SUCCESS;
  1776. }
  1777. /* Internal ESP done function. */
  1778. static void esp_done(struct esp *esp, int error)
  1779. {
  1780. struct scsi_cmnd *done_SC = esp->current_SC;
  1781. esp->current_SC = NULL;
  1782. esp_release_dmabufs(esp, done_SC);
  1783. done_SC->result = error;
  1784. done_SC->scsi_done(done_SC);
  1785. /* Bus is free, issue any commands in the queue. */
  1786. if (esp->issue_SC && !esp->current_SC)
  1787. esp_exec_cmd(esp);
  1788. }
  1789. /* Wheee, ESP interrupt engine. */
  1790. /* Forward declarations. */
  1791. static int esp_do_phase_determine(struct esp *esp);
  1792. static int esp_do_data_finale(struct esp *esp);
  1793. static int esp_select_complete(struct esp *esp);
  1794. static int esp_do_status(struct esp *esp);
  1795. static int esp_do_msgin(struct esp *esp);
  1796. static int esp_do_msgindone(struct esp *esp);
  1797. static int esp_do_msgout(struct esp *esp);
  1798. static int esp_do_cmdbegin(struct esp *esp);
  1799. #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
  1800. #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
  1801. /* Read any bytes found in the FAS366 fifo, storing them into
  1802. * the ESP driver software state structure.
  1803. */
  1804. static void hme_fifo_read(struct esp *esp)
  1805. {
  1806. u8 count = 0;
  1807. u8 status = esp->sreg;
  1808. /* Cannot safely frob the fifo for these following cases, but
  1809. * we must always read the fifo when the reselect interrupt
  1810. * is pending.
  1811. */
  1812. if (((esp->ireg & ESP_INTR_RSEL) == 0) &&
  1813. (sreg_datainp(status) ||
  1814. sreg_dataoutp(status) ||
  1815. (esp->current_SC &&
  1816. esp->current_SC->SCp.phase == in_data_done))) {
  1817. ESPHME(("<wkaround_skipped>"));
  1818. } else {
  1819. unsigned long fcnt = sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES;
  1820. /* The HME stores bytes in multiples of 2 in the fifo. */
  1821. ESPHME(("hme_fifo[fcnt=%d", (int)fcnt));
  1822. while (fcnt) {
  1823. esp->hme_fifo_workaround_buffer[count++] =
  1824. sbus_readb(esp->eregs + ESP_FDATA);
  1825. esp->hme_fifo_workaround_buffer[count++] =
  1826. sbus_readb(esp->eregs + ESP_FDATA);
  1827. ESPHME(("<%02x,%02x>", esp->hme_fifo_workaround_buffer[count-2], esp->hme_fifo_workaround_buffer[count-1]));
  1828. fcnt--;
  1829. }
  1830. if (sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_F1BYTE) {
  1831. ESPHME(("<poke_byte>"));
  1832. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1833. esp->hme_fifo_workaround_buffer[count++] =
  1834. sbus_readb(esp->eregs + ESP_FDATA);
  1835. ESPHME(("<%02x,0x00>", esp->hme_fifo_workaround_buffer[count-1]));
  1836. ESPHME(("CMD_FLUSH"));
  1837. esp_cmd(esp, ESP_CMD_FLUSH);
  1838. } else {
  1839. ESPHME(("no_xtra_byte"));
  1840. }
  1841. }
  1842. ESPHME(("wkarnd_cnt=%d]", (int)count));
  1843. esp->hme_fifo_workaround_count = count;
  1844. }
  1845. static inline void hme_fifo_push(struct esp *esp, u8 *bytes, u8 count)
  1846. {
  1847. esp_cmd(esp, ESP_CMD_FLUSH);
  1848. while (count) {
  1849. u8 tmp = *bytes++;
  1850. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  1851. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1852. count--;
  1853. }
  1854. }
  1855. /* We try to avoid some interrupts by jumping ahead and see if the ESP
  1856. * has gotten far enough yet. Hence the following.
  1857. */
  1858. static inline int skipahead1(struct esp *esp, struct scsi_cmnd *scp,
  1859. int prev_phase, int new_phase)
  1860. {
  1861. if (scp->SCp.sent_command != prev_phase)
  1862. return 0;
  1863. if (ESP_IRQ_P(esp->dregs)) {
  1864. /* Yes, we are able to save an interrupt. */
  1865. if (esp->erev == fashme)
  1866. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1867. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1868. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1869. if (esp->erev == fashme) {
  1870. /* This chip is really losing. */
  1871. ESPHME(("HME["));
  1872. /* Must latch fifo before reading the interrupt
  1873. * register else garbage ends up in the FIFO
  1874. * which confuses the driver utterly.
  1875. * Happy Meal indeed....
  1876. */
  1877. ESPHME(("fifo_workaround]"));
  1878. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1879. (esp->sreg2 & ESP_STAT2_F1BYTE))
  1880. hme_fifo_read(esp);
  1881. }
  1882. if (!(esp->ireg & ESP_INTR_SR))
  1883. return 0;
  1884. else
  1885. return do_reset_complete;
  1886. }
  1887. /* Ho hum, target is taking forever... */
  1888. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1889. return do_intr_end;
  1890. }
  1891. static inline int skipahead2(struct esp *esp, struct scsi_cmnd *scp,
  1892. int prev_phase1, int prev_phase2, int new_phase)
  1893. {
  1894. if (scp->SCp.sent_command != prev_phase1 &&
  1895. scp->SCp.sent_command != prev_phase2)
  1896. return 0;
  1897. if (ESP_IRQ_P(esp->dregs)) {
  1898. /* Yes, we are able to save an interrupt. */
  1899. if (esp->erev == fashme)
  1900. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1901. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1902. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1903. if (esp->erev == fashme) {
  1904. /* This chip is really losing. */
  1905. ESPHME(("HME["));
  1906. /* Must latch fifo before reading the interrupt
  1907. * register else garbage ends up in the FIFO
  1908. * which confuses the driver utterly.
  1909. * Happy Meal indeed....
  1910. */
  1911. ESPHME(("fifo_workaround]"));
  1912. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1913. (esp->sreg2 & ESP_STAT2_F1BYTE))
  1914. hme_fifo_read(esp);
  1915. }
  1916. if (!(esp->ireg & ESP_INTR_SR))
  1917. return 0;
  1918. else
  1919. return do_reset_complete;
  1920. }
  1921. /* Ho hum, target is taking forever... */
  1922. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1923. return do_intr_end;
  1924. }
  1925. /* Now some dma helpers. */
  1926. static void dma_setup(struct esp *esp, __u32 addr, int count, int write)
  1927. {
  1928. u32 nreg = sbus_readl(esp->dregs + DMA_CSR);
  1929. if (write)
  1930. nreg |= DMA_ST_WRITE;
  1931. else
  1932. nreg &= ~(DMA_ST_WRITE);
  1933. nreg |= DMA_ENABLE;
  1934. sbus_writel(nreg, esp->dregs + DMA_CSR);
  1935. if (esp->dma->revision == dvmaesc1) {
  1936. /* This ESC gate array sucks! */
  1937. __u32 src = addr;
  1938. __u32 dest = src + count;
  1939. if (dest & (PAGE_SIZE - 1))
  1940. count = PAGE_ALIGN(count);
  1941. sbus_writel(count, esp->dregs + DMA_COUNT);
  1942. }
  1943. sbus_writel(addr, esp->dregs + DMA_ADDR);
  1944. }
  1945. static void dma_drain(struct esp *esp)
  1946. {
  1947. u32 tmp;
  1948. if (esp->dma->revision == dvmahme)
  1949. return;
  1950. if ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_FIFO_ISDRAIN) {
  1951. switch (esp->dma->revision) {
  1952. default:
  1953. tmp |= DMA_FIFO_STDRAIN;
  1954. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1955. case dvmarev3:
  1956. case dvmaesc1:
  1957. while (sbus_readl(esp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
  1958. udelay(1);
  1959. };
  1960. }
  1961. }
  1962. static void dma_invalidate(struct esp *esp)
  1963. {
  1964. u32 tmp;
  1965. if (esp->dma->revision == dvmahme) {
  1966. sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
  1967. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  1968. (DMA_PARITY_OFF | DMA_2CLKS |
  1969. DMA_SCSI_DISAB | DMA_INT_ENAB)) &
  1970. ~(DMA_ST_WRITE | DMA_ENABLE));
  1971. sbus_writel(0, esp->dregs + DMA_CSR);
  1972. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  1973. /* This is necessary to avoid having the SCSI channel
  1974. * engine lock up on us.
  1975. */
  1976. sbus_writel(0, esp->dregs + DMA_ADDR);
  1977. } else {
  1978. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  1979. udelay(1);
  1980. tmp &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
  1981. tmp |= DMA_FIFO_INV;
  1982. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1983. tmp &= ~DMA_FIFO_INV;
  1984. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1985. }
  1986. }
  1987. static inline void dma_flashclear(struct esp *esp)
  1988. {
  1989. dma_drain(esp);
  1990. dma_invalidate(esp);
  1991. }
  1992. static int dma_can_transfer(struct esp *esp, struct scsi_cmnd *sp)
  1993. {
  1994. __u32 base, end, sz;
  1995. if (esp->dma->revision == dvmarev3) {
  1996. sz = sp->SCp.this_residual;
  1997. if (sz > 0x1000000)
  1998. sz = 0x1000000;
  1999. } else {
  2000. base = ((__u32)((unsigned long)sp->SCp.ptr));
  2001. base &= (0x1000000 - 1);
  2002. end = (base + sp->SCp.this_residual);
  2003. if (end > 0x1000000)
  2004. end = 0x1000000;
  2005. sz = (end - base);
  2006. }
  2007. return sz;
  2008. }
  2009. /* Misc. esp helper macros. */
  2010. #define esp_setcount(__eregs, __cnt, __hme) \
  2011. sbus_writeb(((__cnt)&0xff), (__eregs) + ESP_TCLOW); \
  2012. sbus_writeb((((__cnt)>>8)&0xff), (__eregs) + ESP_TCMED); \
  2013. if (__hme) { \
  2014. sbus_writeb((((__cnt)>>16)&0xff), (__eregs) + FAS_RLO); \
  2015. sbus_writeb(0, (__eregs) + FAS_RHI); \
  2016. }
  2017. #define esp_getcount(__eregs, __hme) \
  2018. ((sbus_readb((__eregs) + ESP_TCLOW)&0xff) | \
  2019. ((sbus_readb((__eregs) + ESP_TCMED)&0xff) << 8) | \
  2020. ((__hme) ? sbus_readb((__eregs) + FAS_RLO) << 16 : 0))
  2021. #define fcount(__esp) \
  2022. (((__esp)->erev == fashme) ? \
  2023. (__esp)->hme_fifo_workaround_count : \
  2024. sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_FBYTES)
  2025. #define fnzero(__esp) \
  2026. (((__esp)->erev == fashme) ? 0 : \
  2027. sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_ONOTZERO)
  2028. /* XXX speculative nops unnecessary when continuing amidst a data phase
  2029. * XXX even on esp100!!! another case of flooding the bus with I/O reg
  2030. * XXX writes...
  2031. */
  2032. #define esp_maybe_nop(__esp) \
  2033. if ((__esp)->erev == esp100) \
  2034. esp_cmd((__esp), ESP_CMD_NULL)
  2035. #define sreg_to_dataphase(__sreg) \
  2036. ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
  2037. /* The ESP100 when in synchronous data phase, can mistake a long final
  2038. * REQ pulse from the target as an extra byte, it places whatever is on
  2039. * the data lines into the fifo. For now, we will assume when this
  2040. * happens that the target is a bit quirky and we don't want to
  2041. * be talking synchronously to it anyways. Regardless, we need to
  2042. * tell the ESP to eat the extraneous byte so that we can proceed
  2043. * to the next phase.
  2044. */
  2045. static int esp100_sync_hwbug(struct esp *esp, struct scsi_cmnd *sp, int fifocnt)
  2046. {
  2047. /* Do not touch this piece of code. */
  2048. if ((!(esp->erev == esp100)) ||
  2049. (!(sreg_datainp((esp->sreg = sbus_readb(esp->eregs + ESP_STATUS))) &&
  2050. !fifocnt) &&
  2051. !(sreg_dataoutp(esp->sreg) && !fnzero(esp)))) {
  2052. if (sp->SCp.phase == in_dataout)
  2053. esp_cmd(esp, ESP_CMD_FLUSH);
  2054. return 0;
  2055. } else {
  2056. /* Async mode for this guy. */
  2057. build_sync_nego_msg(esp, 0, 0);
  2058. /* Ack the bogus byte, but set ATN first. */
  2059. esp_cmd(esp, ESP_CMD_SATN);
  2060. esp_cmd(esp, ESP_CMD_MOK);
  2061. return 1;
  2062. }
  2063. }
  2064. /* This closes the window during a selection with a reselect pending, because
  2065. * we use DMA for the selection process the FIFO should hold the correct
  2066. * contents if we get reselected during this process. So we just need to
  2067. * ack the possible illegal cmd interrupt pending on the esp100.
  2068. */
  2069. static inline int esp100_reconnect_hwbug(struct esp *esp)
  2070. {
  2071. u8 tmp;
  2072. if (esp->erev != esp100)
  2073. return 0;
  2074. tmp = sbus_readb(esp->eregs + ESP_INTRPT);
  2075. if (tmp & ESP_INTR_SR)
  2076. return 1;
  2077. return 0;
  2078. }
  2079. /* This verifies the BUSID bits during a reselection so that we know which
  2080. * target is talking to us.
  2081. */
  2082. static inline int reconnect_target(struct esp *esp)
  2083. {
  2084. int it, me = esp->scsi_id_mask, targ = 0;
  2085. if (2 != fcount(esp))
  2086. return -1;
  2087. if (esp->erev == fashme) {
  2088. /* HME does not latch it's own BUS ID bits during
  2089. * a reselection. Also the target number is given
  2090. * as an unsigned char, not as a sole bit number
  2091. * like the other ESP's do.
  2092. * Happy Meal indeed....
  2093. */
  2094. targ = esp->hme_fifo_workaround_buffer[0];
  2095. } else {
  2096. it = sbus_readb(esp->eregs + ESP_FDATA);
  2097. if (!(it & me))
  2098. return -1;
  2099. it &= ~me;
  2100. if (it & (it - 1))
  2101. return -1;
  2102. while (!(it & 1))
  2103. targ++, it >>= 1;
  2104. }
  2105. return targ;
  2106. }
  2107. /* This verifies the identify from the target so that we know which lun is
  2108. * being reconnected.
  2109. */
  2110. static inline int reconnect_lun(struct esp *esp)
  2111. {
  2112. int lun;
  2113. if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
  2114. return -1;
  2115. if (esp->erev == fashme)
  2116. lun = esp->hme_fifo_workaround_buffer[1];
  2117. else
  2118. lun = sbus_readb(esp->eregs + ESP_FDATA);
  2119. /* Yes, you read this correctly. We report lun of zero
  2120. * if we see parity error. ESP reports parity error for
  2121. * the lun byte, and this is the only way to hope to recover
  2122. * because the target is connected.
  2123. */
  2124. if (esp->sreg & ESP_STAT_PERR)
  2125. return 0;
  2126. /* Check for illegal bits being set in the lun. */
  2127. if ((lun & 0x40) || !(lun & 0x80))
  2128. return -1;
  2129. return lun & 7;
  2130. }
  2131. /* This puts the driver in a state where it can revitalize a command that
  2132. * is being continued due to reselection.
  2133. */
  2134. static inline void esp_connect(struct esp *esp, struct scsi_cmnd *sp)
  2135. {
  2136. struct esp_device *esp_dev = sp->device->hostdata;
  2137. if (esp->prev_soff != esp_dev->sync_max_offset ||
  2138. esp->prev_stp != esp_dev->sync_min_period ||
  2139. (esp->erev > esp100a &&
  2140. esp->prev_cfg3 != esp->config3[sp->device->id])) {
  2141. esp->prev_soff = esp_dev->sync_max_offset;
  2142. esp->prev_stp = esp_dev->sync_min_period;
  2143. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  2144. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  2145. if (esp->erev > esp100a) {
  2146. esp->prev_cfg3 = esp->config3[sp->device->id];
  2147. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  2148. }
  2149. }
  2150. esp->current_SC = sp;
  2151. }
  2152. /* This will place the current working command back into the issue queue
  2153. * if we are to receive a reselection amidst a selection attempt.
  2154. */
  2155. static inline void esp_reconnect(struct esp *esp, struct scsi_cmnd *sp)
  2156. {
  2157. if (!esp->disconnected_SC)
  2158. ESPLOG(("esp%d: Weird, being reselected but disconnected "
  2159. "command queue is empty.\n", esp->esp_id));
  2160. esp->snip = 0;
  2161. esp->current_SC = NULL;
  2162. sp->SCp.phase = not_issued;
  2163. append_SC(&esp->issue_SC, sp);
  2164. }
  2165. /* Begin message in phase. */
  2166. static int esp_do_msgin(struct esp *esp)
  2167. {
  2168. /* Must be very careful with the fifo on the HME */
  2169. if ((esp->erev != fashme) ||
  2170. !(sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_FEMPTY))
  2171. esp_cmd(esp, ESP_CMD_FLUSH);
  2172. esp_maybe_nop(esp);
  2173. esp_cmd(esp, ESP_CMD_TI);
  2174. esp->msgin_len = 1;
  2175. esp->msgin_ctr = 0;
  2176. esp_advance_phase(esp->current_SC, in_msgindone);
  2177. return do_work_bus;
  2178. }
  2179. /* This uses various DMA csr fields and the fifo flags count value to
  2180. * determine how many bytes were successfully sent/received by the ESP.
  2181. */
  2182. static inline int esp_bytes_sent(struct esp *esp, int fifo_count)
  2183. {
  2184. int rval = sbus_readl(esp->dregs + DMA_ADDR) - esp->esp_command_dvma;
  2185. if (esp->dma->revision == dvmarev1)
  2186. rval -= (4 - ((sbus_readl(esp->dregs + DMA_CSR) & DMA_READ_AHEAD)>>11));
  2187. return rval - fifo_count;
  2188. }
  2189. static inline void advance_sg(struct scsi_cmnd *sp)
  2190. {
  2191. ++sp->SCp.buffer;
  2192. --sp->SCp.buffers_residual;
  2193. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  2194. sp->SCp.ptr = (char *)((unsigned long)sg_dma_address(sp->SCp.buffer));
  2195. }
  2196. /* Please note that the way I've coded these routines is that I _always_
  2197. * check for a disconnect during any and all information transfer
  2198. * phases. The SCSI standard states that the target _can_ cause a BUS
  2199. * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
  2200. * that during information transfer phases the target controls every
  2201. * change in phase, the only thing the initiator can do is "ask" for
  2202. * a message out phase by driving ATN true. The target can, and sometimes
  2203. * will, completely ignore this request so we cannot assume anything when
  2204. * we try to force a message out phase to abort/reset a target. Most of
  2205. * the time the target will eventually be nice and go to message out, so
  2206. * we may have to hold on to our state about what we want to tell the target
  2207. * for some period of time.
  2208. */
  2209. /* I think I have things working here correctly. Even partial transfers
  2210. * within a buffer or sub-buffer should not upset us at all no matter
  2211. * how bad the target and/or ESP fucks things up.
  2212. */
  2213. static int esp_do_data(struct esp *esp)
  2214. {
  2215. struct scsi_cmnd *SCptr = esp->current_SC;
  2216. int thisphase, hmuch;
  2217. ESPDATA(("esp_do_data: "));
  2218. esp_maybe_nop(esp);
  2219. thisphase = sreg_to_dataphase(esp->sreg);
  2220. esp_advance_phase(SCptr, thisphase);
  2221. ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
  2222. hmuch = dma_can_transfer(esp, SCptr);
  2223. if (hmuch > (64 * 1024) && (esp->erev != fashme))
  2224. hmuch = (64 * 1024);
  2225. ESPDATA(("hmuch<%d> ", hmuch));
  2226. esp->current_transfer_size = hmuch;
  2227. if (esp->erev == fashme) {
  2228. u32 tmp = esp->prev_hme_dmacsr;
  2229. /* Always set the ESP count registers first. */
  2230. esp_setcount(esp->eregs, hmuch, 1);
  2231. /* Get the DMA csr computed. */
  2232. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  2233. if (thisphase == in_datain)
  2234. tmp |= DMA_ST_WRITE;
  2235. else
  2236. tmp &= ~(DMA_ST_WRITE);
  2237. esp->prev_hme_dmacsr = tmp;
  2238. ESPDATA(("DMA|TI --> do_intr_end\n"));
  2239. if (thisphase == in_datain) {
  2240. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2241. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2242. } else {
  2243. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2244. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2245. }
  2246. sbus_writel((__u32)((unsigned long)SCptr->SCp.ptr), esp->dregs+DMA_ADDR);
  2247. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  2248. } else {
  2249. esp_setcount(esp->eregs, hmuch, 0);
  2250. dma_setup(esp, ((__u32)((unsigned long)SCptr->SCp.ptr)),
  2251. hmuch, (thisphase == in_datain));
  2252. ESPDATA(("DMA|TI --> do_intr_end\n"));
  2253. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2254. }
  2255. return do_intr_end;
  2256. }
  2257. /* See how successful the data transfer was. */
  2258. static int esp_do_data_finale(struct esp *esp)
  2259. {
  2260. struct scsi_cmnd *SCptr = esp->current_SC;
  2261. struct esp_device *esp_dev = SCptr->device->hostdata;
  2262. int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
  2263. ESPDATA(("esp_do_data_finale: "));
  2264. if (SCptr->SCp.phase == in_datain) {
  2265. if (esp->sreg & ESP_STAT_PERR) {
  2266. /* Yuck, parity error. The ESP asserts ATN
  2267. * so that we can go to message out phase
  2268. * immediately and inform the target that
  2269. * something bad happened.
  2270. */
  2271. ESPLOG(("esp%d: data bad parity detected.\n",
  2272. esp->esp_id));
  2273. esp->cur_msgout[0] = INITIATOR_ERROR;
  2274. esp->msgout_len = 1;
  2275. }
  2276. dma_drain(esp);
  2277. }
  2278. dma_invalidate(esp);
  2279. /* This could happen for the above parity error case. */
  2280. if (esp->ireg != ESP_INTR_BSERV) {
  2281. /* Please go to msgout phase, please please please... */
  2282. ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
  2283. esp->esp_id));
  2284. return esp_do_phase_determine(esp);
  2285. }
  2286. /* Check for partial transfers and other horrible events.
  2287. * Note, here we read the real fifo flags register even
  2288. * on HME broken adapters because we skip the HME fifo
  2289. * workaround code in esp_handle() if we are doing data
  2290. * phase things. We don't want to fuck directly with
  2291. * the fifo like that, especially if doing synchronous
  2292. * transfers! Also, will need to double the count on
  2293. * HME if we are doing wide transfers, as the HME fifo
  2294. * will move and count 16-bit quantities during wide data.
  2295. * SMCC _and_ Qlogic can both bite me.
  2296. */
  2297. fifocnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2298. if (esp->erev != fashme)
  2299. ecount = esp_getcount(esp->eregs, 0);
  2300. bytes_sent = esp->current_transfer_size;
  2301. ESPDATA(("trans_sz(%d), ", bytes_sent));
  2302. if (esp->erev == fashme) {
  2303. if (!(esp->sreg & ESP_STAT_TCNT)) {
  2304. ecount = esp_getcount(esp->eregs, 1);
  2305. bytes_sent -= ecount;
  2306. }
  2307. /* Always subtract any cruft remaining in the FIFO. */
  2308. if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
  2309. fifocnt <<= 1;
  2310. if (SCptr->SCp.phase == in_dataout)
  2311. bytes_sent -= fifocnt;
  2312. /* I have an IBM disk which exhibits the following
  2313. * behavior during writes to it. It disconnects in
  2314. * the middle of a partial transfer, the current sglist
  2315. * buffer is 1024 bytes, the disk stops data transfer
  2316. * at 512 bytes.
  2317. *
  2318. * However the FAS366 reports that 32 more bytes were
  2319. * transferred than really were. This is precisely
  2320. * the size of a fully loaded FIFO in wide scsi mode.
  2321. * The FIFO state recorded indicates that it is empty.
  2322. *
  2323. * I have no idea if this is a bug in the FAS366 chip
  2324. * or a bug in the firmware on this IBM disk. In any
  2325. * event the following seems to be a good workaround. -DaveM
  2326. */
  2327. if (bytes_sent != esp->current_transfer_size &&
  2328. SCptr->SCp.phase == in_dataout) {
  2329. int mask = (64 - 1);
  2330. if ((esp->prev_cfg3 & ESP_CONFIG3_EWIDE) == 0)
  2331. mask >>= 1;
  2332. if (bytes_sent & mask)
  2333. bytes_sent -= (bytes_sent & mask);
  2334. }
  2335. } else {
  2336. if (!(esp->sreg & ESP_STAT_TCNT))
  2337. bytes_sent -= ecount;
  2338. if (SCptr->SCp.phase == in_dataout)
  2339. bytes_sent -= fifocnt;
  2340. }
  2341. ESPDATA(("bytes_sent(%d), ", bytes_sent));
  2342. /* If we were in synchronous mode, check for peculiarities. */
  2343. if (esp->erev == fashme) {
  2344. if (esp_dev->sync_max_offset) {
  2345. if (SCptr->SCp.phase == in_dataout)
  2346. esp_cmd(esp, ESP_CMD_FLUSH);
  2347. } else {
  2348. esp_cmd(esp, ESP_CMD_FLUSH);
  2349. }
  2350. } else {
  2351. if (esp_dev->sync_max_offset)
  2352. bogus_data = esp100_sync_hwbug(esp, SCptr, fifocnt);
  2353. else
  2354. esp_cmd(esp, ESP_CMD_FLUSH);
  2355. }
  2356. /* Until we are sure of what has happened, we are certainly
  2357. * in the dark.
  2358. */
  2359. esp_advance_phase(SCptr, in_the_dark);
  2360. if (bytes_sent < 0) {
  2361. /* I've seen this happen due to lost state in this
  2362. * driver. No idea why it happened, but allowing
  2363. * this value to be negative caused things to
  2364. * lock up. This allows greater chance of recovery.
  2365. * In fact every time I've seen this, it has been
  2366. * a driver bug without question.
  2367. */
  2368. ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
  2369. ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
  2370. esp->esp_id,
  2371. esp->current_transfer_size, fifocnt, ecount));
  2372. ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
  2373. esp->esp_id,
  2374. SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
  2375. ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
  2376. SCptr->device->id));
  2377. SCptr->device->borken = 1;
  2378. esp_dev->sync = 0;
  2379. bytes_sent = 0;
  2380. }
  2381. /* Update the state of our transfer. */
  2382. SCptr->SCp.ptr += bytes_sent;
  2383. SCptr->SCp.this_residual -= bytes_sent;
  2384. if (SCptr->SCp.this_residual < 0) {
  2385. /* shit */
  2386. ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
  2387. SCptr->SCp.this_residual = 0;
  2388. }
  2389. /* Maybe continue. */
  2390. if (!bogus_data) {
  2391. ESPDATA(("!bogus_data, "));
  2392. /* NO MATTER WHAT, we advance the scatterlist,
  2393. * if the target should decide to disconnect
  2394. * in between scatter chunks (which is common)
  2395. * we could die horribly! I used to have the sg
  2396. * advance occur only if we are going back into
  2397. * (or are staying in) a data phase, you can
  2398. * imagine the hell I went through trying to
  2399. * figure this out.
  2400. */
  2401. if (SCptr->use_sg && !SCptr->SCp.this_residual)
  2402. advance_sg(SCptr);
  2403. if (sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
  2404. ESPDATA(("to more data\n"));
  2405. return esp_do_data(esp);
  2406. }
  2407. ESPDATA(("to new phase\n"));
  2408. return esp_do_phase_determine(esp);
  2409. }
  2410. /* Bogus data, just wait for next interrupt. */
  2411. ESPLOG(("esp%d: bogus_data during end of data phase\n",
  2412. esp->esp_id));
  2413. return do_intr_end;
  2414. }
  2415. /* We received a non-good status return at the end of
  2416. * running a SCSI command. This is used to decide if
  2417. * we should clear our synchronous transfer state for
  2418. * such a device when that happens.
  2419. *
  2420. * The idea is that when spinning up a disk or rewinding
  2421. * a tape, we don't want to go into a loop re-negotiating
  2422. * synchronous capabilities over and over.
  2423. */
  2424. static int esp_should_clear_sync(struct scsi_cmnd *sp)
  2425. {
  2426. u8 cmd = sp->cmnd[0];
  2427. /* These cases are for spinning up a disk and
  2428. * waiting for that spinup to complete.
  2429. */
  2430. if (cmd == START_STOP)
  2431. return 0;
  2432. if (cmd == TEST_UNIT_READY)
  2433. return 0;
  2434. /* One more special case for SCSI tape drives,
  2435. * this is what is used to probe the device for
  2436. * completion of a rewind or tape load operation.
  2437. */
  2438. if (sp->device->type == TYPE_TAPE) {
  2439. if (cmd == MODE_SENSE)
  2440. return 0;
  2441. }
  2442. return 1;
  2443. }
  2444. /* Either a command is completing or a target is dropping off the bus
  2445. * to continue the command in the background so we can do other work.
  2446. */
  2447. static int esp_do_freebus(struct esp *esp)
  2448. {
  2449. struct scsi_cmnd *SCptr = esp->current_SC;
  2450. struct esp_device *esp_dev = SCptr->device->hostdata;
  2451. int rval;
  2452. rval = skipahead2(esp, SCptr, in_status, in_msgindone, in_freeing);
  2453. if (rval)
  2454. return rval;
  2455. if (esp->ireg != ESP_INTR_DC) {
  2456. ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
  2457. return do_reset_bus; /* target will not drop BSY... */
  2458. }
  2459. esp->msgout_len = 0;
  2460. esp->prevmsgout = NOP;
  2461. if (esp->prevmsgin == COMMAND_COMPLETE) {
  2462. /* Normal end of nexus. */
  2463. if (esp->disconnected_SC || (esp->erev == fashme))
  2464. esp_cmd(esp, ESP_CMD_ESEL);
  2465. if (SCptr->SCp.Status != GOOD &&
  2466. SCptr->SCp.Status != CONDITION_GOOD &&
  2467. ((1<<SCptr->device->id) & esp->targets_present) &&
  2468. esp_dev->sync &&
  2469. esp_dev->sync_max_offset) {
  2470. /* SCSI standard says that the synchronous capabilities
  2471. * should be renegotiated at this point. Most likely
  2472. * we are about to request sense from this target
  2473. * in which case we want to avoid using sync
  2474. * transfers until we are sure of the current target
  2475. * state.
  2476. */
  2477. ESPMISC(("esp: Status <%d> for target %d lun %d\n",
  2478. SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
  2479. /* But don't do this when spinning up a disk at
  2480. * boot time while we poll for completion as it
  2481. * fills up the console with messages. Also, tapes
  2482. * can report not ready many times right after
  2483. * loading up a tape.
  2484. */
  2485. if (esp_should_clear_sync(SCptr) != 0)
  2486. esp_dev->sync = 0;
  2487. }
  2488. ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  2489. esp_done(esp, ((SCptr->SCp.Status & 0xff) |
  2490. ((SCptr->SCp.Message & 0xff)<<8) |
  2491. (DID_OK << 16)));
  2492. } else if (esp->prevmsgin == DISCONNECT) {
  2493. /* Normal disconnect. */
  2494. esp_cmd(esp, ESP_CMD_ESEL);
  2495. ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  2496. append_SC(&esp->disconnected_SC, SCptr);
  2497. esp->current_SC = NULL;
  2498. if (esp->issue_SC)
  2499. esp_exec_cmd(esp);
  2500. } else {
  2501. /* Driver bug, we do not expect a disconnect here
  2502. * and should not have advanced the state engine
  2503. * to in_freeing.
  2504. */
  2505. ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
  2506. esp->esp_id));
  2507. return do_reset_bus;
  2508. }
  2509. return do_intr_end;
  2510. }
  2511. /* When a reselect occurs, and we cannot find the command to
  2512. * reconnect to in our queues, we do this.
  2513. */
  2514. static int esp_bad_reconnect(struct esp *esp)
  2515. {
  2516. struct scsi_cmnd *sp;
  2517. ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
  2518. esp->esp_id));
  2519. ESPLOG(("QUEUE DUMP\n"));
  2520. sp = esp->issue_SC;
  2521. ESPLOG(("esp%d: issue_SC[", esp->esp_id));
  2522. while (sp) {
  2523. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2524. sp = (struct scsi_cmnd *) sp->host_scribble;
  2525. }
  2526. ESPLOG(("]\n"));
  2527. sp = esp->current_SC;
  2528. ESPLOG(("esp%d: current_SC[", esp->esp_id));
  2529. if (sp)
  2530. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2531. else
  2532. ESPLOG(("<NULL>"));
  2533. ESPLOG(("]\n"));
  2534. sp = esp->disconnected_SC;
  2535. ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
  2536. while (sp) {
  2537. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2538. sp = (struct scsi_cmnd *) sp->host_scribble;
  2539. }
  2540. ESPLOG(("]\n"));
  2541. return do_reset_bus;
  2542. }
  2543. /* Do the needy when a target tries to reconnect to us. */
  2544. static int esp_do_reconnect(struct esp *esp)
  2545. {
  2546. int lun, target;
  2547. struct scsi_cmnd *SCptr;
  2548. /* Check for all bogus conditions first. */
  2549. target = reconnect_target(esp);
  2550. if (target < 0) {
  2551. ESPDISC(("bad bus bits\n"));
  2552. return do_reset_bus;
  2553. }
  2554. lun = reconnect_lun(esp);
  2555. if (lun < 0) {
  2556. ESPDISC(("target=%2x, bad identify msg\n", target));
  2557. return do_reset_bus;
  2558. }
  2559. /* Things look ok... */
  2560. ESPDISC(("R<%02x,%02x>", target, lun));
  2561. /* Must not flush FIFO or DVMA on HME. */
  2562. if (esp->erev != fashme) {
  2563. esp_cmd(esp, ESP_CMD_FLUSH);
  2564. if (esp100_reconnect_hwbug(esp))
  2565. return do_reset_bus;
  2566. esp_cmd(esp, ESP_CMD_NULL);
  2567. }
  2568. SCptr = remove_SC(&esp->disconnected_SC, (u8) target, (u8) lun);
  2569. if (!SCptr)
  2570. return esp_bad_reconnect(esp);
  2571. esp_connect(esp, SCptr);
  2572. esp_cmd(esp, ESP_CMD_MOK);
  2573. if (esp->erev == fashme)
  2574. sbus_writeb(((SCptr->device->id & 0xf) |
  2575. (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT)),
  2576. esp->eregs + ESP_BUSID);
  2577. /* Reconnect implies a restore pointers operation. */
  2578. esp_restore_pointers(esp, SCptr);
  2579. esp->snip = 0;
  2580. esp_advance_phase(SCptr, in_the_dark);
  2581. return do_intr_end;
  2582. }
  2583. /* End of NEXUS (hopefully), pick up status + message byte then leave if
  2584. * all goes well.
  2585. */
  2586. static int esp_do_status(struct esp *esp)
  2587. {
  2588. struct scsi_cmnd *SCptr = esp->current_SC;
  2589. int intr, rval;
  2590. rval = skipahead1(esp, SCptr, in_the_dark, in_status);
  2591. if (rval)
  2592. return rval;
  2593. intr = esp->ireg;
  2594. ESPSTAT(("esp_do_status: "));
  2595. if (intr != ESP_INTR_DC) {
  2596. int message_out = 0; /* for parity problems */
  2597. /* Ack the message. */
  2598. ESPSTAT(("ack msg, "));
  2599. esp_cmd(esp, ESP_CMD_MOK);
  2600. if (esp->erev != fashme) {
  2601. dma_flashclear(esp);
  2602. /* Wait till the first bits settle. */
  2603. while (esp->esp_command[0] == 0xff)
  2604. udelay(1);
  2605. } else {
  2606. esp->esp_command[0] = esp->hme_fifo_workaround_buffer[0];
  2607. esp->esp_command[1] = esp->hme_fifo_workaround_buffer[1];
  2608. }
  2609. ESPSTAT(("got something, "));
  2610. /* ESP chimes in with one of
  2611. *
  2612. * 1) function done interrupt:
  2613. * both status and message in bytes
  2614. * are available
  2615. *
  2616. * 2) bus service interrupt:
  2617. * only status byte was acquired
  2618. *
  2619. * 3) Anything else:
  2620. * can't happen, but we test for it
  2621. * anyways
  2622. *
  2623. * ALSO: If bad parity was detected on either
  2624. * the status _or_ the message byte then
  2625. * the ESP has asserted ATN on the bus
  2626. * and we must therefore wait for the
  2627. * next phase change.
  2628. */
  2629. if (intr & ESP_INTR_FDONE) {
  2630. /* We got it all, hallejulia. */
  2631. ESPSTAT(("got both, "));
  2632. SCptr->SCp.Status = esp->esp_command[0];
  2633. SCptr->SCp.Message = esp->esp_command[1];
  2634. esp->prevmsgin = SCptr->SCp.Message;
  2635. esp->cur_msgin[0] = SCptr->SCp.Message;
  2636. if (esp->sreg & ESP_STAT_PERR) {
  2637. /* There was bad parity for the
  2638. * message byte, the status byte
  2639. * was ok.
  2640. */
  2641. message_out = MSG_PARITY_ERROR;
  2642. }
  2643. } else if (intr == ESP_INTR_BSERV) {
  2644. /* Only got status byte. */
  2645. ESPLOG(("esp%d: got status only, ", esp->esp_id));
  2646. if (!(esp->sreg & ESP_STAT_PERR)) {
  2647. SCptr->SCp.Status = esp->esp_command[0];
  2648. SCptr->SCp.Message = 0xff;
  2649. } else {
  2650. /* The status byte had bad parity.
  2651. * we leave the scsi_pointer Status
  2652. * field alone as we set it to a default
  2653. * of CHECK_CONDITION in esp_queue.
  2654. */
  2655. message_out = INITIATOR_ERROR;
  2656. }
  2657. } else {
  2658. /* This shouldn't happen ever. */
  2659. ESPSTAT(("got bolixed\n"));
  2660. esp_advance_phase(SCptr, in_the_dark);
  2661. return esp_do_phase_determine(esp);
  2662. }
  2663. if (!message_out) {
  2664. ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
  2665. SCptr->SCp.Message));
  2666. if (SCptr->SCp.Message == COMMAND_COMPLETE) {
  2667. ESPSTAT(("and was COMMAND_COMPLETE\n"));
  2668. esp_advance_phase(SCptr, in_freeing);
  2669. return esp_do_freebus(esp);
  2670. } else {
  2671. ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
  2672. esp->esp_id));
  2673. esp->msgin_len = esp->msgin_ctr = 1;
  2674. esp_advance_phase(SCptr, in_msgindone);
  2675. return esp_do_msgindone(esp);
  2676. }
  2677. } else {
  2678. /* With luck we'll be able to let the target
  2679. * know that bad parity happened, it will know
  2680. * which byte caused the problems and send it
  2681. * again. For the case where the status byte
  2682. * receives bad parity, I do not believe most
  2683. * targets recover very well. We'll see.
  2684. */
  2685. ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
  2686. esp->esp_id, message_out));
  2687. esp->cur_msgout[0] = message_out;
  2688. esp->msgout_len = esp->msgout_ctr = 1;
  2689. esp_advance_phase(SCptr, in_the_dark);
  2690. return esp_do_phase_determine(esp);
  2691. }
  2692. } else {
  2693. /* If we disconnect now, all hell breaks loose. */
  2694. ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
  2695. esp_advance_phase(SCptr, in_the_dark);
  2696. return esp_do_phase_determine(esp);
  2697. }
  2698. }
  2699. static int esp_enter_status(struct esp *esp)
  2700. {
  2701. u8 thecmd = ESP_CMD_ICCSEQ;
  2702. esp_cmd(esp, ESP_CMD_FLUSH);
  2703. if (esp->erev != fashme) {
  2704. u32 tmp;
  2705. esp->esp_command[0] = esp->esp_command[1] = 0xff;
  2706. sbus_writeb(2, esp->eregs + ESP_TCLOW);
  2707. sbus_writeb(0, esp->eregs + ESP_TCMED);
  2708. tmp = sbus_readl(esp->dregs + DMA_CSR);
  2709. tmp |= (DMA_ST_WRITE | DMA_ENABLE);
  2710. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2711. if (esp->dma->revision == dvmaesc1)
  2712. sbus_writel(0x100, esp->dregs + DMA_COUNT);
  2713. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  2714. thecmd |= ESP_CMD_DMA;
  2715. }
  2716. esp_cmd(esp, thecmd);
  2717. esp_advance_phase(esp->current_SC, in_status);
  2718. return esp_do_status(esp);
  2719. }
  2720. static int esp_disconnect_amidst_phases(struct esp *esp)
  2721. {
  2722. struct scsi_cmnd *sp = esp->current_SC;
  2723. struct esp_device *esp_dev = sp->device->hostdata;
  2724. /* This means real problems if we see this
  2725. * here. Unless we were actually trying
  2726. * to force the device to abort/reset.
  2727. */
  2728. ESPLOG(("esp%d Disconnect amidst phases, ", esp->esp_id));
  2729. ESPLOG(("pphase<%s> cphase<%s>, ",
  2730. phase_string(sp->SCp.phase),
  2731. phase_string(sp->SCp.sent_command)));
  2732. if (esp->disconnected_SC != NULL || (esp->erev == fashme))
  2733. esp_cmd(esp, ESP_CMD_ESEL);
  2734. switch (esp->cur_msgout[0]) {
  2735. default:
  2736. /* We didn't expect this to happen at all. */
  2737. ESPLOG(("device is bolixed\n"));
  2738. esp_advance_phase(sp, in_tgterror);
  2739. esp_done(esp, (DID_ERROR << 16));
  2740. break;
  2741. case BUS_DEVICE_RESET:
  2742. ESPLOG(("device reset successful\n"));
  2743. esp_dev->sync_max_offset = 0;
  2744. esp_dev->sync_min_period = 0;
  2745. esp_dev->sync = 0;
  2746. esp_advance_phase(sp, in_resetdev);
  2747. esp_done(esp, (DID_RESET << 16));
  2748. break;
  2749. case ABORT:
  2750. ESPLOG(("device abort successful\n"));
  2751. esp_advance_phase(sp, in_abortone);
  2752. esp_done(esp, (DID_ABORT << 16));
  2753. break;
  2754. };
  2755. return do_intr_end;
  2756. }
  2757. static int esp_enter_msgout(struct esp *esp)
  2758. {
  2759. esp_advance_phase(esp->current_SC, in_msgout);
  2760. return esp_do_msgout(esp);
  2761. }
  2762. static int esp_enter_msgin(struct esp *esp)
  2763. {
  2764. esp_advance_phase(esp->current_SC, in_msgin);
  2765. return esp_do_msgin(esp);
  2766. }
  2767. static int esp_enter_cmd(struct esp *esp)
  2768. {
  2769. esp_advance_phase(esp->current_SC, in_cmdbegin);
  2770. return esp_do_cmdbegin(esp);
  2771. }
  2772. static int esp_enter_badphase(struct esp *esp)
  2773. {
  2774. ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
  2775. esp->sreg & ESP_STAT_PMASK));
  2776. return do_reset_bus;
  2777. }
  2778. typedef int (*espfunc_t)(struct esp *);
  2779. static espfunc_t phase_vector[] = {
  2780. esp_do_data, /* ESP_DOP */
  2781. esp_do_data, /* ESP_DIP */
  2782. esp_enter_cmd, /* ESP_CMDP */
  2783. esp_enter_status, /* ESP_STATP */
  2784. esp_enter_badphase, /* ESP_STAT_PMSG */
  2785. esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
  2786. esp_enter_msgout, /* ESP_MOP */
  2787. esp_enter_msgin, /* ESP_MIP */
  2788. };
  2789. /* The target has control of the bus and we have to see where it has
  2790. * taken us.
  2791. */
  2792. static int esp_do_phase_determine(struct esp *esp)
  2793. {
  2794. if ((esp->ireg & ESP_INTR_DC) != 0)
  2795. return esp_disconnect_amidst_phases(esp);
  2796. return phase_vector[esp->sreg & ESP_STAT_PMASK](esp);
  2797. }
  2798. /* First interrupt after exec'ing a cmd comes here. */
  2799. static int esp_select_complete(struct esp *esp)
  2800. {
  2801. struct scsi_cmnd *SCptr = esp->current_SC;
  2802. struct esp_device *esp_dev = SCptr->device->hostdata;
  2803. int cmd_bytes_sent, fcnt;
  2804. if (esp->erev != fashme)
  2805. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  2806. if (esp->erev == fashme)
  2807. fcnt = esp->hme_fifo_workaround_count;
  2808. else
  2809. fcnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2810. cmd_bytes_sent = esp_bytes_sent(esp, fcnt);
  2811. dma_invalidate(esp);
  2812. /* Let's check to see if a reselect happened
  2813. * while we we're trying to select. This must
  2814. * be checked first.
  2815. */
  2816. if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
  2817. esp_reconnect(esp, SCptr);
  2818. return esp_do_reconnect(esp);
  2819. }
  2820. /* Looks like things worked, we should see a bus service &
  2821. * a function complete interrupt at this point. Note we
  2822. * are doing a direct comparison because we don't want to
  2823. * be fooled into thinking selection was successful if
  2824. * ESP_INTR_DC is set, see below.
  2825. */
  2826. if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
  2827. /* target speaks... */
  2828. esp->targets_present |= (1<<SCptr->device->id);
  2829. /* What if the target ignores the sdtr? */
  2830. if (esp->snip)
  2831. esp_dev->sync = 1;
  2832. /* See how far, if at all, we got in getting
  2833. * the information out to the target.
  2834. */
  2835. switch (esp->seqreg) {
  2836. default:
  2837. case ESP_STEP_ASEL:
  2838. /* Arbitration won, target selected, but
  2839. * we are in some phase which is not command
  2840. * phase nor is it message out phase.
  2841. *
  2842. * XXX We've confused the target, obviously.
  2843. * XXX So clear it's state, but we also end
  2844. * XXX up clearing everyone elses. That isn't
  2845. * XXX so nice. I'd like to just reset this
  2846. * XXX target, but if I cannot even get it's
  2847. * XXX attention and finish selection to talk
  2848. * XXX to it, there is not much more I can do.
  2849. * XXX If we have a loaded bus we're going to
  2850. * XXX spend the next second or so renegotiating
  2851. * XXX for synchronous transfers.
  2852. */
  2853. ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
  2854. esp->esp_id, SCptr->device->id));
  2855. case ESP_STEP_SID:
  2856. /* Arbitration won, target selected, went
  2857. * to message out phase, sent one message
  2858. * byte, then we stopped. ATN is asserted
  2859. * on the SCSI bus and the target is still
  2860. * there hanging on. This is a legal
  2861. * sequence step if we gave the ESP a select
  2862. * and stop command.
  2863. *
  2864. * XXX See above, I could set the borken flag
  2865. * XXX in the device struct and retry the
  2866. * XXX command. But would that help for
  2867. * XXX tagged capable targets?
  2868. */
  2869. case ESP_STEP_NCMD:
  2870. /* Arbitration won, target selected, maybe
  2871. * sent the one message byte in message out
  2872. * phase, but we did not go to command phase
  2873. * in the end. Actually, we could have sent
  2874. * only some of the message bytes if we tried
  2875. * to send out the entire identify and tag
  2876. * message using ESP_CMD_SA3.
  2877. */
  2878. cmd_bytes_sent = 0;
  2879. break;
  2880. case ESP_STEP_PPC:
  2881. /* No, not the powerPC pinhead. Arbitration
  2882. * won, all message bytes sent if we went to
  2883. * message out phase, went to command phase
  2884. * but only part of the command was sent.
  2885. *
  2886. * XXX I've seen this, but usually in conjunction
  2887. * XXX with a gross error which appears to have
  2888. * XXX occurred between the time I told the
  2889. * XXX ESP to arbitrate and when I got the
  2890. * XXX interrupt. Could I have misloaded the
  2891. * XXX command bytes into the fifo? Actually,
  2892. * XXX I most likely missed a phase, and therefore
  2893. * XXX went into never never land and didn't even
  2894. * XXX know it. That was the old driver though.
  2895. * XXX What is even more peculiar is that the ESP
  2896. * XXX showed the proper function complete and
  2897. * XXX bus service bits in the interrupt register.
  2898. */
  2899. case ESP_STEP_FINI4:
  2900. case ESP_STEP_FINI5:
  2901. case ESP_STEP_FINI6:
  2902. case ESP_STEP_FINI7:
  2903. /* Account for the identify message */
  2904. if (SCptr->SCp.phase == in_slct_norm)
  2905. cmd_bytes_sent -= 1;
  2906. };
  2907. if (esp->erev != fashme)
  2908. esp_cmd(esp, ESP_CMD_NULL);
  2909. /* Be careful, we could really get fucked during synchronous
  2910. * data transfers if we try to flush the fifo now.
  2911. */
  2912. if ((esp->erev != fashme) && /* not a Happy Meal and... */
  2913. !fcnt && /* Fifo is empty and... */
  2914. /* either we are not doing synchronous transfers or... */
  2915. (!esp_dev->sync_max_offset ||
  2916. /* We are not going into data in phase. */
  2917. ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
  2918. esp_cmd(esp, ESP_CMD_FLUSH); /* flush is safe */
  2919. /* See how far we got if this is not a slow command. */
  2920. if (!esp->esp_slowcmd) {
  2921. if (cmd_bytes_sent < 0)
  2922. cmd_bytes_sent = 0;
  2923. if (cmd_bytes_sent != SCptr->cmd_len) {
  2924. /* Crapola, mark it as a slowcmd
  2925. * so that we have some chance of
  2926. * keeping the command alive with
  2927. * good luck.
  2928. *
  2929. * XXX Actually, if we didn't send it all
  2930. * XXX this means either we didn't set things
  2931. * XXX up properly (driver bug) or the target
  2932. * XXX or the ESP detected parity on one of
  2933. * XXX the command bytes. This makes much
  2934. * XXX more sense, and therefore this code
  2935. * XXX should be changed to send out a
  2936. * XXX parity error message or if the status
  2937. * XXX register shows no parity error then
  2938. * XXX just expect the target to bring the
  2939. * XXX bus into message in phase so that it
  2940. * XXX can send us the parity error message.
  2941. * XXX SCSI sucks...
  2942. */
  2943. esp->esp_slowcmd = 1;
  2944. esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
  2945. esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
  2946. }
  2947. }
  2948. /* Now figure out where we went. */
  2949. esp_advance_phase(SCptr, in_the_dark);
  2950. return esp_do_phase_determine(esp);
  2951. }
  2952. /* Did the target even make it? */
  2953. if (esp->ireg == ESP_INTR_DC) {
  2954. /* wheee... nobody there or they didn't like
  2955. * what we told it to do, clean up.
  2956. */
  2957. /* If anyone is off the bus, but working on
  2958. * a command in the background for us, tell
  2959. * the ESP to listen for them.
  2960. */
  2961. if (esp->disconnected_SC)
  2962. esp_cmd(esp, ESP_CMD_ESEL);
  2963. if (((1<<SCptr->device->id) & esp->targets_present) &&
  2964. esp->seqreg != 0 &&
  2965. (esp->cur_msgout[0] == EXTENDED_MESSAGE) &&
  2966. (SCptr->SCp.phase == in_slct_msg ||
  2967. SCptr->SCp.phase == in_slct_stop)) {
  2968. /* shit */
  2969. esp->snip = 0;
  2970. ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
  2971. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  2972. esp_dev->sync_max_offset = 0;
  2973. esp_dev->sync_min_period = 0;
  2974. esp_dev->sync = 1; /* so we don't negotiate again */
  2975. /* Run the command again, this time though we
  2976. * won't try to negotiate for synchronous transfers.
  2977. *
  2978. * XXX I'd like to do something like send an
  2979. * XXX INITIATOR_ERROR or ABORT message to the
  2980. * XXX target to tell it, "Sorry I confused you,
  2981. * XXX please come back and I will be nicer next
  2982. * XXX time". But that requires having the target
  2983. * XXX on the bus, and it has dropped BSY on us.
  2984. */
  2985. esp->current_SC = NULL;
  2986. esp_advance_phase(SCptr, not_issued);
  2987. prepend_SC(&esp->issue_SC, SCptr);
  2988. esp_exec_cmd(esp);
  2989. return do_intr_end;
  2990. }
  2991. /* Ok, this is normal, this is what we see during boot
  2992. * or whenever when we are scanning the bus for targets.
  2993. * But first make sure that is really what is happening.
  2994. */
  2995. if (((1<<SCptr->device->id) & esp->targets_present)) {
  2996. ESPLOG(("esp%d: Warning, live target %d not responding to "
  2997. "selection.\n", esp->esp_id, SCptr->device->id));
  2998. /* This _CAN_ happen. The SCSI standard states that
  2999. * the target is to _not_ respond to selection if
  3000. * _it_ detects bad parity on the bus for any reason.
  3001. * Therefore, we assume that if we've talked successfully
  3002. * to this target before, bad parity is the problem.
  3003. */
  3004. esp_done(esp, (DID_PARITY << 16));
  3005. } else {
  3006. /* Else, there really isn't anyone there. */
  3007. ESPMISC(("esp: selection failure, maybe nobody there?\n"));
  3008. ESPMISC(("esp: target %d lun %d\n",
  3009. SCptr->device->id, SCptr->device->lun));
  3010. esp_done(esp, (DID_BAD_TARGET << 16));
  3011. }
  3012. return do_intr_end;
  3013. }
  3014. ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
  3015. printk("esp%d: Currently -- ", esp->esp_id);
  3016. esp_print_ireg(esp->ireg); printk(" ");
  3017. esp_print_statreg(esp->sreg); printk(" ");
  3018. esp_print_seqreg(esp->seqreg); printk("\n");
  3019. printk("esp%d: New -- ", esp->esp_id);
  3020. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3021. esp->seqreg = sbus_readb(esp->eregs + ESP_SSTEP);
  3022. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  3023. esp_print_ireg(esp->ireg); printk(" ");
  3024. esp_print_statreg(esp->sreg); printk(" ");
  3025. esp_print_seqreg(esp->seqreg); printk("\n");
  3026. ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
  3027. return do_reset_bus; /* ugh... */
  3028. }
  3029. /* Continue reading bytes for msgin phase. */
  3030. static int esp_do_msgincont(struct esp *esp)
  3031. {
  3032. if (esp->ireg & ESP_INTR_BSERV) {
  3033. /* in the right phase too? */
  3034. if ((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
  3035. /* phew... */
  3036. esp_cmd(esp, ESP_CMD_TI);
  3037. esp_advance_phase(esp->current_SC, in_msgindone);
  3038. return do_intr_end;
  3039. }
  3040. /* We changed phase but ESP shows bus service,
  3041. * in this case it is most likely that we, the
  3042. * hacker who has been up for 20hrs straight
  3043. * staring at the screen, drowned in coffee
  3044. * smelling like retched cigarette ashes
  3045. * have miscoded something..... so, try to
  3046. * recover as best we can.
  3047. */
  3048. ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
  3049. }
  3050. esp_advance_phase(esp->current_SC, in_the_dark);
  3051. return do_phase_determine;
  3052. }
  3053. static int check_singlebyte_msg(struct esp *esp)
  3054. {
  3055. esp->prevmsgin = esp->cur_msgin[0];
  3056. if (esp->cur_msgin[0] & 0x80) {
  3057. /* wheee... */
  3058. ESPLOG(("esp%d: target sends identify amidst phases\n",
  3059. esp->esp_id));
  3060. esp_advance_phase(esp->current_SC, in_the_dark);
  3061. return 0;
  3062. } else if (((esp->cur_msgin[0] & 0xf0) == 0x20) ||
  3063. (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
  3064. esp->msgin_len = 2;
  3065. esp_advance_phase(esp->current_SC, in_msgincont);
  3066. return 0;
  3067. }
  3068. esp_advance_phase(esp->current_SC, in_the_dark);
  3069. switch (esp->cur_msgin[0]) {
  3070. default:
  3071. /* We don't want to hear about it. */
  3072. ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
  3073. esp->cur_msgin[0]));
  3074. return MESSAGE_REJECT;
  3075. case NOP:
  3076. ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
  3077. esp->current_SC->device->id));
  3078. return 0;
  3079. case RESTORE_POINTERS:
  3080. /* In this case we might also have to backup the
  3081. * "slow command" pointer. It is rare to get such
  3082. * a save/restore pointer sequence so early in the
  3083. * bus transition sequences, but cover it.
  3084. */
  3085. if (esp->esp_slowcmd) {
  3086. esp->esp_scmdleft = esp->current_SC->cmd_len;
  3087. esp->esp_scmdp = &esp->current_SC->cmnd[0];
  3088. }
  3089. esp_restore_pointers(esp, esp->current_SC);
  3090. return 0;
  3091. case SAVE_POINTERS:
  3092. esp_save_pointers(esp, esp->current_SC);
  3093. return 0;
  3094. case COMMAND_COMPLETE:
  3095. case DISCONNECT:
  3096. /* Freeing the bus, let it go. */
  3097. esp->current_SC->SCp.phase = in_freeing;
  3098. return 0;
  3099. case MESSAGE_REJECT:
  3100. ESPMISC(("msg reject, "));
  3101. if (esp->prevmsgout == EXTENDED_MESSAGE) {
  3102. struct esp_device *esp_dev = esp->current_SC->device->hostdata;
  3103. /* Doesn't look like this target can
  3104. * do synchronous or WIDE transfers.
  3105. */
  3106. ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
  3107. esp_dev->sync = 1;
  3108. esp_dev->wide = 1;
  3109. esp_dev->sync_min_period = 0;
  3110. esp_dev->sync_max_offset = 0;
  3111. return 0;
  3112. } else {
  3113. ESPMISC(("not sync nego, sending ABORT\n"));
  3114. return ABORT;
  3115. }
  3116. };
  3117. }
  3118. /* Target negotiates for synchronous transfers before we do, this
  3119. * is legal although very strange. What is even funnier is that
  3120. * the SCSI2 standard specifically recommends against targets doing
  3121. * this because so many initiators cannot cope with this occurring.
  3122. */
  3123. static int target_with_ants_in_pants(struct esp *esp,
  3124. struct scsi_cmnd *SCptr,
  3125. struct esp_device *esp_dev)
  3126. {
  3127. if (esp_dev->sync || SCptr->device->borken) {
  3128. /* sorry, no can do */
  3129. ESPSDTR(("forcing to async, "));
  3130. build_sync_nego_msg(esp, 0, 0);
  3131. esp_dev->sync = 1;
  3132. esp->snip = 1;
  3133. ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
  3134. esp_advance_phase(SCptr, in_the_dark);
  3135. return EXTENDED_MESSAGE;
  3136. }
  3137. /* Ok, we'll check them out... */
  3138. return 0;
  3139. }
  3140. static void sync_report(struct esp *esp)
  3141. {
  3142. int msg3, msg4;
  3143. char *type;
  3144. msg3 = esp->cur_msgin[3];
  3145. msg4 = esp->cur_msgin[4];
  3146. if (msg4) {
  3147. int hz = 1000000000 / (msg3 * 4);
  3148. int integer = hz / 1000000;
  3149. int fraction = (hz - (integer * 1000000)) / 10000;
  3150. if ((esp->erev == fashme) &&
  3151. (esp->config3[esp->current_SC->device->id] & ESP_CONFIG3_EWIDE)) {
  3152. type = "FAST-WIDE";
  3153. integer <<= 1;
  3154. fraction <<= 1;
  3155. } else if ((msg3 * 4) < 200) {
  3156. type = "FAST";
  3157. } else {
  3158. type = "synchronous";
  3159. }
  3160. /* Do not transform this back into one big printk
  3161. * again, it triggers a bug in our sparc64-gcc272
  3162. * sibling call optimization. -DaveM
  3163. */
  3164. ESPLOG((KERN_INFO "esp%d: target %d ",
  3165. esp->esp_id, esp->current_SC->device->id));
  3166. ESPLOG(("[period %dns offset %d %d.%02dMHz ",
  3167. (int) msg3 * 4, (int) msg4,
  3168. integer, fraction));
  3169. ESPLOG(("%s SCSI%s]\n", type,
  3170. (((msg3 * 4) < 200) ? "-II" : "")));
  3171. } else {
  3172. ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
  3173. esp->esp_id, esp->current_SC->device->id));
  3174. }
  3175. }
  3176. static int check_multibyte_msg(struct esp *esp)
  3177. {
  3178. struct scsi_cmnd *SCptr = esp->current_SC;
  3179. struct esp_device *esp_dev = SCptr->device->hostdata;
  3180. u8 regval = 0;
  3181. int message_out = 0;
  3182. ESPSDTR(("chk multibyte msg: "));
  3183. if (esp->cur_msgin[2] == EXTENDED_SDTR) {
  3184. int period = esp->cur_msgin[3];
  3185. int offset = esp->cur_msgin[4];
  3186. ESPSDTR(("is sync nego response, "));
  3187. if (!esp->snip) {
  3188. int rval;
  3189. /* Target negotiates first! */
  3190. ESPSDTR(("target jumps the gun, "));
  3191. message_out = EXTENDED_MESSAGE; /* we must respond */
  3192. rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
  3193. if (rval)
  3194. return rval;
  3195. }
  3196. ESPSDTR(("examining sdtr, "));
  3197. /* Offset cannot be larger than ESP fifo size. */
  3198. if (offset > 15) {
  3199. ESPSDTR(("offset too big %2x, ", offset));
  3200. offset = 15;
  3201. ESPSDTR(("sending back new offset\n"));
  3202. build_sync_nego_msg(esp, period, offset);
  3203. return EXTENDED_MESSAGE;
  3204. }
  3205. if (offset && period > esp->max_period) {
  3206. /* Yeee, async for this slow device. */
  3207. ESPSDTR(("period too long %2x, ", period));
  3208. build_sync_nego_msg(esp, 0, 0);
  3209. ESPSDTR(("hoping for msgout\n"));
  3210. esp_advance_phase(esp->current_SC, in_the_dark);
  3211. return EXTENDED_MESSAGE;
  3212. } else if (offset && period < esp->min_period) {
  3213. ESPSDTR(("period too short %2x, ", period));
  3214. period = esp->min_period;
  3215. if (esp->erev > esp236)
  3216. regval = 4;
  3217. else
  3218. regval = 5;
  3219. } else if (offset) {
  3220. int tmp;
  3221. ESPSDTR(("period is ok, "));
  3222. tmp = esp->ccycle / 1000;
  3223. regval = (((period << 2) + tmp - 1) / tmp);
  3224. if (regval && ((esp->erev == fas100a ||
  3225. esp->erev == fas236 ||
  3226. esp->erev == fashme))) {
  3227. if (period >= 50)
  3228. regval--;
  3229. }
  3230. }
  3231. if (offset) {
  3232. u8 bit;
  3233. esp_dev->sync_min_period = (regval & 0x1f);
  3234. esp_dev->sync_max_offset = (offset | esp->radelay);
  3235. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3236. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3237. bit = ESP_CONFIG3_FAST;
  3238. else
  3239. bit = ESP_CONFIG3_FSCSI;
  3240. if (period < 50) {
  3241. /* On FAS366, if using fast-20 synchronous transfers
  3242. * we need to make sure the REQ/ACK assert/deassert
  3243. * control bits are clear.
  3244. */
  3245. if (esp->erev == fashme)
  3246. esp_dev->sync_max_offset &= ~esp->radelay;
  3247. esp->config3[SCptr->device->id] |= bit;
  3248. } else {
  3249. esp->config3[SCptr->device->id] &= ~bit;
  3250. }
  3251. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3252. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3253. }
  3254. esp->prev_soff = esp_dev->sync_max_offset;
  3255. esp->prev_stp = esp_dev->sync_min_period;
  3256. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3257. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3258. ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
  3259. esp_dev->sync_max_offset,
  3260. esp_dev->sync_min_period,
  3261. esp->config3[SCptr->device->id]));
  3262. esp->snip = 0;
  3263. } else if (esp_dev->sync_max_offset) {
  3264. u8 bit;
  3265. /* back to async mode */
  3266. ESPSDTR(("unaccaptable sync nego, forcing async\n"));
  3267. esp_dev->sync_max_offset = 0;
  3268. esp_dev->sync_min_period = 0;
  3269. esp->prev_soff = 0;
  3270. esp->prev_stp = 0;
  3271. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3272. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3273. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3274. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3275. bit = ESP_CONFIG3_FAST;
  3276. else
  3277. bit = ESP_CONFIG3_FSCSI;
  3278. esp->config3[SCptr->device->id] &= ~bit;
  3279. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3280. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3281. }
  3282. }
  3283. sync_report(esp);
  3284. ESPSDTR(("chk multibyte msg: sync is known, "));
  3285. esp_dev->sync = 1;
  3286. if (message_out) {
  3287. ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
  3288. esp->esp_id));
  3289. build_sync_nego_msg(esp, period, offset);
  3290. esp_advance_phase(SCptr, in_the_dark);
  3291. return EXTENDED_MESSAGE;
  3292. }
  3293. ESPSDTR(("returning zero\n"));
  3294. esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
  3295. return 0;
  3296. } else if (esp->cur_msgin[2] == EXTENDED_WDTR) {
  3297. int size = 8 << esp->cur_msgin[3];
  3298. esp->wnip = 0;
  3299. if (esp->erev != fashme) {
  3300. ESPLOG(("esp%d: AIEEE wide msg received and not HME.\n",
  3301. esp->esp_id));
  3302. message_out = MESSAGE_REJECT;
  3303. } else if (size > 16) {
  3304. ESPLOG(("esp%d: AIEEE wide transfer for %d size "
  3305. "not supported.\n", esp->esp_id, size));
  3306. message_out = MESSAGE_REJECT;
  3307. } else {
  3308. /* Things look good; let's see what we got. */
  3309. if (size == 16) {
  3310. /* Set config 3 register for this target. */
  3311. esp->config3[SCptr->device->id] |= ESP_CONFIG3_EWIDE;
  3312. } else {
  3313. /* Just make sure it was one byte sized. */
  3314. if (size != 8) {
  3315. ESPLOG(("esp%d: Aieee, wide nego of %d size.\n",
  3316. esp->esp_id, size));
  3317. message_out = MESSAGE_REJECT;
  3318. goto finish;
  3319. }
  3320. /* Pure paranoia. */
  3321. esp->config3[SCptr->device->id] &= ~(ESP_CONFIG3_EWIDE);
  3322. }
  3323. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3324. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3325. /* Regardless, next try for sync transfers. */
  3326. build_sync_nego_msg(esp, esp->sync_defp, 15);
  3327. esp_dev->sync = 1;
  3328. esp->snip = 1;
  3329. message_out = EXTENDED_MESSAGE;
  3330. }
  3331. } else if (esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
  3332. ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
  3333. message_out = MESSAGE_REJECT;
  3334. }
  3335. finish:
  3336. esp_advance_phase(SCptr, in_the_dark);
  3337. return message_out;
  3338. }
  3339. static int esp_do_msgindone(struct esp *esp)
  3340. {
  3341. struct scsi_cmnd *SCptr = esp->current_SC;
  3342. int message_out = 0, it = 0, rval;
  3343. rval = skipahead1(esp, SCptr, in_msgin, in_msgindone);
  3344. if (rval)
  3345. return rval;
  3346. if (SCptr->SCp.sent_command != in_status) {
  3347. if (!(esp->ireg & ESP_INTR_DC)) {
  3348. if (esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
  3349. message_out = MSG_PARITY_ERROR;
  3350. esp_cmd(esp, ESP_CMD_FLUSH);
  3351. } else if (esp->erev != fashme &&
  3352. (it = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES)) != 1) {
  3353. /* We certainly dropped the ball somewhere. */
  3354. message_out = INITIATOR_ERROR;
  3355. esp_cmd(esp, ESP_CMD_FLUSH);
  3356. } else if (!esp->msgin_len) {
  3357. if (esp->erev == fashme)
  3358. it = esp->hme_fifo_workaround_buffer[0];
  3359. else
  3360. it = sbus_readb(esp->eregs + ESP_FDATA);
  3361. esp_advance_phase(SCptr, in_msgincont);
  3362. } else {
  3363. /* it is ok and we want it */
  3364. if (esp->erev == fashme)
  3365. it = esp->cur_msgin[esp->msgin_ctr] =
  3366. esp->hme_fifo_workaround_buffer[0];
  3367. else
  3368. it = esp->cur_msgin[esp->msgin_ctr] =
  3369. sbus_readb(esp->eregs + ESP_FDATA);
  3370. esp->msgin_ctr++;
  3371. }
  3372. } else {
  3373. esp_advance_phase(SCptr, in_the_dark);
  3374. return do_work_bus;
  3375. }
  3376. } else {
  3377. it = esp->cur_msgin[0];
  3378. }
  3379. if (!message_out && esp->msgin_len) {
  3380. if (esp->msgin_ctr < esp->msgin_len) {
  3381. esp_advance_phase(SCptr, in_msgincont);
  3382. } else if (esp->msgin_len == 1) {
  3383. message_out = check_singlebyte_msg(esp);
  3384. } else if (esp->msgin_len == 2) {
  3385. if (esp->cur_msgin[0] == EXTENDED_MESSAGE) {
  3386. if ((it + 2) >= 15) {
  3387. message_out = MESSAGE_REJECT;
  3388. } else {
  3389. esp->msgin_len = (it + 2);
  3390. esp_advance_phase(SCptr, in_msgincont);
  3391. }
  3392. } else {
  3393. message_out = MESSAGE_REJECT; /* foo on you */
  3394. }
  3395. } else {
  3396. message_out = check_multibyte_msg(esp);
  3397. }
  3398. }
  3399. if (message_out < 0) {
  3400. return -message_out;
  3401. } else if (message_out) {
  3402. if (((message_out != 1) &&
  3403. ((message_out < 0x20) || (message_out & 0x80))))
  3404. esp->msgout_len = 1;
  3405. esp->cur_msgout[0] = message_out;
  3406. esp_cmd(esp, ESP_CMD_SATN);
  3407. esp_advance_phase(SCptr, in_the_dark);
  3408. esp->msgin_len = 0;
  3409. }
  3410. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3411. esp->sreg &= ~(ESP_STAT_INTR);
  3412. if ((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
  3413. esp_cmd(esp, ESP_CMD_MOK);
  3414. if ((SCptr->SCp.sent_command == in_msgindone) &&
  3415. (SCptr->SCp.phase == in_freeing))
  3416. return esp_do_freebus(esp);
  3417. return do_intr_end;
  3418. }
  3419. static int esp_do_cmdbegin(struct esp *esp)
  3420. {
  3421. struct scsi_cmnd *SCptr = esp->current_SC;
  3422. esp_advance_phase(SCptr, in_cmdend);
  3423. if (esp->erev == fashme) {
  3424. u32 tmp = sbus_readl(esp->dregs + DMA_CSR);
  3425. int i;
  3426. for (i = 0; i < esp->esp_scmdleft; i++)
  3427. esp->esp_command[i] = *esp->esp_scmdp++;
  3428. esp->esp_scmdleft = 0;
  3429. esp_cmd(esp, ESP_CMD_FLUSH);
  3430. esp_setcount(esp->eregs, i, 1);
  3431. esp_cmd(esp, (ESP_CMD_DMA | ESP_CMD_TI));
  3432. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  3433. tmp &= ~(DMA_ST_WRITE);
  3434. sbus_writel(i, esp->dregs + DMA_COUNT);
  3435. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  3436. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3437. } else {
  3438. u8 tmp;
  3439. esp_cmd(esp, ESP_CMD_FLUSH);
  3440. tmp = *esp->esp_scmdp++;
  3441. esp->esp_scmdleft--;
  3442. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  3443. esp_cmd(esp, ESP_CMD_TI);
  3444. }
  3445. return do_intr_end;
  3446. }
  3447. static int esp_do_cmddone(struct esp *esp)
  3448. {
  3449. if (esp->erev == fashme)
  3450. dma_invalidate(esp);
  3451. else
  3452. esp_cmd(esp, ESP_CMD_NULL);
  3453. if (esp->ireg & ESP_INTR_BSERV) {
  3454. esp_advance_phase(esp->current_SC, in_the_dark);
  3455. return esp_do_phase_determine(esp);
  3456. }
  3457. ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
  3458. esp->esp_id));
  3459. return do_reset_bus;
  3460. }
  3461. static int esp_do_msgout(struct esp *esp)
  3462. {
  3463. esp_cmd(esp, ESP_CMD_FLUSH);
  3464. switch (esp->msgout_len) {
  3465. case 1:
  3466. if (esp->erev == fashme)
  3467. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3468. else
  3469. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3470. esp_cmd(esp, ESP_CMD_TI);
  3471. break;
  3472. case 2:
  3473. esp->esp_command[0] = esp->cur_msgout[0];
  3474. esp->esp_command[1] = esp->cur_msgout[1];
  3475. if (esp->erev == fashme) {
  3476. hme_fifo_push(esp, &esp->cur_msgout[0], 2);
  3477. esp_cmd(esp, ESP_CMD_TI);
  3478. } else {
  3479. dma_setup(esp, esp->esp_command_dvma, 2, 0);
  3480. esp_setcount(esp->eregs, 2, 0);
  3481. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3482. }
  3483. break;
  3484. case 4:
  3485. esp->esp_command[0] = esp->cur_msgout[0];
  3486. esp->esp_command[1] = esp->cur_msgout[1];
  3487. esp->esp_command[2] = esp->cur_msgout[2];
  3488. esp->esp_command[3] = esp->cur_msgout[3];
  3489. esp->snip = 1;
  3490. if (esp->erev == fashme) {
  3491. hme_fifo_push(esp, &esp->cur_msgout[0], 4);
  3492. esp_cmd(esp, ESP_CMD_TI);
  3493. } else {
  3494. dma_setup(esp, esp->esp_command_dvma, 4, 0);
  3495. esp_setcount(esp->eregs, 4, 0);
  3496. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3497. }
  3498. break;
  3499. case 5:
  3500. esp->esp_command[0] = esp->cur_msgout[0];
  3501. esp->esp_command[1] = esp->cur_msgout[1];
  3502. esp->esp_command[2] = esp->cur_msgout[2];
  3503. esp->esp_command[3] = esp->cur_msgout[3];
  3504. esp->esp_command[4] = esp->cur_msgout[4];
  3505. esp->snip = 1;
  3506. if (esp->erev == fashme) {
  3507. hme_fifo_push(esp, &esp->cur_msgout[0], 5);
  3508. esp_cmd(esp, ESP_CMD_TI);
  3509. } else {
  3510. dma_setup(esp, esp->esp_command_dvma, 5, 0);
  3511. esp_setcount(esp->eregs, 5, 0);
  3512. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3513. }
  3514. break;
  3515. default:
  3516. /* whoops */
  3517. ESPMISC(("bogus msgout sending NOP\n"));
  3518. esp->cur_msgout[0] = NOP;
  3519. if (esp->erev == fashme) {
  3520. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3521. } else {
  3522. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3523. }
  3524. esp->msgout_len = 1;
  3525. esp_cmd(esp, ESP_CMD_TI);
  3526. break;
  3527. };
  3528. esp_advance_phase(esp->current_SC, in_msgoutdone);
  3529. return do_intr_end;
  3530. }
  3531. static int esp_do_msgoutdone(struct esp *esp)
  3532. {
  3533. if (esp->msgout_len > 1) {
  3534. /* XXX HME/FAS ATN deassert workaround required,
  3535. * XXX no DMA flushing, only possible ESP_CMD_FLUSH
  3536. * XXX to kill the fifo.
  3537. */
  3538. if (esp->erev != fashme) {
  3539. u32 tmp;
  3540. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  3541. udelay(1);
  3542. tmp &= ~DMA_ENABLE;
  3543. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3544. dma_invalidate(esp);
  3545. } else {
  3546. esp_cmd(esp, ESP_CMD_FLUSH);
  3547. }
  3548. }
  3549. if (!(esp->ireg & ESP_INTR_DC)) {
  3550. if (esp->erev != fashme)
  3551. esp_cmd(esp, ESP_CMD_NULL);
  3552. switch (esp->sreg & ESP_STAT_PMASK) {
  3553. case ESP_MOP:
  3554. /* whoops, parity error */
  3555. ESPLOG(("esp%d: still in msgout, parity error assumed\n",
  3556. esp->esp_id));
  3557. if (esp->msgout_len > 1)
  3558. esp_cmd(esp, ESP_CMD_SATN);
  3559. esp_advance_phase(esp->current_SC, in_msgout);
  3560. return do_work_bus;
  3561. case ESP_DIP:
  3562. break;
  3563. default:
  3564. /* Happy Meal fifo is touchy... */
  3565. if ((esp->erev != fashme) &&
  3566. !fcount(esp) &&
  3567. !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
  3568. esp_cmd(esp, ESP_CMD_FLUSH);
  3569. break;
  3570. };
  3571. } else {
  3572. ESPLOG(("esp%d: disconnect, resetting bus\n", esp->esp_id));
  3573. return do_reset_bus;
  3574. }
  3575. /* If we sent out a synchronous negotiation message, update
  3576. * our state.
  3577. */
  3578. if (esp->cur_msgout[2] == EXTENDED_MESSAGE &&
  3579. esp->cur_msgout[4] == EXTENDED_SDTR) {
  3580. esp->snip = 1; /* anal retentiveness... */
  3581. }
  3582. esp->prevmsgout = esp->cur_msgout[0];
  3583. esp->msgout_len = 0;
  3584. esp_advance_phase(esp->current_SC, in_the_dark);
  3585. return esp_do_phase_determine(esp);
  3586. }
  3587. static int esp_bus_unexpected(struct esp *esp)
  3588. {
  3589. ESPLOG(("esp%d: command in weird state %2x\n",
  3590. esp->esp_id, esp->current_SC->SCp.phase));
  3591. return do_reset_bus;
  3592. }
  3593. static espfunc_t bus_vector[] = {
  3594. esp_do_data_finale,
  3595. esp_do_data_finale,
  3596. esp_bus_unexpected,
  3597. esp_do_msgin,
  3598. esp_do_msgincont,
  3599. esp_do_msgindone,
  3600. esp_do_msgout,
  3601. esp_do_msgoutdone,
  3602. esp_do_cmdbegin,
  3603. esp_do_cmddone,
  3604. esp_do_status,
  3605. esp_do_freebus,
  3606. esp_do_phase_determine,
  3607. esp_bus_unexpected,
  3608. esp_bus_unexpected,
  3609. esp_bus_unexpected,
  3610. };
  3611. /* This is the second tier in our dual-level SCSI state machine. */
  3612. static int esp_work_bus(struct esp *esp)
  3613. {
  3614. struct scsi_cmnd *SCptr = esp->current_SC;
  3615. unsigned int phase;
  3616. ESPBUS(("esp_work_bus: "));
  3617. if (!SCptr) {
  3618. ESPBUS(("reconnect\n"));
  3619. return esp_do_reconnect(esp);
  3620. }
  3621. phase = SCptr->SCp.phase;
  3622. if ((phase & 0xf0) == in_phases_mask)
  3623. return bus_vector[(phase & 0x0f)](esp);
  3624. else if ((phase & 0xf0) == in_slct_mask)
  3625. return esp_select_complete(esp);
  3626. else
  3627. return esp_bus_unexpected(esp);
  3628. }
  3629. static espfunc_t isvc_vector[] = {
  3630. NULL,
  3631. esp_do_phase_determine,
  3632. esp_do_resetbus,
  3633. esp_finish_reset,
  3634. esp_work_bus
  3635. };
  3636. /* Main interrupt handler for an esp adapter. */
  3637. static void esp_handle(struct esp *esp)
  3638. {
  3639. struct scsi_cmnd *SCptr;
  3640. int what_next = do_intr_end;
  3641. SCptr = esp->current_SC;
  3642. /* Check for errors. */
  3643. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3644. esp->sreg &= (~ESP_STAT_INTR);
  3645. if (esp->erev == fashme) {
  3646. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  3647. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  3648. }
  3649. if (esp->sreg & (ESP_STAT_SPAM)) {
  3650. /* Gross error, could be due to one of:
  3651. *
  3652. * - top of fifo overwritten, could be because
  3653. * we tried to do a synchronous transfer with
  3654. * an offset greater than ESP fifo size
  3655. *
  3656. * - top of command register overwritten
  3657. *
  3658. * - DMA setup to go in one direction, SCSI
  3659. * bus points in the other, whoops
  3660. *
  3661. * - weird phase change during asynchronous
  3662. * data phase while we are initiator
  3663. */
  3664. ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
  3665. /* If a command is live on the bus we cannot safely
  3666. * reset the bus, so we'll just let the pieces fall
  3667. * where they may. Here we are hoping that the
  3668. * target will be able to cleanly go away soon
  3669. * so we can safely reset things.
  3670. */
  3671. if (!SCptr) {
  3672. ESPLOG(("esp%d: No current cmd during gross error, "
  3673. "resetting bus\n", esp->esp_id));
  3674. what_next = do_reset_bus;
  3675. goto state_machine;
  3676. }
  3677. }
  3678. if (sbus_readl(esp->dregs + DMA_CSR) & DMA_HNDL_ERROR) {
  3679. /* A DMA gate array error. Here we must
  3680. * be seeing one of two things. Either the
  3681. * virtual to physical address translation
  3682. * on the SBUS could not occur, else the
  3683. * translation it did get pointed to a bogus
  3684. * page. Ho hum...
  3685. */
  3686. ESPLOG(("esp%d: DMA error %08x\n", esp->esp_id,
  3687. sbus_readl(esp->dregs + DMA_CSR)));
  3688. /* DMA gate array itself must be reset to clear the
  3689. * error condition.
  3690. */
  3691. esp_reset_dma(esp);
  3692. what_next = do_reset_bus;
  3693. goto state_machine;
  3694. }
  3695. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT); /* Unlatch intr reg */
  3696. if (esp->erev == fashme) {
  3697. /* This chip is really losing. */
  3698. ESPHME(("HME["));
  3699. ESPHME(("sreg2=%02x,", esp->sreg2));
  3700. /* Must latch fifo before reading the interrupt
  3701. * register else garbage ends up in the FIFO
  3702. * which confuses the driver utterly.
  3703. */
  3704. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  3705. (esp->sreg2 & ESP_STAT2_F1BYTE)) {
  3706. ESPHME(("fifo_workaround]"));
  3707. hme_fifo_read(esp);
  3708. } else {
  3709. ESPHME(("no_fifo_workaround]"));
  3710. }
  3711. }
  3712. /* No current cmd is only valid at this point when there are
  3713. * commands off the bus or we are trying a reset.
  3714. */
  3715. if (!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
  3716. /* Panic is safe, since current_SC is null. */
  3717. ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
  3718. panic("esp_handle: current_SC == penguin within interrupt!");
  3719. }
  3720. if (esp->ireg & (ESP_INTR_IC)) {
  3721. /* Illegal command fed to ESP. Outside of obvious
  3722. * software bugs that could cause this, there is
  3723. * a condition with esp100 where we can confuse the
  3724. * ESP into an erroneous illegal command interrupt
  3725. * because it does not scrape the FIFO properly
  3726. * for reselection. See esp100_reconnect_hwbug()
  3727. * to see how we try very hard to avoid this.
  3728. */
  3729. ESPLOG(("esp%d: invalid command\n", esp->esp_id));
  3730. esp_dump_state(esp);
  3731. if (SCptr != NULL) {
  3732. /* Devices with very buggy firmware can drop BSY
  3733. * during a scatter list interrupt when using sync
  3734. * mode transfers. We continue the transfer as
  3735. * expected, the target drops the bus, the ESP
  3736. * gets confused, and we get a illegal command
  3737. * interrupt because the bus is in the disconnected
  3738. * state now and ESP_CMD_TI is only allowed when
  3739. * a nexus is alive on the bus.
  3740. */
  3741. ESPLOG(("esp%d: Forcing async and disabling disconnect for "
  3742. "target %d\n", esp->esp_id, SCptr->device->id));
  3743. SCptr->device->borken = 1; /* foo on you */
  3744. }
  3745. what_next = do_reset_bus;
  3746. } else if (!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
  3747. if (SCptr) {
  3748. unsigned int phase = SCptr->SCp.phase;
  3749. if (phase & in_phases_mask) {
  3750. what_next = esp_work_bus(esp);
  3751. } else if (phase & in_slct_mask) {
  3752. what_next = esp_select_complete(esp);
  3753. } else {
  3754. ESPLOG(("esp%d: interrupt for no good reason...\n",
  3755. esp->esp_id));
  3756. what_next = do_intr_end;
  3757. }
  3758. } else {
  3759. ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
  3760. esp->esp_id));
  3761. what_next = do_reset_bus;
  3762. }
  3763. } else if (esp->ireg & ESP_INTR_SR) {
  3764. ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
  3765. what_next = do_reset_complete;
  3766. } else if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
  3767. ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
  3768. esp->esp_id));
  3769. what_next = do_reset_bus;
  3770. } else if (esp->ireg & ESP_INTR_RSEL) {
  3771. if (SCptr == NULL) {
  3772. /* This is ok. */
  3773. what_next = esp_do_reconnect(esp);
  3774. } else if (SCptr->SCp.phase & in_slct_mask) {
  3775. /* Only selection code knows how to clean
  3776. * up properly.
  3777. */
  3778. ESPDISC(("Reselected during selection attempt\n"));
  3779. what_next = esp_select_complete(esp);
  3780. } else {
  3781. ESPLOG(("esp%d: Reselected while bus is busy\n",
  3782. esp->esp_id));
  3783. what_next = do_reset_bus;
  3784. }
  3785. }
  3786. /* This is tier-one in our dual level SCSI state machine. */
  3787. state_machine:
  3788. while (what_next != do_intr_end) {
  3789. if (what_next >= do_phase_determine &&
  3790. what_next < do_intr_end) {
  3791. what_next = isvc_vector[what_next](esp);
  3792. } else {
  3793. /* state is completely lost ;-( */
  3794. ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
  3795. esp->esp_id));
  3796. what_next = do_reset_bus;
  3797. }
  3798. }
  3799. }
  3800. /* Service only the ESP described by dev_id. */
  3801. static irqreturn_t esp_intr(int irq, void *dev_id)
  3802. {
  3803. struct esp *esp = dev_id;
  3804. unsigned long flags;
  3805. spin_lock_irqsave(esp->ehost->host_lock, flags);
  3806. if (ESP_IRQ_P(esp->dregs)) {
  3807. ESP_INTSOFF(esp->dregs);
  3808. ESPIRQ(("I[%d:%d](", smp_processor_id(), esp->esp_id));
  3809. esp_handle(esp);
  3810. ESPIRQ((")"));
  3811. ESP_INTSON(esp->dregs);
  3812. }
  3813. spin_unlock_irqrestore(esp->ehost->host_lock, flags);
  3814. return IRQ_HANDLED;
  3815. }
  3816. static int esp_slave_alloc(struct scsi_device *SDptr)
  3817. {
  3818. struct esp_device *esp_dev =
  3819. kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
  3820. if (!esp_dev)
  3821. return -ENOMEM;
  3822. memset(esp_dev, 0, sizeof(struct esp_device));
  3823. SDptr->hostdata = esp_dev;
  3824. return 0;
  3825. }
  3826. static void esp_slave_destroy(struct scsi_device *SDptr)
  3827. {
  3828. struct esp *esp = (struct esp *) SDptr->host->hostdata;
  3829. esp->targets_present &= ~(1 << SDptr->id);
  3830. kfree(SDptr->hostdata);
  3831. SDptr->hostdata = NULL;
  3832. }
  3833. static struct scsi_host_template esp_template = {
  3834. .module = THIS_MODULE,
  3835. .name = "esp",
  3836. .info = esp_info,
  3837. .slave_alloc = esp_slave_alloc,
  3838. .slave_destroy = esp_slave_destroy,
  3839. .queuecommand = esp_queue,
  3840. .eh_abort_handler = esp_abort,
  3841. .eh_bus_reset_handler = esp_reset,
  3842. .can_queue = 7,
  3843. .this_id = 7,
  3844. .sg_tablesize = SG_ALL,
  3845. .cmd_per_lun = 1,
  3846. .use_clustering = ENABLE_CLUSTERING,
  3847. .proc_name = "esp",
  3848. .proc_info = esp_proc_info,
  3849. };
  3850. #ifndef CONFIG_SUN4
  3851. static struct of_device_id esp_match[] = {
  3852. {
  3853. .name = "SUNW,esp",
  3854. .data = &esp_template,
  3855. },
  3856. {
  3857. .name = "SUNW,fas",
  3858. .data = &esp_template,
  3859. },
  3860. {
  3861. .name = "esp",
  3862. .data = &esp_template,
  3863. },
  3864. {},
  3865. };
  3866. MODULE_DEVICE_TABLE(of, esp_match);
  3867. static struct of_platform_driver esp_sbus_driver = {
  3868. .name = "esp",
  3869. .match_table = esp_match,
  3870. .probe = esp_sbus_probe,
  3871. .remove = __devexit_p(esp_sbus_remove),
  3872. };
  3873. #endif
  3874. static int __init esp_init(void)
  3875. {
  3876. #ifdef CONFIG_SUN4
  3877. return esp_sun4_probe(&esp_template);
  3878. #else
  3879. return of_register_driver(&esp_sbus_driver, &sbus_bus_type);
  3880. #endif
  3881. }
  3882. static void __exit esp_exit(void)
  3883. {
  3884. #ifdef CONFIG_SUN4
  3885. esp_sun4_remove();
  3886. #else
  3887. of_unregister_driver(&esp_sbus_driver);
  3888. #endif
  3889. }
  3890. MODULE_DESCRIPTION("ESP Sun SCSI driver");
  3891. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  3892. MODULE_LICENSE("GPL");
  3893. MODULE_VERSION(DRV_VERSION);
  3894. module_init(esp_init);
  3895. module_exit(esp_exit);