sym_hipd.c 141 KB

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