aic79xx_core.c 272 KB

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