esp.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397
  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, struct pt_regs *pregs);
  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(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 esp *esp = dev_get_drvdata(&dev->dev);
  1018. return esp_remove_common(esp);
  1019. }
  1020. #else /* !CONFIG_SUN4 */
  1021. static int __devinit esp_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  1022. {
  1023. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  1024. struct device_node *dp = dev->node;
  1025. struct sbus_dev *dma_sdev = NULL;
  1026. int hme = 0;
  1027. if (dp->parent &&
  1028. (!strcmp(dp->parent->name, "espdma") ||
  1029. !strcmp(dp->parent->name, "dma")))
  1030. dma_sdev = sdev->parent;
  1031. else if (!strcmp(dp->name, "SUNW,fas")) {
  1032. dma_sdev = sdev;
  1033. hme = 1;
  1034. }
  1035. return detect_one_esp(match->data, &dev->dev,
  1036. sdev, dma_sdev, sdev->bus, hme);
  1037. }
  1038. static int __devexit esp_sbus_remove(struct of_device *dev)
  1039. {
  1040. struct esp *esp = dev_get_drvdata(&dev->dev);
  1041. return esp_remove_common(esp);
  1042. }
  1043. #endif /* !CONFIG_SUN4 */
  1044. /* The info function will return whatever useful
  1045. * information the developer sees fit. If not provided, then
  1046. * the name field will be used instead.
  1047. */
  1048. static const char *esp_info(struct Scsi_Host *host)
  1049. {
  1050. struct esp *esp;
  1051. esp = (struct esp *) host->hostdata;
  1052. switch (esp->erev) {
  1053. case esp100:
  1054. return "Sparc ESP100 (NCR53C90)";
  1055. case esp100a:
  1056. return "Sparc ESP100A (NCR53C90A)";
  1057. case esp236:
  1058. return "Sparc ESP236";
  1059. case fas236:
  1060. return "Sparc ESP236-FAST";
  1061. case fashme:
  1062. return "Sparc ESP366-HME";
  1063. case fas100a:
  1064. return "Sparc ESP100A-FAST";
  1065. default:
  1066. return "Bogon ESP revision";
  1067. };
  1068. }
  1069. /* From Wolfgang Stanglmeier's NCR scsi driver. */
  1070. struct info_str
  1071. {
  1072. char *buffer;
  1073. int length;
  1074. int offset;
  1075. int pos;
  1076. };
  1077. static void copy_mem_info(struct info_str *info, char *data, int len)
  1078. {
  1079. if (info->pos + len > info->length)
  1080. len = info->length - info->pos;
  1081. if (info->pos + len < info->offset) {
  1082. info->pos += len;
  1083. return;
  1084. }
  1085. if (info->pos < info->offset) {
  1086. data += (info->offset - info->pos);
  1087. len -= (info->offset - info->pos);
  1088. }
  1089. if (len > 0) {
  1090. memcpy(info->buffer + info->pos, data, len);
  1091. info->pos += len;
  1092. }
  1093. }
  1094. static int copy_info(struct info_str *info, char *fmt, ...)
  1095. {
  1096. va_list args;
  1097. char buf[81];
  1098. int len;
  1099. va_start(args, fmt);
  1100. len = vsprintf(buf, fmt, args);
  1101. va_end(args);
  1102. copy_mem_info(info, buf, len);
  1103. return len;
  1104. }
  1105. static int esp_host_info(struct esp *esp, char *ptr, off_t offset, int len)
  1106. {
  1107. struct scsi_device *sdev;
  1108. struct info_str info;
  1109. int i;
  1110. info.buffer = ptr;
  1111. info.length = len;
  1112. info.offset = offset;
  1113. info.pos = 0;
  1114. copy_info(&info, "Sparc ESP Host Adapter:\n");
  1115. copy_info(&info, "\tPROM node\t\t%08x\n", (unsigned int) esp->prom_node);
  1116. copy_info(&info, "\tPROM name\t\t%s\n", esp->prom_name);
  1117. copy_info(&info, "\tESP Model\t\t");
  1118. switch (esp->erev) {
  1119. case esp100:
  1120. copy_info(&info, "ESP100\n");
  1121. break;
  1122. case esp100a:
  1123. copy_info(&info, "ESP100A\n");
  1124. break;
  1125. case esp236:
  1126. copy_info(&info, "ESP236\n");
  1127. break;
  1128. case fas236:
  1129. copy_info(&info, "FAS236\n");
  1130. break;
  1131. case fas100a:
  1132. copy_info(&info, "FAS100A\n");
  1133. break;
  1134. case fast:
  1135. copy_info(&info, "FAST\n");
  1136. break;
  1137. case fashme:
  1138. copy_info(&info, "Happy Meal FAS\n");
  1139. break;
  1140. case espunknown:
  1141. default:
  1142. copy_info(&info, "Unknown!\n");
  1143. break;
  1144. };
  1145. copy_info(&info, "\tDMA Revision\t\t");
  1146. switch (esp->dma->revision) {
  1147. case dvmarev0:
  1148. copy_info(&info, "Rev 0\n");
  1149. break;
  1150. case dvmaesc1:
  1151. copy_info(&info, "ESC Rev 1\n");
  1152. break;
  1153. case dvmarev1:
  1154. copy_info(&info, "Rev 1\n");
  1155. break;
  1156. case dvmarev2:
  1157. copy_info(&info, "Rev 2\n");
  1158. break;
  1159. case dvmarev3:
  1160. copy_info(&info, "Rev 3\n");
  1161. break;
  1162. case dvmarevplus:
  1163. copy_info(&info, "Rev 1+\n");
  1164. break;
  1165. case dvmahme:
  1166. copy_info(&info, "Rev HME/FAS\n");
  1167. break;
  1168. default:
  1169. copy_info(&info, "Unknown!\n");
  1170. break;
  1171. };
  1172. copy_info(&info, "\tLive Targets\t\t[ ");
  1173. for (i = 0; i < 15; i++) {
  1174. if (esp->targets_present & (1 << i))
  1175. copy_info(&info, "%d ", i);
  1176. }
  1177. copy_info(&info, "]\n\n");
  1178. /* Now describe the state of each existing target. */
  1179. copy_info(&info, "Target #\tconfig3\t\tSync Capabilities\tDisconnect\tWide\n");
  1180. shost_for_each_device(sdev, esp->ehost) {
  1181. struct esp_device *esp_dev = sdev->hostdata;
  1182. uint id = sdev->id;
  1183. if (!(esp->targets_present & (1 << id)))
  1184. continue;
  1185. copy_info(&info, "%d\t\t", id);
  1186. copy_info(&info, "%08lx\t", esp->config3[id]);
  1187. copy_info(&info, "[%02lx,%02lx]\t\t\t",
  1188. esp_dev->sync_max_offset,
  1189. esp_dev->sync_min_period);
  1190. copy_info(&info, "%s\t\t",
  1191. esp_dev->disconnect ? "yes" : "no");
  1192. copy_info(&info, "%s\n",
  1193. (esp->config3[id] & ESP_CONFIG3_EWIDE) ? "yes" : "no");
  1194. }
  1195. return info.pos > info.offset? info.pos - info.offset : 0;
  1196. }
  1197. /* ESP proc filesystem code. */
  1198. static int esp_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1199. int length, int inout)
  1200. {
  1201. struct esp *esp = (struct esp *) host->hostdata;
  1202. if (inout)
  1203. return -EINVAL; /* not yet */
  1204. if (start)
  1205. *start = buffer;
  1206. return esp_host_info(esp, buffer, offset, length);
  1207. }
  1208. static void esp_get_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
  1209. {
  1210. if (sp->use_sg == 0) {
  1211. sp->SCp.this_residual = sp->request_bufflen;
  1212. sp->SCp.buffer = (struct scatterlist *) sp->request_buffer;
  1213. sp->SCp.buffers_residual = 0;
  1214. if (sp->request_bufflen) {
  1215. sp->SCp.have_data_in = sbus_map_single(esp->sdev, sp->SCp.buffer,
  1216. sp->SCp.this_residual,
  1217. sp->sc_data_direction);
  1218. sp->SCp.ptr = (char *) ((unsigned long)sp->SCp.have_data_in);
  1219. } else {
  1220. sp->SCp.ptr = NULL;
  1221. }
  1222. } else {
  1223. sp->SCp.buffer = (struct scatterlist *) sp->buffer;
  1224. sp->SCp.buffers_residual = sbus_map_sg(esp->sdev,
  1225. sp->SCp.buffer,
  1226. sp->use_sg,
  1227. sp->sc_data_direction);
  1228. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  1229. sp->SCp.ptr = (char *) ((unsigned long)sg_dma_address(sp->SCp.buffer));
  1230. }
  1231. }
  1232. static void esp_release_dmabufs(struct esp *esp, struct scsi_cmnd *sp)
  1233. {
  1234. if (sp->use_sg) {
  1235. sbus_unmap_sg(esp->sdev, sp->buffer, sp->use_sg,
  1236. sp->sc_data_direction);
  1237. } else if (sp->request_bufflen) {
  1238. sbus_unmap_single(esp->sdev,
  1239. sp->SCp.have_data_in,
  1240. sp->request_bufflen,
  1241. sp->sc_data_direction);
  1242. }
  1243. }
  1244. static void esp_restore_pointers(struct esp *esp, struct scsi_cmnd *sp)
  1245. {
  1246. struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
  1247. sp->SCp.ptr = ep->saved_ptr;
  1248. sp->SCp.buffer = ep->saved_buffer;
  1249. sp->SCp.this_residual = ep->saved_this_residual;
  1250. sp->SCp.buffers_residual = ep->saved_buffers_residual;
  1251. }
  1252. static void esp_save_pointers(struct esp *esp, struct scsi_cmnd *sp)
  1253. {
  1254. struct esp_pointers *ep = &esp->data_pointers[sp->device->id];
  1255. ep->saved_ptr = sp->SCp.ptr;
  1256. ep->saved_buffer = sp->SCp.buffer;
  1257. ep->saved_this_residual = sp->SCp.this_residual;
  1258. ep->saved_buffers_residual = sp->SCp.buffers_residual;
  1259. }
  1260. /* Some rules:
  1261. *
  1262. * 1) Never ever panic while something is live on the bus.
  1263. * If there is to be any chance of syncing the disks this
  1264. * rule is to be obeyed.
  1265. *
  1266. * 2) Any target that causes a foul condition will no longer
  1267. * have synchronous transfers done to it, no questions
  1268. * asked.
  1269. *
  1270. * 3) Keep register accesses to a minimum. Think about some
  1271. * day when we have Xbus machines this is running on and
  1272. * the ESP chip is on the other end of the machine on a
  1273. * different board from the cpu where this is running.
  1274. */
  1275. /* Fire off a command. We assume the bus is free and that the only
  1276. * case where we could see an interrupt is where we have disconnected
  1277. * commands active and they are trying to reselect us.
  1278. */
  1279. static inline void esp_check_cmd(struct esp *esp, struct scsi_cmnd *sp)
  1280. {
  1281. switch (sp->cmd_len) {
  1282. case 6:
  1283. case 10:
  1284. case 12:
  1285. esp->esp_slowcmd = 0;
  1286. break;
  1287. default:
  1288. esp->esp_slowcmd = 1;
  1289. esp->esp_scmdleft = sp->cmd_len;
  1290. esp->esp_scmdp = &sp->cmnd[0];
  1291. break;
  1292. };
  1293. }
  1294. static inline void build_sync_nego_msg(struct esp *esp, int period, int offset)
  1295. {
  1296. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1297. esp->cur_msgout[1] = 3;
  1298. esp->cur_msgout[2] = EXTENDED_SDTR;
  1299. esp->cur_msgout[3] = period;
  1300. esp->cur_msgout[4] = offset;
  1301. esp->msgout_len = 5;
  1302. }
  1303. /* SIZE is in bits, currently HME only supports 16 bit wide transfers. */
  1304. static inline void build_wide_nego_msg(struct esp *esp, int size)
  1305. {
  1306. esp->cur_msgout[0] = EXTENDED_MESSAGE;
  1307. esp->cur_msgout[1] = 2;
  1308. esp->cur_msgout[2] = EXTENDED_WDTR;
  1309. switch (size) {
  1310. case 32:
  1311. esp->cur_msgout[3] = 2;
  1312. break;
  1313. case 16:
  1314. esp->cur_msgout[3] = 1;
  1315. break;
  1316. case 8:
  1317. default:
  1318. esp->cur_msgout[3] = 0;
  1319. break;
  1320. };
  1321. esp->msgout_len = 4;
  1322. }
  1323. static void esp_exec_cmd(struct esp *esp)
  1324. {
  1325. struct scsi_cmnd *SCptr;
  1326. struct scsi_device *SDptr;
  1327. struct esp_device *esp_dev;
  1328. volatile u8 *cmdp = esp->esp_command;
  1329. u8 the_esp_command;
  1330. int lun, target;
  1331. int i;
  1332. /* Hold off if we have disconnected commands and
  1333. * an IRQ is showing...
  1334. */
  1335. if (esp->disconnected_SC && ESP_IRQ_P(esp->dregs))
  1336. return;
  1337. /* Grab first member of the issue queue. */
  1338. SCptr = esp->current_SC = remove_first_SC(&esp->issue_SC);
  1339. /* Safe to panic here because current_SC is null. */
  1340. if (!SCptr)
  1341. panic("esp: esp_exec_cmd and issue queue is NULL");
  1342. SDptr = SCptr->device;
  1343. esp_dev = SDptr->hostdata;
  1344. lun = SCptr->device->lun;
  1345. target = SCptr->device->id;
  1346. esp->snip = 0;
  1347. esp->msgout_len = 0;
  1348. /* Send it out whole, or piece by piece? The ESP
  1349. * only knows how to automatically send out 6, 10,
  1350. * and 12 byte commands. I used to think that the
  1351. * Linux SCSI code would never throw anything other
  1352. * than that to us, but then again there is the
  1353. * SCSI generic driver which can send us anything.
  1354. */
  1355. esp_check_cmd(esp, SCptr);
  1356. /* If arbitration/selection is successful, the ESP will leave
  1357. * ATN asserted, causing the target to go into message out
  1358. * phase. The ESP will feed the target the identify and then
  1359. * the target can only legally go to one of command,
  1360. * datain/out, status, or message in phase, or stay in message
  1361. * out phase (should we be trying to send a sync negotiation
  1362. * message after the identify). It is not allowed to drop
  1363. * BSY, but some buggy targets do and we check for this
  1364. * condition in the selection complete code. Most of the time
  1365. * we'll make the command bytes available to the ESP and it
  1366. * will not interrupt us until it finishes command phase, we
  1367. * cannot do this for command sizes the ESP does not
  1368. * understand and in this case we'll get interrupted right
  1369. * when the target goes into command phase.
  1370. *
  1371. * It is absolutely _illegal_ in the presence of SCSI-2 devices
  1372. * to use the ESP select w/o ATN command. When SCSI-2 devices are
  1373. * present on the bus we _must_ always go straight to message out
  1374. * phase with an identify message for the target. Being that
  1375. * selection attempts in SCSI-1 w/o ATN was an option, doing SCSI-2
  1376. * selections should not confuse SCSI-1 we hope.
  1377. */
  1378. if (esp_dev->sync) {
  1379. /* this targets sync is known */
  1380. #ifndef __sparc_v9__
  1381. do_sync_known:
  1382. #endif
  1383. if (esp_dev->disconnect)
  1384. *cmdp++ = IDENTIFY(1, lun);
  1385. else
  1386. *cmdp++ = IDENTIFY(0, lun);
  1387. if (esp->esp_slowcmd) {
  1388. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1389. esp_advance_phase(SCptr, in_slct_stop);
  1390. } else {
  1391. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1392. esp_advance_phase(SCptr, in_slct_norm);
  1393. }
  1394. } else if (!(esp->targets_present & (1<<target)) || !(esp_dev->disconnect)) {
  1395. /* After the bootup SCSI code sends both the
  1396. * TEST_UNIT_READY and INQUIRY commands we want
  1397. * to at least attempt allowing the device to
  1398. * disconnect.
  1399. */
  1400. ESPMISC(("esp: Selecting device for first time. target=%d "
  1401. "lun=%d\n", target, SCptr->device->lun));
  1402. if (!SDptr->borken && !esp_dev->disconnect)
  1403. esp_dev->disconnect = 1;
  1404. *cmdp++ = IDENTIFY(0, lun);
  1405. esp->prevmsgout = NOP;
  1406. esp_advance_phase(SCptr, in_slct_norm);
  1407. the_esp_command = (ESP_CMD_SELA | ESP_CMD_DMA);
  1408. /* Take no chances... */
  1409. esp_dev->sync_max_offset = 0;
  1410. esp_dev->sync_min_period = 0;
  1411. } else {
  1412. /* Sorry, I have had way too many problems with
  1413. * various CDROM devices on ESP. -DaveM
  1414. */
  1415. int cdrom_hwbug_wkaround = 0;
  1416. #ifndef __sparc_v9__
  1417. /* Never allow disconnects or synchronous transfers on
  1418. * SparcStation1 and SparcStation1+. Allowing those
  1419. * to be enabled seems to lockup the machine completely.
  1420. */
  1421. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
  1422. (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
  1423. /* But we are nice and allow tapes and removable
  1424. * disks (but not CDROMs) to disconnect.
  1425. */
  1426. if(SDptr->type == TYPE_TAPE ||
  1427. (SDptr->type != TYPE_ROM && SDptr->removable))
  1428. esp_dev->disconnect = 1;
  1429. else
  1430. esp_dev->disconnect = 0;
  1431. esp_dev->sync_max_offset = 0;
  1432. esp_dev->sync_min_period = 0;
  1433. esp_dev->sync = 1;
  1434. esp->snip = 0;
  1435. goto do_sync_known;
  1436. }
  1437. #endif /* !(__sparc_v9__) */
  1438. /* We've talked to this guy before,
  1439. * but never negotiated. Let's try,
  1440. * need to attempt WIDE first, before
  1441. * sync nego, as per SCSI 2 standard.
  1442. */
  1443. if (esp->erev == fashme && !esp_dev->wide) {
  1444. if (!SDptr->borken &&
  1445. SDptr->type != TYPE_ROM &&
  1446. SDptr->removable == 0) {
  1447. build_wide_nego_msg(esp, 16);
  1448. esp_dev->wide = 1;
  1449. esp->wnip = 1;
  1450. goto after_nego_msg_built;
  1451. } else {
  1452. esp_dev->wide = 1;
  1453. /* Fall through and try sync. */
  1454. }
  1455. }
  1456. if (!SDptr->borken) {
  1457. if ((SDptr->type == TYPE_ROM)) {
  1458. /* Nice try sucker... */
  1459. ESPMISC(("esp%d: Disabling sync for buggy "
  1460. "CDROM.\n", esp->esp_id));
  1461. cdrom_hwbug_wkaround = 1;
  1462. build_sync_nego_msg(esp, 0, 0);
  1463. } else if (SDptr->removable != 0) {
  1464. ESPMISC(("esp%d: Not negotiating sync/wide but "
  1465. "allowing disconnect for removable media.\n",
  1466. esp->esp_id));
  1467. build_sync_nego_msg(esp, 0, 0);
  1468. } else {
  1469. build_sync_nego_msg(esp, esp->sync_defp, 15);
  1470. }
  1471. } else {
  1472. build_sync_nego_msg(esp, 0, 0);
  1473. }
  1474. esp_dev->sync = 1;
  1475. esp->snip = 1;
  1476. after_nego_msg_built:
  1477. /* A fix for broken SCSI1 targets, when they disconnect
  1478. * they lock up the bus and confuse ESP. So disallow
  1479. * disconnects for SCSI1 targets for now until we
  1480. * find a better fix.
  1481. *
  1482. * Addendum: This is funny, I figured out what was going
  1483. * on. The blotzed SCSI1 target would disconnect,
  1484. * one of the other SCSI2 targets or both would be
  1485. * disconnected as well. The SCSI1 target would
  1486. * stay disconnected long enough that we start
  1487. * up a command on one of the SCSI2 targets. As
  1488. * the ESP is arbitrating for the bus the SCSI1
  1489. * target begins to arbitrate as well to reselect
  1490. * the ESP. The SCSI1 target refuses to drop it's
  1491. * ID bit on the data bus even though the ESP is
  1492. * at ID 7 and is the obvious winner for any
  1493. * arbitration. The ESP is a poor sport and refuses
  1494. * to lose arbitration, it will continue indefinitely
  1495. * trying to arbitrate for the bus and can only be
  1496. * stopped via a chip reset or SCSI bus reset.
  1497. * Therefore _no_ disconnects for SCSI1 targets
  1498. * thank you very much. ;-)
  1499. */
  1500. if(((SDptr->scsi_level < 3) &&
  1501. (SDptr->type != TYPE_TAPE) &&
  1502. SDptr->removable == 0) ||
  1503. cdrom_hwbug_wkaround || SDptr->borken) {
  1504. ESPMISC((KERN_INFO "esp%d: Disabling DISCONNECT for target %d "
  1505. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  1506. esp_dev->disconnect = 0;
  1507. *cmdp++ = IDENTIFY(0, lun);
  1508. } else {
  1509. *cmdp++ = IDENTIFY(1, lun);
  1510. }
  1511. /* ESP fifo is only so big...
  1512. * Make this look like a slow command.
  1513. */
  1514. esp->esp_slowcmd = 1;
  1515. esp->esp_scmdleft = SCptr->cmd_len;
  1516. esp->esp_scmdp = &SCptr->cmnd[0];
  1517. the_esp_command = (ESP_CMD_SELAS | ESP_CMD_DMA);
  1518. esp_advance_phase(SCptr, in_slct_msg);
  1519. }
  1520. if (!esp->esp_slowcmd)
  1521. for (i = 0; i < SCptr->cmd_len; i++)
  1522. *cmdp++ = SCptr->cmnd[i];
  1523. /* HME sucks... */
  1524. if (esp->erev == fashme)
  1525. sbus_writeb((target & 0xf) | (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT),
  1526. esp->eregs + ESP_BUSID);
  1527. else
  1528. sbus_writeb(target & 7, esp->eregs + ESP_BUSID);
  1529. if (esp->prev_soff != esp_dev->sync_max_offset ||
  1530. esp->prev_stp != esp_dev->sync_min_period ||
  1531. (esp->erev > esp100a &&
  1532. esp->prev_cfg3 != esp->config3[target])) {
  1533. esp->prev_soff = esp_dev->sync_max_offset;
  1534. esp->prev_stp = esp_dev->sync_min_period;
  1535. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  1536. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  1537. if (esp->erev > esp100a) {
  1538. esp->prev_cfg3 = esp->config3[target];
  1539. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  1540. }
  1541. }
  1542. i = (cmdp - esp->esp_command);
  1543. if (esp->erev == fashme) {
  1544. esp_cmd(esp, ESP_CMD_FLUSH); /* Grrr! */
  1545. /* Set up the DMA and HME counters */
  1546. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1547. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1548. sbus_writeb(0, esp->eregs + FAS_RLO);
  1549. sbus_writeb(0, esp->eregs + FAS_RHI);
  1550. esp_cmd(esp, the_esp_command);
  1551. /* Talk about touchy hardware... */
  1552. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  1553. (DMA_SCSI_DISAB | DMA_ENABLE)) &
  1554. ~(DMA_ST_WRITE));
  1555. sbus_writel(16, esp->dregs + DMA_COUNT);
  1556. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1557. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  1558. } else {
  1559. u32 tmp;
  1560. /* Set up the DMA and ESP counters */
  1561. sbus_writeb(i, esp->eregs + ESP_TCLOW);
  1562. sbus_writeb(0, esp->eregs + ESP_TCMED);
  1563. tmp = sbus_readl(esp->dregs + DMA_CSR);
  1564. tmp &= ~DMA_ST_WRITE;
  1565. tmp |= DMA_ENABLE;
  1566. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1567. if (esp->dma->revision == dvmaesc1) {
  1568. if (i) /* Workaround ESC gate array SBUS rerun bug. */
  1569. sbus_writel(PAGE_SIZE, esp->dregs + DMA_COUNT);
  1570. }
  1571. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  1572. /* Tell ESP to "go". */
  1573. esp_cmd(esp, the_esp_command);
  1574. }
  1575. }
  1576. /* Queue a SCSI command delivered from the mid-level Linux SCSI code. */
  1577. static int esp_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  1578. {
  1579. struct esp *esp;
  1580. /* Set up func ptr and initial driver cmd-phase. */
  1581. SCpnt->scsi_done = done;
  1582. SCpnt->SCp.phase = not_issued;
  1583. /* We use the scratch area. */
  1584. ESPQUEUE(("esp_queue: target=%d lun=%d ", SCpnt->device->id, SCpnt->device->lun));
  1585. ESPDISC(("N<%02x,%02x>", SCpnt->device->id, SCpnt->device->lun));
  1586. esp = (struct esp *) SCpnt->device->host->hostdata;
  1587. esp_get_dmabufs(esp, SCpnt);
  1588. esp_save_pointers(esp, SCpnt); /* FIXME for tag queueing */
  1589. SCpnt->SCp.Status = CHECK_CONDITION;
  1590. SCpnt->SCp.Message = 0xff;
  1591. SCpnt->SCp.sent_command = 0;
  1592. /* Place into our queue. */
  1593. if (SCpnt->cmnd[0] == REQUEST_SENSE) {
  1594. ESPQUEUE(("RQSENSE\n"));
  1595. prepend_SC(&esp->issue_SC, SCpnt);
  1596. } else {
  1597. ESPQUEUE(("\n"));
  1598. append_SC(&esp->issue_SC, SCpnt);
  1599. }
  1600. /* Run it now if we can. */
  1601. if (!esp->current_SC && !esp->resetting_bus)
  1602. esp_exec_cmd(esp);
  1603. return 0;
  1604. }
  1605. /* Dump driver state. */
  1606. static void esp_dump_cmd(struct scsi_cmnd *SCptr)
  1607. {
  1608. ESPLOG(("[tgt<%02x> lun<%02x> "
  1609. "pphase<%s> cphase<%s>]",
  1610. SCptr->device->id, SCptr->device->lun,
  1611. phase_string(SCptr->SCp.sent_command),
  1612. phase_string(SCptr->SCp.phase)));
  1613. }
  1614. static void esp_dump_state(struct esp *esp)
  1615. {
  1616. struct scsi_cmnd *SCptr = esp->current_SC;
  1617. #ifdef DEBUG_ESP_CMDS
  1618. int i;
  1619. #endif
  1620. ESPLOG(("esp%d: dumping state\n", esp->esp_id));
  1621. ESPLOG(("esp%d: dma -- cond_reg<%08x> addr<%08x>\n",
  1622. esp->esp_id,
  1623. sbus_readl(esp->dregs + DMA_CSR),
  1624. sbus_readl(esp->dregs + DMA_ADDR)));
  1625. ESPLOG(("esp%d: SW [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1626. esp->esp_id, esp->sreg, esp->seqreg, esp->ireg));
  1627. ESPLOG(("esp%d: HW reread [sreg<%02x> sstep<%02x> ireg<%02x>]\n",
  1628. esp->esp_id,
  1629. sbus_readb(esp->eregs + ESP_STATUS),
  1630. sbus_readb(esp->eregs + ESP_SSTEP),
  1631. sbus_readb(esp->eregs + ESP_INTRPT)));
  1632. #ifdef DEBUG_ESP_CMDS
  1633. printk("esp%d: last ESP cmds [", esp->esp_id);
  1634. i = (esp->espcmdent - 1) & 31;
  1635. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1636. i = (i - 1) & 31;
  1637. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1638. i = (i - 1) & 31;
  1639. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1640. i = (i - 1) & 31;
  1641. printk("<"); esp_print_cmd(esp->espcmdlog[i]); printk(">");
  1642. printk("]\n");
  1643. #endif /* (DEBUG_ESP_CMDS) */
  1644. if (SCptr) {
  1645. ESPLOG(("esp%d: current command ", esp->esp_id));
  1646. esp_dump_cmd(SCptr);
  1647. }
  1648. ESPLOG(("\n"));
  1649. SCptr = esp->disconnected_SC;
  1650. ESPLOG(("esp%d: disconnected ", esp->esp_id));
  1651. while (SCptr) {
  1652. esp_dump_cmd(SCptr);
  1653. SCptr = (struct scsi_cmnd *) SCptr->host_scribble;
  1654. }
  1655. ESPLOG(("\n"));
  1656. }
  1657. /* Abort a command. The host_lock is acquired by caller. */
  1658. static int esp_abort(struct scsi_cmnd *SCptr)
  1659. {
  1660. struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
  1661. int don;
  1662. ESPLOG(("esp%d: Aborting command\n", esp->esp_id));
  1663. esp_dump_state(esp);
  1664. /* Wheee, if this is the current command on the bus, the
  1665. * best we can do is assert ATN and wait for msgout phase.
  1666. * This should even fix a hung SCSI bus when we lose state
  1667. * in the driver and timeout because the eventual phase change
  1668. * will cause the ESP to (eventually) give an interrupt.
  1669. */
  1670. if (esp->current_SC == SCptr) {
  1671. esp->cur_msgout[0] = ABORT;
  1672. esp->msgout_len = 1;
  1673. esp->msgout_ctr = 0;
  1674. esp_cmd(esp, ESP_CMD_SATN);
  1675. return SUCCESS;
  1676. }
  1677. /* If it is still in the issue queue then we can safely
  1678. * call the completion routine and report abort success.
  1679. */
  1680. don = (sbus_readl(esp->dregs + DMA_CSR) & DMA_INT_ENAB);
  1681. if (don) {
  1682. ESP_INTSOFF(esp->dregs);
  1683. }
  1684. if (esp->issue_SC) {
  1685. struct scsi_cmnd **prev, *this;
  1686. for (prev = (&esp->issue_SC), this = esp->issue_SC;
  1687. this != NULL;
  1688. prev = (struct scsi_cmnd **) &(this->host_scribble),
  1689. this = (struct scsi_cmnd *) this->host_scribble) {
  1690. if (this == SCptr) {
  1691. *prev = (struct scsi_cmnd *) this->host_scribble;
  1692. this->host_scribble = NULL;
  1693. esp_release_dmabufs(esp, this);
  1694. this->result = DID_ABORT << 16;
  1695. this->scsi_done(this);
  1696. if (don)
  1697. ESP_INTSON(esp->dregs);
  1698. return SUCCESS;
  1699. }
  1700. }
  1701. }
  1702. /* Yuck, the command to abort is disconnected, it is not
  1703. * worth trying to abort it now if something else is live
  1704. * on the bus at this time. So, we let the SCSI code wait
  1705. * a little bit and try again later.
  1706. */
  1707. if (esp->current_SC) {
  1708. if (don)
  1709. ESP_INTSON(esp->dregs);
  1710. return FAILED;
  1711. }
  1712. /* It's disconnected, we have to reconnect to re-establish
  1713. * the nexus and tell the device to abort. However, we really
  1714. * cannot 'reconnect' per se. Don't try to be fancy, just
  1715. * indicate failure, which causes our caller to reset the whole
  1716. * bus.
  1717. */
  1718. if (don)
  1719. ESP_INTSON(esp->dregs);
  1720. return FAILED;
  1721. }
  1722. /* We've sent ESP_CMD_RS to the ESP, the interrupt had just
  1723. * arrived indicating the end of the SCSI bus reset. Our job
  1724. * is to clean out the command queues and begin re-execution
  1725. * of SCSI commands once more.
  1726. */
  1727. static int esp_finish_reset(struct esp *esp)
  1728. {
  1729. struct scsi_cmnd *sp = esp->current_SC;
  1730. /* Clean up currently executing command, if any. */
  1731. if (sp != NULL) {
  1732. esp->current_SC = NULL;
  1733. esp_release_dmabufs(esp, sp);
  1734. sp->result = (DID_RESET << 16);
  1735. sp->scsi_done(sp);
  1736. }
  1737. /* Clean up disconnected queue, they have been invalidated
  1738. * by the bus reset.
  1739. */
  1740. if (esp->disconnected_SC) {
  1741. while ((sp = remove_first_SC(&esp->disconnected_SC)) != NULL) {
  1742. esp_release_dmabufs(esp, sp);
  1743. sp->result = (DID_RESET << 16);
  1744. sp->scsi_done(sp);
  1745. }
  1746. }
  1747. /* SCSI bus reset is complete. */
  1748. esp->resetting_bus = 0;
  1749. wake_up(&esp->reset_queue);
  1750. /* Ok, now it is safe to get commands going once more. */
  1751. if (esp->issue_SC)
  1752. esp_exec_cmd(esp);
  1753. return do_intr_end;
  1754. }
  1755. static int esp_do_resetbus(struct esp *esp)
  1756. {
  1757. ESPLOG(("esp%d: Resetting scsi bus\n", esp->esp_id));
  1758. esp->resetting_bus = 1;
  1759. esp_cmd(esp, ESP_CMD_RS);
  1760. return do_intr_end;
  1761. }
  1762. /* Reset ESP chip, reset hanging bus, then kill active and
  1763. * disconnected commands for targets without soft reset.
  1764. *
  1765. * The host_lock is acquired by caller.
  1766. */
  1767. static int esp_reset(struct scsi_cmnd *SCptr)
  1768. {
  1769. struct esp *esp = (struct esp *) SCptr->device->host->hostdata;
  1770. spin_lock_irq(esp->ehost->host_lock);
  1771. (void) esp_do_resetbus(esp);
  1772. spin_unlock_irq(esp->ehost->host_lock);
  1773. wait_event(esp->reset_queue, (esp->resetting_bus == 0));
  1774. return SUCCESS;
  1775. }
  1776. /* Internal ESP done function. */
  1777. static void esp_done(struct esp *esp, int error)
  1778. {
  1779. struct scsi_cmnd *done_SC = esp->current_SC;
  1780. esp->current_SC = NULL;
  1781. esp_release_dmabufs(esp, done_SC);
  1782. done_SC->result = error;
  1783. done_SC->scsi_done(done_SC);
  1784. /* Bus is free, issue any commands in the queue. */
  1785. if (esp->issue_SC && !esp->current_SC)
  1786. esp_exec_cmd(esp);
  1787. }
  1788. /* Wheee, ESP interrupt engine. */
  1789. /* Forward declarations. */
  1790. static int esp_do_phase_determine(struct esp *esp);
  1791. static int esp_do_data_finale(struct esp *esp);
  1792. static int esp_select_complete(struct esp *esp);
  1793. static int esp_do_status(struct esp *esp);
  1794. static int esp_do_msgin(struct esp *esp);
  1795. static int esp_do_msgindone(struct esp *esp);
  1796. static int esp_do_msgout(struct esp *esp);
  1797. static int esp_do_cmdbegin(struct esp *esp);
  1798. #define sreg_datainp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DIP)
  1799. #define sreg_dataoutp(__sreg) (((__sreg) & ESP_STAT_PMASK) == ESP_DOP)
  1800. /* Read any bytes found in the FAS366 fifo, storing them into
  1801. * the ESP driver software state structure.
  1802. */
  1803. static void hme_fifo_read(struct esp *esp)
  1804. {
  1805. u8 count = 0;
  1806. u8 status = esp->sreg;
  1807. /* Cannot safely frob the fifo for these following cases, but
  1808. * we must always read the fifo when the reselect interrupt
  1809. * is pending.
  1810. */
  1811. if (((esp->ireg & ESP_INTR_RSEL) == 0) &&
  1812. (sreg_datainp(status) ||
  1813. sreg_dataoutp(status) ||
  1814. (esp->current_SC &&
  1815. esp->current_SC->SCp.phase == in_data_done))) {
  1816. ESPHME(("<wkaround_skipped>"));
  1817. } else {
  1818. unsigned long fcnt = sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES;
  1819. /* The HME stores bytes in multiples of 2 in the fifo. */
  1820. ESPHME(("hme_fifo[fcnt=%d", (int)fcnt));
  1821. while (fcnt) {
  1822. esp->hme_fifo_workaround_buffer[count++] =
  1823. sbus_readb(esp->eregs + ESP_FDATA);
  1824. esp->hme_fifo_workaround_buffer[count++] =
  1825. sbus_readb(esp->eregs + ESP_FDATA);
  1826. ESPHME(("<%02x,%02x>", esp->hme_fifo_workaround_buffer[count-2], esp->hme_fifo_workaround_buffer[count-1]));
  1827. fcnt--;
  1828. }
  1829. if (sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_F1BYTE) {
  1830. ESPHME(("<poke_byte>"));
  1831. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1832. esp->hme_fifo_workaround_buffer[count++] =
  1833. sbus_readb(esp->eregs + ESP_FDATA);
  1834. ESPHME(("<%02x,0x00>", esp->hme_fifo_workaround_buffer[count-1]));
  1835. ESPHME(("CMD_FLUSH"));
  1836. esp_cmd(esp, ESP_CMD_FLUSH);
  1837. } else {
  1838. ESPHME(("no_xtra_byte"));
  1839. }
  1840. }
  1841. ESPHME(("wkarnd_cnt=%d]", (int)count));
  1842. esp->hme_fifo_workaround_count = count;
  1843. }
  1844. static inline void hme_fifo_push(struct esp *esp, u8 *bytes, u8 count)
  1845. {
  1846. esp_cmd(esp, ESP_CMD_FLUSH);
  1847. while (count) {
  1848. u8 tmp = *bytes++;
  1849. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  1850. sbus_writeb(0, esp->eregs + ESP_FDATA);
  1851. count--;
  1852. }
  1853. }
  1854. /* We try to avoid some interrupts by jumping ahead and see if the ESP
  1855. * has gotten far enough yet. Hence the following.
  1856. */
  1857. static inline int skipahead1(struct esp *esp, struct scsi_cmnd *scp,
  1858. int prev_phase, int new_phase)
  1859. {
  1860. if (scp->SCp.sent_command != prev_phase)
  1861. return 0;
  1862. if (ESP_IRQ_P(esp->dregs)) {
  1863. /* Yes, we are able to save an interrupt. */
  1864. if (esp->erev == fashme)
  1865. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1866. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1867. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1868. if (esp->erev == fashme) {
  1869. /* This chip is really losing. */
  1870. ESPHME(("HME["));
  1871. /* Must latch fifo before reading the interrupt
  1872. * register else garbage ends up in the FIFO
  1873. * which confuses the driver utterly.
  1874. * Happy Meal indeed....
  1875. */
  1876. ESPHME(("fifo_workaround]"));
  1877. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1878. (esp->sreg2 & ESP_STAT2_F1BYTE))
  1879. hme_fifo_read(esp);
  1880. }
  1881. if (!(esp->ireg & ESP_INTR_SR))
  1882. return 0;
  1883. else
  1884. return do_reset_complete;
  1885. }
  1886. /* Ho hum, target is taking forever... */
  1887. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1888. return do_intr_end;
  1889. }
  1890. static inline int skipahead2(struct esp *esp, struct scsi_cmnd *scp,
  1891. int prev_phase1, int prev_phase2, int new_phase)
  1892. {
  1893. if (scp->SCp.sent_command != prev_phase1 &&
  1894. scp->SCp.sent_command != prev_phase2)
  1895. return 0;
  1896. if (ESP_IRQ_P(esp->dregs)) {
  1897. /* Yes, we are able to save an interrupt. */
  1898. if (esp->erev == fashme)
  1899. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  1900. esp->sreg = (sbus_readb(esp->eregs + ESP_STATUS) & ~(ESP_STAT_INTR));
  1901. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  1902. if (esp->erev == fashme) {
  1903. /* This chip is really losing. */
  1904. ESPHME(("HME["));
  1905. /* Must latch fifo before reading the interrupt
  1906. * register else garbage ends up in the FIFO
  1907. * which confuses the driver utterly.
  1908. * Happy Meal indeed....
  1909. */
  1910. ESPHME(("fifo_workaround]"));
  1911. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1912. (esp->sreg2 & ESP_STAT2_F1BYTE))
  1913. hme_fifo_read(esp);
  1914. }
  1915. if (!(esp->ireg & ESP_INTR_SR))
  1916. return 0;
  1917. else
  1918. return do_reset_complete;
  1919. }
  1920. /* Ho hum, target is taking forever... */
  1921. scp->SCp.sent_command = new_phase; /* so we don't recurse... */
  1922. return do_intr_end;
  1923. }
  1924. /* Now some dma helpers. */
  1925. static void dma_setup(struct esp *esp, __u32 addr, int count, int write)
  1926. {
  1927. u32 nreg = sbus_readl(esp->dregs + DMA_CSR);
  1928. if (write)
  1929. nreg |= DMA_ST_WRITE;
  1930. else
  1931. nreg &= ~(DMA_ST_WRITE);
  1932. nreg |= DMA_ENABLE;
  1933. sbus_writel(nreg, esp->dregs + DMA_CSR);
  1934. if (esp->dma->revision == dvmaesc1) {
  1935. /* This ESC gate array sucks! */
  1936. __u32 src = addr;
  1937. __u32 dest = src + count;
  1938. if (dest & (PAGE_SIZE - 1))
  1939. count = PAGE_ALIGN(count);
  1940. sbus_writel(count, esp->dregs + DMA_COUNT);
  1941. }
  1942. sbus_writel(addr, esp->dregs + DMA_ADDR);
  1943. }
  1944. static void dma_drain(struct esp *esp)
  1945. {
  1946. u32 tmp;
  1947. if (esp->dma->revision == dvmahme)
  1948. return;
  1949. if ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_FIFO_ISDRAIN) {
  1950. switch (esp->dma->revision) {
  1951. default:
  1952. tmp |= DMA_FIFO_STDRAIN;
  1953. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1954. case dvmarev3:
  1955. case dvmaesc1:
  1956. while (sbus_readl(esp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
  1957. udelay(1);
  1958. };
  1959. }
  1960. }
  1961. static void dma_invalidate(struct esp *esp)
  1962. {
  1963. u32 tmp;
  1964. if (esp->dma->revision == dvmahme) {
  1965. sbus_writel(DMA_RST_SCSI, esp->dregs + DMA_CSR);
  1966. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  1967. (DMA_PARITY_OFF | DMA_2CLKS |
  1968. DMA_SCSI_DISAB | DMA_INT_ENAB)) &
  1969. ~(DMA_ST_WRITE | DMA_ENABLE));
  1970. sbus_writel(0, esp->dregs + DMA_CSR);
  1971. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  1972. /* This is necessary to avoid having the SCSI channel
  1973. * engine lock up on us.
  1974. */
  1975. sbus_writel(0, esp->dregs + DMA_ADDR);
  1976. } else {
  1977. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  1978. udelay(1);
  1979. tmp &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
  1980. tmp |= DMA_FIFO_INV;
  1981. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1982. tmp &= ~DMA_FIFO_INV;
  1983. sbus_writel(tmp, esp->dregs + DMA_CSR);
  1984. }
  1985. }
  1986. static inline void dma_flashclear(struct esp *esp)
  1987. {
  1988. dma_drain(esp);
  1989. dma_invalidate(esp);
  1990. }
  1991. static int dma_can_transfer(struct esp *esp, struct scsi_cmnd *sp)
  1992. {
  1993. __u32 base, end, sz;
  1994. if (esp->dma->revision == dvmarev3) {
  1995. sz = sp->SCp.this_residual;
  1996. if (sz > 0x1000000)
  1997. sz = 0x1000000;
  1998. } else {
  1999. base = ((__u32)((unsigned long)sp->SCp.ptr));
  2000. base &= (0x1000000 - 1);
  2001. end = (base + sp->SCp.this_residual);
  2002. if (end > 0x1000000)
  2003. end = 0x1000000;
  2004. sz = (end - base);
  2005. }
  2006. return sz;
  2007. }
  2008. /* Misc. esp helper macros. */
  2009. #define esp_setcount(__eregs, __cnt, __hme) \
  2010. sbus_writeb(((__cnt)&0xff), (__eregs) + ESP_TCLOW); \
  2011. sbus_writeb((((__cnt)>>8)&0xff), (__eregs) + ESP_TCMED); \
  2012. if (__hme) { \
  2013. sbus_writeb((((__cnt)>>16)&0xff), (__eregs) + FAS_RLO); \
  2014. sbus_writeb(0, (__eregs) + FAS_RHI); \
  2015. }
  2016. #define esp_getcount(__eregs, __hme) \
  2017. ((sbus_readb((__eregs) + ESP_TCLOW)&0xff) | \
  2018. ((sbus_readb((__eregs) + ESP_TCMED)&0xff) << 8) | \
  2019. ((__hme) ? sbus_readb((__eregs) + FAS_RLO) << 16 : 0))
  2020. #define fcount(__esp) \
  2021. (((__esp)->erev == fashme) ? \
  2022. (__esp)->hme_fifo_workaround_count : \
  2023. sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_FBYTES)
  2024. #define fnzero(__esp) \
  2025. (((__esp)->erev == fashme) ? 0 : \
  2026. sbus_readb(((__esp)->eregs) + ESP_FFLAGS) & ESP_FF_ONOTZERO)
  2027. /* XXX speculative nops unnecessary when continuing amidst a data phase
  2028. * XXX even on esp100!!! another case of flooding the bus with I/O reg
  2029. * XXX writes...
  2030. */
  2031. #define esp_maybe_nop(__esp) \
  2032. if ((__esp)->erev == esp100) \
  2033. esp_cmd((__esp), ESP_CMD_NULL)
  2034. #define sreg_to_dataphase(__sreg) \
  2035. ((((__sreg) & ESP_STAT_PMASK) == ESP_DOP) ? in_dataout : in_datain)
  2036. /* The ESP100 when in synchronous data phase, can mistake a long final
  2037. * REQ pulse from the target as an extra byte, it places whatever is on
  2038. * the data lines into the fifo. For now, we will assume when this
  2039. * happens that the target is a bit quirky and we don't want to
  2040. * be talking synchronously to it anyways. Regardless, we need to
  2041. * tell the ESP to eat the extraneous byte so that we can proceed
  2042. * to the next phase.
  2043. */
  2044. static int esp100_sync_hwbug(struct esp *esp, struct scsi_cmnd *sp, int fifocnt)
  2045. {
  2046. /* Do not touch this piece of code. */
  2047. if ((!(esp->erev == esp100)) ||
  2048. (!(sreg_datainp((esp->sreg = sbus_readb(esp->eregs + ESP_STATUS))) &&
  2049. !fifocnt) &&
  2050. !(sreg_dataoutp(esp->sreg) && !fnzero(esp)))) {
  2051. if (sp->SCp.phase == in_dataout)
  2052. esp_cmd(esp, ESP_CMD_FLUSH);
  2053. return 0;
  2054. } else {
  2055. /* Async mode for this guy. */
  2056. build_sync_nego_msg(esp, 0, 0);
  2057. /* Ack the bogus byte, but set ATN first. */
  2058. esp_cmd(esp, ESP_CMD_SATN);
  2059. esp_cmd(esp, ESP_CMD_MOK);
  2060. return 1;
  2061. }
  2062. }
  2063. /* This closes the window during a selection with a reselect pending, because
  2064. * we use DMA for the selection process the FIFO should hold the correct
  2065. * contents if we get reselected during this process. So we just need to
  2066. * ack the possible illegal cmd interrupt pending on the esp100.
  2067. */
  2068. static inline int esp100_reconnect_hwbug(struct esp *esp)
  2069. {
  2070. u8 tmp;
  2071. if (esp->erev != esp100)
  2072. return 0;
  2073. tmp = sbus_readb(esp->eregs + ESP_INTRPT);
  2074. if (tmp & ESP_INTR_SR)
  2075. return 1;
  2076. return 0;
  2077. }
  2078. /* This verifies the BUSID bits during a reselection so that we know which
  2079. * target is talking to us.
  2080. */
  2081. static inline int reconnect_target(struct esp *esp)
  2082. {
  2083. int it, me = esp->scsi_id_mask, targ = 0;
  2084. if (2 != fcount(esp))
  2085. return -1;
  2086. if (esp->erev == fashme) {
  2087. /* HME does not latch it's own BUS ID bits during
  2088. * a reselection. Also the target number is given
  2089. * as an unsigned char, not as a sole bit number
  2090. * like the other ESP's do.
  2091. * Happy Meal indeed....
  2092. */
  2093. targ = esp->hme_fifo_workaround_buffer[0];
  2094. } else {
  2095. it = sbus_readb(esp->eregs + ESP_FDATA);
  2096. if (!(it & me))
  2097. return -1;
  2098. it &= ~me;
  2099. if (it & (it - 1))
  2100. return -1;
  2101. while (!(it & 1))
  2102. targ++, it >>= 1;
  2103. }
  2104. return targ;
  2105. }
  2106. /* This verifies the identify from the target so that we know which lun is
  2107. * being reconnected.
  2108. */
  2109. static inline int reconnect_lun(struct esp *esp)
  2110. {
  2111. int lun;
  2112. if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP)
  2113. return -1;
  2114. if (esp->erev == fashme)
  2115. lun = esp->hme_fifo_workaround_buffer[1];
  2116. else
  2117. lun = sbus_readb(esp->eregs + ESP_FDATA);
  2118. /* Yes, you read this correctly. We report lun of zero
  2119. * if we see parity error. ESP reports parity error for
  2120. * the lun byte, and this is the only way to hope to recover
  2121. * because the target is connected.
  2122. */
  2123. if (esp->sreg & ESP_STAT_PERR)
  2124. return 0;
  2125. /* Check for illegal bits being set in the lun. */
  2126. if ((lun & 0x40) || !(lun & 0x80))
  2127. return -1;
  2128. return lun & 7;
  2129. }
  2130. /* This puts the driver in a state where it can revitalize a command that
  2131. * is being continued due to reselection.
  2132. */
  2133. static inline void esp_connect(struct esp *esp, struct scsi_cmnd *sp)
  2134. {
  2135. struct esp_device *esp_dev = sp->device->hostdata;
  2136. if (esp->prev_soff != esp_dev->sync_max_offset ||
  2137. esp->prev_stp != esp_dev->sync_min_period ||
  2138. (esp->erev > esp100a &&
  2139. esp->prev_cfg3 != esp->config3[sp->device->id])) {
  2140. esp->prev_soff = esp_dev->sync_max_offset;
  2141. esp->prev_stp = esp_dev->sync_min_period;
  2142. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  2143. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  2144. if (esp->erev > esp100a) {
  2145. esp->prev_cfg3 = esp->config3[sp->device->id];
  2146. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  2147. }
  2148. }
  2149. esp->current_SC = sp;
  2150. }
  2151. /* This will place the current working command back into the issue queue
  2152. * if we are to receive a reselection amidst a selection attempt.
  2153. */
  2154. static inline void esp_reconnect(struct esp *esp, struct scsi_cmnd *sp)
  2155. {
  2156. if (!esp->disconnected_SC)
  2157. ESPLOG(("esp%d: Weird, being reselected but disconnected "
  2158. "command queue is empty.\n", esp->esp_id));
  2159. esp->snip = 0;
  2160. esp->current_SC = NULL;
  2161. sp->SCp.phase = not_issued;
  2162. append_SC(&esp->issue_SC, sp);
  2163. }
  2164. /* Begin message in phase. */
  2165. static int esp_do_msgin(struct esp *esp)
  2166. {
  2167. /* Must be very careful with the fifo on the HME */
  2168. if ((esp->erev != fashme) ||
  2169. !(sbus_readb(esp->eregs + ESP_STATUS2) & ESP_STAT2_FEMPTY))
  2170. esp_cmd(esp, ESP_CMD_FLUSH);
  2171. esp_maybe_nop(esp);
  2172. esp_cmd(esp, ESP_CMD_TI);
  2173. esp->msgin_len = 1;
  2174. esp->msgin_ctr = 0;
  2175. esp_advance_phase(esp->current_SC, in_msgindone);
  2176. return do_work_bus;
  2177. }
  2178. /* This uses various DMA csr fields and the fifo flags count value to
  2179. * determine how many bytes were successfully sent/received by the ESP.
  2180. */
  2181. static inline int esp_bytes_sent(struct esp *esp, int fifo_count)
  2182. {
  2183. int rval = sbus_readl(esp->dregs + DMA_ADDR) - esp->esp_command_dvma;
  2184. if (esp->dma->revision == dvmarev1)
  2185. rval -= (4 - ((sbus_readl(esp->dregs + DMA_CSR) & DMA_READ_AHEAD)>>11));
  2186. return rval - fifo_count;
  2187. }
  2188. static inline void advance_sg(struct scsi_cmnd *sp)
  2189. {
  2190. ++sp->SCp.buffer;
  2191. --sp->SCp.buffers_residual;
  2192. sp->SCp.this_residual = sg_dma_len(sp->SCp.buffer);
  2193. sp->SCp.ptr = (char *)((unsigned long)sg_dma_address(sp->SCp.buffer));
  2194. }
  2195. /* Please note that the way I've coded these routines is that I _always_
  2196. * check for a disconnect during any and all information transfer
  2197. * phases. The SCSI standard states that the target _can_ cause a BUS
  2198. * FREE condition by dropping all MSG/CD/IO/BSY signals. Also note
  2199. * that during information transfer phases the target controls every
  2200. * change in phase, the only thing the initiator can do is "ask" for
  2201. * a message out phase by driving ATN true. The target can, and sometimes
  2202. * will, completely ignore this request so we cannot assume anything when
  2203. * we try to force a message out phase to abort/reset a target. Most of
  2204. * the time the target will eventually be nice and go to message out, so
  2205. * we may have to hold on to our state about what we want to tell the target
  2206. * for some period of time.
  2207. */
  2208. /* I think I have things working here correctly. Even partial transfers
  2209. * within a buffer or sub-buffer should not upset us at all no matter
  2210. * how bad the target and/or ESP fucks things up.
  2211. */
  2212. static int esp_do_data(struct esp *esp)
  2213. {
  2214. struct scsi_cmnd *SCptr = esp->current_SC;
  2215. int thisphase, hmuch;
  2216. ESPDATA(("esp_do_data: "));
  2217. esp_maybe_nop(esp);
  2218. thisphase = sreg_to_dataphase(esp->sreg);
  2219. esp_advance_phase(SCptr, thisphase);
  2220. ESPDATA(("newphase<%s> ", (thisphase == in_datain) ? "DATAIN" : "DATAOUT"));
  2221. hmuch = dma_can_transfer(esp, SCptr);
  2222. if (hmuch > (64 * 1024) && (esp->erev != fashme))
  2223. hmuch = (64 * 1024);
  2224. ESPDATA(("hmuch<%d> ", hmuch));
  2225. esp->current_transfer_size = hmuch;
  2226. if (esp->erev == fashme) {
  2227. u32 tmp = esp->prev_hme_dmacsr;
  2228. /* Always set the ESP count registers first. */
  2229. esp_setcount(esp->eregs, hmuch, 1);
  2230. /* Get the DMA csr computed. */
  2231. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  2232. if (thisphase == in_datain)
  2233. tmp |= DMA_ST_WRITE;
  2234. else
  2235. tmp &= ~(DMA_ST_WRITE);
  2236. esp->prev_hme_dmacsr = tmp;
  2237. ESPDATA(("DMA|TI --> do_intr_end\n"));
  2238. if (thisphase == in_datain) {
  2239. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2240. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2241. } else {
  2242. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2243. sbus_writel(hmuch, esp->dregs + DMA_COUNT);
  2244. }
  2245. sbus_writel((__u32)((unsigned long)SCptr->SCp.ptr), esp->dregs+DMA_ADDR);
  2246. sbus_writel(esp->prev_hme_dmacsr, esp->dregs + DMA_CSR);
  2247. } else {
  2248. esp_setcount(esp->eregs, hmuch, 0);
  2249. dma_setup(esp, ((__u32)((unsigned long)SCptr->SCp.ptr)),
  2250. hmuch, (thisphase == in_datain));
  2251. ESPDATA(("DMA|TI --> do_intr_end\n"));
  2252. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  2253. }
  2254. return do_intr_end;
  2255. }
  2256. /* See how successful the data transfer was. */
  2257. static int esp_do_data_finale(struct esp *esp)
  2258. {
  2259. struct scsi_cmnd *SCptr = esp->current_SC;
  2260. struct esp_device *esp_dev = SCptr->device->hostdata;
  2261. int bogus_data = 0, bytes_sent = 0, fifocnt, ecount = 0;
  2262. ESPDATA(("esp_do_data_finale: "));
  2263. if (SCptr->SCp.phase == in_datain) {
  2264. if (esp->sreg & ESP_STAT_PERR) {
  2265. /* Yuck, parity error. The ESP asserts ATN
  2266. * so that we can go to message out phase
  2267. * immediately and inform the target that
  2268. * something bad happened.
  2269. */
  2270. ESPLOG(("esp%d: data bad parity detected.\n",
  2271. esp->esp_id));
  2272. esp->cur_msgout[0] = INITIATOR_ERROR;
  2273. esp->msgout_len = 1;
  2274. }
  2275. dma_drain(esp);
  2276. }
  2277. dma_invalidate(esp);
  2278. /* This could happen for the above parity error case. */
  2279. if (esp->ireg != ESP_INTR_BSERV) {
  2280. /* Please go to msgout phase, please please please... */
  2281. ESPLOG(("esp%d: !BSERV after data, probably to msgout\n",
  2282. esp->esp_id));
  2283. return esp_do_phase_determine(esp);
  2284. }
  2285. /* Check for partial transfers and other horrible events.
  2286. * Note, here we read the real fifo flags register even
  2287. * on HME broken adapters because we skip the HME fifo
  2288. * workaround code in esp_handle() if we are doing data
  2289. * phase things. We don't want to fuck directly with
  2290. * the fifo like that, especially if doing synchronous
  2291. * transfers! Also, will need to double the count on
  2292. * HME if we are doing wide transfers, as the HME fifo
  2293. * will move and count 16-bit quantities during wide data.
  2294. * SMCC _and_ Qlogic can both bite me.
  2295. */
  2296. fifocnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2297. if (esp->erev != fashme)
  2298. ecount = esp_getcount(esp->eregs, 0);
  2299. bytes_sent = esp->current_transfer_size;
  2300. ESPDATA(("trans_sz(%d), ", bytes_sent));
  2301. if (esp->erev == fashme) {
  2302. if (!(esp->sreg & ESP_STAT_TCNT)) {
  2303. ecount = esp_getcount(esp->eregs, 1);
  2304. bytes_sent -= ecount;
  2305. }
  2306. /* Always subtract any cruft remaining in the FIFO. */
  2307. if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
  2308. fifocnt <<= 1;
  2309. if (SCptr->SCp.phase == in_dataout)
  2310. bytes_sent -= fifocnt;
  2311. /* I have an IBM disk which exhibits the following
  2312. * behavior during writes to it. It disconnects in
  2313. * the middle of a partial transfer, the current sglist
  2314. * buffer is 1024 bytes, the disk stops data transfer
  2315. * at 512 bytes.
  2316. *
  2317. * However the FAS366 reports that 32 more bytes were
  2318. * transferred than really were. This is precisely
  2319. * the size of a fully loaded FIFO in wide scsi mode.
  2320. * The FIFO state recorded indicates that it is empty.
  2321. *
  2322. * I have no idea if this is a bug in the FAS366 chip
  2323. * or a bug in the firmware on this IBM disk. In any
  2324. * event the following seems to be a good workaround. -DaveM
  2325. */
  2326. if (bytes_sent != esp->current_transfer_size &&
  2327. SCptr->SCp.phase == in_dataout) {
  2328. int mask = (64 - 1);
  2329. if ((esp->prev_cfg3 & ESP_CONFIG3_EWIDE) == 0)
  2330. mask >>= 1;
  2331. if (bytes_sent & mask)
  2332. bytes_sent -= (bytes_sent & mask);
  2333. }
  2334. } else {
  2335. if (!(esp->sreg & ESP_STAT_TCNT))
  2336. bytes_sent -= ecount;
  2337. if (SCptr->SCp.phase == in_dataout)
  2338. bytes_sent -= fifocnt;
  2339. }
  2340. ESPDATA(("bytes_sent(%d), ", bytes_sent));
  2341. /* If we were in synchronous mode, check for peculiarities. */
  2342. if (esp->erev == fashme) {
  2343. if (esp_dev->sync_max_offset) {
  2344. if (SCptr->SCp.phase == in_dataout)
  2345. esp_cmd(esp, ESP_CMD_FLUSH);
  2346. } else {
  2347. esp_cmd(esp, ESP_CMD_FLUSH);
  2348. }
  2349. } else {
  2350. if (esp_dev->sync_max_offset)
  2351. bogus_data = esp100_sync_hwbug(esp, SCptr, fifocnt);
  2352. else
  2353. esp_cmd(esp, ESP_CMD_FLUSH);
  2354. }
  2355. /* Until we are sure of what has happened, we are certainly
  2356. * in the dark.
  2357. */
  2358. esp_advance_phase(SCptr, in_the_dark);
  2359. if (bytes_sent < 0) {
  2360. /* I've seen this happen due to lost state in this
  2361. * driver. No idea why it happened, but allowing
  2362. * this value to be negative caused things to
  2363. * lock up. This allows greater chance of recovery.
  2364. * In fact every time I've seen this, it has been
  2365. * a driver bug without question.
  2366. */
  2367. ESPLOG(("esp%d: yieee, bytes_sent < 0!\n", esp->esp_id));
  2368. ESPLOG(("esp%d: csz=%d fifocount=%d ecount=%d\n",
  2369. esp->esp_id,
  2370. esp->current_transfer_size, fifocnt, ecount));
  2371. ESPLOG(("esp%d: use_sg=%d ptr=%p this_residual=%d\n",
  2372. esp->esp_id,
  2373. SCptr->use_sg, SCptr->SCp.ptr, SCptr->SCp.this_residual));
  2374. ESPLOG(("esp%d: Forcing async for target %d\n", esp->esp_id,
  2375. SCptr->device->id));
  2376. SCptr->device->borken = 1;
  2377. esp_dev->sync = 0;
  2378. bytes_sent = 0;
  2379. }
  2380. /* Update the state of our transfer. */
  2381. SCptr->SCp.ptr += bytes_sent;
  2382. SCptr->SCp.this_residual -= bytes_sent;
  2383. if (SCptr->SCp.this_residual < 0) {
  2384. /* shit */
  2385. ESPLOG(("esp%d: Data transfer overrun.\n", esp->esp_id));
  2386. SCptr->SCp.this_residual = 0;
  2387. }
  2388. /* Maybe continue. */
  2389. if (!bogus_data) {
  2390. ESPDATA(("!bogus_data, "));
  2391. /* NO MATTER WHAT, we advance the scatterlist,
  2392. * if the target should decide to disconnect
  2393. * in between scatter chunks (which is common)
  2394. * we could die horribly! I used to have the sg
  2395. * advance occur only if we are going back into
  2396. * (or are staying in) a data phase, you can
  2397. * imagine the hell I went through trying to
  2398. * figure this out.
  2399. */
  2400. if (SCptr->use_sg && !SCptr->SCp.this_residual)
  2401. advance_sg(SCptr);
  2402. if (sreg_datainp(esp->sreg) || sreg_dataoutp(esp->sreg)) {
  2403. ESPDATA(("to more data\n"));
  2404. return esp_do_data(esp);
  2405. }
  2406. ESPDATA(("to new phase\n"));
  2407. return esp_do_phase_determine(esp);
  2408. }
  2409. /* Bogus data, just wait for next interrupt. */
  2410. ESPLOG(("esp%d: bogus_data during end of data phase\n",
  2411. esp->esp_id));
  2412. return do_intr_end;
  2413. }
  2414. /* We received a non-good status return at the end of
  2415. * running a SCSI command. This is used to decide if
  2416. * we should clear our synchronous transfer state for
  2417. * such a device when that happens.
  2418. *
  2419. * The idea is that when spinning up a disk or rewinding
  2420. * a tape, we don't want to go into a loop re-negotiating
  2421. * synchronous capabilities over and over.
  2422. */
  2423. static int esp_should_clear_sync(struct scsi_cmnd *sp)
  2424. {
  2425. u8 cmd1 = sp->cmnd[0];
  2426. u8 cmd2 = sp->data_cmnd[0];
  2427. /* These cases are for spinning up a disk and
  2428. * waiting for that spinup to complete.
  2429. */
  2430. if (cmd1 == START_STOP ||
  2431. cmd2 == START_STOP)
  2432. return 0;
  2433. if (cmd1 == TEST_UNIT_READY ||
  2434. cmd2 == TEST_UNIT_READY)
  2435. return 0;
  2436. /* One more special case for SCSI tape drives,
  2437. * this is what is used to probe the device for
  2438. * completion of a rewind or tape load operation.
  2439. */
  2440. if (sp->device->type == TYPE_TAPE) {
  2441. if (cmd1 == MODE_SENSE ||
  2442. cmd2 == MODE_SENSE)
  2443. return 0;
  2444. }
  2445. return 1;
  2446. }
  2447. /* Either a command is completing or a target is dropping off the bus
  2448. * to continue the command in the background so we can do other work.
  2449. */
  2450. static int esp_do_freebus(struct esp *esp)
  2451. {
  2452. struct scsi_cmnd *SCptr = esp->current_SC;
  2453. struct esp_device *esp_dev = SCptr->device->hostdata;
  2454. int rval;
  2455. rval = skipahead2(esp, SCptr, in_status, in_msgindone, in_freeing);
  2456. if (rval)
  2457. return rval;
  2458. if (esp->ireg != ESP_INTR_DC) {
  2459. ESPLOG(("esp%d: Target will not disconnect\n", esp->esp_id));
  2460. return do_reset_bus; /* target will not drop BSY... */
  2461. }
  2462. esp->msgout_len = 0;
  2463. esp->prevmsgout = NOP;
  2464. if (esp->prevmsgin == COMMAND_COMPLETE) {
  2465. /* Normal end of nexus. */
  2466. if (esp->disconnected_SC || (esp->erev == fashme))
  2467. esp_cmd(esp, ESP_CMD_ESEL);
  2468. if (SCptr->SCp.Status != GOOD &&
  2469. SCptr->SCp.Status != CONDITION_GOOD &&
  2470. ((1<<SCptr->device->id) & esp->targets_present) &&
  2471. esp_dev->sync &&
  2472. esp_dev->sync_max_offset) {
  2473. /* SCSI standard says that the synchronous capabilities
  2474. * should be renegotiated at this point. Most likely
  2475. * we are about to request sense from this target
  2476. * in which case we want to avoid using sync
  2477. * transfers until we are sure of the current target
  2478. * state.
  2479. */
  2480. ESPMISC(("esp: Status <%d> for target %d lun %d\n",
  2481. SCptr->SCp.Status, SCptr->device->id, SCptr->device->lun));
  2482. /* But don't do this when spinning up a disk at
  2483. * boot time while we poll for completion as it
  2484. * fills up the console with messages. Also, tapes
  2485. * can report not ready many times right after
  2486. * loading up a tape.
  2487. */
  2488. if (esp_should_clear_sync(SCptr) != 0)
  2489. esp_dev->sync = 0;
  2490. }
  2491. ESPDISC(("F<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  2492. esp_done(esp, ((SCptr->SCp.Status & 0xff) |
  2493. ((SCptr->SCp.Message & 0xff)<<8) |
  2494. (DID_OK << 16)));
  2495. } else if (esp->prevmsgin == DISCONNECT) {
  2496. /* Normal disconnect. */
  2497. esp_cmd(esp, ESP_CMD_ESEL);
  2498. ESPDISC(("D<%02x,%02x>", SCptr->device->id, SCptr->device->lun));
  2499. append_SC(&esp->disconnected_SC, SCptr);
  2500. esp->current_SC = NULL;
  2501. if (esp->issue_SC)
  2502. esp_exec_cmd(esp);
  2503. } else {
  2504. /* Driver bug, we do not expect a disconnect here
  2505. * and should not have advanced the state engine
  2506. * to in_freeing.
  2507. */
  2508. ESPLOG(("esp%d: last msg not disc and not cmd cmplt.\n",
  2509. esp->esp_id));
  2510. return do_reset_bus;
  2511. }
  2512. return do_intr_end;
  2513. }
  2514. /* When a reselect occurs, and we cannot find the command to
  2515. * reconnect to in our queues, we do this.
  2516. */
  2517. static int esp_bad_reconnect(struct esp *esp)
  2518. {
  2519. struct scsi_cmnd *sp;
  2520. ESPLOG(("esp%d: Eieeee, reconnecting unknown command!\n",
  2521. esp->esp_id));
  2522. ESPLOG(("QUEUE DUMP\n"));
  2523. sp = esp->issue_SC;
  2524. ESPLOG(("esp%d: issue_SC[", esp->esp_id));
  2525. while (sp) {
  2526. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2527. sp = (struct scsi_cmnd *) sp->host_scribble;
  2528. }
  2529. ESPLOG(("]\n"));
  2530. sp = esp->current_SC;
  2531. ESPLOG(("esp%d: current_SC[", esp->esp_id));
  2532. if (sp)
  2533. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2534. else
  2535. ESPLOG(("<NULL>"));
  2536. ESPLOG(("]\n"));
  2537. sp = esp->disconnected_SC;
  2538. ESPLOG(("esp%d: disconnected_SC[", esp->esp_id));
  2539. while (sp) {
  2540. ESPLOG(("<%02x,%02x>", sp->device->id, sp->device->lun));
  2541. sp = (struct scsi_cmnd *) sp->host_scribble;
  2542. }
  2543. ESPLOG(("]\n"));
  2544. return do_reset_bus;
  2545. }
  2546. /* Do the needy when a target tries to reconnect to us. */
  2547. static int esp_do_reconnect(struct esp *esp)
  2548. {
  2549. int lun, target;
  2550. struct scsi_cmnd *SCptr;
  2551. /* Check for all bogus conditions first. */
  2552. target = reconnect_target(esp);
  2553. if (target < 0) {
  2554. ESPDISC(("bad bus bits\n"));
  2555. return do_reset_bus;
  2556. }
  2557. lun = reconnect_lun(esp);
  2558. if (lun < 0) {
  2559. ESPDISC(("target=%2x, bad identify msg\n", target));
  2560. return do_reset_bus;
  2561. }
  2562. /* Things look ok... */
  2563. ESPDISC(("R<%02x,%02x>", target, lun));
  2564. /* Must not flush FIFO or DVMA on HME. */
  2565. if (esp->erev != fashme) {
  2566. esp_cmd(esp, ESP_CMD_FLUSH);
  2567. if (esp100_reconnect_hwbug(esp))
  2568. return do_reset_bus;
  2569. esp_cmd(esp, ESP_CMD_NULL);
  2570. }
  2571. SCptr = remove_SC(&esp->disconnected_SC, (u8) target, (u8) lun);
  2572. if (!SCptr)
  2573. return esp_bad_reconnect(esp);
  2574. esp_connect(esp, SCptr);
  2575. esp_cmd(esp, ESP_CMD_MOK);
  2576. if (esp->erev == fashme)
  2577. sbus_writeb(((SCptr->device->id & 0xf) |
  2578. (ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT)),
  2579. esp->eregs + ESP_BUSID);
  2580. /* Reconnect implies a restore pointers operation. */
  2581. esp_restore_pointers(esp, SCptr);
  2582. esp->snip = 0;
  2583. esp_advance_phase(SCptr, in_the_dark);
  2584. return do_intr_end;
  2585. }
  2586. /* End of NEXUS (hopefully), pick up status + message byte then leave if
  2587. * all goes well.
  2588. */
  2589. static int esp_do_status(struct esp *esp)
  2590. {
  2591. struct scsi_cmnd *SCptr = esp->current_SC;
  2592. int intr, rval;
  2593. rval = skipahead1(esp, SCptr, in_the_dark, in_status);
  2594. if (rval)
  2595. return rval;
  2596. intr = esp->ireg;
  2597. ESPSTAT(("esp_do_status: "));
  2598. if (intr != ESP_INTR_DC) {
  2599. int message_out = 0; /* for parity problems */
  2600. /* Ack the message. */
  2601. ESPSTAT(("ack msg, "));
  2602. esp_cmd(esp, ESP_CMD_MOK);
  2603. if (esp->erev != fashme) {
  2604. dma_flashclear(esp);
  2605. /* Wait till the first bits settle. */
  2606. while (esp->esp_command[0] == 0xff)
  2607. udelay(1);
  2608. } else {
  2609. esp->esp_command[0] = esp->hme_fifo_workaround_buffer[0];
  2610. esp->esp_command[1] = esp->hme_fifo_workaround_buffer[1];
  2611. }
  2612. ESPSTAT(("got something, "));
  2613. /* ESP chimes in with one of
  2614. *
  2615. * 1) function done interrupt:
  2616. * both status and message in bytes
  2617. * are available
  2618. *
  2619. * 2) bus service interrupt:
  2620. * only status byte was acquired
  2621. *
  2622. * 3) Anything else:
  2623. * can't happen, but we test for it
  2624. * anyways
  2625. *
  2626. * ALSO: If bad parity was detected on either
  2627. * the status _or_ the message byte then
  2628. * the ESP has asserted ATN on the bus
  2629. * and we must therefore wait for the
  2630. * next phase change.
  2631. */
  2632. if (intr & ESP_INTR_FDONE) {
  2633. /* We got it all, hallejulia. */
  2634. ESPSTAT(("got both, "));
  2635. SCptr->SCp.Status = esp->esp_command[0];
  2636. SCptr->SCp.Message = esp->esp_command[1];
  2637. esp->prevmsgin = SCptr->SCp.Message;
  2638. esp->cur_msgin[0] = SCptr->SCp.Message;
  2639. if (esp->sreg & ESP_STAT_PERR) {
  2640. /* There was bad parity for the
  2641. * message byte, the status byte
  2642. * was ok.
  2643. */
  2644. message_out = MSG_PARITY_ERROR;
  2645. }
  2646. } else if (intr == ESP_INTR_BSERV) {
  2647. /* Only got status byte. */
  2648. ESPLOG(("esp%d: got status only, ", esp->esp_id));
  2649. if (!(esp->sreg & ESP_STAT_PERR)) {
  2650. SCptr->SCp.Status = esp->esp_command[0];
  2651. SCptr->SCp.Message = 0xff;
  2652. } else {
  2653. /* The status byte had bad parity.
  2654. * we leave the scsi_pointer Status
  2655. * field alone as we set it to a default
  2656. * of CHECK_CONDITION in esp_queue.
  2657. */
  2658. message_out = INITIATOR_ERROR;
  2659. }
  2660. } else {
  2661. /* This shouldn't happen ever. */
  2662. ESPSTAT(("got bolixed\n"));
  2663. esp_advance_phase(SCptr, in_the_dark);
  2664. return esp_do_phase_determine(esp);
  2665. }
  2666. if (!message_out) {
  2667. ESPSTAT(("status=%2x msg=%2x, ", SCptr->SCp.Status,
  2668. SCptr->SCp.Message));
  2669. if (SCptr->SCp.Message == COMMAND_COMPLETE) {
  2670. ESPSTAT(("and was COMMAND_COMPLETE\n"));
  2671. esp_advance_phase(SCptr, in_freeing);
  2672. return esp_do_freebus(esp);
  2673. } else {
  2674. ESPLOG(("esp%d: and _not_ COMMAND_COMPLETE\n",
  2675. esp->esp_id));
  2676. esp->msgin_len = esp->msgin_ctr = 1;
  2677. esp_advance_phase(SCptr, in_msgindone);
  2678. return esp_do_msgindone(esp);
  2679. }
  2680. } else {
  2681. /* With luck we'll be able to let the target
  2682. * know that bad parity happened, it will know
  2683. * which byte caused the problems and send it
  2684. * again. For the case where the status byte
  2685. * receives bad parity, I do not believe most
  2686. * targets recover very well. We'll see.
  2687. */
  2688. ESPLOG(("esp%d: bad parity somewhere mout=%2x\n",
  2689. esp->esp_id, message_out));
  2690. esp->cur_msgout[0] = message_out;
  2691. esp->msgout_len = esp->msgout_ctr = 1;
  2692. esp_advance_phase(SCptr, in_the_dark);
  2693. return esp_do_phase_determine(esp);
  2694. }
  2695. } else {
  2696. /* If we disconnect now, all hell breaks loose. */
  2697. ESPLOG(("esp%d: whoops, disconnect\n", esp->esp_id));
  2698. esp_advance_phase(SCptr, in_the_dark);
  2699. return esp_do_phase_determine(esp);
  2700. }
  2701. }
  2702. static int esp_enter_status(struct esp *esp)
  2703. {
  2704. u8 thecmd = ESP_CMD_ICCSEQ;
  2705. esp_cmd(esp, ESP_CMD_FLUSH);
  2706. if (esp->erev != fashme) {
  2707. u32 tmp;
  2708. esp->esp_command[0] = esp->esp_command[1] = 0xff;
  2709. sbus_writeb(2, esp->eregs + ESP_TCLOW);
  2710. sbus_writeb(0, esp->eregs + ESP_TCMED);
  2711. tmp = sbus_readl(esp->dregs + DMA_CSR);
  2712. tmp |= (DMA_ST_WRITE | DMA_ENABLE);
  2713. sbus_writel(tmp, esp->dregs + DMA_CSR);
  2714. if (esp->dma->revision == dvmaesc1)
  2715. sbus_writel(0x100, esp->dregs + DMA_COUNT);
  2716. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  2717. thecmd |= ESP_CMD_DMA;
  2718. }
  2719. esp_cmd(esp, thecmd);
  2720. esp_advance_phase(esp->current_SC, in_status);
  2721. return esp_do_status(esp);
  2722. }
  2723. static int esp_disconnect_amidst_phases(struct esp *esp)
  2724. {
  2725. struct scsi_cmnd *sp = esp->current_SC;
  2726. struct esp_device *esp_dev = sp->device->hostdata;
  2727. /* This means real problems if we see this
  2728. * here. Unless we were actually trying
  2729. * to force the device to abort/reset.
  2730. */
  2731. ESPLOG(("esp%d Disconnect amidst phases, ", esp->esp_id));
  2732. ESPLOG(("pphase<%s> cphase<%s>, ",
  2733. phase_string(sp->SCp.phase),
  2734. phase_string(sp->SCp.sent_command)));
  2735. if (esp->disconnected_SC != NULL || (esp->erev == fashme))
  2736. esp_cmd(esp, ESP_CMD_ESEL);
  2737. switch (esp->cur_msgout[0]) {
  2738. default:
  2739. /* We didn't expect this to happen at all. */
  2740. ESPLOG(("device is bolixed\n"));
  2741. esp_advance_phase(sp, in_tgterror);
  2742. esp_done(esp, (DID_ERROR << 16));
  2743. break;
  2744. case BUS_DEVICE_RESET:
  2745. ESPLOG(("device reset successful\n"));
  2746. esp_dev->sync_max_offset = 0;
  2747. esp_dev->sync_min_period = 0;
  2748. esp_dev->sync = 0;
  2749. esp_advance_phase(sp, in_resetdev);
  2750. esp_done(esp, (DID_RESET << 16));
  2751. break;
  2752. case ABORT:
  2753. ESPLOG(("device abort successful\n"));
  2754. esp_advance_phase(sp, in_abortone);
  2755. esp_done(esp, (DID_ABORT << 16));
  2756. break;
  2757. };
  2758. return do_intr_end;
  2759. }
  2760. static int esp_enter_msgout(struct esp *esp)
  2761. {
  2762. esp_advance_phase(esp->current_SC, in_msgout);
  2763. return esp_do_msgout(esp);
  2764. }
  2765. static int esp_enter_msgin(struct esp *esp)
  2766. {
  2767. esp_advance_phase(esp->current_SC, in_msgin);
  2768. return esp_do_msgin(esp);
  2769. }
  2770. static int esp_enter_cmd(struct esp *esp)
  2771. {
  2772. esp_advance_phase(esp->current_SC, in_cmdbegin);
  2773. return esp_do_cmdbegin(esp);
  2774. }
  2775. static int esp_enter_badphase(struct esp *esp)
  2776. {
  2777. ESPLOG(("esp%d: Bizarre bus phase %2x.\n", esp->esp_id,
  2778. esp->sreg & ESP_STAT_PMASK));
  2779. return do_reset_bus;
  2780. }
  2781. typedef int (*espfunc_t)(struct esp *);
  2782. static espfunc_t phase_vector[] = {
  2783. esp_do_data, /* ESP_DOP */
  2784. esp_do_data, /* ESP_DIP */
  2785. esp_enter_cmd, /* ESP_CMDP */
  2786. esp_enter_status, /* ESP_STATP */
  2787. esp_enter_badphase, /* ESP_STAT_PMSG */
  2788. esp_enter_badphase, /* ESP_STAT_PMSG | ESP_STAT_PIO */
  2789. esp_enter_msgout, /* ESP_MOP */
  2790. esp_enter_msgin, /* ESP_MIP */
  2791. };
  2792. /* The target has control of the bus and we have to see where it has
  2793. * taken us.
  2794. */
  2795. static int esp_do_phase_determine(struct esp *esp)
  2796. {
  2797. if ((esp->ireg & ESP_INTR_DC) != 0)
  2798. return esp_disconnect_amidst_phases(esp);
  2799. return phase_vector[esp->sreg & ESP_STAT_PMASK](esp);
  2800. }
  2801. /* First interrupt after exec'ing a cmd comes here. */
  2802. static int esp_select_complete(struct esp *esp)
  2803. {
  2804. struct scsi_cmnd *SCptr = esp->current_SC;
  2805. struct esp_device *esp_dev = SCptr->device->hostdata;
  2806. int cmd_bytes_sent, fcnt;
  2807. if (esp->erev != fashme)
  2808. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  2809. if (esp->erev == fashme)
  2810. fcnt = esp->hme_fifo_workaround_count;
  2811. else
  2812. fcnt = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES);
  2813. cmd_bytes_sent = esp_bytes_sent(esp, fcnt);
  2814. dma_invalidate(esp);
  2815. /* Let's check to see if a reselect happened
  2816. * while we we're trying to select. This must
  2817. * be checked first.
  2818. */
  2819. if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
  2820. esp_reconnect(esp, SCptr);
  2821. return esp_do_reconnect(esp);
  2822. }
  2823. /* Looks like things worked, we should see a bus service &
  2824. * a function complete interrupt at this point. Note we
  2825. * are doing a direct comparison because we don't want to
  2826. * be fooled into thinking selection was successful if
  2827. * ESP_INTR_DC is set, see below.
  2828. */
  2829. if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
  2830. /* target speaks... */
  2831. esp->targets_present |= (1<<SCptr->device->id);
  2832. /* What if the target ignores the sdtr? */
  2833. if (esp->snip)
  2834. esp_dev->sync = 1;
  2835. /* See how far, if at all, we got in getting
  2836. * the information out to the target.
  2837. */
  2838. switch (esp->seqreg) {
  2839. default:
  2840. case ESP_STEP_ASEL:
  2841. /* Arbitration won, target selected, but
  2842. * we are in some phase which is not command
  2843. * phase nor is it message out phase.
  2844. *
  2845. * XXX We've confused the target, obviously.
  2846. * XXX So clear it's state, but we also end
  2847. * XXX up clearing everyone elses. That isn't
  2848. * XXX so nice. I'd like to just reset this
  2849. * XXX target, but if I cannot even get it's
  2850. * XXX attention and finish selection to talk
  2851. * XXX to it, there is not much more I can do.
  2852. * XXX If we have a loaded bus we're going to
  2853. * XXX spend the next second or so renegotiating
  2854. * XXX for synchronous transfers.
  2855. */
  2856. ESPLOG(("esp%d: STEP_ASEL for tgt %d\n",
  2857. esp->esp_id, SCptr->device->id));
  2858. case ESP_STEP_SID:
  2859. /* Arbitration won, target selected, went
  2860. * to message out phase, sent one message
  2861. * byte, then we stopped. ATN is asserted
  2862. * on the SCSI bus and the target is still
  2863. * there hanging on. This is a legal
  2864. * sequence step if we gave the ESP a select
  2865. * and stop command.
  2866. *
  2867. * XXX See above, I could set the borken flag
  2868. * XXX in the device struct and retry the
  2869. * XXX command. But would that help for
  2870. * XXX tagged capable targets?
  2871. */
  2872. case ESP_STEP_NCMD:
  2873. /* Arbitration won, target selected, maybe
  2874. * sent the one message byte in message out
  2875. * phase, but we did not go to command phase
  2876. * in the end. Actually, we could have sent
  2877. * only some of the message bytes if we tried
  2878. * to send out the entire identify and tag
  2879. * message using ESP_CMD_SA3.
  2880. */
  2881. cmd_bytes_sent = 0;
  2882. break;
  2883. case ESP_STEP_PPC:
  2884. /* No, not the powerPC pinhead. Arbitration
  2885. * won, all message bytes sent if we went to
  2886. * message out phase, went to command phase
  2887. * but only part of the command was sent.
  2888. *
  2889. * XXX I've seen this, but usually in conjunction
  2890. * XXX with a gross error which appears to have
  2891. * XXX occurred between the time I told the
  2892. * XXX ESP to arbitrate and when I got the
  2893. * XXX interrupt. Could I have misloaded the
  2894. * XXX command bytes into the fifo? Actually,
  2895. * XXX I most likely missed a phase, and therefore
  2896. * XXX went into never never land and didn't even
  2897. * XXX know it. That was the old driver though.
  2898. * XXX What is even more peculiar is that the ESP
  2899. * XXX showed the proper function complete and
  2900. * XXX bus service bits in the interrupt register.
  2901. */
  2902. case ESP_STEP_FINI4:
  2903. case ESP_STEP_FINI5:
  2904. case ESP_STEP_FINI6:
  2905. case ESP_STEP_FINI7:
  2906. /* Account for the identify message */
  2907. if (SCptr->SCp.phase == in_slct_norm)
  2908. cmd_bytes_sent -= 1;
  2909. };
  2910. if (esp->erev != fashme)
  2911. esp_cmd(esp, ESP_CMD_NULL);
  2912. /* Be careful, we could really get fucked during synchronous
  2913. * data transfers if we try to flush the fifo now.
  2914. */
  2915. if ((esp->erev != fashme) && /* not a Happy Meal and... */
  2916. !fcnt && /* Fifo is empty and... */
  2917. /* either we are not doing synchronous transfers or... */
  2918. (!esp_dev->sync_max_offset ||
  2919. /* We are not going into data in phase. */
  2920. ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
  2921. esp_cmd(esp, ESP_CMD_FLUSH); /* flush is safe */
  2922. /* See how far we got if this is not a slow command. */
  2923. if (!esp->esp_slowcmd) {
  2924. if (cmd_bytes_sent < 0)
  2925. cmd_bytes_sent = 0;
  2926. if (cmd_bytes_sent != SCptr->cmd_len) {
  2927. /* Crapola, mark it as a slowcmd
  2928. * so that we have some chance of
  2929. * keeping the command alive with
  2930. * good luck.
  2931. *
  2932. * XXX Actually, if we didn't send it all
  2933. * XXX this means either we didn't set things
  2934. * XXX up properly (driver bug) or the target
  2935. * XXX or the ESP detected parity on one of
  2936. * XXX the command bytes. This makes much
  2937. * XXX more sense, and therefore this code
  2938. * XXX should be changed to send out a
  2939. * XXX parity error message or if the status
  2940. * XXX register shows no parity error then
  2941. * XXX just expect the target to bring the
  2942. * XXX bus into message in phase so that it
  2943. * XXX can send us the parity error message.
  2944. * XXX SCSI sucks...
  2945. */
  2946. esp->esp_slowcmd = 1;
  2947. esp->esp_scmdp = &(SCptr->cmnd[cmd_bytes_sent]);
  2948. esp->esp_scmdleft = (SCptr->cmd_len - cmd_bytes_sent);
  2949. }
  2950. }
  2951. /* Now figure out where we went. */
  2952. esp_advance_phase(SCptr, in_the_dark);
  2953. return esp_do_phase_determine(esp);
  2954. }
  2955. /* Did the target even make it? */
  2956. if (esp->ireg == ESP_INTR_DC) {
  2957. /* wheee... nobody there or they didn't like
  2958. * what we told it to do, clean up.
  2959. */
  2960. /* If anyone is off the bus, but working on
  2961. * a command in the background for us, tell
  2962. * the ESP to listen for them.
  2963. */
  2964. if (esp->disconnected_SC)
  2965. esp_cmd(esp, ESP_CMD_ESEL);
  2966. if (((1<<SCptr->device->id) & esp->targets_present) &&
  2967. esp->seqreg != 0 &&
  2968. (esp->cur_msgout[0] == EXTENDED_MESSAGE) &&
  2969. (SCptr->SCp.phase == in_slct_msg ||
  2970. SCptr->SCp.phase == in_slct_stop)) {
  2971. /* shit */
  2972. esp->snip = 0;
  2973. ESPLOG(("esp%d: Failed synchronous negotiation for target %d "
  2974. "lun %d\n", esp->esp_id, SCptr->device->id, SCptr->device->lun));
  2975. esp_dev->sync_max_offset = 0;
  2976. esp_dev->sync_min_period = 0;
  2977. esp_dev->sync = 1; /* so we don't negotiate again */
  2978. /* Run the command again, this time though we
  2979. * won't try to negotiate for synchronous transfers.
  2980. *
  2981. * XXX I'd like to do something like send an
  2982. * XXX INITIATOR_ERROR or ABORT message to the
  2983. * XXX target to tell it, "Sorry I confused you,
  2984. * XXX please come back and I will be nicer next
  2985. * XXX time". But that requires having the target
  2986. * XXX on the bus, and it has dropped BSY on us.
  2987. */
  2988. esp->current_SC = NULL;
  2989. esp_advance_phase(SCptr, not_issued);
  2990. prepend_SC(&esp->issue_SC, SCptr);
  2991. esp_exec_cmd(esp);
  2992. return do_intr_end;
  2993. }
  2994. /* Ok, this is normal, this is what we see during boot
  2995. * or whenever when we are scanning the bus for targets.
  2996. * But first make sure that is really what is happening.
  2997. */
  2998. if (((1<<SCptr->device->id) & esp->targets_present)) {
  2999. ESPLOG(("esp%d: Warning, live target %d not responding to "
  3000. "selection.\n", esp->esp_id, SCptr->device->id));
  3001. /* This _CAN_ happen. The SCSI standard states that
  3002. * the target is to _not_ respond to selection if
  3003. * _it_ detects bad parity on the bus for any reason.
  3004. * Therefore, we assume that if we've talked successfully
  3005. * to this target before, bad parity is the problem.
  3006. */
  3007. esp_done(esp, (DID_PARITY << 16));
  3008. } else {
  3009. /* Else, there really isn't anyone there. */
  3010. ESPMISC(("esp: selection failure, maybe nobody there?\n"));
  3011. ESPMISC(("esp: target %d lun %d\n",
  3012. SCptr->device->id, SCptr->device->lun));
  3013. esp_done(esp, (DID_BAD_TARGET << 16));
  3014. }
  3015. return do_intr_end;
  3016. }
  3017. ESPLOG(("esp%d: Selection failure.\n", esp->esp_id));
  3018. printk("esp%d: Currently -- ", esp->esp_id);
  3019. esp_print_ireg(esp->ireg); printk(" ");
  3020. esp_print_statreg(esp->sreg); printk(" ");
  3021. esp_print_seqreg(esp->seqreg); printk("\n");
  3022. printk("esp%d: New -- ", esp->esp_id);
  3023. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3024. esp->seqreg = sbus_readb(esp->eregs + ESP_SSTEP);
  3025. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT);
  3026. esp_print_ireg(esp->ireg); printk(" ");
  3027. esp_print_statreg(esp->sreg); printk(" ");
  3028. esp_print_seqreg(esp->seqreg); printk("\n");
  3029. ESPLOG(("esp%d: resetting bus\n", esp->esp_id));
  3030. return do_reset_bus; /* ugh... */
  3031. }
  3032. /* Continue reading bytes for msgin phase. */
  3033. static int esp_do_msgincont(struct esp *esp)
  3034. {
  3035. if (esp->ireg & ESP_INTR_BSERV) {
  3036. /* in the right phase too? */
  3037. if ((esp->sreg & ESP_STAT_PMASK) == ESP_MIP) {
  3038. /* phew... */
  3039. esp_cmd(esp, ESP_CMD_TI);
  3040. esp_advance_phase(esp->current_SC, in_msgindone);
  3041. return do_intr_end;
  3042. }
  3043. /* We changed phase but ESP shows bus service,
  3044. * in this case it is most likely that we, the
  3045. * hacker who has been up for 20hrs straight
  3046. * staring at the screen, drowned in coffee
  3047. * smelling like retched cigarette ashes
  3048. * have miscoded something..... so, try to
  3049. * recover as best we can.
  3050. */
  3051. ESPLOG(("esp%d: message in mis-carriage.\n", esp->esp_id));
  3052. }
  3053. esp_advance_phase(esp->current_SC, in_the_dark);
  3054. return do_phase_determine;
  3055. }
  3056. static int check_singlebyte_msg(struct esp *esp)
  3057. {
  3058. esp->prevmsgin = esp->cur_msgin[0];
  3059. if (esp->cur_msgin[0] & 0x80) {
  3060. /* wheee... */
  3061. ESPLOG(("esp%d: target sends identify amidst phases\n",
  3062. esp->esp_id));
  3063. esp_advance_phase(esp->current_SC, in_the_dark);
  3064. return 0;
  3065. } else if (((esp->cur_msgin[0] & 0xf0) == 0x20) ||
  3066. (esp->cur_msgin[0] == EXTENDED_MESSAGE)) {
  3067. esp->msgin_len = 2;
  3068. esp_advance_phase(esp->current_SC, in_msgincont);
  3069. return 0;
  3070. }
  3071. esp_advance_phase(esp->current_SC, in_the_dark);
  3072. switch (esp->cur_msgin[0]) {
  3073. default:
  3074. /* We don't want to hear about it. */
  3075. ESPLOG(("esp%d: msg %02x which we don't know about\n", esp->esp_id,
  3076. esp->cur_msgin[0]));
  3077. return MESSAGE_REJECT;
  3078. case NOP:
  3079. ESPLOG(("esp%d: target %d sends a nop\n", esp->esp_id,
  3080. esp->current_SC->device->id));
  3081. return 0;
  3082. case RESTORE_POINTERS:
  3083. /* In this case we might also have to backup the
  3084. * "slow command" pointer. It is rare to get such
  3085. * a save/restore pointer sequence so early in the
  3086. * bus transition sequences, but cover it.
  3087. */
  3088. if (esp->esp_slowcmd) {
  3089. esp->esp_scmdleft = esp->current_SC->cmd_len;
  3090. esp->esp_scmdp = &esp->current_SC->cmnd[0];
  3091. }
  3092. esp_restore_pointers(esp, esp->current_SC);
  3093. return 0;
  3094. case SAVE_POINTERS:
  3095. esp_save_pointers(esp, esp->current_SC);
  3096. return 0;
  3097. case COMMAND_COMPLETE:
  3098. case DISCONNECT:
  3099. /* Freeing the bus, let it go. */
  3100. esp->current_SC->SCp.phase = in_freeing;
  3101. return 0;
  3102. case MESSAGE_REJECT:
  3103. ESPMISC(("msg reject, "));
  3104. if (esp->prevmsgout == EXTENDED_MESSAGE) {
  3105. struct esp_device *esp_dev = esp->current_SC->device->hostdata;
  3106. /* Doesn't look like this target can
  3107. * do synchronous or WIDE transfers.
  3108. */
  3109. ESPSDTR(("got reject, was trying nego, clearing sync/WIDE\n"));
  3110. esp_dev->sync = 1;
  3111. esp_dev->wide = 1;
  3112. esp_dev->sync_min_period = 0;
  3113. esp_dev->sync_max_offset = 0;
  3114. return 0;
  3115. } else {
  3116. ESPMISC(("not sync nego, sending ABORT\n"));
  3117. return ABORT;
  3118. }
  3119. };
  3120. }
  3121. /* Target negotiates for synchronous transfers before we do, this
  3122. * is legal although very strange. What is even funnier is that
  3123. * the SCSI2 standard specifically recommends against targets doing
  3124. * this because so many initiators cannot cope with this occurring.
  3125. */
  3126. static int target_with_ants_in_pants(struct esp *esp,
  3127. struct scsi_cmnd *SCptr,
  3128. struct esp_device *esp_dev)
  3129. {
  3130. if (esp_dev->sync || SCptr->device->borken) {
  3131. /* sorry, no can do */
  3132. ESPSDTR(("forcing to async, "));
  3133. build_sync_nego_msg(esp, 0, 0);
  3134. esp_dev->sync = 1;
  3135. esp->snip = 1;
  3136. ESPLOG(("esp%d: hoping for msgout\n", esp->esp_id));
  3137. esp_advance_phase(SCptr, in_the_dark);
  3138. return EXTENDED_MESSAGE;
  3139. }
  3140. /* Ok, we'll check them out... */
  3141. return 0;
  3142. }
  3143. static void sync_report(struct esp *esp)
  3144. {
  3145. int msg3, msg4;
  3146. char *type;
  3147. msg3 = esp->cur_msgin[3];
  3148. msg4 = esp->cur_msgin[4];
  3149. if (msg4) {
  3150. int hz = 1000000000 / (msg3 * 4);
  3151. int integer = hz / 1000000;
  3152. int fraction = (hz - (integer * 1000000)) / 10000;
  3153. if ((esp->erev == fashme) &&
  3154. (esp->config3[esp->current_SC->device->id] & ESP_CONFIG3_EWIDE)) {
  3155. type = "FAST-WIDE";
  3156. integer <<= 1;
  3157. fraction <<= 1;
  3158. } else if ((msg3 * 4) < 200) {
  3159. type = "FAST";
  3160. } else {
  3161. type = "synchronous";
  3162. }
  3163. /* Do not transform this back into one big printk
  3164. * again, it triggers a bug in our sparc64-gcc272
  3165. * sibling call optimization. -DaveM
  3166. */
  3167. ESPLOG((KERN_INFO "esp%d: target %d ",
  3168. esp->esp_id, esp->current_SC->device->id));
  3169. ESPLOG(("[period %dns offset %d %d.%02dMHz ",
  3170. (int) msg3 * 4, (int) msg4,
  3171. integer, fraction));
  3172. ESPLOG(("%s SCSI%s]\n", type,
  3173. (((msg3 * 4) < 200) ? "-II" : "")));
  3174. } else {
  3175. ESPLOG((KERN_INFO "esp%d: target %d asynchronous\n",
  3176. esp->esp_id, esp->current_SC->device->id));
  3177. }
  3178. }
  3179. static int check_multibyte_msg(struct esp *esp)
  3180. {
  3181. struct scsi_cmnd *SCptr = esp->current_SC;
  3182. struct esp_device *esp_dev = SCptr->device->hostdata;
  3183. u8 regval = 0;
  3184. int message_out = 0;
  3185. ESPSDTR(("chk multibyte msg: "));
  3186. if (esp->cur_msgin[2] == EXTENDED_SDTR) {
  3187. int period = esp->cur_msgin[3];
  3188. int offset = esp->cur_msgin[4];
  3189. ESPSDTR(("is sync nego response, "));
  3190. if (!esp->snip) {
  3191. int rval;
  3192. /* Target negotiates first! */
  3193. ESPSDTR(("target jumps the gun, "));
  3194. message_out = EXTENDED_MESSAGE; /* we must respond */
  3195. rval = target_with_ants_in_pants(esp, SCptr, esp_dev);
  3196. if (rval)
  3197. return rval;
  3198. }
  3199. ESPSDTR(("examining sdtr, "));
  3200. /* Offset cannot be larger than ESP fifo size. */
  3201. if (offset > 15) {
  3202. ESPSDTR(("offset too big %2x, ", offset));
  3203. offset = 15;
  3204. ESPSDTR(("sending back new offset\n"));
  3205. build_sync_nego_msg(esp, period, offset);
  3206. return EXTENDED_MESSAGE;
  3207. }
  3208. if (offset && period > esp->max_period) {
  3209. /* Yeee, async for this slow device. */
  3210. ESPSDTR(("period too long %2x, ", period));
  3211. build_sync_nego_msg(esp, 0, 0);
  3212. ESPSDTR(("hoping for msgout\n"));
  3213. esp_advance_phase(esp->current_SC, in_the_dark);
  3214. return EXTENDED_MESSAGE;
  3215. } else if (offset && period < esp->min_period) {
  3216. ESPSDTR(("period too short %2x, ", period));
  3217. period = esp->min_period;
  3218. if (esp->erev > esp236)
  3219. regval = 4;
  3220. else
  3221. regval = 5;
  3222. } else if (offset) {
  3223. int tmp;
  3224. ESPSDTR(("period is ok, "));
  3225. tmp = esp->ccycle / 1000;
  3226. regval = (((period << 2) + tmp - 1) / tmp);
  3227. if (regval && ((esp->erev == fas100a ||
  3228. esp->erev == fas236 ||
  3229. esp->erev == fashme))) {
  3230. if (period >= 50)
  3231. regval--;
  3232. }
  3233. }
  3234. if (offset) {
  3235. u8 bit;
  3236. esp_dev->sync_min_period = (regval & 0x1f);
  3237. esp_dev->sync_max_offset = (offset | esp->radelay);
  3238. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3239. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3240. bit = ESP_CONFIG3_FAST;
  3241. else
  3242. bit = ESP_CONFIG3_FSCSI;
  3243. if (period < 50) {
  3244. /* On FAS366, if using fast-20 synchronous transfers
  3245. * we need to make sure the REQ/ACK assert/deassert
  3246. * control bits are clear.
  3247. */
  3248. if (esp->erev == fashme)
  3249. esp_dev->sync_max_offset &= ~esp->radelay;
  3250. esp->config3[SCptr->device->id] |= bit;
  3251. } else {
  3252. esp->config3[SCptr->device->id] &= ~bit;
  3253. }
  3254. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3255. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3256. }
  3257. esp->prev_soff = esp_dev->sync_max_offset;
  3258. esp->prev_stp = esp_dev->sync_min_period;
  3259. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3260. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3261. ESPSDTR(("soff=%2x stp=%2x cfg3=%2x\n",
  3262. esp_dev->sync_max_offset,
  3263. esp_dev->sync_min_period,
  3264. esp->config3[SCptr->device->id]));
  3265. esp->snip = 0;
  3266. } else if (esp_dev->sync_max_offset) {
  3267. u8 bit;
  3268. /* back to async mode */
  3269. ESPSDTR(("unaccaptable sync nego, forcing async\n"));
  3270. esp_dev->sync_max_offset = 0;
  3271. esp_dev->sync_min_period = 0;
  3272. esp->prev_soff = 0;
  3273. esp->prev_stp = 0;
  3274. sbus_writeb(esp->prev_soff, esp->eregs + ESP_SOFF);
  3275. sbus_writeb(esp->prev_stp, esp->eregs + ESP_STP);
  3276. if (esp->erev == fas100a || esp->erev == fas236 || esp->erev == fashme) {
  3277. if ((esp->erev == fas100a) || (esp->erev == fashme))
  3278. bit = ESP_CONFIG3_FAST;
  3279. else
  3280. bit = ESP_CONFIG3_FSCSI;
  3281. esp->config3[SCptr->device->id] &= ~bit;
  3282. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3283. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3284. }
  3285. }
  3286. sync_report(esp);
  3287. ESPSDTR(("chk multibyte msg: sync is known, "));
  3288. esp_dev->sync = 1;
  3289. if (message_out) {
  3290. ESPLOG(("esp%d: sending sdtr back, hoping for msgout\n",
  3291. esp->esp_id));
  3292. build_sync_nego_msg(esp, period, offset);
  3293. esp_advance_phase(SCptr, in_the_dark);
  3294. return EXTENDED_MESSAGE;
  3295. }
  3296. ESPSDTR(("returning zero\n"));
  3297. esp_advance_phase(SCptr, in_the_dark); /* ...or else! */
  3298. return 0;
  3299. } else if (esp->cur_msgin[2] == EXTENDED_WDTR) {
  3300. int size = 8 << esp->cur_msgin[3];
  3301. esp->wnip = 0;
  3302. if (esp->erev != fashme) {
  3303. ESPLOG(("esp%d: AIEEE wide msg received and not HME.\n",
  3304. esp->esp_id));
  3305. message_out = MESSAGE_REJECT;
  3306. } else if (size > 16) {
  3307. ESPLOG(("esp%d: AIEEE wide transfer for %d size "
  3308. "not supported.\n", esp->esp_id, size));
  3309. message_out = MESSAGE_REJECT;
  3310. } else {
  3311. /* Things look good; let's see what we got. */
  3312. if (size == 16) {
  3313. /* Set config 3 register for this target. */
  3314. esp->config3[SCptr->device->id] |= ESP_CONFIG3_EWIDE;
  3315. } else {
  3316. /* Just make sure it was one byte sized. */
  3317. if (size != 8) {
  3318. ESPLOG(("esp%d: Aieee, wide nego of %d size.\n",
  3319. esp->esp_id, size));
  3320. message_out = MESSAGE_REJECT;
  3321. goto finish;
  3322. }
  3323. /* Pure paranoia. */
  3324. esp->config3[SCptr->device->id] &= ~(ESP_CONFIG3_EWIDE);
  3325. }
  3326. esp->prev_cfg3 = esp->config3[SCptr->device->id];
  3327. sbus_writeb(esp->prev_cfg3, esp->eregs + ESP_CFG3);
  3328. /* Regardless, next try for sync transfers. */
  3329. build_sync_nego_msg(esp, esp->sync_defp, 15);
  3330. esp_dev->sync = 1;
  3331. esp->snip = 1;
  3332. message_out = EXTENDED_MESSAGE;
  3333. }
  3334. } else if (esp->cur_msgin[2] == EXTENDED_MODIFY_DATA_POINTER) {
  3335. ESPLOG(("esp%d: rejecting modify data ptr msg\n", esp->esp_id));
  3336. message_out = MESSAGE_REJECT;
  3337. }
  3338. finish:
  3339. esp_advance_phase(SCptr, in_the_dark);
  3340. return message_out;
  3341. }
  3342. static int esp_do_msgindone(struct esp *esp)
  3343. {
  3344. struct scsi_cmnd *SCptr = esp->current_SC;
  3345. int message_out = 0, it = 0, rval;
  3346. rval = skipahead1(esp, SCptr, in_msgin, in_msgindone);
  3347. if (rval)
  3348. return rval;
  3349. if (SCptr->SCp.sent_command != in_status) {
  3350. if (!(esp->ireg & ESP_INTR_DC)) {
  3351. if (esp->msgin_len && (esp->sreg & ESP_STAT_PERR)) {
  3352. message_out = MSG_PARITY_ERROR;
  3353. esp_cmd(esp, ESP_CMD_FLUSH);
  3354. } else if (esp->erev != fashme &&
  3355. (it = (sbus_readb(esp->eregs + ESP_FFLAGS) & ESP_FF_FBYTES)) != 1) {
  3356. /* We certainly dropped the ball somewhere. */
  3357. message_out = INITIATOR_ERROR;
  3358. esp_cmd(esp, ESP_CMD_FLUSH);
  3359. } else if (!esp->msgin_len) {
  3360. if (esp->erev == fashme)
  3361. it = esp->hme_fifo_workaround_buffer[0];
  3362. else
  3363. it = sbus_readb(esp->eregs + ESP_FDATA);
  3364. esp_advance_phase(SCptr, in_msgincont);
  3365. } else {
  3366. /* it is ok and we want it */
  3367. if (esp->erev == fashme)
  3368. it = esp->cur_msgin[esp->msgin_ctr] =
  3369. esp->hme_fifo_workaround_buffer[0];
  3370. else
  3371. it = esp->cur_msgin[esp->msgin_ctr] =
  3372. sbus_readb(esp->eregs + ESP_FDATA);
  3373. esp->msgin_ctr++;
  3374. }
  3375. } else {
  3376. esp_advance_phase(SCptr, in_the_dark);
  3377. return do_work_bus;
  3378. }
  3379. } else {
  3380. it = esp->cur_msgin[0];
  3381. }
  3382. if (!message_out && esp->msgin_len) {
  3383. if (esp->msgin_ctr < esp->msgin_len) {
  3384. esp_advance_phase(SCptr, in_msgincont);
  3385. } else if (esp->msgin_len == 1) {
  3386. message_out = check_singlebyte_msg(esp);
  3387. } else if (esp->msgin_len == 2) {
  3388. if (esp->cur_msgin[0] == EXTENDED_MESSAGE) {
  3389. if ((it + 2) >= 15) {
  3390. message_out = MESSAGE_REJECT;
  3391. } else {
  3392. esp->msgin_len = (it + 2);
  3393. esp_advance_phase(SCptr, in_msgincont);
  3394. }
  3395. } else {
  3396. message_out = MESSAGE_REJECT; /* foo on you */
  3397. }
  3398. } else {
  3399. message_out = check_multibyte_msg(esp);
  3400. }
  3401. }
  3402. if (message_out < 0) {
  3403. return -message_out;
  3404. } else if (message_out) {
  3405. if (((message_out != 1) &&
  3406. ((message_out < 0x20) || (message_out & 0x80))))
  3407. esp->msgout_len = 1;
  3408. esp->cur_msgout[0] = message_out;
  3409. esp_cmd(esp, ESP_CMD_SATN);
  3410. esp_advance_phase(SCptr, in_the_dark);
  3411. esp->msgin_len = 0;
  3412. }
  3413. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3414. esp->sreg &= ~(ESP_STAT_INTR);
  3415. if ((esp->sreg & (ESP_STAT_PMSG|ESP_STAT_PCD)) == (ESP_STAT_PMSG|ESP_STAT_PCD))
  3416. esp_cmd(esp, ESP_CMD_MOK);
  3417. if ((SCptr->SCp.sent_command == in_msgindone) &&
  3418. (SCptr->SCp.phase == in_freeing))
  3419. return esp_do_freebus(esp);
  3420. return do_intr_end;
  3421. }
  3422. static int esp_do_cmdbegin(struct esp *esp)
  3423. {
  3424. struct scsi_cmnd *SCptr = esp->current_SC;
  3425. esp_advance_phase(SCptr, in_cmdend);
  3426. if (esp->erev == fashme) {
  3427. u32 tmp = sbus_readl(esp->dregs + DMA_CSR);
  3428. int i;
  3429. for (i = 0; i < esp->esp_scmdleft; i++)
  3430. esp->esp_command[i] = *esp->esp_scmdp++;
  3431. esp->esp_scmdleft = 0;
  3432. esp_cmd(esp, ESP_CMD_FLUSH);
  3433. esp_setcount(esp->eregs, i, 1);
  3434. esp_cmd(esp, (ESP_CMD_DMA | ESP_CMD_TI));
  3435. tmp |= (DMA_SCSI_DISAB | DMA_ENABLE);
  3436. tmp &= ~(DMA_ST_WRITE);
  3437. sbus_writel(i, esp->dregs + DMA_COUNT);
  3438. sbus_writel(esp->esp_command_dvma, esp->dregs + DMA_ADDR);
  3439. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3440. } else {
  3441. u8 tmp;
  3442. esp_cmd(esp, ESP_CMD_FLUSH);
  3443. tmp = *esp->esp_scmdp++;
  3444. esp->esp_scmdleft--;
  3445. sbus_writeb(tmp, esp->eregs + ESP_FDATA);
  3446. esp_cmd(esp, ESP_CMD_TI);
  3447. }
  3448. return do_intr_end;
  3449. }
  3450. static int esp_do_cmddone(struct esp *esp)
  3451. {
  3452. if (esp->erev == fashme)
  3453. dma_invalidate(esp);
  3454. else
  3455. esp_cmd(esp, ESP_CMD_NULL);
  3456. if (esp->ireg & ESP_INTR_BSERV) {
  3457. esp_advance_phase(esp->current_SC, in_the_dark);
  3458. return esp_do_phase_determine(esp);
  3459. }
  3460. ESPLOG(("esp%d: in do_cmddone() but didn't get BSERV interrupt.\n",
  3461. esp->esp_id));
  3462. return do_reset_bus;
  3463. }
  3464. static int esp_do_msgout(struct esp *esp)
  3465. {
  3466. esp_cmd(esp, ESP_CMD_FLUSH);
  3467. switch (esp->msgout_len) {
  3468. case 1:
  3469. if (esp->erev == fashme)
  3470. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3471. else
  3472. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3473. esp_cmd(esp, ESP_CMD_TI);
  3474. break;
  3475. case 2:
  3476. esp->esp_command[0] = esp->cur_msgout[0];
  3477. esp->esp_command[1] = esp->cur_msgout[1];
  3478. if (esp->erev == fashme) {
  3479. hme_fifo_push(esp, &esp->cur_msgout[0], 2);
  3480. esp_cmd(esp, ESP_CMD_TI);
  3481. } else {
  3482. dma_setup(esp, esp->esp_command_dvma, 2, 0);
  3483. esp_setcount(esp->eregs, 2, 0);
  3484. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3485. }
  3486. break;
  3487. case 4:
  3488. esp->esp_command[0] = esp->cur_msgout[0];
  3489. esp->esp_command[1] = esp->cur_msgout[1];
  3490. esp->esp_command[2] = esp->cur_msgout[2];
  3491. esp->esp_command[3] = esp->cur_msgout[3];
  3492. esp->snip = 1;
  3493. if (esp->erev == fashme) {
  3494. hme_fifo_push(esp, &esp->cur_msgout[0], 4);
  3495. esp_cmd(esp, ESP_CMD_TI);
  3496. } else {
  3497. dma_setup(esp, esp->esp_command_dvma, 4, 0);
  3498. esp_setcount(esp->eregs, 4, 0);
  3499. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3500. }
  3501. break;
  3502. case 5:
  3503. esp->esp_command[0] = esp->cur_msgout[0];
  3504. esp->esp_command[1] = esp->cur_msgout[1];
  3505. esp->esp_command[2] = esp->cur_msgout[2];
  3506. esp->esp_command[3] = esp->cur_msgout[3];
  3507. esp->esp_command[4] = esp->cur_msgout[4];
  3508. esp->snip = 1;
  3509. if (esp->erev == fashme) {
  3510. hme_fifo_push(esp, &esp->cur_msgout[0], 5);
  3511. esp_cmd(esp, ESP_CMD_TI);
  3512. } else {
  3513. dma_setup(esp, esp->esp_command_dvma, 5, 0);
  3514. esp_setcount(esp->eregs, 5, 0);
  3515. esp_cmd(esp, ESP_CMD_DMA | ESP_CMD_TI);
  3516. }
  3517. break;
  3518. default:
  3519. /* whoops */
  3520. ESPMISC(("bogus msgout sending NOP\n"));
  3521. esp->cur_msgout[0] = NOP;
  3522. if (esp->erev == fashme) {
  3523. hme_fifo_push(esp, &esp->cur_msgout[0], 1);
  3524. } else {
  3525. sbus_writeb(esp->cur_msgout[0], esp->eregs + ESP_FDATA);
  3526. }
  3527. esp->msgout_len = 1;
  3528. esp_cmd(esp, ESP_CMD_TI);
  3529. break;
  3530. };
  3531. esp_advance_phase(esp->current_SC, in_msgoutdone);
  3532. return do_intr_end;
  3533. }
  3534. static int esp_do_msgoutdone(struct esp *esp)
  3535. {
  3536. if (esp->msgout_len > 1) {
  3537. /* XXX HME/FAS ATN deassert workaround required,
  3538. * XXX no DMA flushing, only possible ESP_CMD_FLUSH
  3539. * XXX to kill the fifo.
  3540. */
  3541. if (esp->erev != fashme) {
  3542. u32 tmp;
  3543. while ((tmp = sbus_readl(esp->dregs + DMA_CSR)) & DMA_PEND_READ)
  3544. udelay(1);
  3545. tmp &= ~DMA_ENABLE;
  3546. sbus_writel(tmp, esp->dregs + DMA_CSR);
  3547. dma_invalidate(esp);
  3548. } else {
  3549. esp_cmd(esp, ESP_CMD_FLUSH);
  3550. }
  3551. }
  3552. if (!(esp->ireg & ESP_INTR_DC)) {
  3553. if (esp->erev != fashme)
  3554. esp_cmd(esp, ESP_CMD_NULL);
  3555. switch (esp->sreg & ESP_STAT_PMASK) {
  3556. case ESP_MOP:
  3557. /* whoops, parity error */
  3558. ESPLOG(("esp%d: still in msgout, parity error assumed\n",
  3559. esp->esp_id));
  3560. if (esp->msgout_len > 1)
  3561. esp_cmd(esp, ESP_CMD_SATN);
  3562. esp_advance_phase(esp->current_SC, in_msgout);
  3563. return do_work_bus;
  3564. case ESP_DIP:
  3565. break;
  3566. default:
  3567. /* Happy Meal fifo is touchy... */
  3568. if ((esp->erev != fashme) &&
  3569. !fcount(esp) &&
  3570. !(((struct esp_device *)esp->current_SC->device->hostdata)->sync_max_offset))
  3571. esp_cmd(esp, ESP_CMD_FLUSH);
  3572. break;
  3573. };
  3574. } else {
  3575. ESPLOG(("esp%d: disconnect, resetting bus\n", esp->esp_id));
  3576. return do_reset_bus;
  3577. }
  3578. /* If we sent out a synchronous negotiation message, update
  3579. * our state.
  3580. */
  3581. if (esp->cur_msgout[2] == EXTENDED_MESSAGE &&
  3582. esp->cur_msgout[4] == EXTENDED_SDTR) {
  3583. esp->snip = 1; /* anal retentiveness... */
  3584. }
  3585. esp->prevmsgout = esp->cur_msgout[0];
  3586. esp->msgout_len = 0;
  3587. esp_advance_phase(esp->current_SC, in_the_dark);
  3588. return esp_do_phase_determine(esp);
  3589. }
  3590. static int esp_bus_unexpected(struct esp *esp)
  3591. {
  3592. ESPLOG(("esp%d: command in weird state %2x\n",
  3593. esp->esp_id, esp->current_SC->SCp.phase));
  3594. return do_reset_bus;
  3595. }
  3596. static espfunc_t bus_vector[] = {
  3597. esp_do_data_finale,
  3598. esp_do_data_finale,
  3599. esp_bus_unexpected,
  3600. esp_do_msgin,
  3601. esp_do_msgincont,
  3602. esp_do_msgindone,
  3603. esp_do_msgout,
  3604. esp_do_msgoutdone,
  3605. esp_do_cmdbegin,
  3606. esp_do_cmddone,
  3607. esp_do_status,
  3608. esp_do_freebus,
  3609. esp_do_phase_determine,
  3610. esp_bus_unexpected,
  3611. esp_bus_unexpected,
  3612. esp_bus_unexpected,
  3613. };
  3614. /* This is the second tier in our dual-level SCSI state machine. */
  3615. static int esp_work_bus(struct esp *esp)
  3616. {
  3617. struct scsi_cmnd *SCptr = esp->current_SC;
  3618. unsigned int phase;
  3619. ESPBUS(("esp_work_bus: "));
  3620. if (!SCptr) {
  3621. ESPBUS(("reconnect\n"));
  3622. return esp_do_reconnect(esp);
  3623. }
  3624. phase = SCptr->SCp.phase;
  3625. if ((phase & 0xf0) == in_phases_mask)
  3626. return bus_vector[(phase & 0x0f)](esp);
  3627. else if ((phase & 0xf0) == in_slct_mask)
  3628. return esp_select_complete(esp);
  3629. else
  3630. return esp_bus_unexpected(esp);
  3631. }
  3632. static espfunc_t isvc_vector[] = {
  3633. NULL,
  3634. esp_do_phase_determine,
  3635. esp_do_resetbus,
  3636. esp_finish_reset,
  3637. esp_work_bus
  3638. };
  3639. /* Main interrupt handler for an esp adapter. */
  3640. static void esp_handle(struct esp *esp)
  3641. {
  3642. struct scsi_cmnd *SCptr;
  3643. int what_next = do_intr_end;
  3644. SCptr = esp->current_SC;
  3645. /* Check for errors. */
  3646. esp->sreg = sbus_readb(esp->eregs + ESP_STATUS);
  3647. esp->sreg &= (~ESP_STAT_INTR);
  3648. if (esp->erev == fashme) {
  3649. esp->sreg2 = sbus_readb(esp->eregs + ESP_STATUS2);
  3650. esp->seqreg = (sbus_readb(esp->eregs + ESP_SSTEP) & ESP_STEP_VBITS);
  3651. }
  3652. if (esp->sreg & (ESP_STAT_SPAM)) {
  3653. /* Gross error, could be due to one of:
  3654. *
  3655. * - top of fifo overwritten, could be because
  3656. * we tried to do a synchronous transfer with
  3657. * an offset greater than ESP fifo size
  3658. *
  3659. * - top of command register overwritten
  3660. *
  3661. * - DMA setup to go in one direction, SCSI
  3662. * bus points in the other, whoops
  3663. *
  3664. * - weird phase change during asynchronous
  3665. * data phase while we are initiator
  3666. */
  3667. ESPLOG(("esp%d: Gross error sreg=%2x\n", esp->esp_id, esp->sreg));
  3668. /* If a command is live on the bus we cannot safely
  3669. * reset the bus, so we'll just let the pieces fall
  3670. * where they may. Here we are hoping that the
  3671. * target will be able to cleanly go away soon
  3672. * so we can safely reset things.
  3673. */
  3674. if (!SCptr) {
  3675. ESPLOG(("esp%d: No current cmd during gross error, "
  3676. "resetting bus\n", esp->esp_id));
  3677. what_next = do_reset_bus;
  3678. goto state_machine;
  3679. }
  3680. }
  3681. if (sbus_readl(esp->dregs + DMA_CSR) & DMA_HNDL_ERROR) {
  3682. /* A DMA gate array error. Here we must
  3683. * be seeing one of two things. Either the
  3684. * virtual to physical address translation
  3685. * on the SBUS could not occur, else the
  3686. * translation it did get pointed to a bogus
  3687. * page. Ho hum...
  3688. */
  3689. ESPLOG(("esp%d: DMA error %08x\n", esp->esp_id,
  3690. sbus_readl(esp->dregs + DMA_CSR)));
  3691. /* DMA gate array itself must be reset to clear the
  3692. * error condition.
  3693. */
  3694. esp_reset_dma(esp);
  3695. what_next = do_reset_bus;
  3696. goto state_machine;
  3697. }
  3698. esp->ireg = sbus_readb(esp->eregs + ESP_INTRPT); /* Unlatch intr reg */
  3699. if (esp->erev == fashme) {
  3700. /* This chip is really losing. */
  3701. ESPHME(("HME["));
  3702. ESPHME(("sreg2=%02x,", esp->sreg2));
  3703. /* Must latch fifo before reading the interrupt
  3704. * register else garbage ends up in the FIFO
  3705. * which confuses the driver utterly.
  3706. */
  3707. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  3708. (esp->sreg2 & ESP_STAT2_F1BYTE)) {
  3709. ESPHME(("fifo_workaround]"));
  3710. hme_fifo_read(esp);
  3711. } else {
  3712. ESPHME(("no_fifo_workaround]"));
  3713. }
  3714. }
  3715. /* No current cmd is only valid at this point when there are
  3716. * commands off the bus or we are trying a reset.
  3717. */
  3718. if (!SCptr && !esp->disconnected_SC && !(esp->ireg & ESP_INTR_SR)) {
  3719. /* Panic is safe, since current_SC is null. */
  3720. ESPLOG(("esp%d: no command in esp_handle()\n", esp->esp_id));
  3721. panic("esp_handle: current_SC == penguin within interrupt!");
  3722. }
  3723. if (esp->ireg & (ESP_INTR_IC)) {
  3724. /* Illegal command fed to ESP. Outside of obvious
  3725. * software bugs that could cause this, there is
  3726. * a condition with esp100 where we can confuse the
  3727. * ESP into an erroneous illegal command interrupt
  3728. * because it does not scrape the FIFO properly
  3729. * for reselection. See esp100_reconnect_hwbug()
  3730. * to see how we try very hard to avoid this.
  3731. */
  3732. ESPLOG(("esp%d: invalid command\n", esp->esp_id));
  3733. esp_dump_state(esp);
  3734. if (SCptr != NULL) {
  3735. /* Devices with very buggy firmware can drop BSY
  3736. * during a scatter list interrupt when using sync
  3737. * mode transfers. We continue the transfer as
  3738. * expected, the target drops the bus, the ESP
  3739. * gets confused, and we get a illegal command
  3740. * interrupt because the bus is in the disconnected
  3741. * state now and ESP_CMD_TI is only allowed when
  3742. * a nexus is alive on the bus.
  3743. */
  3744. ESPLOG(("esp%d: Forcing async and disabling disconnect for "
  3745. "target %d\n", esp->esp_id, SCptr->device->id));
  3746. SCptr->device->borken = 1; /* foo on you */
  3747. }
  3748. what_next = do_reset_bus;
  3749. } else if (!(esp->ireg & ~(ESP_INTR_FDONE | ESP_INTR_BSERV | ESP_INTR_DC))) {
  3750. if (SCptr) {
  3751. unsigned int phase = SCptr->SCp.phase;
  3752. if (phase & in_phases_mask) {
  3753. what_next = esp_work_bus(esp);
  3754. } else if (phase & in_slct_mask) {
  3755. what_next = esp_select_complete(esp);
  3756. } else {
  3757. ESPLOG(("esp%d: interrupt for no good reason...\n",
  3758. esp->esp_id));
  3759. what_next = do_intr_end;
  3760. }
  3761. } else {
  3762. ESPLOG(("esp%d: BSERV or FDONE or DC while SCptr==NULL\n",
  3763. esp->esp_id));
  3764. what_next = do_reset_bus;
  3765. }
  3766. } else if (esp->ireg & ESP_INTR_SR) {
  3767. ESPLOG(("esp%d: SCSI bus reset interrupt\n", esp->esp_id));
  3768. what_next = do_reset_complete;
  3769. } else if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN)) {
  3770. ESPLOG(("esp%d: AIEEE we have been selected by another initiator!\n",
  3771. esp->esp_id));
  3772. what_next = do_reset_bus;
  3773. } else if (esp->ireg & ESP_INTR_RSEL) {
  3774. if (SCptr == NULL) {
  3775. /* This is ok. */
  3776. what_next = esp_do_reconnect(esp);
  3777. } else if (SCptr->SCp.phase & in_slct_mask) {
  3778. /* Only selection code knows how to clean
  3779. * up properly.
  3780. */
  3781. ESPDISC(("Reselected during selection attempt\n"));
  3782. what_next = esp_select_complete(esp);
  3783. } else {
  3784. ESPLOG(("esp%d: Reselected while bus is busy\n",
  3785. esp->esp_id));
  3786. what_next = do_reset_bus;
  3787. }
  3788. }
  3789. /* This is tier-one in our dual level SCSI state machine. */
  3790. state_machine:
  3791. while (what_next != do_intr_end) {
  3792. if (what_next >= do_phase_determine &&
  3793. what_next < do_intr_end) {
  3794. what_next = isvc_vector[what_next](esp);
  3795. } else {
  3796. /* state is completely lost ;-( */
  3797. ESPLOG(("esp%d: interrupt engine loses state, resetting bus\n",
  3798. esp->esp_id));
  3799. what_next = do_reset_bus;
  3800. }
  3801. }
  3802. }
  3803. /* Service only the ESP described by dev_id. */
  3804. static irqreturn_t esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
  3805. {
  3806. struct esp *esp = dev_id;
  3807. unsigned long flags;
  3808. spin_lock_irqsave(esp->ehost->host_lock, flags);
  3809. if (ESP_IRQ_P(esp->dregs)) {
  3810. ESP_INTSOFF(esp->dregs);
  3811. ESPIRQ(("I[%d:%d](", smp_processor_id(), esp->esp_id));
  3812. esp_handle(esp);
  3813. ESPIRQ((")"));
  3814. ESP_INTSON(esp->dregs);
  3815. }
  3816. spin_unlock_irqrestore(esp->ehost->host_lock, flags);
  3817. return IRQ_HANDLED;
  3818. }
  3819. static int esp_slave_alloc(struct scsi_device *SDptr)
  3820. {
  3821. struct esp_device *esp_dev =
  3822. kmalloc(sizeof(struct esp_device), GFP_ATOMIC);
  3823. if (!esp_dev)
  3824. return -ENOMEM;
  3825. memset(esp_dev, 0, sizeof(struct esp_device));
  3826. SDptr->hostdata = esp_dev;
  3827. return 0;
  3828. }
  3829. static void esp_slave_destroy(struct scsi_device *SDptr)
  3830. {
  3831. struct esp *esp = (struct esp *) SDptr->host->hostdata;
  3832. esp->targets_present &= ~(1 << SDptr->id);
  3833. kfree(SDptr->hostdata);
  3834. SDptr->hostdata = NULL;
  3835. }
  3836. static struct scsi_host_template esp_template = {
  3837. .module = THIS_MODULE,
  3838. .name = "esp",
  3839. .info = esp_info,
  3840. .slave_alloc = esp_slave_alloc,
  3841. .slave_destroy = esp_slave_destroy,
  3842. .queuecommand = esp_queue,
  3843. .eh_abort_handler = esp_abort,
  3844. .eh_bus_reset_handler = esp_reset,
  3845. .can_queue = 7,
  3846. .this_id = 7,
  3847. .sg_tablesize = SG_ALL,
  3848. .cmd_per_lun = 1,
  3849. .use_clustering = ENABLE_CLUSTERING,
  3850. .proc_name = "esp",
  3851. .proc_info = esp_proc_info,
  3852. };
  3853. #ifndef CONFIG_SUN4
  3854. static struct of_device_id esp_match[] = {
  3855. {
  3856. .name = "SUNW,esp",
  3857. .data = &esp_template,
  3858. },
  3859. {
  3860. .name = "SUNW,fas",
  3861. .data = &esp_template,
  3862. },
  3863. {
  3864. .name = "esp",
  3865. .data = &esp_template,
  3866. },
  3867. {},
  3868. };
  3869. MODULE_DEVICE_TABLE(of, esp_match);
  3870. static struct of_platform_driver esp_sbus_driver = {
  3871. .name = "esp",
  3872. .match_table = esp_match,
  3873. .probe = esp_sbus_probe,
  3874. .remove = __devexit_p(esp_sbus_remove),
  3875. };
  3876. #endif
  3877. static int __init esp_init(void)
  3878. {
  3879. #ifdef CONFIG_SUN4
  3880. return esp_sun4_probe(&esp_template);
  3881. #else
  3882. return of_register_driver(&esp_sbus_driver, &sbus_bus_type);
  3883. #endif
  3884. }
  3885. static void __exit esp_exit(void)
  3886. {
  3887. #ifdef CONFIG_SUN4
  3888. esp_sun4_remove();
  3889. #else
  3890. of_unregister_driver(&esp_sbus_driver);
  3891. #endif
  3892. }
  3893. MODULE_DESCRIPTION("ESP Sun SCSI driver");
  3894. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  3895. MODULE_LICENSE("GPL");
  3896. MODULE_VERSION(DRV_VERSION);
  3897. module_init(esp_init);
  3898. module_exit(esp_exit);