aic79xx_core.c 269 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924
  1. /*
  2. * Core routines and tables shareable across OS platforms.
  3. *
  4. * Copyright (c) 1994-2002 Justin T. Gibbs.
  5. * Copyright (c) 2000-2003 Adaptec Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions, and the following disclaimer,
  13. * without modification.
  14. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  15. * substantially similar to the "NO WARRANTY" disclaimer below
  16. * ("Disclaimer") and any redistribution must be conditioned upon
  17. * including a substantially similar Disclaimer requirement for further
  18. * binary redistribution.
  19. * 3. Neither the names of the above-listed copyright holders nor the names
  20. * of any contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * Alternatively, this software may be distributed under the terms of the
  24. * GNU General Public License ("GPL") version 2 as published by the Free
  25. * Software Foundation.
  26. *
  27. * NO WARRANTY
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGES.
  39. *
  40. * $Id: //depot/aic7xxx/aic7xxx/aic79xx.c#250 $
  41. */
  42. #ifdef __linux__
  43. #include "aic79xx_osm.h"
  44. #include "aic79xx_inline.h"
  45. #include "aicasm/aicasm_insformat.h"
  46. #else
  47. #include <dev/aic7xxx/aic79xx_osm.h>
  48. #include <dev/aic7xxx/aic79xx_inline.h>
  49. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  50. #endif
  51. /***************************** Lookup Tables **********************************/
  52. char *ahd_chip_names[] =
  53. {
  54. "NONE",
  55. "aic7901",
  56. "aic7902",
  57. "aic7901A"
  58. };
  59. static const u_int num_chip_names = NUM_ELEMENTS(ahd_chip_names);
  60. /*
  61. * Hardware error codes.
  62. */
  63. struct ahd_hard_error_entry {
  64. uint8_t errno;
  65. char *errmesg;
  66. };
  67. static struct ahd_hard_error_entry ahd_hard_errors[] = {
  68. { DSCTMOUT, "Discard Timer has timed out" },
  69. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  70. { SQPARERR, "Sequencer Parity Error" },
  71. { DPARERR, "Data-path Parity Error" },
  72. { MPARERR, "Scratch or SCB Memory Parity Error" },
  73. { CIOPARERR, "CIOBUS Parity Error" },
  74. };
  75. static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
  76. static struct ahd_phase_table_entry ahd_phase_table[] =
  77. {
  78. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  79. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  80. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  81. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  82. { P_COMMAND, MSG_NOOP, "in Command phase" },
  83. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  84. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  85. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  86. { P_BUSFREE, MSG_NOOP, "while idle" },
  87. { 0, MSG_NOOP, "in unknown phase" }
  88. };
  89. /*
  90. * In most cases we only wish to itterate over real phases, so
  91. * exclude the last element from the count.
  92. */
  93. static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
  94. /* Our Sequencer Program */
  95. #include "aic79xx_seq.h"
  96. /**************************** Function Declarations ***************************/
  97. static void ahd_handle_transmission_error(struct ahd_softc *ahd);
  98. static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
  99. u_int lqistat1);
  100. static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
  101. u_int busfreetime);
  102. static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
  103. static void ahd_handle_proto_violation(struct ahd_softc *ahd);
  104. static void ahd_force_renegotiation(struct ahd_softc *ahd,
  105. struct ahd_devinfo *devinfo);
  106. static struct ahd_tmode_tstate*
  107. ahd_alloc_tstate(struct ahd_softc *ahd,
  108. u_int scsi_id, char channel);
  109. #ifdef AHD_TARGET_MODE
  110. static void ahd_free_tstate(struct ahd_softc *ahd,
  111. u_int scsi_id, char channel, int force);
  112. #endif
  113. static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
  114. struct ahd_initiator_tinfo *,
  115. u_int *period,
  116. u_int *ppr_options,
  117. role_t role);
  118. static void ahd_update_neg_table(struct ahd_softc *ahd,
  119. struct ahd_devinfo *devinfo,
  120. struct ahd_transinfo *tinfo);
  121. static void ahd_update_pending_scbs(struct ahd_softc *ahd);
  122. static void ahd_fetch_devinfo(struct ahd_softc *ahd,
  123. struct ahd_devinfo *devinfo);
  124. static void ahd_scb_devinfo(struct ahd_softc *ahd,
  125. struct ahd_devinfo *devinfo,
  126. struct scb *scb);
  127. static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
  128. struct ahd_devinfo *devinfo,
  129. struct scb *scb);
  130. static void ahd_build_transfer_msg(struct ahd_softc *ahd,
  131. struct ahd_devinfo *devinfo);
  132. static void ahd_construct_sdtr(struct ahd_softc *ahd,
  133. struct ahd_devinfo *devinfo,
  134. u_int period, u_int offset);
  135. static void ahd_construct_wdtr(struct ahd_softc *ahd,
  136. struct ahd_devinfo *devinfo,
  137. u_int bus_width);
  138. static void ahd_construct_ppr(struct ahd_softc *ahd,
  139. struct ahd_devinfo *devinfo,
  140. u_int period, u_int offset,
  141. u_int bus_width, u_int ppr_options);
  142. static void ahd_clear_msg_state(struct ahd_softc *ahd);
  143. static void ahd_handle_message_phase(struct ahd_softc *ahd);
  144. typedef enum {
  145. AHDMSG_1B,
  146. AHDMSG_2B,
  147. AHDMSG_EXT
  148. } ahd_msgtype;
  149. static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
  150. u_int msgval, int full);
  151. static int ahd_parse_msg(struct ahd_softc *ahd,
  152. struct ahd_devinfo *devinfo);
  153. static int ahd_handle_msg_reject(struct ahd_softc *ahd,
  154. struct ahd_devinfo *devinfo);
  155. static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
  156. struct ahd_devinfo *devinfo);
  157. static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
  158. static void ahd_handle_devreset(struct ahd_softc *ahd,
  159. struct ahd_devinfo *devinfo,
  160. u_int lun, cam_status status,
  161. char *message, int verbose_level);
  162. #ifdef AHD_TARGET_MODE
  163. static void ahd_setup_target_msgin(struct ahd_softc *ahd,
  164. struct ahd_devinfo *devinfo,
  165. struct scb *scb);
  166. #endif
  167. static u_int ahd_sglist_size(struct ahd_softc *ahd);
  168. static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
  169. static bus_dmamap_callback_t
  170. ahd_dmamap_cb;
  171. static void ahd_initialize_hscbs(struct ahd_softc *ahd);
  172. static int ahd_init_scbdata(struct ahd_softc *ahd);
  173. static void ahd_fini_scbdata(struct ahd_softc *ahd);
  174. static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
  175. static void ahd_iocell_first_selection(struct ahd_softc *ahd);
  176. static void ahd_add_col_list(struct ahd_softc *ahd,
  177. struct scb *scb, u_int col_idx);
  178. static void ahd_rem_col_list(struct ahd_softc *ahd,
  179. struct scb *scb);
  180. static void ahd_chip_init(struct ahd_softc *ahd);
  181. static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
  182. struct scb *prev_scb,
  183. struct scb *scb);
  184. static int ahd_qinfifo_count(struct ahd_softc *ahd);
  185. static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
  186. char channel, int lun, u_int tag,
  187. role_t role, uint32_t status,
  188. ahd_search_action action,
  189. u_int *list_head, u_int *list_tail,
  190. u_int tid);
  191. static void ahd_stitch_tid_list(struct ahd_softc *ahd,
  192. u_int tid_prev, u_int tid_cur,
  193. u_int tid_next);
  194. static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
  195. u_int scbid);
  196. static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  197. u_int prev, u_int next, u_int tid);
  198. static void ahd_reset_current_bus(struct ahd_softc *ahd);
  199. static ahd_callback_t ahd_stat_timer;
  200. #ifdef AHD_DUMP_SEQ
  201. static void ahd_dumpseq(struct ahd_softc *ahd);
  202. #endif
  203. static void ahd_loadseq(struct ahd_softc *ahd);
  204. static int ahd_check_patch(struct ahd_softc *ahd,
  205. struct patch **start_patch,
  206. u_int start_instr, u_int *skip_addr);
  207. static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
  208. u_int address);
  209. static void ahd_download_instr(struct ahd_softc *ahd,
  210. u_int instrptr, uint8_t *dconsts);
  211. static int ahd_probe_stack_size(struct ahd_softc *ahd);
  212. static int ahd_scb_active_in_fifo(struct ahd_softc *ahd,
  213. struct scb *scb);
  214. static void ahd_run_data_fifo(struct ahd_softc *ahd,
  215. struct scb *scb);
  216. #ifdef AHD_TARGET_MODE
  217. static void ahd_queue_lstate_event(struct ahd_softc *ahd,
  218. struct ahd_tmode_lstate *lstate,
  219. u_int initiator_id,
  220. u_int event_type,
  221. u_int event_arg);
  222. static void ahd_update_scsiid(struct ahd_softc *ahd,
  223. u_int targid_mask);
  224. static int ahd_handle_target_cmd(struct ahd_softc *ahd,
  225. struct target_cmd *cmd);
  226. #endif
  227. /******************************** Private Inlines *****************************/
  228. static __inline void ahd_assert_atn(struct ahd_softc *ahd);
  229. static __inline int ahd_currently_packetized(struct ahd_softc *ahd);
  230. static __inline int ahd_set_active_fifo(struct ahd_softc *ahd);
  231. static __inline void
  232. ahd_assert_atn(struct ahd_softc *ahd)
  233. {
  234. ahd_outb(ahd, SCSISIGO, ATNO);
  235. }
  236. /*
  237. * Determine if the current connection has a packetized
  238. * agreement. This does not necessarily mean that we
  239. * are currently in a packetized transfer. We could
  240. * just as easily be sending or receiving a message.
  241. */
  242. static __inline int
  243. ahd_currently_packetized(struct ahd_softc *ahd)
  244. {
  245. ahd_mode_state saved_modes;
  246. int packetized;
  247. saved_modes = ahd_save_modes(ahd);
  248. if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
  249. /*
  250. * The packetized bit refers to the last
  251. * connection, not the current one. Check
  252. * for non-zero LQISTATE instead.
  253. */
  254. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  255. packetized = ahd_inb(ahd, LQISTATE) != 0;
  256. } else {
  257. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  258. packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
  259. }
  260. ahd_restore_modes(ahd, saved_modes);
  261. return (packetized);
  262. }
  263. static __inline int
  264. ahd_set_active_fifo(struct ahd_softc *ahd)
  265. {
  266. u_int active_fifo;
  267. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  268. active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  269. switch (active_fifo) {
  270. case 0:
  271. case 1:
  272. ahd_set_modes(ahd, active_fifo, active_fifo);
  273. return (1);
  274. default:
  275. return (0);
  276. }
  277. }
  278. /************************* Sequencer Execution Control ************************/
  279. /*
  280. * Restart the sequencer program from address zero
  281. */
  282. void
  283. ahd_restart(struct ahd_softc *ahd)
  284. {
  285. ahd_pause(ahd);
  286. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  287. /* No more pending messages */
  288. ahd_clear_msg_state(ahd);
  289. ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
  290. ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
  291. ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
  292. ahd_outb(ahd, SEQINTCTL, 0);
  293. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  294. ahd_outb(ahd, SEQ_FLAGS, 0);
  295. ahd_outb(ahd, SAVED_SCSIID, 0xFF);
  296. ahd_outb(ahd, SAVED_LUN, 0xFF);
  297. /*
  298. * Ensure that the sequencer's idea of TQINPOS
  299. * matches our own. The sequencer increments TQINPOS
  300. * only after it sees a DMA complete and a reset could
  301. * occur before the increment leaving the kernel to believe
  302. * the command arrived but the sequencer to not.
  303. */
  304. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  305. /* Always allow reselection */
  306. ahd_outb(ahd, SCSISEQ1,
  307. ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  308. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  309. /*
  310. * Clear any pending sequencer interrupt. It is no
  311. * longer relevant since we're resetting the Program
  312. * Counter.
  313. */
  314. ahd_outb(ahd, CLRINT, CLRSEQINT);
  315. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  316. ahd_unpause(ahd);
  317. }
  318. void
  319. ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
  320. {
  321. ahd_mode_state saved_modes;
  322. #ifdef AHD_DEBUG
  323. if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
  324. printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
  325. #endif
  326. saved_modes = ahd_save_modes(ahd);
  327. ahd_set_modes(ahd, fifo, fifo);
  328. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  329. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  330. ahd_outb(ahd, CCSGCTL, CCSGRESET);
  331. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  332. ahd_outb(ahd, SG_STATE, 0);
  333. ahd_restore_modes(ahd, saved_modes);
  334. }
  335. /************************* Input/Output Queues ********************************/
  336. /*
  337. * Flush and completed commands that are sitting in the command
  338. * complete queues down on the chip but have yet to be dma'ed back up.
  339. */
  340. void
  341. ahd_flush_qoutfifo(struct ahd_softc *ahd)
  342. {
  343. struct scb *scb;
  344. ahd_mode_state saved_modes;
  345. u_int saved_scbptr;
  346. u_int ccscbctl;
  347. u_int scbid;
  348. u_int next_scbid;
  349. saved_modes = ahd_save_modes(ahd);
  350. /*
  351. * Flush the good status FIFO for completed packetized commands.
  352. */
  353. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  354. saved_scbptr = ahd_get_scbptr(ahd);
  355. while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
  356. u_int fifo_mode;
  357. u_int i;
  358. scbid = ahd_inw(ahd, GSFIFO);
  359. scb = ahd_lookup_scb(ahd, scbid);
  360. if (scb == NULL) {
  361. printf("%s: Warning - GSFIFO SCB %d invalid\n",
  362. ahd_name(ahd), scbid);
  363. continue;
  364. }
  365. /*
  366. * Determine if this transaction is still active in
  367. * any FIFO. If it is, we must flush that FIFO to
  368. * the host before completing the command.
  369. */
  370. fifo_mode = 0;
  371. rescan_fifos:
  372. for (i = 0; i < 2; i++) {
  373. /* Toggle to the other mode. */
  374. fifo_mode ^= 1;
  375. ahd_set_modes(ahd, fifo_mode, fifo_mode);
  376. if (ahd_scb_active_in_fifo(ahd, scb) == 0)
  377. continue;
  378. ahd_run_data_fifo(ahd, scb);
  379. /*
  380. * Running this FIFO may cause a CFG4DATA for
  381. * this same transaction to assert in the other
  382. * FIFO or a new snapshot SAVEPTRS interrupt
  383. * in this FIFO. Even running a FIFO may not
  384. * clear the transaction if we are still waiting
  385. * for data to drain to the host. We must loop
  386. * until the transaction is not active in either
  387. * FIFO just to be sure. Reset our loop counter
  388. * so we will visit both FIFOs again before
  389. * declaring this transaction finished. We
  390. * also delay a bit so that status has a chance
  391. * to change before we look at this FIFO again.
  392. */
  393. ahd_delay(200);
  394. goto rescan_fifos;
  395. }
  396. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  397. ahd_set_scbptr(ahd, scbid);
  398. if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0
  399. && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0
  400. || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR)
  401. & SG_LIST_NULL) != 0)) {
  402. u_int comp_head;
  403. /*
  404. * The transfer completed with a residual.
  405. * Place this SCB on the complete DMA list
  406. * so that we update our in-core copy of the
  407. * SCB before completing the command.
  408. */
  409. ahd_outb(ahd, SCB_SCSI_STATUS, 0);
  410. ahd_outb(ahd, SCB_SGPTR,
  411. ahd_inb_scbram(ahd, SCB_SGPTR)
  412. | SG_STATUS_VALID);
  413. ahd_outw(ahd, SCB_TAG, scbid);
  414. ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL);
  415. comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  416. if (SCBID_IS_NULL(comp_head)) {
  417. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid);
  418. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
  419. } else {
  420. u_int tail;
  421. tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL);
  422. ahd_set_scbptr(ahd, tail);
  423. ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid);
  424. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
  425. ahd_set_scbptr(ahd, scbid);
  426. }
  427. } else
  428. ahd_complete_scb(ahd, scb);
  429. }
  430. ahd_set_scbptr(ahd, saved_scbptr);
  431. /*
  432. * Setup for command channel portion of flush.
  433. */
  434. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  435. /*
  436. * Wait for any inprogress DMA to complete and clear DMA state
  437. * if this if for an SCB in the qinfifo.
  438. */
  439. while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) {
  440. if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
  441. if ((ccscbctl & ARRDONE) != 0)
  442. break;
  443. } else if ((ccscbctl & CCSCBDONE) != 0)
  444. break;
  445. ahd_delay(200);
  446. }
  447. /*
  448. * We leave the sequencer to cleanup in the case of DMA's to
  449. * update the qoutfifo. In all other cases (DMA's to the
  450. * chip or a push of an SCB from the COMPLETE_DMA_SCB list),
  451. * we disable the DMA engine so that the sequencer will not
  452. * attempt to handle the DMA completion.
  453. */
  454. if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0)
  455. ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
  456. /*
  457. * Complete any SCBs that just finished
  458. * being DMA'ed into the qoutfifo.
  459. */
  460. ahd_run_qoutfifo(ahd);
  461. saved_scbptr = ahd_get_scbptr(ahd);
  462. /*
  463. * Manually update/complete any completed SCBs that are waiting to be
  464. * DMA'ed back up to the host.
  465. */
  466. scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  467. while (!SCBID_IS_NULL(scbid)) {
  468. uint8_t *hscb_ptr;
  469. u_int i;
  470. ahd_set_scbptr(ahd, scbid);
  471. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  472. scb = ahd_lookup_scb(ahd, scbid);
  473. if (scb == NULL) {
  474. printf("%s: Warning - DMA-up and complete "
  475. "SCB %d invalid\n", ahd_name(ahd), scbid);
  476. continue;
  477. }
  478. hscb_ptr = (uint8_t *)scb->hscb;
  479. for (i = 0; i < sizeof(struct hardware_scb); i++)
  480. *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
  481. ahd_complete_scb(ahd, scb);
  482. scbid = next_scbid;
  483. }
  484. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  485. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
  486. scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
  487. while (!SCBID_IS_NULL(scbid)) {
  488. ahd_set_scbptr(ahd, scbid);
  489. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  490. scb = ahd_lookup_scb(ahd, scbid);
  491. if (scb == NULL) {
  492. printf("%s: Warning - Complete Qfrz SCB %d invalid\n",
  493. ahd_name(ahd), scbid);
  494. continue;
  495. }
  496. ahd_complete_scb(ahd, scb);
  497. scbid = next_scbid;
  498. }
  499. ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
  500. scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  501. while (!SCBID_IS_NULL(scbid)) {
  502. ahd_set_scbptr(ahd, scbid);
  503. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  504. scb = ahd_lookup_scb(ahd, scbid);
  505. if (scb == NULL) {
  506. printf("%s: Warning - Complete SCB %d invalid\n",
  507. ahd_name(ahd), scbid);
  508. continue;
  509. }
  510. ahd_complete_scb(ahd, scb);
  511. scbid = next_scbid;
  512. }
  513. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  514. /*
  515. * Restore state.
  516. */
  517. ahd_set_scbptr(ahd, saved_scbptr);
  518. ahd_restore_modes(ahd, saved_modes);
  519. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  520. }
  521. /*
  522. * Determine if an SCB for a packetized transaction
  523. * is active in a FIFO.
  524. */
  525. static int
  526. ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb)
  527. {
  528. /*
  529. * The FIFO is only active for our transaction if
  530. * the SCBPTR matches the SCB's ID and the firmware
  531. * has installed a handler for the FIFO or we have
  532. * a pending SAVEPTRS or CFG4DATA interrupt.
  533. */
  534. if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb)
  535. || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0
  536. && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0))
  537. return (0);
  538. return (1);
  539. }
  540. /*
  541. * Run a data fifo to completion for a transaction we know
  542. * has completed across the SCSI bus (good status has been
  543. * received). We are already set to the correct FIFO mode
  544. * on entry to this routine.
  545. *
  546. * This function attempts to operate exactly as the firmware
  547. * would when running this FIFO. Care must be taken to update
  548. * this routine any time the firmware's FIFO algorithm is
  549. * changed.
  550. */
  551. static void
  552. ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
  553. {
  554. u_int seqintsrc;
  555. seqintsrc = ahd_inb(ahd, SEQINTSRC);
  556. if ((seqintsrc & CFG4DATA) != 0) {
  557. uint32_t datacnt;
  558. uint32_t sgptr;
  559. /*
  560. * Clear full residual flag.
  561. */
  562. sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
  563. ahd_outb(ahd, SCB_SGPTR, sgptr);
  564. /*
  565. * Load datacnt and address.
  566. */
  567. datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
  568. if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
  569. sgptr |= LAST_SEG;
  570. ahd_outb(ahd, SG_STATE, 0);
  571. } else
  572. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  573. ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
  574. ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
  575. ahd_outb(ahd, SG_CACHE_PRE, sgptr);
  576. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  577. /*
  578. * Initialize Residual Fields.
  579. */
  580. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
  581. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
  582. /*
  583. * Mark the SCB as having a FIFO in use.
  584. */
  585. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  586. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
  587. /*
  588. * Install a "fake" handler for this FIFO.
  589. */
  590. ahd_outw(ahd, LONGJMP_ADDR, 0);
  591. /*
  592. * Notify the hardware that we have satisfied
  593. * this sequencer interrupt.
  594. */
  595. ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
  596. } else if ((seqintsrc & SAVEPTRS) != 0) {
  597. uint32_t sgptr;
  598. uint32_t resid;
  599. if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
  600. /*
  601. * Snapshot Save Pointers. All that
  602. * is necessary to clear the snapshot
  603. * is a CLRCHN.
  604. */
  605. goto clrchn;
  606. }
  607. /*
  608. * Disable S/G fetch so the DMA engine
  609. * is available to future users.
  610. */
  611. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  612. ahd_outb(ahd, CCSGCTL, 0);
  613. ahd_outb(ahd, SG_STATE, 0);
  614. /*
  615. * Flush the data FIFO. Strickly only
  616. * necessary for Rev A parts.
  617. */
  618. ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
  619. /*
  620. * Calculate residual.
  621. */
  622. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  623. resid = ahd_inl(ahd, SHCNT);
  624. resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
  625. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
  626. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
  627. /*
  628. * Must back up to the correct S/G element.
  629. * Typically this just means resetting our
  630. * low byte to the offset in the SG_CACHE,
  631. * but if we wrapped, we have to correct
  632. * the other bytes of the sgptr too.
  633. */
  634. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
  635. && (sgptr & 0x80) == 0)
  636. sgptr -= 0x100;
  637. sgptr &= ~0xFF;
  638. sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
  639. & SG_ADDR_MASK;
  640. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  641. ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
  642. } else if ((resid & AHD_SG_LEN_MASK) == 0) {
  643. ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
  644. sgptr | SG_LIST_NULL);
  645. }
  646. /*
  647. * Save Pointers.
  648. */
  649. ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
  650. ahd_outl(ahd, SCB_DATACNT, resid);
  651. ahd_outl(ahd, SCB_SGPTR, sgptr);
  652. ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
  653. ahd_outb(ahd, SEQIMODE,
  654. ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
  655. /*
  656. * If the data is to the SCSI bus, we are
  657. * done, otherwise wait for FIFOEMP.
  658. */
  659. if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
  660. goto clrchn;
  661. } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
  662. uint32_t sgptr;
  663. uint64_t data_addr;
  664. uint32_t data_len;
  665. u_int dfcntrl;
  666. /*
  667. * Disable S/G fetch so the DMA engine
  668. * is available to future users. We won't
  669. * be using the DMA engine to load segments.
  670. */
  671. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
  672. ahd_outb(ahd, CCSGCTL, 0);
  673. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  674. }
  675. /*
  676. * Wait for the DMA engine to notice that the
  677. * host transfer is enabled and that there is
  678. * space in the S/G FIFO for new segments before
  679. * loading more segments.
  680. */
  681. if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0
  682. && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) {
  683. /*
  684. * Determine the offset of the next S/G
  685. * element to load.
  686. */
  687. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  688. sgptr &= SG_PTR_MASK;
  689. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  690. struct ahd_dma64_seg *sg;
  691. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  692. data_addr = sg->addr;
  693. data_len = sg->len;
  694. sgptr += sizeof(*sg);
  695. } else {
  696. struct ahd_dma_seg *sg;
  697. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  698. data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK;
  699. data_addr <<= 8;
  700. data_addr |= sg->addr;
  701. data_len = sg->len;
  702. sgptr += sizeof(*sg);
  703. }
  704. /*
  705. * Update residual information.
  706. */
  707. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24);
  708. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  709. /*
  710. * Load the S/G.
  711. */
  712. if (data_len & AHD_DMA_LAST_SEG) {
  713. sgptr |= LAST_SEG;
  714. ahd_outb(ahd, SG_STATE, 0);
  715. }
  716. ahd_outq(ahd, HADDR, data_addr);
  717. ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK);
  718. ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF);
  719. /*
  720. * Advertise the segment to the hardware.
  721. */
  722. dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
  723. if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
  724. /*
  725. * Use SCSIENWRDIS so that SCSIEN
  726. * is never modified by this
  727. * operation.
  728. */
  729. dfcntrl |= SCSIENWRDIS;
  730. }
  731. ahd_outb(ahd, DFCNTRL, dfcntrl);
  732. }
  733. } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) {
  734. /*
  735. * Transfer completed to the end of SG list
  736. * and has flushed to the host.
  737. */
  738. ahd_outb(ahd, SCB_SGPTR,
  739. ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
  740. goto clrchn;
  741. } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
  742. clrchn:
  743. /*
  744. * Clear any handler for this FIFO, decrement
  745. * the FIFO use count for the SCB, and release
  746. * the FIFO.
  747. */
  748. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  749. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  750. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
  751. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  752. }
  753. }
  754. /*
  755. * Look for entries in the QoutFIFO that have completed.
  756. * The valid_tag completion field indicates the validity
  757. * of the entry - the valid value toggles each time through
  758. * the queue. We use the sg_status field in the completion
  759. * entry to avoid referencing the hscb if the completion
  760. * occurred with no errors and no residual. sg_status is
  761. * a copy of the first byte (little endian) of the sgptr
  762. * hscb field.
  763. */
  764. void
  765. ahd_run_qoutfifo(struct ahd_softc *ahd)
  766. {
  767. struct ahd_completion *completion;
  768. struct scb *scb;
  769. u_int scb_index;
  770. if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
  771. panic("ahd_run_qoutfifo recursion");
  772. ahd->flags |= AHD_RUNNING_QOUTFIFO;
  773. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
  774. for (;;) {
  775. completion = &ahd->qoutfifo[ahd->qoutfifonext];
  776. if (completion->valid_tag != ahd->qoutfifonext_valid_tag)
  777. break;
  778. scb_index = ahd_le16toh(completion->tag);
  779. scb = ahd_lookup_scb(ahd, scb_index);
  780. if (scb == NULL) {
  781. printf("%s: WARNING no command for scb %d "
  782. "(cmdcmplt)\nQOUTPOS = %d\n",
  783. ahd_name(ahd), scb_index,
  784. ahd->qoutfifonext);
  785. ahd_dump_card_state(ahd);
  786. } else if ((completion->sg_status & SG_STATUS_VALID) != 0) {
  787. ahd_handle_scb_status(ahd, scb);
  788. } else {
  789. ahd_done(ahd, scb);
  790. }
  791. ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
  792. if (ahd->qoutfifonext == 0)
  793. ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID;
  794. }
  795. ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
  796. }
  797. /************************* Interrupt Handling *********************************/
  798. void
  799. ahd_handle_hwerrint(struct ahd_softc *ahd)
  800. {
  801. /*
  802. * Some catastrophic hardware error has occurred.
  803. * Print it for the user and disable the controller.
  804. */
  805. int i;
  806. int error;
  807. error = ahd_inb(ahd, ERROR);
  808. for (i = 0; i < num_errors; i++) {
  809. if ((error & ahd_hard_errors[i].errno) != 0)
  810. printf("%s: hwerrint, %s\n",
  811. ahd_name(ahd), ahd_hard_errors[i].errmesg);
  812. }
  813. ahd_dump_card_state(ahd);
  814. panic("BRKADRINT");
  815. /* Tell everyone that this HBA is no longer available */
  816. ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  817. CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  818. CAM_NO_HBA);
  819. /* Tell the system that this controller has gone away. */
  820. ahd_free(ahd);
  821. }
  822. void
  823. ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
  824. {
  825. u_int seqintcode;
  826. /*
  827. * Save the sequencer interrupt code and clear the SEQINT
  828. * bit. We will unpause the sequencer, if appropriate,
  829. * after servicing the request.
  830. */
  831. seqintcode = ahd_inb(ahd, SEQINTCODE);
  832. ahd_outb(ahd, CLRINT, CLRSEQINT);
  833. if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
  834. /*
  835. * Unpause the sequencer and let it clear
  836. * SEQINT by writing NO_SEQINT to it. This
  837. * will cause the sequencer to be paused again,
  838. * which is the expected state of this routine.
  839. */
  840. ahd_unpause(ahd);
  841. while (!ahd_is_paused(ahd))
  842. ;
  843. ahd_outb(ahd, CLRINT, CLRSEQINT);
  844. }
  845. ahd_update_modes(ahd);
  846. #ifdef AHD_DEBUG
  847. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  848. printf("%s: Handle Seqint Called for code %d\n",
  849. ahd_name(ahd), seqintcode);
  850. #endif
  851. switch (seqintcode) {
  852. case ENTERING_NONPACK:
  853. {
  854. struct scb *scb;
  855. u_int scbid;
  856. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  857. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  858. scbid = ahd_get_scbptr(ahd);
  859. scb = ahd_lookup_scb(ahd, scbid);
  860. if (scb == NULL) {
  861. /*
  862. * Somehow need to know if this
  863. * is from a selection or reselection.
  864. * From that, we can determine target
  865. * ID so we at least have an I_T nexus.
  866. */
  867. } else {
  868. ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
  869. ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
  870. ahd_outb(ahd, SEQ_FLAGS, 0x0);
  871. }
  872. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
  873. && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  874. /*
  875. * Phase change after read stream with
  876. * CRC error with P0 asserted on last
  877. * packet.
  878. */
  879. #ifdef AHD_DEBUG
  880. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  881. printf("%s: Assuming LQIPHASE_NLQ with "
  882. "P0 assertion\n", ahd_name(ahd));
  883. #endif
  884. }
  885. #ifdef AHD_DEBUG
  886. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  887. printf("%s: Entering NONPACK\n", ahd_name(ahd));
  888. #endif
  889. break;
  890. }
  891. case INVALID_SEQINT:
  892. printf("%s: Invalid Sequencer interrupt occurred, "
  893. "resetting channel.\n",
  894. ahd_name(ahd));
  895. #ifdef AHD_DEBUG
  896. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  897. ahd_dump_card_state(ahd);
  898. #endif
  899. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  900. break;
  901. case STATUS_OVERRUN:
  902. {
  903. struct scb *scb;
  904. u_int scbid;
  905. scbid = ahd_get_scbptr(ahd);
  906. scb = ahd_lookup_scb(ahd, scbid);
  907. if (scb != NULL)
  908. ahd_print_path(ahd, scb);
  909. else
  910. printf("%s: ", ahd_name(ahd));
  911. printf("SCB %d Packetized Status Overrun", scbid);
  912. ahd_dump_card_state(ahd);
  913. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  914. break;
  915. }
  916. case CFG4ISTAT_INTR:
  917. {
  918. struct scb *scb;
  919. u_int scbid;
  920. scbid = ahd_get_scbptr(ahd);
  921. scb = ahd_lookup_scb(ahd, scbid);
  922. if (scb == NULL) {
  923. ahd_dump_card_state(ahd);
  924. printf("CFG4ISTAT: Free SCB %d referenced", scbid);
  925. panic("For safety");
  926. }
  927. ahd_outq(ahd, HADDR, scb->sense_busaddr);
  928. ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
  929. ahd_outb(ahd, HCNT + 2, 0);
  930. ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
  931. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  932. break;
  933. }
  934. case ILLEGAL_PHASE:
  935. {
  936. u_int bus_phase;
  937. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  938. printf("%s: ILLEGAL_PHASE 0x%x\n",
  939. ahd_name(ahd), bus_phase);
  940. switch (bus_phase) {
  941. case P_DATAOUT:
  942. case P_DATAIN:
  943. case P_DATAOUT_DT:
  944. case P_DATAIN_DT:
  945. case P_MESGOUT:
  946. case P_STATUS:
  947. case P_MESGIN:
  948. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  949. printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
  950. break;
  951. case P_COMMAND:
  952. {
  953. struct ahd_devinfo devinfo;
  954. struct scb *scb;
  955. struct ahd_initiator_tinfo *targ_info;
  956. struct ahd_tmode_tstate *tstate;
  957. struct ahd_transinfo *tinfo;
  958. u_int scbid;
  959. /*
  960. * If a target takes us into the command phase
  961. * assume that it has been externally reset and
  962. * has thus lost our previous packetized negotiation
  963. * agreement.
  964. * Revert to async/narrow transfers until we
  965. * can renegotiate with the device and notify
  966. * the OSM about the reset.
  967. */
  968. scbid = ahd_get_scbptr(ahd);
  969. scb = ahd_lookup_scb(ahd, scbid);
  970. if (scb == NULL) {
  971. printf("Invalid phase with no valid SCB. "
  972. "Resetting bus.\n");
  973. ahd_reset_channel(ahd, 'A',
  974. /*Initiate Reset*/TRUE);
  975. break;
  976. }
  977. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  978. SCB_GET_TARGET(ahd, scb),
  979. SCB_GET_LUN(scb),
  980. SCB_GET_CHANNEL(ahd, scb),
  981. ROLE_INITIATOR);
  982. targ_info = ahd_fetch_transinfo(ahd,
  983. devinfo.channel,
  984. devinfo.our_scsiid,
  985. devinfo.target,
  986. &tstate);
  987. tinfo = &targ_info->curr;
  988. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  989. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  990. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  991. /*offset*/0, /*ppr_options*/0,
  992. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  993. scb->flags |= SCB_EXTERNAL_RESET;
  994. ahd_freeze_devq(ahd, scb);
  995. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  996. ahd_freeze_scb(scb);
  997. /* Notify XPT */
  998. ahd_send_async(ahd, devinfo.channel, devinfo.target,
  999. CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
  1000. /*
  1001. * Allow the sequencer to continue with
  1002. * non-pack processing.
  1003. */
  1004. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1005. ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
  1006. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  1007. ahd_outb(ahd, CLRLQOINT1, 0);
  1008. }
  1009. #ifdef AHD_DEBUG
  1010. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1011. ahd_print_path(ahd, scb);
  1012. printf("Unexpected command phase from "
  1013. "packetized target\n");
  1014. }
  1015. #endif
  1016. break;
  1017. }
  1018. }
  1019. break;
  1020. }
  1021. case CFG4OVERRUN:
  1022. {
  1023. struct scb *scb;
  1024. u_int scb_index;
  1025. #ifdef AHD_DEBUG
  1026. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1027. printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
  1028. ahd_inb(ahd, MODE_PTR));
  1029. }
  1030. #endif
  1031. scb_index = ahd_get_scbptr(ahd);
  1032. scb = ahd_lookup_scb(ahd, scb_index);
  1033. if (scb == NULL) {
  1034. /*
  1035. * Attempt to transfer to an SCB that is
  1036. * not outstanding.
  1037. */
  1038. ahd_assert_atn(ahd);
  1039. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1040. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  1041. ahd->msgout_len = 1;
  1042. ahd->msgout_index = 0;
  1043. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1044. /*
  1045. * Clear status received flag to prevent any
  1046. * attempt to complete this bogus SCB.
  1047. */
  1048. ahd_outb(ahd, SCB_CONTROL,
  1049. ahd_inb_scbram(ahd, SCB_CONTROL)
  1050. & ~STATUS_RCVD);
  1051. }
  1052. break;
  1053. }
  1054. case DUMP_CARD_STATE:
  1055. {
  1056. ahd_dump_card_state(ahd);
  1057. break;
  1058. }
  1059. case PDATA_REINIT:
  1060. {
  1061. #ifdef AHD_DEBUG
  1062. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1063. printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
  1064. "SG_CACHE_SHADOW = 0x%x\n",
  1065. ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
  1066. ahd_inb(ahd, SG_CACHE_SHADOW));
  1067. }
  1068. #endif
  1069. ahd_reinitialize_dataptrs(ahd);
  1070. break;
  1071. }
  1072. case HOST_MSG_LOOP:
  1073. {
  1074. struct ahd_devinfo devinfo;
  1075. /*
  1076. * The sequencer has encountered a message phase
  1077. * that requires host assistance for completion.
  1078. * While handling the message phase(s), we will be
  1079. * notified by the sequencer after each byte is
  1080. * transfered so we can track bus phase changes.
  1081. *
  1082. * If this is the first time we've seen a HOST_MSG_LOOP
  1083. * interrupt, initialize the state of the host message
  1084. * loop.
  1085. */
  1086. ahd_fetch_devinfo(ahd, &devinfo);
  1087. if (ahd->msg_type == MSG_TYPE_NONE) {
  1088. struct scb *scb;
  1089. u_int scb_index;
  1090. u_int bus_phase;
  1091. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1092. if (bus_phase != P_MESGIN
  1093. && bus_phase != P_MESGOUT) {
  1094. printf("ahd_intr: HOST_MSG_LOOP bad "
  1095. "phase 0x%x\n", bus_phase);
  1096. /*
  1097. * Probably transitioned to bus free before
  1098. * we got here. Just punt the message.
  1099. */
  1100. ahd_dump_card_state(ahd);
  1101. ahd_clear_intstat(ahd);
  1102. ahd_restart(ahd);
  1103. return;
  1104. }
  1105. scb_index = ahd_get_scbptr(ahd);
  1106. scb = ahd_lookup_scb(ahd, scb_index);
  1107. if (devinfo.role == ROLE_INITIATOR) {
  1108. if (bus_phase == P_MESGOUT)
  1109. ahd_setup_initiator_msgout(ahd,
  1110. &devinfo,
  1111. scb);
  1112. else {
  1113. ahd->msg_type =
  1114. MSG_TYPE_INITIATOR_MSGIN;
  1115. ahd->msgin_index = 0;
  1116. }
  1117. }
  1118. #ifdef AHD_TARGET_MODE
  1119. else {
  1120. if (bus_phase == P_MESGOUT) {
  1121. ahd->msg_type =
  1122. MSG_TYPE_TARGET_MSGOUT;
  1123. ahd->msgin_index = 0;
  1124. }
  1125. else
  1126. ahd_setup_target_msgin(ahd,
  1127. &devinfo,
  1128. scb);
  1129. }
  1130. #endif
  1131. }
  1132. ahd_handle_message_phase(ahd);
  1133. break;
  1134. }
  1135. case NO_MATCH:
  1136. {
  1137. /* Ensure we don't leave the selection hardware on */
  1138. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  1139. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1140. printf("%s:%c:%d: no active SCB for reconnecting "
  1141. "target - issuing BUS DEVICE RESET\n",
  1142. ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
  1143. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  1144. "REG0 == 0x%x ACCUM = 0x%x\n",
  1145. ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
  1146. ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
  1147. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  1148. "SINDEX == 0x%x\n",
  1149. ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
  1150. ahd_find_busy_tcl(ahd,
  1151. BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
  1152. ahd_inb(ahd, SAVED_LUN))),
  1153. ahd_inw(ahd, SINDEX));
  1154. printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  1155. "SCB_CONTROL == 0x%x\n",
  1156. ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
  1157. ahd_inb_scbram(ahd, SCB_LUN),
  1158. ahd_inb_scbram(ahd, SCB_CONTROL));
  1159. printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
  1160. ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
  1161. printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
  1162. printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
  1163. ahd_dump_card_state(ahd);
  1164. ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
  1165. ahd->msgout_len = 1;
  1166. ahd->msgout_index = 0;
  1167. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1168. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1169. ahd_assert_atn(ahd);
  1170. break;
  1171. }
  1172. case PROTO_VIOLATION:
  1173. {
  1174. ahd_handle_proto_violation(ahd);
  1175. break;
  1176. }
  1177. case IGN_WIDE_RES:
  1178. {
  1179. struct ahd_devinfo devinfo;
  1180. ahd_fetch_devinfo(ahd, &devinfo);
  1181. ahd_handle_ign_wide_residue(ahd, &devinfo);
  1182. break;
  1183. }
  1184. case BAD_PHASE:
  1185. {
  1186. u_int lastphase;
  1187. lastphase = ahd_inb(ahd, LASTPHASE);
  1188. printf("%s:%c:%d: unknown scsi bus phase %x, "
  1189. "lastphase = 0x%x. Attempting to continue\n",
  1190. ahd_name(ahd), 'A',
  1191. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1192. lastphase, ahd_inb(ahd, SCSISIGI));
  1193. break;
  1194. }
  1195. case MISSED_BUSFREE:
  1196. {
  1197. u_int lastphase;
  1198. lastphase = ahd_inb(ahd, LASTPHASE);
  1199. printf("%s:%c:%d: Missed busfree. "
  1200. "Lastphase = 0x%x, Curphase = 0x%x\n",
  1201. ahd_name(ahd), 'A',
  1202. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1203. lastphase, ahd_inb(ahd, SCSISIGI));
  1204. ahd_restart(ahd);
  1205. return;
  1206. }
  1207. case DATA_OVERRUN:
  1208. {
  1209. /*
  1210. * When the sequencer detects an overrun, it
  1211. * places the controller in "BITBUCKET" mode
  1212. * and allows the target to complete its transfer.
  1213. * Unfortunately, none of the counters get updated
  1214. * when the controller is in this mode, so we have
  1215. * no way of knowing how large the overrun was.
  1216. */
  1217. struct scb *scb;
  1218. u_int scbindex;
  1219. #ifdef AHD_DEBUG
  1220. u_int lastphase;
  1221. #endif
  1222. scbindex = ahd_get_scbptr(ahd);
  1223. scb = ahd_lookup_scb(ahd, scbindex);
  1224. #ifdef AHD_DEBUG
  1225. lastphase = ahd_inb(ahd, LASTPHASE);
  1226. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1227. ahd_print_path(ahd, scb);
  1228. printf("data overrun detected %s. Tag == 0x%x.\n",
  1229. ahd_lookup_phase_entry(lastphase)->phasemsg,
  1230. SCB_GET_TAG(scb));
  1231. ahd_print_path(ahd, scb);
  1232. printf("%s seen Data Phase. Length = %ld. "
  1233. "NumSGs = %d.\n",
  1234. ahd_inb(ahd, SEQ_FLAGS) & DPHASE
  1235. ? "Have" : "Haven't",
  1236. ahd_get_transfer_length(scb), scb->sg_count);
  1237. ahd_dump_sglist(scb);
  1238. }
  1239. #endif
  1240. /*
  1241. * Set this and it will take effect when the
  1242. * target does a command complete.
  1243. */
  1244. ahd_freeze_devq(ahd, scb);
  1245. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1246. ahd_freeze_scb(scb);
  1247. break;
  1248. }
  1249. case MKMSG_FAILED:
  1250. {
  1251. struct ahd_devinfo devinfo;
  1252. struct scb *scb;
  1253. u_int scbid;
  1254. ahd_fetch_devinfo(ahd, &devinfo);
  1255. printf("%s:%c:%d:%d: Attempt to issue message failed\n",
  1256. ahd_name(ahd), devinfo.channel, devinfo.target,
  1257. devinfo.lun);
  1258. scbid = ahd_get_scbptr(ahd);
  1259. scb = ahd_lookup_scb(ahd, scbid);
  1260. if (scb != NULL
  1261. && (scb->flags & SCB_RECOVERY_SCB) != 0)
  1262. /*
  1263. * Ensure that we didn't put a second instance of this
  1264. * SCB into the QINFIFO.
  1265. */
  1266. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1267. SCB_GET_CHANNEL(ahd, scb),
  1268. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1269. ROLE_INITIATOR, /*status*/0,
  1270. SEARCH_REMOVE);
  1271. ahd_outb(ahd, SCB_CONTROL,
  1272. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  1273. break;
  1274. }
  1275. case TASKMGMT_FUNC_COMPLETE:
  1276. {
  1277. u_int scbid;
  1278. struct scb *scb;
  1279. scbid = ahd_get_scbptr(ahd);
  1280. scb = ahd_lookup_scb(ahd, scbid);
  1281. if (scb != NULL) {
  1282. u_int lun;
  1283. u_int tag;
  1284. cam_status error;
  1285. ahd_print_path(ahd, scb);
  1286. printf("Task Management Func 0x%x Complete\n",
  1287. scb->hscb->task_management);
  1288. lun = CAM_LUN_WILDCARD;
  1289. tag = SCB_LIST_NULL;
  1290. switch (scb->hscb->task_management) {
  1291. case SIU_TASKMGMT_ABORT_TASK:
  1292. tag = SCB_GET_TAG(scb);
  1293. case SIU_TASKMGMT_ABORT_TASK_SET:
  1294. case SIU_TASKMGMT_CLEAR_TASK_SET:
  1295. lun = scb->hscb->lun;
  1296. error = CAM_REQ_ABORTED;
  1297. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  1298. 'A', lun, tag, ROLE_INITIATOR,
  1299. error);
  1300. break;
  1301. case SIU_TASKMGMT_LUN_RESET:
  1302. lun = scb->hscb->lun;
  1303. case SIU_TASKMGMT_TARGET_RESET:
  1304. {
  1305. struct ahd_devinfo devinfo;
  1306. ahd_scb_devinfo(ahd, &devinfo, scb);
  1307. error = CAM_BDR_SENT;
  1308. ahd_handle_devreset(ahd, &devinfo, lun,
  1309. CAM_BDR_SENT,
  1310. lun != CAM_LUN_WILDCARD
  1311. ? "Lun Reset"
  1312. : "Target Reset",
  1313. /*verbose_level*/0);
  1314. break;
  1315. }
  1316. default:
  1317. panic("Unexpected TaskMgmt Func\n");
  1318. break;
  1319. }
  1320. }
  1321. break;
  1322. }
  1323. case TASKMGMT_CMD_CMPLT_OKAY:
  1324. {
  1325. u_int scbid;
  1326. struct scb *scb;
  1327. /*
  1328. * An ABORT TASK TMF failed to be delivered before
  1329. * the targeted command completed normally.
  1330. */
  1331. scbid = ahd_get_scbptr(ahd);
  1332. scb = ahd_lookup_scb(ahd, scbid);
  1333. if (scb != NULL) {
  1334. /*
  1335. * Remove the second instance of this SCB from
  1336. * the QINFIFO if it is still there.
  1337. */
  1338. ahd_print_path(ahd, scb);
  1339. printf("SCB completes before TMF\n");
  1340. /*
  1341. * Handle losing the race. Wait until any
  1342. * current selection completes. We will then
  1343. * set the TMF back to zero in this SCB so that
  1344. * the sequencer doesn't bother to issue another
  1345. * sequencer interrupt for its completion.
  1346. */
  1347. while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  1348. && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
  1349. && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
  1350. ;
  1351. ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
  1352. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1353. SCB_GET_CHANNEL(ahd, scb),
  1354. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1355. ROLE_INITIATOR, /*status*/0,
  1356. SEARCH_REMOVE);
  1357. }
  1358. break;
  1359. }
  1360. case TRACEPOINT0:
  1361. case TRACEPOINT1:
  1362. case TRACEPOINT2:
  1363. case TRACEPOINT3:
  1364. printf("%s: Tracepoint %d\n", ahd_name(ahd),
  1365. seqintcode - TRACEPOINT0);
  1366. break;
  1367. case NO_SEQINT:
  1368. break;
  1369. case SAW_HWERR:
  1370. ahd_handle_hwerrint(ahd);
  1371. break;
  1372. default:
  1373. printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
  1374. seqintcode);
  1375. break;
  1376. }
  1377. /*
  1378. * The sequencer is paused immediately on
  1379. * a SEQINT, so we should restart it when
  1380. * we're done.
  1381. */
  1382. ahd_unpause(ahd);
  1383. }
  1384. void
  1385. ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
  1386. {
  1387. struct scb *scb;
  1388. u_int status0;
  1389. u_int status3;
  1390. u_int status;
  1391. u_int lqistat1;
  1392. u_int lqostat0;
  1393. u_int scbid;
  1394. u_int busfreetime;
  1395. ahd_update_modes(ahd);
  1396. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1397. status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
  1398. status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
  1399. status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  1400. lqistat1 = ahd_inb(ahd, LQISTAT1);
  1401. lqostat0 = ahd_inb(ahd, LQOSTAT0);
  1402. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1403. /*
  1404. * Ignore external resets after a bus reset.
  1405. */
  1406. if (((status & SCSIRSTI) != 0) && (ahd->flags & AHD_BUS_RESET_ACTIVE))
  1407. return;
  1408. /*
  1409. * Clear bus reset flag
  1410. */
  1411. ahd->flags &= ~AHD_BUS_RESET_ACTIVE;
  1412. if ((status0 & (SELDI|SELDO)) != 0) {
  1413. u_int simode0;
  1414. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1415. simode0 = ahd_inb(ahd, SIMODE0);
  1416. status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
  1417. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1418. }
  1419. scbid = ahd_get_scbptr(ahd);
  1420. scb = ahd_lookup_scb(ahd, scbid);
  1421. if (scb != NULL
  1422. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  1423. scb = NULL;
  1424. if ((status0 & IOERR) != 0) {
  1425. u_int now_lvd;
  1426. now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
  1427. printf("%s: Transceiver State Has Changed to %s mode\n",
  1428. ahd_name(ahd), now_lvd ? "LVD" : "SE");
  1429. ahd_outb(ahd, CLRSINT0, CLRIOERR);
  1430. /*
  1431. * A change in I/O mode is equivalent to a bus reset.
  1432. */
  1433. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1434. ahd_pause(ahd);
  1435. ahd_setup_iocell_workaround(ahd);
  1436. ahd_unpause(ahd);
  1437. } else if ((status0 & OVERRUN) != 0) {
  1438. printf("%s: SCSI offset overrun detected. Resetting bus.\n",
  1439. ahd_name(ahd));
  1440. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1441. } else if ((status & SCSIRSTI) != 0) {
  1442. printf("%s: Someone reset channel A\n", ahd_name(ahd));
  1443. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
  1444. } else if ((status & SCSIPERR) != 0) {
  1445. /* Make sure the sequencer is in a safe location. */
  1446. ahd_clear_critical_section(ahd);
  1447. ahd_handle_transmission_error(ahd);
  1448. } else if (lqostat0 != 0) {
  1449. printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
  1450. ahd_outb(ahd, CLRLQOINT0, lqostat0);
  1451. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
  1452. ahd_outb(ahd, CLRLQOINT1, 0);
  1453. } else if ((status & SELTO) != 0) {
  1454. u_int scbid;
  1455. /* Stop the selection */
  1456. ahd_outb(ahd, SCSISEQ0, 0);
  1457. /* Make sure the sequencer is in a safe location. */
  1458. ahd_clear_critical_section(ahd);
  1459. /* No more pending messages */
  1460. ahd_clear_msg_state(ahd);
  1461. /* Clear interrupt state */
  1462. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1463. /*
  1464. * Although the driver does not care about the
  1465. * 'Selection in Progress' status bit, the busy
  1466. * LED does. SELINGO is only cleared by a sucessfull
  1467. * selection, so we must manually clear it to insure
  1468. * the LED turns off just incase no future successful
  1469. * selections occur (e.g. no devices on the bus).
  1470. */
  1471. ahd_outb(ahd, CLRSINT0, CLRSELINGO);
  1472. scbid = ahd_inw(ahd, WAITING_TID_HEAD);
  1473. scb = ahd_lookup_scb(ahd, scbid);
  1474. if (scb == NULL) {
  1475. printf("%s: ahd_intr - referenced scb not "
  1476. "valid during SELTO scb(0x%x)\n",
  1477. ahd_name(ahd), scbid);
  1478. ahd_dump_card_state(ahd);
  1479. } else {
  1480. struct ahd_devinfo devinfo;
  1481. #ifdef AHD_DEBUG
  1482. if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
  1483. ahd_print_path(ahd, scb);
  1484. printf("Saw Selection Timeout for SCB 0x%x\n",
  1485. scbid);
  1486. }
  1487. #endif
  1488. ahd_scb_devinfo(ahd, &devinfo, scb);
  1489. ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1490. ahd_freeze_devq(ahd, scb);
  1491. /*
  1492. * Cancel any pending transactions on the device
  1493. * now that it seems to be missing. This will
  1494. * also revert us to async/narrow transfers until
  1495. * we can renegotiate with the device.
  1496. */
  1497. ahd_handle_devreset(ahd, &devinfo,
  1498. CAM_LUN_WILDCARD,
  1499. CAM_SEL_TIMEOUT,
  1500. "Selection Timeout",
  1501. /*verbose_level*/1);
  1502. }
  1503. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1504. ahd_iocell_first_selection(ahd);
  1505. ahd_unpause(ahd);
  1506. } else if ((status0 & (SELDI|SELDO)) != 0) {
  1507. ahd_iocell_first_selection(ahd);
  1508. ahd_unpause(ahd);
  1509. } else if (status3 != 0) {
  1510. printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
  1511. ahd_name(ahd), status3);
  1512. ahd_outb(ahd, CLRSINT3, status3);
  1513. } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
  1514. /* Make sure the sequencer is in a safe location. */
  1515. ahd_clear_critical_section(ahd);
  1516. ahd_handle_lqiphase_error(ahd, lqistat1);
  1517. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1518. /*
  1519. * This status can be delayed during some
  1520. * streaming operations. The SCSIPHASE
  1521. * handler has already dealt with this case
  1522. * so just clear the error.
  1523. */
  1524. ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
  1525. } else if ((status & BUSFREE) != 0
  1526. || (lqistat1 & LQOBUSFREE) != 0) {
  1527. u_int lqostat1;
  1528. int restart;
  1529. int clear_fifo;
  1530. int packetized;
  1531. u_int mode;
  1532. /*
  1533. * Clear our selection hardware as soon as possible.
  1534. * We may have an entry in the waiting Q for this target,
  1535. * that is affected by this busfree and we don't want to
  1536. * go about selecting the target while we handle the event.
  1537. */
  1538. ahd_outb(ahd, SCSISEQ0, 0);
  1539. /* Make sure the sequencer is in a safe location. */
  1540. ahd_clear_critical_section(ahd);
  1541. /*
  1542. * Determine what we were up to at the time of
  1543. * the busfree.
  1544. */
  1545. mode = AHD_MODE_SCSI;
  1546. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1547. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1548. switch (busfreetime) {
  1549. case BUSFREE_DFF0:
  1550. case BUSFREE_DFF1:
  1551. {
  1552. u_int scbid;
  1553. struct scb *scb;
  1554. mode = busfreetime == BUSFREE_DFF0
  1555. ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
  1556. ahd_set_modes(ahd, mode, mode);
  1557. scbid = ahd_get_scbptr(ahd);
  1558. scb = ahd_lookup_scb(ahd, scbid);
  1559. if (scb == NULL) {
  1560. printf("%s: Invalid SCB %d in DFF%d "
  1561. "during unexpected busfree\n",
  1562. ahd_name(ahd), scbid, mode);
  1563. packetized = 0;
  1564. } else
  1565. packetized = (scb->flags & SCB_PACKETIZED) != 0;
  1566. clear_fifo = 1;
  1567. break;
  1568. }
  1569. case BUSFREE_LQO:
  1570. clear_fifo = 0;
  1571. packetized = 1;
  1572. break;
  1573. default:
  1574. clear_fifo = 0;
  1575. packetized = (lqostat1 & LQOBUSFREE) != 0;
  1576. if (!packetized
  1577. && ahd_inb(ahd, LASTPHASE) == P_BUSFREE
  1578. && (ahd_inb(ahd, SSTAT0) & SELDI) == 0
  1579. && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0
  1580. || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0))
  1581. /*
  1582. * Assume packetized if we are not
  1583. * on the bus in a non-packetized
  1584. * capacity and any pending selection
  1585. * was a packetized selection.
  1586. */
  1587. packetized = 1;
  1588. break;
  1589. }
  1590. #ifdef AHD_DEBUG
  1591. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  1592. printf("Saw Busfree. Busfreetime = 0x%x.\n",
  1593. busfreetime);
  1594. #endif
  1595. /*
  1596. * Busfrees that occur in non-packetized phases are
  1597. * handled by the nonpkt_busfree handler.
  1598. */
  1599. if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
  1600. restart = ahd_handle_pkt_busfree(ahd, busfreetime);
  1601. } else {
  1602. packetized = 0;
  1603. restart = ahd_handle_nonpkt_busfree(ahd);
  1604. }
  1605. /*
  1606. * Clear the busfree interrupt status. The setting of
  1607. * the interrupt is a pulse, so in a perfect world, we
  1608. * would not need to muck with the ENBUSFREE logic. This
  1609. * would ensure that if the bus moves on to another
  1610. * connection, busfree protection is still in force. If
  1611. * BUSFREEREV is broken, however, we must manually clear
  1612. * the ENBUSFREE if the busfree occurred during a non-pack
  1613. * connection so that we don't get false positives during
  1614. * future, packetized, connections.
  1615. */
  1616. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  1617. if (packetized == 0
  1618. && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
  1619. ahd_outb(ahd, SIMODE1,
  1620. ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
  1621. if (clear_fifo)
  1622. ahd_clear_fifo(ahd, mode);
  1623. ahd_clear_msg_state(ahd);
  1624. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1625. if (restart) {
  1626. ahd_restart(ahd);
  1627. } else {
  1628. ahd_unpause(ahd);
  1629. }
  1630. } else {
  1631. printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
  1632. ahd_name(ahd), status);
  1633. ahd_dump_card_state(ahd);
  1634. ahd_clear_intstat(ahd);
  1635. ahd_unpause(ahd);
  1636. }
  1637. }
  1638. static void
  1639. ahd_handle_transmission_error(struct ahd_softc *ahd)
  1640. {
  1641. struct scb *scb;
  1642. u_int scbid;
  1643. u_int lqistat1;
  1644. u_int lqistat2;
  1645. u_int msg_out;
  1646. u_int curphase;
  1647. u_int lastphase;
  1648. u_int perrdiag;
  1649. u_int cur_col;
  1650. int silent;
  1651. scb = NULL;
  1652. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1653. lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
  1654. lqistat2 = ahd_inb(ahd, LQISTAT2);
  1655. if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
  1656. && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
  1657. u_int lqistate;
  1658. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1659. lqistate = ahd_inb(ahd, LQISTATE);
  1660. if ((lqistate >= 0x1E && lqistate <= 0x24)
  1661. || (lqistate == 0x29)) {
  1662. #ifdef AHD_DEBUG
  1663. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1664. printf("%s: NLQCRC found via LQISTATE\n",
  1665. ahd_name(ahd));
  1666. }
  1667. #endif
  1668. lqistat1 |= LQICRCI_NLQ;
  1669. }
  1670. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1671. }
  1672. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1673. lastphase = ahd_inb(ahd, LASTPHASE);
  1674. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1675. perrdiag = ahd_inb(ahd, PERRDIAG);
  1676. msg_out = MSG_INITIATOR_DET_ERR;
  1677. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
  1678. /*
  1679. * Try to find the SCB associated with this error.
  1680. */
  1681. silent = FALSE;
  1682. if (lqistat1 == 0
  1683. || (lqistat1 & LQICRCI_NLQ) != 0) {
  1684. if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
  1685. ahd_set_active_fifo(ahd);
  1686. scbid = ahd_get_scbptr(ahd);
  1687. scb = ahd_lookup_scb(ahd, scbid);
  1688. if (scb != NULL && SCB_IS_SILENT(scb))
  1689. silent = TRUE;
  1690. }
  1691. cur_col = 0;
  1692. if (silent == FALSE) {
  1693. printf("%s: Transmission error detected\n", ahd_name(ahd));
  1694. ahd_lqistat1_print(lqistat1, &cur_col, 50);
  1695. ahd_lastphase_print(lastphase, &cur_col, 50);
  1696. ahd_scsisigi_print(curphase, &cur_col, 50);
  1697. ahd_perrdiag_print(perrdiag, &cur_col, 50);
  1698. printf("\n");
  1699. ahd_dump_card_state(ahd);
  1700. }
  1701. if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
  1702. if (silent == FALSE) {
  1703. printf("%s: Gross protocol error during incoming "
  1704. "packet. lqistat1 == 0x%x. Resetting bus.\n",
  1705. ahd_name(ahd), lqistat1);
  1706. }
  1707. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1708. return;
  1709. } else if ((lqistat1 & LQICRCI_LQ) != 0) {
  1710. /*
  1711. * A CRC error has been detected on an incoming LQ.
  1712. * The bus is currently hung on the last ACK.
  1713. * Hit LQIRETRY to release the last ack, and
  1714. * wait for the sequencer to determine that ATNO
  1715. * is asserted while in message out to take us
  1716. * to our host message loop. No NONPACKREQ or
  1717. * LQIPHASE type errors will occur in this
  1718. * scenario. After this first LQIRETRY, the LQI
  1719. * manager will be in ISELO where it will
  1720. * happily sit until another packet phase begins.
  1721. * Unexpected bus free detection is enabled
  1722. * through any phases that occur after we release
  1723. * this last ack until the LQI manager sees a
  1724. * packet phase. This implies we may have to
  1725. * ignore a perfectly valid "unexected busfree"
  1726. * after our "initiator detected error" message is
  1727. * sent. A busfree is the expected response after
  1728. * we tell the target that it's L_Q was corrupted.
  1729. * (SPI4R09 10.7.3.3.3)
  1730. */
  1731. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1732. printf("LQIRetry for LQICRCI_LQ to release ACK\n");
  1733. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1734. /*
  1735. * We detected a CRC error in a NON-LQ packet.
  1736. * The hardware has varying behavior in this situation
  1737. * depending on whether this packet was part of a
  1738. * stream or not.
  1739. *
  1740. * PKT by PKT mode:
  1741. * The hardware has already acked the complete packet.
  1742. * If the target honors our outstanding ATN condition,
  1743. * we should be (or soon will be) in MSGOUT phase.
  1744. * This will trigger the LQIPHASE_LQ status bit as the
  1745. * hardware was expecting another LQ. Unexpected
  1746. * busfree detection is enabled. Once LQIPHASE_LQ is
  1747. * true (first entry into host message loop is much
  1748. * the same), we must clear LQIPHASE_LQ and hit
  1749. * LQIRETRY so the hardware is ready to handle
  1750. * a future LQ. NONPACKREQ will not be asserted again
  1751. * once we hit LQIRETRY until another packet is
  1752. * processed. The target may either go busfree
  1753. * or start another packet in response to our message.
  1754. *
  1755. * Read Streaming P0 asserted:
  1756. * If we raise ATN and the target completes the entire
  1757. * stream (P0 asserted during the last packet), the
  1758. * hardware will ack all data and return to the ISTART
  1759. * state. When the target reponds to our ATN condition,
  1760. * LQIPHASE_LQ will be asserted. We should respond to
  1761. * this with an LQIRETRY to prepare for any future
  1762. * packets. NONPACKREQ will not be asserted again
  1763. * once we hit LQIRETRY until another packet is
  1764. * processed. The target may either go busfree or
  1765. * start another packet in response to our message.
  1766. * Busfree detection is enabled.
  1767. *
  1768. * Read Streaming P0 not asserted:
  1769. * If we raise ATN and the target transitions to
  1770. * MSGOUT in or after a packet where P0 is not
  1771. * asserted, the hardware will assert LQIPHASE_NLQ.
  1772. * We should respond to the LQIPHASE_NLQ with an
  1773. * LQIRETRY. Should the target stay in a non-pkt
  1774. * phase after we send our message, the hardware
  1775. * will assert LQIPHASE_LQ. Recovery is then just as
  1776. * listed above for the read streaming with P0 asserted.
  1777. * Busfree detection is enabled.
  1778. */
  1779. if (silent == FALSE)
  1780. printf("LQICRC_NLQ\n");
  1781. if (scb == NULL) {
  1782. printf("%s: No SCB valid for LQICRC_NLQ. "
  1783. "Resetting bus\n", ahd_name(ahd));
  1784. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1785. return;
  1786. }
  1787. } else if ((lqistat1 & LQIBADLQI) != 0) {
  1788. printf("Need to handle BADLQI!\n");
  1789. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1790. return;
  1791. } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
  1792. if ((curphase & ~P_DATAIN_DT) != 0) {
  1793. /* Ack the byte. So we can continue. */
  1794. if (silent == FALSE)
  1795. printf("Acking %s to clear perror\n",
  1796. ahd_lookup_phase_entry(curphase)->phasemsg);
  1797. ahd_inb(ahd, SCSIDAT);
  1798. }
  1799. if (curphase == P_MESGIN)
  1800. msg_out = MSG_PARITY_ERROR;
  1801. }
  1802. /*
  1803. * We've set the hardware to assert ATN if we
  1804. * get a parity error on "in" phases, so all we
  1805. * need to do is stuff the message buffer with
  1806. * the appropriate message. "In" phases have set
  1807. * mesg_out to something other than MSG_NOP.
  1808. */
  1809. ahd->send_msg_perror = msg_out;
  1810. if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
  1811. scb->flags |= SCB_TRANSMISSION_ERROR;
  1812. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1813. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1814. ahd_unpause(ahd);
  1815. }
  1816. static void
  1817. ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
  1818. {
  1819. /*
  1820. * Clear the sources of the interrupts.
  1821. */
  1822. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1823. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1824. /*
  1825. * If the "illegal" phase changes were in response
  1826. * to our ATN to flag a CRC error, AND we ended up
  1827. * on packet boundaries, clear the error, restart the
  1828. * LQI manager as appropriate, and go on our merry
  1829. * way toward sending the message. Otherwise, reset
  1830. * the bus to clear the error.
  1831. */
  1832. ahd_set_active_fifo(ahd);
  1833. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
  1834. && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
  1835. if ((lqistat1 & LQIPHASE_LQ) != 0) {
  1836. printf("LQIRETRY for LQIPHASE_LQ\n");
  1837. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1838. } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
  1839. printf("LQIRETRY for LQIPHASE_NLQ\n");
  1840. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1841. } else
  1842. panic("ahd_handle_lqiphase_error: No phase errors\n");
  1843. ahd_dump_card_state(ahd);
  1844. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1845. ahd_unpause(ahd);
  1846. } else {
  1847. printf("Reseting Channel for LQI Phase error\n");
  1848. ahd_dump_card_state(ahd);
  1849. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1850. }
  1851. }
  1852. /*
  1853. * Packetized unexpected or expected busfree.
  1854. * Entered in mode based on busfreetime.
  1855. */
  1856. static int
  1857. ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
  1858. {
  1859. u_int lqostat1;
  1860. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  1861. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  1862. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1863. if ((lqostat1 & LQOBUSFREE) != 0) {
  1864. struct scb *scb;
  1865. u_int scbid;
  1866. u_int saved_scbptr;
  1867. u_int waiting_h;
  1868. u_int waiting_t;
  1869. u_int next;
  1870. /*
  1871. * The LQO manager detected an unexpected busfree
  1872. * either:
  1873. *
  1874. * 1) During an outgoing LQ.
  1875. * 2) After an outgoing LQ but before the first
  1876. * REQ of the command packet.
  1877. * 3) During an outgoing command packet.
  1878. *
  1879. * In all cases, CURRSCB is pointing to the
  1880. * SCB that encountered the failure. Clean
  1881. * up the queue, clear SELDO and LQOBUSFREE,
  1882. * and allow the sequencer to restart the select
  1883. * out at its lesure.
  1884. */
  1885. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1886. scbid = ahd_inw(ahd, CURRSCB);
  1887. scb = ahd_lookup_scb(ahd, scbid);
  1888. if (scb == NULL)
  1889. panic("SCB not valid during LQOBUSFREE");
  1890. /*
  1891. * Clear the status.
  1892. */
  1893. ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
  1894. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
  1895. ahd_outb(ahd, CLRLQOINT1, 0);
  1896. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1897. ahd_flush_device_writes(ahd);
  1898. ahd_outb(ahd, CLRSINT0, CLRSELDO);
  1899. /*
  1900. * Return the LQO manager to its idle loop. It will
  1901. * not do this automatically if the busfree occurs
  1902. * after the first REQ of either the LQ or command
  1903. * packet or between the LQ and command packet.
  1904. */
  1905. ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
  1906. /*
  1907. * Update the waiting for selection queue so
  1908. * we restart on the correct SCB.
  1909. */
  1910. waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
  1911. saved_scbptr = ahd_get_scbptr(ahd);
  1912. if (waiting_h != scbid) {
  1913. ahd_outw(ahd, WAITING_TID_HEAD, scbid);
  1914. waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
  1915. if (waiting_t == waiting_h) {
  1916. ahd_outw(ahd, WAITING_TID_TAIL, scbid);
  1917. next = SCB_LIST_NULL;
  1918. } else {
  1919. ahd_set_scbptr(ahd, waiting_h);
  1920. next = ahd_inw_scbram(ahd, SCB_NEXT2);
  1921. }
  1922. ahd_set_scbptr(ahd, scbid);
  1923. ahd_outw(ahd, SCB_NEXT2, next);
  1924. }
  1925. ahd_set_scbptr(ahd, saved_scbptr);
  1926. if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
  1927. if (SCB_IS_SILENT(scb) == FALSE) {
  1928. ahd_print_path(ahd, scb);
  1929. printf("Probable outgoing LQ CRC error. "
  1930. "Retrying command\n");
  1931. }
  1932. scb->crc_retry_count++;
  1933. } else {
  1934. ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1935. ahd_freeze_scb(scb);
  1936. ahd_freeze_devq(ahd, scb);
  1937. }
  1938. /* Return unpausing the sequencer. */
  1939. return (0);
  1940. } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
  1941. /*
  1942. * Ignore what are really parity errors that
  1943. * occur on the last REQ of a free running
  1944. * clock prior to going busfree. Some drives
  1945. * do not properly active negate just before
  1946. * going busfree resulting in a parity glitch.
  1947. */
  1948. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
  1949. #ifdef AHD_DEBUG
  1950. if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
  1951. printf("%s: Parity on last REQ detected "
  1952. "during busfree phase.\n",
  1953. ahd_name(ahd));
  1954. #endif
  1955. /* Return unpausing the sequencer. */
  1956. return (0);
  1957. }
  1958. if (ahd->src_mode != AHD_MODE_SCSI) {
  1959. u_int scbid;
  1960. struct scb *scb;
  1961. scbid = ahd_get_scbptr(ahd);
  1962. scb = ahd_lookup_scb(ahd, scbid);
  1963. ahd_print_path(ahd, scb);
  1964. printf("Unexpected PKT busfree condition\n");
  1965. ahd_dump_card_state(ahd);
  1966. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
  1967. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1968. ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
  1969. /* Return restarting the sequencer. */
  1970. return (1);
  1971. }
  1972. printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
  1973. ahd_dump_card_state(ahd);
  1974. /* Restart the sequencer. */
  1975. return (1);
  1976. }
  1977. /*
  1978. * Non-packetized unexpected or expected busfree.
  1979. */
  1980. static int
  1981. ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
  1982. {
  1983. struct ahd_devinfo devinfo;
  1984. struct scb *scb;
  1985. u_int lastphase;
  1986. u_int saved_scsiid;
  1987. u_int saved_lun;
  1988. u_int target;
  1989. u_int initiator_role_id;
  1990. u_int scbid;
  1991. u_int ppr_busfree;
  1992. int printerror;
  1993. /*
  1994. * Look at what phase we were last in. If its message out,
  1995. * chances are pretty good that the busfree was in response
  1996. * to one of our abort requests.
  1997. */
  1998. lastphase = ahd_inb(ahd, LASTPHASE);
  1999. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  2000. saved_lun = ahd_inb(ahd, SAVED_LUN);
  2001. target = SCSIID_TARGET(ahd, saved_scsiid);
  2002. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  2003. ahd_compile_devinfo(&devinfo, initiator_role_id,
  2004. target, saved_lun, 'A', ROLE_INITIATOR);
  2005. printerror = 1;
  2006. scbid = ahd_get_scbptr(ahd);
  2007. scb = ahd_lookup_scb(ahd, scbid);
  2008. if (scb != NULL
  2009. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  2010. scb = NULL;
  2011. ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
  2012. if (lastphase == P_MESGOUT) {
  2013. u_int tag;
  2014. tag = SCB_LIST_NULL;
  2015. if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
  2016. || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
  2017. int found;
  2018. int sent_msg;
  2019. if (scb == NULL) {
  2020. ahd_print_devinfo(ahd, &devinfo);
  2021. printf("Abort for unidentified "
  2022. "connection completed.\n");
  2023. /* restart the sequencer. */
  2024. return (1);
  2025. }
  2026. sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
  2027. ahd_print_path(ahd, scb);
  2028. printf("SCB %d - Abort%s Completed.\n",
  2029. SCB_GET_TAG(scb),
  2030. sent_msg == MSG_ABORT_TAG ? "" : " Tag");
  2031. if (sent_msg == MSG_ABORT_TAG)
  2032. tag = SCB_GET_TAG(scb);
  2033. found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
  2034. tag, ROLE_INITIATOR,
  2035. CAM_REQ_ABORTED);
  2036. printf("found == 0x%x\n", found);
  2037. printerror = 0;
  2038. } else if (ahd_sent_msg(ahd, AHDMSG_1B,
  2039. MSG_BUS_DEV_RESET, TRUE)) {
  2040. #ifdef __FreeBSD__
  2041. /*
  2042. * Don't mark the user's request for this BDR
  2043. * as completing with CAM_BDR_SENT. CAM3
  2044. * specifies CAM_REQ_CMP.
  2045. */
  2046. if (scb != NULL
  2047. && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  2048. && ahd_match_scb(ahd, scb, target, 'A',
  2049. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  2050. ROLE_INITIATOR))
  2051. ahd_set_transaction_status(scb, CAM_REQ_CMP);
  2052. #endif
  2053. ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
  2054. CAM_BDR_SENT, "Bus Device Reset",
  2055. /*verbose_level*/0);
  2056. printerror = 0;
  2057. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
  2058. && ppr_busfree == 0) {
  2059. struct ahd_initiator_tinfo *tinfo;
  2060. struct ahd_tmode_tstate *tstate;
  2061. /*
  2062. * PPR Rejected.
  2063. *
  2064. * If the previous negotiation was packetized,
  2065. * this could be because the device has been
  2066. * reset without our knowledge. Force our
  2067. * current negotiation to async and retry the
  2068. * negotiation. Otherwise retry the command
  2069. * with non-ppr negotiation.
  2070. */
  2071. #ifdef AHD_DEBUG
  2072. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2073. printf("PPR negotiation rejected busfree.\n");
  2074. #endif
  2075. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  2076. devinfo.our_scsiid,
  2077. devinfo.target, &tstate);
  2078. if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ)!=0) {
  2079. ahd_set_width(ahd, &devinfo,
  2080. MSG_EXT_WDTR_BUS_8_BIT,
  2081. AHD_TRANS_CUR,
  2082. /*paused*/TRUE);
  2083. ahd_set_syncrate(ahd, &devinfo,
  2084. /*period*/0, /*offset*/0,
  2085. /*ppr_options*/0,
  2086. AHD_TRANS_CUR,
  2087. /*paused*/TRUE);
  2088. /*
  2089. * The expect PPR busfree handler below
  2090. * will effect the retry and necessary
  2091. * abort.
  2092. */
  2093. } else {
  2094. tinfo->curr.transport_version = 2;
  2095. tinfo->goal.transport_version = 2;
  2096. tinfo->goal.ppr_options = 0;
  2097. /*
  2098. * Remove any SCBs in the waiting for selection
  2099. * queue that may also be for this target so
  2100. * that command ordering is preserved.
  2101. */
  2102. ahd_freeze_devq(ahd, scb);
  2103. ahd_qinfifo_requeue_tail(ahd, scb);
  2104. printerror = 0;
  2105. }
  2106. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
  2107. && ppr_busfree == 0) {
  2108. /*
  2109. * Negotiation Rejected. Go-narrow and
  2110. * retry command.
  2111. */
  2112. #ifdef AHD_DEBUG
  2113. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2114. printf("WDTR negotiation rejected busfree.\n");
  2115. #endif
  2116. ahd_set_width(ahd, &devinfo,
  2117. MSG_EXT_WDTR_BUS_8_BIT,
  2118. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2119. /*paused*/TRUE);
  2120. /*
  2121. * Remove any SCBs in the waiting for selection
  2122. * queue that may also be for this target so that
  2123. * command ordering is preserved.
  2124. */
  2125. ahd_freeze_devq(ahd, scb);
  2126. ahd_qinfifo_requeue_tail(ahd, scb);
  2127. printerror = 0;
  2128. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
  2129. && ppr_busfree == 0) {
  2130. /*
  2131. * Negotiation Rejected. Go-async and
  2132. * retry command.
  2133. */
  2134. #ifdef AHD_DEBUG
  2135. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2136. printf("SDTR negotiation rejected busfree.\n");
  2137. #endif
  2138. ahd_set_syncrate(ahd, &devinfo,
  2139. /*period*/0, /*offset*/0,
  2140. /*ppr_options*/0,
  2141. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2142. /*paused*/TRUE);
  2143. /*
  2144. * Remove any SCBs in the waiting for selection
  2145. * queue that may also be for this target so that
  2146. * command ordering is preserved.
  2147. */
  2148. ahd_freeze_devq(ahd, scb);
  2149. ahd_qinfifo_requeue_tail(ahd, scb);
  2150. printerror = 0;
  2151. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
  2152. && ahd_sent_msg(ahd, AHDMSG_1B,
  2153. MSG_INITIATOR_DET_ERR, TRUE)) {
  2154. #ifdef AHD_DEBUG
  2155. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2156. printf("Expected IDE Busfree\n");
  2157. #endif
  2158. printerror = 0;
  2159. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
  2160. && ahd_sent_msg(ahd, AHDMSG_1B,
  2161. MSG_MESSAGE_REJECT, TRUE)) {
  2162. #ifdef AHD_DEBUG
  2163. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2164. printf("Expected QAS Reject Busfree\n");
  2165. #endif
  2166. printerror = 0;
  2167. }
  2168. }
  2169. /*
  2170. * The busfree required flag is honored at the end of
  2171. * the message phases. We check it last in case we
  2172. * had to send some other message that caused a busfree.
  2173. */
  2174. if (printerror != 0
  2175. && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
  2176. && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
  2177. ahd_freeze_devq(ahd, scb);
  2178. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  2179. ahd_freeze_scb(scb);
  2180. if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
  2181. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  2182. SCB_GET_CHANNEL(ahd, scb),
  2183. SCB_GET_LUN(scb), SCB_LIST_NULL,
  2184. ROLE_INITIATOR, CAM_REQ_ABORTED);
  2185. } else {
  2186. #ifdef AHD_DEBUG
  2187. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2188. printf("PPR Negotiation Busfree.\n");
  2189. #endif
  2190. ahd_done(ahd, scb);
  2191. }
  2192. printerror = 0;
  2193. }
  2194. if (printerror != 0) {
  2195. int aborted;
  2196. aborted = 0;
  2197. if (scb != NULL) {
  2198. u_int tag;
  2199. if ((scb->hscb->control & TAG_ENB) != 0)
  2200. tag = SCB_GET_TAG(scb);
  2201. else
  2202. tag = SCB_LIST_NULL;
  2203. ahd_print_path(ahd, scb);
  2204. aborted = ahd_abort_scbs(ahd, target, 'A',
  2205. SCB_GET_LUN(scb), tag,
  2206. ROLE_INITIATOR,
  2207. CAM_UNEXP_BUSFREE);
  2208. } else {
  2209. /*
  2210. * We had not fully identified this connection,
  2211. * so we cannot abort anything.
  2212. */
  2213. printf("%s: ", ahd_name(ahd));
  2214. }
  2215. printf("Unexpected busfree %s, %d SCBs aborted, "
  2216. "PRGMCNT == 0x%x\n",
  2217. ahd_lookup_phase_entry(lastphase)->phasemsg,
  2218. aborted,
  2219. ahd_inw(ahd, PRGMCNT));
  2220. ahd_dump_card_state(ahd);
  2221. if (lastphase != P_BUSFREE)
  2222. ahd_force_renegotiation(ahd, &devinfo);
  2223. }
  2224. /* Always restart the sequencer. */
  2225. return (1);
  2226. }
  2227. static void
  2228. ahd_handle_proto_violation(struct ahd_softc *ahd)
  2229. {
  2230. struct ahd_devinfo devinfo;
  2231. struct scb *scb;
  2232. u_int scbid;
  2233. u_int seq_flags;
  2234. u_int curphase;
  2235. u_int lastphase;
  2236. int found;
  2237. ahd_fetch_devinfo(ahd, &devinfo);
  2238. scbid = ahd_get_scbptr(ahd);
  2239. scb = ahd_lookup_scb(ahd, scbid);
  2240. seq_flags = ahd_inb(ahd, SEQ_FLAGS);
  2241. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  2242. lastphase = ahd_inb(ahd, LASTPHASE);
  2243. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2244. /*
  2245. * The reconnecting target either did not send an
  2246. * identify message, or did, but we didn't find an SCB
  2247. * to match.
  2248. */
  2249. ahd_print_devinfo(ahd, &devinfo);
  2250. printf("Target did not send an IDENTIFY message. "
  2251. "LASTPHASE = 0x%x.\n", lastphase);
  2252. scb = NULL;
  2253. } else if (scb == NULL) {
  2254. /*
  2255. * We don't seem to have an SCB active for this
  2256. * transaction. Print an error and reset the bus.
  2257. */
  2258. ahd_print_devinfo(ahd, &devinfo);
  2259. printf("No SCB found during protocol violation\n");
  2260. goto proto_violation_reset;
  2261. } else {
  2262. ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2263. if ((seq_flags & NO_CDB_SENT) != 0) {
  2264. ahd_print_path(ahd, scb);
  2265. printf("No or incomplete CDB sent to device.\n");
  2266. } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
  2267. & STATUS_RCVD) == 0) {
  2268. /*
  2269. * The target never bothered to provide status to
  2270. * us prior to completing the command. Since we don't
  2271. * know the disposition of this command, we must attempt
  2272. * to abort it. Assert ATN and prepare to send an abort
  2273. * message.
  2274. */
  2275. ahd_print_path(ahd, scb);
  2276. printf("Completed command without status.\n");
  2277. } else {
  2278. ahd_print_path(ahd, scb);
  2279. printf("Unknown protocol violation.\n");
  2280. ahd_dump_card_state(ahd);
  2281. }
  2282. }
  2283. if ((lastphase & ~P_DATAIN_DT) == 0
  2284. || lastphase == P_COMMAND) {
  2285. proto_violation_reset:
  2286. /*
  2287. * Target either went directly to data
  2288. * phase or didn't respond to our ATN.
  2289. * The only safe thing to do is to blow
  2290. * it away with a bus reset.
  2291. */
  2292. found = ahd_reset_channel(ahd, 'A', TRUE);
  2293. printf("%s: Issued Channel %c Bus Reset. "
  2294. "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
  2295. } else {
  2296. /*
  2297. * Leave the selection hardware off in case
  2298. * this abort attempt will affect yet to
  2299. * be sent commands.
  2300. */
  2301. ahd_outb(ahd, SCSISEQ0,
  2302. ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  2303. ahd_assert_atn(ahd);
  2304. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  2305. if (scb == NULL) {
  2306. ahd_print_devinfo(ahd, &devinfo);
  2307. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  2308. ahd->msgout_len = 1;
  2309. ahd->msgout_index = 0;
  2310. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2311. } else {
  2312. ahd_print_path(ahd, scb);
  2313. scb->flags |= SCB_ABORT;
  2314. }
  2315. printf("Protocol violation %s. Attempting to abort.\n",
  2316. ahd_lookup_phase_entry(curphase)->phasemsg);
  2317. }
  2318. }
  2319. /*
  2320. * Force renegotiation to occur the next time we initiate
  2321. * a command to the current device.
  2322. */
  2323. static void
  2324. ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  2325. {
  2326. struct ahd_initiator_tinfo *targ_info;
  2327. struct ahd_tmode_tstate *tstate;
  2328. #ifdef AHD_DEBUG
  2329. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2330. ahd_print_devinfo(ahd, devinfo);
  2331. printf("Forcing renegotiation\n");
  2332. }
  2333. #endif
  2334. targ_info = ahd_fetch_transinfo(ahd,
  2335. devinfo->channel,
  2336. devinfo->our_scsiid,
  2337. devinfo->target,
  2338. &tstate);
  2339. ahd_update_neg_request(ahd, devinfo, tstate,
  2340. targ_info, AHD_NEG_IF_NON_ASYNC);
  2341. }
  2342. #define AHD_MAX_STEPS 2000
  2343. void
  2344. ahd_clear_critical_section(struct ahd_softc *ahd)
  2345. {
  2346. ahd_mode_state saved_modes;
  2347. int stepping;
  2348. int steps;
  2349. int first_instr;
  2350. u_int simode0;
  2351. u_int simode1;
  2352. u_int simode3;
  2353. u_int lqimode0;
  2354. u_int lqimode1;
  2355. u_int lqomode0;
  2356. u_int lqomode1;
  2357. if (ahd->num_critical_sections == 0)
  2358. return;
  2359. stepping = FALSE;
  2360. steps = 0;
  2361. first_instr = 0;
  2362. simode0 = 0;
  2363. simode1 = 0;
  2364. simode3 = 0;
  2365. lqimode0 = 0;
  2366. lqimode1 = 0;
  2367. lqomode0 = 0;
  2368. lqomode1 = 0;
  2369. saved_modes = ahd_save_modes(ahd);
  2370. for (;;) {
  2371. struct cs *cs;
  2372. u_int seqaddr;
  2373. u_int i;
  2374. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2375. seqaddr = ahd_inw(ahd, CURADDR);
  2376. cs = ahd->critical_sections;
  2377. for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
  2378. if (cs->begin < seqaddr && cs->end >= seqaddr)
  2379. break;
  2380. }
  2381. if (i == ahd->num_critical_sections)
  2382. break;
  2383. if (steps > AHD_MAX_STEPS) {
  2384. printf("%s: Infinite loop in critical section\n"
  2385. "%s: First Instruction 0x%x now 0x%x\n",
  2386. ahd_name(ahd), ahd_name(ahd), first_instr,
  2387. seqaddr);
  2388. ahd_dump_card_state(ahd);
  2389. panic("critical section loop");
  2390. }
  2391. steps++;
  2392. #ifdef AHD_DEBUG
  2393. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  2394. printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
  2395. seqaddr);
  2396. #endif
  2397. if (stepping == FALSE) {
  2398. first_instr = seqaddr;
  2399. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2400. simode0 = ahd_inb(ahd, SIMODE0);
  2401. simode3 = ahd_inb(ahd, SIMODE3);
  2402. lqimode0 = ahd_inb(ahd, LQIMODE0);
  2403. lqimode1 = ahd_inb(ahd, LQIMODE1);
  2404. lqomode0 = ahd_inb(ahd, LQOMODE0);
  2405. lqomode1 = ahd_inb(ahd, LQOMODE1);
  2406. ahd_outb(ahd, SIMODE0, 0);
  2407. ahd_outb(ahd, SIMODE3, 0);
  2408. ahd_outb(ahd, LQIMODE0, 0);
  2409. ahd_outb(ahd, LQIMODE1, 0);
  2410. ahd_outb(ahd, LQOMODE0, 0);
  2411. ahd_outb(ahd, LQOMODE1, 0);
  2412. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2413. simode1 = ahd_inb(ahd, SIMODE1);
  2414. /*
  2415. * We don't clear ENBUSFREE. Unfortunately
  2416. * we cannot re-enable busfree detection within
  2417. * the current connection, so we must leave it
  2418. * on while single stepping.
  2419. */
  2420. ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
  2421. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
  2422. stepping = TRUE;
  2423. }
  2424. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  2425. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2426. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  2427. ahd_outb(ahd, HCNTRL, ahd->unpause);
  2428. while (!ahd_is_paused(ahd))
  2429. ahd_delay(200);
  2430. ahd_update_modes(ahd);
  2431. }
  2432. if (stepping) {
  2433. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2434. ahd_outb(ahd, SIMODE0, simode0);
  2435. ahd_outb(ahd, SIMODE3, simode3);
  2436. ahd_outb(ahd, LQIMODE0, lqimode0);
  2437. ahd_outb(ahd, LQIMODE1, lqimode1);
  2438. ahd_outb(ahd, LQOMODE0, lqomode0);
  2439. ahd_outb(ahd, LQOMODE1, lqomode1);
  2440. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2441. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
  2442. ahd_outb(ahd, SIMODE1, simode1);
  2443. /*
  2444. * SCSIINT seems to glitch occassionally when
  2445. * the interrupt masks are restored. Clear SCSIINT
  2446. * one more time so that only persistent errors
  2447. * are seen as a real interrupt.
  2448. */
  2449. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2450. }
  2451. ahd_restore_modes(ahd, saved_modes);
  2452. }
  2453. /*
  2454. * Clear any pending interrupt status.
  2455. */
  2456. void
  2457. ahd_clear_intstat(struct ahd_softc *ahd)
  2458. {
  2459. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  2460. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  2461. /* Clear any interrupt conditions this may have caused */
  2462. ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
  2463. |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
  2464. ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
  2465. |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
  2466. |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
  2467. ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
  2468. |CLRLQOATNPKT|CLRLQOTCRC);
  2469. ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
  2470. |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
  2471. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  2472. ahd_outb(ahd, CLRLQOINT0, 0);
  2473. ahd_outb(ahd, CLRLQOINT1, 0);
  2474. }
  2475. ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
  2476. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  2477. |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
  2478. ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
  2479. |CLRIOERR|CLROVERRUN);
  2480. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2481. }
  2482. /**************************** Debugging Routines ******************************/
  2483. #ifdef AHD_DEBUG
  2484. uint32_t ahd_debug = AHD_DEBUG_OPTS;
  2485. #endif
  2486. void
  2487. ahd_print_scb(struct scb *scb)
  2488. {
  2489. struct hardware_scb *hscb;
  2490. int i;
  2491. hscb = scb->hscb;
  2492. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
  2493. (void *)scb,
  2494. hscb->control,
  2495. hscb->scsiid,
  2496. hscb->lun,
  2497. hscb->cdb_len);
  2498. printf("Shared Data: ");
  2499. for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
  2500. printf("%#02x", hscb->shared_data.idata.cdb[i]);
  2501. printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
  2502. (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
  2503. (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
  2504. ahd_le32toh(hscb->datacnt),
  2505. ahd_le32toh(hscb->sgptr),
  2506. SCB_GET_TAG(scb));
  2507. ahd_dump_sglist(scb);
  2508. }
  2509. void
  2510. ahd_dump_sglist(struct scb *scb)
  2511. {
  2512. int i;
  2513. if (scb->sg_count > 0) {
  2514. if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
  2515. struct ahd_dma64_seg *sg_list;
  2516. sg_list = (struct ahd_dma64_seg*)scb->sg_list;
  2517. for (i = 0; i < scb->sg_count; i++) {
  2518. uint64_t addr;
  2519. uint32_t len;
  2520. addr = ahd_le64toh(sg_list[i].addr);
  2521. len = ahd_le32toh(sg_list[i].len);
  2522. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2523. i,
  2524. (uint32_t)((addr >> 32) & 0xFFFFFFFF),
  2525. (uint32_t)(addr & 0xFFFFFFFF),
  2526. sg_list[i].len & AHD_SG_LEN_MASK,
  2527. (sg_list[i].len & AHD_DMA_LAST_SEG)
  2528. ? " Last" : "");
  2529. }
  2530. } else {
  2531. struct ahd_dma_seg *sg_list;
  2532. sg_list = (struct ahd_dma_seg*)scb->sg_list;
  2533. for (i = 0; i < scb->sg_count; i++) {
  2534. uint32_t len;
  2535. len = ahd_le32toh(sg_list[i].len);
  2536. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2537. i,
  2538. (len & AHD_SG_HIGH_ADDR_MASK) >> 24,
  2539. ahd_le32toh(sg_list[i].addr),
  2540. len & AHD_SG_LEN_MASK,
  2541. len & AHD_DMA_LAST_SEG ? " Last" : "");
  2542. }
  2543. }
  2544. }
  2545. }
  2546. /************************* Transfer Negotiation *******************************/
  2547. /*
  2548. * Allocate per target mode instance (ID we respond to as a target)
  2549. * transfer negotiation data structures.
  2550. */
  2551. static struct ahd_tmode_tstate *
  2552. ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
  2553. {
  2554. struct ahd_tmode_tstate *master_tstate;
  2555. struct ahd_tmode_tstate *tstate;
  2556. int i;
  2557. master_tstate = ahd->enabled_targets[ahd->our_id];
  2558. if (ahd->enabled_targets[scsi_id] != NULL
  2559. && ahd->enabled_targets[scsi_id] != master_tstate)
  2560. panic("%s: ahd_alloc_tstate - Target already allocated",
  2561. ahd_name(ahd));
  2562. tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
  2563. if (tstate == NULL)
  2564. return (NULL);
  2565. /*
  2566. * If we have allocated a master tstate, copy user settings from
  2567. * the master tstate (taken from SRAM or the EEPROM) for this
  2568. * channel, but reset our current and goal settings to async/narrow
  2569. * until an initiator talks to us.
  2570. */
  2571. if (master_tstate != NULL) {
  2572. memcpy(tstate, master_tstate, sizeof(*tstate));
  2573. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  2574. for (i = 0; i < 16; i++) {
  2575. memset(&tstate->transinfo[i].curr, 0,
  2576. sizeof(tstate->transinfo[i].curr));
  2577. memset(&tstate->transinfo[i].goal, 0,
  2578. sizeof(tstate->transinfo[i].goal));
  2579. }
  2580. } else
  2581. memset(tstate, 0, sizeof(*tstate));
  2582. ahd->enabled_targets[scsi_id] = tstate;
  2583. return (tstate);
  2584. }
  2585. #ifdef AHD_TARGET_MODE
  2586. /*
  2587. * Free per target mode instance (ID we respond to as a target)
  2588. * transfer negotiation data structures.
  2589. */
  2590. static void
  2591. ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
  2592. {
  2593. struct ahd_tmode_tstate *tstate;
  2594. /*
  2595. * Don't clean up our "master" tstate.
  2596. * It has our default user settings.
  2597. */
  2598. if (scsi_id == ahd->our_id
  2599. && force == FALSE)
  2600. return;
  2601. tstate = ahd->enabled_targets[scsi_id];
  2602. if (tstate != NULL)
  2603. free(tstate, M_DEVBUF);
  2604. ahd->enabled_targets[scsi_id] = NULL;
  2605. }
  2606. #endif
  2607. /*
  2608. * Called when we have an active connection to a target on the bus,
  2609. * this function finds the nearest period to the input period limited
  2610. * by the capabilities of the bus connectivity of and sync settings for
  2611. * the target.
  2612. */
  2613. void
  2614. ahd_devlimited_syncrate(struct ahd_softc *ahd,
  2615. struct ahd_initiator_tinfo *tinfo,
  2616. u_int *period, u_int *ppr_options, role_t role)
  2617. {
  2618. struct ahd_transinfo *transinfo;
  2619. u_int maxsync;
  2620. if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
  2621. && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
  2622. maxsync = AHD_SYNCRATE_PACED;
  2623. } else {
  2624. maxsync = AHD_SYNCRATE_ULTRA;
  2625. /* Can't do DT related options on an SE bus */
  2626. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2627. }
  2628. /*
  2629. * Never allow a value higher than our current goal
  2630. * period otherwise we may allow a target initiated
  2631. * negotiation to go above the limit as set by the
  2632. * user. In the case of an initiator initiated
  2633. * sync negotiation, we limit based on the user
  2634. * setting. This allows the system to still accept
  2635. * incoming negotiations even if target initiated
  2636. * negotiation is not performed.
  2637. */
  2638. if (role == ROLE_TARGET)
  2639. transinfo = &tinfo->user;
  2640. else
  2641. transinfo = &tinfo->goal;
  2642. *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
  2643. if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
  2644. maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
  2645. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2646. }
  2647. if (transinfo->period == 0) {
  2648. *period = 0;
  2649. *ppr_options = 0;
  2650. } else {
  2651. *period = MAX(*period, transinfo->period);
  2652. ahd_find_syncrate(ahd, period, ppr_options, maxsync);
  2653. }
  2654. }
  2655. /*
  2656. * Look up the valid period to SCSIRATE conversion in our table.
  2657. * Return the period and offset that should be sent to the target
  2658. * if this was the beginning of an SDTR.
  2659. */
  2660. void
  2661. ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
  2662. u_int *ppr_options, u_int maxsync)
  2663. {
  2664. if (*period < maxsync)
  2665. *period = maxsync;
  2666. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
  2667. && *period > AHD_SYNCRATE_MIN_DT)
  2668. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2669. if (*period > AHD_SYNCRATE_MIN)
  2670. *period = 0;
  2671. /* Honor PPR option conformance rules. */
  2672. if (*period > AHD_SYNCRATE_PACED)
  2673. *ppr_options &= ~MSG_EXT_PPR_RTI;
  2674. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  2675. *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
  2676. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
  2677. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2678. /* Skip all PACED only entries if IU is not available */
  2679. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
  2680. && *period < AHD_SYNCRATE_DT)
  2681. *period = AHD_SYNCRATE_DT;
  2682. /* Skip all DT only entries if DT is not available */
  2683. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  2684. && *period < AHD_SYNCRATE_ULTRA2)
  2685. *period = AHD_SYNCRATE_ULTRA2;
  2686. }
  2687. /*
  2688. * Truncate the given synchronous offset to a value the
  2689. * current adapter type and syncrate are capable of.
  2690. */
  2691. void
  2692. ahd_validate_offset(struct ahd_softc *ahd,
  2693. struct ahd_initiator_tinfo *tinfo,
  2694. u_int period, u_int *offset, int wide,
  2695. role_t role)
  2696. {
  2697. u_int maxoffset;
  2698. /* Limit offset to what we can do */
  2699. if (period == 0)
  2700. maxoffset = 0;
  2701. else if (period <= AHD_SYNCRATE_PACED) {
  2702. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
  2703. maxoffset = MAX_OFFSET_PACED_BUG;
  2704. else
  2705. maxoffset = MAX_OFFSET_PACED;
  2706. } else
  2707. maxoffset = MAX_OFFSET_NON_PACED;
  2708. *offset = MIN(*offset, maxoffset);
  2709. if (tinfo != NULL) {
  2710. if (role == ROLE_TARGET)
  2711. *offset = MIN(*offset, tinfo->user.offset);
  2712. else
  2713. *offset = MIN(*offset, tinfo->goal.offset);
  2714. }
  2715. }
  2716. /*
  2717. * Truncate the given transfer width parameter to a value the
  2718. * current adapter type is capable of.
  2719. */
  2720. void
  2721. ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
  2722. u_int *bus_width, role_t role)
  2723. {
  2724. switch (*bus_width) {
  2725. default:
  2726. if (ahd->features & AHD_WIDE) {
  2727. /* Respond Wide */
  2728. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  2729. break;
  2730. }
  2731. /* FALLTHROUGH */
  2732. case MSG_EXT_WDTR_BUS_8_BIT:
  2733. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  2734. break;
  2735. }
  2736. if (tinfo != NULL) {
  2737. if (role == ROLE_TARGET)
  2738. *bus_width = MIN(tinfo->user.width, *bus_width);
  2739. else
  2740. *bus_width = MIN(tinfo->goal.width, *bus_width);
  2741. }
  2742. }
  2743. /*
  2744. * Update the bitmask of targets for which the controller should
  2745. * negotiate with at the next convenient oportunity. This currently
  2746. * means the next time we send the initial identify messages for
  2747. * a new transaction.
  2748. */
  2749. int
  2750. ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2751. struct ahd_tmode_tstate *tstate,
  2752. struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
  2753. {
  2754. u_int auto_negotiate_orig;
  2755. auto_negotiate_orig = tstate->auto_negotiate;
  2756. if (neg_type == AHD_NEG_ALWAYS) {
  2757. /*
  2758. * Force our "current" settings to be
  2759. * unknown so that unless a bus reset
  2760. * occurs the need to renegotiate is
  2761. * recorded persistently.
  2762. */
  2763. if ((ahd->features & AHD_WIDE) != 0)
  2764. tinfo->curr.width = AHD_WIDTH_UNKNOWN;
  2765. tinfo->curr.period = AHD_PERIOD_UNKNOWN;
  2766. tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
  2767. }
  2768. if (tinfo->curr.period != tinfo->goal.period
  2769. || tinfo->curr.width != tinfo->goal.width
  2770. || tinfo->curr.offset != tinfo->goal.offset
  2771. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  2772. || (neg_type == AHD_NEG_IF_NON_ASYNC
  2773. && (tinfo->goal.offset != 0
  2774. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  2775. || tinfo->goal.ppr_options != 0)))
  2776. tstate->auto_negotiate |= devinfo->target_mask;
  2777. else
  2778. tstate->auto_negotiate &= ~devinfo->target_mask;
  2779. return (auto_negotiate_orig != tstate->auto_negotiate);
  2780. }
  2781. /*
  2782. * Update the user/goal/curr tables of synchronous negotiation
  2783. * parameters as well as, in the case of a current or active update,
  2784. * any data structures on the host controller. In the case of an
  2785. * active update, the specified target is currently talking to us on
  2786. * the bus, so the transfer parameter update must take effect
  2787. * immediately.
  2788. */
  2789. void
  2790. ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2791. u_int period, u_int offset, u_int ppr_options,
  2792. u_int type, int paused)
  2793. {
  2794. struct ahd_initiator_tinfo *tinfo;
  2795. struct ahd_tmode_tstate *tstate;
  2796. u_int old_period;
  2797. u_int old_offset;
  2798. u_int old_ppr;
  2799. int active;
  2800. int update_needed;
  2801. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2802. update_needed = 0;
  2803. if (period == 0 || offset == 0) {
  2804. period = 0;
  2805. offset = 0;
  2806. }
  2807. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2808. devinfo->target, &tstate);
  2809. if ((type & AHD_TRANS_USER) != 0) {
  2810. tinfo->user.period = period;
  2811. tinfo->user.offset = offset;
  2812. tinfo->user.ppr_options = ppr_options;
  2813. }
  2814. if ((type & AHD_TRANS_GOAL) != 0) {
  2815. tinfo->goal.period = period;
  2816. tinfo->goal.offset = offset;
  2817. tinfo->goal.ppr_options = ppr_options;
  2818. }
  2819. old_period = tinfo->curr.period;
  2820. old_offset = tinfo->curr.offset;
  2821. old_ppr = tinfo->curr.ppr_options;
  2822. if ((type & AHD_TRANS_CUR) != 0
  2823. && (old_period != period
  2824. || old_offset != offset
  2825. || old_ppr != ppr_options)) {
  2826. update_needed++;
  2827. tinfo->curr.period = period;
  2828. tinfo->curr.offset = offset;
  2829. tinfo->curr.ppr_options = ppr_options;
  2830. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2831. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2832. if (bootverbose) {
  2833. if (offset != 0) {
  2834. int options;
  2835. printf("%s: target %d synchronous with "
  2836. "period = 0x%x, offset = 0x%x",
  2837. ahd_name(ahd), devinfo->target,
  2838. period, offset);
  2839. options = 0;
  2840. if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
  2841. printf("(RDSTRM");
  2842. options++;
  2843. }
  2844. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
  2845. printf("%s", options ? "|DT" : "(DT");
  2846. options++;
  2847. }
  2848. if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
  2849. printf("%s", options ? "|IU" : "(IU");
  2850. options++;
  2851. }
  2852. if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
  2853. printf("%s", options ? "|RTI" : "(RTI");
  2854. options++;
  2855. }
  2856. if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
  2857. printf("%s", options ? "|QAS" : "(QAS");
  2858. options++;
  2859. }
  2860. if (options != 0)
  2861. printf(")\n");
  2862. else
  2863. printf("\n");
  2864. } else {
  2865. printf("%s: target %d using "
  2866. "asynchronous transfers%s\n",
  2867. ahd_name(ahd), devinfo->target,
  2868. (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
  2869. ? "(QAS)" : "");
  2870. }
  2871. }
  2872. }
  2873. /*
  2874. * Always refresh the neg-table to handle the case of the
  2875. * sequencer setting the ENATNO bit for a MK_MESSAGE request.
  2876. * We will always renegotiate in that case if this is a
  2877. * packetized request. Also manage the busfree expected flag
  2878. * from this common routine so that we catch changes due to
  2879. * WDTR or SDTR messages.
  2880. */
  2881. if ((type & AHD_TRANS_CUR) != 0) {
  2882. if (!paused)
  2883. ahd_pause(ahd);
  2884. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2885. if (!paused)
  2886. ahd_unpause(ahd);
  2887. if (ahd->msg_type != MSG_TYPE_NONE) {
  2888. if ((old_ppr & MSG_EXT_PPR_IU_REQ)
  2889. != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
  2890. #ifdef AHD_DEBUG
  2891. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2892. ahd_print_devinfo(ahd, devinfo);
  2893. printf("Expecting IU Change busfree\n");
  2894. }
  2895. #endif
  2896. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  2897. | MSG_FLAG_IU_REQ_CHANGED;
  2898. }
  2899. if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
  2900. #ifdef AHD_DEBUG
  2901. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2902. printf("PPR with IU_REQ outstanding\n");
  2903. #endif
  2904. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
  2905. }
  2906. }
  2907. }
  2908. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2909. tinfo, AHD_NEG_TO_GOAL);
  2910. if (update_needed && active)
  2911. ahd_update_pending_scbs(ahd);
  2912. }
  2913. /*
  2914. * Update the user/goal/curr tables of wide negotiation
  2915. * parameters as well as, in the case of a current or active update,
  2916. * any data structures on the host controller. In the case of an
  2917. * active update, the specified target is currently talking to us on
  2918. * the bus, so the transfer parameter update must take effect
  2919. * immediately.
  2920. */
  2921. void
  2922. ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2923. u_int width, u_int type, int paused)
  2924. {
  2925. struct ahd_initiator_tinfo *tinfo;
  2926. struct ahd_tmode_tstate *tstate;
  2927. u_int oldwidth;
  2928. int active;
  2929. int update_needed;
  2930. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2931. update_needed = 0;
  2932. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2933. devinfo->target, &tstate);
  2934. if ((type & AHD_TRANS_USER) != 0)
  2935. tinfo->user.width = width;
  2936. if ((type & AHD_TRANS_GOAL) != 0)
  2937. tinfo->goal.width = width;
  2938. oldwidth = tinfo->curr.width;
  2939. if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
  2940. update_needed++;
  2941. tinfo->curr.width = width;
  2942. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2943. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2944. if (bootverbose) {
  2945. printf("%s: target %d using %dbit transfers\n",
  2946. ahd_name(ahd), devinfo->target,
  2947. 8 * (0x01 << width));
  2948. }
  2949. }
  2950. if ((type & AHD_TRANS_CUR) != 0) {
  2951. if (!paused)
  2952. ahd_pause(ahd);
  2953. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2954. if (!paused)
  2955. ahd_unpause(ahd);
  2956. }
  2957. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2958. tinfo, AHD_NEG_TO_GOAL);
  2959. if (update_needed && active)
  2960. ahd_update_pending_scbs(ahd);
  2961. }
  2962. /*
  2963. * Update the current state of tagged queuing for a given target.
  2964. */
  2965. void
  2966. ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2967. ahd_queue_alg alg)
  2968. {
  2969. ahd_platform_set_tags(ahd, devinfo, alg);
  2970. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2971. devinfo->lun, AC_TRANSFER_NEG, &alg);
  2972. }
  2973. static void
  2974. ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2975. struct ahd_transinfo *tinfo)
  2976. {
  2977. ahd_mode_state saved_modes;
  2978. u_int period;
  2979. u_int ppr_opts;
  2980. u_int con_opts;
  2981. u_int offset;
  2982. u_int saved_negoaddr;
  2983. uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
  2984. saved_modes = ahd_save_modes(ahd);
  2985. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2986. saved_negoaddr = ahd_inb(ahd, NEGOADDR);
  2987. ahd_outb(ahd, NEGOADDR, devinfo->target);
  2988. period = tinfo->period;
  2989. offset = tinfo->offset;
  2990. memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
  2991. ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
  2992. |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
  2993. con_opts = 0;
  2994. if (period == 0)
  2995. period = AHD_SYNCRATE_ASYNC;
  2996. if (period == AHD_SYNCRATE_160) {
  2997. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
  2998. /*
  2999. * When the SPI4 spec was finalized, PACE transfers
  3000. * was not made a configurable option in the PPR
  3001. * message. Instead it is assumed to be enabled for
  3002. * any syncrate faster than 80MHz. Nevertheless,
  3003. * Harpoon2A4 allows this to be configurable.
  3004. *
  3005. * Harpoon2A4 also assumes at most 2 data bytes per
  3006. * negotiated REQ/ACK offset. Paced transfers take
  3007. * 4, so we must adjust our offset.
  3008. */
  3009. ppr_opts |= PPROPT_PACE;
  3010. offset *= 2;
  3011. /*
  3012. * Harpoon2A assumed that there would be a
  3013. * fallback rate between 160MHz and 80Mhz,
  3014. * so 7 is used as the period factor rather
  3015. * than 8 for 160MHz.
  3016. */
  3017. period = AHD_SYNCRATE_REVA_160;
  3018. }
  3019. if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
  3020. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
  3021. ~AHD_PRECOMP_MASK;
  3022. } else {
  3023. /*
  3024. * Precomp should be disabled for non-paced transfers.
  3025. */
  3026. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
  3027. if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
  3028. && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0
  3029. && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) {
  3030. /*
  3031. * Slow down our CRC interval to be
  3032. * compatible with non-packetized
  3033. * U160 devices that can't handle a
  3034. * CRC at full speed.
  3035. */
  3036. con_opts |= ENSLOWCRC;
  3037. }
  3038. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
  3039. /*
  3040. * On H2A4, revert to a slower slewrate
  3041. * on non-paced transfers.
  3042. */
  3043. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
  3044. ~AHD_SLEWRATE_MASK;
  3045. }
  3046. }
  3047. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
  3048. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
  3049. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
  3050. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
  3051. ahd_outb(ahd, NEGPERIOD, period);
  3052. ahd_outb(ahd, NEGPPROPTS, ppr_opts);
  3053. ahd_outb(ahd, NEGOFFSET, offset);
  3054. if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
  3055. con_opts |= WIDEXFER;
  3056. /*
  3057. * Slow down our CRC interval to be
  3058. * compatible with packetized U320 devices
  3059. * that can't handle a CRC at full speed
  3060. */
  3061. if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
  3062. con_opts |= ENSLOWCRC;
  3063. }
  3064. /*
  3065. * During packetized transfers, the target will
  3066. * give us the oportunity to send command packets
  3067. * without us asserting attention.
  3068. */
  3069. if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  3070. con_opts |= ENAUTOATNO;
  3071. ahd_outb(ahd, NEGCONOPTS, con_opts);
  3072. ahd_outb(ahd, NEGOADDR, saved_negoaddr);
  3073. ahd_restore_modes(ahd, saved_modes);
  3074. }
  3075. /*
  3076. * When the transfer settings for a connection change, setup for
  3077. * negotiation in pending SCBs to effect the change as quickly as
  3078. * possible. We also cancel any negotiations that are scheduled
  3079. * for inflight SCBs that have not been started yet.
  3080. */
  3081. static void
  3082. ahd_update_pending_scbs(struct ahd_softc *ahd)
  3083. {
  3084. struct scb *pending_scb;
  3085. int pending_scb_count;
  3086. int paused;
  3087. u_int saved_scbptr;
  3088. ahd_mode_state saved_modes;
  3089. /*
  3090. * Traverse the pending SCB list and ensure that all of the
  3091. * SCBs there have the proper settings. We can only safely
  3092. * clear the negotiation required flag (setting requires the
  3093. * execution queue to be modified) and this is only possible
  3094. * if we are not already attempting to select out for this
  3095. * SCB. For this reason, all callers only call this routine
  3096. * if we are changing the negotiation settings for the currently
  3097. * active transaction on the bus.
  3098. */
  3099. pending_scb_count = 0;
  3100. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  3101. struct ahd_devinfo devinfo;
  3102. struct ahd_initiator_tinfo *tinfo;
  3103. struct ahd_tmode_tstate *tstate;
  3104. ahd_scb_devinfo(ahd, &devinfo, pending_scb);
  3105. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  3106. devinfo.our_scsiid,
  3107. devinfo.target, &tstate);
  3108. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  3109. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  3110. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  3111. pending_scb->hscb->control &= ~MK_MESSAGE;
  3112. }
  3113. ahd_sync_scb(ahd, pending_scb,
  3114. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  3115. pending_scb_count++;
  3116. }
  3117. if (pending_scb_count == 0)
  3118. return;
  3119. if (ahd_is_paused(ahd)) {
  3120. paused = 1;
  3121. } else {
  3122. paused = 0;
  3123. ahd_pause(ahd);
  3124. }
  3125. /*
  3126. * Force the sequencer to reinitialize the selection for
  3127. * the command at the head of the execution queue if it
  3128. * has already been setup. The negotiation changes may
  3129. * effect whether we select-out with ATN. It is only
  3130. * safe to clear ENSELO when the bus is not free and no
  3131. * selection is in progres or completed.
  3132. */
  3133. saved_modes = ahd_save_modes(ahd);
  3134. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3135. if ((ahd_inb(ahd, SCSISIGI) & BSYI) != 0
  3136. && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
  3137. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  3138. saved_scbptr = ahd_get_scbptr(ahd);
  3139. /* Ensure that the hscbs down on the card match the new information */
  3140. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  3141. u_int scb_tag;
  3142. u_int control;
  3143. scb_tag = SCB_GET_TAG(pending_scb);
  3144. ahd_set_scbptr(ahd, scb_tag);
  3145. control = ahd_inb_scbram(ahd, SCB_CONTROL);
  3146. control &= ~MK_MESSAGE;
  3147. control |= pending_scb->hscb->control & MK_MESSAGE;
  3148. ahd_outb(ahd, SCB_CONTROL, control);
  3149. }
  3150. ahd_set_scbptr(ahd, saved_scbptr);
  3151. ahd_restore_modes(ahd, saved_modes);
  3152. if (paused == 0)
  3153. ahd_unpause(ahd);
  3154. }
  3155. /**************************** Pathing Information *****************************/
  3156. static void
  3157. ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3158. {
  3159. ahd_mode_state saved_modes;
  3160. u_int saved_scsiid;
  3161. role_t role;
  3162. int our_id;
  3163. saved_modes = ahd_save_modes(ahd);
  3164. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3165. if (ahd_inb(ahd, SSTAT0) & TARGET)
  3166. role = ROLE_TARGET;
  3167. else
  3168. role = ROLE_INITIATOR;
  3169. if (role == ROLE_TARGET
  3170. && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
  3171. /* We were selected, so pull our id from TARGIDIN */
  3172. our_id = ahd_inb(ahd, TARGIDIN) & OID;
  3173. } else if (role == ROLE_TARGET)
  3174. our_id = ahd_inb(ahd, TOWNID);
  3175. else
  3176. our_id = ahd_inb(ahd, IOWNID);
  3177. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  3178. ahd_compile_devinfo(devinfo,
  3179. our_id,
  3180. SCSIID_TARGET(ahd, saved_scsiid),
  3181. ahd_inb(ahd, SAVED_LUN),
  3182. SCSIID_CHANNEL(ahd, saved_scsiid),
  3183. role);
  3184. ahd_restore_modes(ahd, saved_modes);
  3185. }
  3186. void
  3187. ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3188. {
  3189. printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
  3190. devinfo->target, devinfo->lun);
  3191. }
  3192. struct ahd_phase_table_entry*
  3193. ahd_lookup_phase_entry(int phase)
  3194. {
  3195. struct ahd_phase_table_entry *entry;
  3196. struct ahd_phase_table_entry *last_entry;
  3197. /*
  3198. * num_phases doesn't include the default entry which
  3199. * will be returned if the phase doesn't match.
  3200. */
  3201. last_entry = &ahd_phase_table[num_phases];
  3202. for (entry = ahd_phase_table; entry < last_entry; entry++) {
  3203. if (phase == entry->phase)
  3204. break;
  3205. }
  3206. return (entry);
  3207. }
  3208. void
  3209. ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
  3210. u_int lun, char channel, role_t role)
  3211. {
  3212. devinfo->our_scsiid = our_id;
  3213. devinfo->target = target;
  3214. devinfo->lun = lun;
  3215. devinfo->target_offset = target;
  3216. devinfo->channel = channel;
  3217. devinfo->role = role;
  3218. if (channel == 'B')
  3219. devinfo->target_offset += 8;
  3220. devinfo->target_mask = (0x01 << devinfo->target_offset);
  3221. }
  3222. static void
  3223. ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3224. struct scb *scb)
  3225. {
  3226. role_t role;
  3227. int our_id;
  3228. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  3229. role = ROLE_INITIATOR;
  3230. if ((scb->hscb->control & TARGET_SCB) != 0)
  3231. role = ROLE_TARGET;
  3232. ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
  3233. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
  3234. }
  3235. /************************ Message Phase Processing ****************************/
  3236. /*
  3237. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  3238. * or enters the initial message out phase, we are interrupted. Fill our
  3239. * outgoing message buffer with the appropriate message and beging handing
  3240. * the message phase(s) manually.
  3241. */
  3242. static void
  3243. ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3244. struct scb *scb)
  3245. {
  3246. /*
  3247. * To facilitate adding multiple messages together,
  3248. * each routine should increment the index and len
  3249. * variables instead of setting them explicitly.
  3250. */
  3251. ahd->msgout_index = 0;
  3252. ahd->msgout_len = 0;
  3253. if (ahd_currently_packetized(ahd))
  3254. ahd->msg_flags |= MSG_FLAG_PACKETIZED;
  3255. if (ahd->send_msg_perror
  3256. && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
  3257. ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
  3258. ahd->msgout_len++;
  3259. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3260. #ifdef AHD_DEBUG
  3261. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3262. printf("Setting up for Parity Error delivery\n");
  3263. #endif
  3264. return;
  3265. } else if (scb == NULL) {
  3266. printf("%s: WARNING. No pending message for "
  3267. "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
  3268. ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
  3269. ahd->msgout_len++;
  3270. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3271. return;
  3272. }
  3273. if ((scb->flags & SCB_DEVICE_RESET) == 0
  3274. && (scb->flags & SCB_PACKETIZED) == 0
  3275. && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
  3276. u_int identify_msg;
  3277. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  3278. if ((scb->hscb->control & DISCENB) != 0)
  3279. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  3280. ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
  3281. ahd->msgout_len++;
  3282. if ((scb->hscb->control & TAG_ENB) != 0) {
  3283. ahd->msgout_buf[ahd->msgout_index++] =
  3284. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  3285. ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
  3286. ahd->msgout_len += 2;
  3287. }
  3288. }
  3289. if (scb->flags & SCB_DEVICE_RESET) {
  3290. ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
  3291. ahd->msgout_len++;
  3292. ahd_print_path(ahd, scb);
  3293. printf("Bus Device Reset Message Sent\n");
  3294. /*
  3295. * Clear our selection hardware in advance of
  3296. * the busfree. We may have an entry in the waiting
  3297. * Q for this target, and we don't want to go about
  3298. * selecting while we handle the busfree and blow it
  3299. * away.
  3300. */
  3301. ahd_outb(ahd, SCSISEQ0, 0);
  3302. } else if ((scb->flags & SCB_ABORT) != 0) {
  3303. if ((scb->hscb->control & TAG_ENB) != 0) {
  3304. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
  3305. } else {
  3306. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
  3307. }
  3308. ahd->msgout_len++;
  3309. ahd_print_path(ahd, scb);
  3310. printf("Abort%s Message Sent\n",
  3311. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  3312. /*
  3313. * Clear our selection hardware in advance of
  3314. * the busfree. We may have an entry in the waiting
  3315. * Q for this target, and we don't want to go about
  3316. * selecting while we handle the busfree and blow it
  3317. * away.
  3318. */
  3319. ahd_outb(ahd, SCSISEQ0, 0);
  3320. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  3321. ahd_build_transfer_msg(ahd, devinfo);
  3322. /*
  3323. * Clear our selection hardware in advance of potential
  3324. * PPR IU status change busfree. We may have an entry in
  3325. * the waiting Q for this target, and we don't want to go
  3326. * about selecting while we handle the busfree and blow
  3327. * it away.
  3328. */
  3329. ahd_outb(ahd, SCSISEQ0, 0);
  3330. } else {
  3331. printf("ahd_intr: AWAITING_MSG for an SCB that "
  3332. "does not have a waiting message\n");
  3333. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  3334. devinfo->target_mask);
  3335. panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
  3336. "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
  3337. ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
  3338. scb->flags);
  3339. }
  3340. /*
  3341. * Clear the MK_MESSAGE flag from the SCB so we aren't
  3342. * asked to send this message again.
  3343. */
  3344. ahd_outb(ahd, SCB_CONTROL,
  3345. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  3346. scb->hscb->control &= ~MK_MESSAGE;
  3347. ahd->msgout_index = 0;
  3348. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3349. }
  3350. /*
  3351. * Build an appropriate transfer negotiation message for the
  3352. * currently active target.
  3353. */
  3354. static void
  3355. ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3356. {
  3357. /*
  3358. * We need to initiate transfer negotiations.
  3359. * If our current and goal settings are identical,
  3360. * we want to renegotiate due to a check condition.
  3361. */
  3362. struct ahd_initiator_tinfo *tinfo;
  3363. struct ahd_tmode_tstate *tstate;
  3364. int dowide;
  3365. int dosync;
  3366. int doppr;
  3367. u_int period;
  3368. u_int ppr_options;
  3369. u_int offset;
  3370. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3371. devinfo->target, &tstate);
  3372. /*
  3373. * Filter our period based on the current connection.
  3374. * If we can't perform DT transfers on this segment (not in LVD
  3375. * mode for instance), then our decision to issue a PPR message
  3376. * may change.
  3377. */
  3378. period = tinfo->goal.period;
  3379. offset = tinfo->goal.offset;
  3380. ppr_options = tinfo->goal.ppr_options;
  3381. /* Target initiated PPR is not allowed in the SCSI spec */
  3382. if (devinfo->role == ROLE_TARGET)
  3383. ppr_options = 0;
  3384. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3385. &ppr_options, devinfo->role);
  3386. dowide = tinfo->curr.width != tinfo->goal.width;
  3387. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  3388. /*
  3389. * Only use PPR if we have options that need it, even if the device
  3390. * claims to support it. There might be an expander in the way
  3391. * that doesn't.
  3392. */
  3393. doppr = ppr_options != 0;
  3394. if (!dowide && !dosync && !doppr) {
  3395. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  3396. dosync = tinfo->goal.offset != 0;
  3397. }
  3398. if (!dowide && !dosync && !doppr) {
  3399. /*
  3400. * Force async with a WDTR message if we have a wide bus,
  3401. * or just issue an SDTR with a 0 offset.
  3402. */
  3403. if ((ahd->features & AHD_WIDE) != 0)
  3404. dowide = 1;
  3405. else
  3406. dosync = 1;
  3407. if (bootverbose) {
  3408. ahd_print_devinfo(ahd, devinfo);
  3409. printf("Ensuring async\n");
  3410. }
  3411. }
  3412. /* Target initiated PPR is not allowed in the SCSI spec */
  3413. if (devinfo->role == ROLE_TARGET)
  3414. doppr = 0;
  3415. /*
  3416. * Both the PPR message and SDTR message require the
  3417. * goal syncrate to be limited to what the target device
  3418. * is capable of handling (based on whether an LVD->SE
  3419. * expander is on the bus), so combine these two cases.
  3420. * Regardless, guarantee that if we are using WDTR and SDTR
  3421. * messages that WDTR comes first.
  3422. */
  3423. if (doppr || (dosync && !dowide)) {
  3424. offset = tinfo->goal.offset;
  3425. ahd_validate_offset(ahd, tinfo, period, &offset,
  3426. doppr ? tinfo->goal.width
  3427. : tinfo->curr.width,
  3428. devinfo->role);
  3429. if (doppr) {
  3430. ahd_construct_ppr(ahd, devinfo, period, offset,
  3431. tinfo->goal.width, ppr_options);
  3432. } else {
  3433. ahd_construct_sdtr(ahd, devinfo, period, offset);
  3434. }
  3435. } else {
  3436. ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
  3437. }
  3438. }
  3439. /*
  3440. * Build a synchronous negotiation message in our message
  3441. * buffer based on the input parameters.
  3442. */
  3443. static void
  3444. ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3445. u_int period, u_int offset)
  3446. {
  3447. if (offset == 0)
  3448. period = AHD_ASYNC_XFER_PERIOD;
  3449. ahd->msgout_index += spi_populate_sync_msg(
  3450. ahd->msgout_buf + ahd->msgout_index, period, offset);
  3451. ahd->msgout_len += 5;
  3452. if (bootverbose) {
  3453. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  3454. ahd_name(ahd), devinfo->channel, devinfo->target,
  3455. devinfo->lun, period, offset);
  3456. }
  3457. }
  3458. /*
  3459. * Build a wide negotiateion message in our message
  3460. * buffer based on the input parameters.
  3461. */
  3462. static void
  3463. ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3464. u_int bus_width)
  3465. {
  3466. ahd->msgout_index += spi_populate_width_msg(
  3467. ahd->msgout_buf + ahd->msgout_index, bus_width);
  3468. ahd->msgout_len += 4;
  3469. if (bootverbose) {
  3470. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  3471. ahd_name(ahd), devinfo->channel, devinfo->target,
  3472. devinfo->lun, bus_width);
  3473. }
  3474. }
  3475. /*
  3476. * Build a parallel protocol request message in our message
  3477. * buffer based on the input parameters.
  3478. */
  3479. static void
  3480. ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3481. u_int period, u_int offset, u_int bus_width,
  3482. u_int ppr_options)
  3483. {
  3484. /*
  3485. * Always request precompensation from
  3486. * the other target if we are running
  3487. * at paced syncrates.
  3488. */
  3489. if (period <= AHD_SYNCRATE_PACED)
  3490. ppr_options |= MSG_EXT_PPR_PCOMP_EN;
  3491. if (offset == 0)
  3492. period = AHD_ASYNC_XFER_PERIOD;
  3493. ahd->msgout_index += spi_populate_ppr_msg(
  3494. ahd->msgout_buf + ahd->msgout_index, period, offset,
  3495. bus_width, ppr_options);
  3496. ahd->msgout_len += 8;
  3497. if (bootverbose) {
  3498. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  3499. "offset %x, ppr_options %x\n", ahd_name(ahd),
  3500. devinfo->channel, devinfo->target, devinfo->lun,
  3501. bus_width, period, offset, ppr_options);
  3502. }
  3503. }
  3504. /*
  3505. * Clear any active message state.
  3506. */
  3507. static void
  3508. ahd_clear_msg_state(struct ahd_softc *ahd)
  3509. {
  3510. ahd_mode_state saved_modes;
  3511. saved_modes = ahd_save_modes(ahd);
  3512. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3513. ahd->send_msg_perror = 0;
  3514. ahd->msg_flags = MSG_FLAG_NONE;
  3515. ahd->msgout_len = 0;
  3516. ahd->msgin_index = 0;
  3517. ahd->msg_type = MSG_TYPE_NONE;
  3518. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  3519. /*
  3520. * The target didn't care to respond to our
  3521. * message request, so clear ATN.
  3522. */
  3523. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3524. }
  3525. ahd_outb(ahd, MSG_OUT, MSG_NOOP);
  3526. ahd_outb(ahd, SEQ_FLAGS2,
  3527. ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  3528. ahd_restore_modes(ahd, saved_modes);
  3529. }
  3530. /*
  3531. * Manual message loop handler.
  3532. */
  3533. static void
  3534. ahd_handle_message_phase(struct ahd_softc *ahd)
  3535. {
  3536. struct ahd_devinfo devinfo;
  3537. u_int bus_phase;
  3538. int end_session;
  3539. ahd_fetch_devinfo(ahd, &devinfo);
  3540. end_session = FALSE;
  3541. bus_phase = ahd_inb(ahd, LASTPHASE);
  3542. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
  3543. printf("LQIRETRY for LQIPHASE_OUTPKT\n");
  3544. ahd_outb(ahd, LQCTL2, LQIRETRY);
  3545. }
  3546. reswitch:
  3547. switch (ahd->msg_type) {
  3548. case MSG_TYPE_INITIATOR_MSGOUT:
  3549. {
  3550. int lastbyte;
  3551. int phasemis;
  3552. int msgdone;
  3553. if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
  3554. panic("HOST_MSG_LOOP interrupt with no active message");
  3555. #ifdef AHD_DEBUG
  3556. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3557. ahd_print_devinfo(ahd, &devinfo);
  3558. printf("INITIATOR_MSG_OUT");
  3559. }
  3560. #endif
  3561. phasemis = bus_phase != P_MESGOUT;
  3562. if (phasemis) {
  3563. #ifdef AHD_DEBUG
  3564. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3565. printf(" PHASEMIS %s\n",
  3566. ahd_lookup_phase_entry(bus_phase)
  3567. ->phasemsg);
  3568. }
  3569. #endif
  3570. if (bus_phase == P_MESGIN) {
  3571. /*
  3572. * Change gears and see if
  3573. * this messages is of interest to
  3574. * us or should be passed back to
  3575. * the sequencer.
  3576. */
  3577. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3578. ahd->send_msg_perror = 0;
  3579. ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  3580. ahd->msgin_index = 0;
  3581. goto reswitch;
  3582. }
  3583. end_session = TRUE;
  3584. break;
  3585. }
  3586. if (ahd->send_msg_perror) {
  3587. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3588. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3589. #ifdef AHD_DEBUG
  3590. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3591. printf(" byte 0x%x\n", ahd->send_msg_perror);
  3592. #endif
  3593. /*
  3594. * If we are notifying the target of a CRC error
  3595. * during packetized operations, the target is
  3596. * within its rights to acknowledge our message
  3597. * with a busfree.
  3598. */
  3599. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
  3600. && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
  3601. ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
  3602. ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
  3603. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3604. break;
  3605. }
  3606. msgdone = ahd->msgout_index == ahd->msgout_len;
  3607. if (msgdone) {
  3608. /*
  3609. * The target has requested a retry.
  3610. * Re-assert ATN, reset our message index to
  3611. * 0, and try again.
  3612. */
  3613. ahd->msgout_index = 0;
  3614. ahd_assert_atn(ahd);
  3615. }
  3616. lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
  3617. if (lastbyte) {
  3618. /* Last byte is signified by dropping ATN */
  3619. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3620. }
  3621. /*
  3622. * Clear our interrupt status and present
  3623. * the next byte on the bus.
  3624. */
  3625. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3626. #ifdef AHD_DEBUG
  3627. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3628. printf(" byte 0x%x\n",
  3629. ahd->msgout_buf[ahd->msgout_index]);
  3630. #endif
  3631. ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
  3632. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3633. break;
  3634. }
  3635. case MSG_TYPE_INITIATOR_MSGIN:
  3636. {
  3637. int phasemis;
  3638. int message_done;
  3639. #ifdef AHD_DEBUG
  3640. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3641. ahd_print_devinfo(ahd, &devinfo);
  3642. printf("INITIATOR_MSG_IN");
  3643. }
  3644. #endif
  3645. phasemis = bus_phase != P_MESGIN;
  3646. if (phasemis) {
  3647. #ifdef AHD_DEBUG
  3648. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3649. printf(" PHASEMIS %s\n",
  3650. ahd_lookup_phase_entry(bus_phase)
  3651. ->phasemsg);
  3652. }
  3653. #endif
  3654. ahd->msgin_index = 0;
  3655. if (bus_phase == P_MESGOUT
  3656. && (ahd->send_msg_perror != 0
  3657. || (ahd->msgout_len != 0
  3658. && ahd->msgout_index == 0))) {
  3659. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3660. goto reswitch;
  3661. }
  3662. end_session = TRUE;
  3663. break;
  3664. }
  3665. /* Pull the byte in without acking it */
  3666. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
  3667. #ifdef AHD_DEBUG
  3668. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3669. printf(" byte 0x%x\n",
  3670. ahd->msgin_buf[ahd->msgin_index]);
  3671. #endif
  3672. message_done = ahd_parse_msg(ahd, &devinfo);
  3673. if (message_done) {
  3674. /*
  3675. * Clear our incoming message buffer in case there
  3676. * is another message following this one.
  3677. */
  3678. ahd->msgin_index = 0;
  3679. /*
  3680. * If this message illicited a response,
  3681. * assert ATN so the target takes us to the
  3682. * message out phase.
  3683. */
  3684. if (ahd->msgout_len != 0) {
  3685. #ifdef AHD_DEBUG
  3686. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3687. ahd_print_devinfo(ahd, &devinfo);
  3688. printf("Asserting ATN for response\n");
  3689. }
  3690. #endif
  3691. ahd_assert_atn(ahd);
  3692. }
  3693. } else
  3694. ahd->msgin_index++;
  3695. if (message_done == MSGLOOP_TERMINATED) {
  3696. end_session = TRUE;
  3697. } else {
  3698. /* Ack the byte */
  3699. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3700. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
  3701. }
  3702. break;
  3703. }
  3704. case MSG_TYPE_TARGET_MSGIN:
  3705. {
  3706. int msgdone;
  3707. int msgout_request;
  3708. /*
  3709. * By default, the message loop will continue.
  3710. */
  3711. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3712. if (ahd->msgout_len == 0)
  3713. panic("Target MSGIN with no active message");
  3714. /*
  3715. * If we interrupted a mesgout session, the initiator
  3716. * will not know this until our first REQ. So, we
  3717. * only honor mesgout requests after we've sent our
  3718. * first byte.
  3719. */
  3720. if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
  3721. && ahd->msgout_index > 0)
  3722. msgout_request = TRUE;
  3723. else
  3724. msgout_request = FALSE;
  3725. if (msgout_request) {
  3726. /*
  3727. * Change gears and see if
  3728. * this messages is of interest to
  3729. * us or should be passed back to
  3730. * the sequencer.
  3731. */
  3732. ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
  3733. ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
  3734. ahd->msgin_index = 0;
  3735. /* Dummy read to REQ for first byte */
  3736. ahd_inb(ahd, SCSIDAT);
  3737. ahd_outb(ahd, SXFRCTL0,
  3738. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3739. break;
  3740. }
  3741. msgdone = ahd->msgout_index == ahd->msgout_len;
  3742. if (msgdone) {
  3743. ahd_outb(ahd, SXFRCTL0,
  3744. ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3745. end_session = TRUE;
  3746. break;
  3747. }
  3748. /*
  3749. * Present the next byte on the bus.
  3750. */
  3751. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3752. ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
  3753. break;
  3754. }
  3755. case MSG_TYPE_TARGET_MSGOUT:
  3756. {
  3757. int lastbyte;
  3758. int msgdone;
  3759. /*
  3760. * By default, the message loop will continue.
  3761. */
  3762. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3763. /*
  3764. * The initiator signals that this is
  3765. * the last byte by dropping ATN.
  3766. */
  3767. lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
  3768. /*
  3769. * Read the latched byte, but turn off SPIOEN first
  3770. * so that we don't inadvertently cause a REQ for the
  3771. * next byte.
  3772. */
  3773. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3774. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
  3775. msgdone = ahd_parse_msg(ahd, &devinfo);
  3776. if (msgdone == MSGLOOP_TERMINATED) {
  3777. /*
  3778. * The message is *really* done in that it caused
  3779. * us to go to bus free. The sequencer has already
  3780. * been reset at this point, so pull the ejection
  3781. * handle.
  3782. */
  3783. return;
  3784. }
  3785. ahd->msgin_index++;
  3786. /*
  3787. * XXX Read spec about initiator dropping ATN too soon
  3788. * and use msgdone to detect it.
  3789. */
  3790. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  3791. ahd->msgin_index = 0;
  3792. /*
  3793. * If this message illicited a response, transition
  3794. * to the Message in phase and send it.
  3795. */
  3796. if (ahd->msgout_len != 0) {
  3797. ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
  3798. ahd_outb(ahd, SXFRCTL0,
  3799. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3800. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  3801. ahd->msgin_index = 0;
  3802. break;
  3803. }
  3804. }
  3805. if (lastbyte)
  3806. end_session = TRUE;
  3807. else {
  3808. /* Ask for the next byte. */
  3809. ahd_outb(ahd, SXFRCTL0,
  3810. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3811. }
  3812. break;
  3813. }
  3814. default:
  3815. panic("Unknown REQINIT message type");
  3816. }
  3817. if (end_session) {
  3818. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
  3819. printf("%s: Returning to Idle Loop\n",
  3820. ahd_name(ahd));
  3821. ahd_clear_msg_state(ahd);
  3822. /*
  3823. * Perform the equivalent of a clear_target_state.
  3824. */
  3825. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  3826. ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT);
  3827. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  3828. } else {
  3829. ahd_clear_msg_state(ahd);
  3830. ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
  3831. }
  3832. }
  3833. }
  3834. /*
  3835. * See if we sent a particular extended message to the target.
  3836. * If "full" is true, return true only if the target saw the full
  3837. * message. If "full" is false, return true if the target saw at
  3838. * least the first byte of the message.
  3839. */
  3840. static int
  3841. ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
  3842. {
  3843. int found;
  3844. u_int index;
  3845. found = FALSE;
  3846. index = 0;
  3847. while (index < ahd->msgout_len) {
  3848. if (ahd->msgout_buf[index] == MSG_EXTENDED) {
  3849. u_int end_index;
  3850. end_index = index + 1 + ahd->msgout_buf[index + 1];
  3851. if (ahd->msgout_buf[index+2] == msgval
  3852. && type == AHDMSG_EXT) {
  3853. if (full) {
  3854. if (ahd->msgout_index > end_index)
  3855. found = TRUE;
  3856. } else if (ahd->msgout_index > index)
  3857. found = TRUE;
  3858. }
  3859. index = end_index;
  3860. } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
  3861. && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  3862. /* Skip tag type and tag id or residue param*/
  3863. index += 2;
  3864. } else {
  3865. /* Single byte message */
  3866. if (type == AHDMSG_1B
  3867. && ahd->msgout_index > index
  3868. && (ahd->msgout_buf[index] == msgval
  3869. || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
  3870. && msgval == MSG_IDENTIFYFLAG)))
  3871. found = TRUE;
  3872. index++;
  3873. }
  3874. if (found)
  3875. break;
  3876. }
  3877. return (found);
  3878. }
  3879. /*
  3880. * Wait for a complete incoming message, parse it, and respond accordingly.
  3881. */
  3882. static int
  3883. ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3884. {
  3885. struct ahd_initiator_tinfo *tinfo;
  3886. struct ahd_tmode_tstate *tstate;
  3887. int reject;
  3888. int done;
  3889. int response;
  3890. done = MSGLOOP_IN_PROG;
  3891. response = FALSE;
  3892. reject = FALSE;
  3893. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3894. devinfo->target, &tstate);
  3895. /*
  3896. * Parse as much of the message as is available,
  3897. * rejecting it if we don't support it. When
  3898. * the entire message is available and has been
  3899. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  3900. * that we have parsed an entire message.
  3901. *
  3902. * In the case of extended messages, we accept the length
  3903. * byte outright and perform more checking once we know the
  3904. * extended message type.
  3905. */
  3906. switch (ahd->msgin_buf[0]) {
  3907. case MSG_DISCONNECT:
  3908. case MSG_SAVEDATAPOINTER:
  3909. case MSG_CMDCOMPLETE:
  3910. case MSG_RESTOREPOINTERS:
  3911. case MSG_IGN_WIDE_RESIDUE:
  3912. /*
  3913. * End our message loop as these are messages
  3914. * the sequencer handles on its own.
  3915. */
  3916. done = MSGLOOP_TERMINATED;
  3917. break;
  3918. case MSG_MESSAGE_REJECT:
  3919. response = ahd_handle_msg_reject(ahd, devinfo);
  3920. /* FALLTHROUGH */
  3921. case MSG_NOOP:
  3922. done = MSGLOOP_MSGCOMPLETE;
  3923. break;
  3924. case MSG_EXTENDED:
  3925. {
  3926. /* Wait for enough of the message to begin validation */
  3927. if (ahd->msgin_index < 2)
  3928. break;
  3929. switch (ahd->msgin_buf[2]) {
  3930. case MSG_EXT_SDTR:
  3931. {
  3932. u_int period;
  3933. u_int ppr_options;
  3934. u_int offset;
  3935. u_int saved_offset;
  3936. if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  3937. reject = TRUE;
  3938. break;
  3939. }
  3940. /*
  3941. * Wait until we have both args before validating
  3942. * and acting on this message.
  3943. *
  3944. * Add one to MSG_EXT_SDTR_LEN to account for
  3945. * the extended message preamble.
  3946. */
  3947. if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  3948. break;
  3949. period = ahd->msgin_buf[3];
  3950. ppr_options = 0;
  3951. saved_offset = offset = ahd->msgin_buf[4];
  3952. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3953. &ppr_options, devinfo->role);
  3954. ahd_validate_offset(ahd, tinfo, period, &offset,
  3955. tinfo->curr.width, devinfo->role);
  3956. if (bootverbose) {
  3957. printf("(%s:%c:%d:%d): Received "
  3958. "SDTR period %x, offset %x\n\t"
  3959. "Filtered to period %x, offset %x\n",
  3960. ahd_name(ahd), devinfo->channel,
  3961. devinfo->target, devinfo->lun,
  3962. ahd->msgin_buf[3], saved_offset,
  3963. period, offset);
  3964. }
  3965. ahd_set_syncrate(ahd, devinfo, period,
  3966. offset, ppr_options,
  3967. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  3968. /*paused*/TRUE);
  3969. /*
  3970. * See if we initiated Sync Negotiation
  3971. * and didn't have to fall down to async
  3972. * transfers.
  3973. */
  3974. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  3975. /* We started it */
  3976. if (saved_offset != offset) {
  3977. /* Went too low - force async */
  3978. reject = TRUE;
  3979. }
  3980. } else {
  3981. /*
  3982. * Send our own SDTR in reply
  3983. */
  3984. if (bootverbose
  3985. && devinfo->role == ROLE_INITIATOR) {
  3986. printf("(%s:%c:%d:%d): Target "
  3987. "Initiated SDTR\n",
  3988. ahd_name(ahd), devinfo->channel,
  3989. devinfo->target, devinfo->lun);
  3990. }
  3991. ahd->msgout_index = 0;
  3992. ahd->msgout_len = 0;
  3993. ahd_construct_sdtr(ahd, devinfo,
  3994. period, offset);
  3995. ahd->msgout_index = 0;
  3996. response = TRUE;
  3997. }
  3998. done = MSGLOOP_MSGCOMPLETE;
  3999. break;
  4000. }
  4001. case MSG_EXT_WDTR:
  4002. {
  4003. u_int bus_width;
  4004. u_int saved_width;
  4005. u_int sending_reply;
  4006. sending_reply = FALSE;
  4007. if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  4008. reject = TRUE;
  4009. break;
  4010. }
  4011. /*
  4012. * Wait until we have our arg before validating
  4013. * and acting on this message.
  4014. *
  4015. * Add one to MSG_EXT_WDTR_LEN to account for
  4016. * the extended message preamble.
  4017. */
  4018. if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  4019. break;
  4020. bus_width = ahd->msgin_buf[3];
  4021. saved_width = bus_width;
  4022. ahd_validate_width(ahd, tinfo, &bus_width,
  4023. devinfo->role);
  4024. if (bootverbose) {
  4025. printf("(%s:%c:%d:%d): Received WDTR "
  4026. "%x filtered to %x\n",
  4027. ahd_name(ahd), devinfo->channel,
  4028. devinfo->target, devinfo->lun,
  4029. saved_width, bus_width);
  4030. }
  4031. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  4032. /*
  4033. * Don't send a WDTR back to the
  4034. * target, since we asked first.
  4035. * If the width went higher than our
  4036. * request, reject it.
  4037. */
  4038. if (saved_width > bus_width) {
  4039. reject = TRUE;
  4040. printf("(%s:%c:%d:%d): requested %dBit "
  4041. "transfers. Rejecting...\n",
  4042. ahd_name(ahd), devinfo->channel,
  4043. devinfo->target, devinfo->lun,
  4044. 8 * (0x01 << bus_width));
  4045. bus_width = 0;
  4046. }
  4047. } else {
  4048. /*
  4049. * Send our own WDTR in reply
  4050. */
  4051. if (bootverbose
  4052. && devinfo->role == ROLE_INITIATOR) {
  4053. printf("(%s:%c:%d:%d): Target "
  4054. "Initiated WDTR\n",
  4055. ahd_name(ahd), devinfo->channel,
  4056. devinfo->target, devinfo->lun);
  4057. }
  4058. ahd->msgout_index = 0;
  4059. ahd->msgout_len = 0;
  4060. ahd_construct_wdtr(ahd, devinfo, bus_width);
  4061. ahd->msgout_index = 0;
  4062. response = TRUE;
  4063. sending_reply = TRUE;
  4064. }
  4065. /*
  4066. * After a wide message, we are async, but
  4067. * some devices don't seem to honor this portion
  4068. * of the spec. Force a renegotiation of the
  4069. * sync component of our transfer agreement even
  4070. * if our goal is async. By updating our width
  4071. * after forcing the negotiation, we avoid
  4072. * renegotiating for width.
  4073. */
  4074. ahd_update_neg_request(ahd, devinfo, tstate,
  4075. tinfo, AHD_NEG_ALWAYS);
  4076. ahd_set_width(ahd, devinfo, bus_width,
  4077. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4078. /*paused*/TRUE);
  4079. if (sending_reply == FALSE && reject == FALSE) {
  4080. /*
  4081. * We will always have an SDTR to send.
  4082. */
  4083. ahd->msgout_index = 0;
  4084. ahd->msgout_len = 0;
  4085. ahd_build_transfer_msg(ahd, devinfo);
  4086. ahd->msgout_index = 0;
  4087. response = TRUE;
  4088. }
  4089. done = MSGLOOP_MSGCOMPLETE;
  4090. break;
  4091. }
  4092. case MSG_EXT_PPR:
  4093. {
  4094. u_int period;
  4095. u_int offset;
  4096. u_int bus_width;
  4097. u_int ppr_options;
  4098. u_int saved_width;
  4099. u_int saved_offset;
  4100. u_int saved_ppr_options;
  4101. if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  4102. reject = TRUE;
  4103. break;
  4104. }
  4105. /*
  4106. * Wait until we have all args before validating
  4107. * and acting on this message.
  4108. *
  4109. * Add one to MSG_EXT_PPR_LEN to account for
  4110. * the extended message preamble.
  4111. */
  4112. if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
  4113. break;
  4114. period = ahd->msgin_buf[3];
  4115. offset = ahd->msgin_buf[5];
  4116. bus_width = ahd->msgin_buf[6];
  4117. saved_width = bus_width;
  4118. ppr_options = ahd->msgin_buf[7];
  4119. /*
  4120. * According to the spec, a DT only
  4121. * period factor with no DT option
  4122. * set implies async.
  4123. */
  4124. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  4125. && period <= 9)
  4126. offset = 0;
  4127. saved_ppr_options = ppr_options;
  4128. saved_offset = offset;
  4129. /*
  4130. * Transfer options are only available if we
  4131. * are negotiating wide.
  4132. */
  4133. if (bus_width == 0)
  4134. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  4135. ahd_validate_width(ahd, tinfo, &bus_width,
  4136. devinfo->role);
  4137. ahd_devlimited_syncrate(ahd, tinfo, &period,
  4138. &ppr_options, devinfo->role);
  4139. ahd_validate_offset(ahd, tinfo, period, &offset,
  4140. bus_width, devinfo->role);
  4141. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
  4142. /*
  4143. * If we are unable to do any of the
  4144. * requested options (we went too low),
  4145. * then we'll have to reject the message.
  4146. */
  4147. if (saved_width > bus_width
  4148. || saved_offset != offset
  4149. || saved_ppr_options != ppr_options) {
  4150. reject = TRUE;
  4151. period = 0;
  4152. offset = 0;
  4153. bus_width = 0;
  4154. ppr_options = 0;
  4155. }
  4156. } else {
  4157. if (devinfo->role != ROLE_TARGET)
  4158. printf("(%s:%c:%d:%d): Target "
  4159. "Initiated PPR\n",
  4160. ahd_name(ahd), devinfo->channel,
  4161. devinfo->target, devinfo->lun);
  4162. else
  4163. printf("(%s:%c:%d:%d): Initiator "
  4164. "Initiated PPR\n",
  4165. ahd_name(ahd), devinfo->channel,
  4166. devinfo->target, devinfo->lun);
  4167. ahd->msgout_index = 0;
  4168. ahd->msgout_len = 0;
  4169. ahd_construct_ppr(ahd, devinfo, period, offset,
  4170. bus_width, ppr_options);
  4171. ahd->msgout_index = 0;
  4172. response = TRUE;
  4173. }
  4174. if (bootverbose) {
  4175. printf("(%s:%c:%d:%d): Received PPR width %x, "
  4176. "period %x, offset %x,options %x\n"
  4177. "\tFiltered to width %x, period %x, "
  4178. "offset %x, options %x\n",
  4179. ahd_name(ahd), devinfo->channel,
  4180. devinfo->target, devinfo->lun,
  4181. saved_width, ahd->msgin_buf[3],
  4182. saved_offset, saved_ppr_options,
  4183. bus_width, period, offset, ppr_options);
  4184. }
  4185. ahd_set_width(ahd, devinfo, bus_width,
  4186. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4187. /*paused*/TRUE);
  4188. ahd_set_syncrate(ahd, devinfo, period,
  4189. offset, ppr_options,
  4190. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4191. /*paused*/TRUE);
  4192. done = MSGLOOP_MSGCOMPLETE;
  4193. break;
  4194. }
  4195. default:
  4196. /* Unknown extended message. Reject it. */
  4197. reject = TRUE;
  4198. break;
  4199. }
  4200. break;
  4201. }
  4202. #ifdef AHD_TARGET_MODE
  4203. case MSG_BUS_DEV_RESET:
  4204. ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
  4205. CAM_BDR_SENT,
  4206. "Bus Device Reset Received",
  4207. /*verbose_level*/0);
  4208. ahd_restart(ahd);
  4209. done = MSGLOOP_TERMINATED;
  4210. break;
  4211. case MSG_ABORT_TAG:
  4212. case MSG_ABORT:
  4213. case MSG_CLEAR_QUEUE:
  4214. {
  4215. int tag;
  4216. /* Target mode messages */
  4217. if (devinfo->role != ROLE_TARGET) {
  4218. reject = TRUE;
  4219. break;
  4220. }
  4221. tag = SCB_LIST_NULL;
  4222. if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
  4223. tag = ahd_inb(ahd, INITIATOR_TAG);
  4224. ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4225. devinfo->lun, tag, ROLE_TARGET,
  4226. CAM_REQ_ABORTED);
  4227. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4228. if (tstate != NULL) {
  4229. struct ahd_tmode_lstate* lstate;
  4230. lstate = tstate->enabled_luns[devinfo->lun];
  4231. if (lstate != NULL) {
  4232. ahd_queue_lstate_event(ahd, lstate,
  4233. devinfo->our_scsiid,
  4234. ahd->msgin_buf[0],
  4235. /*arg*/tag);
  4236. ahd_send_lstate_events(ahd, lstate);
  4237. }
  4238. }
  4239. ahd_restart(ahd);
  4240. done = MSGLOOP_TERMINATED;
  4241. break;
  4242. }
  4243. #endif
  4244. case MSG_QAS_REQUEST:
  4245. #ifdef AHD_DEBUG
  4246. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  4247. printf("%s: QAS request. SCSISIGI == 0x%x\n",
  4248. ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
  4249. #endif
  4250. ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
  4251. /* FALLTHROUGH */
  4252. case MSG_TERM_IO_PROC:
  4253. default:
  4254. reject = TRUE;
  4255. break;
  4256. }
  4257. if (reject) {
  4258. /*
  4259. * Setup to reject the message.
  4260. */
  4261. ahd->msgout_index = 0;
  4262. ahd->msgout_len = 1;
  4263. ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
  4264. done = MSGLOOP_MSGCOMPLETE;
  4265. response = TRUE;
  4266. }
  4267. if (done != MSGLOOP_IN_PROG && !response)
  4268. /* Clear the outgoing message buffer */
  4269. ahd->msgout_len = 0;
  4270. return (done);
  4271. }
  4272. /*
  4273. * Process a message reject message.
  4274. */
  4275. static int
  4276. ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4277. {
  4278. /*
  4279. * What we care about here is if we had an
  4280. * outstanding SDTR or WDTR message for this
  4281. * target. If we did, this is a signal that
  4282. * the target is refusing negotiation.
  4283. */
  4284. struct scb *scb;
  4285. struct ahd_initiator_tinfo *tinfo;
  4286. struct ahd_tmode_tstate *tstate;
  4287. u_int scb_index;
  4288. u_int last_msg;
  4289. int response = 0;
  4290. scb_index = ahd_get_scbptr(ahd);
  4291. scb = ahd_lookup_scb(ahd, scb_index);
  4292. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
  4293. devinfo->our_scsiid,
  4294. devinfo->target, &tstate);
  4295. /* Might be necessary */
  4296. last_msg = ahd_inb(ahd, LAST_MSG);
  4297. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  4298. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
  4299. && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
  4300. /*
  4301. * Target may not like our SPI-4 PPR Options.
  4302. * Attempt to negotiate 80MHz which will turn
  4303. * off these options.
  4304. */
  4305. if (bootverbose) {
  4306. printf("(%s:%c:%d:%d): PPR Rejected. "
  4307. "Trying simple U160 PPR\n",
  4308. ahd_name(ahd), devinfo->channel,
  4309. devinfo->target, devinfo->lun);
  4310. }
  4311. tinfo->goal.period = AHD_SYNCRATE_DT;
  4312. tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
  4313. | MSG_EXT_PPR_QAS_REQ
  4314. | MSG_EXT_PPR_DT_REQ;
  4315. } else {
  4316. /*
  4317. * Target does not support the PPR message.
  4318. * Attempt to negotiate SPI-2 style.
  4319. */
  4320. if (bootverbose) {
  4321. printf("(%s:%c:%d:%d): PPR Rejected. "
  4322. "Trying WDTR/SDTR\n",
  4323. ahd_name(ahd), devinfo->channel,
  4324. devinfo->target, devinfo->lun);
  4325. }
  4326. tinfo->goal.ppr_options = 0;
  4327. tinfo->curr.transport_version = 2;
  4328. tinfo->goal.transport_version = 2;
  4329. }
  4330. ahd->msgout_index = 0;
  4331. ahd->msgout_len = 0;
  4332. ahd_build_transfer_msg(ahd, devinfo);
  4333. ahd->msgout_index = 0;
  4334. response = 1;
  4335. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  4336. /* note 8bit xfers */
  4337. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  4338. "8bit transfers\n", ahd_name(ahd),
  4339. devinfo->channel, devinfo->target, devinfo->lun);
  4340. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4341. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4342. /*paused*/TRUE);
  4343. /*
  4344. * No need to clear the sync rate. If the target
  4345. * did not accept the command, our syncrate is
  4346. * unaffected. If the target started the negotiation,
  4347. * but rejected our response, we already cleared the
  4348. * sync rate before sending our WDTR.
  4349. */
  4350. if (tinfo->goal.offset != tinfo->curr.offset) {
  4351. /* Start the sync negotiation */
  4352. ahd->msgout_index = 0;
  4353. ahd->msgout_len = 0;
  4354. ahd_build_transfer_msg(ahd, devinfo);
  4355. ahd->msgout_index = 0;
  4356. response = 1;
  4357. }
  4358. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  4359. /* note asynch xfers and clear flag */
  4360. ahd_set_syncrate(ahd, devinfo, /*period*/0,
  4361. /*offset*/0, /*ppr_options*/0,
  4362. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4363. /*paused*/TRUE);
  4364. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  4365. "Using asynchronous transfers\n",
  4366. ahd_name(ahd), devinfo->channel,
  4367. devinfo->target, devinfo->lun);
  4368. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  4369. int tag_type;
  4370. int mask;
  4371. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  4372. if (tag_type == MSG_SIMPLE_TASK) {
  4373. printf("(%s:%c:%d:%d): refuses tagged commands. "
  4374. "Performing non-tagged I/O\n", ahd_name(ahd),
  4375. devinfo->channel, devinfo->target, devinfo->lun);
  4376. ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE);
  4377. mask = ~0x23;
  4378. } else {
  4379. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  4380. "Performing simple queue tagged I/O only\n",
  4381. ahd_name(ahd), devinfo->channel, devinfo->target,
  4382. devinfo->lun, tag_type == MSG_ORDERED_TASK
  4383. ? "ordered" : "head of queue");
  4384. ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC);
  4385. mask = ~0x03;
  4386. }
  4387. /*
  4388. * Resend the identify for this CCB as the target
  4389. * may believe that the selection is invalid otherwise.
  4390. */
  4391. ahd_outb(ahd, SCB_CONTROL,
  4392. ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
  4393. scb->hscb->control &= mask;
  4394. ahd_set_transaction_tag(scb, /*enabled*/FALSE,
  4395. /*type*/MSG_SIMPLE_TASK);
  4396. ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
  4397. ahd_assert_atn(ahd);
  4398. ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  4399. SCB_GET_TAG(scb));
  4400. /*
  4401. * Requeue all tagged commands for this target
  4402. * currently in our posession so they can be
  4403. * converted to untagged commands.
  4404. */
  4405. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  4406. SCB_GET_CHANNEL(ahd, scb),
  4407. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  4408. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  4409. SEARCH_COMPLETE);
  4410. } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
  4411. /*
  4412. * Most likely the device believes that we had
  4413. * previously negotiated packetized.
  4414. */
  4415. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  4416. | MSG_FLAG_IU_REQ_CHANGED;
  4417. ahd_force_renegotiation(ahd, devinfo);
  4418. ahd->msgout_index = 0;
  4419. ahd->msgout_len = 0;
  4420. ahd_build_transfer_msg(ahd, devinfo);
  4421. ahd->msgout_index = 0;
  4422. response = 1;
  4423. } else {
  4424. /*
  4425. * Otherwise, we ignore it.
  4426. */
  4427. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  4428. ahd_name(ahd), devinfo->channel, devinfo->target,
  4429. last_msg);
  4430. }
  4431. return (response);
  4432. }
  4433. /*
  4434. * Process an ingnore wide residue message.
  4435. */
  4436. static void
  4437. ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4438. {
  4439. u_int scb_index;
  4440. struct scb *scb;
  4441. scb_index = ahd_get_scbptr(ahd);
  4442. scb = ahd_lookup_scb(ahd, scb_index);
  4443. /*
  4444. * XXX Actually check data direction in the sequencer?
  4445. * Perhaps add datadir to some spare bits in the hscb?
  4446. */
  4447. if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
  4448. || ahd_get_transfer_dir(scb) != CAM_DIR_IN) {
  4449. /*
  4450. * Ignore the message if we haven't
  4451. * seen an appropriate data phase yet.
  4452. */
  4453. } else {
  4454. /*
  4455. * If the residual occurred on the last
  4456. * transfer and the transfer request was
  4457. * expected to end on an odd count, do
  4458. * nothing. Otherwise, subtract a byte
  4459. * and update the residual count accordingly.
  4460. */
  4461. uint32_t sgptr;
  4462. sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4463. if ((sgptr & SG_LIST_NULL) != 0
  4464. && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4465. & SCB_XFERLEN_ODD) != 0) {
  4466. /*
  4467. * If the residual occurred on the last
  4468. * transfer and the transfer request was
  4469. * expected to end on an odd count, do
  4470. * nothing.
  4471. */
  4472. } else {
  4473. uint32_t data_cnt;
  4474. uint64_t data_addr;
  4475. uint32_t sglen;
  4476. /* Pull in the rest of the sgptr */
  4477. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4478. data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4479. if ((sgptr & SG_LIST_NULL) != 0) {
  4480. /*
  4481. * The residual data count is not updated
  4482. * for the command run to completion case.
  4483. * Explicitly zero the count.
  4484. */
  4485. data_cnt &= ~AHD_SG_LEN_MASK;
  4486. }
  4487. data_addr = ahd_inq(ahd, SHADDR);
  4488. data_cnt += 1;
  4489. data_addr -= 1;
  4490. sgptr &= SG_PTR_MASK;
  4491. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4492. struct ahd_dma64_seg *sg;
  4493. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4494. /*
  4495. * The residual sg ptr points to the next S/G
  4496. * to load so we must go back one.
  4497. */
  4498. sg--;
  4499. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4500. if (sg != scb->sg_list
  4501. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4502. sg--;
  4503. sglen = ahd_le32toh(sg->len);
  4504. /*
  4505. * Preserve High Address and SG_LIST
  4506. * bits while setting the count to 1.
  4507. */
  4508. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4509. data_addr = ahd_le64toh(sg->addr)
  4510. + (sglen & AHD_SG_LEN_MASK)
  4511. - 1;
  4512. /*
  4513. * Increment sg so it points to the
  4514. * "next" sg.
  4515. */
  4516. sg++;
  4517. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4518. sg);
  4519. }
  4520. } else {
  4521. struct ahd_dma_seg *sg;
  4522. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4523. /*
  4524. * The residual sg ptr points to the next S/G
  4525. * to load so we must go back one.
  4526. */
  4527. sg--;
  4528. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4529. if (sg != scb->sg_list
  4530. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4531. sg--;
  4532. sglen = ahd_le32toh(sg->len);
  4533. /*
  4534. * Preserve High Address and SG_LIST
  4535. * bits while setting the count to 1.
  4536. */
  4537. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4538. data_addr = ahd_le32toh(sg->addr)
  4539. + (sglen & AHD_SG_LEN_MASK)
  4540. - 1;
  4541. /*
  4542. * Increment sg so it points to the
  4543. * "next" sg.
  4544. */
  4545. sg++;
  4546. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4547. sg);
  4548. }
  4549. }
  4550. /*
  4551. * Toggle the "oddness" of the transfer length
  4552. * to handle this mid-transfer ignore wide
  4553. * residue. This ensures that the oddness is
  4554. * correct for subsequent data transfers.
  4555. */
  4556. ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
  4557. ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4558. ^ SCB_XFERLEN_ODD);
  4559. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  4560. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
  4561. /*
  4562. * The FIFO's pointers will be updated if/when the
  4563. * sequencer re-enters a data phase.
  4564. */
  4565. }
  4566. }
  4567. }
  4568. /*
  4569. * Reinitialize the data pointers for the active transfer
  4570. * based on its current residual.
  4571. */
  4572. static void
  4573. ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
  4574. {
  4575. struct scb *scb;
  4576. ahd_mode_state saved_modes;
  4577. u_int scb_index;
  4578. u_int wait;
  4579. uint32_t sgptr;
  4580. uint32_t resid;
  4581. uint64_t dataptr;
  4582. AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
  4583. AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
  4584. scb_index = ahd_get_scbptr(ahd);
  4585. scb = ahd_lookup_scb(ahd, scb_index);
  4586. /*
  4587. * Release and reacquire the FIFO so we
  4588. * have a clean slate.
  4589. */
  4590. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  4591. wait = 1000;
  4592. while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE))
  4593. ahd_delay(100);
  4594. if (wait == 0) {
  4595. ahd_print_path(ahd, scb);
  4596. printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
  4597. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  4598. }
  4599. saved_modes = ahd_save_modes(ahd);
  4600. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4601. ahd_outb(ahd, DFFSTAT,
  4602. ahd_inb(ahd, DFFSTAT)
  4603. | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
  4604. /*
  4605. * Determine initial values for data_addr and data_cnt
  4606. * for resuming the data phase.
  4607. */
  4608. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4609. sgptr &= SG_PTR_MASK;
  4610. resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
  4611. | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
  4612. | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4613. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4614. struct ahd_dma64_seg *sg;
  4615. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4616. /* The residual sg_ptr always points to the next sg */
  4617. sg--;
  4618. dataptr = ahd_le64toh(sg->addr)
  4619. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4620. - resid;
  4621. ahd_outl(ahd, HADDR + 4, dataptr >> 32);
  4622. } else {
  4623. struct ahd_dma_seg *sg;
  4624. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4625. /* The residual sg_ptr always points to the next sg */
  4626. sg--;
  4627. dataptr = ahd_le32toh(sg->addr)
  4628. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4629. - resid;
  4630. ahd_outb(ahd, HADDR + 4,
  4631. (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
  4632. }
  4633. ahd_outl(ahd, HADDR, dataptr);
  4634. ahd_outb(ahd, HCNT + 2, resid >> 16);
  4635. ahd_outb(ahd, HCNT + 1, resid >> 8);
  4636. ahd_outb(ahd, HCNT, resid);
  4637. }
  4638. /*
  4639. * Handle the effects of issuing a bus device reset message.
  4640. */
  4641. static void
  4642. ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4643. u_int lun, cam_status status, char *message,
  4644. int verbose_level)
  4645. {
  4646. #ifdef AHD_TARGET_MODE
  4647. struct ahd_tmode_tstate* tstate;
  4648. #endif
  4649. int found;
  4650. found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4651. lun, SCB_LIST_NULL, devinfo->role,
  4652. status);
  4653. #ifdef AHD_TARGET_MODE
  4654. /*
  4655. * Send an immediate notify ccb to all target mord peripheral
  4656. * drivers affected by this action.
  4657. */
  4658. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4659. if (tstate != NULL) {
  4660. u_int cur_lun;
  4661. u_int max_lun;
  4662. if (lun != CAM_LUN_WILDCARD) {
  4663. cur_lun = 0;
  4664. max_lun = AHD_NUM_LUNS - 1;
  4665. } else {
  4666. cur_lun = lun;
  4667. max_lun = lun;
  4668. }
  4669. for (cur_lun <= max_lun; cur_lun++) {
  4670. struct ahd_tmode_lstate* lstate;
  4671. lstate = tstate->enabled_luns[cur_lun];
  4672. if (lstate == NULL)
  4673. continue;
  4674. ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
  4675. MSG_BUS_DEV_RESET, /*arg*/0);
  4676. ahd_send_lstate_events(ahd, lstate);
  4677. }
  4678. }
  4679. #endif
  4680. /*
  4681. * Go back to async/narrow transfers and renegotiate.
  4682. */
  4683. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4684. AHD_TRANS_CUR, /*paused*/TRUE);
  4685. ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
  4686. /*ppr_options*/0, AHD_TRANS_CUR,
  4687. /*paused*/TRUE);
  4688. if (status != CAM_SEL_TIMEOUT)
  4689. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  4690. CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
  4691. if (message != NULL && bootverbose)
  4692. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
  4693. message, devinfo->channel, devinfo->target, found);
  4694. }
  4695. #ifdef AHD_TARGET_MODE
  4696. static void
  4697. ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4698. struct scb *scb)
  4699. {
  4700. /*
  4701. * To facilitate adding multiple messages together,
  4702. * each routine should increment the index and len
  4703. * variables instead of setting them explicitly.
  4704. */
  4705. ahd->msgout_index = 0;
  4706. ahd->msgout_len = 0;
  4707. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  4708. ahd_build_transfer_msg(ahd, devinfo);
  4709. else
  4710. panic("ahd_intr: AWAITING target message with no message");
  4711. ahd->msgout_index = 0;
  4712. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  4713. }
  4714. #endif
  4715. /**************************** Initialization **********************************/
  4716. static u_int
  4717. ahd_sglist_size(struct ahd_softc *ahd)
  4718. {
  4719. bus_size_t list_size;
  4720. list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
  4721. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  4722. list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
  4723. return (list_size);
  4724. }
  4725. /*
  4726. * Calculate the optimum S/G List allocation size. S/G elements used
  4727. * for a given transaction must be physically contiguous. Assume the
  4728. * OS will allocate full pages to us, so it doesn't make sense to request
  4729. * less than a page.
  4730. */
  4731. static u_int
  4732. ahd_sglist_allocsize(struct ahd_softc *ahd)
  4733. {
  4734. bus_size_t sg_list_increment;
  4735. bus_size_t sg_list_size;
  4736. bus_size_t max_list_size;
  4737. bus_size_t best_list_size;
  4738. /* Start out with the minimum required for AHD_NSEG. */
  4739. sg_list_increment = ahd_sglist_size(ahd);
  4740. sg_list_size = sg_list_increment;
  4741. /* Get us as close as possible to a page in size. */
  4742. while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
  4743. sg_list_size += sg_list_increment;
  4744. /*
  4745. * Try to reduce the amount of wastage by allocating
  4746. * multiple pages.
  4747. */
  4748. best_list_size = sg_list_size;
  4749. max_list_size = roundup(sg_list_increment, PAGE_SIZE);
  4750. if (max_list_size < 4 * PAGE_SIZE)
  4751. max_list_size = 4 * PAGE_SIZE;
  4752. if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
  4753. max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
  4754. while ((sg_list_size + sg_list_increment) <= max_list_size
  4755. && (sg_list_size % PAGE_SIZE) != 0) {
  4756. bus_size_t new_mod;
  4757. bus_size_t best_mod;
  4758. sg_list_size += sg_list_increment;
  4759. new_mod = sg_list_size % PAGE_SIZE;
  4760. best_mod = best_list_size % PAGE_SIZE;
  4761. if (new_mod > best_mod || new_mod == 0) {
  4762. best_list_size = sg_list_size;
  4763. }
  4764. }
  4765. return (best_list_size);
  4766. }
  4767. /*
  4768. * Allocate a controller structure for a new device
  4769. * and perform initial initializion.
  4770. */
  4771. struct ahd_softc *
  4772. ahd_alloc(void *platform_arg, char *name)
  4773. {
  4774. struct ahd_softc *ahd;
  4775. #ifndef __FreeBSD__
  4776. ahd = malloc(sizeof(*ahd), M_DEVBUF, M_NOWAIT);
  4777. if (!ahd) {
  4778. printf("aic7xxx: cannot malloc softc!\n");
  4779. free(name, M_DEVBUF);
  4780. return NULL;
  4781. }
  4782. #else
  4783. ahd = device_get_softc((device_t)platform_arg);
  4784. #endif
  4785. memset(ahd, 0, sizeof(*ahd));
  4786. ahd->seep_config = malloc(sizeof(*ahd->seep_config),
  4787. M_DEVBUF, M_NOWAIT);
  4788. if (ahd->seep_config == NULL) {
  4789. #ifndef __FreeBSD__
  4790. free(ahd, M_DEVBUF);
  4791. #endif
  4792. free(name, M_DEVBUF);
  4793. return (NULL);
  4794. }
  4795. LIST_INIT(&ahd->pending_scbs);
  4796. /* We don't know our unit number until the OSM sets it */
  4797. ahd->name = name;
  4798. ahd->unit = -1;
  4799. ahd->description = NULL;
  4800. ahd->bus_description = NULL;
  4801. ahd->channel = 'A';
  4802. ahd->chip = AHD_NONE;
  4803. ahd->features = AHD_FENONE;
  4804. ahd->bugs = AHD_BUGNONE;
  4805. ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A
  4806. | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A;
  4807. ahd_timer_init(&ahd->reset_timer);
  4808. ahd_timer_init(&ahd->stat_timer);
  4809. ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
  4810. ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
  4811. ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
  4812. ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
  4813. ahd->int_coalescing_stop_threshold =
  4814. AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
  4815. if (ahd_platform_alloc(ahd, platform_arg) != 0) {
  4816. ahd_free(ahd);
  4817. ahd = NULL;
  4818. }
  4819. #ifdef AHD_DEBUG
  4820. if ((ahd_debug & AHD_SHOW_MEMORY) != 0) {
  4821. printf("%s: scb size = 0x%x, hscb size = 0x%x\n",
  4822. ahd_name(ahd), (u_int)sizeof(struct scb),
  4823. (u_int)sizeof(struct hardware_scb));
  4824. }
  4825. #endif
  4826. return (ahd);
  4827. }
  4828. int
  4829. ahd_softc_init(struct ahd_softc *ahd)
  4830. {
  4831. ahd->unpause = 0;
  4832. ahd->pause = PAUSE;
  4833. return (0);
  4834. }
  4835. void
  4836. ahd_set_unit(struct ahd_softc *ahd, int unit)
  4837. {
  4838. ahd->unit = unit;
  4839. }
  4840. void
  4841. ahd_set_name(struct ahd_softc *ahd, char *name)
  4842. {
  4843. if (ahd->name != NULL)
  4844. free(ahd->name, M_DEVBUF);
  4845. ahd->name = name;
  4846. }
  4847. void
  4848. ahd_free(struct ahd_softc *ahd)
  4849. {
  4850. int i;
  4851. switch (ahd->init_level) {
  4852. default:
  4853. case 5:
  4854. ahd_shutdown(ahd);
  4855. /* FALLTHROUGH */
  4856. case 4:
  4857. ahd_dmamap_unload(ahd, ahd->shared_data_dmat,
  4858. ahd->shared_data_map.dmamap);
  4859. /* FALLTHROUGH */
  4860. case 3:
  4861. ahd_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
  4862. ahd->shared_data_map.dmamap);
  4863. ahd_dmamap_destroy(ahd, ahd->shared_data_dmat,
  4864. ahd->shared_data_map.dmamap);
  4865. /* FALLTHROUGH */
  4866. case 2:
  4867. ahd_dma_tag_destroy(ahd, ahd->shared_data_dmat);
  4868. case 1:
  4869. #ifndef __linux__
  4870. ahd_dma_tag_destroy(ahd, ahd->buffer_dmat);
  4871. #endif
  4872. break;
  4873. case 0:
  4874. break;
  4875. }
  4876. #ifndef __linux__
  4877. ahd_dma_tag_destroy(ahd, ahd->parent_dmat);
  4878. #endif
  4879. ahd_platform_free(ahd);
  4880. ahd_fini_scbdata(ahd);
  4881. for (i = 0; i < AHD_NUM_TARGETS; i++) {
  4882. struct ahd_tmode_tstate *tstate;
  4883. tstate = ahd->enabled_targets[i];
  4884. if (tstate != NULL) {
  4885. #ifdef AHD_TARGET_MODE
  4886. int j;
  4887. for (j = 0; j < AHD_NUM_LUNS; j++) {
  4888. struct ahd_tmode_lstate *lstate;
  4889. lstate = tstate->enabled_luns[j];
  4890. if (lstate != NULL) {
  4891. xpt_free_path(lstate->path);
  4892. free(lstate, M_DEVBUF);
  4893. }
  4894. }
  4895. #endif
  4896. free(tstate, M_DEVBUF);
  4897. }
  4898. }
  4899. #ifdef AHD_TARGET_MODE
  4900. if (ahd->black_hole != NULL) {
  4901. xpt_free_path(ahd->black_hole->path);
  4902. free(ahd->black_hole, M_DEVBUF);
  4903. }
  4904. #endif
  4905. if (ahd->name != NULL)
  4906. free(ahd->name, M_DEVBUF);
  4907. if (ahd->seep_config != NULL)
  4908. free(ahd->seep_config, M_DEVBUF);
  4909. if (ahd->saved_stack != NULL)
  4910. free(ahd->saved_stack, M_DEVBUF);
  4911. #ifndef __FreeBSD__
  4912. free(ahd, M_DEVBUF);
  4913. #endif
  4914. return;
  4915. }
  4916. void
  4917. ahd_shutdown(void *arg)
  4918. {
  4919. struct ahd_softc *ahd;
  4920. ahd = (struct ahd_softc *)arg;
  4921. /*
  4922. * Stop periodic timer callbacks.
  4923. */
  4924. ahd_timer_stop(&ahd->reset_timer);
  4925. ahd_timer_stop(&ahd->stat_timer);
  4926. /* This will reset most registers to 0, but not all */
  4927. ahd_reset(ahd, /*reinit*/FALSE);
  4928. }
  4929. /*
  4930. * Reset the controller and record some information about it
  4931. * that is only available just after a reset. If "reinit" is
  4932. * non-zero, this reset occured after initial configuration
  4933. * and the caller requests that the chip be fully reinitialized
  4934. * to a runable state. Chip interrupts are *not* enabled after
  4935. * a reinitialization. The caller must enable interrupts via
  4936. * ahd_intr_enable().
  4937. */
  4938. int
  4939. ahd_reset(struct ahd_softc *ahd, int reinit)
  4940. {
  4941. u_int sxfrctl1;
  4942. int wait;
  4943. uint32_t cmd;
  4944. /*
  4945. * Preserve the value of the SXFRCTL1 register for all channels.
  4946. * It contains settings that affect termination and we don't want
  4947. * to disturb the integrity of the bus.
  4948. */
  4949. ahd_pause(ahd);
  4950. ahd_update_modes(ahd);
  4951. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4952. sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
  4953. cmd = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
  4954. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  4955. uint32_t mod_cmd;
  4956. /*
  4957. * A4 Razor #632
  4958. * During the assertion of CHIPRST, the chip
  4959. * does not disable its parity logic prior to
  4960. * the start of the reset. This may cause a
  4961. * parity error to be detected and thus a
  4962. * spurious SERR or PERR assertion. Disble
  4963. * PERR and SERR responses during the CHIPRST.
  4964. */
  4965. mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN);
  4966. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  4967. mod_cmd, /*bytes*/2);
  4968. }
  4969. ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
  4970. /*
  4971. * Ensure that the reset has finished. We delay 1000us
  4972. * prior to reading the register to make sure the chip
  4973. * has sufficiently completed its reset to handle register
  4974. * accesses.
  4975. */
  4976. wait = 1000;
  4977. do {
  4978. ahd_delay(1000);
  4979. } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
  4980. if (wait == 0) {
  4981. printf("%s: WARNING - Failed chip reset! "
  4982. "Trying to initialize anyway.\n", ahd_name(ahd));
  4983. }
  4984. ahd_outb(ahd, HCNTRL, ahd->pause);
  4985. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  4986. /*
  4987. * Clear any latched PCI error status and restore
  4988. * previous SERR and PERR response enables.
  4989. */
  4990. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  4991. 0xFF, /*bytes*/1);
  4992. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  4993. cmd, /*bytes*/2);
  4994. }
  4995. /*
  4996. * Mode should be SCSI after a chip reset, but lets
  4997. * set it just to be safe. We touch the MODE_PTR
  4998. * register directly so as to bypass the lazy update
  4999. * code in ahd_set_modes().
  5000. */
  5001. ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5002. ahd_outb(ahd, MODE_PTR,
  5003. ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
  5004. /*
  5005. * Restore SXFRCTL1.
  5006. *
  5007. * We must always initialize STPWEN to 1 before we
  5008. * restore the saved values. STPWEN is initialized
  5009. * to a tri-state condition which can only be cleared
  5010. * by turning it on.
  5011. */
  5012. ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
  5013. ahd_outb(ahd, SXFRCTL1, sxfrctl1);
  5014. /* Determine chip configuration */
  5015. ahd->features &= ~AHD_WIDE;
  5016. if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
  5017. ahd->features |= AHD_WIDE;
  5018. /*
  5019. * If a recovery action has forced a chip reset,
  5020. * re-initialize the chip to our liking.
  5021. */
  5022. if (reinit != 0)
  5023. ahd_chip_init(ahd);
  5024. return (0);
  5025. }
  5026. /*
  5027. * Determine the number of SCBs available on the controller
  5028. */
  5029. int
  5030. ahd_probe_scbs(struct ahd_softc *ahd) {
  5031. int i;
  5032. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  5033. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  5034. for (i = 0; i < AHD_SCB_MAX; i++) {
  5035. int j;
  5036. ahd_set_scbptr(ahd, i);
  5037. ahd_outw(ahd, SCB_BASE, i);
  5038. for (j = 2; j < 64; j++)
  5039. ahd_outb(ahd, SCB_BASE+j, 0);
  5040. /* Start out life as unallocated (needing an abort) */
  5041. ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
  5042. if (ahd_inw_scbram(ahd, SCB_BASE) != i)
  5043. break;
  5044. ahd_set_scbptr(ahd, 0);
  5045. if (ahd_inw_scbram(ahd, SCB_BASE) != 0)
  5046. break;
  5047. }
  5048. return (i);
  5049. }
  5050. static void
  5051. ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  5052. {
  5053. dma_addr_t *baddr;
  5054. baddr = (dma_addr_t *)arg;
  5055. *baddr = segs->ds_addr;
  5056. }
  5057. static void
  5058. ahd_initialize_hscbs(struct ahd_softc *ahd)
  5059. {
  5060. int i;
  5061. for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
  5062. ahd_set_scbptr(ahd, i);
  5063. /* Clear the control byte. */
  5064. ahd_outb(ahd, SCB_CONTROL, 0);
  5065. /* Set the next pointer */
  5066. ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
  5067. }
  5068. }
  5069. static int
  5070. ahd_init_scbdata(struct ahd_softc *ahd)
  5071. {
  5072. struct scb_data *scb_data;
  5073. int i;
  5074. scb_data = &ahd->scb_data;
  5075. TAILQ_INIT(&scb_data->free_scbs);
  5076. for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
  5077. LIST_INIT(&scb_data->free_scb_lists[i]);
  5078. LIST_INIT(&scb_data->any_dev_free_scb_list);
  5079. SLIST_INIT(&scb_data->hscb_maps);
  5080. SLIST_INIT(&scb_data->sg_maps);
  5081. SLIST_INIT(&scb_data->sense_maps);
  5082. /* Determine the number of hardware SCBs and initialize them */
  5083. scb_data->maxhscbs = ahd_probe_scbs(ahd);
  5084. if (scb_data->maxhscbs == 0) {
  5085. printf("%s: No SCB space found\n", ahd_name(ahd));
  5086. return (ENXIO);
  5087. }
  5088. ahd_initialize_hscbs(ahd);
  5089. /*
  5090. * Create our DMA tags. These tags define the kinds of device
  5091. * accessible memory allocations and memory mappings we will
  5092. * need to perform during normal operation.
  5093. *
  5094. * Unless we need to further restrict the allocation, we rely
  5095. * on the restrictions of the parent dmat, hence the common
  5096. * use of MAXADDR and MAXSIZE.
  5097. */
  5098. /* DMA tag for our hardware scb structures */
  5099. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5100. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5101. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5102. /*highaddr*/BUS_SPACE_MAXADDR,
  5103. /*filter*/NULL, /*filterarg*/NULL,
  5104. PAGE_SIZE, /*nsegments*/1,
  5105. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5106. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  5107. goto error_exit;
  5108. }
  5109. scb_data->init_level++;
  5110. /* DMA tag for our S/G structures. */
  5111. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8,
  5112. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5113. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5114. /*highaddr*/BUS_SPACE_MAXADDR,
  5115. /*filter*/NULL, /*filterarg*/NULL,
  5116. ahd_sglist_allocsize(ahd), /*nsegments*/1,
  5117. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5118. /*flags*/0, &scb_data->sg_dmat) != 0) {
  5119. goto error_exit;
  5120. }
  5121. #ifdef AHD_DEBUG
  5122. if ((ahd_debug & AHD_SHOW_MEMORY) != 0)
  5123. printf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd),
  5124. ahd_sglist_allocsize(ahd));
  5125. #endif
  5126. scb_data->init_level++;
  5127. /* DMA tag for our sense buffers. We allocate in page sized chunks */
  5128. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5129. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5130. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5131. /*highaddr*/BUS_SPACE_MAXADDR,
  5132. /*filter*/NULL, /*filterarg*/NULL,
  5133. PAGE_SIZE, /*nsegments*/1,
  5134. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5135. /*flags*/0, &scb_data->sense_dmat) != 0) {
  5136. goto error_exit;
  5137. }
  5138. scb_data->init_level++;
  5139. /* Perform initial CCB allocation */
  5140. ahd_alloc_scbs(ahd);
  5141. if (scb_data->numscbs == 0) {
  5142. printf("%s: ahd_init_scbdata - "
  5143. "Unable to allocate initial scbs\n",
  5144. ahd_name(ahd));
  5145. goto error_exit;
  5146. }
  5147. /*
  5148. * Note that we were successfull
  5149. */
  5150. return (0);
  5151. error_exit:
  5152. return (ENOMEM);
  5153. }
  5154. static struct scb *
  5155. ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
  5156. {
  5157. struct scb *scb;
  5158. /*
  5159. * Look on the pending list.
  5160. */
  5161. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  5162. if (SCB_GET_TAG(scb) == tag)
  5163. return (scb);
  5164. }
  5165. /*
  5166. * Then on all of the collision free lists.
  5167. */
  5168. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5169. struct scb *list_scb;
  5170. list_scb = scb;
  5171. do {
  5172. if (SCB_GET_TAG(list_scb) == tag)
  5173. return (list_scb);
  5174. list_scb = LIST_NEXT(list_scb, collision_links);
  5175. } while (list_scb);
  5176. }
  5177. /*
  5178. * And finally on the generic free list.
  5179. */
  5180. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  5181. if (SCB_GET_TAG(scb) == tag)
  5182. return (scb);
  5183. }
  5184. return (NULL);
  5185. }
  5186. static void
  5187. ahd_fini_scbdata(struct ahd_softc *ahd)
  5188. {
  5189. struct scb_data *scb_data;
  5190. scb_data = &ahd->scb_data;
  5191. if (scb_data == NULL)
  5192. return;
  5193. switch (scb_data->init_level) {
  5194. default:
  5195. case 7:
  5196. {
  5197. struct map_node *sns_map;
  5198. while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
  5199. SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
  5200. ahd_dmamap_unload(ahd, scb_data->sense_dmat,
  5201. sns_map->dmamap);
  5202. ahd_dmamem_free(ahd, scb_data->sense_dmat,
  5203. sns_map->vaddr, sns_map->dmamap);
  5204. free(sns_map, M_DEVBUF);
  5205. }
  5206. ahd_dma_tag_destroy(ahd, scb_data->sense_dmat);
  5207. /* FALLTHROUGH */
  5208. }
  5209. case 6:
  5210. {
  5211. struct map_node *sg_map;
  5212. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
  5213. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  5214. ahd_dmamap_unload(ahd, scb_data->sg_dmat,
  5215. sg_map->dmamap);
  5216. ahd_dmamem_free(ahd, scb_data->sg_dmat,
  5217. sg_map->vaddr, sg_map->dmamap);
  5218. free(sg_map, M_DEVBUF);
  5219. }
  5220. ahd_dma_tag_destroy(ahd, scb_data->sg_dmat);
  5221. /* FALLTHROUGH */
  5222. }
  5223. case 5:
  5224. {
  5225. struct map_node *hscb_map;
  5226. while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
  5227. SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
  5228. ahd_dmamap_unload(ahd, scb_data->hscb_dmat,
  5229. hscb_map->dmamap);
  5230. ahd_dmamem_free(ahd, scb_data->hscb_dmat,
  5231. hscb_map->vaddr, hscb_map->dmamap);
  5232. free(hscb_map, M_DEVBUF);
  5233. }
  5234. ahd_dma_tag_destroy(ahd, scb_data->hscb_dmat);
  5235. /* FALLTHROUGH */
  5236. }
  5237. case 4:
  5238. case 3:
  5239. case 2:
  5240. case 1:
  5241. case 0:
  5242. break;
  5243. }
  5244. }
  5245. /*
  5246. * DSP filter Bypass must be enabled until the first selection
  5247. * after a change in bus mode (Razor #491 and #493).
  5248. */
  5249. static void
  5250. ahd_setup_iocell_workaround(struct ahd_softc *ahd)
  5251. {
  5252. ahd_mode_state saved_modes;
  5253. saved_modes = ahd_save_modes(ahd);
  5254. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5255. ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
  5256. | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
  5257. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
  5258. #ifdef AHD_DEBUG
  5259. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5260. printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
  5261. #endif
  5262. ahd_restore_modes(ahd, saved_modes);
  5263. ahd->flags &= ~AHD_HAD_FIRST_SEL;
  5264. }
  5265. static void
  5266. ahd_iocell_first_selection(struct ahd_softc *ahd)
  5267. {
  5268. ahd_mode_state saved_modes;
  5269. u_int sblkctl;
  5270. if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0)
  5271. return;
  5272. saved_modes = ahd_save_modes(ahd);
  5273. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5274. sblkctl = ahd_inb(ahd, SBLKCTL);
  5275. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5276. #ifdef AHD_DEBUG
  5277. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5278. printf("%s: iocell first selection\n", ahd_name(ahd));
  5279. #endif
  5280. if ((sblkctl & ENAB40) != 0) {
  5281. ahd_outb(ahd, DSPDATACTL,
  5282. ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
  5283. #ifdef AHD_DEBUG
  5284. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5285. printf("%s: BYPASS now disabled\n", ahd_name(ahd));
  5286. #endif
  5287. }
  5288. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
  5289. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5290. ahd_restore_modes(ahd, saved_modes);
  5291. ahd->flags |= AHD_HAD_FIRST_SEL;
  5292. }
  5293. /*************************** SCB Management ***********************************/
  5294. static void
  5295. ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
  5296. {
  5297. struct scb_list *free_list;
  5298. struct scb_tailq *free_tailq;
  5299. struct scb *first_scb;
  5300. scb->flags |= SCB_ON_COL_LIST;
  5301. AHD_SET_SCB_COL_IDX(scb, col_idx);
  5302. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5303. free_tailq = &ahd->scb_data.free_scbs;
  5304. first_scb = LIST_FIRST(free_list);
  5305. if (first_scb != NULL) {
  5306. LIST_INSERT_AFTER(first_scb, scb, collision_links);
  5307. } else {
  5308. LIST_INSERT_HEAD(free_list, scb, collision_links);
  5309. TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
  5310. }
  5311. }
  5312. static void
  5313. ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
  5314. {
  5315. struct scb_list *free_list;
  5316. struct scb_tailq *free_tailq;
  5317. struct scb *first_scb;
  5318. u_int col_idx;
  5319. scb->flags &= ~SCB_ON_COL_LIST;
  5320. col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
  5321. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5322. free_tailq = &ahd->scb_data.free_scbs;
  5323. first_scb = LIST_FIRST(free_list);
  5324. if (first_scb == scb) {
  5325. struct scb *next_scb;
  5326. /*
  5327. * Maintain order in the collision free
  5328. * lists for fairness if this device has
  5329. * other colliding tags active.
  5330. */
  5331. next_scb = LIST_NEXT(scb, collision_links);
  5332. if (next_scb != NULL) {
  5333. TAILQ_INSERT_AFTER(free_tailq, scb,
  5334. next_scb, links.tqe);
  5335. }
  5336. TAILQ_REMOVE(free_tailq, scb, links.tqe);
  5337. }
  5338. LIST_REMOVE(scb, collision_links);
  5339. }
  5340. /*
  5341. * Get a free scb. If there are none, see if we can allocate a new SCB.
  5342. */
  5343. struct scb *
  5344. ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
  5345. {
  5346. struct scb *scb;
  5347. int tries;
  5348. tries = 0;
  5349. look_again:
  5350. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5351. if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
  5352. ahd_rem_col_list(ahd, scb);
  5353. goto found;
  5354. }
  5355. }
  5356. if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
  5357. if (tries++ != 0)
  5358. return (NULL);
  5359. ahd_alloc_scbs(ahd);
  5360. goto look_again;
  5361. }
  5362. LIST_REMOVE(scb, links.le);
  5363. if (col_idx != AHD_NEVER_COL_IDX
  5364. && (scb->col_scb != NULL)
  5365. && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
  5366. LIST_REMOVE(scb->col_scb, links.le);
  5367. ahd_add_col_list(ahd, scb->col_scb, col_idx);
  5368. }
  5369. found:
  5370. scb->flags |= SCB_ACTIVE;
  5371. return (scb);
  5372. }
  5373. /*
  5374. * Return an SCB resource to the free list.
  5375. */
  5376. void
  5377. ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
  5378. {
  5379. /* Clean up for the next user */
  5380. scb->flags = SCB_FLAG_NONE;
  5381. scb->hscb->control = 0;
  5382. ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
  5383. if (scb->col_scb == NULL) {
  5384. /*
  5385. * No collision possible. Just free normally.
  5386. */
  5387. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5388. scb, links.le);
  5389. } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
  5390. /*
  5391. * The SCB we might have collided with is on
  5392. * a free collision list. Put both SCBs on
  5393. * the generic list.
  5394. */
  5395. ahd_rem_col_list(ahd, scb->col_scb);
  5396. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5397. scb, links.le);
  5398. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5399. scb->col_scb, links.le);
  5400. } else if ((scb->col_scb->flags
  5401. & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
  5402. && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
  5403. /*
  5404. * The SCB we might collide with on the next allocation
  5405. * is still active in a non-packetized, tagged, context.
  5406. * Put us on the SCB collision list.
  5407. */
  5408. ahd_add_col_list(ahd, scb,
  5409. AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
  5410. } else {
  5411. /*
  5412. * The SCB we might collide with on the next allocation
  5413. * is either active in a packetized context, or free.
  5414. * Since we can't collide, put this SCB on the generic
  5415. * free list.
  5416. */
  5417. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5418. scb, links.le);
  5419. }
  5420. ahd_platform_scb_free(ahd, scb);
  5421. }
  5422. void
  5423. ahd_alloc_scbs(struct ahd_softc *ahd)
  5424. {
  5425. struct scb_data *scb_data;
  5426. struct scb *next_scb;
  5427. struct hardware_scb *hscb;
  5428. struct map_node *hscb_map;
  5429. struct map_node *sg_map;
  5430. struct map_node *sense_map;
  5431. uint8_t *segs;
  5432. uint8_t *sense_data;
  5433. dma_addr_t hscb_busaddr;
  5434. dma_addr_t sg_busaddr;
  5435. dma_addr_t sense_busaddr;
  5436. int newcount;
  5437. int i;
  5438. scb_data = &ahd->scb_data;
  5439. if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
  5440. /* Can't allocate any more */
  5441. return;
  5442. if (scb_data->scbs_left != 0) {
  5443. int offset;
  5444. offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
  5445. hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
  5446. hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
  5447. hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
  5448. } else {
  5449. hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
  5450. if (hscb_map == NULL)
  5451. return;
  5452. /* Allocate the next batch of hardware SCBs */
  5453. if (ahd_dmamem_alloc(ahd, scb_data->hscb_dmat,
  5454. (void **)&hscb_map->vaddr,
  5455. BUS_DMA_NOWAIT, &hscb_map->dmamap) != 0) {
  5456. free(hscb_map, M_DEVBUF);
  5457. return;
  5458. }
  5459. SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
  5460. ahd_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap,
  5461. hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5462. &hscb_map->physaddr, /*flags*/0);
  5463. hscb = (struct hardware_scb *)hscb_map->vaddr;
  5464. hscb_busaddr = hscb_map->physaddr;
  5465. scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
  5466. }
  5467. if (scb_data->sgs_left != 0) {
  5468. int offset;
  5469. offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
  5470. - scb_data->sgs_left) * ahd_sglist_size(ahd);
  5471. sg_map = SLIST_FIRST(&scb_data->sg_maps);
  5472. segs = sg_map->vaddr + offset;
  5473. sg_busaddr = sg_map->physaddr + offset;
  5474. } else {
  5475. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  5476. if (sg_map == NULL)
  5477. return;
  5478. /* Allocate the next batch of S/G lists */
  5479. if (ahd_dmamem_alloc(ahd, scb_data->sg_dmat,
  5480. (void **)&sg_map->vaddr,
  5481. BUS_DMA_NOWAIT, &sg_map->dmamap) != 0) {
  5482. free(sg_map, M_DEVBUF);
  5483. return;
  5484. }
  5485. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  5486. ahd_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap,
  5487. sg_map->vaddr, ahd_sglist_allocsize(ahd),
  5488. ahd_dmamap_cb, &sg_map->physaddr, /*flags*/0);
  5489. segs = sg_map->vaddr;
  5490. sg_busaddr = sg_map->physaddr;
  5491. scb_data->sgs_left =
  5492. ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
  5493. #ifdef AHD_DEBUG
  5494. if (ahd_debug & AHD_SHOW_MEMORY)
  5495. printf("Mapped SG data\n");
  5496. #endif
  5497. }
  5498. if (scb_data->sense_left != 0) {
  5499. int offset;
  5500. offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
  5501. sense_map = SLIST_FIRST(&scb_data->sense_maps);
  5502. sense_data = sense_map->vaddr + offset;
  5503. sense_busaddr = sense_map->physaddr + offset;
  5504. } else {
  5505. sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
  5506. if (sense_map == NULL)
  5507. return;
  5508. /* Allocate the next batch of sense buffers */
  5509. if (ahd_dmamem_alloc(ahd, scb_data->sense_dmat,
  5510. (void **)&sense_map->vaddr,
  5511. BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) {
  5512. free(sense_map, M_DEVBUF);
  5513. return;
  5514. }
  5515. SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
  5516. ahd_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap,
  5517. sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5518. &sense_map->physaddr, /*flags*/0);
  5519. sense_data = sense_map->vaddr;
  5520. sense_busaddr = sense_map->physaddr;
  5521. scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
  5522. #ifdef AHD_DEBUG
  5523. if (ahd_debug & AHD_SHOW_MEMORY)
  5524. printf("Mapped sense data\n");
  5525. #endif
  5526. }
  5527. newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
  5528. newcount = MIN(newcount, scb_data->sgs_left);
  5529. newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
  5530. for (i = 0; i < newcount; i++) {
  5531. struct scb_platform_data *pdata;
  5532. u_int col_tag;
  5533. #ifndef __linux__
  5534. int error;
  5535. #endif
  5536. next_scb = (struct scb *)malloc(sizeof(*next_scb),
  5537. M_DEVBUF, M_NOWAIT);
  5538. if (next_scb == NULL)
  5539. break;
  5540. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  5541. M_DEVBUF, M_NOWAIT);
  5542. if (pdata == NULL) {
  5543. free(next_scb, M_DEVBUF);
  5544. break;
  5545. }
  5546. next_scb->platform_data = pdata;
  5547. next_scb->hscb_map = hscb_map;
  5548. next_scb->sg_map = sg_map;
  5549. next_scb->sense_map = sense_map;
  5550. next_scb->sg_list = segs;
  5551. next_scb->sense_data = sense_data;
  5552. next_scb->sense_busaddr = sense_busaddr;
  5553. memset(hscb, 0, sizeof(*hscb));
  5554. next_scb->hscb = hscb;
  5555. hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
  5556. /*
  5557. * The sequencer always starts with the second entry.
  5558. * The first entry is embedded in the scb.
  5559. */
  5560. next_scb->sg_list_busaddr = sg_busaddr;
  5561. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  5562. next_scb->sg_list_busaddr
  5563. += sizeof(struct ahd_dma64_seg);
  5564. else
  5565. next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
  5566. next_scb->ahd_softc = ahd;
  5567. next_scb->flags = SCB_FLAG_NONE;
  5568. #ifndef __linux__
  5569. error = ahd_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0,
  5570. &next_scb->dmamap);
  5571. if (error != 0) {
  5572. free(next_scb, M_DEVBUF);
  5573. free(pdata, M_DEVBUF);
  5574. break;
  5575. }
  5576. #endif
  5577. next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
  5578. col_tag = scb_data->numscbs ^ 0x100;
  5579. next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
  5580. if (next_scb->col_scb != NULL)
  5581. next_scb->col_scb->col_scb = next_scb;
  5582. ahd_free_scb(ahd, next_scb);
  5583. hscb++;
  5584. hscb_busaddr += sizeof(*hscb);
  5585. segs += ahd_sglist_size(ahd);
  5586. sg_busaddr += ahd_sglist_size(ahd);
  5587. sense_data += AHD_SENSE_BUFSIZE;
  5588. sense_busaddr += AHD_SENSE_BUFSIZE;
  5589. scb_data->numscbs++;
  5590. scb_data->sense_left--;
  5591. scb_data->scbs_left--;
  5592. scb_data->sgs_left--;
  5593. }
  5594. }
  5595. void
  5596. ahd_controller_info(struct ahd_softc *ahd, char *buf)
  5597. {
  5598. const char *speed;
  5599. const char *type;
  5600. int len;
  5601. len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
  5602. buf += len;
  5603. speed = "Ultra320 ";
  5604. if ((ahd->features & AHD_WIDE) != 0) {
  5605. type = "Wide ";
  5606. } else {
  5607. type = "Single ";
  5608. }
  5609. len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
  5610. speed, type, ahd->channel, ahd->our_id);
  5611. buf += len;
  5612. sprintf(buf, "%s, %d SCBs", ahd->bus_description,
  5613. ahd->scb_data.maxhscbs);
  5614. }
  5615. static const char *channel_strings[] = {
  5616. "Primary Low",
  5617. "Primary High",
  5618. "Secondary Low",
  5619. "Secondary High"
  5620. };
  5621. static const char *termstat_strings[] = {
  5622. "Terminated Correctly",
  5623. "Over Terminated",
  5624. "Under Terminated",
  5625. "Not Configured"
  5626. };
  5627. /*
  5628. * Start the board, ready for normal operation
  5629. */
  5630. int
  5631. ahd_init(struct ahd_softc *ahd)
  5632. {
  5633. uint8_t *next_vaddr;
  5634. dma_addr_t next_baddr;
  5635. size_t driver_data_size;
  5636. int i;
  5637. int error;
  5638. u_int warn_user;
  5639. uint8_t current_sensing;
  5640. uint8_t fstat;
  5641. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5642. ahd->stack_size = ahd_probe_stack_size(ahd);
  5643. ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
  5644. M_DEVBUF, M_NOWAIT);
  5645. if (ahd->saved_stack == NULL)
  5646. return (ENOMEM);
  5647. /*
  5648. * Verify that the compiler hasn't over-agressively
  5649. * padded important structures.
  5650. */
  5651. if (sizeof(struct hardware_scb) != 64)
  5652. panic("Hardware SCB size is incorrect");
  5653. #ifdef AHD_DEBUG
  5654. if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
  5655. ahd->flags |= AHD_SEQUENCER_DEBUG;
  5656. #endif
  5657. /*
  5658. * Default to allowing initiator operations.
  5659. */
  5660. ahd->flags |= AHD_INITIATORROLE;
  5661. /*
  5662. * Only allow target mode features if this unit has them enabled.
  5663. */
  5664. if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
  5665. ahd->features &= ~AHD_TARGETMODE;
  5666. #ifndef __linux__
  5667. /* DMA tag for mapping buffers into device visible space. */
  5668. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5669. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5670. /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING
  5671. ? (dma_addr_t)0x7FFFFFFFFFULL
  5672. : BUS_SPACE_MAXADDR_32BIT,
  5673. /*highaddr*/BUS_SPACE_MAXADDR,
  5674. /*filter*/NULL, /*filterarg*/NULL,
  5675. /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE,
  5676. /*nsegments*/AHD_NSEG,
  5677. /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
  5678. /*flags*/BUS_DMA_ALLOCNOW,
  5679. &ahd->buffer_dmat) != 0) {
  5680. return (ENOMEM);
  5681. }
  5682. #endif
  5683. ahd->init_level++;
  5684. /*
  5685. * DMA tag for our command fifos and other data in system memory
  5686. * the card's sequencer must be able to access. For initiator
  5687. * roles, we need to allocate space for the qoutfifo. When providing
  5688. * for the target mode role, we must additionally provide space for
  5689. * the incoming target command fifo.
  5690. */
  5691. driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo)
  5692. + sizeof(struct hardware_scb);
  5693. if ((ahd->features & AHD_TARGETMODE) != 0)
  5694. driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5695. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
  5696. driver_data_size += PKT_OVERRUN_BUFSIZE;
  5697. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5698. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5699. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5700. /*highaddr*/BUS_SPACE_MAXADDR,
  5701. /*filter*/NULL, /*filterarg*/NULL,
  5702. driver_data_size,
  5703. /*nsegments*/1,
  5704. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5705. /*flags*/0, &ahd->shared_data_dmat) != 0) {
  5706. return (ENOMEM);
  5707. }
  5708. ahd->init_level++;
  5709. /* Allocation of driver data */
  5710. if (ahd_dmamem_alloc(ahd, ahd->shared_data_dmat,
  5711. (void **)&ahd->shared_data_map.vaddr,
  5712. BUS_DMA_NOWAIT,
  5713. &ahd->shared_data_map.dmamap) != 0) {
  5714. return (ENOMEM);
  5715. }
  5716. ahd->init_level++;
  5717. /* And permanently map it in */
  5718. ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
  5719. ahd->shared_data_map.vaddr, driver_data_size,
  5720. ahd_dmamap_cb, &ahd->shared_data_map.physaddr,
  5721. /*flags*/0);
  5722. ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr;
  5723. next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
  5724. next_baddr = ahd->shared_data_map.physaddr
  5725. + AHD_QOUT_SIZE*sizeof(struct ahd_completion);
  5726. if ((ahd->features & AHD_TARGETMODE) != 0) {
  5727. ahd->targetcmds = (struct target_cmd *)next_vaddr;
  5728. next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5729. next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5730. }
  5731. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
  5732. ahd->overrun_buf = next_vaddr;
  5733. next_vaddr += PKT_OVERRUN_BUFSIZE;
  5734. next_baddr += PKT_OVERRUN_BUFSIZE;
  5735. }
  5736. /*
  5737. * We need one SCB to serve as the "next SCB". Since the
  5738. * tag identifier in this SCB will never be used, there is
  5739. * no point in using a valid HSCB tag from an SCB pulled from
  5740. * the standard free pool. So, we allocate this "sentinel"
  5741. * specially from the DMA safe memory chunk used for the QOUTFIFO.
  5742. */
  5743. ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
  5744. ahd->next_queued_hscb_map = &ahd->shared_data_map;
  5745. ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
  5746. ahd->init_level++;
  5747. /* Allocate SCB data now that buffer_dmat is initialized */
  5748. if (ahd_init_scbdata(ahd) != 0)
  5749. return (ENOMEM);
  5750. if ((ahd->flags & AHD_INITIATORROLE) == 0)
  5751. ahd->flags &= ~AHD_RESET_BUS_A;
  5752. /*
  5753. * Before committing these settings to the chip, give
  5754. * the OSM one last chance to modify our configuration.
  5755. */
  5756. ahd_platform_init(ahd);
  5757. /* Bring up the chip. */
  5758. ahd_chip_init(ahd);
  5759. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5760. if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
  5761. goto init_done;
  5762. /*
  5763. * Verify termination based on current draw and
  5764. * warn user if the bus is over/under terminated.
  5765. */
  5766. error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
  5767. CURSENSE_ENB);
  5768. if (error != 0) {
  5769. printf("%s: current sensing timeout 1\n", ahd_name(ahd));
  5770. goto init_done;
  5771. }
  5772. for (i = 20, fstat = FLX_FSTAT_BUSY;
  5773. (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
  5774. error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
  5775. if (error != 0) {
  5776. printf("%s: current sensing timeout 2\n",
  5777. ahd_name(ahd));
  5778. goto init_done;
  5779. }
  5780. }
  5781. if (i == 0) {
  5782. printf("%s: Timedout during current-sensing test\n",
  5783. ahd_name(ahd));
  5784. goto init_done;
  5785. }
  5786. /* Latch Current Sensing status. */
  5787. error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, &current_sensing);
  5788. if (error != 0) {
  5789. printf("%s: current sensing timeout 3\n", ahd_name(ahd));
  5790. goto init_done;
  5791. }
  5792. /* Diable current sensing. */
  5793. ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
  5794. #ifdef AHD_DEBUG
  5795. if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
  5796. printf("%s: current_sensing == 0x%x\n",
  5797. ahd_name(ahd), current_sensing);
  5798. }
  5799. #endif
  5800. warn_user = 0;
  5801. for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
  5802. u_int term_stat;
  5803. term_stat = (current_sensing & FLX_CSTAT_MASK);
  5804. switch (term_stat) {
  5805. case FLX_CSTAT_OVER:
  5806. case FLX_CSTAT_UNDER:
  5807. warn_user++;
  5808. case FLX_CSTAT_INVALID:
  5809. case FLX_CSTAT_OKAY:
  5810. if (warn_user == 0 && bootverbose == 0)
  5811. break;
  5812. printf("%s: %s Channel %s\n", ahd_name(ahd),
  5813. channel_strings[i], termstat_strings[term_stat]);
  5814. break;
  5815. }
  5816. }
  5817. if (warn_user) {
  5818. printf("%s: WARNING. Termination is not configured correctly.\n"
  5819. "%s: WARNING. SCSI bus operations may FAIL.\n",
  5820. ahd_name(ahd), ahd_name(ahd));
  5821. }
  5822. init_done:
  5823. ahd_restart(ahd);
  5824. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  5825. ahd_stat_timer, ahd);
  5826. return (0);
  5827. }
  5828. /*
  5829. * (Re)initialize chip state after a chip reset.
  5830. */
  5831. static void
  5832. ahd_chip_init(struct ahd_softc *ahd)
  5833. {
  5834. uint32_t busaddr;
  5835. u_int sxfrctl1;
  5836. u_int scsiseq_template;
  5837. u_int wait;
  5838. u_int i;
  5839. u_int target;
  5840. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5841. /*
  5842. * Take the LED out of diagnostic mode
  5843. */
  5844. ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
  5845. /*
  5846. * Return HS_MAILBOX to its default value.
  5847. */
  5848. ahd->hs_mailbox = 0;
  5849. ahd_outb(ahd, HS_MAILBOX, 0);
  5850. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
  5851. ahd_outb(ahd, IOWNID, ahd->our_id);
  5852. ahd_outb(ahd, TOWNID, ahd->our_id);
  5853. sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
  5854. sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
  5855. if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
  5856. && (ahd->seltime != STIMESEL_MIN)) {
  5857. /*
  5858. * The selection timer duration is twice as long
  5859. * as it should be. Halve it by adding "1" to
  5860. * the user specified setting.
  5861. */
  5862. sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
  5863. } else {
  5864. sxfrctl1 |= ahd->seltime;
  5865. }
  5866. ahd_outb(ahd, SXFRCTL0, DFON);
  5867. ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
  5868. ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  5869. /*
  5870. * Now that termination is set, wait for up
  5871. * to 500ms for our transceivers to settle. If
  5872. * the adapter does not have a cable attached,
  5873. * the transceivers may never settle, so don't
  5874. * complain if we fail here.
  5875. */
  5876. for (wait = 10000;
  5877. (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  5878. wait--)
  5879. ahd_delay(100);
  5880. /* Clear any false bus resets due to the transceivers settling */
  5881. ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
  5882. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5883. /* Initialize mode specific S/G state. */
  5884. for (i = 0; i < 2; i++) {
  5885. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  5886. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  5887. ahd_outb(ahd, SG_STATE, 0);
  5888. ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
  5889. ahd_outb(ahd, SEQIMODE,
  5890. ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
  5891. |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
  5892. }
  5893. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5894. ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
  5895. ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
  5896. ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
  5897. ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
  5898. if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
  5899. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
  5900. } else {
  5901. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
  5902. }
  5903. ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
  5904. if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
  5905. /*
  5906. * Do not issue a target abort when a split completion
  5907. * error occurs. Let our PCIX interrupt handler deal
  5908. * with it instead. H2A4 Razor #625
  5909. */
  5910. ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
  5911. if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
  5912. ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
  5913. /*
  5914. * Tweak IOCELL settings.
  5915. */
  5916. if ((ahd->flags & AHD_HP_BOARD) != 0) {
  5917. for (i = 0; i < NUMDSPS; i++) {
  5918. ahd_outb(ahd, DSPSELECT, i);
  5919. ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
  5920. }
  5921. #ifdef AHD_DEBUG
  5922. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5923. printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
  5924. WRTBIASCTL_HP_DEFAULT);
  5925. #endif
  5926. }
  5927. ahd_setup_iocell_workaround(ahd);
  5928. /*
  5929. * Enable LQI Manager interrupts.
  5930. */
  5931. ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
  5932. | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
  5933. | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
  5934. ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
  5935. /*
  5936. * We choose to have the sequencer catch LQOPHCHGINPKT errors
  5937. * manually for the command phase at the start of a packetized
  5938. * selection case. ENLQOBUSFREE should be made redundant by
  5939. * the BUSFREE interrupt, but it seems that some LQOBUSFREE
  5940. * events fail to assert the BUSFREE interrupt so we must
  5941. * also enable LQOBUSFREE interrupts.
  5942. */
  5943. ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE);
  5944. /*
  5945. * Setup sequencer interrupt handlers.
  5946. */
  5947. ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
  5948. ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
  5949. /*
  5950. * Setup SCB Offset registers.
  5951. */
  5952. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  5953. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
  5954. pkt_long_lun));
  5955. } else {
  5956. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
  5957. }
  5958. ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
  5959. ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
  5960. ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
  5961. ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
  5962. shared_data.idata.cdb));
  5963. ahd_outb(ahd, QNEXTPTR,
  5964. offsetof(struct hardware_scb, next_hscb_busaddr));
  5965. ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
  5966. ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
  5967. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  5968. ahd_outb(ahd, LUNLEN,
  5969. sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
  5970. } else {
  5971. ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
  5972. }
  5973. ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
  5974. ahd_outb(ahd, MAXCMD, 0xFF);
  5975. ahd_outb(ahd, SCBAUTOPTR,
  5976. AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
  5977. /* We haven't been enabled for target mode yet. */
  5978. ahd_outb(ahd, MULTARGID, 0);
  5979. ahd_outb(ahd, MULTARGID + 1, 0);
  5980. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5981. /* Initialize the negotiation table. */
  5982. if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
  5983. /*
  5984. * Clear the spare bytes in the neg table to avoid
  5985. * spurious parity errors.
  5986. */
  5987. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  5988. ahd_outb(ahd, NEGOADDR, target);
  5989. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
  5990. for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
  5991. ahd_outb(ahd, ANNEXDAT, 0);
  5992. }
  5993. }
  5994. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  5995. struct ahd_devinfo devinfo;
  5996. struct ahd_initiator_tinfo *tinfo;
  5997. struct ahd_tmode_tstate *tstate;
  5998. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  5999. target, &tstate);
  6000. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6001. target, CAM_LUN_WILDCARD,
  6002. 'A', ROLE_INITIATOR);
  6003. ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
  6004. }
  6005. ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
  6006. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  6007. #ifdef NEEDS_MORE_TESTING
  6008. /*
  6009. * Always enable abort on incoming L_Qs if this feature is
  6010. * supported. We use this to catch invalid SCB references.
  6011. */
  6012. if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
  6013. ahd_outb(ahd, LQCTL1, ABORTPENDING);
  6014. else
  6015. #endif
  6016. ahd_outb(ahd, LQCTL1, 0);
  6017. /* All of our queues are empty */
  6018. ahd->qoutfifonext = 0;
  6019. ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID;
  6020. ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID);
  6021. for (i = 0; i < AHD_QOUT_SIZE; i++)
  6022. ahd->qoutfifo[i].valid_tag = 0;
  6023. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
  6024. ahd->qinfifonext = 0;
  6025. for (i = 0; i < AHD_QIN_SIZE; i++)
  6026. ahd->qinfifo[i] = SCB_LIST_NULL;
  6027. if ((ahd->features & AHD_TARGETMODE) != 0) {
  6028. /* All target command blocks start out invalid. */
  6029. for (i = 0; i < AHD_TMODE_CMDS; i++)
  6030. ahd->targetcmds[i].cmd_valid = 0;
  6031. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
  6032. ahd->tqinfifonext = 1;
  6033. ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
  6034. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  6035. }
  6036. /* Initialize Scratch Ram. */
  6037. ahd_outb(ahd, SEQ_FLAGS, 0);
  6038. ahd_outb(ahd, SEQ_FLAGS2, 0);
  6039. /* We don't have any waiting selections */
  6040. ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
  6041. ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
  6042. ahd_outw(ahd, MK_MESSAGE_SCB, SCB_LIST_NULL);
  6043. ahd_outw(ahd, MK_MESSAGE_SCSIID, 0xFF);
  6044. for (i = 0; i < AHD_NUM_TARGETS; i++)
  6045. ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
  6046. /*
  6047. * Nobody is waiting to be DMAed into the QOUTFIFO.
  6048. */
  6049. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  6050. ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
  6051. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  6052. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
  6053. ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
  6054. /*
  6055. * The Freeze Count is 0.
  6056. */
  6057. ahd->qfreeze_cnt = 0;
  6058. ahd_outw(ahd, QFREEZE_COUNT, 0);
  6059. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0);
  6060. /*
  6061. * Tell the sequencer where it can find our arrays in memory.
  6062. */
  6063. busaddr = ahd->shared_data_map.physaddr;
  6064. ahd_outl(ahd, SHARED_DATA_ADDR, busaddr);
  6065. ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr);
  6066. /*
  6067. * Setup the allowed SCSI Sequences based on operational mode.
  6068. * If we are a target, we'll enable select in operations once
  6069. * we've had a lun enabled.
  6070. */
  6071. scsiseq_template = ENAUTOATNP;
  6072. if ((ahd->flags & AHD_INITIATORROLE) != 0)
  6073. scsiseq_template |= ENRSELI;
  6074. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
  6075. /* There are no busy SCBs yet. */
  6076. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  6077. int lun;
  6078. for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
  6079. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
  6080. }
  6081. /*
  6082. * Initialize the group code to command length table.
  6083. * Vendor Unique codes are set to 0 so we only capture
  6084. * the first byte of the cdb. These can be overridden
  6085. * when target mode is enabled.
  6086. */
  6087. ahd_outb(ahd, CMDSIZE_TABLE, 5);
  6088. ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
  6089. ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
  6090. ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
  6091. ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
  6092. ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
  6093. ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
  6094. ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
  6095. /* Tell the sequencer of our initial queue positions */
  6096. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6097. ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
  6098. ahd->qinfifonext = 0;
  6099. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6100. ahd_set_hescb_qoff(ahd, 0);
  6101. ahd_set_snscb_qoff(ahd, 0);
  6102. ahd_set_sescb_qoff(ahd, 0);
  6103. ahd_set_sdscb_qoff(ahd, 0);
  6104. /*
  6105. * Tell the sequencer which SCB will be the next one it receives.
  6106. */
  6107. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6108. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6109. /*
  6110. * Default to coalescing disabled.
  6111. */
  6112. ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
  6113. ahd_outw(ahd, CMDS_PENDING, 0);
  6114. ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
  6115. ahd->int_coalescing_maxcmds,
  6116. ahd->int_coalescing_mincmds);
  6117. ahd_enable_coalescing(ahd, FALSE);
  6118. ahd_loadseq(ahd);
  6119. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6120. if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
  6121. u_int negodat3 = ahd_inb(ahd, NEGCONOPTS);
  6122. negodat3 |= ENSLOWCRC;
  6123. ahd_outb(ahd, NEGCONOPTS, negodat3);
  6124. negodat3 = ahd_inb(ahd, NEGCONOPTS);
  6125. if (!(negodat3 & ENSLOWCRC))
  6126. printf("aic79xx: failed to set the SLOWCRC bit\n");
  6127. else
  6128. printf("aic79xx: SLOWCRC bit set\n");
  6129. }
  6130. }
  6131. /*
  6132. * Setup default device and controller settings.
  6133. * This should only be called if our probe has
  6134. * determined that no configuration data is available.
  6135. */
  6136. int
  6137. ahd_default_config(struct ahd_softc *ahd)
  6138. {
  6139. int targ;
  6140. ahd->our_id = 7;
  6141. /*
  6142. * Allocate a tstate to house information for our
  6143. * initiator presence on the bus as well as the user
  6144. * data for any target mode initiator.
  6145. */
  6146. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6147. printf("%s: unable to allocate ahd_tmode_tstate. "
  6148. "Failing attach\n", ahd_name(ahd));
  6149. return (ENOMEM);
  6150. }
  6151. for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
  6152. struct ahd_devinfo devinfo;
  6153. struct ahd_initiator_tinfo *tinfo;
  6154. struct ahd_tmode_tstate *tstate;
  6155. uint16_t target_mask;
  6156. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6157. targ, &tstate);
  6158. /*
  6159. * We support SPC2 and SPI4.
  6160. */
  6161. tinfo->user.protocol_version = 4;
  6162. tinfo->user.transport_version = 4;
  6163. target_mask = 0x01 << targ;
  6164. ahd->user_discenable |= target_mask;
  6165. tstate->discenable |= target_mask;
  6166. ahd->user_tagenable |= target_mask;
  6167. #ifdef AHD_FORCE_160
  6168. tinfo->user.period = AHD_SYNCRATE_DT;
  6169. #else
  6170. tinfo->user.period = AHD_SYNCRATE_160;
  6171. #endif
  6172. tinfo->user.offset = MAX_OFFSET;
  6173. tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM
  6174. | MSG_EXT_PPR_WR_FLOW
  6175. | MSG_EXT_PPR_HOLD_MCS
  6176. | MSG_EXT_PPR_IU_REQ
  6177. | MSG_EXT_PPR_QAS_REQ
  6178. | MSG_EXT_PPR_DT_REQ;
  6179. if ((ahd->features & AHD_RTI) != 0)
  6180. tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
  6181. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  6182. /*
  6183. * Start out Async/Narrow/Untagged and with
  6184. * conservative protocol support.
  6185. */
  6186. tinfo->goal.protocol_version = 2;
  6187. tinfo->goal.transport_version = 2;
  6188. tinfo->curr.protocol_version = 2;
  6189. tinfo->curr.transport_version = 2;
  6190. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6191. targ, CAM_LUN_WILDCARD,
  6192. 'A', ROLE_INITIATOR);
  6193. tstate->tagenable &= ~target_mask;
  6194. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6195. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6196. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6197. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6198. /*paused*/TRUE);
  6199. }
  6200. return (0);
  6201. }
  6202. /*
  6203. * Parse device configuration information.
  6204. */
  6205. int
  6206. ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
  6207. {
  6208. int targ;
  6209. int max_targ;
  6210. max_targ = sc->max_targets & CFMAXTARG;
  6211. ahd->our_id = sc->brtime_id & CFSCSIID;
  6212. /*
  6213. * Allocate a tstate to house information for our
  6214. * initiator presence on the bus as well as the user
  6215. * data for any target mode initiator.
  6216. */
  6217. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6218. printf("%s: unable to allocate ahd_tmode_tstate. "
  6219. "Failing attach\n", ahd_name(ahd));
  6220. return (ENOMEM);
  6221. }
  6222. for (targ = 0; targ < max_targ; targ++) {
  6223. struct ahd_devinfo devinfo;
  6224. struct ahd_initiator_tinfo *tinfo;
  6225. struct ahd_transinfo *user_tinfo;
  6226. struct ahd_tmode_tstate *tstate;
  6227. uint16_t target_mask;
  6228. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6229. targ, &tstate);
  6230. user_tinfo = &tinfo->user;
  6231. /*
  6232. * We support SPC2 and SPI4.
  6233. */
  6234. tinfo->user.protocol_version = 4;
  6235. tinfo->user.transport_version = 4;
  6236. target_mask = 0x01 << targ;
  6237. ahd->user_discenable &= ~target_mask;
  6238. tstate->discenable &= ~target_mask;
  6239. ahd->user_tagenable &= ~target_mask;
  6240. if (sc->device_flags[targ] & CFDISC) {
  6241. tstate->discenable |= target_mask;
  6242. ahd->user_discenable |= target_mask;
  6243. ahd->user_tagenable |= target_mask;
  6244. } else {
  6245. /*
  6246. * Cannot be packetized without disconnection.
  6247. */
  6248. sc->device_flags[targ] &= ~CFPACKETIZED;
  6249. }
  6250. user_tinfo->ppr_options = 0;
  6251. user_tinfo->period = (sc->device_flags[targ] & CFXFER);
  6252. if (user_tinfo->period < CFXFER_ASYNC) {
  6253. if (user_tinfo->period <= AHD_PERIOD_10MHz)
  6254. user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
  6255. user_tinfo->offset = MAX_OFFSET;
  6256. } else {
  6257. user_tinfo->offset = 0;
  6258. user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
  6259. }
  6260. #ifdef AHD_FORCE_160
  6261. if (user_tinfo->period <= AHD_SYNCRATE_160)
  6262. user_tinfo->period = AHD_SYNCRATE_DT;
  6263. #endif
  6264. if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
  6265. user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM
  6266. | MSG_EXT_PPR_WR_FLOW
  6267. | MSG_EXT_PPR_HOLD_MCS
  6268. | MSG_EXT_PPR_IU_REQ;
  6269. if ((ahd->features & AHD_RTI) != 0)
  6270. user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
  6271. }
  6272. if ((sc->device_flags[targ] & CFQAS) != 0)
  6273. user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
  6274. if ((sc->device_flags[targ] & CFWIDEB) != 0)
  6275. user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
  6276. else
  6277. user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
  6278. #ifdef AHD_DEBUG
  6279. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  6280. printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
  6281. user_tinfo->period, user_tinfo->offset,
  6282. user_tinfo->ppr_options);
  6283. #endif
  6284. /*
  6285. * Start out Async/Narrow/Untagged and with
  6286. * conservative protocol support.
  6287. */
  6288. tstate->tagenable &= ~target_mask;
  6289. tinfo->goal.protocol_version = 2;
  6290. tinfo->goal.transport_version = 2;
  6291. tinfo->curr.protocol_version = 2;
  6292. tinfo->curr.transport_version = 2;
  6293. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6294. targ, CAM_LUN_WILDCARD,
  6295. 'A', ROLE_INITIATOR);
  6296. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6297. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6298. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6299. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6300. /*paused*/TRUE);
  6301. }
  6302. ahd->flags &= ~AHD_SPCHK_ENB_A;
  6303. if (sc->bios_control & CFSPARITY)
  6304. ahd->flags |= AHD_SPCHK_ENB_A;
  6305. ahd->flags &= ~AHD_RESET_BUS_A;
  6306. if (sc->bios_control & CFRESETB)
  6307. ahd->flags |= AHD_RESET_BUS_A;
  6308. ahd->flags &= ~AHD_EXTENDED_TRANS_A;
  6309. if (sc->bios_control & CFEXTEND)
  6310. ahd->flags |= AHD_EXTENDED_TRANS_A;
  6311. ahd->flags &= ~AHD_BIOS_ENABLED;
  6312. if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
  6313. ahd->flags |= AHD_BIOS_ENABLED;
  6314. ahd->flags &= ~AHD_STPWLEVEL_A;
  6315. if ((sc->adapter_control & CFSTPWLEVEL) != 0)
  6316. ahd->flags |= AHD_STPWLEVEL_A;
  6317. return (0);
  6318. }
  6319. /*
  6320. * Parse device configuration information.
  6321. */
  6322. int
  6323. ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
  6324. {
  6325. int error;
  6326. error = ahd_verify_vpd_cksum(vpd);
  6327. if (error == 0)
  6328. return (EINVAL);
  6329. if ((vpd->bios_flags & VPDBOOTHOST) != 0)
  6330. ahd->flags |= AHD_BOOT_CHANNEL;
  6331. return (0);
  6332. }
  6333. void
  6334. ahd_intr_enable(struct ahd_softc *ahd, int enable)
  6335. {
  6336. u_int hcntrl;
  6337. hcntrl = ahd_inb(ahd, HCNTRL);
  6338. hcntrl &= ~INTEN;
  6339. ahd->pause &= ~INTEN;
  6340. ahd->unpause &= ~INTEN;
  6341. if (enable) {
  6342. hcntrl |= INTEN;
  6343. ahd->pause |= INTEN;
  6344. ahd->unpause |= INTEN;
  6345. }
  6346. ahd_outb(ahd, HCNTRL, hcntrl);
  6347. }
  6348. void
  6349. ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
  6350. u_int mincmds)
  6351. {
  6352. if (timer > AHD_TIMER_MAX_US)
  6353. timer = AHD_TIMER_MAX_US;
  6354. ahd->int_coalescing_timer = timer;
  6355. if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
  6356. maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
  6357. if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
  6358. mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
  6359. ahd->int_coalescing_maxcmds = maxcmds;
  6360. ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
  6361. ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
  6362. ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
  6363. }
  6364. void
  6365. ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
  6366. {
  6367. ahd->hs_mailbox &= ~ENINT_COALESCE;
  6368. if (enable)
  6369. ahd->hs_mailbox |= ENINT_COALESCE;
  6370. ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
  6371. ahd_flush_device_writes(ahd);
  6372. ahd_run_qoutfifo(ahd);
  6373. }
  6374. /*
  6375. * Ensure that the card is paused in a location
  6376. * outside of all critical sections and that all
  6377. * pending work is completed prior to returning.
  6378. * This routine should only be called from outside
  6379. * an interrupt context.
  6380. */
  6381. void
  6382. ahd_pause_and_flushwork(struct ahd_softc *ahd)
  6383. {
  6384. u_int intstat;
  6385. u_int maxloops;
  6386. maxloops = 1000;
  6387. ahd->flags |= AHD_ALL_INTERRUPTS;
  6388. ahd_pause(ahd);
  6389. /*
  6390. * Freeze the outgoing selections. We do this only
  6391. * until we are safely paused without further selections
  6392. * pending.
  6393. */
  6394. ahd->qfreeze_cnt--;
  6395. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  6396. ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
  6397. do {
  6398. ahd_unpause(ahd);
  6399. /*
  6400. * Give the sequencer some time to service
  6401. * any active selections.
  6402. */
  6403. ahd_delay(500);
  6404. ahd_intr(ahd);
  6405. ahd_pause(ahd);
  6406. intstat = ahd_inb(ahd, INTSTAT);
  6407. if ((intstat & INT_PEND) == 0) {
  6408. ahd_clear_critical_section(ahd);
  6409. intstat = ahd_inb(ahd, INTSTAT);
  6410. }
  6411. } while (--maxloops
  6412. && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
  6413. && ((intstat & INT_PEND) != 0
  6414. || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  6415. || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
  6416. if (maxloops == 0) {
  6417. printf("Infinite interrupt loop, INTSTAT = %x",
  6418. ahd_inb(ahd, INTSTAT));
  6419. }
  6420. ahd->qfreeze_cnt++;
  6421. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  6422. ahd_flush_qoutfifo(ahd);
  6423. ahd->flags &= ~AHD_ALL_INTERRUPTS;
  6424. }
  6425. int
  6426. ahd_suspend(struct ahd_softc *ahd)
  6427. {
  6428. ahd_pause_and_flushwork(ahd);
  6429. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  6430. ahd_unpause(ahd);
  6431. return (EBUSY);
  6432. }
  6433. ahd_shutdown(ahd);
  6434. return (0);
  6435. }
  6436. int
  6437. ahd_resume(struct ahd_softc *ahd)
  6438. {
  6439. ahd_reset(ahd, /*reinit*/TRUE);
  6440. ahd_intr_enable(ahd, TRUE);
  6441. ahd_restart(ahd);
  6442. return (0);
  6443. }
  6444. /************************** Busy Target Table *********************************/
  6445. /*
  6446. * Set SCBPTR to the SCB that contains the busy
  6447. * table entry for TCL. Return the offset into
  6448. * the SCB that contains the entry for TCL.
  6449. * saved_scbid is dereferenced and set to the
  6450. * scbid that should be restored once manipualtion
  6451. * of the TCL entry is complete.
  6452. */
  6453. static __inline u_int
  6454. ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
  6455. {
  6456. /*
  6457. * Index to the SCB that contains the busy entry.
  6458. */
  6459. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6460. *saved_scbid = ahd_get_scbptr(ahd);
  6461. ahd_set_scbptr(ahd, TCL_LUN(tcl)
  6462. | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
  6463. /*
  6464. * And now calculate the SCB offset to the entry.
  6465. * Each entry is 2 bytes wide, hence the
  6466. * multiplication by 2.
  6467. */
  6468. return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
  6469. }
  6470. /*
  6471. * Return the untagged transaction id for a given target/channel lun.
  6472. */
  6473. u_int
  6474. ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
  6475. {
  6476. u_int scbid;
  6477. u_int scb_offset;
  6478. u_int saved_scbptr;
  6479. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6480. scbid = ahd_inw_scbram(ahd, scb_offset);
  6481. ahd_set_scbptr(ahd, saved_scbptr);
  6482. return (scbid);
  6483. }
  6484. void
  6485. ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
  6486. {
  6487. u_int scb_offset;
  6488. u_int saved_scbptr;
  6489. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6490. ahd_outw(ahd, scb_offset, scbid);
  6491. ahd_set_scbptr(ahd, saved_scbptr);
  6492. }
  6493. /************************** SCB and SCB queue management **********************/
  6494. int
  6495. ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
  6496. char channel, int lun, u_int tag, role_t role)
  6497. {
  6498. int targ = SCB_GET_TARGET(ahd, scb);
  6499. char chan = SCB_GET_CHANNEL(ahd, scb);
  6500. int slun = SCB_GET_LUN(scb);
  6501. int match;
  6502. match = ((chan == channel) || (channel == ALL_CHANNELS));
  6503. if (match != 0)
  6504. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  6505. if (match != 0)
  6506. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  6507. if (match != 0) {
  6508. #ifdef AHD_TARGET_MODE
  6509. int group;
  6510. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  6511. if (role == ROLE_INITIATOR) {
  6512. match = (group != XPT_FC_GROUP_TMODE)
  6513. && ((tag == SCB_GET_TAG(scb))
  6514. || (tag == SCB_LIST_NULL));
  6515. } else if (role == ROLE_TARGET) {
  6516. match = (group == XPT_FC_GROUP_TMODE)
  6517. && ((tag == scb->io_ctx->csio.tag_id)
  6518. || (tag == SCB_LIST_NULL));
  6519. }
  6520. #else /* !AHD_TARGET_MODE */
  6521. match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
  6522. #endif /* AHD_TARGET_MODE */
  6523. }
  6524. return match;
  6525. }
  6526. void
  6527. ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
  6528. {
  6529. int target;
  6530. char channel;
  6531. int lun;
  6532. target = SCB_GET_TARGET(ahd, scb);
  6533. lun = SCB_GET_LUN(scb);
  6534. channel = SCB_GET_CHANNEL(ahd, scb);
  6535. ahd_search_qinfifo(ahd, target, channel, lun,
  6536. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  6537. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  6538. ahd_platform_freeze_devq(ahd, scb);
  6539. }
  6540. void
  6541. ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
  6542. {
  6543. struct scb *prev_scb;
  6544. ahd_mode_state saved_modes;
  6545. saved_modes = ahd_save_modes(ahd);
  6546. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6547. prev_scb = NULL;
  6548. if (ahd_qinfifo_count(ahd) != 0) {
  6549. u_int prev_tag;
  6550. u_int prev_pos;
  6551. prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
  6552. prev_tag = ahd->qinfifo[prev_pos];
  6553. prev_scb = ahd_lookup_scb(ahd, prev_tag);
  6554. }
  6555. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6556. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6557. ahd_restore_modes(ahd, saved_modes);
  6558. }
  6559. static void
  6560. ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
  6561. struct scb *scb)
  6562. {
  6563. if (prev_scb == NULL) {
  6564. uint32_t busaddr;
  6565. busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
  6566. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6567. } else {
  6568. prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
  6569. ahd_sync_scb(ahd, prev_scb,
  6570. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6571. }
  6572. ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
  6573. ahd->qinfifonext++;
  6574. scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
  6575. ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6576. }
  6577. static int
  6578. ahd_qinfifo_count(struct ahd_softc *ahd)
  6579. {
  6580. u_int qinpos;
  6581. u_int wrap_qinpos;
  6582. u_int wrap_qinfifonext;
  6583. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6584. qinpos = ahd_get_snscb_qoff(ahd);
  6585. wrap_qinpos = AHD_QIN_WRAP(qinpos);
  6586. wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
  6587. if (wrap_qinfifonext >= wrap_qinpos)
  6588. return (wrap_qinfifonext - wrap_qinpos);
  6589. else
  6590. return (wrap_qinfifonext
  6591. + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
  6592. }
  6593. void
  6594. ahd_reset_cmds_pending(struct ahd_softc *ahd)
  6595. {
  6596. struct scb *scb;
  6597. ahd_mode_state saved_modes;
  6598. u_int pending_cmds;
  6599. saved_modes = ahd_save_modes(ahd);
  6600. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6601. /*
  6602. * Don't count any commands as outstanding that the
  6603. * sequencer has already marked for completion.
  6604. */
  6605. ahd_flush_qoutfifo(ahd);
  6606. pending_cmds = 0;
  6607. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  6608. pending_cmds++;
  6609. }
  6610. ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
  6611. ahd_restore_modes(ahd, saved_modes);
  6612. ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
  6613. }
  6614. void
  6615. ahd_done_with_status(struct ahd_softc *ahd, struct scb *scb, uint32_t status)
  6616. {
  6617. cam_status ostat;
  6618. cam_status cstat;
  6619. ostat = ahd_get_transaction_status(scb);
  6620. if (ostat == CAM_REQ_INPROG)
  6621. ahd_set_transaction_status(scb, status);
  6622. cstat = ahd_get_transaction_status(scb);
  6623. if (cstat != CAM_REQ_CMP)
  6624. ahd_freeze_scb(scb);
  6625. ahd_done(ahd, scb);
  6626. }
  6627. int
  6628. ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
  6629. int lun, u_int tag, role_t role, uint32_t status,
  6630. ahd_search_action action)
  6631. {
  6632. struct scb *scb;
  6633. struct scb *mk_msg_scb;
  6634. struct scb *prev_scb;
  6635. ahd_mode_state saved_modes;
  6636. u_int qinstart;
  6637. u_int qinpos;
  6638. u_int qintail;
  6639. u_int tid_next;
  6640. u_int tid_prev;
  6641. u_int scbid;
  6642. u_int seq_flags2;
  6643. u_int savedscbptr;
  6644. uint32_t busaddr;
  6645. int found;
  6646. int targets;
  6647. /* Must be in CCHAN mode */
  6648. saved_modes = ahd_save_modes(ahd);
  6649. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6650. /*
  6651. * Halt any pending SCB DMA. The sequencer will reinitiate
  6652. * this dma if the qinfifo is not empty once we unpause.
  6653. */
  6654. if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
  6655. == (CCARREN|CCSCBEN|CCSCBDIR)) {
  6656. ahd_outb(ahd, CCSCBCTL,
  6657. ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
  6658. while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
  6659. ;
  6660. }
  6661. /* Determine sequencer's position in the qinfifo. */
  6662. qintail = AHD_QIN_WRAP(ahd->qinfifonext);
  6663. qinstart = ahd_get_snscb_qoff(ahd);
  6664. qinpos = AHD_QIN_WRAP(qinstart);
  6665. found = 0;
  6666. prev_scb = NULL;
  6667. if (action == SEARCH_PRINT) {
  6668. printf("qinstart = %d qinfifonext = %d\nQINFIFO:",
  6669. qinstart, ahd->qinfifonext);
  6670. }
  6671. /*
  6672. * Start with an empty queue. Entries that are not chosen
  6673. * for removal will be re-added to the queue as we go.
  6674. */
  6675. ahd->qinfifonext = qinstart;
  6676. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6677. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6678. while (qinpos != qintail) {
  6679. scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
  6680. if (scb == NULL) {
  6681. printf("qinpos = %d, SCB index = %d\n",
  6682. qinpos, ahd->qinfifo[qinpos]);
  6683. panic("Loop 1\n");
  6684. }
  6685. if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
  6686. /*
  6687. * We found an scb that needs to be acted on.
  6688. */
  6689. found++;
  6690. switch (action) {
  6691. case SEARCH_COMPLETE:
  6692. if ((scb->flags & SCB_ACTIVE) == 0)
  6693. printf("Inactive SCB in qinfifo\n");
  6694. ahd_done_with_status(ahd, scb, status);
  6695. /* FALLTHROUGH */
  6696. case SEARCH_REMOVE:
  6697. break;
  6698. case SEARCH_PRINT:
  6699. printf(" 0x%x", ahd->qinfifo[qinpos]);
  6700. /* FALLTHROUGH */
  6701. case SEARCH_COUNT:
  6702. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6703. prev_scb = scb;
  6704. break;
  6705. }
  6706. } else {
  6707. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6708. prev_scb = scb;
  6709. }
  6710. qinpos = AHD_QIN_WRAP(qinpos+1);
  6711. }
  6712. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6713. if (action == SEARCH_PRINT)
  6714. printf("\nWAITING_TID_QUEUES:\n");
  6715. /*
  6716. * Search waiting for selection lists. We traverse the
  6717. * list of "their ids" waiting for selection and, if
  6718. * appropriate, traverse the SCBs of each "their id"
  6719. * looking for matches.
  6720. */
  6721. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6722. seq_flags2 = ahd_inb(ahd, SEQ_FLAGS2);
  6723. if ((seq_flags2 & PENDING_MK_MESSAGE) != 0) {
  6724. scbid = ahd_inw(ahd, MK_MESSAGE_SCB);
  6725. mk_msg_scb = ahd_lookup_scb(ahd, scbid);
  6726. } else
  6727. mk_msg_scb = NULL;
  6728. savedscbptr = ahd_get_scbptr(ahd);
  6729. tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
  6730. tid_prev = SCB_LIST_NULL;
  6731. targets = 0;
  6732. for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
  6733. u_int tid_head;
  6734. u_int tid_tail;
  6735. targets++;
  6736. if (targets > AHD_NUM_TARGETS)
  6737. panic("TID LIST LOOP");
  6738. if (scbid >= ahd->scb_data.numscbs) {
  6739. printf("%s: Waiting TID List inconsistency. "
  6740. "SCB index == 0x%x, yet numscbs == 0x%x.",
  6741. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6742. ahd_dump_card_state(ahd);
  6743. panic("for safety");
  6744. }
  6745. scb = ahd_lookup_scb(ahd, scbid);
  6746. if (scb == NULL) {
  6747. printf("%s: SCB = 0x%x Not Active!\n",
  6748. ahd_name(ahd), scbid);
  6749. panic("Waiting TID List traversal\n");
  6750. }
  6751. ahd_set_scbptr(ahd, scbid);
  6752. tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
  6753. if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
  6754. SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
  6755. tid_prev = scbid;
  6756. continue;
  6757. }
  6758. /*
  6759. * We found a list of scbs that needs to be searched.
  6760. */
  6761. if (action == SEARCH_PRINT)
  6762. printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
  6763. tid_head = scbid;
  6764. found += ahd_search_scb_list(ahd, target, channel,
  6765. lun, tag, role, status,
  6766. action, &tid_head, &tid_tail,
  6767. SCB_GET_TARGET(ahd, scb));
  6768. /*
  6769. * Check any MK_MESSAGE SCB that is still waiting to
  6770. * enter this target's waiting for selection queue.
  6771. */
  6772. if (mk_msg_scb != NULL
  6773. && ahd_match_scb(ahd, mk_msg_scb, target, channel,
  6774. lun, tag, role)) {
  6775. /*
  6776. * We found an scb that needs to be acted on.
  6777. */
  6778. found++;
  6779. switch (action) {
  6780. case SEARCH_COMPLETE:
  6781. if ((mk_msg_scb->flags & SCB_ACTIVE) == 0)
  6782. printf("Inactive SCB pending MK_MSG\n");
  6783. ahd_done_with_status(ahd, mk_msg_scb, status);
  6784. /* FALLTHROUGH */
  6785. case SEARCH_REMOVE:
  6786. {
  6787. u_int tail_offset;
  6788. printf("Removing MK_MSG scb\n");
  6789. /*
  6790. * Reset our tail to the tail of the
  6791. * main per-target list.
  6792. */
  6793. tail_offset = WAITING_SCB_TAILS
  6794. + (2 * SCB_GET_TARGET(ahd, mk_msg_scb));
  6795. ahd_outw(ahd, tail_offset, tid_tail);
  6796. seq_flags2 &= ~PENDING_MK_MESSAGE;
  6797. ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
  6798. ahd_outw(ahd, CMDS_PENDING,
  6799. ahd_inw(ahd, CMDS_PENDING)-1);
  6800. mk_msg_scb = NULL;
  6801. break;
  6802. }
  6803. case SEARCH_PRINT:
  6804. printf(" 0x%x", SCB_GET_TAG(scb));
  6805. /* FALLTHROUGH */
  6806. case SEARCH_COUNT:
  6807. break;
  6808. }
  6809. }
  6810. if (mk_msg_scb != NULL
  6811. && SCBID_IS_NULL(tid_head)
  6812. && ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
  6813. SCB_LIST_NULL, ROLE_UNKNOWN)) {
  6814. /*
  6815. * When removing the last SCB for a target
  6816. * queue with a pending MK_MESSAGE scb, we
  6817. * must queue the MK_MESSAGE scb.
  6818. */
  6819. printf("Queueing mk_msg_scb\n");
  6820. tid_head = ahd_inw(ahd, MK_MESSAGE_SCB);
  6821. seq_flags2 &= ~PENDING_MK_MESSAGE;
  6822. ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
  6823. mk_msg_scb = NULL;
  6824. }
  6825. if (tid_head != scbid)
  6826. ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
  6827. if (!SCBID_IS_NULL(tid_head))
  6828. tid_prev = tid_head;
  6829. if (action == SEARCH_PRINT)
  6830. printf(")\n");
  6831. }
  6832. /* Restore saved state. */
  6833. ahd_set_scbptr(ahd, savedscbptr);
  6834. ahd_restore_modes(ahd, saved_modes);
  6835. return (found);
  6836. }
  6837. static int
  6838. ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
  6839. int lun, u_int tag, role_t role, uint32_t status,
  6840. ahd_search_action action, u_int *list_head,
  6841. u_int *list_tail, u_int tid)
  6842. {
  6843. struct scb *scb;
  6844. u_int scbid;
  6845. u_int next;
  6846. u_int prev;
  6847. int found;
  6848. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6849. found = 0;
  6850. prev = SCB_LIST_NULL;
  6851. next = *list_head;
  6852. *list_tail = SCB_LIST_NULL;
  6853. for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
  6854. if (scbid >= ahd->scb_data.numscbs) {
  6855. printf("%s:SCB List inconsistency. "
  6856. "SCB == 0x%x, yet numscbs == 0x%x.",
  6857. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6858. ahd_dump_card_state(ahd);
  6859. panic("for safety");
  6860. }
  6861. scb = ahd_lookup_scb(ahd, scbid);
  6862. if (scb == NULL) {
  6863. printf("%s: SCB = %d Not Active!\n",
  6864. ahd_name(ahd), scbid);
  6865. panic("Waiting List traversal\n");
  6866. }
  6867. ahd_set_scbptr(ahd, scbid);
  6868. *list_tail = scbid;
  6869. next = ahd_inw_scbram(ahd, SCB_NEXT);
  6870. if (ahd_match_scb(ahd, scb, target, channel,
  6871. lun, SCB_LIST_NULL, role) == 0) {
  6872. prev = scbid;
  6873. continue;
  6874. }
  6875. found++;
  6876. switch (action) {
  6877. case SEARCH_COMPLETE:
  6878. if ((scb->flags & SCB_ACTIVE) == 0)
  6879. printf("Inactive SCB in Waiting List\n");
  6880. ahd_done_with_status(ahd, scb, status);
  6881. /* FALLTHROUGH */
  6882. case SEARCH_REMOVE:
  6883. ahd_rem_wscb(ahd, scbid, prev, next, tid);
  6884. *list_tail = prev;
  6885. if (SCBID_IS_NULL(prev))
  6886. *list_head = next;
  6887. break;
  6888. case SEARCH_PRINT:
  6889. printf("0x%x ", scbid);
  6890. case SEARCH_COUNT:
  6891. prev = scbid;
  6892. break;
  6893. }
  6894. if (found > AHD_SCB_MAX)
  6895. panic("SCB LIST LOOP");
  6896. }
  6897. if (action == SEARCH_COMPLETE
  6898. || action == SEARCH_REMOVE)
  6899. ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
  6900. return (found);
  6901. }
  6902. static void
  6903. ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
  6904. u_int tid_cur, u_int tid_next)
  6905. {
  6906. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6907. if (SCBID_IS_NULL(tid_cur)) {
  6908. /* Bypass current TID list */
  6909. if (SCBID_IS_NULL(tid_prev)) {
  6910. ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
  6911. } else {
  6912. ahd_set_scbptr(ahd, tid_prev);
  6913. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6914. }
  6915. if (SCBID_IS_NULL(tid_next))
  6916. ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
  6917. } else {
  6918. /* Stitch through tid_cur */
  6919. if (SCBID_IS_NULL(tid_prev)) {
  6920. ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
  6921. } else {
  6922. ahd_set_scbptr(ahd, tid_prev);
  6923. ahd_outw(ahd, SCB_NEXT2, tid_cur);
  6924. }
  6925. ahd_set_scbptr(ahd, tid_cur);
  6926. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6927. if (SCBID_IS_NULL(tid_next))
  6928. ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
  6929. }
  6930. }
  6931. /*
  6932. * Manipulate the waiting for selection list and return the
  6933. * scb that follows the one that we remove.
  6934. */
  6935. static u_int
  6936. ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  6937. u_int prev, u_int next, u_int tid)
  6938. {
  6939. u_int tail_offset;
  6940. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6941. if (!SCBID_IS_NULL(prev)) {
  6942. ahd_set_scbptr(ahd, prev);
  6943. ahd_outw(ahd, SCB_NEXT, next);
  6944. }
  6945. /*
  6946. * SCBs that have MK_MESSAGE set in them may
  6947. * cause the tail pointer to be updated without
  6948. * setting the next pointer of the previous tail.
  6949. * Only clear the tail if the removed SCB was
  6950. * the tail.
  6951. */
  6952. tail_offset = WAITING_SCB_TAILS + (2 * tid);
  6953. if (SCBID_IS_NULL(next)
  6954. && ahd_inw(ahd, tail_offset) == scbid)
  6955. ahd_outw(ahd, tail_offset, prev);
  6956. ahd_add_scb_to_free_list(ahd, scbid);
  6957. return (next);
  6958. }
  6959. /*
  6960. * Add the SCB as selected by SCBPTR onto the on chip list of
  6961. * free hardware SCBs. This list is empty/unused if we are not
  6962. * performing SCB paging.
  6963. */
  6964. static void
  6965. ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
  6966. {
  6967. /* XXX Need some other mechanism to designate "free". */
  6968. /*
  6969. * Invalidate the tag so that our abort
  6970. * routines don't think it's active.
  6971. ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
  6972. */
  6973. }
  6974. /******************************** Error Handling ******************************/
  6975. /*
  6976. * Abort all SCBs that match the given description (target/channel/lun/tag),
  6977. * setting their status to the passed in status if the status has not already
  6978. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  6979. * is paused before it is called.
  6980. */
  6981. int
  6982. ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
  6983. int lun, u_int tag, role_t role, uint32_t status)
  6984. {
  6985. struct scb *scbp;
  6986. struct scb *scbp_next;
  6987. u_int i, j;
  6988. u_int maxtarget;
  6989. u_int minlun;
  6990. u_int maxlun;
  6991. int found;
  6992. ahd_mode_state saved_modes;
  6993. /* restore this when we're done */
  6994. saved_modes = ahd_save_modes(ahd);
  6995. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6996. found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
  6997. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  6998. /*
  6999. * Clean out the busy target table for any untagged commands.
  7000. */
  7001. i = 0;
  7002. maxtarget = 16;
  7003. if (target != CAM_TARGET_WILDCARD) {
  7004. i = target;
  7005. if (channel == 'B')
  7006. i += 8;
  7007. maxtarget = i + 1;
  7008. }
  7009. if (lun == CAM_LUN_WILDCARD) {
  7010. minlun = 0;
  7011. maxlun = AHD_NUM_LUNS_NONPKT;
  7012. } else if (lun >= AHD_NUM_LUNS_NONPKT) {
  7013. minlun = maxlun = 0;
  7014. } else {
  7015. minlun = lun;
  7016. maxlun = lun + 1;
  7017. }
  7018. if (role != ROLE_TARGET) {
  7019. for (;i < maxtarget; i++) {
  7020. for (j = minlun;j < maxlun; j++) {
  7021. u_int scbid;
  7022. u_int tcl;
  7023. tcl = BUILD_TCL_RAW(i, 'A', j);
  7024. scbid = ahd_find_busy_tcl(ahd, tcl);
  7025. scbp = ahd_lookup_scb(ahd, scbid);
  7026. if (scbp == NULL
  7027. || ahd_match_scb(ahd, scbp, target, channel,
  7028. lun, tag, role) == 0)
  7029. continue;
  7030. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
  7031. }
  7032. }
  7033. }
  7034. /*
  7035. * Don't abort commands that have already completed,
  7036. * but haven't quite made it up to the host yet.
  7037. */
  7038. ahd_flush_qoutfifo(ahd);
  7039. /*
  7040. * Go through the pending CCB list and look for
  7041. * commands for this target that are still active.
  7042. * These are other tagged commands that were
  7043. * disconnected when the reset occurred.
  7044. */
  7045. scbp_next = LIST_FIRST(&ahd->pending_scbs);
  7046. while (scbp_next != NULL) {
  7047. scbp = scbp_next;
  7048. scbp_next = LIST_NEXT(scbp, pending_links);
  7049. if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
  7050. cam_status ostat;
  7051. ostat = ahd_get_transaction_status(scbp);
  7052. if (ostat == CAM_REQ_INPROG)
  7053. ahd_set_transaction_status(scbp, status);
  7054. if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
  7055. ahd_freeze_scb(scbp);
  7056. if ((scbp->flags & SCB_ACTIVE) == 0)
  7057. printf("Inactive SCB on pending list\n");
  7058. ahd_done(ahd, scbp);
  7059. found++;
  7060. }
  7061. }
  7062. ahd_restore_modes(ahd, saved_modes);
  7063. ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
  7064. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  7065. return found;
  7066. }
  7067. static void
  7068. ahd_reset_current_bus(struct ahd_softc *ahd)
  7069. {
  7070. uint8_t scsiseq;
  7071. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  7072. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
  7073. scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
  7074. ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
  7075. ahd_flush_device_writes(ahd);
  7076. ahd_delay(AHD_BUSRESET_DELAY);
  7077. /* Turn off the bus reset */
  7078. ahd_outb(ahd, SCSISEQ0, scsiseq);
  7079. ahd_flush_device_writes(ahd);
  7080. ahd_delay(AHD_BUSRESET_DELAY);
  7081. if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
  7082. /*
  7083. * 2A Razor #474
  7084. * Certain chip state is not cleared for
  7085. * SCSI bus resets that we initiate, so
  7086. * we must reset the chip.
  7087. */
  7088. ahd_reset(ahd, /*reinit*/TRUE);
  7089. ahd_intr_enable(ahd, /*enable*/TRUE);
  7090. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  7091. }
  7092. ahd_clear_intstat(ahd);
  7093. }
  7094. int
  7095. ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
  7096. {
  7097. struct ahd_devinfo devinfo;
  7098. u_int initiator;
  7099. u_int target;
  7100. u_int max_scsiid;
  7101. int found;
  7102. u_int fifo;
  7103. u_int next_fifo;
  7104. uint8_t scsiseq;
  7105. /*
  7106. * Check if the last bus reset is cleared
  7107. */
  7108. if (ahd->flags & AHD_BUS_RESET_ACTIVE) {
  7109. printf("%s: bus reset still active\n",
  7110. ahd_name(ahd));
  7111. return 0;
  7112. }
  7113. ahd->flags |= AHD_BUS_RESET_ACTIVE;
  7114. ahd->pending_device = NULL;
  7115. ahd_compile_devinfo(&devinfo,
  7116. CAM_TARGET_WILDCARD,
  7117. CAM_TARGET_WILDCARD,
  7118. CAM_LUN_WILDCARD,
  7119. channel, ROLE_UNKNOWN);
  7120. ahd_pause(ahd);
  7121. /* Make sure the sequencer is in a safe location. */
  7122. ahd_clear_critical_section(ahd);
  7123. /*
  7124. * Run our command complete fifos to ensure that we perform
  7125. * completion processing on any commands that 'completed'
  7126. * before the reset occurred.
  7127. */
  7128. ahd_run_qoutfifo(ahd);
  7129. #ifdef AHD_TARGET_MODE
  7130. if ((ahd->flags & AHD_TARGETROLE) != 0) {
  7131. ahd_run_tqinfifo(ahd, /*paused*/TRUE);
  7132. }
  7133. #endif
  7134. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7135. /*
  7136. * Disable selections so no automatic hardware
  7137. * functions will modify chip state.
  7138. */
  7139. ahd_outb(ahd, SCSISEQ0, 0);
  7140. ahd_outb(ahd, SCSISEQ1, 0);
  7141. /*
  7142. * Safely shut down our DMA engines. Always start with
  7143. * the FIFO that is not currently active (if any are
  7144. * actively connected).
  7145. */
  7146. next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  7147. if (next_fifo > CURRFIFO_1)
  7148. /* If disconneced, arbitrarily start with FIFO1. */
  7149. next_fifo = fifo = 0;
  7150. do {
  7151. next_fifo ^= CURRFIFO_1;
  7152. ahd_set_modes(ahd, next_fifo, next_fifo);
  7153. ahd_outb(ahd, DFCNTRL,
  7154. ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
  7155. while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
  7156. ahd_delay(10);
  7157. /*
  7158. * Set CURRFIFO to the now inactive channel.
  7159. */
  7160. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7161. ahd_outb(ahd, DFFSTAT, next_fifo);
  7162. } while (next_fifo != fifo);
  7163. /*
  7164. * Reset the bus if we are initiating this reset
  7165. */
  7166. ahd_clear_msg_state(ahd);
  7167. ahd_outb(ahd, SIMODE1,
  7168. ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST));
  7169. if (initiate_reset)
  7170. ahd_reset_current_bus(ahd);
  7171. ahd_clear_intstat(ahd);
  7172. /*
  7173. * Clean up all the state information for the
  7174. * pending transactions on this bus.
  7175. */
  7176. found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
  7177. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  7178. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  7179. /*
  7180. * Cleanup anything left in the FIFOs.
  7181. */
  7182. ahd_clear_fifo(ahd, 0);
  7183. ahd_clear_fifo(ahd, 1);
  7184. /*
  7185. * Reenable selections
  7186. */
  7187. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
  7188. scsiseq = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  7189. ahd_outb(ahd, SCSISEQ1, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  7190. max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
  7191. #ifdef AHD_TARGET_MODE
  7192. /*
  7193. * Send an immediate notify ccb to all target more peripheral
  7194. * drivers affected by this action.
  7195. */
  7196. for (target = 0; target <= max_scsiid; target++) {
  7197. struct ahd_tmode_tstate* tstate;
  7198. u_int lun;
  7199. tstate = ahd->enabled_targets[target];
  7200. if (tstate == NULL)
  7201. continue;
  7202. for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
  7203. struct ahd_tmode_lstate* lstate;
  7204. lstate = tstate->enabled_luns[lun];
  7205. if (lstate == NULL)
  7206. continue;
  7207. ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
  7208. EVENT_TYPE_BUS_RESET, /*arg*/0);
  7209. ahd_send_lstate_events(ahd, lstate);
  7210. }
  7211. }
  7212. #endif
  7213. /* Notify the XPT that a bus reset occurred */
  7214. ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
  7215. CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
  7216. /*
  7217. * Revert to async/narrow transfers until we renegotiate.
  7218. */
  7219. for (target = 0; target <= max_scsiid; target++) {
  7220. if (ahd->enabled_targets[target] == NULL)
  7221. continue;
  7222. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  7223. struct ahd_devinfo devinfo;
  7224. ahd_compile_devinfo(&devinfo, target, initiator,
  7225. CAM_LUN_WILDCARD,
  7226. 'A', ROLE_UNKNOWN);
  7227. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  7228. AHD_TRANS_CUR, /*paused*/TRUE);
  7229. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  7230. /*offset*/0, /*ppr_options*/0,
  7231. AHD_TRANS_CUR, /*paused*/TRUE);
  7232. }
  7233. }
  7234. ahd_restart(ahd);
  7235. return (found);
  7236. }
  7237. /**************************** Statistics Processing ***************************/
  7238. static void
  7239. ahd_stat_timer(void *arg)
  7240. {
  7241. struct ahd_softc *ahd = arg;
  7242. u_long s;
  7243. int enint_coal;
  7244. ahd_lock(ahd, &s);
  7245. enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
  7246. if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
  7247. enint_coal |= ENINT_COALESCE;
  7248. else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
  7249. enint_coal &= ~ENINT_COALESCE;
  7250. if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
  7251. ahd_enable_coalescing(ahd, enint_coal);
  7252. #ifdef AHD_DEBUG
  7253. if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
  7254. printf("%s: Interrupt coalescing "
  7255. "now %sabled. Cmds %d\n",
  7256. ahd_name(ahd),
  7257. (enint_coal & ENINT_COALESCE) ? "en" : "dis",
  7258. ahd->cmdcmplt_total);
  7259. #endif
  7260. }
  7261. ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
  7262. ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
  7263. ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
  7264. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  7265. ahd_stat_timer, ahd);
  7266. ahd_unlock(ahd, &s);
  7267. }
  7268. /****************************** Status Processing *****************************/
  7269. void
  7270. ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
  7271. {
  7272. if (scb->hscb->shared_data.istatus.scsi_status != 0) {
  7273. ahd_handle_scsi_status(ahd, scb);
  7274. } else {
  7275. ahd_calc_residual(ahd, scb);
  7276. ahd_done(ahd, scb);
  7277. }
  7278. }
  7279. void
  7280. ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
  7281. {
  7282. struct hardware_scb *hscb;
  7283. int paused;
  7284. /*
  7285. * The sequencer freezes its select-out queue
  7286. * anytime a SCSI status error occurs. We must
  7287. * handle the error and increment our qfreeze count
  7288. * to allow the sequencer to continue. We don't
  7289. * bother clearing critical sections here since all
  7290. * operations are on data structures that the sequencer
  7291. * is not touching once the queue is frozen.
  7292. */
  7293. hscb = scb->hscb;
  7294. if (ahd_is_paused(ahd)) {
  7295. paused = 1;
  7296. } else {
  7297. paused = 0;
  7298. ahd_pause(ahd);
  7299. }
  7300. /* Freeze the queue until the client sees the error. */
  7301. ahd_freeze_devq(ahd, scb);
  7302. ahd_freeze_scb(scb);
  7303. ahd->qfreeze_cnt++;
  7304. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  7305. if (paused == 0)
  7306. ahd_unpause(ahd);
  7307. /* Don't want to clobber the original sense code */
  7308. if ((scb->flags & SCB_SENSE) != 0) {
  7309. /*
  7310. * Clear the SCB_SENSE Flag and perform
  7311. * a normal command completion.
  7312. */
  7313. scb->flags &= ~SCB_SENSE;
  7314. ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  7315. ahd_done(ahd, scb);
  7316. return;
  7317. }
  7318. ahd_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  7319. ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
  7320. switch (hscb->shared_data.istatus.scsi_status) {
  7321. case STATUS_PKT_SENSE:
  7322. {
  7323. struct scsi_status_iu_header *siu;
  7324. ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
  7325. siu = (struct scsi_status_iu_header *)scb->sense_data;
  7326. ahd_set_scsi_status(scb, siu->status);
  7327. #ifdef AHD_DEBUG
  7328. if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
  7329. ahd_print_path(ahd, scb);
  7330. printf("SCB 0x%x Received PKT Status of 0x%x\n",
  7331. SCB_GET_TAG(scb), siu->status);
  7332. printf("\tflags = 0x%x, sense len = 0x%x, "
  7333. "pktfail = 0x%x\n",
  7334. siu->flags, scsi_4btoul(siu->sense_length),
  7335. scsi_4btoul(siu->pkt_failures_length));
  7336. }
  7337. #endif
  7338. if ((siu->flags & SIU_RSPVALID) != 0) {
  7339. ahd_print_path(ahd, scb);
  7340. if (scsi_4btoul(siu->pkt_failures_length) < 4) {
  7341. printf("Unable to parse pkt_failures\n");
  7342. } else {
  7343. switch (SIU_PKTFAIL_CODE(siu)) {
  7344. case SIU_PFC_NONE:
  7345. printf("No packet failure found\n");
  7346. break;
  7347. case SIU_PFC_CIU_FIELDS_INVALID:
  7348. printf("Invalid Command IU Field\n");
  7349. break;
  7350. case SIU_PFC_TMF_NOT_SUPPORTED:
  7351. printf("TMF not supportd\n");
  7352. break;
  7353. case SIU_PFC_TMF_FAILED:
  7354. printf("TMF failed\n");
  7355. break;
  7356. case SIU_PFC_INVALID_TYPE_CODE:
  7357. printf("Invalid L_Q Type code\n");
  7358. break;
  7359. case SIU_PFC_ILLEGAL_REQUEST:
  7360. printf("Illegal request\n");
  7361. default:
  7362. break;
  7363. }
  7364. }
  7365. if (siu->status == SCSI_STATUS_OK)
  7366. ahd_set_transaction_status(scb,
  7367. CAM_REQ_CMP_ERR);
  7368. }
  7369. if ((siu->flags & SIU_SNSVALID) != 0) {
  7370. scb->flags |= SCB_PKT_SENSE;
  7371. #ifdef AHD_DEBUG
  7372. if ((ahd_debug & AHD_SHOW_SENSE) != 0)
  7373. printf("Sense data available\n");
  7374. #endif
  7375. }
  7376. ahd_done(ahd, scb);
  7377. break;
  7378. }
  7379. case SCSI_STATUS_CMD_TERMINATED:
  7380. case SCSI_STATUS_CHECK_COND:
  7381. {
  7382. struct ahd_devinfo devinfo;
  7383. struct ahd_dma_seg *sg;
  7384. struct scsi_sense *sc;
  7385. struct ahd_initiator_tinfo *targ_info;
  7386. struct ahd_tmode_tstate *tstate;
  7387. struct ahd_transinfo *tinfo;
  7388. #ifdef AHD_DEBUG
  7389. if (ahd_debug & AHD_SHOW_SENSE) {
  7390. ahd_print_path(ahd, scb);
  7391. printf("SCB %d: requests Check Status\n",
  7392. SCB_GET_TAG(scb));
  7393. }
  7394. #endif
  7395. if (ahd_perform_autosense(scb) == 0)
  7396. break;
  7397. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  7398. SCB_GET_TARGET(ahd, scb),
  7399. SCB_GET_LUN(scb),
  7400. SCB_GET_CHANNEL(ahd, scb),
  7401. ROLE_INITIATOR);
  7402. targ_info = ahd_fetch_transinfo(ahd,
  7403. devinfo.channel,
  7404. devinfo.our_scsiid,
  7405. devinfo.target,
  7406. &tstate);
  7407. tinfo = &targ_info->curr;
  7408. sg = scb->sg_list;
  7409. sc = (struct scsi_sense *)hscb->shared_data.idata.cdb;
  7410. /*
  7411. * Save off the residual if there is one.
  7412. */
  7413. ahd_update_residual(ahd, scb);
  7414. #ifdef AHD_DEBUG
  7415. if (ahd_debug & AHD_SHOW_SENSE) {
  7416. ahd_print_path(ahd, scb);
  7417. printf("Sending Sense\n");
  7418. }
  7419. #endif
  7420. scb->sg_count = 0;
  7421. sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
  7422. ahd_get_sense_bufsize(ahd, scb),
  7423. /*last*/TRUE);
  7424. sc->opcode = REQUEST_SENSE;
  7425. sc->byte2 = 0;
  7426. if (tinfo->protocol_version <= SCSI_REV_2
  7427. && SCB_GET_LUN(scb) < 8)
  7428. sc->byte2 = SCB_GET_LUN(scb) << 5;
  7429. sc->unused[0] = 0;
  7430. sc->unused[1] = 0;
  7431. sc->length = ahd_get_sense_bufsize(ahd, scb);
  7432. sc->control = 0;
  7433. /*
  7434. * We can't allow the target to disconnect.
  7435. * This will be an untagged transaction and
  7436. * having the target disconnect will make this
  7437. * transaction indestinguishable from outstanding
  7438. * tagged transactions.
  7439. */
  7440. hscb->control = 0;
  7441. /*
  7442. * This request sense could be because the
  7443. * the device lost power or in some other
  7444. * way has lost our transfer negotiations.
  7445. * Renegotiate if appropriate. Unit attention
  7446. * errors will be reported before any data
  7447. * phases occur.
  7448. */
  7449. if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
  7450. ahd_update_neg_request(ahd, &devinfo,
  7451. tstate, targ_info,
  7452. AHD_NEG_IF_NON_ASYNC);
  7453. }
  7454. if (tstate->auto_negotiate & devinfo.target_mask) {
  7455. hscb->control |= MK_MESSAGE;
  7456. scb->flags &=
  7457. ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
  7458. scb->flags |= SCB_AUTO_NEGOTIATE;
  7459. }
  7460. hscb->cdb_len = sizeof(*sc);
  7461. ahd_setup_data_scb(ahd, scb);
  7462. scb->flags |= SCB_SENSE;
  7463. ahd_queue_scb(ahd, scb);
  7464. break;
  7465. }
  7466. case SCSI_STATUS_OK:
  7467. printf("%s: Interrupted for staus of 0???\n",
  7468. ahd_name(ahd));
  7469. /* FALLTHROUGH */
  7470. default:
  7471. ahd_done(ahd, scb);
  7472. break;
  7473. }
  7474. }
  7475. /*
  7476. * Calculate the residual for a just completed SCB.
  7477. */
  7478. void
  7479. ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
  7480. {
  7481. struct hardware_scb *hscb;
  7482. struct initiator_status *spkt;
  7483. uint32_t sgptr;
  7484. uint32_t resid_sgptr;
  7485. uint32_t resid;
  7486. /*
  7487. * 5 cases.
  7488. * 1) No residual.
  7489. * SG_STATUS_VALID clear in sgptr.
  7490. * 2) Transferless command
  7491. * 3) Never performed any transfers.
  7492. * sgptr has SG_FULL_RESID set.
  7493. * 4) No residual but target did not
  7494. * save data pointers after the
  7495. * last transfer, so sgptr was
  7496. * never updated.
  7497. * 5) We have a partial residual.
  7498. * Use residual_sgptr to determine
  7499. * where we are.
  7500. */
  7501. hscb = scb->hscb;
  7502. sgptr = ahd_le32toh(hscb->sgptr);
  7503. if ((sgptr & SG_STATUS_VALID) == 0)
  7504. /* Case 1 */
  7505. return;
  7506. sgptr &= ~SG_STATUS_VALID;
  7507. if ((sgptr & SG_LIST_NULL) != 0)
  7508. /* Case 2 */
  7509. return;
  7510. /*
  7511. * Residual fields are the same in both
  7512. * target and initiator status packets,
  7513. * so we can always use the initiator fields
  7514. * regardless of the role for this SCB.
  7515. */
  7516. spkt = &hscb->shared_data.istatus;
  7517. resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
  7518. if ((sgptr & SG_FULL_RESID) != 0) {
  7519. /* Case 3 */
  7520. resid = ahd_get_transfer_length(scb);
  7521. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  7522. /* Case 4 */
  7523. return;
  7524. } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
  7525. ahd_print_path(ahd, scb);
  7526. printf("data overrun detected Tag == 0x%x.\n",
  7527. SCB_GET_TAG(scb));
  7528. ahd_freeze_devq(ahd, scb);
  7529. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  7530. ahd_freeze_scb(scb);
  7531. return;
  7532. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  7533. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  7534. /* NOTREACHED */
  7535. } else {
  7536. struct ahd_dma_seg *sg;
  7537. /*
  7538. * Remainder of the SG where the transfer
  7539. * stopped.
  7540. */
  7541. resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
  7542. sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
  7543. /* The residual sg_ptr always points to the next sg */
  7544. sg--;
  7545. /*
  7546. * Add up the contents of all residual
  7547. * SG segments that are after the SG where
  7548. * the transfer stopped.
  7549. */
  7550. while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
  7551. sg++;
  7552. resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  7553. }
  7554. }
  7555. if ((scb->flags & SCB_SENSE) == 0)
  7556. ahd_set_residual(scb, resid);
  7557. else
  7558. ahd_set_sense_residual(scb, resid);
  7559. #ifdef AHD_DEBUG
  7560. if ((ahd_debug & AHD_SHOW_MISC) != 0) {
  7561. ahd_print_path(ahd, scb);
  7562. printf("Handled %sResidual of %d bytes\n",
  7563. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  7564. }
  7565. #endif
  7566. }
  7567. /******************************* Target Mode **********************************/
  7568. #ifdef AHD_TARGET_MODE
  7569. /*
  7570. * Add a target mode event to this lun's queue
  7571. */
  7572. static void
  7573. ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
  7574. u_int initiator_id, u_int event_type, u_int event_arg)
  7575. {
  7576. struct ahd_tmode_event *event;
  7577. int pending;
  7578. xpt_freeze_devq(lstate->path, /*count*/1);
  7579. if (lstate->event_w_idx >= lstate->event_r_idx)
  7580. pending = lstate->event_w_idx - lstate->event_r_idx;
  7581. else
  7582. pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
  7583. - (lstate->event_r_idx - lstate->event_w_idx);
  7584. if (event_type == EVENT_TYPE_BUS_RESET
  7585. || event_type == MSG_BUS_DEV_RESET) {
  7586. /*
  7587. * Any earlier events are irrelevant, so reset our buffer.
  7588. * This has the effect of allowing us to deal with reset
  7589. * floods (an external device holding down the reset line)
  7590. * without losing the event that is really interesting.
  7591. */
  7592. lstate->event_r_idx = 0;
  7593. lstate->event_w_idx = 0;
  7594. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  7595. }
  7596. if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
  7597. xpt_print_path(lstate->path);
  7598. printf("immediate event %x:%x lost\n",
  7599. lstate->event_buffer[lstate->event_r_idx].event_type,
  7600. lstate->event_buffer[lstate->event_r_idx].event_arg);
  7601. lstate->event_r_idx++;
  7602. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7603. lstate->event_r_idx = 0;
  7604. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  7605. }
  7606. event = &lstate->event_buffer[lstate->event_w_idx];
  7607. event->initiator_id = initiator_id;
  7608. event->event_type = event_type;
  7609. event->event_arg = event_arg;
  7610. lstate->event_w_idx++;
  7611. if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7612. lstate->event_w_idx = 0;
  7613. }
  7614. /*
  7615. * Send any target mode events queued up waiting
  7616. * for immediate notify resources.
  7617. */
  7618. void
  7619. ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
  7620. {
  7621. struct ccb_hdr *ccbh;
  7622. struct ccb_immed_notify *inot;
  7623. while (lstate->event_r_idx != lstate->event_w_idx
  7624. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  7625. struct ahd_tmode_event *event;
  7626. event = &lstate->event_buffer[lstate->event_r_idx];
  7627. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  7628. inot = (struct ccb_immed_notify *)ccbh;
  7629. switch (event->event_type) {
  7630. case EVENT_TYPE_BUS_RESET:
  7631. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  7632. break;
  7633. default:
  7634. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  7635. inot->message_args[0] = event->event_type;
  7636. inot->message_args[1] = event->event_arg;
  7637. break;
  7638. }
  7639. inot->initiator_id = event->initiator_id;
  7640. inot->sense_len = 0;
  7641. xpt_done((union ccb *)inot);
  7642. lstate->event_r_idx++;
  7643. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7644. lstate->event_r_idx = 0;
  7645. }
  7646. }
  7647. #endif
  7648. /******************** Sequencer Program Patching/Download *********************/
  7649. #ifdef AHD_DUMP_SEQ
  7650. void
  7651. ahd_dumpseq(struct ahd_softc* ahd)
  7652. {
  7653. int i;
  7654. int max_prog;
  7655. max_prog = 2048;
  7656. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7657. ahd_outw(ahd, PRGMCNT, 0);
  7658. for (i = 0; i < max_prog; i++) {
  7659. uint8_t ins_bytes[4];
  7660. ahd_insb(ahd, SEQRAM, ins_bytes, 4);
  7661. printf("0x%08x\n", ins_bytes[0] << 24
  7662. | ins_bytes[1] << 16
  7663. | ins_bytes[2] << 8
  7664. | ins_bytes[3]);
  7665. }
  7666. }
  7667. #endif
  7668. static void
  7669. ahd_loadseq(struct ahd_softc *ahd)
  7670. {
  7671. struct cs cs_table[num_critical_sections];
  7672. u_int begin_set[num_critical_sections];
  7673. u_int end_set[num_critical_sections];
  7674. struct patch *cur_patch;
  7675. u_int cs_count;
  7676. u_int cur_cs;
  7677. u_int i;
  7678. int downloaded;
  7679. u_int skip_addr;
  7680. u_int sg_prefetch_cnt;
  7681. u_int sg_prefetch_cnt_limit;
  7682. u_int sg_prefetch_align;
  7683. u_int sg_size;
  7684. u_int cacheline_mask;
  7685. uint8_t download_consts[DOWNLOAD_CONST_COUNT];
  7686. if (bootverbose)
  7687. printf("%s: Downloading Sequencer Program...",
  7688. ahd_name(ahd));
  7689. #if DOWNLOAD_CONST_COUNT != 8
  7690. #error "Download Const Mismatch"
  7691. #endif
  7692. /*
  7693. * Start out with 0 critical sections
  7694. * that apply to this firmware load.
  7695. */
  7696. cs_count = 0;
  7697. cur_cs = 0;
  7698. memset(begin_set, 0, sizeof(begin_set));
  7699. memset(end_set, 0, sizeof(end_set));
  7700. /*
  7701. * Setup downloadable constant table.
  7702. *
  7703. * The computation for the S/G prefetch variables is
  7704. * a bit complicated. We would like to always fetch
  7705. * in terms of cachelined sized increments. However,
  7706. * if the cacheline is not an even multiple of the
  7707. * SG element size or is larger than our SG RAM, using
  7708. * just the cache size might leave us with only a portion
  7709. * of an SG element at the tail of a prefetch. If the
  7710. * cacheline is larger than our S/G prefetch buffer less
  7711. * the size of an SG element, we may round down to a cacheline
  7712. * that doesn't contain any or all of the S/G of interest
  7713. * within the bounds of our S/G ram. Provide variables to
  7714. * the sequencer that will allow it to handle these edge
  7715. * cases.
  7716. */
  7717. /* Start by aligning to the nearest cacheline. */
  7718. sg_prefetch_align = ahd->pci_cachesize;
  7719. if (sg_prefetch_align == 0)
  7720. sg_prefetch_align = 8;
  7721. /* Round down to the nearest power of 2. */
  7722. while (powerof2(sg_prefetch_align) == 0)
  7723. sg_prefetch_align--;
  7724. cacheline_mask = sg_prefetch_align - 1;
  7725. /*
  7726. * If the cacheline boundary is greater than half our prefetch RAM
  7727. * we risk not being able to fetch even a single complete S/G
  7728. * segment if we align to that boundary.
  7729. */
  7730. if (sg_prefetch_align > CCSGADDR_MAX/2)
  7731. sg_prefetch_align = CCSGADDR_MAX/2;
  7732. /* Start by fetching a single cacheline. */
  7733. sg_prefetch_cnt = sg_prefetch_align;
  7734. /*
  7735. * Increment the prefetch count by cachelines until
  7736. * at least one S/G element will fit.
  7737. */
  7738. sg_size = sizeof(struct ahd_dma_seg);
  7739. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  7740. sg_size = sizeof(struct ahd_dma64_seg);
  7741. while (sg_prefetch_cnt < sg_size)
  7742. sg_prefetch_cnt += sg_prefetch_align;
  7743. /*
  7744. * If the cacheline is not an even multiple of
  7745. * the S/G size, we may only get a partial S/G when
  7746. * we align. Add a cacheline if this is the case.
  7747. */
  7748. if ((sg_prefetch_align % sg_size) != 0
  7749. && (sg_prefetch_cnt < CCSGADDR_MAX))
  7750. sg_prefetch_cnt += sg_prefetch_align;
  7751. /*
  7752. * Lastly, compute a value that the sequencer can use
  7753. * to determine if the remainder of the CCSGRAM buffer
  7754. * has a full S/G element in it.
  7755. */
  7756. sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
  7757. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  7758. download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
  7759. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
  7760. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
  7761. download_consts[SG_SIZEOF] = sg_size;
  7762. download_consts[PKT_OVERRUN_BUFOFFSET] =
  7763. (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
  7764. download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
  7765. download_consts[CACHELINE_MASK] = cacheline_mask;
  7766. cur_patch = patches;
  7767. downloaded = 0;
  7768. skip_addr = 0;
  7769. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7770. ahd_outw(ahd, PRGMCNT, 0);
  7771. for (i = 0; i < sizeof(seqprog)/4; i++) {
  7772. if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
  7773. /*
  7774. * Don't download this instruction as it
  7775. * is in a patch that was removed.
  7776. */
  7777. continue;
  7778. }
  7779. /*
  7780. * Move through the CS table until we find a CS
  7781. * that might apply to this instruction.
  7782. */
  7783. for (; cur_cs < num_critical_sections; cur_cs++) {
  7784. if (critical_sections[cur_cs].end <= i) {
  7785. if (begin_set[cs_count] == TRUE
  7786. && end_set[cs_count] == FALSE) {
  7787. cs_table[cs_count].end = downloaded;
  7788. end_set[cs_count] = TRUE;
  7789. cs_count++;
  7790. }
  7791. continue;
  7792. }
  7793. if (critical_sections[cur_cs].begin <= i
  7794. && begin_set[cs_count] == FALSE) {
  7795. cs_table[cs_count].begin = downloaded;
  7796. begin_set[cs_count] = TRUE;
  7797. }
  7798. break;
  7799. }
  7800. ahd_download_instr(ahd, i, download_consts);
  7801. downloaded++;
  7802. }
  7803. ahd->num_critical_sections = cs_count;
  7804. if (cs_count != 0) {
  7805. cs_count *= sizeof(struct cs);
  7806. ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  7807. if (ahd->critical_sections == NULL)
  7808. panic("ahd_loadseq: Could not malloc");
  7809. memcpy(ahd->critical_sections, cs_table, cs_count);
  7810. }
  7811. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
  7812. if (bootverbose) {
  7813. printf(" %d instructions downloaded\n", downloaded);
  7814. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  7815. ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
  7816. }
  7817. }
  7818. static int
  7819. ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
  7820. u_int start_instr, u_int *skip_addr)
  7821. {
  7822. struct patch *cur_patch;
  7823. struct patch *last_patch;
  7824. u_int num_patches;
  7825. num_patches = sizeof(patches)/sizeof(struct patch);
  7826. last_patch = &patches[num_patches];
  7827. cur_patch = *start_patch;
  7828. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  7829. if (cur_patch->patch_func(ahd) == 0) {
  7830. /* Start rejecting code */
  7831. *skip_addr = start_instr + cur_patch->skip_instr;
  7832. cur_patch += cur_patch->skip_patch;
  7833. } else {
  7834. /* Accepted this patch. Advance to the next
  7835. * one and wait for our intruction pointer to
  7836. * hit this point.
  7837. */
  7838. cur_patch++;
  7839. }
  7840. }
  7841. *start_patch = cur_patch;
  7842. if (start_instr < *skip_addr)
  7843. /* Still skipping */
  7844. return (0);
  7845. return (1);
  7846. }
  7847. static u_int
  7848. ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
  7849. {
  7850. struct patch *cur_patch;
  7851. int address_offset;
  7852. u_int skip_addr;
  7853. u_int i;
  7854. address_offset = 0;
  7855. cur_patch = patches;
  7856. skip_addr = 0;
  7857. for (i = 0; i < address;) {
  7858. ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
  7859. if (skip_addr > i) {
  7860. int end_addr;
  7861. end_addr = MIN(address, skip_addr);
  7862. address_offset += end_addr - i;
  7863. i = skip_addr;
  7864. } else {
  7865. i++;
  7866. }
  7867. }
  7868. return (address - address_offset);
  7869. }
  7870. static void
  7871. ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
  7872. {
  7873. union ins_formats instr;
  7874. struct ins_format1 *fmt1_ins;
  7875. struct ins_format3 *fmt3_ins;
  7876. u_int opcode;
  7877. /*
  7878. * The firmware is always compiled into a little endian format.
  7879. */
  7880. instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  7881. fmt1_ins = &instr.format1;
  7882. fmt3_ins = NULL;
  7883. /* Pull the opcode */
  7884. opcode = instr.format1.opcode;
  7885. switch (opcode) {
  7886. case AIC_OP_JMP:
  7887. case AIC_OP_JC:
  7888. case AIC_OP_JNC:
  7889. case AIC_OP_CALL:
  7890. case AIC_OP_JNE:
  7891. case AIC_OP_JNZ:
  7892. case AIC_OP_JE:
  7893. case AIC_OP_JZ:
  7894. {
  7895. fmt3_ins = &instr.format3;
  7896. fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
  7897. /* FALLTHROUGH */
  7898. }
  7899. case AIC_OP_OR:
  7900. case AIC_OP_AND:
  7901. case AIC_OP_XOR:
  7902. case AIC_OP_ADD:
  7903. case AIC_OP_ADC:
  7904. case AIC_OP_BMOV:
  7905. if (fmt1_ins->parity != 0) {
  7906. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  7907. }
  7908. fmt1_ins->parity = 0;
  7909. /* FALLTHROUGH */
  7910. case AIC_OP_ROL:
  7911. {
  7912. int i, count;
  7913. /* Calculate odd parity for the instruction */
  7914. for (i = 0, count = 0; i < 31; i++) {
  7915. uint32_t mask;
  7916. mask = 0x01 << i;
  7917. if ((instr.integer & mask) != 0)
  7918. count++;
  7919. }
  7920. if ((count & 0x01) == 0)
  7921. instr.format1.parity = 1;
  7922. /* The sequencer is a little endian cpu */
  7923. instr.integer = ahd_htole32(instr.integer);
  7924. ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
  7925. break;
  7926. }
  7927. default:
  7928. panic("Unknown opcode encountered in seq program");
  7929. break;
  7930. }
  7931. }
  7932. static int
  7933. ahd_probe_stack_size(struct ahd_softc *ahd)
  7934. {
  7935. int last_probe;
  7936. last_probe = 0;
  7937. while (1) {
  7938. int i;
  7939. /*
  7940. * We avoid using 0 as a pattern to avoid
  7941. * confusion if the stack implementation
  7942. * "back-fills" with zeros when "poping'
  7943. * entries.
  7944. */
  7945. for (i = 1; i <= last_probe+1; i++) {
  7946. ahd_outb(ahd, STACK, i & 0xFF);
  7947. ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
  7948. }
  7949. /* Verify */
  7950. for (i = last_probe+1; i > 0; i--) {
  7951. u_int stack_entry;
  7952. stack_entry = ahd_inb(ahd, STACK)
  7953. |(ahd_inb(ahd, STACK) << 8);
  7954. if (stack_entry != i)
  7955. goto sized;
  7956. }
  7957. last_probe++;
  7958. }
  7959. sized:
  7960. return (last_probe);
  7961. }
  7962. int
  7963. ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
  7964. const char *name, u_int address, u_int value,
  7965. u_int *cur_column, u_int wrap_point)
  7966. {
  7967. int printed;
  7968. u_int printed_mask;
  7969. if (cur_column != NULL && *cur_column >= wrap_point) {
  7970. printf("\n");
  7971. *cur_column = 0;
  7972. }
  7973. printed = printf("%s[0x%x]", name, value);
  7974. if (table == NULL) {
  7975. printed += printf(" ");
  7976. *cur_column += printed;
  7977. return (printed);
  7978. }
  7979. printed_mask = 0;
  7980. while (printed_mask != 0xFF) {
  7981. int entry;
  7982. for (entry = 0; entry < num_entries; entry++) {
  7983. if (((value & table[entry].mask)
  7984. != table[entry].value)
  7985. || ((printed_mask & table[entry].mask)
  7986. == table[entry].mask))
  7987. continue;
  7988. printed += printf("%s%s",
  7989. printed_mask == 0 ? ":(" : "|",
  7990. table[entry].name);
  7991. printed_mask |= table[entry].mask;
  7992. break;
  7993. }
  7994. if (entry >= num_entries)
  7995. break;
  7996. }
  7997. if (printed_mask != 0)
  7998. printed += printf(") ");
  7999. else
  8000. printed += printf(" ");
  8001. if (cur_column != NULL)
  8002. *cur_column += printed;
  8003. return (printed);
  8004. }
  8005. void
  8006. ahd_dump_card_state(struct ahd_softc *ahd)
  8007. {
  8008. struct scb *scb;
  8009. ahd_mode_state saved_modes;
  8010. u_int dffstat;
  8011. int paused;
  8012. u_int scb_index;
  8013. u_int saved_scb_index;
  8014. u_int cur_col;
  8015. int i;
  8016. if (ahd_is_paused(ahd)) {
  8017. paused = 1;
  8018. } else {
  8019. paused = 0;
  8020. ahd_pause(ahd);
  8021. }
  8022. saved_modes = ahd_save_modes(ahd);
  8023. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  8024. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  8025. "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
  8026. ahd_name(ahd),
  8027. ahd_inw(ahd, CURADDR),
  8028. ahd_build_mode_state(ahd, ahd->saved_src_mode,
  8029. ahd->saved_dst_mode));
  8030. if (paused)
  8031. printf("Card was paused\n");
  8032. if (ahd_check_cmdcmpltqueues(ahd))
  8033. printf("Completions are pending\n");
  8034. /*
  8035. * Mode independent registers.
  8036. */
  8037. cur_col = 0;
  8038. ahd_intstat_print(ahd_inb(ahd, INTSTAT), &cur_col, 50);
  8039. ahd_seloid_print(ahd_inb(ahd, SELOID), &cur_col, 50);
  8040. ahd_selid_print(ahd_inb(ahd, SELID), &cur_col, 50);
  8041. ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
  8042. ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
  8043. ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
  8044. ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
  8045. ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
  8046. ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
  8047. ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
  8048. ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
  8049. ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
  8050. ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
  8051. ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
  8052. ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
  8053. ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
  8054. ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
  8055. ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
  8056. ahd_qfreeze_count_print(ahd_inw(ahd, QFREEZE_COUNT), &cur_col, 50);
  8057. ahd_kernel_qfreeze_count_print(ahd_inw(ahd, KERNEL_QFREEZE_COUNT),
  8058. &cur_col, 50);
  8059. ahd_mk_message_scb_print(ahd_inw(ahd, MK_MESSAGE_SCB), &cur_col, 50);
  8060. ahd_mk_message_scsiid_print(ahd_inb(ahd, MK_MESSAGE_SCSIID),
  8061. &cur_col, 50);
  8062. ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
  8063. ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
  8064. ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
  8065. ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
  8066. ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
  8067. ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
  8068. ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
  8069. ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
  8070. ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
  8071. ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
  8072. ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
  8073. ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
  8074. printf("\n");
  8075. printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
  8076. "CURRSCB 0x%x NEXTSCB 0x%x\n",
  8077. ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
  8078. ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
  8079. ahd_inw(ahd, NEXTSCB));
  8080. cur_col = 0;
  8081. /* QINFIFO */
  8082. ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  8083. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  8084. ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
  8085. saved_scb_index = ahd_get_scbptr(ahd);
  8086. printf("Pending list:");
  8087. i = 0;
  8088. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  8089. if (i++ > AHD_SCB_MAX)
  8090. break;
  8091. cur_col = printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
  8092. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT));
  8093. ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
  8094. ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL),
  8095. &cur_col, 60);
  8096. ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID),
  8097. &cur_col, 60);
  8098. }
  8099. printf("\nTotal %d\n", i);
  8100. printf("Kernel Free SCB list: ");
  8101. i = 0;
  8102. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  8103. struct scb *list_scb;
  8104. list_scb = scb;
  8105. do {
  8106. printf("%d ", SCB_GET_TAG(list_scb));
  8107. list_scb = LIST_NEXT(list_scb, collision_links);
  8108. } while (list_scb && i++ < AHD_SCB_MAX);
  8109. }
  8110. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  8111. if (i++ > AHD_SCB_MAX)
  8112. break;
  8113. printf("%d ", SCB_GET_TAG(scb));
  8114. }
  8115. printf("\n");
  8116. printf("Sequencer Complete DMA-inprog list: ");
  8117. scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
  8118. i = 0;
  8119. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8120. ahd_set_scbptr(ahd, scb_index);
  8121. printf("%d ", scb_index);
  8122. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8123. }
  8124. printf("\n");
  8125. printf("Sequencer Complete list: ");
  8126. scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  8127. i = 0;
  8128. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8129. ahd_set_scbptr(ahd, scb_index);
  8130. printf("%d ", scb_index);
  8131. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8132. }
  8133. printf("\n");
  8134. printf("Sequencer DMA-Up and Complete list: ");
  8135. scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  8136. i = 0;
  8137. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8138. ahd_set_scbptr(ahd, scb_index);
  8139. printf("%d ", scb_index);
  8140. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8141. }
  8142. printf("\n");
  8143. printf("Sequencer On QFreeze and Complete list: ");
  8144. scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
  8145. i = 0;
  8146. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8147. ahd_set_scbptr(ahd, scb_index);
  8148. printf("%d ", scb_index);
  8149. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8150. }
  8151. printf("\n");
  8152. ahd_set_scbptr(ahd, saved_scb_index);
  8153. dffstat = ahd_inb(ahd, DFFSTAT);
  8154. for (i = 0; i < 2; i++) {
  8155. #ifdef AHD_DEBUG
  8156. struct scb *fifo_scb;
  8157. #endif
  8158. u_int fifo_scbptr;
  8159. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  8160. fifo_scbptr = ahd_get_scbptr(ahd);
  8161. printf("\n\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
  8162. ahd_name(ahd), i,
  8163. (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
  8164. ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
  8165. cur_col = 0;
  8166. ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
  8167. ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
  8168. ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
  8169. ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
  8170. ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
  8171. &cur_col, 50);
  8172. ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
  8173. ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
  8174. ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
  8175. ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
  8176. if (cur_col > 50) {
  8177. printf("\n");
  8178. cur_col = 0;
  8179. }
  8180. cur_col += printf("SHADDR = 0x%x%x, SHCNT = 0x%x ",
  8181. ahd_inl(ahd, SHADDR+4),
  8182. ahd_inl(ahd, SHADDR),
  8183. (ahd_inb(ahd, SHCNT)
  8184. | (ahd_inb(ahd, SHCNT + 1) << 8)
  8185. | (ahd_inb(ahd, SHCNT + 2) << 16)));
  8186. if (cur_col > 50) {
  8187. printf("\n");
  8188. cur_col = 0;
  8189. }
  8190. cur_col += printf("HADDR = 0x%x%x, HCNT = 0x%x ",
  8191. ahd_inl(ahd, HADDR+4),
  8192. ahd_inl(ahd, HADDR),
  8193. (ahd_inb(ahd, HCNT)
  8194. | (ahd_inb(ahd, HCNT + 1) << 8)
  8195. | (ahd_inb(ahd, HCNT + 2) << 16)));
  8196. ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
  8197. #ifdef AHD_DEBUG
  8198. if ((ahd_debug & AHD_SHOW_SG) != 0) {
  8199. fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
  8200. if (fifo_scb != NULL)
  8201. ahd_dump_sglist(fifo_scb);
  8202. }
  8203. #endif
  8204. }
  8205. printf("\nLQIN: ");
  8206. for (i = 0; i < 20; i++)
  8207. printf("0x%x ", ahd_inb(ahd, LQIN + i));
  8208. printf("\n");
  8209. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  8210. printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
  8211. ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
  8212. ahd_inb(ahd, OPTIONMODE));
  8213. printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
  8214. ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
  8215. ahd_inb(ahd, MAXCMDCNT));
  8216. printf("%s: SAVED_SCSIID = 0x%x SAVED_LUN = 0x%x\n",
  8217. ahd_name(ahd), ahd_inb(ahd, SAVED_SCSIID),
  8218. ahd_inb(ahd, SAVED_LUN));
  8219. ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
  8220. printf("\n");
  8221. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  8222. cur_col = 0;
  8223. ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50);
  8224. printf("\n");
  8225. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  8226. printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
  8227. ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
  8228. ahd_inw(ahd, DINDEX));
  8229. printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
  8230. ahd_name(ahd), ahd_get_scbptr(ahd),
  8231. ahd_inw_scbram(ahd, SCB_NEXT),
  8232. ahd_inw_scbram(ahd, SCB_NEXT2));
  8233. printf("CDB %x %x %x %x %x %x\n",
  8234. ahd_inb_scbram(ahd, SCB_CDB_STORE),
  8235. ahd_inb_scbram(ahd, SCB_CDB_STORE+1),
  8236. ahd_inb_scbram(ahd, SCB_CDB_STORE+2),
  8237. ahd_inb_scbram(ahd, SCB_CDB_STORE+3),
  8238. ahd_inb_scbram(ahd, SCB_CDB_STORE+4),
  8239. ahd_inb_scbram(ahd, SCB_CDB_STORE+5));
  8240. printf("STACK:");
  8241. for (i = 0; i < ahd->stack_size; i++) {
  8242. ahd->saved_stack[i] =
  8243. ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
  8244. printf(" 0x%x", ahd->saved_stack[i]);
  8245. }
  8246. for (i = ahd->stack_size-1; i >= 0; i--) {
  8247. ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
  8248. ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
  8249. }
  8250. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  8251. ahd_restore_modes(ahd, saved_modes);
  8252. if (paused == 0)
  8253. ahd_unpause(ahd);
  8254. }
  8255. void
  8256. ahd_dump_scbs(struct ahd_softc *ahd)
  8257. {
  8258. ahd_mode_state saved_modes;
  8259. u_int saved_scb_index;
  8260. int i;
  8261. saved_modes = ahd_save_modes(ahd);
  8262. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  8263. saved_scb_index = ahd_get_scbptr(ahd);
  8264. for (i = 0; i < AHD_SCB_MAX; i++) {
  8265. ahd_set_scbptr(ahd, i);
  8266. printf("%3d", i);
  8267. printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
  8268. ahd_inb_scbram(ahd, SCB_CONTROL),
  8269. ahd_inb_scbram(ahd, SCB_SCSIID),
  8270. ahd_inw_scbram(ahd, SCB_NEXT),
  8271. ahd_inw_scbram(ahd, SCB_NEXT2),
  8272. ahd_inl_scbram(ahd, SCB_SGPTR),
  8273. ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR));
  8274. }
  8275. printf("\n");
  8276. ahd_set_scbptr(ahd, saved_scb_index);
  8277. ahd_restore_modes(ahd, saved_modes);
  8278. }
  8279. /**************************** Flexport Logic **********************************/
  8280. /*
  8281. * Read count 16bit words from 16bit word address start_addr from the
  8282. * SEEPROM attached to the controller, into buf, using the controller's
  8283. * SEEPROM reading state machine. Optionally treat the data as a byte
  8284. * stream in terms of byte order.
  8285. */
  8286. int
  8287. ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8288. u_int start_addr, u_int count, int bytestream)
  8289. {
  8290. u_int cur_addr;
  8291. u_int end_addr;
  8292. int error;
  8293. /*
  8294. * If we never make it through the loop even once,
  8295. * we were passed invalid arguments.
  8296. */
  8297. error = EINVAL;
  8298. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8299. end_addr = start_addr + count;
  8300. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8301. ahd_outb(ahd, SEEADR, cur_addr);
  8302. ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
  8303. error = ahd_wait_seeprom(ahd);
  8304. if (error)
  8305. break;
  8306. if (bytestream != 0) {
  8307. uint8_t *bytestream_ptr;
  8308. bytestream_ptr = (uint8_t *)buf;
  8309. *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
  8310. *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
  8311. } else {
  8312. /*
  8313. * ahd_inw() already handles machine byte order.
  8314. */
  8315. *buf = ahd_inw(ahd, SEEDAT);
  8316. }
  8317. buf++;
  8318. }
  8319. return (error);
  8320. }
  8321. /*
  8322. * Write count 16bit words from buf, into SEEPROM attache to the
  8323. * controller starting at 16bit word address start_addr, using the
  8324. * controller's SEEPROM writing state machine.
  8325. */
  8326. int
  8327. ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8328. u_int start_addr, u_int count)
  8329. {
  8330. u_int cur_addr;
  8331. u_int end_addr;
  8332. int error;
  8333. int retval;
  8334. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8335. error = ENOENT;
  8336. /* Place the chip into write-enable mode */
  8337. ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
  8338. ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
  8339. error = ahd_wait_seeprom(ahd);
  8340. if (error)
  8341. return (error);
  8342. /*
  8343. * Write the data. If we don't get throught the loop at
  8344. * least once, the arguments were invalid.
  8345. */
  8346. retval = EINVAL;
  8347. end_addr = start_addr + count;
  8348. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8349. ahd_outw(ahd, SEEDAT, *buf++);
  8350. ahd_outb(ahd, SEEADR, cur_addr);
  8351. ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
  8352. retval = ahd_wait_seeprom(ahd);
  8353. if (retval)
  8354. break;
  8355. }
  8356. /*
  8357. * Disable writes.
  8358. */
  8359. ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
  8360. ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
  8361. error = ahd_wait_seeprom(ahd);
  8362. if (error)
  8363. return (error);
  8364. return (retval);
  8365. }
  8366. /*
  8367. * Wait ~100us for the serial eeprom to satisfy our request.
  8368. */
  8369. int
  8370. ahd_wait_seeprom(struct ahd_softc *ahd)
  8371. {
  8372. int cnt;
  8373. cnt = 5000;
  8374. while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
  8375. ahd_delay(5);
  8376. if (cnt == 0)
  8377. return (ETIMEDOUT);
  8378. return (0);
  8379. }
  8380. /*
  8381. * Validate the two checksums in the per_channel
  8382. * vital product data struct.
  8383. */
  8384. int
  8385. ahd_verify_vpd_cksum(struct vpd_config *vpd)
  8386. {
  8387. int i;
  8388. int maxaddr;
  8389. uint32_t checksum;
  8390. uint8_t *vpdarray;
  8391. vpdarray = (uint8_t *)vpd;
  8392. maxaddr = offsetof(struct vpd_config, vpd_checksum);
  8393. checksum = 0;
  8394. for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
  8395. checksum = checksum + vpdarray[i];
  8396. if (checksum == 0
  8397. || (-checksum & 0xFF) != vpd->vpd_checksum)
  8398. return (0);
  8399. checksum = 0;
  8400. maxaddr = offsetof(struct vpd_config, checksum);
  8401. for (i = offsetof(struct vpd_config, default_target_flags);
  8402. i < maxaddr; i++)
  8403. checksum = checksum + vpdarray[i];
  8404. if (checksum == 0
  8405. || (-checksum & 0xFF) != vpd->checksum)
  8406. return (0);
  8407. return (1);
  8408. }
  8409. int
  8410. ahd_verify_cksum(struct seeprom_config *sc)
  8411. {
  8412. int i;
  8413. int maxaddr;
  8414. uint32_t checksum;
  8415. uint16_t *scarray;
  8416. maxaddr = (sizeof(*sc)/2) - 1;
  8417. checksum = 0;
  8418. scarray = (uint16_t *)sc;
  8419. for (i = 0; i < maxaddr; i++)
  8420. checksum = checksum + scarray[i];
  8421. if (checksum == 0
  8422. || (checksum & 0xFFFF) != sc->checksum) {
  8423. return (0);
  8424. } else {
  8425. return (1);
  8426. }
  8427. }
  8428. int
  8429. ahd_acquire_seeprom(struct ahd_softc *ahd)
  8430. {
  8431. /*
  8432. * We should be able to determine the SEEPROM type
  8433. * from the flexport logic, but unfortunately not
  8434. * all implementations have this logic and there is
  8435. * no programatic method for determining if the logic
  8436. * is present.
  8437. */
  8438. return (1);
  8439. #if 0
  8440. uint8_t seetype;
  8441. int error;
  8442. error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
  8443. if (error != 0
  8444. || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
  8445. return (0);
  8446. return (1);
  8447. #endif
  8448. }
  8449. void
  8450. ahd_release_seeprom(struct ahd_softc *ahd)
  8451. {
  8452. /* Currently a no-op */
  8453. }
  8454. int
  8455. ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
  8456. {
  8457. int error;
  8458. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8459. if (addr > 7)
  8460. panic("ahd_write_flexport: address out of range");
  8461. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8462. error = ahd_wait_flexport(ahd);
  8463. if (error != 0)
  8464. return (error);
  8465. ahd_outb(ahd, BRDDAT, value);
  8466. ahd_flush_device_writes(ahd);
  8467. ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
  8468. ahd_flush_device_writes(ahd);
  8469. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8470. ahd_flush_device_writes(ahd);
  8471. ahd_outb(ahd, BRDCTL, 0);
  8472. ahd_flush_device_writes(ahd);
  8473. return (0);
  8474. }
  8475. int
  8476. ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
  8477. {
  8478. int error;
  8479. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8480. if (addr > 7)
  8481. panic("ahd_read_flexport: address out of range");
  8482. ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
  8483. error = ahd_wait_flexport(ahd);
  8484. if (error != 0)
  8485. return (error);
  8486. *value = ahd_inb(ahd, BRDDAT);
  8487. ahd_outb(ahd, BRDCTL, 0);
  8488. ahd_flush_device_writes(ahd);
  8489. return (0);
  8490. }
  8491. /*
  8492. * Wait at most 2 seconds for flexport arbitration to succeed.
  8493. */
  8494. int
  8495. ahd_wait_flexport(struct ahd_softc *ahd)
  8496. {
  8497. int cnt;
  8498. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8499. cnt = 1000000 * 2 / 5;
  8500. while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
  8501. ahd_delay(5);
  8502. if (cnt == 0)
  8503. return (ETIMEDOUT);
  8504. return (0);
  8505. }
  8506. /************************* Target Mode ****************************************/
  8507. #ifdef AHD_TARGET_MODE
  8508. cam_status
  8509. ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
  8510. struct ahd_tmode_tstate **tstate,
  8511. struct ahd_tmode_lstate **lstate,
  8512. int notfound_failure)
  8513. {
  8514. if ((ahd->features & AHD_TARGETMODE) == 0)
  8515. return (CAM_REQ_INVALID);
  8516. /*
  8517. * Handle the 'black hole' device that sucks up
  8518. * requests to unattached luns on enabled targets.
  8519. */
  8520. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  8521. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  8522. *tstate = NULL;
  8523. *lstate = ahd->black_hole;
  8524. } else {
  8525. u_int max_id;
  8526. max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
  8527. if (ccb->ccb_h.target_id > max_id)
  8528. return (CAM_TID_INVALID);
  8529. if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
  8530. return (CAM_LUN_INVALID);
  8531. *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
  8532. *lstate = NULL;
  8533. if (*tstate != NULL)
  8534. *lstate =
  8535. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  8536. }
  8537. if (notfound_failure != 0 && *lstate == NULL)
  8538. return (CAM_PATH_INVALID);
  8539. return (CAM_REQ_CMP);
  8540. }
  8541. void
  8542. ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
  8543. {
  8544. #if NOT_YET
  8545. struct ahd_tmode_tstate *tstate;
  8546. struct ahd_tmode_lstate *lstate;
  8547. struct ccb_en_lun *cel;
  8548. cam_status status;
  8549. u_int target;
  8550. u_int lun;
  8551. u_int target_mask;
  8552. u_long s;
  8553. char channel;
  8554. status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
  8555. /*notfound_failure*/FALSE);
  8556. if (status != CAM_REQ_CMP) {
  8557. ccb->ccb_h.status = status;
  8558. return;
  8559. }
  8560. if ((ahd->features & AHD_MULTIROLE) != 0) {
  8561. u_int our_id;
  8562. our_id = ahd->our_id;
  8563. if (ccb->ccb_h.target_id != our_id) {
  8564. if ((ahd->features & AHD_MULTI_TID) != 0
  8565. && (ahd->flags & AHD_INITIATORROLE) != 0) {
  8566. /*
  8567. * Only allow additional targets if
  8568. * the initiator role is disabled.
  8569. * The hardware cannot handle a re-select-in
  8570. * on the initiator id during a re-select-out
  8571. * on a different target id.
  8572. */
  8573. status = CAM_TID_INVALID;
  8574. } else if ((ahd->flags & AHD_INITIATORROLE) != 0
  8575. || ahd->enabled_luns > 0) {
  8576. /*
  8577. * Only allow our target id to change
  8578. * if the initiator role is not configured
  8579. * and there are no enabled luns which
  8580. * are attached to the currently registered
  8581. * scsi id.
  8582. */
  8583. status = CAM_TID_INVALID;
  8584. }
  8585. }
  8586. }
  8587. if (status != CAM_REQ_CMP) {
  8588. ccb->ccb_h.status = status;
  8589. return;
  8590. }
  8591. /*
  8592. * We now have an id that is valid.
  8593. * If we aren't in target mode, switch modes.
  8594. */
  8595. if ((ahd->flags & AHD_TARGETROLE) == 0
  8596. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  8597. u_long s;
  8598. printf("Configuring Target Mode\n");
  8599. ahd_lock(ahd, &s);
  8600. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  8601. ccb->ccb_h.status = CAM_BUSY;
  8602. ahd_unlock(ahd, &s);
  8603. return;
  8604. }
  8605. ahd->flags |= AHD_TARGETROLE;
  8606. if ((ahd->features & AHD_MULTIROLE) == 0)
  8607. ahd->flags &= ~AHD_INITIATORROLE;
  8608. ahd_pause(ahd);
  8609. ahd_loadseq(ahd);
  8610. ahd_restart(ahd);
  8611. ahd_unlock(ahd, &s);
  8612. }
  8613. cel = &ccb->cel;
  8614. target = ccb->ccb_h.target_id;
  8615. lun = ccb->ccb_h.target_lun;
  8616. channel = SIM_CHANNEL(ahd, sim);
  8617. target_mask = 0x01 << target;
  8618. if (channel == 'B')
  8619. target_mask <<= 8;
  8620. if (cel->enable != 0) {
  8621. u_int scsiseq1;
  8622. /* Are we already enabled?? */
  8623. if (lstate != NULL) {
  8624. xpt_print_path(ccb->ccb_h.path);
  8625. printf("Lun already enabled\n");
  8626. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  8627. return;
  8628. }
  8629. if (cel->grp6_len != 0
  8630. || cel->grp7_len != 0) {
  8631. /*
  8632. * Don't (yet?) support vendor
  8633. * specific commands.
  8634. */
  8635. ccb->ccb_h.status = CAM_REQ_INVALID;
  8636. printf("Non-zero Group Codes\n");
  8637. return;
  8638. }
  8639. /*
  8640. * Seems to be okay.
  8641. * Setup our data structures.
  8642. */
  8643. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  8644. tstate = ahd_alloc_tstate(ahd, target, channel);
  8645. if (tstate == NULL) {
  8646. xpt_print_path(ccb->ccb_h.path);
  8647. printf("Couldn't allocate tstate\n");
  8648. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8649. return;
  8650. }
  8651. }
  8652. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  8653. if (lstate == NULL) {
  8654. xpt_print_path(ccb->ccb_h.path);
  8655. printf("Couldn't allocate lstate\n");
  8656. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8657. return;
  8658. }
  8659. memset(lstate, 0, sizeof(*lstate));
  8660. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  8661. xpt_path_path_id(ccb->ccb_h.path),
  8662. xpt_path_target_id(ccb->ccb_h.path),
  8663. xpt_path_lun_id(ccb->ccb_h.path));
  8664. if (status != CAM_REQ_CMP) {
  8665. free(lstate, M_DEVBUF);
  8666. xpt_print_path(ccb->ccb_h.path);
  8667. printf("Couldn't allocate path\n");
  8668. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8669. return;
  8670. }
  8671. SLIST_INIT(&lstate->accept_tios);
  8672. SLIST_INIT(&lstate->immed_notifies);
  8673. ahd_lock(ahd, &s);
  8674. ahd_pause(ahd);
  8675. if (target != CAM_TARGET_WILDCARD) {
  8676. tstate->enabled_luns[lun] = lstate;
  8677. ahd->enabled_luns++;
  8678. if ((ahd->features & AHD_MULTI_TID) != 0) {
  8679. u_int targid_mask;
  8680. targid_mask = ahd_inw(ahd, TARGID);
  8681. targid_mask |= target_mask;
  8682. ahd_outw(ahd, TARGID, targid_mask);
  8683. ahd_update_scsiid(ahd, targid_mask);
  8684. } else {
  8685. u_int our_id;
  8686. char channel;
  8687. channel = SIM_CHANNEL(ahd, sim);
  8688. our_id = SIM_SCSI_ID(ahd, sim);
  8689. /*
  8690. * This can only happen if selections
  8691. * are not enabled
  8692. */
  8693. if (target != our_id) {
  8694. u_int sblkctl;
  8695. char cur_channel;
  8696. int swap;
  8697. sblkctl = ahd_inb(ahd, SBLKCTL);
  8698. cur_channel = (sblkctl & SELBUSB)
  8699. ? 'B' : 'A';
  8700. if ((ahd->features & AHD_TWIN) == 0)
  8701. cur_channel = 'A';
  8702. swap = cur_channel != channel;
  8703. ahd->our_id = target;
  8704. if (swap)
  8705. ahd_outb(ahd, SBLKCTL,
  8706. sblkctl ^ SELBUSB);
  8707. ahd_outb(ahd, SCSIID, target);
  8708. if (swap)
  8709. ahd_outb(ahd, SBLKCTL, sblkctl);
  8710. }
  8711. }
  8712. } else
  8713. ahd->black_hole = lstate;
  8714. /* Allow select-in operations */
  8715. if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
  8716. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8717. scsiseq1 |= ENSELI;
  8718. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8719. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8720. scsiseq1 |= ENSELI;
  8721. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8722. }
  8723. ahd_unpause(ahd);
  8724. ahd_unlock(ahd, &s);
  8725. ccb->ccb_h.status = CAM_REQ_CMP;
  8726. xpt_print_path(ccb->ccb_h.path);
  8727. printf("Lun now enabled for target mode\n");
  8728. } else {
  8729. struct scb *scb;
  8730. int i, empty;
  8731. if (lstate == NULL) {
  8732. ccb->ccb_h.status = CAM_LUN_INVALID;
  8733. return;
  8734. }
  8735. ahd_lock(ahd, &s);
  8736. ccb->ccb_h.status = CAM_REQ_CMP;
  8737. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  8738. struct ccb_hdr *ccbh;
  8739. ccbh = &scb->io_ctx->ccb_h;
  8740. if (ccbh->func_code == XPT_CONT_TARGET_IO
  8741. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  8742. printf("CTIO pending\n");
  8743. ccb->ccb_h.status = CAM_REQ_INVALID;
  8744. ahd_unlock(ahd, &s);
  8745. return;
  8746. }
  8747. }
  8748. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  8749. printf("ATIOs pending\n");
  8750. ccb->ccb_h.status = CAM_REQ_INVALID;
  8751. }
  8752. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  8753. printf("INOTs pending\n");
  8754. ccb->ccb_h.status = CAM_REQ_INVALID;
  8755. }
  8756. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  8757. ahd_unlock(ahd, &s);
  8758. return;
  8759. }
  8760. xpt_print_path(ccb->ccb_h.path);
  8761. printf("Target mode disabled\n");
  8762. xpt_free_path(lstate->path);
  8763. free(lstate, M_DEVBUF);
  8764. ahd_pause(ahd);
  8765. /* Can we clean up the target too? */
  8766. if (target != CAM_TARGET_WILDCARD) {
  8767. tstate->enabled_luns[lun] = NULL;
  8768. ahd->enabled_luns--;
  8769. for (empty = 1, i = 0; i < 8; i++)
  8770. if (tstate->enabled_luns[i] != NULL) {
  8771. empty = 0;
  8772. break;
  8773. }
  8774. if (empty) {
  8775. ahd_free_tstate(ahd, target, channel,
  8776. /*force*/FALSE);
  8777. if (ahd->features & AHD_MULTI_TID) {
  8778. u_int targid_mask;
  8779. targid_mask = ahd_inw(ahd, TARGID);
  8780. targid_mask &= ~target_mask;
  8781. ahd_outw(ahd, TARGID, targid_mask);
  8782. ahd_update_scsiid(ahd, targid_mask);
  8783. }
  8784. }
  8785. } else {
  8786. ahd->black_hole = NULL;
  8787. /*
  8788. * We can't allow selections without
  8789. * our black hole device.
  8790. */
  8791. empty = TRUE;
  8792. }
  8793. if (ahd->enabled_luns == 0) {
  8794. /* Disallow select-in */
  8795. u_int scsiseq1;
  8796. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8797. scsiseq1 &= ~ENSELI;
  8798. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8799. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8800. scsiseq1 &= ~ENSELI;
  8801. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8802. if ((ahd->features & AHD_MULTIROLE) == 0) {
  8803. printf("Configuring Initiator Mode\n");
  8804. ahd->flags &= ~AHD_TARGETROLE;
  8805. ahd->flags |= AHD_INITIATORROLE;
  8806. ahd_pause(ahd);
  8807. ahd_loadseq(ahd);
  8808. ahd_restart(ahd);
  8809. /*
  8810. * Unpaused. The extra unpause
  8811. * that follows is harmless.
  8812. */
  8813. }
  8814. }
  8815. ahd_unpause(ahd);
  8816. ahd_unlock(ahd, &s);
  8817. }
  8818. #endif
  8819. }
  8820. static void
  8821. ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
  8822. {
  8823. #if NOT_YET
  8824. u_int scsiid_mask;
  8825. u_int scsiid;
  8826. if ((ahd->features & AHD_MULTI_TID) == 0)
  8827. panic("ahd_update_scsiid called on non-multitid unit\n");
  8828. /*
  8829. * Since we will rely on the TARGID mask
  8830. * for selection enables, ensure that OID
  8831. * in SCSIID is not set to some other ID
  8832. * that we don't want to allow selections on.
  8833. */
  8834. if ((ahd->features & AHD_ULTRA2) != 0)
  8835. scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
  8836. else
  8837. scsiid = ahd_inb(ahd, SCSIID);
  8838. scsiid_mask = 0x1 << (scsiid & OID);
  8839. if ((targid_mask & scsiid_mask) == 0) {
  8840. u_int our_id;
  8841. /* ffs counts from 1 */
  8842. our_id = ffs(targid_mask);
  8843. if (our_id == 0)
  8844. our_id = ahd->our_id;
  8845. else
  8846. our_id--;
  8847. scsiid &= TID;
  8848. scsiid |= our_id;
  8849. }
  8850. if ((ahd->features & AHD_ULTRA2) != 0)
  8851. ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
  8852. else
  8853. ahd_outb(ahd, SCSIID, scsiid);
  8854. #endif
  8855. }
  8856. void
  8857. ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
  8858. {
  8859. struct target_cmd *cmd;
  8860. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
  8861. while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
  8862. /*
  8863. * Only advance through the queue if we
  8864. * have the resources to process the command.
  8865. */
  8866. if (ahd_handle_target_cmd(ahd, cmd) != 0)
  8867. break;
  8868. cmd->cmd_valid = 0;
  8869. ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
  8870. ahd->shared_data_map.dmamap,
  8871. ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
  8872. sizeof(struct target_cmd),
  8873. BUS_DMASYNC_PREREAD);
  8874. ahd->tqinfifonext++;
  8875. /*
  8876. * Lazily update our position in the target mode incoming
  8877. * command queue as seen by the sequencer.
  8878. */
  8879. if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  8880. u_int hs_mailbox;
  8881. hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
  8882. hs_mailbox &= ~HOST_TQINPOS;
  8883. hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
  8884. ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
  8885. }
  8886. }
  8887. }
  8888. static int
  8889. ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
  8890. {
  8891. struct ahd_tmode_tstate *tstate;
  8892. struct ahd_tmode_lstate *lstate;
  8893. struct ccb_accept_tio *atio;
  8894. uint8_t *byte;
  8895. int initiator;
  8896. int target;
  8897. int lun;
  8898. initiator = SCSIID_TARGET(ahd, cmd->scsiid);
  8899. target = SCSIID_OUR_ID(cmd->scsiid);
  8900. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  8901. byte = cmd->bytes;
  8902. tstate = ahd->enabled_targets[target];
  8903. lstate = NULL;
  8904. if (tstate != NULL)
  8905. lstate = tstate->enabled_luns[lun];
  8906. /*
  8907. * Commands for disabled luns go to the black hole driver.
  8908. */
  8909. if (lstate == NULL)
  8910. lstate = ahd->black_hole;
  8911. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  8912. if (atio == NULL) {
  8913. ahd->flags |= AHD_TQINFIFO_BLOCKED;
  8914. /*
  8915. * Wait for more ATIOs from the peripheral driver for this lun.
  8916. */
  8917. return (1);
  8918. } else
  8919. ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
  8920. #ifdef AHD_DEBUG
  8921. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  8922. printf("Incoming command from %d for %d:%d%s\n",
  8923. initiator, target, lun,
  8924. lstate == ahd->black_hole ? "(Black Holed)" : "");
  8925. #endif
  8926. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  8927. if (lstate == ahd->black_hole) {
  8928. /* Fill in the wildcards */
  8929. atio->ccb_h.target_id = target;
  8930. atio->ccb_h.target_lun = lun;
  8931. }
  8932. /*
  8933. * Package it up and send it off to
  8934. * whomever has this lun enabled.
  8935. */
  8936. atio->sense_len = 0;
  8937. atio->init_id = initiator;
  8938. if (byte[0] != 0xFF) {
  8939. /* Tag was included */
  8940. atio->tag_action = *byte++;
  8941. atio->tag_id = *byte++;
  8942. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  8943. } else {
  8944. atio->ccb_h.flags = 0;
  8945. }
  8946. byte++;
  8947. /* Okay. Now determine the cdb size based on the command code */
  8948. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  8949. case 0:
  8950. atio->cdb_len = 6;
  8951. break;
  8952. case 1:
  8953. case 2:
  8954. atio->cdb_len = 10;
  8955. break;
  8956. case 4:
  8957. atio->cdb_len = 16;
  8958. break;
  8959. case 5:
  8960. atio->cdb_len = 12;
  8961. break;
  8962. case 3:
  8963. default:
  8964. /* Only copy the opcode. */
  8965. atio->cdb_len = 1;
  8966. printf("Reserved or VU command code type encountered\n");
  8967. break;
  8968. }
  8969. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  8970. atio->ccb_h.status |= CAM_CDB_RECVD;
  8971. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  8972. /*
  8973. * We weren't allowed to disconnect.
  8974. * We're hanging on the bus until a
  8975. * continue target I/O comes in response
  8976. * to this accept tio.
  8977. */
  8978. #ifdef AHD_DEBUG
  8979. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  8980. printf("Received Immediate Command %d:%d:%d - %p\n",
  8981. initiator, target, lun, ahd->pending_device);
  8982. #endif
  8983. ahd->pending_device = lstate;
  8984. ahd_freeze_ccb((union ccb *)atio);
  8985. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  8986. }
  8987. xpt_done((union ccb*)atio);
  8988. return (0);
  8989. }
  8990. #endif