sym_hipd.c 141 KB

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