aic79xx_core.c 266 KB

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