aic79xx_core.c 293 KB

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