sym_hipd.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748
  1. /*
  2. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  3. * of PCI-SCSI IO processors.
  4. *
  5. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  6. * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
  7. *
  8. * This driver is derived from the Linux sym53c8xx driver.
  9. * Copyright (C) 1998-2000 Gerard Roudier
  10. *
  11. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  12. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  13. *
  14. * The original ncr driver has been written for 386bsd and FreeBSD by
  15. * Wolfgang Stanglmeier <wolf@cologne.de>
  16. * Stefan Esser <se@mi.Uni-Koeln.de>
  17. * Copyright (C) 1994 Wolfgang Stanglmeier
  18. *
  19. * Other major contributions:
  20. *
  21. * NVRAM detection and reading.
  22. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  23. *
  24. *-----------------------------------------------------------------------------
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include <linux/slab.h>
  41. #include <asm/param.h> /* for timeouts in units of HZ */
  42. #include "sym_glue.h"
  43. #include "sym_nvram.h"
  44. #if 0
  45. #define SYM_DEBUG_GENERIC_SUPPORT
  46. #endif
  47. /*
  48. * Needed function prototypes.
  49. */
  50. static void sym_int_ma (struct sym_hcb *np);
  51. static void sym_int_sir (struct sym_hcb *np);
  52. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
  53. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
  54. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
  55. static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
  56. static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
  57. static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
  58. /*
  59. * Print a buffer in hexadecimal format with a ".\n" at end.
  60. */
  61. static void sym_printl_hex(u_char *p, int n)
  62. {
  63. while (n-- > 0)
  64. printf (" %x", *p++);
  65. printf (".\n");
  66. }
  67. static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
  68. {
  69. if (label)
  70. sym_print_addr(cp->cmd, "%s: ", label);
  71. else
  72. sym_print_addr(cp->cmd, "");
  73. spi_print_msg(msg);
  74. printf("\n");
  75. }
  76. static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
  77. {
  78. struct sym_tcb *tp = &np->target[target];
  79. dev_info(&tp->starget->dev, "%s: ", label);
  80. spi_print_msg(msg);
  81. printf("\n");
  82. }
  83. /*
  84. * Print something that tells about extended errors.
  85. */
  86. void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
  87. {
  88. if (x_status & XE_PARITY_ERR) {
  89. sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
  90. }
  91. if (x_status & XE_EXTRA_DATA) {
  92. sym_print_addr(cmd, "extraneous data discarded.\n");
  93. }
  94. if (x_status & XE_BAD_PHASE) {
  95. sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
  96. }
  97. if (x_status & XE_SODL_UNRUN) {
  98. sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
  99. }
  100. if (x_status & XE_SWIDE_OVRUN) {
  101. sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
  102. }
  103. }
  104. /*
  105. * Return a string for SCSI BUS mode.
  106. */
  107. static char *sym_scsi_bus_mode(int mode)
  108. {
  109. switch(mode) {
  110. case SMODE_HVD: return "HVD";
  111. case SMODE_SE: return "SE";
  112. case SMODE_LVD: return "LVD";
  113. }
  114. return "??";
  115. }
  116. /*
  117. * Soft reset the chip.
  118. *
  119. * Raising SRST when the chip is running may cause
  120. * problems on dual function chips (see below).
  121. * On the other hand, LVD devices need some delay
  122. * to settle and report actual BUS mode in STEST4.
  123. */
  124. static void sym_chip_reset (struct sym_hcb *np)
  125. {
  126. OUTB(np, nc_istat, SRST);
  127. INB(np, nc_mbox1);
  128. udelay(10);
  129. OUTB(np, nc_istat, 0);
  130. INB(np, nc_mbox1);
  131. udelay(2000); /* For BUS MODE to settle */
  132. }
  133. /*
  134. * Really soft reset the chip.:)
  135. *
  136. * Some 896 and 876 chip revisions may hang-up if we set
  137. * the SRST (soft reset) bit at the wrong time when SCRIPTS
  138. * are running.
  139. * So, we need to abort the current operation prior to
  140. * soft resetting the chip.
  141. */
  142. static void sym_soft_reset (struct sym_hcb *np)
  143. {
  144. u_char istat = 0;
  145. int i;
  146. if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
  147. goto do_chip_reset;
  148. OUTB(np, nc_istat, CABRT);
  149. for (i = 100000 ; i ; --i) {
  150. istat = INB(np, nc_istat);
  151. if (istat & SIP) {
  152. INW(np, nc_sist);
  153. }
  154. else if (istat & DIP) {
  155. if (INB(np, nc_dstat) & ABRT)
  156. break;
  157. }
  158. udelay(5);
  159. }
  160. OUTB(np, nc_istat, 0);
  161. if (!i)
  162. printf("%s: unable to abort current chip operation, "
  163. "ISTAT=0x%02x.\n", sym_name(np), istat);
  164. do_chip_reset:
  165. sym_chip_reset(np);
  166. }
  167. /*
  168. * Start reset process.
  169. *
  170. * The interrupt handler will reinitialize the chip.
  171. */
  172. static void sym_start_reset(struct sym_hcb *np)
  173. {
  174. sym_reset_scsi_bus(np, 1);
  175. }
  176. int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
  177. {
  178. u32 term;
  179. int retv = 0;
  180. sym_soft_reset(np); /* Soft reset the chip */
  181. if (enab_int)
  182. OUTW(np, nc_sien, RST);
  183. /*
  184. * Enable Tolerant, reset IRQD if present and
  185. * properly set IRQ mode, prior to resetting the bus.
  186. */
  187. OUTB(np, nc_stest3, TE);
  188. OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
  189. OUTB(np, nc_scntl1, CRST);
  190. INB(np, nc_mbox1);
  191. udelay(200);
  192. if (!SYM_SETUP_SCSI_BUS_CHECK)
  193. goto out;
  194. /*
  195. * Check for no terminators or SCSI bus shorts to ground.
  196. * Read SCSI data bus, data parity bits and control signals.
  197. * We are expecting RESET to be TRUE and other signals to be
  198. * FALSE.
  199. */
  200. term = INB(np, nc_sstat0);
  201. term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
  202. term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
  203. ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
  204. ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
  205. INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
  206. if (!np->maxwide)
  207. term &= 0x3ffff;
  208. if (term != (2<<7)) {
  209. printf("%s: suspicious SCSI data while resetting the BUS.\n",
  210. sym_name(np));
  211. printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
  212. "0x%lx, expecting 0x%lx\n",
  213. sym_name(np),
  214. (np->features & FE_WIDE) ? "dp1,d15-8," : "",
  215. (u_long)term, (u_long)(2<<7));
  216. if (SYM_SETUP_SCSI_BUS_CHECK == 1)
  217. retv = 1;
  218. }
  219. out:
  220. OUTB(np, nc_scntl1, 0);
  221. return retv;
  222. }
  223. /*
  224. * Select SCSI clock frequency
  225. */
  226. static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
  227. {
  228. /*
  229. * If multiplier not present or not selected, leave here.
  230. */
  231. if (np->multiplier <= 1) {
  232. OUTB(np, nc_scntl3, scntl3);
  233. return;
  234. }
  235. if (sym_verbose >= 2)
  236. printf ("%s: enabling clock multiplier\n", sym_name(np));
  237. OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
  238. /*
  239. * Wait for the LCKFRQ bit to be set if supported by the chip.
  240. * Otherwise wait 50 micro-seconds (at least).
  241. */
  242. if (np->features & FE_LCKFRQ) {
  243. int i = 20;
  244. while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
  245. udelay(20);
  246. if (!i)
  247. printf("%s: the chip cannot lock the frequency\n",
  248. sym_name(np));
  249. } else {
  250. INB(np, nc_mbox1);
  251. udelay(50+10);
  252. }
  253. OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
  254. OUTB(np, nc_scntl3, scntl3);
  255. OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
  256. OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
  257. }
  258. /*
  259. * Determine the chip's clock frequency.
  260. *
  261. * This is essential for the negotiation of the synchronous
  262. * transfer rate.
  263. *
  264. * Note: we have to return the correct value.
  265. * THERE IS NO SAFE DEFAULT VALUE.
  266. *
  267. * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
  268. * 53C860 and 53C875 rev. 1 support fast20 transfers but
  269. * do not have a clock doubler and so are provided with a
  270. * 80 MHz clock. All other fast20 boards incorporate a doubler
  271. * and so should be delivered with a 40 MHz clock.
  272. * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
  273. * clock and provide a clock quadrupler (160 Mhz).
  274. */
  275. /*
  276. * calculate SCSI clock frequency (in KHz)
  277. */
  278. static unsigned getfreq (struct sym_hcb *np, int gen)
  279. {
  280. unsigned int ms = 0;
  281. unsigned int f;
  282. /*
  283. * Measure GEN timer delay in order
  284. * to calculate SCSI clock frequency
  285. *
  286. * This code will never execute too
  287. * many loop iterations (if DELAY is
  288. * reasonably correct). It could get
  289. * too low a delay (too high a freq.)
  290. * if the CPU is slow executing the
  291. * loop for some reason (an NMI, for
  292. * example). For this reason we will
  293. * if multiple measurements are to be
  294. * performed trust the higher delay
  295. * (lower frequency returned).
  296. */
  297. OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
  298. INW(np, nc_sist); /* clear pending scsi interrupt */
  299. OUTB(np, nc_dien, 0); /* mask all dma interrupts */
  300. INW(np, nc_sist); /* another one, just to be sure :) */
  301. /*
  302. * The C1010-33 core does not report GEN in SIST,
  303. * if this interrupt is masked in SIEN.
  304. * I don't know yet if the C1010-66 behaves the same way.
  305. */
  306. if (np->features & FE_C10) {
  307. OUTW(np, nc_sien, GEN);
  308. OUTB(np, nc_istat1, SIRQD);
  309. }
  310. OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
  311. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  312. OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
  313. while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
  314. udelay(1000/4); /* count in 1/4 of ms */
  315. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  316. /*
  317. * Undo C1010-33 specific settings.
  318. */
  319. if (np->features & FE_C10) {
  320. OUTW(np, nc_sien, 0);
  321. OUTB(np, nc_istat1, 0);
  322. }
  323. /*
  324. * set prescaler to divide by whatever 0 means
  325. * 0 ought to choose divide by 2, but appears
  326. * to set divide by 3.5 mode in my 53c810 ...
  327. */
  328. OUTB(np, nc_scntl3, 0);
  329. /*
  330. * adjust for prescaler, and convert into KHz
  331. */
  332. f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
  333. /*
  334. * The C1010-33 result is biased by a factor
  335. * of 2/3 compared to earlier chips.
  336. */
  337. if (np->features & FE_C10)
  338. f = (f * 2) / 3;
  339. if (sym_verbose >= 2)
  340. printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
  341. sym_name(np), gen, ms/4, f);
  342. return f;
  343. }
  344. static unsigned sym_getfreq (struct sym_hcb *np)
  345. {
  346. u_int f1, f2;
  347. int gen = 8;
  348. getfreq (np, gen); /* throw away first result */
  349. f1 = getfreq (np, gen);
  350. f2 = getfreq (np, gen);
  351. if (f1 > f2) f1 = f2; /* trust lower result */
  352. return f1;
  353. }
  354. /*
  355. * Get/probe chip SCSI clock frequency
  356. */
  357. static void sym_getclock (struct sym_hcb *np, int mult)
  358. {
  359. unsigned char scntl3 = np->sv_scntl3;
  360. unsigned char stest1 = np->sv_stest1;
  361. unsigned f1;
  362. np->multiplier = 1;
  363. f1 = 40000;
  364. /*
  365. * True with 875/895/896/895A with clock multiplier selected
  366. */
  367. if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
  368. if (sym_verbose >= 2)
  369. printf ("%s: clock multiplier found\n", sym_name(np));
  370. np->multiplier = mult;
  371. }
  372. /*
  373. * If multiplier not found or scntl3 not 7,5,3,
  374. * reset chip and get frequency from general purpose timer.
  375. * Otherwise trust scntl3 BIOS setting.
  376. */
  377. if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
  378. OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
  379. f1 = sym_getfreq (np);
  380. if (sym_verbose)
  381. printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
  382. if (f1 < 45000) f1 = 40000;
  383. else if (f1 < 55000) f1 = 50000;
  384. else f1 = 80000;
  385. if (f1 < 80000 && mult > 1) {
  386. if (sym_verbose >= 2)
  387. printf ("%s: clock multiplier assumed\n",
  388. sym_name(np));
  389. np->multiplier = mult;
  390. }
  391. } else {
  392. if ((scntl3 & 7) == 3) f1 = 40000;
  393. else if ((scntl3 & 7) == 5) f1 = 80000;
  394. else f1 = 160000;
  395. f1 /= np->multiplier;
  396. }
  397. /*
  398. * Compute controller synchronous parameters.
  399. */
  400. f1 *= np->multiplier;
  401. np->clock_khz = f1;
  402. }
  403. /*
  404. * Get/probe PCI clock frequency
  405. */
  406. static int sym_getpciclock (struct sym_hcb *np)
  407. {
  408. int f = 0;
  409. /*
  410. * For now, we only need to know about the actual
  411. * PCI BUS clock frequency for C1010-66 chips.
  412. */
  413. #if 1
  414. if (np->features & FE_66MHZ) {
  415. #else
  416. if (1) {
  417. #endif
  418. OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
  419. f = sym_getfreq(np);
  420. OUTB(np, nc_stest1, 0);
  421. }
  422. np->pciclk_khz = f;
  423. return f;
  424. }
  425. /*
  426. * SYMBIOS chip clock divisor table.
  427. *
  428. * Divisors are multiplied by 10,000,000 in order to make
  429. * calculations more simple.
  430. */
  431. #define _5M 5000000
  432. static const u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
  433. /*
  434. * Get clock factor and sync divisor for a given
  435. * synchronous factor period.
  436. */
  437. static int
  438. sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
  439. {
  440. u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
  441. int div = np->clock_divn; /* Number of divisors supported */
  442. u32 fak; /* Sync factor in sxfer */
  443. u32 per; /* Period in tenths of ns */
  444. u32 kpc; /* (per * clk) */
  445. int ret;
  446. /*
  447. * Compute the synchronous period in tenths of nano-seconds
  448. */
  449. if (dt && sfac <= 9) per = 125;
  450. else if (sfac <= 10) per = 250;
  451. else if (sfac == 11) per = 303;
  452. else if (sfac == 12) per = 500;
  453. else per = 40 * sfac;
  454. ret = per;
  455. kpc = per * clk;
  456. if (dt)
  457. kpc <<= 1;
  458. /*
  459. * For earliest C10 revision 0, we cannot use extra
  460. * clocks for the setting of the SCSI clocking.
  461. * Note that this limits the lowest sync data transfer
  462. * to 5 Mega-transfers per second and may result in
  463. * using higher clock divisors.
  464. */
  465. #if 1
  466. if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
  467. /*
  468. * Look for the lowest clock divisor that allows an
  469. * output speed not faster than the period.
  470. */
  471. while (div > 0) {
  472. --div;
  473. if (kpc > (div_10M[div] << 2)) {
  474. ++div;
  475. break;
  476. }
  477. }
  478. fak = 0; /* No extra clocks */
  479. if (div == np->clock_divn) { /* Are we too fast ? */
  480. ret = -1;
  481. }
  482. *divp = div;
  483. *fakp = fak;
  484. return ret;
  485. }
  486. #endif
  487. /*
  488. * Look for the greatest clock divisor that allows an
  489. * input speed faster than the period.
  490. */
  491. while (div-- > 0)
  492. if (kpc >= (div_10M[div] << 2)) break;
  493. /*
  494. * Calculate the lowest clock factor that allows an output
  495. * speed not faster than the period, and the max output speed.
  496. * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
  497. * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
  498. */
  499. if (dt) {
  500. fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
  501. /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
  502. } else {
  503. fak = (kpc - 1) / div_10M[div] + 1 - 4;
  504. /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
  505. }
  506. /*
  507. * Check against our hardware limits, or bugs :).
  508. */
  509. if (fak > 2) {
  510. fak = 2;
  511. ret = -1;
  512. }
  513. /*
  514. * Compute and return sync parameters.
  515. */
  516. *divp = div;
  517. *fakp = fak;
  518. return ret;
  519. }
  520. /*
  521. * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
  522. * 128 transfers. All chips support at least 16 transfers
  523. * bursts. The 825A, 875 and 895 chips support bursts of up
  524. * to 128 transfers and the 895A and 896 support bursts of up
  525. * to 64 transfers. All other chips support up to 16
  526. * transfers bursts.
  527. *
  528. * For PCI 32 bit data transfers each transfer is a DWORD.
  529. * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
  530. *
  531. * We use log base 2 (burst length) as internal code, with
  532. * value 0 meaning "burst disabled".
  533. */
  534. /*
  535. * Burst length from burst code.
  536. */
  537. #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
  538. /*
  539. * Burst code from io register bits.
  540. */
  541. #define burst_code(dmode, ctest4, ctest5) \
  542. (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
  543. /*
  544. * Set initial io register bits from burst code.
  545. */
  546. static __inline void sym_init_burst(struct sym_hcb *np, u_char bc)
  547. {
  548. np->rv_ctest4 &= ~0x80;
  549. np->rv_dmode &= ~(0x3 << 6);
  550. np->rv_ctest5 &= ~0x4;
  551. if (!bc) {
  552. np->rv_ctest4 |= 0x80;
  553. }
  554. else {
  555. --bc;
  556. np->rv_dmode |= ((bc & 0x3) << 6);
  557. np->rv_ctest5 |= (bc & 0x4);
  558. }
  559. }
  560. /*
  561. * Save initial settings of some IO registers.
  562. * Assumed to have been set by BIOS.
  563. * We cannot reset the chip prior to reading the
  564. * IO registers, since informations will be lost.
  565. * Since the SCRIPTS processor may be running, this
  566. * is not safe on paper, but it seems to work quite
  567. * well. :)
  568. */
  569. static void sym_save_initial_setting (struct sym_hcb *np)
  570. {
  571. np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
  572. np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
  573. np->sv_dmode = INB(np, nc_dmode) & 0xce;
  574. np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
  575. np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
  576. np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
  577. np->sv_gpcntl = INB(np, nc_gpcntl);
  578. np->sv_stest1 = INB(np, nc_stest1);
  579. np->sv_stest2 = INB(np, nc_stest2) & 0x20;
  580. np->sv_stest4 = INB(np, nc_stest4);
  581. if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
  582. np->sv_scntl4 = INB(np, nc_scntl4);
  583. np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
  584. }
  585. else
  586. np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
  587. }
  588. /*
  589. * Set SCSI BUS mode.
  590. * - LVD capable chips (895/895A/896/1010) report the current BUS mode
  591. * through the STEST4 IO register.
  592. * - For previous generation chips (825/825A/875), the user has to tell us
  593. * how to check against HVD, since a 100% safe algorithm is not possible.
  594. */
  595. static void sym_set_bus_mode(struct sym_hcb *np, struct sym_nvram *nvram)
  596. {
  597. if (np->scsi_mode)
  598. return;
  599. np->scsi_mode = SMODE_SE;
  600. if (np->features & (FE_ULTRA2|FE_ULTRA3))
  601. np->scsi_mode = (np->sv_stest4 & SMODE);
  602. else if (np->features & FE_DIFF) {
  603. if (SYM_SETUP_SCSI_DIFF == 1) {
  604. if (np->sv_scntl3) {
  605. if (np->sv_stest2 & 0x20)
  606. np->scsi_mode = SMODE_HVD;
  607. } else if (nvram->type == SYM_SYMBIOS_NVRAM) {
  608. if (!(INB(np, nc_gpreg) & 0x08))
  609. np->scsi_mode = SMODE_HVD;
  610. }
  611. } else if (SYM_SETUP_SCSI_DIFF == 2)
  612. np->scsi_mode = SMODE_HVD;
  613. }
  614. if (np->scsi_mode == SMODE_HVD)
  615. np->rv_stest2 |= 0x20;
  616. }
  617. /*
  618. * Prepare io register values used by sym_start_up()
  619. * according to selected and supported features.
  620. */
  621. static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
  622. {
  623. u_char burst_max;
  624. u32 period;
  625. int i;
  626. np->maxwide = (np->features & FE_WIDE) ? 1 : 0;
  627. /*
  628. * Guess the frequency of the chip's clock.
  629. */
  630. if (np->features & (FE_ULTRA3 | FE_ULTRA2))
  631. np->clock_khz = 160000;
  632. else if (np->features & FE_ULTRA)
  633. np->clock_khz = 80000;
  634. else
  635. np->clock_khz = 40000;
  636. /*
  637. * Get the clock multiplier factor.
  638. */
  639. if (np->features & FE_QUAD)
  640. np->multiplier = 4;
  641. else if (np->features & FE_DBLR)
  642. np->multiplier = 2;
  643. else
  644. np->multiplier = 1;
  645. /*
  646. * Measure SCSI clock frequency for chips
  647. * it may vary from assumed one.
  648. */
  649. if (np->features & FE_VARCLK)
  650. sym_getclock(np, np->multiplier);
  651. /*
  652. * Divisor to be used for async (timer pre-scaler).
  653. */
  654. i = np->clock_divn - 1;
  655. while (--i >= 0) {
  656. if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
  657. ++i;
  658. break;
  659. }
  660. }
  661. np->rv_scntl3 = i+1;
  662. /*
  663. * The C1010 uses hardwired divisors for async.
  664. * So, we just throw away, the async. divisor.:-)
  665. */
  666. if (np->features & FE_C10)
  667. np->rv_scntl3 = 0;
  668. /*
  669. * Minimum synchronous period factor supported by the chip.
  670. * Btw, 'period' is in tenths of nanoseconds.
  671. */
  672. period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
  673. if (period <= 250) np->minsync = 10;
  674. else if (period <= 303) np->minsync = 11;
  675. else if (period <= 500) np->minsync = 12;
  676. else np->minsync = (period + 40 - 1) / 40;
  677. /*
  678. * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
  679. */
  680. if (np->minsync < 25 &&
  681. !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
  682. np->minsync = 25;
  683. else if (np->minsync < 12 &&
  684. !(np->features & (FE_ULTRA2|FE_ULTRA3)))
  685. np->minsync = 12;
  686. /*
  687. * Maximum synchronous period factor supported by the chip.
  688. */
  689. period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
  690. np->maxsync = period > 2540 ? 254 : period / 10;
  691. /*
  692. * If chip is a C1010, guess the sync limits in DT mode.
  693. */
  694. if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
  695. if (np->clock_khz == 160000) {
  696. np->minsync_dt = 9;
  697. np->maxsync_dt = 50;
  698. np->maxoffs_dt = nvram->type ? 62 : 31;
  699. }
  700. }
  701. /*
  702. * 64 bit addressing (895A/896/1010) ?
  703. */
  704. if (np->features & FE_DAC) {
  705. #if SYM_CONF_DMA_ADDRESSING_MODE == 0
  706. np->rv_ccntl1 |= (DDAC);
  707. #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
  708. if (!np->use_dac)
  709. np->rv_ccntl1 |= (DDAC);
  710. else
  711. np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
  712. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  713. if (!np->use_dac)
  714. np->rv_ccntl1 |= (DDAC);
  715. else
  716. np->rv_ccntl1 |= (0 | EXTIBMV);
  717. #endif
  718. }
  719. /*
  720. * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
  721. */
  722. if (np->features & FE_NOPM)
  723. np->rv_ccntl0 |= (ENPMJ);
  724. /*
  725. * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
  726. * In dual channel mode, contention occurs if internal cycles
  727. * are used. Disable internal cycles.
  728. */
  729. if (np->s.device->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
  730. np->s.device->revision < 0x1)
  731. np->rv_ccntl0 |= DILS;
  732. /*
  733. * Select burst length (dwords)
  734. */
  735. burst_max = SYM_SETUP_BURST_ORDER;
  736. if (burst_max == 255)
  737. burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
  738. np->sv_ctest5);
  739. if (burst_max > 7)
  740. burst_max = 7;
  741. if (burst_max > np->maxburst)
  742. burst_max = np->maxburst;
  743. /*
  744. * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
  745. * This chip and the 860 Rev 1 may wrongly use PCI cache line
  746. * based transactions on LOAD/STORE instructions. So we have
  747. * to prevent these chips from using such PCI transactions in
  748. * this driver. The generic ncr driver that does not use
  749. * LOAD/STORE instructions does not need this work-around.
  750. */
  751. if ((np->s.device->device == PCI_DEVICE_ID_NCR_53C810 &&
  752. np->s.device->revision >= 0x10 && np->s.device->revision <= 0x11) ||
  753. (np->s.device->device == PCI_DEVICE_ID_NCR_53C860 &&
  754. np->s.device->revision <= 0x1))
  755. np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
  756. /*
  757. * Select all supported special features.
  758. * If we are using on-board RAM for scripts, prefetch (PFEN)
  759. * does not help, but burst op fetch (BOF) does.
  760. * Disabling PFEN makes sure BOF will be used.
  761. */
  762. if (np->features & FE_ERL)
  763. np->rv_dmode |= ERL; /* Enable Read Line */
  764. if (np->features & FE_BOF)
  765. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  766. if (np->features & FE_ERMP)
  767. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  768. #if 1
  769. if ((np->features & FE_PFEN) && !np->ram_ba)
  770. #else
  771. if (np->features & FE_PFEN)
  772. #endif
  773. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  774. if (np->features & FE_CLSE)
  775. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  776. if (np->features & FE_WRIE)
  777. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  778. if (np->features & FE_DFS)
  779. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  780. /*
  781. * Select some other
  782. */
  783. np->rv_ctest4 |= MPEE; /* Master parity checking */
  784. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  785. /*
  786. * Get parity checking, host ID and verbose mode from NVRAM
  787. */
  788. np->myaddr = 255;
  789. np->scsi_mode = 0;
  790. sym_nvram_setup_host(shost, np, nvram);
  791. /*
  792. * Get SCSI addr of host adapter (set by bios?).
  793. */
  794. if (np->myaddr == 255) {
  795. np->myaddr = INB(np, nc_scid) & 0x07;
  796. if (!np->myaddr)
  797. np->myaddr = SYM_SETUP_HOST_ID;
  798. }
  799. /*
  800. * Prepare initial io register bits for burst length
  801. */
  802. sym_init_burst(np, burst_max);
  803. sym_set_bus_mode(np, nvram);
  804. /*
  805. * Set LED support from SCRIPTS.
  806. * Ignore this feature for boards known to use a
  807. * specific GPIO wiring and for the 895A, 896
  808. * and 1010 that drive the LED directly.
  809. */
  810. if ((SYM_SETUP_SCSI_LED ||
  811. (nvram->type == SYM_SYMBIOS_NVRAM ||
  812. (nvram->type == SYM_TEKRAM_NVRAM &&
  813. np->s.device->device == PCI_DEVICE_ID_NCR_53C895))) &&
  814. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  815. np->features |= FE_LED0;
  816. /*
  817. * Set irq mode.
  818. */
  819. switch(SYM_SETUP_IRQ_MODE & 3) {
  820. case 2:
  821. np->rv_dcntl |= IRQM;
  822. break;
  823. case 1:
  824. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  825. break;
  826. default:
  827. break;
  828. }
  829. /*
  830. * Configure targets according to driver setup.
  831. * If NVRAM present get targets setup from NVRAM.
  832. */
  833. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  834. struct sym_tcb *tp = &np->target[i];
  835. tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  836. tp->usrtags = SYM_SETUP_MAX_TAG;
  837. tp->usr_width = np->maxwide;
  838. tp->usr_period = 9;
  839. sym_nvram_setup_target(tp, i, nvram);
  840. if (!tp->usrtags)
  841. tp->usrflags &= ~SYM_TAGS_ENABLED;
  842. }
  843. /*
  844. * Let user know about the settings.
  845. */
  846. printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
  847. sym_nvram_type(nvram), np->myaddr,
  848. (np->features & FE_ULTRA3) ? 80 :
  849. (np->features & FE_ULTRA2) ? 40 :
  850. (np->features & FE_ULTRA) ? 20 : 10,
  851. sym_scsi_bus_mode(np->scsi_mode),
  852. (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
  853. /*
  854. * Tell him more on demand.
  855. */
  856. if (sym_verbose) {
  857. printf("%s: %s IRQ line driver%s\n",
  858. sym_name(np),
  859. np->rv_dcntl & IRQM ? "totem pole" : "open drain",
  860. np->ram_ba ? ", using on-chip SRAM" : "");
  861. printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
  862. if (np->features & FE_NOPM)
  863. printf("%s: handling phase mismatch from SCRIPTS.\n",
  864. sym_name(np));
  865. }
  866. /*
  867. * And still more.
  868. */
  869. if (sym_verbose >= 2) {
  870. printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  871. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  872. sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  873. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  874. printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  875. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  876. sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  877. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  878. }
  879. return 0;
  880. }
  881. /*
  882. * Test the pci bus snoop logic :-(
  883. *
  884. * Has to be called with interrupts disabled.
  885. */
  886. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  887. static int sym_regtest(struct sym_hcb *np)
  888. {
  889. register volatile u32 data;
  890. /*
  891. * chip registers may NOT be cached.
  892. * write 0xffffffff to a read only register area,
  893. * and try to read it back.
  894. */
  895. data = 0xffffffff;
  896. OUTL(np, nc_dstat, data);
  897. data = INL(np, nc_dstat);
  898. #if 1
  899. if (data == 0xffffffff) {
  900. #else
  901. if ((data & 0xe2f0fffd) != 0x02000080) {
  902. #endif
  903. printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  904. (unsigned) data);
  905. return 0x10;
  906. }
  907. return 0;
  908. }
  909. #else
  910. static inline int sym_regtest(struct sym_hcb *np)
  911. {
  912. return 0;
  913. }
  914. #endif
  915. static int sym_snooptest(struct sym_hcb *np)
  916. {
  917. u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
  918. int i, err;
  919. err = sym_regtest(np);
  920. if (err)
  921. return err;
  922. restart_test:
  923. /*
  924. * Enable Master Parity Checking as we intend
  925. * to enable it for normal operations.
  926. */
  927. OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
  928. /*
  929. * init
  930. */
  931. pc = SCRIPTZ_BA(np, snooptest);
  932. host_wr = 1;
  933. sym_wr = 2;
  934. /*
  935. * Set memory and register.
  936. */
  937. np->scratch = cpu_to_scr(host_wr);
  938. OUTL(np, nc_temp, sym_wr);
  939. /*
  940. * Start script (exchange values)
  941. */
  942. OUTL(np, nc_dsa, np->hcb_ba);
  943. OUTL_DSP(np, pc);
  944. /*
  945. * Wait 'til done (with timeout)
  946. */
  947. for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
  948. if (INB(np, nc_istat) & (INTF|SIP|DIP))
  949. break;
  950. if (i>=SYM_SNOOP_TIMEOUT) {
  951. printf ("CACHE TEST FAILED: timeout.\n");
  952. return (0x20);
  953. }
  954. /*
  955. * Check for fatal DMA errors.
  956. */
  957. dstat = INB(np, nc_dstat);
  958. #if 1 /* Band aiding for broken hardwares that fail PCI parity */
  959. if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
  960. printf ("%s: PCI DATA PARITY ERROR DETECTED - "
  961. "DISABLING MASTER DATA PARITY CHECKING.\n",
  962. sym_name(np));
  963. np->rv_ctest4 &= ~MPEE;
  964. goto restart_test;
  965. }
  966. #endif
  967. if (dstat & (MDPE|BF|IID)) {
  968. printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
  969. return (0x80);
  970. }
  971. /*
  972. * Save termination position.
  973. */
  974. pc = INL(np, nc_dsp);
  975. /*
  976. * Read memory and register.
  977. */
  978. host_rd = scr_to_cpu(np->scratch);
  979. sym_rd = INL(np, nc_scratcha);
  980. sym_bk = INL(np, nc_temp);
  981. /*
  982. * Check termination position.
  983. */
  984. if (pc != SCRIPTZ_BA(np, snoopend)+8) {
  985. printf ("CACHE TEST FAILED: script execution failed.\n");
  986. printf ("start=%08lx, pc=%08lx, end=%08lx\n",
  987. (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
  988. (u_long) SCRIPTZ_BA(np, snoopend) +8);
  989. return (0x40);
  990. }
  991. /*
  992. * Show results.
  993. */
  994. if (host_wr != sym_rd) {
  995. printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
  996. (int) host_wr, (int) sym_rd);
  997. err |= 1;
  998. }
  999. if (host_rd != sym_wr) {
  1000. printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
  1001. (int) sym_wr, (int) host_rd);
  1002. err |= 2;
  1003. }
  1004. if (sym_bk != sym_wr) {
  1005. printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
  1006. (int) sym_wr, (int) sym_bk);
  1007. err |= 4;
  1008. }
  1009. return err;
  1010. }
  1011. /*
  1012. * log message for real hard errors
  1013. *
  1014. * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
  1015. * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
  1016. *
  1017. * exception register:
  1018. * ds: dstat
  1019. * si: sist
  1020. *
  1021. * SCSI bus lines:
  1022. * so: control lines as driven by chip.
  1023. * si: control lines as seen by chip.
  1024. * sd: scsi data lines as seen by chip.
  1025. *
  1026. * wide/fastmode:
  1027. * sx: sxfer (see the manual)
  1028. * s3: scntl3 (see the manual)
  1029. * s4: scntl4 (see the manual)
  1030. *
  1031. * current script command:
  1032. * dsp: script address (relative to start of script).
  1033. * dbc: first word of script command.
  1034. *
  1035. * First 24 register of the chip:
  1036. * r0..rf
  1037. */
  1038. static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
  1039. {
  1040. u32 dsp;
  1041. int script_ofs;
  1042. int script_size;
  1043. char *script_name;
  1044. u_char *script_base;
  1045. int i;
  1046. dsp = INL(np, nc_dsp);
  1047. if (dsp > np->scripta_ba &&
  1048. dsp <= np->scripta_ba + np->scripta_sz) {
  1049. script_ofs = dsp - np->scripta_ba;
  1050. script_size = np->scripta_sz;
  1051. script_base = (u_char *) np->scripta0;
  1052. script_name = "scripta";
  1053. }
  1054. else if (np->scriptb_ba < dsp &&
  1055. dsp <= np->scriptb_ba + np->scriptb_sz) {
  1056. script_ofs = dsp - np->scriptb_ba;
  1057. script_size = np->scriptb_sz;
  1058. script_base = (u_char *) np->scriptb0;
  1059. script_name = "scriptb";
  1060. } else {
  1061. script_ofs = dsp;
  1062. script_size = 0;
  1063. script_base = NULL;
  1064. script_name = "mem";
  1065. }
  1066. printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
  1067. sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
  1068. (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
  1069. (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
  1070. (unsigned)INB(np, nc_scntl3),
  1071. (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
  1072. script_name, script_ofs, (unsigned)INL(np, nc_dbc));
  1073. if (((script_ofs & 3) == 0) &&
  1074. (unsigned)script_ofs < script_size) {
  1075. printf ("%s: script cmd = %08x\n", sym_name(np),
  1076. scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
  1077. }
  1078. printf("%s: regdump:", sym_name(np));
  1079. for (i = 0; i < 24; i++)
  1080. printf(" %02x", (unsigned)INB_OFF(np, i));
  1081. printf(".\n");
  1082. /*
  1083. * PCI BUS error.
  1084. */
  1085. if (dstat & (MDPE|BF))
  1086. sym_log_bus_error(np);
  1087. }
  1088. void sym_dump_registers(struct sym_hcb *np)
  1089. {
  1090. u_short sist;
  1091. u_char dstat;
  1092. sist = INW(np, nc_sist);
  1093. dstat = INB(np, nc_dstat);
  1094. sym_log_hard_error(np, sist, dstat);
  1095. }
  1096. static struct sym_chip sym_dev_table[] = {
  1097. {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
  1098. FE_ERL}
  1099. ,
  1100. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1101. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1102. FE_BOF}
  1103. ,
  1104. #else
  1105. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1106. FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
  1107. ,
  1108. #endif
  1109. {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
  1110. FE_BOF|FE_ERL}
  1111. ,
  1112. {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
  1113. FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
  1114. ,
  1115. {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
  1116. FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
  1117. ,
  1118. {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
  1119. FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
  1120. ,
  1121. {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
  1122. FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1123. FE_RAM|FE_DIFF|FE_VARCLK}
  1124. ,
  1125. {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
  1126. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1127. FE_RAM|FE_DIFF|FE_VARCLK}
  1128. ,
  1129. {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
  1130. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1131. FE_RAM|FE_DIFF|FE_VARCLK}
  1132. ,
  1133. {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
  1134. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1135. FE_RAM|FE_DIFF|FE_VARCLK}
  1136. ,
  1137. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1138. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1139. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
  1140. FE_RAM|FE_LCKFRQ}
  1141. ,
  1142. #else
  1143. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1144. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1145. FE_RAM|FE_LCKFRQ}
  1146. ,
  1147. #endif
  1148. {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
  1149. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1150. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1151. ,
  1152. {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
  1153. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1154. FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1155. ,
  1156. {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
  1157. FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1158. FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1159. ,
  1160. {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
  1161. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1162. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1163. FE_C10}
  1164. ,
  1165. {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
  1166. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1167. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1168. FE_C10|FE_U3EN}
  1169. ,
  1170. {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
  1171. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1172. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
  1173. FE_C10|FE_U3EN}
  1174. ,
  1175. {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
  1176. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1177. FE_RAM|FE_IO256|FE_LEDC}
  1178. };
  1179. #define sym_num_devs (ARRAY_SIZE(sym_dev_table))
  1180. /*
  1181. * Look up the chip table.
  1182. *
  1183. * Return a pointer to the chip entry if found,
  1184. * zero otherwise.
  1185. */
  1186. struct sym_chip *
  1187. sym_lookup_chip_table (u_short device_id, u_char revision)
  1188. {
  1189. struct sym_chip *chip;
  1190. int i;
  1191. for (i = 0; i < sym_num_devs; i++) {
  1192. chip = &sym_dev_table[i];
  1193. if (device_id != chip->device_id)
  1194. continue;
  1195. if (revision > chip->revision_id)
  1196. continue;
  1197. return chip;
  1198. }
  1199. return NULL;
  1200. }
  1201. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1202. /*
  1203. * Lookup the 64 bit DMA segments map.
  1204. * This is only used if the direct mapping
  1205. * has been unsuccessful.
  1206. */
  1207. int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
  1208. {
  1209. int i;
  1210. if (!np->use_dac)
  1211. goto weird;
  1212. /* Look up existing mappings */
  1213. for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
  1214. if (h == np->dmap_bah[i])
  1215. return i;
  1216. }
  1217. /* If direct mapping is free, get it */
  1218. if (!np->dmap_bah[s])
  1219. goto new;
  1220. /* Collision -> lookup free mappings */
  1221. for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
  1222. if (!np->dmap_bah[s])
  1223. goto new;
  1224. }
  1225. weird:
  1226. panic("sym: ran out of 64 bit DMA segment registers");
  1227. return -1;
  1228. new:
  1229. np->dmap_bah[s] = h;
  1230. np->dmap_dirty = 1;
  1231. return s;
  1232. }
  1233. /*
  1234. * Update IO registers scratch C..R so they will be
  1235. * in sync. with queued CCB expectations.
  1236. */
  1237. static void sym_update_dmap_regs(struct sym_hcb *np)
  1238. {
  1239. int o, i;
  1240. if (!np->dmap_dirty)
  1241. return;
  1242. o = offsetof(struct sym_reg, nc_scrx[0]);
  1243. for (i = 0; i < SYM_DMAP_SIZE; i++) {
  1244. OUTL_OFF(np, o, np->dmap_bah[i]);
  1245. o += 4;
  1246. }
  1247. np->dmap_dirty = 0;
  1248. }
  1249. #endif
  1250. /* Enforce all the fiddly SPI rules and the chip limitations */
  1251. static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
  1252. struct sym_trans *goal)
  1253. {
  1254. if (!spi_support_wide(starget))
  1255. goal->width = 0;
  1256. if (!spi_support_sync(starget)) {
  1257. goal->iu = 0;
  1258. goal->dt = 0;
  1259. goal->qas = 0;
  1260. goal->offset = 0;
  1261. return;
  1262. }
  1263. if (spi_support_dt(starget)) {
  1264. if (spi_support_dt_only(starget))
  1265. goal->dt = 1;
  1266. if (goal->offset == 0)
  1267. goal->dt = 0;
  1268. } else {
  1269. goal->dt = 0;
  1270. }
  1271. /* Some targets fail to properly negotiate DT in SE mode */
  1272. if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
  1273. goal->dt = 0;
  1274. if (goal->dt) {
  1275. /* all DT transfers must be wide */
  1276. goal->width = 1;
  1277. if (goal->offset > np->maxoffs_dt)
  1278. goal->offset = np->maxoffs_dt;
  1279. if (goal->period < np->minsync_dt)
  1280. goal->period = np->minsync_dt;
  1281. if (goal->period > np->maxsync_dt)
  1282. goal->period = np->maxsync_dt;
  1283. } else {
  1284. goal->iu = goal->qas = 0;
  1285. if (goal->offset > np->maxoffs)
  1286. goal->offset = np->maxoffs;
  1287. if (goal->period < np->minsync)
  1288. goal->period = np->minsync;
  1289. if (goal->period > np->maxsync)
  1290. goal->period = np->maxsync;
  1291. }
  1292. }
  1293. /*
  1294. * Prepare the next negotiation message if needed.
  1295. *
  1296. * Fill in the part of message buffer that contains the
  1297. * negotiation and the nego_status field of the CCB.
  1298. * Returns the size of the message in bytes.
  1299. */
  1300. static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
  1301. {
  1302. struct sym_tcb *tp = &np->target[cp->target];
  1303. struct scsi_target *starget = tp->starget;
  1304. struct sym_trans *goal = &tp->tgoal;
  1305. int msglen = 0;
  1306. int nego;
  1307. sym_check_goals(np, starget, goal);
  1308. /*
  1309. * Many devices implement PPR in a buggy way, so only use it if we
  1310. * really want to.
  1311. */
  1312. if (goal->offset &&
  1313. (goal->iu || goal->dt || goal->qas || (goal->period < 0xa))) {
  1314. nego = NS_PPR;
  1315. } else if (spi_width(starget) != goal->width) {
  1316. nego = NS_WIDE;
  1317. } else if (spi_period(starget) != goal->period ||
  1318. spi_offset(starget) != goal->offset) {
  1319. nego = NS_SYNC;
  1320. } else {
  1321. goal->check_nego = 0;
  1322. nego = 0;
  1323. }
  1324. switch (nego) {
  1325. case NS_SYNC:
  1326. msglen += spi_populate_sync_msg(msgptr + msglen, goal->period,
  1327. goal->offset);
  1328. break;
  1329. case NS_WIDE:
  1330. msglen += spi_populate_width_msg(msgptr + msglen, goal->width);
  1331. break;
  1332. case NS_PPR:
  1333. msglen += spi_populate_ppr_msg(msgptr + msglen, goal->period,
  1334. goal->offset, goal->width,
  1335. (goal->iu ? PPR_OPT_IU : 0) |
  1336. (goal->dt ? PPR_OPT_DT : 0) |
  1337. (goal->qas ? PPR_OPT_QAS : 0));
  1338. break;
  1339. }
  1340. cp->nego_status = nego;
  1341. if (nego) {
  1342. tp->nego_cp = cp; /* Keep track a nego will be performed */
  1343. if (DEBUG_FLAGS & DEBUG_NEGO) {
  1344. sym_print_nego_msg(np, cp->target,
  1345. nego == NS_SYNC ? "sync msgout" :
  1346. nego == NS_WIDE ? "wide msgout" :
  1347. "ppr msgout", msgptr);
  1348. }
  1349. }
  1350. return msglen;
  1351. }
  1352. /*
  1353. * Insert a job into the start queue.
  1354. */
  1355. void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
  1356. {
  1357. u_short qidx;
  1358. #ifdef SYM_CONF_IARB_SUPPORT
  1359. /*
  1360. * If the previously queued CCB is not yet done,
  1361. * set the IARB hint. The SCRIPTS will go with IARB
  1362. * for this job when starting the previous one.
  1363. * We leave devices a chance to win arbitration by
  1364. * not using more than 'iarb_max' consecutive
  1365. * immediate arbitrations.
  1366. */
  1367. if (np->last_cp && np->iarb_count < np->iarb_max) {
  1368. np->last_cp->host_flags |= HF_HINT_IARB;
  1369. ++np->iarb_count;
  1370. }
  1371. else
  1372. np->iarb_count = 0;
  1373. np->last_cp = cp;
  1374. #endif
  1375. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1376. /*
  1377. * Make SCRIPTS aware of the 64 bit DMA
  1378. * segment registers not being up-to-date.
  1379. */
  1380. if (np->dmap_dirty)
  1381. cp->host_xflags |= HX_DMAP_DIRTY;
  1382. #endif
  1383. /*
  1384. * Insert first the idle task and then our job.
  1385. * The MBs should ensure proper ordering.
  1386. */
  1387. qidx = np->squeueput + 2;
  1388. if (qidx >= MAX_QUEUE*2) qidx = 0;
  1389. np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
  1390. MEMORY_WRITE_BARRIER();
  1391. np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
  1392. np->squeueput = qidx;
  1393. if (DEBUG_FLAGS & DEBUG_QUEUE)
  1394. printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput);
  1395. /*
  1396. * Script processor may be waiting for reselect.
  1397. * Wake it up.
  1398. */
  1399. MEMORY_WRITE_BARRIER();
  1400. OUTB(np, nc_istat, SIGP|np->istat_sem);
  1401. }
  1402. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1403. /*
  1404. * Start next ready-to-start CCBs.
  1405. */
  1406. void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
  1407. {
  1408. SYM_QUEHEAD *qp;
  1409. struct sym_ccb *cp;
  1410. /*
  1411. * Paranoia, as usual. :-)
  1412. */
  1413. assert(!lp->started_tags || !lp->started_no_tag);
  1414. /*
  1415. * Try to start as many commands as asked by caller.
  1416. * Prevent from having both tagged and untagged
  1417. * commands queued to the device at the same time.
  1418. */
  1419. while (maxn--) {
  1420. qp = sym_remque_head(&lp->waiting_ccbq);
  1421. if (!qp)
  1422. break;
  1423. cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
  1424. if (cp->tag != NO_TAG) {
  1425. if (lp->started_no_tag ||
  1426. lp->started_tags >= lp->started_max) {
  1427. sym_insque_head(qp, &lp->waiting_ccbq);
  1428. break;
  1429. }
  1430. lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
  1431. lp->head.resel_sa =
  1432. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  1433. ++lp->started_tags;
  1434. } else {
  1435. if (lp->started_no_tag || lp->started_tags) {
  1436. sym_insque_head(qp, &lp->waiting_ccbq);
  1437. break;
  1438. }
  1439. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  1440. lp->head.resel_sa =
  1441. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  1442. ++lp->started_no_tag;
  1443. }
  1444. cp->started = 1;
  1445. sym_insque_tail(qp, &lp->started_ccbq);
  1446. sym_put_start_queue(np, cp);
  1447. }
  1448. }
  1449. #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
  1450. /*
  1451. * The chip may have completed jobs. Look at the DONE QUEUE.
  1452. *
  1453. * On paper, memory read barriers may be needed here to
  1454. * prevent out of order LOADs by the CPU from having
  1455. * prefetched stale data prior to DMA having occurred.
  1456. */
  1457. static int sym_wakeup_done (struct sym_hcb *np)
  1458. {
  1459. struct sym_ccb *cp;
  1460. int i, n;
  1461. u32 dsa;
  1462. n = 0;
  1463. i = np->dqueueget;
  1464. /* MEMORY_READ_BARRIER(); */
  1465. while (1) {
  1466. dsa = scr_to_cpu(np->dqueue[i]);
  1467. if (!dsa)
  1468. break;
  1469. np->dqueue[i] = 0;
  1470. if ((i = i+2) >= MAX_QUEUE*2)
  1471. i = 0;
  1472. cp = sym_ccb_from_dsa(np, dsa);
  1473. if (cp) {
  1474. MEMORY_READ_BARRIER();
  1475. sym_complete_ok (np, cp);
  1476. ++n;
  1477. }
  1478. else
  1479. printf ("%s: bad DSA (%x) in done queue.\n",
  1480. sym_name(np), (u_int) dsa);
  1481. }
  1482. np->dqueueget = i;
  1483. return n;
  1484. }
  1485. /*
  1486. * Complete all CCBs queued to the COMP queue.
  1487. *
  1488. * These CCBs are assumed:
  1489. * - Not to be referenced either by devices or
  1490. * SCRIPTS-related queues and datas.
  1491. * - To have to be completed with an error condition
  1492. * or requeued.
  1493. *
  1494. * The device queue freeze count is incremented
  1495. * for each CCB that does not prevent this.
  1496. * This function is called when all CCBs involved
  1497. * in error handling/recovery have been reaped.
  1498. */
  1499. static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
  1500. {
  1501. SYM_QUEHEAD *qp;
  1502. struct sym_ccb *cp;
  1503. while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) {
  1504. struct scsi_cmnd *cmd;
  1505. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1506. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  1507. /* Leave quiet CCBs waiting for resources */
  1508. if (cp->host_status == HS_WAIT)
  1509. continue;
  1510. cmd = cp->cmd;
  1511. if (cam_status)
  1512. sym_set_cam_status(cmd, cam_status);
  1513. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1514. if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
  1515. struct sym_tcb *tp = &np->target[cp->target];
  1516. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  1517. if (lp) {
  1518. sym_remque(&cp->link2_ccbq);
  1519. sym_insque_tail(&cp->link2_ccbq,
  1520. &lp->waiting_ccbq);
  1521. if (cp->started) {
  1522. if (cp->tag != NO_TAG)
  1523. --lp->started_tags;
  1524. else
  1525. --lp->started_no_tag;
  1526. }
  1527. }
  1528. cp->started = 0;
  1529. continue;
  1530. }
  1531. #endif
  1532. sym_free_ccb(np, cp);
  1533. sym_xpt_done(np, cmd);
  1534. }
  1535. }
  1536. /*
  1537. * Complete all active CCBs with error.
  1538. * Used on CHIP/SCSI RESET.
  1539. */
  1540. static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
  1541. {
  1542. /*
  1543. * Move all active CCBs to the COMP queue
  1544. * and flush this queue.
  1545. */
  1546. sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
  1547. sym_que_init(&np->busy_ccbq);
  1548. sym_flush_comp_queue(np, cam_status);
  1549. }
  1550. /*
  1551. * Start chip.
  1552. *
  1553. * 'reason' means:
  1554. * 0: initialisation.
  1555. * 1: SCSI BUS RESET delivered or received.
  1556. * 2: SCSI BUS MODE changed.
  1557. */
  1558. void sym_start_up (struct sym_hcb *np, int reason)
  1559. {
  1560. int i;
  1561. u32 phys;
  1562. /*
  1563. * Reset chip if asked, otherwise just clear fifos.
  1564. */
  1565. if (reason == 1)
  1566. sym_soft_reset(np);
  1567. else {
  1568. OUTB(np, nc_stest3, TE|CSF);
  1569. OUTONB(np, nc_ctest3, CLF);
  1570. }
  1571. /*
  1572. * Clear Start Queue
  1573. */
  1574. phys = np->squeue_ba;
  1575. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1576. np->squeue[i] = cpu_to_scr(np->idletask_ba);
  1577. np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1578. }
  1579. np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1580. /*
  1581. * Start at first entry.
  1582. */
  1583. np->squeueput = 0;
  1584. /*
  1585. * Clear Done Queue
  1586. */
  1587. phys = np->dqueue_ba;
  1588. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1589. np->dqueue[i] = 0;
  1590. np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1591. }
  1592. np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1593. /*
  1594. * Start at first entry.
  1595. */
  1596. np->dqueueget = 0;
  1597. /*
  1598. * Install patches in scripts.
  1599. * This also let point to first position the start
  1600. * and done queue pointers used from SCRIPTS.
  1601. */
  1602. np->fw_patch(np);
  1603. /*
  1604. * Wakeup all pending jobs.
  1605. */
  1606. sym_flush_busy_queue(np, DID_RESET);
  1607. /*
  1608. * Init chip.
  1609. */
  1610. OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
  1611. INB(np, nc_mbox1);
  1612. udelay(2000); /* The 895 needs time for the bus mode to settle */
  1613. OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
  1614. /* full arb., ena parity, par->ATN */
  1615. OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
  1616. sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
  1617. OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
  1618. OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
  1619. OUTB(np, nc_istat , SIGP ); /* Signal Process */
  1620. OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
  1621. OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
  1622. OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
  1623. OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
  1624. OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
  1625. /* Extended Sreq/Sack filtering not supported on the C10 */
  1626. if (np->features & FE_C10)
  1627. OUTB(np, nc_stest2, np->rv_stest2);
  1628. else
  1629. OUTB(np, nc_stest2, EXT|np->rv_stest2);
  1630. OUTB(np, nc_stest3, TE); /* TolerANT enable */
  1631. OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
  1632. /*
  1633. * For now, disable AIP generation on C1010-66.
  1634. */
  1635. if (np->s.device->device == PCI_DEVICE_ID_LSI_53C1010_66)
  1636. OUTB(np, nc_aipcntl1, DISAIP);
  1637. /*
  1638. * C10101 rev. 0 errata.
  1639. * Errant SGE's when in narrow. Write bits 4 & 5 of
  1640. * STEST1 register to disable SGE. We probably should do
  1641. * that from SCRIPTS for each selection/reselection, but
  1642. * I just don't want. :)
  1643. */
  1644. if (np->s.device->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
  1645. np->s.device->revision < 1)
  1646. OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
  1647. /*
  1648. * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
  1649. * Disable overlapped arbitration for some dual function devices,
  1650. * regardless revision id (kind of post-chip-design feature. ;-))
  1651. */
  1652. if (np->s.device->device == PCI_DEVICE_ID_NCR_53C875)
  1653. OUTB(np, nc_ctest0, (1<<5));
  1654. else if (np->s.device->device == PCI_DEVICE_ID_NCR_53C896)
  1655. np->rv_ccntl0 |= DPR;
  1656. /*
  1657. * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
  1658. * and/or hardware phase mismatch, since only such chips
  1659. * seem to support those IO registers.
  1660. */
  1661. if (np->features & (FE_DAC|FE_NOPM)) {
  1662. OUTB(np, nc_ccntl0, np->rv_ccntl0);
  1663. OUTB(np, nc_ccntl1, np->rv_ccntl1);
  1664. }
  1665. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1666. /*
  1667. * Set up scratch C and DRS IO registers to map the 32 bit
  1668. * DMA address range our data structures are located in.
  1669. */
  1670. if (np->use_dac) {
  1671. np->dmap_bah[0] = 0; /* ??? */
  1672. OUTL(np, nc_scrx[0], np->dmap_bah[0]);
  1673. OUTL(np, nc_drs, np->dmap_bah[0]);
  1674. }
  1675. #endif
  1676. /*
  1677. * If phase mismatch handled by scripts (895A/896/1010),
  1678. * set PM jump addresses.
  1679. */
  1680. if (np->features & FE_NOPM) {
  1681. OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
  1682. OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
  1683. }
  1684. /*
  1685. * Enable GPIO0 pin for writing if LED support from SCRIPTS.
  1686. * Also set GPIO5 and clear GPIO6 if hardware LED control.
  1687. */
  1688. if (np->features & FE_LED0)
  1689. OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
  1690. else if (np->features & FE_LEDC)
  1691. OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
  1692. /*
  1693. * enable ints
  1694. */
  1695. OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
  1696. OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
  1697. /*
  1698. * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
  1699. * Try to eat the spurious SBMC interrupt that may occur when
  1700. * we reset the chip but not the SCSI BUS (at initialization).
  1701. */
  1702. if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
  1703. OUTONW(np, nc_sien, SBMC);
  1704. if (reason == 0) {
  1705. INB(np, nc_mbox1);
  1706. mdelay(100);
  1707. INW(np, nc_sist);
  1708. }
  1709. np->scsi_mode = INB(np, nc_stest4) & SMODE;
  1710. }
  1711. /*
  1712. * Fill in target structure.
  1713. * Reinitialize usrsync.
  1714. * Reinitialize usrwide.
  1715. * Prepare sync negotiation according to actual SCSI bus mode.
  1716. */
  1717. for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
  1718. struct sym_tcb *tp = &np->target[i];
  1719. tp->to_reset = 0;
  1720. tp->head.sval = 0;
  1721. tp->head.wval = np->rv_scntl3;
  1722. tp->head.uval = 0;
  1723. }
  1724. /*
  1725. * Download SCSI SCRIPTS to on-chip RAM if present,
  1726. * and start script processor.
  1727. * We do the download preferently from the CPU.
  1728. * For platforms that may not support PCI memory mapping,
  1729. * we use simple SCRIPTS that performs MEMORY MOVEs.
  1730. */
  1731. phys = SCRIPTA_BA(np, init);
  1732. if (np->ram_ba) {
  1733. if (sym_verbose >= 2)
  1734. printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
  1735. memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
  1736. if (np->ram_ws == 8192) {
  1737. memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
  1738. phys = scr_to_cpu(np->scr_ram_seg);
  1739. OUTL(np, nc_mmws, phys);
  1740. OUTL(np, nc_mmrs, phys);
  1741. OUTL(np, nc_sfs, phys);
  1742. phys = SCRIPTB_BA(np, start64);
  1743. }
  1744. }
  1745. np->istat_sem = 0;
  1746. OUTL(np, nc_dsa, np->hcb_ba);
  1747. OUTL_DSP(np, phys);
  1748. /*
  1749. * Notify the XPT about the RESET condition.
  1750. */
  1751. if (reason != 0)
  1752. sym_xpt_async_bus_reset(np);
  1753. }
  1754. /*
  1755. * Switch trans mode for current job and its target.
  1756. */
  1757. static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1758. u_char per, u_char wide, u_char div, u_char fak)
  1759. {
  1760. SYM_QUEHEAD *qp;
  1761. u_char sval, wval, uval;
  1762. struct sym_tcb *tp = &np->target[target];
  1763. assert(target == (INB(np, nc_sdid) & 0x0f));
  1764. sval = tp->head.sval;
  1765. wval = tp->head.wval;
  1766. uval = tp->head.uval;
  1767. #if 0
  1768. printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
  1769. sval, wval, uval, np->rv_scntl3);
  1770. #endif
  1771. /*
  1772. * Set the offset.
  1773. */
  1774. if (!(np->features & FE_C10))
  1775. sval = (sval & ~0x1f) | ofs;
  1776. else
  1777. sval = (sval & ~0x3f) | ofs;
  1778. /*
  1779. * Set the sync divisor and extra clock factor.
  1780. */
  1781. if (ofs != 0) {
  1782. wval = (wval & ~0x70) | ((div+1) << 4);
  1783. if (!(np->features & FE_C10))
  1784. sval = (sval & ~0xe0) | (fak << 5);
  1785. else {
  1786. uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
  1787. if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
  1788. if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
  1789. }
  1790. }
  1791. /*
  1792. * Set the bus width.
  1793. */
  1794. wval = wval & ~EWS;
  1795. if (wide != 0)
  1796. wval |= EWS;
  1797. /*
  1798. * Set misc. ultra enable bits.
  1799. */
  1800. if (np->features & FE_C10) {
  1801. uval = uval & ~(U3EN|AIPCKEN);
  1802. if (opts) {
  1803. assert(np->features & FE_U3EN);
  1804. uval |= U3EN;
  1805. }
  1806. } else {
  1807. wval = wval & ~ULTRA;
  1808. if (per <= 12) wval |= ULTRA;
  1809. }
  1810. /*
  1811. * Stop there if sync parameters are unchanged.
  1812. */
  1813. if (tp->head.sval == sval &&
  1814. tp->head.wval == wval &&
  1815. tp->head.uval == uval)
  1816. return;
  1817. tp->head.sval = sval;
  1818. tp->head.wval = wval;
  1819. tp->head.uval = uval;
  1820. /*
  1821. * Disable extended Sreq/Sack filtering if per < 50.
  1822. * Not supported on the C1010.
  1823. */
  1824. if (per < 50 && !(np->features & FE_C10))
  1825. OUTOFFB(np, nc_stest2, EXT);
  1826. /*
  1827. * set actual value and sync_status
  1828. */
  1829. OUTB(np, nc_sxfer, tp->head.sval);
  1830. OUTB(np, nc_scntl3, tp->head.wval);
  1831. if (np->features & FE_C10) {
  1832. OUTB(np, nc_scntl4, tp->head.uval);
  1833. }
  1834. /*
  1835. * patch ALL busy ccbs of this target.
  1836. */
  1837. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  1838. struct sym_ccb *cp;
  1839. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1840. if (cp->target != target)
  1841. continue;
  1842. cp->phys.select.sel_scntl3 = tp->head.wval;
  1843. cp->phys.select.sel_sxfer = tp->head.sval;
  1844. if (np->features & FE_C10) {
  1845. cp->phys.select.sel_scntl4 = tp->head.uval;
  1846. }
  1847. }
  1848. }
  1849. /*
  1850. * We received a WDTR.
  1851. * Let everything be aware of the changes.
  1852. */
  1853. static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
  1854. {
  1855. struct sym_tcb *tp = &np->target[target];
  1856. struct scsi_target *starget = tp->starget;
  1857. if (spi_width(starget) == wide)
  1858. return;
  1859. sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
  1860. tp->tgoal.width = wide;
  1861. spi_offset(starget) = 0;
  1862. spi_period(starget) = 0;
  1863. spi_width(starget) = wide;
  1864. spi_iu(starget) = 0;
  1865. spi_dt(starget) = 0;
  1866. spi_qas(starget) = 0;
  1867. if (sym_verbose >= 3)
  1868. spi_display_xfer_agreement(starget);
  1869. }
  1870. /*
  1871. * We received a SDTR.
  1872. * Let everything be aware of the changes.
  1873. */
  1874. static void
  1875. sym_setsync(struct sym_hcb *np, int target,
  1876. u_char ofs, u_char per, u_char div, u_char fak)
  1877. {
  1878. struct sym_tcb *tp = &np->target[target];
  1879. struct scsi_target *starget = tp->starget;
  1880. u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
  1881. sym_settrans(np, target, 0, ofs, per, wide, div, fak);
  1882. spi_period(starget) = per;
  1883. spi_offset(starget) = ofs;
  1884. spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
  1885. if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
  1886. tp->tgoal.period = per;
  1887. tp->tgoal.offset = ofs;
  1888. tp->tgoal.check_nego = 0;
  1889. }
  1890. spi_display_xfer_agreement(starget);
  1891. }
  1892. /*
  1893. * We received a PPR.
  1894. * Let everything be aware of the changes.
  1895. */
  1896. static void
  1897. sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1898. u_char per, u_char wide, u_char div, u_char fak)
  1899. {
  1900. struct sym_tcb *tp = &np->target[target];
  1901. struct scsi_target *starget = tp->starget;
  1902. sym_settrans(np, target, opts, ofs, per, wide, div, fak);
  1903. spi_width(starget) = tp->tgoal.width = wide;
  1904. spi_period(starget) = tp->tgoal.period = per;
  1905. spi_offset(starget) = tp->tgoal.offset = ofs;
  1906. spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
  1907. spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
  1908. spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
  1909. tp->tgoal.check_nego = 0;
  1910. spi_display_xfer_agreement(starget);
  1911. }
  1912. /*
  1913. * generic recovery from scsi interrupt
  1914. *
  1915. * The doc says that when the chip gets an SCSI interrupt,
  1916. * it tries to stop in an orderly fashion, by completing
  1917. * an instruction fetch that had started or by flushing
  1918. * the DMA fifo for a write to memory that was executing.
  1919. * Such a fashion is not enough to know if the instruction
  1920. * that was just before the current DSP value has been
  1921. * executed or not.
  1922. *
  1923. * There are some small SCRIPTS sections that deal with
  1924. * the start queue and the done queue that may break any
  1925. * assomption from the C code if we are interrupted
  1926. * inside, so we reset if this happens. Btw, since these
  1927. * SCRIPTS sections are executed while the SCRIPTS hasn't
  1928. * started SCSI operations, it is very unlikely to happen.
  1929. *
  1930. * All the driver data structures are supposed to be
  1931. * allocated from the same 4 GB memory window, so there
  1932. * is a 1 to 1 relationship between DSA and driver data
  1933. * structures. Since we are careful :) to invalidate the
  1934. * DSA when we complete a command or when the SCRIPTS
  1935. * pushes a DSA into a queue, we can trust it when it
  1936. * points to a CCB.
  1937. */
  1938. static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
  1939. {
  1940. u32 dsp = INL(np, nc_dsp);
  1941. u32 dsa = INL(np, nc_dsa);
  1942. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  1943. /*
  1944. * If we haven't been interrupted inside the SCRIPTS
  1945. * critical pathes, we can safely restart the SCRIPTS
  1946. * and trust the DSA value if it matches a CCB.
  1947. */
  1948. if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
  1949. dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
  1950. (!(dsp > SCRIPTA_BA(np, ungetjob) &&
  1951. dsp < SCRIPTA_BA(np, reselect) + 1)) &&
  1952. (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
  1953. dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
  1954. (!(dsp > SCRIPTA_BA(np, done) &&
  1955. dsp < SCRIPTA_BA(np, done_end) + 1))) {
  1956. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  1957. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  1958. /*
  1959. * If we have a CCB, let the SCRIPTS call us back for
  1960. * the handling of the error with SCRATCHA filled with
  1961. * STARTPOS. This way, we will be able to freeze the
  1962. * device queue and requeue awaiting IOs.
  1963. */
  1964. if (cp) {
  1965. cp->host_status = hsts;
  1966. OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
  1967. }
  1968. /*
  1969. * Otherwise just restart the SCRIPTS.
  1970. */
  1971. else {
  1972. OUTL(np, nc_dsa, 0xffffff);
  1973. OUTL_DSP(np, SCRIPTA_BA(np, start));
  1974. }
  1975. }
  1976. else
  1977. goto reset_all;
  1978. return;
  1979. reset_all:
  1980. sym_start_reset(np);
  1981. }
  1982. /*
  1983. * chip exception handler for selection timeout
  1984. */
  1985. static void sym_int_sto (struct sym_hcb *np)
  1986. {
  1987. u32 dsp = INL(np, nc_dsp);
  1988. if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
  1989. if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
  1990. sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
  1991. else
  1992. sym_start_reset(np);
  1993. }
  1994. /*
  1995. * chip exception handler for unexpected disconnect
  1996. */
  1997. static void sym_int_udc (struct sym_hcb *np)
  1998. {
  1999. printf ("%s: unexpected disconnect\n", sym_name(np));
  2000. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2001. }
  2002. /*
  2003. * chip exception handler for SCSI bus mode change
  2004. *
  2005. * spi2-r12 11.2.3 says a transceiver mode change must
  2006. * generate a reset event and a device that detects a reset
  2007. * event shall initiate a hard reset. It says also that a
  2008. * device that detects a mode change shall set data transfer
  2009. * mode to eight bit asynchronous, etc...
  2010. * So, just reinitializing all except chip should be enough.
  2011. */
  2012. static void sym_int_sbmc (struct sym_hcb *np)
  2013. {
  2014. u_char scsi_mode = INB(np, nc_stest4) & SMODE;
  2015. /*
  2016. * Notify user.
  2017. */
  2018. printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
  2019. sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
  2020. /*
  2021. * Should suspend command processing for a few seconds and
  2022. * reinitialize all except the chip.
  2023. */
  2024. sym_start_up (np, 2);
  2025. }
  2026. /*
  2027. * chip exception handler for SCSI parity error.
  2028. *
  2029. * When the chip detects a SCSI parity error and is
  2030. * currently executing a (CH)MOV instruction, it does
  2031. * not interrupt immediately, but tries to finish the
  2032. * transfer of the current scatter entry before
  2033. * interrupting. The following situations may occur:
  2034. *
  2035. * - The complete scatter entry has been transferred
  2036. * without the device having changed phase.
  2037. * The chip will then interrupt with the DSP pointing
  2038. * to the instruction that follows the MOV.
  2039. *
  2040. * - A phase mismatch occurs before the MOV finished
  2041. * and phase errors are to be handled by the C code.
  2042. * The chip will then interrupt with both PAR and MA
  2043. * conditions set.
  2044. *
  2045. * - A phase mismatch occurs before the MOV finished and
  2046. * phase errors are to be handled by SCRIPTS.
  2047. * The chip will load the DSP with the phase mismatch
  2048. * JUMP address and interrupt the host processor.
  2049. */
  2050. static void sym_int_par (struct sym_hcb *np, u_short sist)
  2051. {
  2052. u_char hsts = INB(np, HS_PRT);
  2053. u32 dsp = INL(np, nc_dsp);
  2054. u32 dbc = INL(np, nc_dbc);
  2055. u32 dsa = INL(np, nc_dsa);
  2056. u_char sbcl = INB(np, nc_sbcl);
  2057. u_char cmd = dbc >> 24;
  2058. int phase = cmd & 7;
  2059. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  2060. printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
  2061. sym_name(np), hsts, dbc, sbcl);
  2062. /*
  2063. * Check that the chip is connected to the SCSI BUS.
  2064. */
  2065. if (!(INB(np, nc_scntl1) & ISCON)) {
  2066. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2067. return;
  2068. }
  2069. /*
  2070. * If the nexus is not clearly identified, reset the bus.
  2071. * We will try to do better later.
  2072. */
  2073. if (!cp)
  2074. goto reset_all;
  2075. /*
  2076. * Check instruction was a MOV, direction was INPUT and
  2077. * ATN is asserted.
  2078. */
  2079. if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
  2080. goto reset_all;
  2081. /*
  2082. * Keep track of the parity error.
  2083. */
  2084. OUTONB(np, HF_PRT, HF_EXT_ERR);
  2085. cp->xerr_status |= XE_PARITY_ERR;
  2086. /*
  2087. * Prepare the message to send to the device.
  2088. */
  2089. np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
  2090. /*
  2091. * If the old phase was DATA IN phase, we have to deal with
  2092. * the 3 situations described above.
  2093. * For other input phases (MSG IN and STATUS), the device
  2094. * must resend the whole thing that failed parity checking
  2095. * or signal error. So, jumping to dispatcher should be OK.
  2096. */
  2097. if (phase == 1 || phase == 5) {
  2098. /* Phase mismatch handled by SCRIPTS */
  2099. if (dsp == SCRIPTB_BA(np, pm_handle))
  2100. OUTL_DSP(np, dsp);
  2101. /* Phase mismatch handled by the C code */
  2102. else if (sist & MA)
  2103. sym_int_ma (np);
  2104. /* No phase mismatch occurred */
  2105. else {
  2106. sym_set_script_dp (np, cp, dsp);
  2107. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2108. }
  2109. }
  2110. else if (phase == 7) /* We definitely cannot handle parity errors */
  2111. #if 1 /* in message-in phase due to the relection */
  2112. goto reset_all; /* path and various message anticipations. */
  2113. #else
  2114. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  2115. #endif
  2116. else
  2117. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2118. return;
  2119. reset_all:
  2120. sym_start_reset(np);
  2121. return;
  2122. }
  2123. /*
  2124. * chip exception handler for phase errors.
  2125. *
  2126. * We have to construct a new transfer descriptor,
  2127. * to transfer the rest of the current block.
  2128. */
  2129. static void sym_int_ma (struct sym_hcb *np)
  2130. {
  2131. u32 dbc;
  2132. u32 rest;
  2133. u32 dsp;
  2134. u32 dsa;
  2135. u32 nxtdsp;
  2136. u32 *vdsp;
  2137. u32 oadr, olen;
  2138. u32 *tblp;
  2139. u32 newcmd;
  2140. u_int delta;
  2141. u_char cmd;
  2142. u_char hflags, hflags0;
  2143. struct sym_pmc *pm;
  2144. struct sym_ccb *cp;
  2145. dsp = INL(np, nc_dsp);
  2146. dbc = INL(np, nc_dbc);
  2147. dsa = INL(np, nc_dsa);
  2148. cmd = dbc >> 24;
  2149. rest = dbc & 0xffffff;
  2150. delta = 0;
  2151. /*
  2152. * locate matching cp if any.
  2153. */
  2154. cp = sym_ccb_from_dsa(np, dsa);
  2155. /*
  2156. * Donnot take into account dma fifo and various buffers in
  2157. * INPUT phase since the chip flushes everything before
  2158. * raising the MA interrupt for interrupted INPUT phases.
  2159. * For DATA IN phase, we will check for the SWIDE later.
  2160. */
  2161. if ((cmd & 7) != 1 && (cmd & 7) != 5) {
  2162. u_char ss0, ss2;
  2163. if (np->features & FE_DFBC)
  2164. delta = INW(np, nc_dfbc);
  2165. else {
  2166. u32 dfifo;
  2167. /*
  2168. * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
  2169. */
  2170. dfifo = INL(np, nc_dfifo);
  2171. /*
  2172. * Calculate remaining bytes in DMA fifo.
  2173. * (CTEST5 = dfifo >> 16)
  2174. */
  2175. if (dfifo & (DFS << 16))
  2176. delta = ((((dfifo >> 8) & 0x300) |
  2177. (dfifo & 0xff)) - rest) & 0x3ff;
  2178. else
  2179. delta = ((dfifo & 0xff) - rest) & 0x7f;
  2180. }
  2181. /*
  2182. * The data in the dma fifo has not been transfered to
  2183. * the target -> add the amount to the rest
  2184. * and clear the data.
  2185. * Check the sstat2 register in case of wide transfer.
  2186. */
  2187. rest += delta;
  2188. ss0 = INB(np, nc_sstat0);
  2189. if (ss0 & OLF) rest++;
  2190. if (!(np->features & FE_C10))
  2191. if (ss0 & ORF) rest++;
  2192. if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
  2193. ss2 = INB(np, nc_sstat2);
  2194. if (ss2 & OLF1) rest++;
  2195. if (!(np->features & FE_C10))
  2196. if (ss2 & ORF1) rest++;
  2197. }
  2198. /*
  2199. * Clear fifos.
  2200. */
  2201. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
  2202. OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
  2203. }
  2204. /*
  2205. * log the information
  2206. */
  2207. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  2208. printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
  2209. (unsigned) rest, (unsigned) delta);
  2210. /*
  2211. * try to find the interrupted script command,
  2212. * and the address at which to continue.
  2213. */
  2214. vdsp = NULL;
  2215. nxtdsp = 0;
  2216. if (dsp > np->scripta_ba &&
  2217. dsp <= np->scripta_ba + np->scripta_sz) {
  2218. vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
  2219. nxtdsp = dsp;
  2220. }
  2221. else if (dsp > np->scriptb_ba &&
  2222. dsp <= np->scriptb_ba + np->scriptb_sz) {
  2223. vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
  2224. nxtdsp = dsp;
  2225. }
  2226. /*
  2227. * log the information
  2228. */
  2229. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2230. printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
  2231. cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
  2232. }
  2233. if (!vdsp) {
  2234. printf ("%s: interrupted SCRIPT address not found.\n",
  2235. sym_name (np));
  2236. goto reset_all;
  2237. }
  2238. if (!cp) {
  2239. printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
  2240. sym_name (np));
  2241. goto reset_all;
  2242. }
  2243. /*
  2244. * get old startaddress and old length.
  2245. */
  2246. oadr = scr_to_cpu(vdsp[1]);
  2247. if (cmd & 0x10) { /* Table indirect */
  2248. tblp = (u32 *) ((char*) &cp->phys + oadr);
  2249. olen = scr_to_cpu(tblp[0]);
  2250. oadr = scr_to_cpu(tblp[1]);
  2251. } else {
  2252. tblp = (u32 *) 0;
  2253. olen = scr_to_cpu(vdsp[0]) & 0xffffff;
  2254. }
  2255. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2256. printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
  2257. (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
  2258. tblp,
  2259. (unsigned) olen,
  2260. (unsigned) oadr);
  2261. }
  2262. /*
  2263. * check cmd against assumed interrupted script command.
  2264. * If dt data phase, the MOVE instruction hasn't bit 4 of
  2265. * the phase.
  2266. */
  2267. if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
  2268. sym_print_addr(cp->cmd,
  2269. "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
  2270. cmd, scr_to_cpu(vdsp[0]) >> 24);
  2271. goto reset_all;
  2272. }
  2273. /*
  2274. * if old phase not dataphase, leave here.
  2275. */
  2276. if (cmd & 2) {
  2277. sym_print_addr(cp->cmd,
  2278. "phase change %x-%x %d@%08x resid=%d.\n",
  2279. cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
  2280. (unsigned)oadr, (unsigned)rest);
  2281. goto unexpected_phase;
  2282. }
  2283. /*
  2284. * Choose the correct PM save area.
  2285. *
  2286. * Look at the PM_SAVE SCRIPT if you want to understand
  2287. * this stuff. The equivalent code is implemented in
  2288. * SCRIPTS for the 895A, 896 and 1010 that are able to
  2289. * handle PM from the SCRIPTS processor.
  2290. */
  2291. hflags0 = INB(np, HF_PRT);
  2292. hflags = hflags0;
  2293. if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
  2294. if (hflags & HF_IN_PM0)
  2295. nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
  2296. else if (hflags & HF_IN_PM1)
  2297. nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
  2298. if (hflags & HF_DP_SAVED)
  2299. hflags ^= HF_ACT_PM;
  2300. }
  2301. if (!(hflags & HF_ACT_PM)) {
  2302. pm = &cp->phys.pm0;
  2303. newcmd = SCRIPTA_BA(np, pm0_data);
  2304. }
  2305. else {
  2306. pm = &cp->phys.pm1;
  2307. newcmd = SCRIPTA_BA(np, pm1_data);
  2308. }
  2309. hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
  2310. if (hflags != hflags0)
  2311. OUTB(np, HF_PRT, hflags);
  2312. /*
  2313. * fillin the phase mismatch context
  2314. */
  2315. pm->sg.addr = cpu_to_scr(oadr + olen - rest);
  2316. pm->sg.size = cpu_to_scr(rest);
  2317. pm->ret = cpu_to_scr(nxtdsp);
  2318. /*
  2319. * If we have a SWIDE,
  2320. * - prepare the address to write the SWIDE from SCRIPTS,
  2321. * - compute the SCRIPTS address to restart from,
  2322. * - move current data pointer context by one byte.
  2323. */
  2324. nxtdsp = SCRIPTA_BA(np, dispatch);
  2325. if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
  2326. (INB(np, nc_scntl2) & WSR)) {
  2327. u32 tmp;
  2328. /*
  2329. * Set up the table indirect for the MOVE
  2330. * of the residual byte and adjust the data
  2331. * pointer context.
  2332. */
  2333. tmp = scr_to_cpu(pm->sg.addr);
  2334. cp->phys.wresid.addr = cpu_to_scr(tmp);
  2335. pm->sg.addr = cpu_to_scr(tmp + 1);
  2336. tmp = scr_to_cpu(pm->sg.size);
  2337. cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
  2338. pm->sg.size = cpu_to_scr(tmp - 1);
  2339. /*
  2340. * If only the residual byte is to be moved,
  2341. * no PM context is needed.
  2342. */
  2343. if ((tmp&0xffffff) == 1)
  2344. newcmd = pm->ret;
  2345. /*
  2346. * Prepare the address of SCRIPTS that will
  2347. * move the residual byte to memory.
  2348. */
  2349. nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
  2350. }
  2351. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2352. sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
  2353. hflags0, hflags, newcmd,
  2354. (unsigned)scr_to_cpu(pm->sg.addr),
  2355. (unsigned)scr_to_cpu(pm->sg.size),
  2356. (unsigned)scr_to_cpu(pm->ret));
  2357. }
  2358. /*
  2359. * Restart the SCRIPTS processor.
  2360. */
  2361. sym_set_script_dp (np, cp, newcmd);
  2362. OUTL_DSP(np, nxtdsp);
  2363. return;
  2364. /*
  2365. * Unexpected phase changes that occurs when the current phase
  2366. * is not a DATA IN or DATA OUT phase are due to error conditions.
  2367. * Such event may only happen when the SCRIPTS is using a
  2368. * multibyte SCSI MOVE.
  2369. *
  2370. * Phase change Some possible cause
  2371. *
  2372. * COMMAND --> MSG IN SCSI parity error detected by target.
  2373. * COMMAND --> STATUS Bad command or refused by target.
  2374. * MSG OUT --> MSG IN Message rejected by target.
  2375. * MSG OUT --> COMMAND Bogus target that discards extended
  2376. * negotiation messages.
  2377. *
  2378. * The code below does not care of the new phase and so
  2379. * trusts the target. Why to annoy it ?
  2380. * If the interrupted phase is COMMAND phase, we restart at
  2381. * dispatcher.
  2382. * If a target does not get all the messages after selection,
  2383. * the code assumes blindly that the target discards extended
  2384. * messages and clears the negotiation status.
  2385. * If the target does not want all our response to negotiation,
  2386. * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
  2387. * bloat for such a should_not_happen situation).
  2388. * In all other situation, we reset the BUS.
  2389. * Are these assumptions reasonnable ? (Wait and see ...)
  2390. */
  2391. unexpected_phase:
  2392. dsp -= 8;
  2393. nxtdsp = 0;
  2394. switch (cmd & 7) {
  2395. case 2: /* COMMAND phase */
  2396. nxtdsp = SCRIPTA_BA(np, dispatch);
  2397. break;
  2398. #if 0
  2399. case 3: /* STATUS phase */
  2400. nxtdsp = SCRIPTA_BA(np, dispatch);
  2401. break;
  2402. #endif
  2403. case 6: /* MSG OUT phase */
  2404. /*
  2405. * If the device may want to use untagged when we want
  2406. * tagged, we prepare an IDENTIFY without disc. granted,
  2407. * since we will not be able to handle reselect.
  2408. * Otherwise, we just don't care.
  2409. */
  2410. if (dsp == SCRIPTA_BA(np, send_ident)) {
  2411. if (cp->tag != NO_TAG && olen - rest <= 3) {
  2412. cp->host_status = HS_BUSY;
  2413. np->msgout[0] = IDENTIFY(0, cp->lun);
  2414. nxtdsp = SCRIPTB_BA(np, ident_break_atn);
  2415. }
  2416. else
  2417. nxtdsp = SCRIPTB_BA(np, ident_break);
  2418. }
  2419. else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
  2420. dsp == SCRIPTB_BA(np, send_sdtr) ||
  2421. dsp == SCRIPTB_BA(np, send_ppr)) {
  2422. nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
  2423. if (dsp == SCRIPTB_BA(np, send_ppr)) {
  2424. struct scsi_device *dev = cp->cmd->device;
  2425. dev->ppr = 0;
  2426. }
  2427. }
  2428. break;
  2429. #if 0
  2430. case 7: /* MSG IN phase */
  2431. nxtdsp = SCRIPTA_BA(np, clrack);
  2432. break;
  2433. #endif
  2434. }
  2435. if (nxtdsp) {
  2436. OUTL_DSP(np, nxtdsp);
  2437. return;
  2438. }
  2439. reset_all:
  2440. sym_start_reset(np);
  2441. }
  2442. /*
  2443. * chip interrupt handler
  2444. *
  2445. * In normal situations, interrupt conditions occur one at
  2446. * a time. But when something bad happens on the SCSI BUS,
  2447. * the chip may raise several interrupt flags before
  2448. * stopping and interrupting the CPU. The additionnal
  2449. * interrupt flags are stacked in some extra registers
  2450. * after the SIP and/or DIP flag has been raised in the
  2451. * ISTAT. After the CPU has read the interrupt condition
  2452. * flag from SIST or DSTAT, the chip unstacks the other
  2453. * interrupt flags and sets the corresponding bits in
  2454. * SIST or DSTAT. Since the chip starts stacking once the
  2455. * SIP or DIP flag is set, there is a small window of time
  2456. * where the stacking does not occur.
  2457. *
  2458. * Typically, multiple interrupt conditions may happen in
  2459. * the following situations:
  2460. *
  2461. * - SCSI parity error + Phase mismatch (PAR|MA)
  2462. * When an parity error is detected in input phase
  2463. * and the device switches to msg-in phase inside a
  2464. * block MOV.
  2465. * - SCSI parity error + Unexpected disconnect (PAR|UDC)
  2466. * When a stupid device does not want to handle the
  2467. * recovery of an SCSI parity error.
  2468. * - Some combinations of STO, PAR, UDC, ...
  2469. * When using non compliant SCSI stuff, when user is
  2470. * doing non compliant hot tampering on the BUS, when
  2471. * something really bad happens to a device, etc ...
  2472. *
  2473. * The heuristic suggested by SYMBIOS to handle
  2474. * multiple interrupts is to try unstacking all
  2475. * interrupts conditions and to handle them on some
  2476. * priority based on error severity.
  2477. * This will work when the unstacking has been
  2478. * successful, but we cannot be 100 % sure of that,
  2479. * since the CPU may have been faster to unstack than
  2480. * the chip is able to stack. Hmmm ... But it seems that
  2481. * such a situation is very unlikely to happen.
  2482. *
  2483. * If this happen, for example STO caught by the CPU
  2484. * then UDC happenning before the CPU have restarted
  2485. * the SCRIPTS, the driver may wrongly complete the
  2486. * same command on UDC, since the SCRIPTS didn't restart
  2487. * and the DSA still points to the same command.
  2488. * We avoid this situation by setting the DSA to an
  2489. * invalid value when the CCB is completed and before
  2490. * restarting the SCRIPTS.
  2491. *
  2492. * Another issue is that we need some section of our
  2493. * recovery procedures to be somehow uninterruptible but
  2494. * the SCRIPTS processor does not provides such a
  2495. * feature. For this reason, we handle recovery preferently
  2496. * from the C code and check against some SCRIPTS critical
  2497. * sections from the C code.
  2498. *
  2499. * Hopefully, the interrupt handling of the driver is now
  2500. * able to resist to weird BUS error conditions, but donnot
  2501. * ask me for any guarantee that it will never fail. :-)
  2502. * Use at your own decision and risk.
  2503. */
  2504. void sym_interrupt (struct sym_hcb *np)
  2505. {
  2506. u_char istat, istatc;
  2507. u_char dstat;
  2508. u_short sist;
  2509. /*
  2510. * interrupt on the fly ?
  2511. * (SCRIPTS may still be running)
  2512. *
  2513. * A `dummy read' is needed to ensure that the
  2514. * clear of the INTF flag reaches the device
  2515. * and that posted writes are flushed to memory
  2516. * before the scanning of the DONE queue.
  2517. * Note that SCRIPTS also (dummy) read to memory
  2518. * prior to deliver the INTF interrupt condition.
  2519. */
  2520. istat = INB(np, nc_istat);
  2521. if (istat & INTF) {
  2522. OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
  2523. istat = INB(np, nc_istat); /* DUMMY READ */
  2524. if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
  2525. sym_wakeup_done(np);
  2526. }
  2527. if (!(istat & (SIP|DIP)))
  2528. return;
  2529. #if 0 /* We should never get this one */
  2530. if (istat & CABRT)
  2531. OUTB(np, nc_istat, CABRT);
  2532. #endif
  2533. /*
  2534. * PAR and MA interrupts may occur at the same time,
  2535. * and we need to know of both in order to handle
  2536. * this situation properly. We try to unstack SCSI
  2537. * interrupts for that reason. BTW, I dislike a LOT
  2538. * such a loop inside the interrupt routine.
  2539. * Even if DMA interrupt stacking is very unlikely to
  2540. * happen, we also try unstacking these ones, since
  2541. * this has no performance impact.
  2542. */
  2543. sist = 0;
  2544. dstat = 0;
  2545. istatc = istat;
  2546. do {
  2547. if (istatc & SIP)
  2548. sist |= INW(np, nc_sist);
  2549. if (istatc & DIP)
  2550. dstat |= INB(np, nc_dstat);
  2551. istatc = INB(np, nc_istat);
  2552. istat |= istatc;
  2553. /* Prevent deadlock waiting on a condition that may
  2554. * never clear. */
  2555. if (unlikely(sist == 0xffff && dstat == 0xff)) {
  2556. if (pci_channel_offline(np->s.device))
  2557. return;
  2558. }
  2559. } while (istatc & (SIP|DIP));
  2560. if (DEBUG_FLAGS & DEBUG_TINY)
  2561. printf ("<%d|%x:%x|%x:%x>",
  2562. (int)INB(np, nc_scr0),
  2563. dstat,sist,
  2564. (unsigned)INL(np, nc_dsp),
  2565. (unsigned)INL(np, nc_dbc));
  2566. /*
  2567. * On paper, a memory read barrier may be needed here to
  2568. * prevent out of order LOADs by the CPU from having
  2569. * prefetched stale data prior to DMA having occurred.
  2570. * And since we are paranoid ... :)
  2571. */
  2572. MEMORY_READ_BARRIER();
  2573. /*
  2574. * First, interrupts we want to service cleanly.
  2575. *
  2576. * Phase mismatch (MA) is the most frequent interrupt
  2577. * for chip earlier than the 896 and so we have to service
  2578. * it as quickly as possible.
  2579. * A SCSI parity error (PAR) may be combined with a phase
  2580. * mismatch condition (MA).
  2581. * Programmed interrupts (SIR) are used to call the C code
  2582. * from SCRIPTS.
  2583. * The single step interrupt (SSI) is not used in this
  2584. * driver.
  2585. */
  2586. if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
  2587. !(dstat & (MDPE|BF|ABRT|IID))) {
  2588. if (sist & PAR) sym_int_par (np, sist);
  2589. else if (sist & MA) sym_int_ma (np);
  2590. else if (dstat & SIR) sym_int_sir (np);
  2591. else if (dstat & SSI) OUTONB_STD();
  2592. else goto unknown_int;
  2593. return;
  2594. }
  2595. /*
  2596. * Now, interrupts that donnot happen in normal
  2597. * situations and that we may need to recover from.
  2598. *
  2599. * On SCSI RESET (RST), we reset everything.
  2600. * On SCSI BUS MODE CHANGE (SBMC), we complete all
  2601. * active CCBs with RESET status, prepare all devices
  2602. * for negotiating again and restart the SCRIPTS.
  2603. * On STO and UDC, we complete the CCB with the corres-
  2604. * ponding status and restart the SCRIPTS.
  2605. */
  2606. if (sist & RST) {
  2607. printf("%s: SCSI BUS reset detected.\n", sym_name(np));
  2608. sym_start_up (np, 1);
  2609. return;
  2610. }
  2611. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  2612. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  2613. if (!(sist & (GEN|HTH|SGE)) &&
  2614. !(dstat & (MDPE|BF|ABRT|IID))) {
  2615. if (sist & SBMC) sym_int_sbmc (np);
  2616. else if (sist & STO) sym_int_sto (np);
  2617. else if (sist & UDC) sym_int_udc (np);
  2618. else goto unknown_int;
  2619. return;
  2620. }
  2621. /*
  2622. * Now, interrupts we are not able to recover cleanly.
  2623. *
  2624. * Log message for hard errors.
  2625. * Reset everything.
  2626. */
  2627. sym_log_hard_error(np, sist, dstat);
  2628. if ((sist & (GEN|HTH|SGE)) ||
  2629. (dstat & (MDPE|BF|ABRT|IID))) {
  2630. sym_start_reset(np);
  2631. return;
  2632. }
  2633. unknown_int:
  2634. /*
  2635. * We just miss the cause of the interrupt. :(
  2636. * Print a message. The timeout will do the real work.
  2637. */
  2638. printf( "%s: unknown interrupt(s) ignored, "
  2639. "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
  2640. sym_name(np), istat, dstat, sist);
  2641. }
  2642. /*
  2643. * Dequeue from the START queue all CCBs that match
  2644. * a given target/lun/task condition (-1 means all),
  2645. * and move them from the BUSY queue to the COMP queue
  2646. * with DID_SOFT_ERROR status condition.
  2647. * This function is used during error handling/recovery.
  2648. * It is called with SCRIPTS not running.
  2649. */
  2650. static int
  2651. sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
  2652. {
  2653. int j;
  2654. struct sym_ccb *cp;
  2655. /*
  2656. * Make sure the starting index is within range.
  2657. */
  2658. assert((i >= 0) && (i < 2*MAX_QUEUE));
  2659. /*
  2660. * Walk until end of START queue and dequeue every job
  2661. * that matches the target/lun/task condition.
  2662. */
  2663. j = i;
  2664. while (i != np->squeueput) {
  2665. cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
  2666. assert(cp);
  2667. #ifdef SYM_CONF_IARB_SUPPORT
  2668. /* Forget hints for IARB, they may be no longer relevant */
  2669. cp->host_flags &= ~HF_HINT_IARB;
  2670. #endif
  2671. if ((target == -1 || cp->target == target) &&
  2672. (lun == -1 || cp->lun == lun) &&
  2673. (task == -1 || cp->tag == task)) {
  2674. sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
  2675. sym_remque(&cp->link_ccbq);
  2676. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2677. }
  2678. else {
  2679. if (i != j)
  2680. np->squeue[j] = np->squeue[i];
  2681. if ((j += 2) >= MAX_QUEUE*2) j = 0;
  2682. }
  2683. if ((i += 2) >= MAX_QUEUE*2) i = 0;
  2684. }
  2685. if (i != j) /* Copy back the idle task if needed */
  2686. np->squeue[j] = np->squeue[i];
  2687. np->squeueput = j; /* Update our current start queue pointer */
  2688. return (i - j) / 2;
  2689. }
  2690. /*
  2691. * chip handler for bad SCSI status condition
  2692. *
  2693. * In case of bad SCSI status, we unqueue all the tasks
  2694. * currently queued to the controller but not yet started
  2695. * and then restart the SCRIPTS processor immediately.
  2696. *
  2697. * QUEUE FULL and BUSY conditions are handled the same way.
  2698. * Basically all the not yet started tasks are requeued in
  2699. * device queue and the queue is frozen until a completion.
  2700. *
  2701. * For CHECK CONDITION and COMMAND TERMINATED status, we use
  2702. * the CCB of the failed command to prepare a REQUEST SENSE
  2703. * SCSI command and queue it to the controller queue.
  2704. *
  2705. * SCRATCHA is assumed to have been loaded with STARTPOS
  2706. * before the SCRIPTS called the C code.
  2707. */
  2708. static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
  2709. {
  2710. u32 startp;
  2711. u_char s_status = cp->ssss_status;
  2712. u_char h_flags = cp->host_flags;
  2713. int msglen;
  2714. int i;
  2715. /*
  2716. * Compute the index of the next job to start from SCRIPTS.
  2717. */
  2718. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  2719. /*
  2720. * The last CCB queued used for IARB hint may be
  2721. * no longer relevant. Forget it.
  2722. */
  2723. #ifdef SYM_CONF_IARB_SUPPORT
  2724. if (np->last_cp)
  2725. np->last_cp = 0;
  2726. #endif
  2727. /*
  2728. * Now deal with the SCSI status.
  2729. */
  2730. switch(s_status) {
  2731. case S_BUSY:
  2732. case S_QUEUE_FULL:
  2733. if (sym_verbose >= 2) {
  2734. sym_print_addr(cp->cmd, "%s\n",
  2735. s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
  2736. }
  2737. default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
  2738. sym_complete_error (np, cp);
  2739. break;
  2740. case S_TERMINATED:
  2741. case S_CHECK_COND:
  2742. /*
  2743. * If we get an SCSI error when requesting sense, give up.
  2744. */
  2745. if (h_flags & HF_SENSE) {
  2746. sym_complete_error (np, cp);
  2747. break;
  2748. }
  2749. /*
  2750. * Dequeue all queued CCBs for that device not yet started,
  2751. * and restart the SCRIPTS processor immediately.
  2752. */
  2753. sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  2754. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2755. /*
  2756. * Save some info of the actual IO.
  2757. * Compute the data residual.
  2758. */
  2759. cp->sv_scsi_status = cp->ssss_status;
  2760. cp->sv_xerr_status = cp->xerr_status;
  2761. cp->sv_resid = sym_compute_residual(np, cp);
  2762. /*
  2763. * Prepare all needed data structures for
  2764. * requesting sense data.
  2765. */
  2766. cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
  2767. msglen = 1;
  2768. /*
  2769. * If we are currently using anything different from
  2770. * async. 8 bit data transfers with that target,
  2771. * start a negotiation, since the device may want
  2772. * to report us a UNIT ATTENTION condition due to
  2773. * a cause we currently ignore, and we donnot want
  2774. * to be stuck with WIDE and/or SYNC data transfer.
  2775. *
  2776. * cp->nego_status is filled by sym_prepare_nego().
  2777. */
  2778. cp->nego_status = 0;
  2779. msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
  2780. /*
  2781. * Message table indirect structure.
  2782. */
  2783. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
  2784. cp->phys.smsg.size = cpu_to_scr(msglen);
  2785. /*
  2786. * sense command
  2787. */
  2788. cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
  2789. cp->phys.cmd.size = cpu_to_scr(6);
  2790. /*
  2791. * patch requested size into sense command
  2792. */
  2793. cp->sensecmd[0] = REQUEST_SENSE;
  2794. cp->sensecmd[1] = 0;
  2795. if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
  2796. cp->sensecmd[1] = cp->lun << 5;
  2797. cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
  2798. cp->data_len = SYM_SNS_BBUF_LEN;
  2799. /*
  2800. * sense data
  2801. */
  2802. memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
  2803. cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
  2804. cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
  2805. /*
  2806. * requeue the command.
  2807. */
  2808. startp = SCRIPTB_BA(np, sdata_in);
  2809. cp->phys.head.savep = cpu_to_scr(startp);
  2810. cp->phys.head.lastp = cpu_to_scr(startp);
  2811. cp->startp = cpu_to_scr(startp);
  2812. cp->goalp = cpu_to_scr(startp + 16);
  2813. cp->host_xflags = 0;
  2814. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  2815. cp->ssss_status = S_ILLEGAL;
  2816. cp->host_flags = (HF_SENSE|HF_DATA_IN);
  2817. cp->xerr_status = 0;
  2818. cp->extra_bytes = 0;
  2819. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  2820. /*
  2821. * Requeue the command.
  2822. */
  2823. sym_put_start_queue(np, cp);
  2824. /*
  2825. * Give back to upper layer everything we have dequeued.
  2826. */
  2827. sym_flush_comp_queue(np, 0);
  2828. break;
  2829. }
  2830. }
  2831. /*
  2832. * After a device has accepted some management message
  2833. * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
  2834. * a device signals a UNIT ATTENTION condition, some
  2835. * tasks are thrown away by the device. We are required
  2836. * to reflect that on our tasks list since the device
  2837. * will never complete these tasks.
  2838. *
  2839. * This function move from the BUSY queue to the COMP
  2840. * queue all disconnected CCBs for a given target that
  2841. * match the following criteria:
  2842. * - lun=-1 means any logical UNIT otherwise a given one.
  2843. * - task=-1 means any task, otherwise a given one.
  2844. */
  2845. int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
  2846. {
  2847. SYM_QUEHEAD qtmp, *qp;
  2848. int i = 0;
  2849. struct sym_ccb *cp;
  2850. /*
  2851. * Move the entire BUSY queue to our temporary queue.
  2852. */
  2853. sym_que_init(&qtmp);
  2854. sym_que_splice(&np->busy_ccbq, &qtmp);
  2855. sym_que_init(&np->busy_ccbq);
  2856. /*
  2857. * Put all CCBs that matches our criteria into
  2858. * the COMP queue and put back other ones into
  2859. * the BUSY queue.
  2860. */
  2861. while ((qp = sym_remque_head(&qtmp)) != 0) {
  2862. struct scsi_cmnd *cmd;
  2863. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2864. cmd = cp->cmd;
  2865. if (cp->host_status != HS_DISCONNECT ||
  2866. cp->target != target ||
  2867. (lun != -1 && cp->lun != lun) ||
  2868. (task != -1 &&
  2869. (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
  2870. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  2871. continue;
  2872. }
  2873. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2874. /* Preserve the software timeout condition */
  2875. if (sym_get_cam_status(cmd) != DID_TIME_OUT)
  2876. sym_set_cam_status(cmd, cam_status);
  2877. ++i;
  2878. #if 0
  2879. printf("XXXX TASK @%p CLEARED\n", cp);
  2880. #endif
  2881. }
  2882. return i;
  2883. }
  2884. /*
  2885. * chip handler for TASKS recovery
  2886. *
  2887. * We cannot safely abort a command, while the SCRIPTS
  2888. * processor is running, since we just would be in race
  2889. * with it.
  2890. *
  2891. * As long as we have tasks to abort, we keep the SEM
  2892. * bit set in the ISTAT. When this bit is set, the
  2893. * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
  2894. * each time it enters the scheduler.
  2895. *
  2896. * If we have to reset a target, clear tasks of a unit,
  2897. * or to perform the abort of a disconnected job, we
  2898. * restart the SCRIPTS for selecting the target. Once
  2899. * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
  2900. * If it loses arbitration, the SCRIPTS will interrupt again
  2901. * the next time it will enter its scheduler, and so on ...
  2902. *
  2903. * On SIR_TARGET_SELECTED, we scan for the more
  2904. * appropriate thing to do:
  2905. *
  2906. * - If nothing, we just sent a M_ABORT message to the
  2907. * target to get rid of the useless SCSI bus ownership.
  2908. * According to the specs, no tasks shall be affected.
  2909. * - If the target is to be reset, we send it a M_RESET
  2910. * message.
  2911. * - If a logical UNIT is to be cleared , we send the
  2912. * IDENTIFY(lun) + M_ABORT.
  2913. * - If an untagged task is to be aborted, we send the
  2914. * IDENTIFY(lun) + M_ABORT.
  2915. * - If a tagged task is to be aborted, we send the
  2916. * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
  2917. *
  2918. * Once our 'kiss of death' :) message has been accepted
  2919. * by the target, the SCRIPTS interrupts again
  2920. * (SIR_ABORT_SENT). On this interrupt, we complete
  2921. * all the CCBs that should have been aborted by the
  2922. * target according to our message.
  2923. */
  2924. static void sym_sir_task_recovery(struct sym_hcb *np, int num)
  2925. {
  2926. SYM_QUEHEAD *qp;
  2927. struct sym_ccb *cp;
  2928. struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
  2929. struct scsi_target *starget;
  2930. int target=-1, lun=-1, task;
  2931. int i, k;
  2932. switch(num) {
  2933. /*
  2934. * The SCRIPTS processor stopped before starting
  2935. * the next command in order to allow us to perform
  2936. * some task recovery.
  2937. */
  2938. case SIR_SCRIPT_STOPPED:
  2939. /*
  2940. * Do we have any target to reset or unit to clear ?
  2941. */
  2942. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  2943. tp = &np->target[i];
  2944. if (tp->to_reset ||
  2945. (tp->lun0p && tp->lun0p->to_clear)) {
  2946. target = i;
  2947. break;
  2948. }
  2949. if (!tp->lunmp)
  2950. continue;
  2951. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  2952. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  2953. target = i;
  2954. break;
  2955. }
  2956. }
  2957. if (target != -1)
  2958. break;
  2959. }
  2960. /*
  2961. * If not, walk the busy queue for any
  2962. * disconnected CCB to be aborted.
  2963. */
  2964. if (target == -1) {
  2965. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  2966. cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
  2967. if (cp->host_status != HS_DISCONNECT)
  2968. continue;
  2969. if (cp->to_abort) {
  2970. target = cp->target;
  2971. break;
  2972. }
  2973. }
  2974. }
  2975. /*
  2976. * If some target is to be selected,
  2977. * prepare and start the selection.
  2978. */
  2979. if (target != -1) {
  2980. tp = &np->target[target];
  2981. np->abrt_sel.sel_id = target;
  2982. np->abrt_sel.sel_scntl3 = tp->head.wval;
  2983. np->abrt_sel.sel_sxfer = tp->head.sval;
  2984. OUTL(np, nc_dsa, np->hcb_ba);
  2985. OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
  2986. return;
  2987. }
  2988. /*
  2989. * Now look for a CCB to abort that haven't started yet.
  2990. * Btw, the SCRIPTS processor is still stopped, so
  2991. * we are not in race.
  2992. */
  2993. i = 0;
  2994. cp = NULL;
  2995. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  2996. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2997. if (cp->host_status != HS_BUSY &&
  2998. cp->host_status != HS_NEGOTIATE)
  2999. continue;
  3000. if (!cp->to_abort)
  3001. continue;
  3002. #ifdef SYM_CONF_IARB_SUPPORT
  3003. /*
  3004. * If we are using IMMEDIATE ARBITRATION, we donnot
  3005. * want to cancel the last queued CCB, since the
  3006. * SCRIPTS may have anticipated the selection.
  3007. */
  3008. if (cp == np->last_cp) {
  3009. cp->to_abort = 0;
  3010. continue;
  3011. }
  3012. #endif
  3013. i = 1; /* Means we have found some */
  3014. break;
  3015. }
  3016. if (!i) {
  3017. /*
  3018. * We are done, so we donnot need
  3019. * to synchronize with the SCRIPTS anylonger.
  3020. * Remove the SEM flag from the ISTAT.
  3021. */
  3022. np->istat_sem = 0;
  3023. OUTB(np, nc_istat, SIGP);
  3024. break;
  3025. }
  3026. /*
  3027. * Compute index of next position in the start
  3028. * queue the SCRIPTS intends to start and dequeue
  3029. * all CCBs for that device that haven't been started.
  3030. */
  3031. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3032. i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  3033. /*
  3034. * Make sure at least our IO to abort has been dequeued.
  3035. */
  3036. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  3037. assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
  3038. #else
  3039. sym_remque(&cp->link_ccbq);
  3040. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  3041. #endif
  3042. /*
  3043. * Keep track in cam status of the reason of the abort.
  3044. */
  3045. if (cp->to_abort == 2)
  3046. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3047. else
  3048. sym_set_cam_status(cp->cmd, DID_ABORT);
  3049. /*
  3050. * Complete with error everything that we have dequeued.
  3051. */
  3052. sym_flush_comp_queue(np, 0);
  3053. break;
  3054. /*
  3055. * The SCRIPTS processor has selected a target
  3056. * we may have some manual recovery to perform for.
  3057. */
  3058. case SIR_TARGET_SELECTED:
  3059. target = INB(np, nc_sdid) & 0xf;
  3060. tp = &np->target[target];
  3061. np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
  3062. /*
  3063. * If the target is to be reset, prepare a
  3064. * M_RESET message and clear the to_reset flag
  3065. * since we donnot expect this operation to fail.
  3066. */
  3067. if (tp->to_reset) {
  3068. np->abrt_msg[0] = M_RESET;
  3069. np->abrt_tbl.size = 1;
  3070. tp->to_reset = 0;
  3071. break;
  3072. }
  3073. /*
  3074. * Otherwise, look for some logical unit to be cleared.
  3075. */
  3076. if (tp->lun0p && tp->lun0p->to_clear)
  3077. lun = 0;
  3078. else if (tp->lunmp) {
  3079. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  3080. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  3081. lun = k;
  3082. break;
  3083. }
  3084. }
  3085. }
  3086. /*
  3087. * If a logical unit is to be cleared, prepare
  3088. * an IDENTIFY(lun) + ABORT MESSAGE.
  3089. */
  3090. if (lun != -1) {
  3091. struct sym_lcb *lp = sym_lp(tp, lun);
  3092. lp->to_clear = 0; /* We don't expect to fail here */
  3093. np->abrt_msg[0] = IDENTIFY(0, lun);
  3094. np->abrt_msg[1] = M_ABORT;
  3095. np->abrt_tbl.size = 2;
  3096. break;
  3097. }
  3098. /*
  3099. * Otherwise, look for some disconnected job to
  3100. * abort for this target.
  3101. */
  3102. i = 0;
  3103. cp = NULL;
  3104. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3105. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3106. if (cp->host_status != HS_DISCONNECT)
  3107. continue;
  3108. if (cp->target != target)
  3109. continue;
  3110. if (!cp->to_abort)
  3111. continue;
  3112. i = 1; /* Means we have some */
  3113. break;
  3114. }
  3115. /*
  3116. * If we have none, probably since the device has
  3117. * completed the command before we won abitration,
  3118. * send a M_ABORT message without IDENTIFY.
  3119. * According to the specs, the device must just
  3120. * disconnect the BUS and not abort any task.
  3121. */
  3122. if (!i) {
  3123. np->abrt_msg[0] = M_ABORT;
  3124. np->abrt_tbl.size = 1;
  3125. break;
  3126. }
  3127. /*
  3128. * We have some task to abort.
  3129. * Set the IDENTIFY(lun)
  3130. */
  3131. np->abrt_msg[0] = IDENTIFY(0, cp->lun);
  3132. /*
  3133. * If we want to abort an untagged command, we
  3134. * will send a IDENTIFY + M_ABORT.
  3135. * Otherwise (tagged command), we will send
  3136. * a IDENTITFY + task attributes + ABORT TAG.
  3137. */
  3138. if (cp->tag == NO_TAG) {
  3139. np->abrt_msg[1] = M_ABORT;
  3140. np->abrt_tbl.size = 2;
  3141. } else {
  3142. np->abrt_msg[1] = cp->scsi_smsg[1];
  3143. np->abrt_msg[2] = cp->scsi_smsg[2];
  3144. np->abrt_msg[3] = M_ABORT_TAG;
  3145. np->abrt_tbl.size = 4;
  3146. }
  3147. /*
  3148. * Keep track of software timeout condition, since the
  3149. * peripheral driver may not count retries on abort
  3150. * conditions not due to timeout.
  3151. */
  3152. if (cp->to_abort == 2)
  3153. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3154. cp->to_abort = 0; /* We donnot expect to fail here */
  3155. break;
  3156. /*
  3157. * The target has accepted our message and switched
  3158. * to BUS FREE phase as we expected.
  3159. */
  3160. case SIR_ABORT_SENT:
  3161. target = INB(np, nc_sdid) & 0xf;
  3162. tp = &np->target[target];
  3163. starget = tp->starget;
  3164. /*
  3165. ** If we didn't abort anything, leave here.
  3166. */
  3167. if (np->abrt_msg[0] == M_ABORT)
  3168. break;
  3169. /*
  3170. * If we sent a M_RESET, then a hardware reset has
  3171. * been performed by the target.
  3172. * - Reset everything to async 8 bit
  3173. * - Tell ourself to negotiate next time :-)
  3174. * - Prepare to clear all disconnected CCBs for
  3175. * this target from our task list (lun=task=-1)
  3176. */
  3177. lun = -1;
  3178. task = -1;
  3179. if (np->abrt_msg[0] == M_RESET) {
  3180. tp->head.sval = 0;
  3181. tp->head.wval = np->rv_scntl3;
  3182. tp->head.uval = 0;
  3183. spi_period(starget) = 0;
  3184. spi_offset(starget) = 0;
  3185. spi_width(starget) = 0;
  3186. spi_iu(starget) = 0;
  3187. spi_dt(starget) = 0;
  3188. spi_qas(starget) = 0;
  3189. tp->tgoal.check_nego = 1;
  3190. }
  3191. /*
  3192. * Otherwise, check for the LUN and TASK(s)
  3193. * concerned by the cancelation.
  3194. * If it is not ABORT_TAG then it is CLEAR_QUEUE
  3195. * or an ABORT message :-)
  3196. */
  3197. else {
  3198. lun = np->abrt_msg[0] & 0x3f;
  3199. if (np->abrt_msg[1] == M_ABORT_TAG)
  3200. task = np->abrt_msg[2];
  3201. }
  3202. /*
  3203. * Complete all the CCBs the device should have
  3204. * aborted due to our 'kiss of death' message.
  3205. */
  3206. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3207. sym_dequeue_from_squeue(np, i, target, lun, -1);
  3208. sym_clear_tasks(np, DID_ABORT, target, lun, task);
  3209. sym_flush_comp_queue(np, 0);
  3210. /*
  3211. * If we sent a BDR, make upper layer aware of that.
  3212. */
  3213. if (np->abrt_msg[0] == M_RESET)
  3214. sym_xpt_async_sent_bdr(np, target);
  3215. break;
  3216. }
  3217. /*
  3218. * Print to the log the message we intend to send.
  3219. */
  3220. if (num == SIR_TARGET_SELECTED) {
  3221. dev_info(&tp->starget->dev, "control msgout:");
  3222. sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
  3223. np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
  3224. }
  3225. /*
  3226. * Let the SCRIPTS processor continue.
  3227. */
  3228. OUTONB_STD();
  3229. }
  3230. /*
  3231. * Gerard's alchemy:) that deals with with the data
  3232. * pointer for both MDP and the residual calculation.
  3233. *
  3234. * I didn't want to bloat the code by more than 200
  3235. * lines for the handling of both MDP and the residual.
  3236. * This has been achieved by using a data pointer
  3237. * representation consisting in an index in the data
  3238. * array (dp_sg) and a negative offset (dp_ofs) that
  3239. * have the following meaning:
  3240. *
  3241. * - dp_sg = SYM_CONF_MAX_SG
  3242. * we are at the end of the data script.
  3243. * - dp_sg < SYM_CONF_MAX_SG
  3244. * dp_sg points to the next entry of the scatter array
  3245. * we want to transfer.
  3246. * - dp_ofs < 0
  3247. * dp_ofs represents the residual of bytes of the
  3248. * previous entry scatter entry we will send first.
  3249. * - dp_ofs = 0
  3250. * no residual to send first.
  3251. *
  3252. * The function sym_evaluate_dp() accepts an arbitray
  3253. * offset (basically from the MDP message) and returns
  3254. * the corresponding values of dp_sg and dp_ofs.
  3255. */
  3256. static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
  3257. {
  3258. u32 dp_scr;
  3259. int dp_ofs, dp_sg, dp_sgmin;
  3260. int tmp;
  3261. struct sym_pmc *pm;
  3262. /*
  3263. * Compute the resulted data pointer in term of a script
  3264. * address within some DATA script and a signed byte offset.
  3265. */
  3266. dp_scr = scr;
  3267. dp_ofs = *ofs;
  3268. if (dp_scr == SCRIPTA_BA(np, pm0_data))
  3269. pm = &cp->phys.pm0;
  3270. else if (dp_scr == SCRIPTA_BA(np, pm1_data))
  3271. pm = &cp->phys.pm1;
  3272. else
  3273. pm = NULL;
  3274. if (pm) {
  3275. dp_scr = scr_to_cpu(pm->ret);
  3276. dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
  3277. }
  3278. /*
  3279. * If we are auto-sensing, then we are done.
  3280. */
  3281. if (cp->host_flags & HF_SENSE) {
  3282. *ofs = dp_ofs;
  3283. return 0;
  3284. }
  3285. /*
  3286. * Deduce the index of the sg entry.
  3287. * Keep track of the index of the first valid entry.
  3288. * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
  3289. * end of the data.
  3290. */
  3291. tmp = scr_to_cpu(cp->goalp);
  3292. dp_sg = SYM_CONF_MAX_SG;
  3293. if (dp_scr != tmp)
  3294. dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
  3295. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3296. /*
  3297. * Move to the sg entry the data pointer belongs to.
  3298. *
  3299. * If we are inside the data area, we expect result to be:
  3300. *
  3301. * Either,
  3302. * dp_ofs = 0 and dp_sg is the index of the sg entry
  3303. * the data pointer belongs to (or the end of the data)
  3304. * Or,
  3305. * dp_ofs < 0 and dp_sg is the index of the sg entry
  3306. * the data pointer belongs to + 1.
  3307. */
  3308. if (dp_ofs < 0) {
  3309. int n;
  3310. while (dp_sg > dp_sgmin) {
  3311. --dp_sg;
  3312. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3313. n = dp_ofs + (tmp & 0xffffff);
  3314. if (n > 0) {
  3315. ++dp_sg;
  3316. break;
  3317. }
  3318. dp_ofs = n;
  3319. }
  3320. }
  3321. else if (dp_ofs > 0) {
  3322. while (dp_sg < SYM_CONF_MAX_SG) {
  3323. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3324. dp_ofs -= (tmp & 0xffffff);
  3325. ++dp_sg;
  3326. if (dp_ofs <= 0)
  3327. break;
  3328. }
  3329. }
  3330. /*
  3331. * Make sure the data pointer is inside the data area.
  3332. * If not, return some error.
  3333. */
  3334. if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
  3335. goto out_err;
  3336. else if (dp_sg > SYM_CONF_MAX_SG ||
  3337. (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
  3338. goto out_err;
  3339. /*
  3340. * Save the extreme pointer if needed.
  3341. */
  3342. if (dp_sg > cp->ext_sg ||
  3343. (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
  3344. cp->ext_sg = dp_sg;
  3345. cp->ext_ofs = dp_ofs;
  3346. }
  3347. /*
  3348. * Return data.
  3349. */
  3350. *ofs = dp_ofs;
  3351. return dp_sg;
  3352. out_err:
  3353. return -1;
  3354. }
  3355. /*
  3356. * chip handler for MODIFY DATA POINTER MESSAGE
  3357. *
  3358. * We also call this function on IGNORE WIDE RESIDUE
  3359. * messages that do not match a SWIDE full condition.
  3360. * Btw, we assume in that situation that such a message
  3361. * is equivalent to a MODIFY DATA POINTER (offset=-1).
  3362. */
  3363. static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
  3364. {
  3365. int dp_ofs = ofs;
  3366. u32 dp_scr = sym_get_script_dp (np, cp);
  3367. u32 dp_ret;
  3368. u32 tmp;
  3369. u_char hflags;
  3370. int dp_sg;
  3371. struct sym_pmc *pm;
  3372. /*
  3373. * Not supported for auto-sense.
  3374. */
  3375. if (cp->host_flags & HF_SENSE)
  3376. goto out_reject;
  3377. /*
  3378. * Apply our alchemy:) (see comments in sym_evaluate_dp()),
  3379. * to the resulted data pointer.
  3380. */
  3381. dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
  3382. if (dp_sg < 0)
  3383. goto out_reject;
  3384. /*
  3385. * And our alchemy:) allows to easily calculate the data
  3386. * script address we want to return for the next data phase.
  3387. */
  3388. dp_ret = cpu_to_scr(cp->goalp);
  3389. dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
  3390. /*
  3391. * If offset / scatter entry is zero we donnot need
  3392. * a context for the new current data pointer.
  3393. */
  3394. if (dp_ofs == 0) {
  3395. dp_scr = dp_ret;
  3396. goto out_ok;
  3397. }
  3398. /*
  3399. * Get a context for the new current data pointer.
  3400. */
  3401. hflags = INB(np, HF_PRT);
  3402. if (hflags & HF_DP_SAVED)
  3403. hflags ^= HF_ACT_PM;
  3404. if (!(hflags & HF_ACT_PM)) {
  3405. pm = &cp->phys.pm0;
  3406. dp_scr = SCRIPTA_BA(np, pm0_data);
  3407. }
  3408. else {
  3409. pm = &cp->phys.pm1;
  3410. dp_scr = SCRIPTA_BA(np, pm1_data);
  3411. }
  3412. hflags &= ~(HF_DP_SAVED);
  3413. OUTB(np, HF_PRT, hflags);
  3414. /*
  3415. * Set up the new current data pointer.
  3416. * ofs < 0 there, and for the next data phase, we
  3417. * want to transfer part of the data of the sg entry
  3418. * corresponding to index dp_sg-1 prior to returning
  3419. * to the main data script.
  3420. */
  3421. pm->ret = cpu_to_scr(dp_ret);
  3422. tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
  3423. tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
  3424. pm->sg.addr = cpu_to_scr(tmp);
  3425. pm->sg.size = cpu_to_scr(-dp_ofs);
  3426. out_ok:
  3427. sym_set_script_dp (np, cp, dp_scr);
  3428. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3429. return;
  3430. out_reject:
  3431. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3432. }
  3433. /*
  3434. * chip calculation of the data residual.
  3435. *
  3436. * As I used to say, the requirement of data residual
  3437. * in SCSI is broken, useless and cannot be achieved
  3438. * without huge complexity.
  3439. * But most OSes and even the official CAM require it.
  3440. * When stupidity happens to be so widely spread inside
  3441. * a community, it gets hard to convince.
  3442. *
  3443. * Anyway, I don't care, since I am not going to use
  3444. * any software that considers this data residual as
  3445. * a relevant information. :)
  3446. */
  3447. int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
  3448. {
  3449. int dp_sg, dp_sgmin, resid = 0;
  3450. int dp_ofs = 0;
  3451. /*
  3452. * Check for some data lost or just thrown away.
  3453. * We are not required to be quite accurate in this
  3454. * situation. Btw, if we are odd for output and the
  3455. * device claims some more data, it may well happen
  3456. * than our residual be zero. :-)
  3457. */
  3458. if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
  3459. if (cp->xerr_status & XE_EXTRA_DATA)
  3460. resid -= cp->extra_bytes;
  3461. if (cp->xerr_status & XE_SODL_UNRUN)
  3462. ++resid;
  3463. if (cp->xerr_status & XE_SWIDE_OVRUN)
  3464. --resid;
  3465. }
  3466. /*
  3467. * If all data has been transferred,
  3468. * there is no residual.
  3469. */
  3470. if (cp->phys.head.lastp == cp->goalp)
  3471. return resid;
  3472. /*
  3473. * If no data transfer occurs, or if the data
  3474. * pointer is weird, return full residual.
  3475. */
  3476. if (cp->startp == cp->phys.head.lastp ||
  3477. sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
  3478. &dp_ofs) < 0) {
  3479. return cp->data_len;
  3480. }
  3481. /*
  3482. * If we were auto-sensing, then we are done.
  3483. */
  3484. if (cp->host_flags & HF_SENSE) {
  3485. return -dp_ofs;
  3486. }
  3487. /*
  3488. * We are now full comfortable in the computation
  3489. * of the data residual (2's complement).
  3490. */
  3491. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3492. resid = -cp->ext_ofs;
  3493. for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
  3494. u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3495. resid += (tmp & 0xffffff);
  3496. }
  3497. resid -= cp->odd_byte_adjustment;
  3498. /*
  3499. * Hopefully, the result is not too wrong.
  3500. */
  3501. return resid;
  3502. }
  3503. /*
  3504. * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
  3505. *
  3506. * When we try to negotiate, we append the negotiation message
  3507. * to the identify and (maybe) simple tag message.
  3508. * The host status field is set to HS_NEGOTIATE to mark this
  3509. * situation.
  3510. *
  3511. * If the target doesn't answer this message immediately
  3512. * (as required by the standard), the SIR_NEGO_FAILED interrupt
  3513. * will be raised eventually.
  3514. * The handler removes the HS_NEGOTIATE status, and sets the
  3515. * negotiated value to the default (async / nowide).
  3516. *
  3517. * If we receive a matching answer immediately, we check it
  3518. * for validity, and set the values.
  3519. *
  3520. * If we receive a Reject message immediately, we assume the
  3521. * negotiation has failed, and fall back to standard values.
  3522. *
  3523. * If we receive a negotiation message while not in HS_NEGOTIATE
  3524. * state, it's a target initiated negotiation. We prepare a
  3525. * (hopefully) valid answer, set our parameters, and send back
  3526. * this answer to the target.
  3527. *
  3528. * If the target doesn't fetch the answer (no message out phase),
  3529. * we assume the negotiation has failed, and fall back to default
  3530. * settings (SIR_NEGO_PROTO interrupt).
  3531. *
  3532. * When we set the values, we adjust them in all ccbs belonging
  3533. * to this target, in the controller's register, and in the "phys"
  3534. * field of the controller's struct sym_hcb.
  3535. */
  3536. /*
  3537. * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
  3538. */
  3539. static int
  3540. sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3541. {
  3542. int target = cp->target;
  3543. u_char chg, ofs, per, fak, div;
  3544. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3545. sym_print_nego_msg(np, target, "sync msgin", np->msgin);
  3546. }
  3547. /*
  3548. * Get requested values.
  3549. */
  3550. chg = 0;
  3551. per = np->msgin[3];
  3552. ofs = np->msgin[4];
  3553. /*
  3554. * Check values against our limits.
  3555. */
  3556. if (ofs) {
  3557. if (ofs > np->maxoffs)
  3558. {chg = 1; ofs = np->maxoffs;}
  3559. }
  3560. if (ofs) {
  3561. if (per < np->minsync)
  3562. {chg = 1; per = np->minsync;}
  3563. }
  3564. /*
  3565. * Get new chip synchronous parameters value.
  3566. */
  3567. div = fak = 0;
  3568. if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
  3569. goto reject_it;
  3570. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3571. sym_print_addr(cp->cmd,
  3572. "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
  3573. ofs, per, div, fak, chg);
  3574. }
  3575. /*
  3576. * If it was an answer we want to change,
  3577. * then it isn't acceptable. Reject it.
  3578. */
  3579. if (!req && chg)
  3580. goto reject_it;
  3581. /*
  3582. * Apply new values.
  3583. */
  3584. sym_setsync (np, target, ofs, per, div, fak);
  3585. /*
  3586. * It was an answer. We are done.
  3587. */
  3588. if (!req)
  3589. return 0;
  3590. /*
  3591. * It was a request. Prepare an answer message.
  3592. */
  3593. spi_populate_sync_msg(np->msgout, per, ofs);
  3594. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3595. sym_print_nego_msg(np, target, "sync msgout", np->msgout);
  3596. }
  3597. np->msgin [0] = M_NOOP;
  3598. return 0;
  3599. reject_it:
  3600. sym_setsync (np, target, 0, 0, 0, 0);
  3601. return -1;
  3602. }
  3603. static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3604. {
  3605. int req = 1;
  3606. int result;
  3607. /*
  3608. * Request or answer ?
  3609. */
  3610. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3611. OUTB(np, HS_PRT, HS_BUSY);
  3612. if (cp->nego_status && cp->nego_status != NS_SYNC)
  3613. goto reject_it;
  3614. req = 0;
  3615. }
  3616. /*
  3617. * Check and apply new values.
  3618. */
  3619. result = sym_sync_nego_check(np, req, cp);
  3620. if (result) /* Not acceptable, reject it */
  3621. goto reject_it;
  3622. if (req) { /* Was a request, send response. */
  3623. cp->nego_status = NS_SYNC;
  3624. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3625. }
  3626. else /* Was a response, we are done. */
  3627. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3628. return;
  3629. reject_it:
  3630. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3631. }
  3632. /*
  3633. * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
  3634. */
  3635. static int
  3636. sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
  3637. {
  3638. struct sym_tcb *tp = &np->target[target];
  3639. unsigned char fak, div;
  3640. int dt, chg = 0;
  3641. unsigned char per = np->msgin[3];
  3642. unsigned char ofs = np->msgin[5];
  3643. unsigned char wide = np->msgin[6];
  3644. unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
  3645. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3646. sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
  3647. }
  3648. /*
  3649. * Check values against our limits.
  3650. */
  3651. if (wide > np->maxwide) {
  3652. chg = 1;
  3653. wide = np->maxwide;
  3654. }
  3655. if (!wide || !(np->features & FE_U3EN))
  3656. opts = 0;
  3657. if (opts != (np->msgin[7] & PPR_OPT_MASK))
  3658. chg = 1;
  3659. dt = opts & PPR_OPT_DT;
  3660. if (ofs) {
  3661. unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
  3662. if (ofs > maxoffs) {
  3663. chg = 1;
  3664. ofs = maxoffs;
  3665. }
  3666. }
  3667. if (ofs) {
  3668. unsigned char minsync = dt ? np->minsync_dt : np->minsync;
  3669. if (per < minsync) {
  3670. chg = 1;
  3671. per = minsync;
  3672. }
  3673. }
  3674. /*
  3675. * Get new chip synchronous parameters value.
  3676. */
  3677. div = fak = 0;
  3678. if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
  3679. goto reject_it;
  3680. /*
  3681. * If it was an answer we want to change,
  3682. * then it isn't acceptable. Reject it.
  3683. */
  3684. if (!req && chg)
  3685. goto reject_it;
  3686. /*
  3687. * Apply new values.
  3688. */
  3689. sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
  3690. /*
  3691. * It was an answer. We are done.
  3692. */
  3693. if (!req)
  3694. return 0;
  3695. /*
  3696. * It was a request. Prepare an answer message.
  3697. */
  3698. spi_populate_ppr_msg(np->msgout, per, ofs, wide, opts);
  3699. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3700. sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
  3701. }
  3702. np->msgin [0] = M_NOOP;
  3703. return 0;
  3704. reject_it:
  3705. sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
  3706. /*
  3707. * If it is a device response that should result in
  3708. * ST, we may want to try a legacy negotiation later.
  3709. */
  3710. if (!req && !opts) {
  3711. tp->tgoal.period = per;
  3712. tp->tgoal.offset = ofs;
  3713. tp->tgoal.width = wide;
  3714. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3715. tp->tgoal.check_nego = 1;
  3716. }
  3717. return -1;
  3718. }
  3719. static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3720. {
  3721. int req = 1;
  3722. int result;
  3723. /*
  3724. * Request or answer ?
  3725. */
  3726. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3727. OUTB(np, HS_PRT, HS_BUSY);
  3728. if (cp->nego_status && cp->nego_status != NS_PPR)
  3729. goto reject_it;
  3730. req = 0;
  3731. }
  3732. /*
  3733. * Check and apply new values.
  3734. */
  3735. result = sym_ppr_nego_check(np, req, cp->target);
  3736. if (result) /* Not acceptable, reject it */
  3737. goto reject_it;
  3738. if (req) { /* Was a request, send response. */
  3739. cp->nego_status = NS_PPR;
  3740. OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
  3741. }
  3742. else /* Was a response, we are done. */
  3743. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3744. return;
  3745. reject_it:
  3746. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3747. }
  3748. /*
  3749. * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
  3750. */
  3751. static int
  3752. sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3753. {
  3754. int target = cp->target;
  3755. u_char chg, wide;
  3756. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3757. sym_print_nego_msg(np, target, "wide msgin", np->msgin);
  3758. }
  3759. /*
  3760. * Get requested values.
  3761. */
  3762. chg = 0;
  3763. wide = np->msgin[3];
  3764. /*
  3765. * Check values against our limits.
  3766. */
  3767. if (wide > np->maxwide) {
  3768. chg = 1;
  3769. wide = np->maxwide;
  3770. }
  3771. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3772. sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
  3773. wide, chg);
  3774. }
  3775. /*
  3776. * If it was an answer we want to change,
  3777. * then it isn't acceptable. Reject it.
  3778. */
  3779. if (!req && chg)
  3780. goto reject_it;
  3781. /*
  3782. * Apply new values.
  3783. */
  3784. sym_setwide (np, target, wide);
  3785. /*
  3786. * It was an answer. We are done.
  3787. */
  3788. if (!req)
  3789. return 0;
  3790. /*
  3791. * It was a request. Prepare an answer message.
  3792. */
  3793. spi_populate_width_msg(np->msgout, wide);
  3794. np->msgin [0] = M_NOOP;
  3795. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3796. sym_print_nego_msg(np, target, "wide msgout", np->msgout);
  3797. }
  3798. return 0;
  3799. reject_it:
  3800. return -1;
  3801. }
  3802. static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3803. {
  3804. int req = 1;
  3805. int result;
  3806. /*
  3807. * Request or answer ?
  3808. */
  3809. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3810. OUTB(np, HS_PRT, HS_BUSY);
  3811. if (cp->nego_status && cp->nego_status != NS_WIDE)
  3812. goto reject_it;
  3813. req = 0;
  3814. }
  3815. /*
  3816. * Check and apply new values.
  3817. */
  3818. result = sym_wide_nego_check(np, req, cp);
  3819. if (result) /* Not acceptable, reject it */
  3820. goto reject_it;
  3821. if (req) { /* Was a request, send response. */
  3822. cp->nego_status = NS_WIDE;
  3823. OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
  3824. } else { /* Was a response. */
  3825. /*
  3826. * Negotiate for SYNC immediately after WIDE response.
  3827. * This allows to negotiate for both WIDE and SYNC on
  3828. * a single SCSI command (Suggested by Justin Gibbs).
  3829. */
  3830. if (tp->tgoal.offset) {
  3831. spi_populate_sync_msg(np->msgout, tp->tgoal.period,
  3832. tp->tgoal.offset);
  3833. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3834. sym_print_nego_msg(np, cp->target,
  3835. "sync msgout", np->msgout);
  3836. }
  3837. cp->nego_status = NS_SYNC;
  3838. OUTB(np, HS_PRT, HS_NEGOTIATE);
  3839. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3840. return;
  3841. } else
  3842. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3843. }
  3844. return;
  3845. reject_it:
  3846. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3847. }
  3848. /*
  3849. * Reset DT, SYNC or WIDE to default settings.
  3850. *
  3851. * Called when a negotiation does not succeed either
  3852. * on rejection or on protocol error.
  3853. *
  3854. * A target that understands a PPR message should never
  3855. * reject it, and messing with it is very unlikely.
  3856. * So, if a PPR makes problems, we may just want to
  3857. * try a legacy negotiation later.
  3858. */
  3859. static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3860. {
  3861. switch (cp->nego_status) {
  3862. case NS_PPR:
  3863. #if 0
  3864. sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
  3865. #else
  3866. if (tp->tgoal.period < np->minsync)
  3867. tp->tgoal.period = np->minsync;
  3868. if (tp->tgoal.offset > np->maxoffs)
  3869. tp->tgoal.offset = np->maxoffs;
  3870. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3871. tp->tgoal.check_nego = 1;
  3872. #endif
  3873. break;
  3874. case NS_SYNC:
  3875. sym_setsync (np, cp->target, 0, 0, 0, 0);
  3876. break;
  3877. case NS_WIDE:
  3878. sym_setwide (np, cp->target, 0);
  3879. break;
  3880. }
  3881. np->msgin [0] = M_NOOP;
  3882. np->msgout[0] = M_NOOP;
  3883. cp->nego_status = 0;
  3884. }
  3885. /*
  3886. * chip handler for MESSAGE REJECT received in response to
  3887. * PPR, WIDE or SYNCHRONOUS negotiation.
  3888. */
  3889. static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3890. {
  3891. sym_nego_default(np, tp, cp);
  3892. OUTB(np, HS_PRT, HS_BUSY);
  3893. }
  3894. /*
  3895. * chip exception handler for programmed interrupts.
  3896. */
  3897. static void sym_int_sir (struct sym_hcb *np)
  3898. {
  3899. u_char num = INB(np, nc_dsps);
  3900. u32 dsa = INL(np, nc_dsa);
  3901. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  3902. u_char target = INB(np, nc_sdid) & 0x0f;
  3903. struct sym_tcb *tp = &np->target[target];
  3904. int tmp;
  3905. if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
  3906. switch (num) {
  3907. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  3908. /*
  3909. * SCRIPTS tell us that we may have to update
  3910. * 64 bit DMA segment registers.
  3911. */
  3912. case SIR_DMAP_DIRTY:
  3913. sym_update_dmap_regs(np);
  3914. goto out;
  3915. #endif
  3916. /*
  3917. * Command has been completed with error condition
  3918. * or has been auto-sensed.
  3919. */
  3920. case SIR_COMPLETE_ERROR:
  3921. sym_complete_error(np, cp);
  3922. return;
  3923. /*
  3924. * The C code is currently trying to recover from something.
  3925. * Typically, user want to abort some command.
  3926. */
  3927. case SIR_SCRIPT_STOPPED:
  3928. case SIR_TARGET_SELECTED:
  3929. case SIR_ABORT_SENT:
  3930. sym_sir_task_recovery(np, num);
  3931. return;
  3932. /*
  3933. * The device didn't go to MSG OUT phase after having
  3934. * been selected with ATN. We donnot want to handle
  3935. * that.
  3936. */
  3937. case SIR_SEL_ATN_NO_MSG_OUT:
  3938. printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
  3939. sym_name (np), target);
  3940. goto out_stuck;
  3941. /*
  3942. * The device didn't switch to MSG IN phase after
  3943. * having reseleted the initiator.
  3944. */
  3945. case SIR_RESEL_NO_MSG_IN:
  3946. printf ("%s:%d: No MSG IN phase after reselection.\n",
  3947. sym_name (np), target);
  3948. goto out_stuck;
  3949. /*
  3950. * After reselection, the device sent a message that wasn't
  3951. * an IDENTIFY.
  3952. */
  3953. case SIR_RESEL_NO_IDENTIFY:
  3954. printf ("%s:%d: No IDENTIFY after reselection.\n",
  3955. sym_name (np), target);
  3956. goto out_stuck;
  3957. /*
  3958. * The device reselected a LUN we donnot know about.
  3959. */
  3960. case SIR_RESEL_BAD_LUN:
  3961. np->msgout[0] = M_RESET;
  3962. goto out;
  3963. /*
  3964. * The device reselected for an untagged nexus and we
  3965. * haven't any.
  3966. */
  3967. case SIR_RESEL_BAD_I_T_L:
  3968. np->msgout[0] = M_ABORT;
  3969. goto out;
  3970. /*
  3971. * The device reselected for a tagged nexus that we donnot
  3972. * have.
  3973. */
  3974. case SIR_RESEL_BAD_I_T_L_Q:
  3975. np->msgout[0] = M_ABORT_TAG;
  3976. goto out;
  3977. /*
  3978. * The SCRIPTS let us know that the device has grabbed
  3979. * our message and will abort the job.
  3980. */
  3981. case SIR_RESEL_ABORTED:
  3982. np->lastmsg = np->msgout[0];
  3983. np->msgout[0] = M_NOOP;
  3984. printf ("%s:%d: message %x sent on bad reselection.\n",
  3985. sym_name (np), target, np->lastmsg);
  3986. goto out;
  3987. /*
  3988. * The SCRIPTS let us know that a message has been
  3989. * successfully sent to the device.
  3990. */
  3991. case SIR_MSG_OUT_DONE:
  3992. np->lastmsg = np->msgout[0];
  3993. np->msgout[0] = M_NOOP;
  3994. /* Should we really care of that */
  3995. if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
  3996. if (cp) {
  3997. cp->xerr_status &= ~XE_PARITY_ERR;
  3998. if (!cp->xerr_status)
  3999. OUTOFFB(np, HF_PRT, HF_EXT_ERR);
  4000. }
  4001. }
  4002. goto out;
  4003. /*
  4004. * The device didn't send a GOOD SCSI status.
  4005. * We may have some work to do prior to allow
  4006. * the SCRIPTS processor to continue.
  4007. */
  4008. case SIR_BAD_SCSI_STATUS:
  4009. if (!cp)
  4010. goto out;
  4011. sym_sir_bad_scsi_status(np, num, cp);
  4012. return;
  4013. /*
  4014. * We are asked by the SCRIPTS to prepare a
  4015. * REJECT message.
  4016. */
  4017. case SIR_REJECT_TO_SEND:
  4018. sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
  4019. np->msgout[0] = M_REJECT;
  4020. goto out;
  4021. /*
  4022. * We have been ODD at the end of a DATA IN
  4023. * transfer and the device didn't send a
  4024. * IGNORE WIDE RESIDUE message.
  4025. * It is a data overrun condition.
  4026. */
  4027. case SIR_SWIDE_OVERRUN:
  4028. if (cp) {
  4029. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4030. cp->xerr_status |= XE_SWIDE_OVRUN;
  4031. }
  4032. goto out;
  4033. /*
  4034. * We have been ODD at the end of a DATA OUT
  4035. * transfer.
  4036. * It is a data underrun condition.
  4037. */
  4038. case SIR_SODL_UNDERRUN:
  4039. if (cp) {
  4040. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4041. cp->xerr_status |= XE_SODL_UNRUN;
  4042. }
  4043. goto out;
  4044. /*
  4045. * The device wants us to tranfer more data than
  4046. * expected or in the wrong direction.
  4047. * The number of extra bytes is in scratcha.
  4048. * It is a data overrun condition.
  4049. */
  4050. case SIR_DATA_OVERRUN:
  4051. if (cp) {
  4052. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4053. cp->xerr_status |= XE_EXTRA_DATA;
  4054. cp->extra_bytes += INL(np, nc_scratcha);
  4055. }
  4056. goto out;
  4057. /*
  4058. * The device switched to an illegal phase (4/5).
  4059. */
  4060. case SIR_BAD_PHASE:
  4061. if (cp) {
  4062. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4063. cp->xerr_status |= XE_BAD_PHASE;
  4064. }
  4065. goto out;
  4066. /*
  4067. * We received a message.
  4068. */
  4069. case SIR_MSG_RECEIVED:
  4070. if (!cp)
  4071. goto out_stuck;
  4072. switch (np->msgin [0]) {
  4073. /*
  4074. * We received an extended message.
  4075. * We handle MODIFY DATA POINTER, SDTR, WDTR
  4076. * and reject all other extended messages.
  4077. */
  4078. case M_EXTENDED:
  4079. switch (np->msgin [2]) {
  4080. case M_X_MODIFY_DP:
  4081. if (DEBUG_FLAGS & DEBUG_POINTER)
  4082. sym_print_msg(cp, NULL, np->msgin);
  4083. tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
  4084. (np->msgin[5]<<8) + (np->msgin[6]);
  4085. sym_modify_dp(np, tp, cp, tmp);
  4086. return;
  4087. case M_X_SYNC_REQ:
  4088. sym_sync_nego(np, tp, cp);
  4089. return;
  4090. case M_X_PPR_REQ:
  4091. sym_ppr_nego(np, tp, cp);
  4092. return;
  4093. case M_X_WIDE_REQ:
  4094. sym_wide_nego(np, tp, cp);
  4095. return;
  4096. default:
  4097. goto out_reject;
  4098. }
  4099. break;
  4100. /*
  4101. * We received a 1/2 byte message not handled from SCRIPTS.
  4102. * We are only expecting MESSAGE REJECT and IGNORE WIDE
  4103. * RESIDUE messages that haven't been anticipated by
  4104. * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
  4105. * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
  4106. */
  4107. case M_IGN_RESIDUE:
  4108. if (DEBUG_FLAGS & DEBUG_POINTER)
  4109. sym_print_msg(cp, NULL, np->msgin);
  4110. if (cp->host_flags & HF_SENSE)
  4111. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4112. else
  4113. sym_modify_dp(np, tp, cp, -1);
  4114. return;
  4115. case M_REJECT:
  4116. if (INB(np, HS_PRT) == HS_NEGOTIATE)
  4117. sym_nego_rejected(np, tp, cp);
  4118. else {
  4119. sym_print_addr(cp->cmd,
  4120. "M_REJECT received (%x:%x).\n",
  4121. scr_to_cpu(np->lastmsg), np->msgout[0]);
  4122. }
  4123. goto out_clrack;
  4124. break;
  4125. default:
  4126. goto out_reject;
  4127. }
  4128. break;
  4129. /*
  4130. * We received an unknown message.
  4131. * Ignore all MSG IN phases and reject it.
  4132. */
  4133. case SIR_MSG_WEIRD:
  4134. sym_print_msg(cp, "WEIRD message received", np->msgin);
  4135. OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
  4136. return;
  4137. /*
  4138. * Negotiation failed.
  4139. * Target does not send us the reply.
  4140. * Remove the HS_NEGOTIATE status.
  4141. */
  4142. case SIR_NEGO_FAILED:
  4143. OUTB(np, HS_PRT, HS_BUSY);
  4144. /*
  4145. * Negotiation failed.
  4146. * Target does not want answer message.
  4147. */
  4148. case SIR_NEGO_PROTO:
  4149. sym_nego_default(np, tp, cp);
  4150. goto out;
  4151. }
  4152. out:
  4153. OUTONB_STD();
  4154. return;
  4155. out_reject:
  4156. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  4157. return;
  4158. out_clrack:
  4159. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4160. return;
  4161. out_stuck:
  4162. return;
  4163. }
  4164. /*
  4165. * Acquire a control block
  4166. */
  4167. struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
  4168. {
  4169. u_char tn = cmd->device->id;
  4170. u_char ln = cmd->device->lun;
  4171. struct sym_tcb *tp = &np->target[tn];
  4172. struct sym_lcb *lp = sym_lp(tp, ln);
  4173. u_short tag = NO_TAG;
  4174. SYM_QUEHEAD *qp;
  4175. struct sym_ccb *cp = NULL;
  4176. /*
  4177. * Look for a free CCB
  4178. */
  4179. if (sym_que_empty(&np->free_ccbq))
  4180. sym_alloc_ccb(np);
  4181. qp = sym_remque_head(&np->free_ccbq);
  4182. if (!qp)
  4183. goto out;
  4184. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4185. {
  4186. /*
  4187. * If we have been asked for a tagged command.
  4188. */
  4189. if (tag_order) {
  4190. /*
  4191. * Debugging purpose.
  4192. */
  4193. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4194. if (lp->busy_itl != 0)
  4195. goto out_free;
  4196. #endif
  4197. /*
  4198. * Allocate resources for tags if not yet.
  4199. */
  4200. if (!lp->cb_tags) {
  4201. sym_alloc_lcb_tags(np, tn, ln);
  4202. if (!lp->cb_tags)
  4203. goto out_free;
  4204. }
  4205. /*
  4206. * Get a tag for this SCSI IO and set up
  4207. * the CCB bus address for reselection,
  4208. * and count it for this LUN.
  4209. * Toggle reselect path to tagged.
  4210. */
  4211. if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
  4212. tag = lp->cb_tags[lp->ia_tag];
  4213. if (++lp->ia_tag == SYM_CONF_MAX_TASK)
  4214. lp->ia_tag = 0;
  4215. ++lp->busy_itlq;
  4216. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4217. lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
  4218. lp->head.resel_sa =
  4219. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  4220. #endif
  4221. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4222. cp->tags_si = lp->tags_si;
  4223. ++lp->tags_sum[cp->tags_si];
  4224. ++lp->tags_since;
  4225. #endif
  4226. }
  4227. else
  4228. goto out_free;
  4229. }
  4230. /*
  4231. * This command will not be tagged.
  4232. * If we already have either a tagged or untagged
  4233. * one, refuse to overlap this untagged one.
  4234. */
  4235. else {
  4236. /*
  4237. * Debugging purpose.
  4238. */
  4239. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4240. if (lp->busy_itl != 0 || lp->busy_itlq != 0)
  4241. goto out_free;
  4242. #endif
  4243. /*
  4244. * Count this nexus for this LUN.
  4245. * Set up the CCB bus address for reselection.
  4246. * Toggle reselect path to untagged.
  4247. */
  4248. ++lp->busy_itl;
  4249. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4250. if (lp->busy_itl == 1) {
  4251. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  4252. lp->head.resel_sa =
  4253. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  4254. }
  4255. else
  4256. goto out_free;
  4257. #endif
  4258. }
  4259. }
  4260. /*
  4261. * Put the CCB into the busy queue.
  4262. */
  4263. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  4264. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4265. if (lp) {
  4266. sym_remque(&cp->link2_ccbq);
  4267. sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
  4268. }
  4269. #endif
  4270. cp->to_abort = 0;
  4271. cp->odd_byte_adjustment = 0;
  4272. cp->tag = tag;
  4273. cp->order = tag_order;
  4274. cp->target = tn;
  4275. cp->lun = ln;
  4276. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4277. sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
  4278. }
  4279. out:
  4280. return cp;
  4281. out_free:
  4282. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4283. return NULL;
  4284. }
  4285. /*
  4286. * Release one control block
  4287. */
  4288. void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
  4289. {
  4290. struct sym_tcb *tp = &np->target[cp->target];
  4291. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  4292. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4293. sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
  4294. cp, cp->tag);
  4295. }
  4296. /*
  4297. * If LCB available,
  4298. */
  4299. if (lp) {
  4300. /*
  4301. * If tagged, release the tag, set the relect path
  4302. */
  4303. if (cp->tag != NO_TAG) {
  4304. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4305. --lp->tags_sum[cp->tags_si];
  4306. #endif
  4307. /*
  4308. * Free the tag value.
  4309. */
  4310. lp->cb_tags[lp->if_tag] = cp->tag;
  4311. if (++lp->if_tag == SYM_CONF_MAX_TASK)
  4312. lp->if_tag = 0;
  4313. /*
  4314. * Make the reselect path invalid,
  4315. * and uncount this CCB.
  4316. */
  4317. lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
  4318. --lp->busy_itlq;
  4319. } else { /* Untagged */
  4320. /*
  4321. * Make the reselect path invalid,
  4322. * and uncount this CCB.
  4323. */
  4324. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4325. --lp->busy_itl;
  4326. }
  4327. /*
  4328. * If no JOB active, make the LUN reselect path invalid.
  4329. */
  4330. if (lp->busy_itlq == 0 && lp->busy_itl == 0)
  4331. lp->head.resel_sa =
  4332. cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4333. }
  4334. /*
  4335. * We donnot queue more than 1 ccb per target
  4336. * with negotiation at any time. If this ccb was
  4337. * used for negotiation, clear this info in the tcb.
  4338. */
  4339. if (cp == tp->nego_cp)
  4340. tp->nego_cp = NULL;
  4341. #ifdef SYM_CONF_IARB_SUPPORT
  4342. /*
  4343. * If we just complete the last queued CCB,
  4344. * clear this info that is no longer relevant.
  4345. */
  4346. if (cp == np->last_cp)
  4347. np->last_cp = 0;
  4348. #endif
  4349. /*
  4350. * Make this CCB available.
  4351. */
  4352. cp->cmd = NULL;
  4353. cp->host_status = HS_IDLE;
  4354. sym_remque(&cp->link_ccbq);
  4355. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4356. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4357. if (lp) {
  4358. sym_remque(&cp->link2_ccbq);
  4359. sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
  4360. if (cp->started) {
  4361. if (cp->tag != NO_TAG)
  4362. --lp->started_tags;
  4363. else
  4364. --lp->started_no_tag;
  4365. }
  4366. }
  4367. cp->started = 0;
  4368. #endif
  4369. }
  4370. /*
  4371. * Allocate a CCB from memory and initialize its fixed part.
  4372. */
  4373. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
  4374. {
  4375. struct sym_ccb *cp = NULL;
  4376. int hcode;
  4377. /*
  4378. * Prevent from allocating more CCBs than we can
  4379. * queue to the controller.
  4380. */
  4381. if (np->actccbs >= SYM_CONF_MAX_START)
  4382. return NULL;
  4383. /*
  4384. * Allocate memory for this CCB.
  4385. */
  4386. cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
  4387. if (!cp)
  4388. goto out_free;
  4389. /*
  4390. * Count it.
  4391. */
  4392. np->actccbs++;
  4393. /*
  4394. * Compute the bus address of this ccb.
  4395. */
  4396. cp->ccb_ba = vtobus(cp);
  4397. /*
  4398. * Insert this ccb into the hashed list.
  4399. */
  4400. hcode = CCB_HASH_CODE(cp->ccb_ba);
  4401. cp->link_ccbh = np->ccbh[hcode];
  4402. np->ccbh[hcode] = cp;
  4403. /*
  4404. * Initialyze the start and restart actions.
  4405. */
  4406. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  4407. cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  4408. /*
  4409. * Initilialyze some other fields.
  4410. */
  4411. cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
  4412. /*
  4413. * Chain into free ccb queue.
  4414. */
  4415. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4416. /*
  4417. * Chain into optionnal lists.
  4418. */
  4419. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4420. sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
  4421. #endif
  4422. return cp;
  4423. out_free:
  4424. if (cp)
  4425. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  4426. return NULL;
  4427. }
  4428. /*
  4429. * Look up a CCB from a DSA value.
  4430. */
  4431. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
  4432. {
  4433. int hcode;
  4434. struct sym_ccb *cp;
  4435. hcode = CCB_HASH_CODE(dsa);
  4436. cp = np->ccbh[hcode];
  4437. while (cp) {
  4438. if (cp->ccb_ba == dsa)
  4439. break;
  4440. cp = cp->link_ccbh;
  4441. }
  4442. return cp;
  4443. }
  4444. /*
  4445. * Target control block initialisation.
  4446. * Nothing important to do at the moment.
  4447. */
  4448. static void sym_init_tcb (struct sym_hcb *np, u_char tn)
  4449. {
  4450. #if 0 /* Hmmm... this checking looks paranoid. */
  4451. /*
  4452. * Check some alignments required by the chip.
  4453. */
  4454. assert (((offsetof(struct sym_reg, nc_sxfer) ^
  4455. offsetof(struct sym_tcb, head.sval)) &3) == 0);
  4456. assert (((offsetof(struct sym_reg, nc_scntl3) ^
  4457. offsetof(struct sym_tcb, head.wval)) &3) == 0);
  4458. #endif
  4459. }
  4460. /*
  4461. * Lun control block allocation and initialization.
  4462. */
  4463. struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
  4464. {
  4465. struct sym_tcb *tp = &np->target[tn];
  4466. struct sym_lcb *lp = NULL;
  4467. /*
  4468. * Initialize the target control block if not yet.
  4469. */
  4470. sym_init_tcb (np, tn);
  4471. /*
  4472. * Allocate the LCB bus address array.
  4473. * Compute the bus address of this table.
  4474. */
  4475. if (ln && !tp->luntbl) {
  4476. int i;
  4477. tp->luntbl = sym_calloc_dma(256, "LUNTBL");
  4478. if (!tp->luntbl)
  4479. goto fail;
  4480. for (i = 0 ; i < 64 ; i++)
  4481. tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  4482. tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
  4483. }
  4484. /*
  4485. * Allocate the table of pointers for LUN(s) > 0, if needed.
  4486. */
  4487. if (ln && !tp->lunmp) {
  4488. tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
  4489. GFP_KERNEL);
  4490. if (!tp->lunmp)
  4491. goto fail;
  4492. }
  4493. /*
  4494. * Allocate the lcb.
  4495. * Make it available to the chip.
  4496. */
  4497. lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
  4498. if (!lp)
  4499. goto fail;
  4500. if (ln) {
  4501. tp->lunmp[ln] = lp;
  4502. tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
  4503. }
  4504. else {
  4505. tp->lun0p = lp;
  4506. tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
  4507. }
  4508. /*
  4509. * Let the itl task point to error handling.
  4510. */
  4511. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4512. /*
  4513. * Set the reselect pattern to our default. :)
  4514. */
  4515. lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4516. /*
  4517. * Set user capabilities.
  4518. */
  4519. lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  4520. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4521. /*
  4522. * Initialize device queueing.
  4523. */
  4524. sym_que_init(&lp->waiting_ccbq);
  4525. sym_que_init(&lp->started_ccbq);
  4526. lp->started_max = SYM_CONF_MAX_TASK;
  4527. lp->started_limit = SYM_CONF_MAX_TASK;
  4528. #endif
  4529. fail:
  4530. return lp;
  4531. }
  4532. /*
  4533. * Allocate LCB resources for tagged command queuing.
  4534. */
  4535. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
  4536. {
  4537. struct sym_tcb *tp = &np->target[tn];
  4538. struct sym_lcb *lp = sym_lp(tp, ln);
  4539. int i;
  4540. /*
  4541. * Allocate the task table and and the tag allocation
  4542. * circular buffer. We want both or none.
  4543. */
  4544. lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4545. if (!lp->itlq_tbl)
  4546. goto fail;
  4547. lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
  4548. if (!lp->cb_tags) {
  4549. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4550. lp->itlq_tbl = NULL;
  4551. goto fail;
  4552. }
  4553. /*
  4554. * Initialize the task table with invalid entries.
  4555. */
  4556. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4557. lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
  4558. /*
  4559. * Fill up the tag buffer with tag numbers.
  4560. */
  4561. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4562. lp->cb_tags[i] = i;
  4563. /*
  4564. * Make the task table available to SCRIPTS,
  4565. * And accept tagged commands now.
  4566. */
  4567. lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
  4568. return;
  4569. fail:
  4570. return;
  4571. }
  4572. /*
  4573. * Queue a SCSI IO to the controller.
  4574. */
  4575. int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  4576. {
  4577. struct scsi_device *sdev = cmd->device;
  4578. struct sym_tcb *tp;
  4579. struct sym_lcb *lp;
  4580. u_char *msgptr;
  4581. u_int msglen;
  4582. int can_disconnect;
  4583. /*
  4584. * Keep track of the IO in our CCB.
  4585. */
  4586. cp->cmd = cmd;
  4587. /*
  4588. * Retrieve the target descriptor.
  4589. */
  4590. tp = &np->target[cp->target];
  4591. /*
  4592. * Retrieve the lun descriptor.
  4593. */
  4594. lp = sym_lp(tp, sdev->lun);
  4595. can_disconnect = (cp->tag != NO_TAG) ||
  4596. (lp && (lp->curr_flags & SYM_DISC_ENABLED));
  4597. msgptr = cp->scsi_smsg;
  4598. msglen = 0;
  4599. msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
  4600. /*
  4601. * Build the tag message if present.
  4602. */
  4603. if (cp->tag != NO_TAG) {
  4604. u_char order = cp->order;
  4605. switch(order) {
  4606. case M_ORDERED_TAG:
  4607. break;
  4608. case M_HEAD_TAG:
  4609. break;
  4610. default:
  4611. order = M_SIMPLE_TAG;
  4612. }
  4613. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4614. /*
  4615. * Avoid too much reordering of SCSI commands.
  4616. * The algorithm tries to prevent completion of any
  4617. * tagged command from being delayed against more
  4618. * than 3 times the max number of queued commands.
  4619. */
  4620. if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
  4621. lp->tags_si = !(lp->tags_si);
  4622. if (lp->tags_sum[lp->tags_si]) {
  4623. order = M_ORDERED_TAG;
  4624. if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
  4625. sym_print_addr(cmd,
  4626. "ordered tag forced.\n");
  4627. }
  4628. }
  4629. lp->tags_since = 0;
  4630. }
  4631. #endif
  4632. msgptr[msglen++] = order;
  4633. /*
  4634. * For less than 128 tags, actual tags are numbered
  4635. * 1,3,5,..2*MAXTAGS+1,since we may have to deal
  4636. * with devices that have problems with #TAG 0 or too
  4637. * great #TAG numbers. For more tags (up to 256),
  4638. * we use directly our tag number.
  4639. */
  4640. #if SYM_CONF_MAX_TASK > (512/4)
  4641. msgptr[msglen++] = cp->tag;
  4642. #else
  4643. msgptr[msglen++] = (cp->tag << 1) + 1;
  4644. #endif
  4645. }
  4646. /*
  4647. * Build a negotiation message if needed.
  4648. * (nego_status is filled by sym_prepare_nego())
  4649. */
  4650. cp->nego_status = 0;
  4651. if (tp->tgoal.check_nego && !tp->nego_cp && lp) {
  4652. msglen += sym_prepare_nego(np, cp, msgptr + msglen);
  4653. }
  4654. /*
  4655. * Startqueue
  4656. */
  4657. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  4658. cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
  4659. /*
  4660. * select
  4661. */
  4662. cp->phys.select.sel_id = cp->target;
  4663. cp->phys.select.sel_scntl3 = tp->head.wval;
  4664. cp->phys.select.sel_sxfer = tp->head.sval;
  4665. cp->phys.select.sel_scntl4 = tp->head.uval;
  4666. /*
  4667. * message
  4668. */
  4669. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
  4670. cp->phys.smsg.size = cpu_to_scr(msglen);
  4671. /*
  4672. * status
  4673. */
  4674. cp->host_xflags = 0;
  4675. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  4676. cp->ssss_status = S_ILLEGAL;
  4677. cp->xerr_status = 0;
  4678. cp->host_flags = 0;
  4679. cp->extra_bytes = 0;
  4680. /*
  4681. * extreme data pointer.
  4682. * shall be positive, so -1 is lower than lowest.:)
  4683. */
  4684. cp->ext_sg = -1;
  4685. cp->ext_ofs = 0;
  4686. /*
  4687. * Build the CDB and DATA descriptor block
  4688. * and start the IO.
  4689. */
  4690. return sym_setup_data_and_start(np, cmd, cp);
  4691. }
  4692. /*
  4693. * Reset a SCSI target (all LUNs of this target).
  4694. */
  4695. int sym_reset_scsi_target(struct sym_hcb *np, int target)
  4696. {
  4697. struct sym_tcb *tp;
  4698. if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
  4699. return -1;
  4700. tp = &np->target[target];
  4701. tp->to_reset = 1;
  4702. np->istat_sem = SEM;
  4703. OUTB(np, nc_istat, SIGP|SEM);
  4704. return 0;
  4705. }
  4706. /*
  4707. * Abort a SCSI IO.
  4708. */
  4709. static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
  4710. {
  4711. /*
  4712. * Check that the IO is active.
  4713. */
  4714. if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
  4715. return -1;
  4716. /*
  4717. * If a previous abort didn't succeed in time,
  4718. * perform a BUS reset.
  4719. */
  4720. if (cp->to_abort) {
  4721. sym_reset_scsi_bus(np, 1);
  4722. return 0;
  4723. }
  4724. /*
  4725. * Mark the CCB for abort and allow time for.
  4726. */
  4727. cp->to_abort = timed_out ? 2 : 1;
  4728. /*
  4729. * Tell the SCRIPTS processor to stop and synchronize with us.
  4730. */
  4731. np->istat_sem = SEM;
  4732. OUTB(np, nc_istat, SIGP|SEM);
  4733. return 0;
  4734. }
  4735. int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
  4736. {
  4737. struct sym_ccb *cp;
  4738. SYM_QUEHEAD *qp;
  4739. /*
  4740. * Look up our CCB control block.
  4741. */
  4742. cp = NULL;
  4743. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  4744. struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4745. if (cp2->cmd == cmd) {
  4746. cp = cp2;
  4747. break;
  4748. }
  4749. }
  4750. return sym_abort_ccb(np, cp, timed_out);
  4751. }
  4752. /*
  4753. * Complete execution of a SCSI command with extended
  4754. * error, SCSI status error, or having been auto-sensed.
  4755. *
  4756. * The SCRIPTS processor is not running there, so we
  4757. * can safely access IO registers and remove JOBs from
  4758. * the START queue.
  4759. * SCRATCHA is assumed to have been loaded with STARTPOS
  4760. * before the SCRIPTS called the C code.
  4761. */
  4762. void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
  4763. {
  4764. struct scsi_device *sdev;
  4765. struct scsi_cmnd *cmd;
  4766. struct sym_tcb *tp;
  4767. struct sym_lcb *lp;
  4768. int resid;
  4769. int i;
  4770. /*
  4771. * Paranoid check. :)
  4772. */
  4773. if (!cp || !cp->cmd)
  4774. return;
  4775. cmd = cp->cmd;
  4776. sdev = cmd->device;
  4777. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
  4778. dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
  4779. cp->host_status, cp->ssss_status, cp->host_flags);
  4780. }
  4781. /*
  4782. * Get target and lun pointers.
  4783. */
  4784. tp = &np->target[cp->target];
  4785. lp = sym_lp(tp, sdev->lun);
  4786. /*
  4787. * Check for extended errors.
  4788. */
  4789. if (cp->xerr_status) {
  4790. if (sym_verbose)
  4791. sym_print_xerr(cmd, cp->xerr_status);
  4792. if (cp->host_status == HS_COMPLETE)
  4793. cp->host_status = HS_COMP_ERR;
  4794. }
  4795. /*
  4796. * Calculate the residual.
  4797. */
  4798. resid = sym_compute_residual(np, cp);
  4799. if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
  4800. resid = 0; /* throw them away. :) */
  4801. cp->sv_resid = 0;
  4802. }
  4803. #ifdef DEBUG_2_0_X
  4804. if (resid)
  4805. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4806. #endif
  4807. /*
  4808. * Dequeue all queued CCBs for that device
  4809. * not yet started by SCRIPTS.
  4810. */
  4811. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  4812. i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
  4813. /*
  4814. * Restart the SCRIPTS processor.
  4815. */
  4816. OUTL_DSP(np, SCRIPTA_BA(np, start));
  4817. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4818. if (cp->host_status == HS_COMPLETE &&
  4819. cp->ssss_status == S_QUEUE_FULL) {
  4820. if (!lp || lp->started_tags - i < 2)
  4821. goto weirdness;
  4822. /*
  4823. * Decrease queue depth as needed.
  4824. */
  4825. lp->started_max = lp->started_tags - i - 1;
  4826. lp->num_sgood = 0;
  4827. if (sym_verbose >= 2) {
  4828. sym_print_addr(cmd, " queue depth is now %d\n",
  4829. lp->started_max);
  4830. }
  4831. /*
  4832. * Repair the CCB.
  4833. */
  4834. cp->host_status = HS_BUSY;
  4835. cp->ssss_status = S_ILLEGAL;
  4836. /*
  4837. * Let's requeue it to device.
  4838. */
  4839. sym_set_cam_status(cmd, DID_SOFT_ERROR);
  4840. goto finish;
  4841. }
  4842. weirdness:
  4843. #endif
  4844. /*
  4845. * Build result in CAM ccb.
  4846. */
  4847. sym_set_cam_result_error(np, cp, resid);
  4848. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4849. finish:
  4850. #endif
  4851. /*
  4852. * Add this one to the COMP queue.
  4853. */
  4854. sym_remque(&cp->link_ccbq);
  4855. sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
  4856. /*
  4857. * Complete all those commands with either error
  4858. * or requeue condition.
  4859. */
  4860. sym_flush_comp_queue(np, 0);
  4861. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4862. /*
  4863. * Donnot start more than 1 command after an error.
  4864. */
  4865. sym_start_next_ccbs(np, lp, 1);
  4866. #endif
  4867. }
  4868. /*
  4869. * Complete execution of a successful SCSI command.
  4870. *
  4871. * Only successful commands go to the DONE queue,
  4872. * since we need to have the SCRIPTS processor
  4873. * stopped on any error condition.
  4874. * The SCRIPTS processor is running while we are
  4875. * completing successful commands.
  4876. */
  4877. void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
  4878. {
  4879. struct sym_tcb *tp;
  4880. struct sym_lcb *lp;
  4881. struct scsi_cmnd *cmd;
  4882. int resid;
  4883. /*
  4884. * Paranoid check. :)
  4885. */
  4886. if (!cp || !cp->cmd)
  4887. return;
  4888. assert (cp->host_status == HS_COMPLETE);
  4889. /*
  4890. * Get user command.
  4891. */
  4892. cmd = cp->cmd;
  4893. /*
  4894. * Get target and lun pointers.
  4895. */
  4896. tp = &np->target[cp->target];
  4897. lp = sym_lp(tp, cp->lun);
  4898. /*
  4899. * If all data have been transferred, given than no
  4900. * extended error did occur, there is no residual.
  4901. */
  4902. resid = 0;
  4903. if (cp->phys.head.lastp != cp->goalp)
  4904. resid = sym_compute_residual(np, cp);
  4905. /*
  4906. * Wrong transfer residuals may be worse than just always
  4907. * returning zero. User can disable this feature in
  4908. * sym53c8xx.h. Residual support is enabled by default.
  4909. */
  4910. if (!SYM_SETUP_RESIDUAL_SUPPORT)
  4911. resid = 0;
  4912. #ifdef DEBUG_2_0_X
  4913. if (resid)
  4914. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4915. #endif
  4916. /*
  4917. * Build result in CAM ccb.
  4918. */
  4919. sym_set_cam_result_ok(cp, cmd, resid);
  4920. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4921. /*
  4922. * If max number of started ccbs had been reduced,
  4923. * increase it if 200 good status received.
  4924. */
  4925. if (lp && lp->started_max < lp->started_limit) {
  4926. ++lp->num_sgood;
  4927. if (lp->num_sgood >= 200) {
  4928. lp->num_sgood = 0;
  4929. ++lp->started_max;
  4930. if (sym_verbose >= 2) {
  4931. sym_print_addr(cmd, " queue depth is now %d\n",
  4932. lp->started_max);
  4933. }
  4934. }
  4935. }
  4936. #endif
  4937. /*
  4938. * Free our CCB.
  4939. */
  4940. sym_free_ccb (np, cp);
  4941. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4942. /*
  4943. * Requeue a couple of awaiting scsi commands.
  4944. */
  4945. if (!sym_que_empty(&lp->waiting_ccbq))
  4946. sym_start_next_ccbs(np, lp, 2);
  4947. #endif
  4948. /*
  4949. * Complete the command.
  4950. */
  4951. sym_xpt_done(np, cmd);
  4952. }
  4953. /*
  4954. * Soft-attach the controller.
  4955. */
  4956. int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
  4957. {
  4958. struct sym_hcb *np = sym_get_hcb(shost);
  4959. int i;
  4960. /*
  4961. * Get some info about the firmware.
  4962. */
  4963. np->scripta_sz = fw->a_size;
  4964. np->scriptb_sz = fw->b_size;
  4965. np->scriptz_sz = fw->z_size;
  4966. np->fw_setup = fw->setup;
  4967. np->fw_patch = fw->patch;
  4968. np->fw_name = fw->name;
  4969. /*
  4970. * Save setting of some IO registers, so we will
  4971. * be able to probe specific implementations.
  4972. */
  4973. sym_save_initial_setting (np);
  4974. /*
  4975. * Reset the chip now, since it has been reported
  4976. * that SCSI clock calibration may not work properly
  4977. * if the chip is currently active.
  4978. */
  4979. sym_chip_reset(np);
  4980. /*
  4981. * Prepare controller and devices settings, according
  4982. * to chip features, user set-up and driver set-up.
  4983. */
  4984. sym_prepare_setting(shost, np, nvram);
  4985. /*
  4986. * Check the PCI clock frequency.
  4987. * Must be performed after prepare_setting since it destroys
  4988. * STEST1 that is used to probe for the clock doubler.
  4989. */
  4990. i = sym_getpciclock(np);
  4991. if (i > 37000 && !(np->features & FE_66MHZ))
  4992. printf("%s: PCI BUS clock seems too high: %u KHz.\n",
  4993. sym_name(np), i);
  4994. /*
  4995. * Allocate the start queue.
  4996. */
  4997. np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
  4998. if (!np->squeue)
  4999. goto attach_failed;
  5000. np->squeue_ba = vtobus(np->squeue);
  5001. /*
  5002. * Allocate the done queue.
  5003. */
  5004. np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
  5005. if (!np->dqueue)
  5006. goto attach_failed;
  5007. np->dqueue_ba = vtobus(np->dqueue);
  5008. /*
  5009. * Allocate the target bus address array.
  5010. */
  5011. np->targtbl = sym_calloc_dma(256, "TARGTBL");
  5012. if (!np->targtbl)
  5013. goto attach_failed;
  5014. np->targtbl_ba = vtobus(np->targtbl);
  5015. /*
  5016. * Allocate SCRIPTS areas.
  5017. */
  5018. np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
  5019. np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
  5020. np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
  5021. if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
  5022. goto attach_failed;
  5023. /*
  5024. * Allocate the array of lists of CCBs hashed by DSA.
  5025. */
  5026. np->ccbh = kcalloc(CCB_HASH_SIZE, sizeof(struct sym_ccb **), GFP_KERNEL);
  5027. if (!np->ccbh)
  5028. goto attach_failed;
  5029. /*
  5030. * Initialyze the CCB free and busy queues.
  5031. */
  5032. sym_que_init(&np->free_ccbq);
  5033. sym_que_init(&np->busy_ccbq);
  5034. sym_que_init(&np->comp_ccbq);
  5035. /*
  5036. * Initialization for optional handling
  5037. * of device queueing.
  5038. */
  5039. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5040. sym_que_init(&np->dummy_ccbq);
  5041. #endif
  5042. /*
  5043. * Allocate some CCB. We need at least ONE.
  5044. */
  5045. if (!sym_alloc_ccb(np))
  5046. goto attach_failed;
  5047. /*
  5048. * Calculate BUS addresses where we are going
  5049. * to load the SCRIPTS.
  5050. */
  5051. np->scripta_ba = vtobus(np->scripta0);
  5052. np->scriptb_ba = vtobus(np->scriptb0);
  5053. np->scriptz_ba = vtobus(np->scriptz0);
  5054. if (np->ram_ba) {
  5055. np->scripta_ba = np->ram_ba;
  5056. if (np->features & FE_RAM8K) {
  5057. np->ram_ws = 8192;
  5058. np->scriptb_ba = np->scripta_ba + 4096;
  5059. #if 0 /* May get useful for 64 BIT PCI addressing */
  5060. np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
  5061. #endif
  5062. }
  5063. else
  5064. np->ram_ws = 4096;
  5065. }
  5066. /*
  5067. * Copy scripts to controller instance.
  5068. */
  5069. memcpy(np->scripta0, fw->a_base, np->scripta_sz);
  5070. memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
  5071. memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
  5072. /*
  5073. * Setup variable parts in scripts and compute
  5074. * scripts bus addresses used from the C code.
  5075. */
  5076. np->fw_setup(np, fw);
  5077. /*
  5078. * Bind SCRIPTS with physical addresses usable by the
  5079. * SCRIPTS processor (as seen from the BUS = BUS addresses).
  5080. */
  5081. sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
  5082. sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
  5083. sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
  5084. #ifdef SYM_CONF_IARB_SUPPORT
  5085. /*
  5086. * If user wants IARB to be set when we win arbitration
  5087. * and have other jobs, compute the max number of consecutive
  5088. * settings of IARB hints before we leave devices a chance to
  5089. * arbitrate for reselection.
  5090. */
  5091. #ifdef SYM_SETUP_IARB_MAX
  5092. np->iarb_max = SYM_SETUP_IARB_MAX;
  5093. #else
  5094. np->iarb_max = 4;
  5095. #endif
  5096. #endif
  5097. /*
  5098. * Prepare the idle and invalid task actions.
  5099. */
  5100. np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5101. np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5102. np->idletask_ba = vtobus(&np->idletask);
  5103. np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5104. np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5105. np->notask_ba = vtobus(&np->notask);
  5106. np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5107. np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5108. np->bad_itl_ba = vtobus(&np->bad_itl);
  5109. np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5110. np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
  5111. np->bad_itlq_ba = vtobus(&np->bad_itlq);
  5112. /*
  5113. * Allocate and prepare the lun JUMP table that is used
  5114. * for a target prior the probing of devices (bad lun table).
  5115. * A private table will be allocated for the target on the
  5116. * first INQUIRY response received.
  5117. */
  5118. np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
  5119. if (!np->badluntbl)
  5120. goto attach_failed;
  5121. np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  5122. for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
  5123. np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  5124. /*
  5125. * Prepare the bus address array that contains the bus
  5126. * address of each target control block.
  5127. * For now, assume all logical units are wrong. :)
  5128. */
  5129. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  5130. np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
  5131. np->target[i].head.luntbl_sa =
  5132. cpu_to_scr(vtobus(np->badluntbl));
  5133. np->target[i].head.lun0_sa =
  5134. cpu_to_scr(vtobus(&np->badlun_sa));
  5135. }
  5136. /*
  5137. * Now check the cache handling of the pci chipset.
  5138. */
  5139. if (sym_snooptest (np)) {
  5140. printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
  5141. goto attach_failed;
  5142. }
  5143. /*
  5144. * Sigh! we are done.
  5145. */
  5146. return 0;
  5147. attach_failed:
  5148. return -ENXIO;
  5149. }
  5150. /*
  5151. * Free everything that has been allocated for this device.
  5152. */
  5153. void sym_hcb_free(struct sym_hcb *np)
  5154. {
  5155. SYM_QUEHEAD *qp;
  5156. struct sym_ccb *cp;
  5157. struct sym_tcb *tp;
  5158. int target;
  5159. if (np->scriptz0)
  5160. sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
  5161. if (np->scriptb0)
  5162. sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
  5163. if (np->scripta0)
  5164. sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
  5165. if (np->squeue)
  5166. sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
  5167. if (np->dqueue)
  5168. sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
  5169. if (np->actccbs) {
  5170. while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
  5171. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  5172. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  5173. }
  5174. }
  5175. kfree(np->ccbh);
  5176. if (np->badluntbl)
  5177. sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
  5178. for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
  5179. tp = &np->target[target];
  5180. #if SYM_CONF_MAX_LUN > 1
  5181. kfree(tp->lunmp);
  5182. #endif
  5183. }
  5184. if (np->targtbl)
  5185. sym_mfree_dma(np->targtbl, 256, "TARGTBL");
  5186. }