aic79xx_core.c 268 KB

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