aic7xxx_core.c 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386
  1. /*
  2. * Core routines and tables shareable across OS platforms.
  3. *
  4. * Copyright (c) 1994-2002 Justin T. Gibbs.
  5. * Copyright (c) 2000-2002 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/aic7xxx.c#155 $
  41. */
  42. #ifdef __linux__
  43. #include "aic7xxx_osm.h"
  44. #include "aic7xxx_inline.h"
  45. #include "aicasm/aicasm_insformat.h"
  46. #else
  47. #include <dev/aic7xxx/aic7xxx_osm.h>
  48. #include <dev/aic7xxx/aic7xxx_inline.h>
  49. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  50. #endif
  51. /***************************** Lookup Tables **********************************/
  52. char *ahc_chip_names[] =
  53. {
  54. "NONE",
  55. "aic7770",
  56. "aic7850",
  57. "aic7855",
  58. "aic7859",
  59. "aic7860",
  60. "aic7870",
  61. "aic7880",
  62. "aic7895",
  63. "aic7895C",
  64. "aic7890/91",
  65. "aic7896/97",
  66. "aic7892",
  67. "aic7899"
  68. };
  69. static const u_int num_chip_names = NUM_ELEMENTS(ahc_chip_names);
  70. /*
  71. * Hardware error codes.
  72. */
  73. struct ahc_hard_error_entry {
  74. uint8_t errno;
  75. char *errmesg;
  76. };
  77. static struct ahc_hard_error_entry ahc_hard_errors[] = {
  78. { ILLHADDR, "Illegal Host Access" },
  79. { ILLSADDR, "Illegal Sequencer Address referrenced" },
  80. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  81. { SQPARERR, "Sequencer Parity Error" },
  82. { DPARERR, "Data-path Parity Error" },
  83. { MPARERR, "Scratch or SCB Memory Parity Error" },
  84. { PCIERRSTAT, "PCI Error detected" },
  85. { CIOPARERR, "CIOBUS Parity Error" },
  86. };
  87. static const u_int num_errors = NUM_ELEMENTS(ahc_hard_errors);
  88. static struct ahc_phase_table_entry ahc_phase_table[] =
  89. {
  90. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  91. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  92. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  93. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  94. { P_COMMAND, MSG_NOOP, "in Command phase" },
  95. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  96. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  97. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  98. { P_BUSFREE, MSG_NOOP, "while idle" },
  99. { 0, MSG_NOOP, "in unknown phase" }
  100. };
  101. /*
  102. * In most cases we only wish to itterate over real phases, so
  103. * exclude the last element from the count.
  104. */
  105. static const u_int num_phases = NUM_ELEMENTS(ahc_phase_table) - 1;
  106. /*
  107. * Valid SCSIRATE values. (p. 3-17)
  108. * Provides a mapping of tranfer periods in ns to the proper value to
  109. * stick in the scsixfer reg.
  110. */
  111. static struct ahc_syncrate ahc_syncrates[] =
  112. {
  113. /* ultra2 fast/ultra period rate */
  114. { 0x42, 0x000, 9, "80.0" },
  115. { 0x03, 0x000, 10, "40.0" },
  116. { 0x04, 0x000, 11, "33.0" },
  117. { 0x05, 0x100, 12, "20.0" },
  118. { 0x06, 0x110, 15, "16.0" },
  119. { 0x07, 0x120, 18, "13.4" },
  120. { 0x08, 0x000, 25, "10.0" },
  121. { 0x19, 0x010, 31, "8.0" },
  122. { 0x1a, 0x020, 37, "6.67" },
  123. { 0x1b, 0x030, 43, "5.7" },
  124. { 0x1c, 0x040, 50, "5.0" },
  125. { 0x00, 0x050, 56, "4.4" },
  126. { 0x00, 0x060, 62, "4.0" },
  127. { 0x00, 0x070, 68, "3.6" },
  128. { 0x00, 0x000, 0, NULL }
  129. };
  130. /* Our Sequencer Program */
  131. #include "aic7xxx_seq.h"
  132. /**************************** Function Declarations ***************************/
  133. static void ahc_force_renegotiation(struct ahc_softc *ahc,
  134. struct ahc_devinfo *devinfo);
  135. static struct ahc_tmode_tstate*
  136. ahc_alloc_tstate(struct ahc_softc *ahc,
  137. u_int scsi_id, char channel);
  138. #ifdef AHC_TARGET_MODE
  139. static void ahc_free_tstate(struct ahc_softc *ahc,
  140. u_int scsi_id, char channel, int force);
  141. #endif
  142. static struct ahc_syncrate*
  143. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  144. struct ahc_initiator_tinfo *,
  145. u_int *period,
  146. u_int *ppr_options,
  147. role_t role);
  148. static void ahc_update_pending_scbs(struct ahc_softc *ahc);
  149. static void ahc_fetch_devinfo(struct ahc_softc *ahc,
  150. struct ahc_devinfo *devinfo);
  151. static void ahc_scb_devinfo(struct ahc_softc *ahc,
  152. struct ahc_devinfo *devinfo,
  153. struct scb *scb);
  154. static void ahc_assert_atn(struct ahc_softc *ahc);
  155. static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
  156. struct ahc_devinfo *devinfo,
  157. struct scb *scb);
  158. static void ahc_build_transfer_msg(struct ahc_softc *ahc,
  159. struct ahc_devinfo *devinfo);
  160. static void ahc_construct_sdtr(struct ahc_softc *ahc,
  161. struct ahc_devinfo *devinfo,
  162. u_int period, u_int offset);
  163. static void ahc_construct_wdtr(struct ahc_softc *ahc,
  164. struct ahc_devinfo *devinfo,
  165. u_int bus_width);
  166. static void ahc_construct_ppr(struct ahc_softc *ahc,
  167. struct ahc_devinfo *devinfo,
  168. u_int period, u_int offset,
  169. u_int bus_width, u_int ppr_options);
  170. static void ahc_clear_msg_state(struct ahc_softc *ahc);
  171. static void ahc_handle_proto_violation(struct ahc_softc *ahc);
  172. static void ahc_handle_message_phase(struct ahc_softc *ahc);
  173. typedef enum {
  174. AHCMSG_1B,
  175. AHCMSG_2B,
  176. AHCMSG_EXT
  177. } ahc_msgtype;
  178. static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
  179. u_int msgval, int full);
  180. static int ahc_parse_msg(struct ahc_softc *ahc,
  181. struct ahc_devinfo *devinfo);
  182. static int ahc_handle_msg_reject(struct ahc_softc *ahc,
  183. struct ahc_devinfo *devinfo);
  184. static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
  185. struct ahc_devinfo *devinfo);
  186. static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
  187. static void ahc_handle_devreset(struct ahc_softc *ahc,
  188. struct ahc_devinfo *devinfo,
  189. cam_status status, char *message,
  190. int verbose_level);
  191. #ifdef AHC_TARGET_MODE
  192. static void ahc_setup_target_msgin(struct ahc_softc *ahc,
  193. struct ahc_devinfo *devinfo,
  194. struct scb *scb);
  195. #endif
  196. static bus_dmamap_callback_t ahc_dmamap_cb;
  197. static void ahc_build_free_scb_list(struct ahc_softc *ahc);
  198. static int ahc_init_scbdata(struct ahc_softc *ahc);
  199. static void ahc_fini_scbdata(struct ahc_softc *ahc);
  200. static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
  201. struct scb *prev_scb,
  202. struct scb *scb);
  203. static int ahc_qinfifo_count(struct ahc_softc *ahc);
  204. static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
  205. u_int prev, u_int scbptr);
  206. static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
  207. static u_int ahc_rem_wscb(struct ahc_softc *ahc,
  208. u_int scbpos, u_int prev);
  209. static void ahc_reset_current_bus(struct ahc_softc *ahc);
  210. #ifdef AHC_DUMP_SEQ
  211. static void ahc_dumpseq(struct ahc_softc *ahc);
  212. #endif
  213. static int ahc_loadseq(struct ahc_softc *ahc);
  214. static int ahc_check_patch(struct ahc_softc *ahc,
  215. struct patch **start_patch,
  216. u_int start_instr, u_int *skip_addr);
  217. static void ahc_download_instr(struct ahc_softc *ahc,
  218. u_int instrptr, uint8_t *dconsts);
  219. #ifdef AHC_TARGET_MODE
  220. static void ahc_queue_lstate_event(struct ahc_softc *ahc,
  221. struct ahc_tmode_lstate *lstate,
  222. u_int initiator_id,
  223. u_int event_type,
  224. u_int event_arg);
  225. static void ahc_update_scsiid(struct ahc_softc *ahc,
  226. u_int targid_mask);
  227. static int ahc_handle_target_cmd(struct ahc_softc *ahc,
  228. struct target_cmd *cmd);
  229. #endif
  230. /************************* Sequencer Execution Control ************************/
  231. /*
  232. * Restart the sequencer program from address zero
  233. */
  234. void
  235. ahc_restart(struct ahc_softc *ahc)
  236. {
  237. ahc_pause(ahc);
  238. /* No more pending messages. */
  239. ahc_clear_msg_state(ahc);
  240. ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
  241. ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
  242. ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
  243. ahc_outb(ahc, LASTPHASE, P_BUSFREE);
  244. ahc_outb(ahc, SAVED_SCSIID, 0xFF);
  245. ahc_outb(ahc, SAVED_LUN, 0xFF);
  246. /*
  247. * Ensure that the sequencer's idea of TQINPOS
  248. * matches our own. The sequencer increments TQINPOS
  249. * only after it sees a DMA complete and a reset could
  250. * occur before the increment leaving the kernel to believe
  251. * the command arrived but the sequencer to not.
  252. */
  253. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  254. /* Always allow reselection */
  255. ahc_outb(ahc, SCSISEQ,
  256. ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  257. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  258. /* Ensure that no DMA operations are in progress */
  259. ahc_outb(ahc, CCSCBCNT, 0);
  260. ahc_outb(ahc, CCSGCTL, 0);
  261. ahc_outb(ahc, CCSCBCTL, 0);
  262. }
  263. /*
  264. * If we were in the process of DMA'ing SCB data into
  265. * an SCB, replace that SCB on the free list. This prevents
  266. * an SCB leak.
  267. */
  268. if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
  269. ahc_add_curscb_to_free_list(ahc);
  270. ahc_outb(ahc, SEQ_FLAGS2,
  271. ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
  272. }
  273. /*
  274. * Clear any pending sequencer interrupt. It is no
  275. * longer relevant since we're resetting the Program
  276. * Counter.
  277. */
  278. ahc_outb(ahc, CLRINT, CLRSEQINT);
  279. ahc_outb(ahc, MWI_RESIDUAL, 0);
  280. ahc_outb(ahc, SEQCTL, ahc->seqctl);
  281. ahc_outb(ahc, SEQADDR0, 0);
  282. ahc_outb(ahc, SEQADDR1, 0);
  283. ahc_unpause(ahc);
  284. }
  285. /************************* Input/Output Queues ********************************/
  286. void
  287. ahc_run_qoutfifo(struct ahc_softc *ahc)
  288. {
  289. struct scb *scb;
  290. u_int scb_index;
  291. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  292. while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
  293. scb_index = ahc->qoutfifo[ahc->qoutfifonext];
  294. if ((ahc->qoutfifonext & 0x03) == 0x03) {
  295. u_int modnext;
  296. /*
  297. * Clear 32bits of QOUTFIFO at a time
  298. * so that we don't clobber an incoming
  299. * byte DMA to the array on architectures
  300. * that only support 32bit load and store
  301. * operations.
  302. */
  303. modnext = ahc->qoutfifonext & ~0x3;
  304. *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
  305. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  306. ahc->shared_data_dmamap,
  307. /*offset*/modnext, /*len*/4,
  308. BUS_DMASYNC_PREREAD);
  309. }
  310. ahc->qoutfifonext++;
  311. scb = ahc_lookup_scb(ahc, scb_index);
  312. if (scb == NULL) {
  313. printf("%s: WARNING no command for scb %d "
  314. "(cmdcmplt)\nQOUTPOS = %d\n",
  315. ahc_name(ahc), scb_index,
  316. (ahc->qoutfifonext - 1) & 0xFF);
  317. continue;
  318. }
  319. /*
  320. * Save off the residual
  321. * if there is one.
  322. */
  323. ahc_update_residual(ahc, scb);
  324. ahc_done(ahc, scb);
  325. }
  326. }
  327. void
  328. ahc_run_untagged_queues(struct ahc_softc *ahc)
  329. {
  330. int i;
  331. for (i = 0; i < 16; i++)
  332. ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
  333. }
  334. void
  335. ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
  336. {
  337. struct scb *scb;
  338. if (ahc->untagged_queue_lock != 0)
  339. return;
  340. if ((scb = TAILQ_FIRST(queue)) != NULL
  341. && (scb->flags & SCB_ACTIVE) == 0) {
  342. scb->flags |= SCB_ACTIVE;
  343. ahc_queue_scb(ahc, scb);
  344. }
  345. }
  346. /************************* Interrupt Handling *********************************/
  347. void
  348. ahc_handle_brkadrint(struct ahc_softc *ahc)
  349. {
  350. /*
  351. * We upset the sequencer :-(
  352. * Lookup the error message
  353. */
  354. int i;
  355. int error;
  356. error = ahc_inb(ahc, ERROR);
  357. for (i = 0; error != 1 && i < num_errors; i++)
  358. error >>= 1;
  359. printf("%s: brkadrint, %s at seqaddr = 0x%x\n",
  360. ahc_name(ahc), ahc_hard_errors[i].errmesg,
  361. ahc_inb(ahc, SEQADDR0) |
  362. (ahc_inb(ahc, SEQADDR1) << 8));
  363. ahc_dump_card_state(ahc);
  364. /* Tell everyone that this HBA is no longer available */
  365. ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  366. CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  367. CAM_NO_HBA);
  368. /* Disable all interrupt sources by resetting the controller */
  369. ahc_shutdown(ahc);
  370. }
  371. void
  372. ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
  373. {
  374. struct scb *scb;
  375. struct ahc_devinfo devinfo;
  376. ahc_fetch_devinfo(ahc, &devinfo);
  377. /*
  378. * Clear the upper byte that holds SEQINT status
  379. * codes and clear the SEQINT bit. We will unpause
  380. * the sequencer, if appropriate, after servicing
  381. * the request.
  382. */
  383. ahc_outb(ahc, CLRINT, CLRSEQINT);
  384. switch (intstat & SEQINT_MASK) {
  385. case BAD_STATUS:
  386. {
  387. u_int scb_index;
  388. struct hardware_scb *hscb;
  389. /*
  390. * Set the default return value to 0 (don't
  391. * send sense). The sense code will change
  392. * this if needed.
  393. */
  394. ahc_outb(ahc, RETURN_1, 0);
  395. /*
  396. * The sequencer will notify us when a command
  397. * has an error that would be of interest to
  398. * the kernel. This allows us to leave the sequencer
  399. * running in the common case of command completes
  400. * without error. The sequencer will already have
  401. * dma'd the SCB back up to us, so we can reference
  402. * the in kernel copy directly.
  403. */
  404. scb_index = ahc_inb(ahc, SCB_TAG);
  405. scb = ahc_lookup_scb(ahc, scb_index);
  406. if (scb == NULL) {
  407. ahc_print_devinfo(ahc, &devinfo);
  408. printf("ahc_intr - referenced scb "
  409. "not valid during seqint 0x%x scb(%d)\n",
  410. intstat, scb_index);
  411. ahc_dump_card_state(ahc);
  412. panic("for safety");
  413. goto unpause;
  414. }
  415. hscb = scb->hscb;
  416. /* Don't want to clobber the original sense code */
  417. if ((scb->flags & SCB_SENSE) != 0) {
  418. /*
  419. * Clear the SCB_SENSE Flag and have
  420. * the sequencer do a normal command
  421. * complete.
  422. */
  423. scb->flags &= ~SCB_SENSE;
  424. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  425. break;
  426. }
  427. ahc_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  428. /* Freeze the queue until the client sees the error. */
  429. ahc_freeze_devq(ahc, scb);
  430. ahc_freeze_scb(scb);
  431. ahc_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
  432. switch (hscb->shared_data.status.scsi_status) {
  433. case SCSI_STATUS_OK:
  434. printf("%s: Interrupted for staus of 0???\n",
  435. ahc_name(ahc));
  436. break;
  437. case SCSI_STATUS_CMD_TERMINATED:
  438. case SCSI_STATUS_CHECK_COND:
  439. {
  440. struct ahc_dma_seg *sg;
  441. struct scsi_sense *sc;
  442. struct ahc_initiator_tinfo *targ_info;
  443. struct ahc_tmode_tstate *tstate;
  444. struct ahc_transinfo *tinfo;
  445. #ifdef AHC_DEBUG
  446. if (ahc_debug & AHC_SHOW_SENSE) {
  447. ahc_print_path(ahc, scb);
  448. printf("SCB %d: requests Check Status\n",
  449. scb->hscb->tag);
  450. }
  451. #endif
  452. if (ahc_perform_autosense(scb) == 0)
  453. break;
  454. targ_info = ahc_fetch_transinfo(ahc,
  455. devinfo.channel,
  456. devinfo.our_scsiid,
  457. devinfo.target,
  458. &tstate);
  459. tinfo = &targ_info->curr;
  460. sg = scb->sg_list;
  461. sc = (struct scsi_sense *)(&hscb->shared_data.cdb);
  462. /*
  463. * Save off the residual if there is one.
  464. */
  465. ahc_update_residual(ahc, scb);
  466. #ifdef AHC_DEBUG
  467. if (ahc_debug & AHC_SHOW_SENSE) {
  468. ahc_print_path(ahc, scb);
  469. printf("Sending Sense\n");
  470. }
  471. #endif
  472. sg->addr = ahc_get_sense_bufaddr(ahc, scb);
  473. sg->len = ahc_get_sense_bufsize(ahc, scb);
  474. sg->len |= AHC_DMA_LAST_SEG;
  475. /* Fixup byte order */
  476. sg->addr = ahc_htole32(sg->addr);
  477. sg->len = ahc_htole32(sg->len);
  478. sc->opcode = REQUEST_SENSE;
  479. sc->byte2 = 0;
  480. if (tinfo->protocol_version <= SCSI_REV_2
  481. && SCB_GET_LUN(scb) < 8)
  482. sc->byte2 = SCB_GET_LUN(scb) << 5;
  483. sc->unused[0] = 0;
  484. sc->unused[1] = 0;
  485. sc->length = sg->len;
  486. sc->control = 0;
  487. /*
  488. * We can't allow the target to disconnect.
  489. * This will be an untagged transaction and
  490. * having the target disconnect will make this
  491. * transaction indestinguishable from outstanding
  492. * tagged transactions.
  493. */
  494. hscb->control = 0;
  495. /*
  496. * This request sense could be because the
  497. * the device lost power or in some other
  498. * way has lost our transfer negotiations.
  499. * Renegotiate if appropriate. Unit attention
  500. * errors will be reported before any data
  501. * phases occur.
  502. */
  503. if (ahc_get_residual(scb)
  504. == ahc_get_transfer_length(scb)) {
  505. ahc_update_neg_request(ahc, &devinfo,
  506. tstate, targ_info,
  507. AHC_NEG_IF_NON_ASYNC);
  508. }
  509. if (tstate->auto_negotiate & devinfo.target_mask) {
  510. hscb->control |= MK_MESSAGE;
  511. scb->flags &= ~SCB_NEGOTIATE;
  512. scb->flags |= SCB_AUTO_NEGOTIATE;
  513. }
  514. hscb->cdb_len = sizeof(*sc);
  515. hscb->dataptr = sg->addr;
  516. hscb->datacnt = sg->len;
  517. hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
  518. hscb->sgptr = ahc_htole32(hscb->sgptr);
  519. scb->sg_count = 1;
  520. scb->flags |= SCB_SENSE;
  521. ahc_qinfifo_requeue_tail(ahc, scb);
  522. ahc_outb(ahc, RETURN_1, SEND_SENSE);
  523. /*
  524. * Ensure we have enough time to actually
  525. * retrieve the sense.
  526. */
  527. ahc_scb_timer_reset(scb, 5 * 1000000);
  528. break;
  529. }
  530. default:
  531. break;
  532. }
  533. break;
  534. }
  535. case NO_MATCH:
  536. {
  537. /* Ensure we don't leave the selection hardware on */
  538. ahc_outb(ahc, SCSISEQ,
  539. ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  540. printf("%s:%c:%d: no active SCB for reconnecting "
  541. "target - issuing BUS DEVICE RESET\n",
  542. ahc_name(ahc), devinfo.channel, devinfo.target);
  543. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  544. "ARG_1 == 0x%x ACCUM = 0x%x\n",
  545. ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  546. ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  547. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  548. "SINDEX == 0x%x\n",
  549. ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  550. ahc_index_busy_tcl(ahc,
  551. BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  552. ahc_inb(ahc, SAVED_LUN))),
  553. ahc_inb(ahc, SINDEX));
  554. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  555. "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
  556. ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  557. ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  558. ahc_inb(ahc, SCB_CONTROL));
  559. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
  560. ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  561. printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0));
  562. printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL));
  563. ahc_dump_card_state(ahc);
  564. ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
  565. ahc->msgout_len = 1;
  566. ahc->msgout_index = 0;
  567. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  568. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  569. ahc_assert_atn(ahc);
  570. break;
  571. }
  572. case SEND_REJECT:
  573. {
  574. u_int rejbyte = ahc_inb(ahc, ACCUM);
  575. printf("%s:%c:%d: Warning - unknown message received from "
  576. "target (0x%x). Rejecting\n",
  577. ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
  578. break;
  579. }
  580. case PROTO_VIOLATION:
  581. {
  582. ahc_handle_proto_violation(ahc);
  583. break;
  584. }
  585. case IGN_WIDE_RES:
  586. ahc_handle_ign_wide_residue(ahc, &devinfo);
  587. break;
  588. case PDATA_REINIT:
  589. ahc_reinitialize_dataptrs(ahc);
  590. break;
  591. case BAD_PHASE:
  592. {
  593. u_int lastphase;
  594. lastphase = ahc_inb(ahc, LASTPHASE);
  595. printf("%s:%c:%d: unknown scsi bus phase %x, "
  596. "lastphase = 0x%x. Attempting to continue\n",
  597. ahc_name(ahc), devinfo.channel, devinfo.target,
  598. lastphase, ahc_inb(ahc, SCSISIGI));
  599. break;
  600. }
  601. case MISSED_BUSFREE:
  602. {
  603. u_int lastphase;
  604. lastphase = ahc_inb(ahc, LASTPHASE);
  605. printf("%s:%c:%d: Missed busfree. "
  606. "Lastphase = 0x%x, Curphase = 0x%x\n",
  607. ahc_name(ahc), devinfo.channel, devinfo.target,
  608. lastphase, ahc_inb(ahc, SCSISIGI));
  609. ahc_restart(ahc);
  610. return;
  611. }
  612. case HOST_MSG_LOOP:
  613. {
  614. /*
  615. * The sequencer has encountered a message phase
  616. * that requires host assistance for completion.
  617. * While handling the message phase(s), we will be
  618. * notified by the sequencer after each byte is
  619. * transfered so we can track bus phase changes.
  620. *
  621. * If this is the first time we've seen a HOST_MSG_LOOP
  622. * interrupt, initialize the state of the host message
  623. * loop.
  624. */
  625. if (ahc->msg_type == MSG_TYPE_NONE) {
  626. struct scb *scb;
  627. u_int scb_index;
  628. u_int bus_phase;
  629. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  630. if (bus_phase != P_MESGIN
  631. && bus_phase != P_MESGOUT) {
  632. printf("ahc_intr: HOST_MSG_LOOP bad "
  633. "phase 0x%x\n",
  634. bus_phase);
  635. /*
  636. * Probably transitioned to bus free before
  637. * we got here. Just punt the message.
  638. */
  639. ahc_clear_intstat(ahc);
  640. ahc_restart(ahc);
  641. return;
  642. }
  643. scb_index = ahc_inb(ahc, SCB_TAG);
  644. scb = ahc_lookup_scb(ahc, scb_index);
  645. if (devinfo.role == ROLE_INITIATOR) {
  646. if (scb == NULL)
  647. panic("HOST_MSG_LOOP with "
  648. "invalid SCB %x\n", scb_index);
  649. if (bus_phase == P_MESGOUT)
  650. ahc_setup_initiator_msgout(ahc,
  651. &devinfo,
  652. scb);
  653. else {
  654. ahc->msg_type =
  655. MSG_TYPE_INITIATOR_MSGIN;
  656. ahc->msgin_index = 0;
  657. }
  658. }
  659. #ifdef AHC_TARGET_MODE
  660. else {
  661. if (bus_phase == P_MESGOUT) {
  662. ahc->msg_type =
  663. MSG_TYPE_TARGET_MSGOUT;
  664. ahc->msgin_index = 0;
  665. }
  666. else
  667. ahc_setup_target_msgin(ahc,
  668. &devinfo,
  669. scb);
  670. }
  671. #endif
  672. }
  673. ahc_handle_message_phase(ahc);
  674. break;
  675. }
  676. case PERR_DETECTED:
  677. {
  678. /*
  679. * If we've cleared the parity error interrupt
  680. * but the sequencer still believes that SCSIPERR
  681. * is true, it must be that the parity error is
  682. * for the currently presented byte on the bus,
  683. * and we are not in a phase (data-in) where we will
  684. * eventually ack this byte. Ack the byte and
  685. * throw it away in the hope that the target will
  686. * take us to message out to deliver the appropriate
  687. * error message.
  688. */
  689. if ((intstat & SCSIINT) == 0
  690. && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
  691. if ((ahc->features & AHC_DT) == 0) {
  692. u_int curphase;
  693. /*
  694. * The hardware will only let you ack bytes
  695. * if the expected phase in SCSISIGO matches
  696. * the current phase. Make sure this is
  697. * currently the case.
  698. */
  699. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  700. ahc_outb(ahc, LASTPHASE, curphase);
  701. ahc_outb(ahc, SCSISIGO, curphase);
  702. }
  703. if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) {
  704. int wait;
  705. /*
  706. * In a data phase. Faster to bitbucket
  707. * the data than to individually ack each
  708. * byte. This is also the only strategy
  709. * that will work with AUTOACK enabled.
  710. */
  711. ahc_outb(ahc, SXFRCTL1,
  712. ahc_inb(ahc, SXFRCTL1) | BITBUCKET);
  713. wait = 5000;
  714. while (--wait != 0) {
  715. if ((ahc_inb(ahc, SCSISIGI)
  716. & (CDI|MSGI)) != 0)
  717. break;
  718. ahc_delay(100);
  719. }
  720. ahc_outb(ahc, SXFRCTL1,
  721. ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
  722. if (wait == 0) {
  723. struct scb *scb;
  724. u_int scb_index;
  725. ahc_print_devinfo(ahc, &devinfo);
  726. printf("Unable to clear parity error. "
  727. "Resetting bus.\n");
  728. scb_index = ahc_inb(ahc, SCB_TAG);
  729. scb = ahc_lookup_scb(ahc, scb_index);
  730. if (scb != NULL)
  731. ahc_set_transaction_status(scb,
  732. CAM_UNCOR_PARITY);
  733. ahc_reset_channel(ahc, devinfo.channel,
  734. /*init reset*/TRUE);
  735. }
  736. } else {
  737. ahc_inb(ahc, SCSIDATL);
  738. }
  739. }
  740. break;
  741. }
  742. case DATA_OVERRUN:
  743. {
  744. /*
  745. * When the sequencer detects an overrun, it
  746. * places the controller in "BITBUCKET" mode
  747. * and allows the target to complete its transfer.
  748. * Unfortunately, none of the counters get updated
  749. * when the controller is in this mode, so we have
  750. * no way of knowing how large the overrun was.
  751. */
  752. u_int scbindex = ahc_inb(ahc, SCB_TAG);
  753. u_int lastphase = ahc_inb(ahc, LASTPHASE);
  754. u_int i;
  755. scb = ahc_lookup_scb(ahc, scbindex);
  756. for (i = 0; i < num_phases; i++) {
  757. if (lastphase == ahc_phase_table[i].phase)
  758. break;
  759. }
  760. ahc_print_path(ahc, scb);
  761. printf("data overrun detected %s."
  762. " Tag == 0x%x.\n",
  763. ahc_phase_table[i].phasemsg,
  764. scb->hscb->tag);
  765. ahc_print_path(ahc, scb);
  766. printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
  767. ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
  768. ahc_get_transfer_length(scb), scb->sg_count);
  769. if (scb->sg_count > 0) {
  770. for (i = 0; i < scb->sg_count; i++) {
  771. printf("sg[%d] - Addr 0x%x%x : Length %d\n",
  772. i,
  773. (ahc_le32toh(scb->sg_list[i].len) >> 24
  774. & SG_HIGH_ADDR_BITS),
  775. ahc_le32toh(scb->sg_list[i].addr),
  776. ahc_le32toh(scb->sg_list[i].len)
  777. & AHC_SG_LEN_MASK);
  778. }
  779. }
  780. /*
  781. * Set this and it will take effect when the
  782. * target does a command complete.
  783. */
  784. ahc_freeze_devq(ahc, scb);
  785. if ((scb->flags & SCB_SENSE) == 0) {
  786. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  787. } else {
  788. scb->flags &= ~SCB_SENSE;
  789. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  790. }
  791. ahc_freeze_scb(scb);
  792. if ((ahc->features & AHC_ULTRA2) != 0) {
  793. /*
  794. * Clear the channel in case we return
  795. * to data phase later.
  796. */
  797. ahc_outb(ahc, SXFRCTL0,
  798. ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  799. ahc_outb(ahc, SXFRCTL0,
  800. ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  801. }
  802. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  803. u_int dscommand1;
  804. /* Ensure HHADDR is 0 for future DMA operations. */
  805. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  806. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  807. ahc_outb(ahc, HADDR, 0);
  808. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  809. }
  810. break;
  811. }
  812. case MKMSG_FAILED:
  813. {
  814. u_int scbindex;
  815. printf("%s:%c:%d:%d: Attempt to issue message failed\n",
  816. ahc_name(ahc), devinfo.channel, devinfo.target,
  817. devinfo.lun);
  818. scbindex = ahc_inb(ahc, SCB_TAG);
  819. scb = ahc_lookup_scb(ahc, scbindex);
  820. if (scb != NULL
  821. && (scb->flags & SCB_RECOVERY_SCB) != 0)
  822. /*
  823. * Ensure that we didn't put a second instance of this
  824. * SCB into the QINFIFO.
  825. */
  826. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  827. SCB_GET_CHANNEL(ahc, scb),
  828. SCB_GET_LUN(scb), scb->hscb->tag,
  829. ROLE_INITIATOR, /*status*/0,
  830. SEARCH_REMOVE);
  831. break;
  832. }
  833. case NO_FREE_SCB:
  834. {
  835. printf("%s: No free or disconnected SCBs\n", ahc_name(ahc));
  836. ahc_dump_card_state(ahc);
  837. panic("for safety");
  838. break;
  839. }
  840. case SCB_MISMATCH:
  841. {
  842. u_int scbptr;
  843. scbptr = ahc_inb(ahc, SCBPTR);
  844. printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
  845. scbptr, ahc_inb(ahc, ARG_1),
  846. ahc->scb_data->hscbs[scbptr].tag);
  847. ahc_dump_card_state(ahc);
  848. panic("for saftey");
  849. break;
  850. }
  851. case OUT_OF_RANGE:
  852. {
  853. printf("%s: BTT calculation out of range\n", ahc_name(ahc));
  854. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  855. "ARG_1 == 0x%x ACCUM = 0x%x\n",
  856. ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  857. ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  858. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  859. "SINDEX == 0x%x\n, A == 0x%x\n",
  860. ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  861. ahc_index_busy_tcl(ahc,
  862. BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  863. ahc_inb(ahc, SAVED_LUN))),
  864. ahc_inb(ahc, SINDEX),
  865. ahc_inb(ahc, ACCUM));
  866. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  867. "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
  868. ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  869. ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  870. ahc_inb(ahc, SCB_CONTROL));
  871. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
  872. ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  873. ahc_dump_card_state(ahc);
  874. panic("for safety");
  875. break;
  876. }
  877. default:
  878. printf("ahc_intr: seqint, "
  879. "intstat == 0x%x, scsisigi = 0x%x\n",
  880. intstat, ahc_inb(ahc, SCSISIGI));
  881. break;
  882. }
  883. unpause:
  884. /*
  885. * The sequencer is paused immediately on
  886. * a SEQINT, so we should restart it when
  887. * we're done.
  888. */
  889. ahc_unpause(ahc);
  890. }
  891. void
  892. ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
  893. {
  894. u_int scb_index;
  895. u_int status0;
  896. u_int status;
  897. struct scb *scb;
  898. char cur_channel;
  899. char intr_channel;
  900. if ((ahc->features & AHC_TWIN) != 0
  901. && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
  902. cur_channel = 'B';
  903. else
  904. cur_channel = 'A';
  905. intr_channel = cur_channel;
  906. if ((ahc->features & AHC_ULTRA2) != 0)
  907. status0 = ahc_inb(ahc, SSTAT0) & IOERR;
  908. else
  909. status0 = 0;
  910. status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  911. if (status == 0 && status0 == 0) {
  912. if ((ahc->features & AHC_TWIN) != 0) {
  913. /* Try the other channel */
  914. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  915. status = ahc_inb(ahc, SSTAT1)
  916. & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  917. intr_channel = (cur_channel == 'A') ? 'B' : 'A';
  918. }
  919. if (status == 0) {
  920. printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
  921. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  922. ahc_unpause(ahc);
  923. return;
  924. }
  925. }
  926. /* Make sure the sequencer is in a safe location. */
  927. ahc_clear_critical_section(ahc);
  928. scb_index = ahc_inb(ahc, SCB_TAG);
  929. scb = ahc_lookup_scb(ahc, scb_index);
  930. if (scb != NULL
  931. && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  932. scb = NULL;
  933. if ((ahc->features & AHC_ULTRA2) != 0
  934. && (status0 & IOERR) != 0) {
  935. int now_lvd;
  936. now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
  937. printf("%s: Transceiver State Has Changed to %s mode\n",
  938. ahc_name(ahc), now_lvd ? "LVD" : "SE");
  939. ahc_outb(ahc, CLRSINT0, CLRIOERR);
  940. /*
  941. * When transitioning to SE mode, the reset line
  942. * glitches, triggering an arbitration bug in some
  943. * Ultra2 controllers. This bug is cleared when we
  944. * assert the reset line. Since a reset glitch has
  945. * already occurred with this transition and a
  946. * transceiver state change is handled just like
  947. * a bus reset anyway, asserting the reset line
  948. * ourselves is safe.
  949. */
  950. ahc_reset_channel(ahc, intr_channel,
  951. /*Initiate Reset*/now_lvd == 0);
  952. } else if ((status & SCSIRSTI) != 0) {
  953. printf("%s: Someone reset channel %c\n",
  954. ahc_name(ahc), intr_channel);
  955. if (intr_channel != cur_channel)
  956. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  957. ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
  958. } else if ((status & SCSIPERR) != 0) {
  959. /*
  960. * Determine the bus phase and queue an appropriate message.
  961. * SCSIPERR is latched true as soon as a parity error
  962. * occurs. If the sequencer acked the transfer that
  963. * caused the parity error and the currently presented
  964. * transfer on the bus has correct parity, SCSIPERR will
  965. * be cleared by CLRSCSIPERR. Use this to determine if
  966. * we should look at the last phase the sequencer recorded,
  967. * or the current phase presented on the bus.
  968. */
  969. struct ahc_devinfo devinfo;
  970. u_int mesg_out;
  971. u_int curphase;
  972. u_int errorphase;
  973. u_int lastphase;
  974. u_int scsirate;
  975. u_int i;
  976. u_int sstat2;
  977. int silent;
  978. lastphase = ahc_inb(ahc, LASTPHASE);
  979. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  980. sstat2 = ahc_inb(ahc, SSTAT2);
  981. ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
  982. /*
  983. * For all phases save DATA, the sequencer won't
  984. * automatically ack a byte that has a parity error
  985. * in it. So the only way that the current phase
  986. * could be 'data-in' is if the parity error is for
  987. * an already acked byte in the data phase. During
  988. * synchronous data-in transfers, we may actually
  989. * ack bytes before latching the current phase in
  990. * LASTPHASE, leading to the discrepancy between
  991. * curphase and lastphase.
  992. */
  993. if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
  994. || curphase == P_DATAIN || curphase == P_DATAIN_DT)
  995. errorphase = curphase;
  996. else
  997. errorphase = lastphase;
  998. for (i = 0; i < num_phases; i++) {
  999. if (errorphase == ahc_phase_table[i].phase)
  1000. break;
  1001. }
  1002. mesg_out = ahc_phase_table[i].mesg_out;
  1003. silent = FALSE;
  1004. if (scb != NULL) {
  1005. if (SCB_IS_SILENT(scb))
  1006. silent = TRUE;
  1007. else
  1008. ahc_print_path(ahc, scb);
  1009. scb->flags |= SCB_TRANSMISSION_ERROR;
  1010. } else
  1011. printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
  1012. SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
  1013. scsirate = ahc_inb(ahc, SCSIRATE);
  1014. if (silent == FALSE) {
  1015. printf("parity error detected %s. "
  1016. "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
  1017. ahc_phase_table[i].phasemsg,
  1018. ahc_inw(ahc, SEQADDR0),
  1019. scsirate);
  1020. if ((ahc->features & AHC_DT) != 0) {
  1021. if ((sstat2 & CRCVALERR) != 0)
  1022. printf("\tCRC Value Mismatch\n");
  1023. if ((sstat2 & CRCENDERR) != 0)
  1024. printf("\tNo terminal CRC packet "
  1025. "recevied\n");
  1026. if ((sstat2 & CRCREQERR) != 0)
  1027. printf("\tIllegal CRC packet "
  1028. "request\n");
  1029. if ((sstat2 & DUAL_EDGE_ERR) != 0)
  1030. printf("\tUnexpected %sDT Data Phase\n",
  1031. (scsirate & SINGLE_EDGE)
  1032. ? "" : "non-");
  1033. }
  1034. }
  1035. if ((ahc->features & AHC_DT) != 0
  1036. && (sstat2 & DUAL_EDGE_ERR) != 0) {
  1037. /*
  1038. * This error applies regardless of
  1039. * data direction, so ignore the value
  1040. * in the phase table.
  1041. */
  1042. mesg_out = MSG_INITIATOR_DET_ERR;
  1043. }
  1044. /*
  1045. * We've set the hardware to assert ATN if we
  1046. * get a parity error on "in" phases, so all we
  1047. * need to do is stuff the message buffer with
  1048. * the appropriate message. "In" phases have set
  1049. * mesg_out to something other than MSG_NOP.
  1050. */
  1051. if (mesg_out != MSG_NOOP) {
  1052. if (ahc->msg_type != MSG_TYPE_NONE)
  1053. ahc->send_msg_perror = TRUE;
  1054. else
  1055. ahc_outb(ahc, MSG_OUT, mesg_out);
  1056. }
  1057. /*
  1058. * Force a renegotiation with this target just in
  1059. * case we are out of sync for some external reason
  1060. * unknown (or unreported) by the target.
  1061. */
  1062. ahc_fetch_devinfo(ahc, &devinfo);
  1063. ahc_force_renegotiation(ahc, &devinfo);
  1064. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1065. ahc_unpause(ahc);
  1066. } else if ((status & SELTO) != 0) {
  1067. u_int scbptr;
  1068. /* Stop the selection */
  1069. ahc_outb(ahc, SCSISEQ, 0);
  1070. /* No more pending messages */
  1071. ahc_clear_msg_state(ahc);
  1072. /* Clear interrupt state */
  1073. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1074. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1075. /*
  1076. * Although the driver does not care about the
  1077. * 'Selection in Progress' status bit, the busy
  1078. * LED does. SELINGO is only cleared by a sucessfull
  1079. * selection, so we must manually clear it to insure
  1080. * the LED turns off just incase no future successful
  1081. * selections occur (e.g. no devices on the bus).
  1082. */
  1083. ahc_outb(ahc, CLRSINT0, CLRSELINGO);
  1084. scbptr = ahc_inb(ahc, WAITING_SCBH);
  1085. ahc_outb(ahc, SCBPTR, scbptr);
  1086. scb_index = ahc_inb(ahc, SCB_TAG);
  1087. scb = ahc_lookup_scb(ahc, scb_index);
  1088. if (scb == NULL) {
  1089. printf("%s: ahc_intr - referenced scb not "
  1090. "valid during SELTO scb(%d, %d)\n",
  1091. ahc_name(ahc), scbptr, scb_index);
  1092. ahc_dump_card_state(ahc);
  1093. } else {
  1094. struct ahc_devinfo devinfo;
  1095. #ifdef AHC_DEBUG
  1096. if ((ahc_debug & AHC_SHOW_SELTO) != 0) {
  1097. ahc_print_path(ahc, scb);
  1098. printf("Saw Selection Timeout for SCB 0x%x\n",
  1099. scb_index);
  1100. }
  1101. #endif
  1102. ahc_scb_devinfo(ahc, &devinfo, scb);
  1103. ahc_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1104. ahc_freeze_devq(ahc, scb);
  1105. /*
  1106. * Cancel any pending transactions on the device
  1107. * now that it seems to be missing. This will
  1108. * also revert us to async/narrow transfers until
  1109. * we can renegotiate with the device.
  1110. */
  1111. ahc_handle_devreset(ahc, &devinfo,
  1112. CAM_SEL_TIMEOUT,
  1113. "Selection Timeout",
  1114. /*verbose_level*/1);
  1115. }
  1116. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1117. ahc_restart(ahc);
  1118. } else if ((status & BUSFREE) != 0
  1119. && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
  1120. struct ahc_devinfo devinfo;
  1121. u_int lastphase;
  1122. u_int saved_scsiid;
  1123. u_int saved_lun;
  1124. u_int target;
  1125. u_int initiator_role_id;
  1126. char channel;
  1127. int printerror;
  1128. /*
  1129. * Clear our selection hardware as soon as possible.
  1130. * We may have an entry in the waiting Q for this target,
  1131. * that is affected by this busfree and we don't want to
  1132. * go about selecting the target while we handle the event.
  1133. */
  1134. ahc_outb(ahc, SCSISEQ,
  1135. ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  1136. /*
  1137. * Disable busfree interrupts and clear the busfree
  1138. * interrupt status. We do this here so that several
  1139. * bus transactions occur prior to clearing the SCSIINT
  1140. * latch. It can take a bit for the clearing to take effect.
  1141. */
  1142. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1143. ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
  1144. /*
  1145. * Look at what phase we were last in.
  1146. * If its message out, chances are pretty good
  1147. * that the busfree was in response to one of
  1148. * our abort requests.
  1149. */
  1150. lastphase = ahc_inb(ahc, LASTPHASE);
  1151. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  1152. saved_lun = ahc_inb(ahc, SAVED_LUN);
  1153. target = SCSIID_TARGET(ahc, saved_scsiid);
  1154. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  1155. channel = SCSIID_CHANNEL(ahc, saved_scsiid);
  1156. ahc_compile_devinfo(&devinfo, initiator_role_id,
  1157. target, saved_lun, channel, ROLE_INITIATOR);
  1158. printerror = 1;
  1159. if (lastphase == P_MESGOUT) {
  1160. u_int tag;
  1161. tag = SCB_LIST_NULL;
  1162. if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
  1163. || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
  1164. if (ahc->msgout_buf[ahc->msgout_index - 1]
  1165. == MSG_ABORT_TAG)
  1166. tag = scb->hscb->tag;
  1167. ahc_print_path(ahc, scb);
  1168. printf("SCB %d - Abort%s Completed.\n",
  1169. scb->hscb->tag, tag == SCB_LIST_NULL ?
  1170. "" : " Tag");
  1171. ahc_abort_scbs(ahc, target, channel,
  1172. saved_lun, tag,
  1173. ROLE_INITIATOR,
  1174. CAM_REQ_ABORTED);
  1175. printerror = 0;
  1176. } else if (ahc_sent_msg(ahc, AHCMSG_1B,
  1177. MSG_BUS_DEV_RESET, TRUE)) {
  1178. #ifdef __FreeBSD__
  1179. /*
  1180. * Don't mark the user's request for this BDR
  1181. * as completing with CAM_BDR_SENT. CAM3
  1182. * specifies CAM_REQ_CMP.
  1183. */
  1184. if (scb != NULL
  1185. && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  1186. && ahc_match_scb(ahc, scb, target, channel,
  1187. CAM_LUN_WILDCARD,
  1188. SCB_LIST_NULL,
  1189. ROLE_INITIATOR)) {
  1190. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1191. }
  1192. #endif
  1193. ahc_compile_devinfo(&devinfo,
  1194. initiator_role_id,
  1195. target,
  1196. CAM_LUN_WILDCARD,
  1197. channel,
  1198. ROLE_INITIATOR);
  1199. ahc_handle_devreset(ahc, &devinfo,
  1200. CAM_BDR_SENT,
  1201. "Bus Device Reset",
  1202. /*verbose_level*/0);
  1203. printerror = 0;
  1204. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1205. MSG_EXT_PPR, FALSE)) {
  1206. struct ahc_initiator_tinfo *tinfo;
  1207. struct ahc_tmode_tstate *tstate;
  1208. /*
  1209. * PPR Rejected. Try non-ppr negotiation
  1210. * and retry command.
  1211. */
  1212. tinfo = ahc_fetch_transinfo(ahc,
  1213. devinfo.channel,
  1214. devinfo.our_scsiid,
  1215. devinfo.target,
  1216. &tstate);
  1217. tinfo->curr.transport_version = 2;
  1218. tinfo->goal.transport_version = 2;
  1219. tinfo->goal.ppr_options = 0;
  1220. ahc_qinfifo_requeue_tail(ahc, scb);
  1221. printerror = 0;
  1222. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1223. MSG_EXT_WDTR, FALSE)) {
  1224. /*
  1225. * Negotiation Rejected. Go-narrow and
  1226. * retry command.
  1227. */
  1228. ahc_set_width(ahc, &devinfo,
  1229. MSG_EXT_WDTR_BUS_8_BIT,
  1230. AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1231. /*paused*/TRUE);
  1232. ahc_qinfifo_requeue_tail(ahc, scb);
  1233. printerror = 0;
  1234. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1235. MSG_EXT_SDTR, FALSE)) {
  1236. /*
  1237. * Negotiation Rejected. Go-async and
  1238. * retry command.
  1239. */
  1240. ahc_set_syncrate(ahc, &devinfo,
  1241. /*syncrate*/NULL,
  1242. /*period*/0, /*offset*/0,
  1243. /*ppr_options*/0,
  1244. AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1245. /*paused*/TRUE);
  1246. ahc_qinfifo_requeue_tail(ahc, scb);
  1247. printerror = 0;
  1248. }
  1249. }
  1250. if (printerror != 0) {
  1251. u_int i;
  1252. if (scb != NULL) {
  1253. u_int tag;
  1254. if ((scb->hscb->control & TAG_ENB) != 0)
  1255. tag = scb->hscb->tag;
  1256. else
  1257. tag = SCB_LIST_NULL;
  1258. ahc_print_path(ahc, scb);
  1259. ahc_abort_scbs(ahc, target, channel,
  1260. SCB_GET_LUN(scb), tag,
  1261. ROLE_INITIATOR,
  1262. CAM_UNEXP_BUSFREE);
  1263. } else {
  1264. /*
  1265. * We had not fully identified this connection,
  1266. * so we cannot abort anything.
  1267. */
  1268. printf("%s: ", ahc_name(ahc));
  1269. }
  1270. for (i = 0; i < num_phases; i++) {
  1271. if (lastphase == ahc_phase_table[i].phase)
  1272. break;
  1273. }
  1274. if (lastphase != P_BUSFREE) {
  1275. /*
  1276. * Renegotiate with this device at the
  1277. * next oportunity just in case this busfree
  1278. * is due to a negotiation mismatch with the
  1279. * device.
  1280. */
  1281. ahc_force_renegotiation(ahc, &devinfo);
  1282. }
  1283. printf("Unexpected busfree %s\n"
  1284. "SEQADDR == 0x%x\n",
  1285. ahc_phase_table[i].phasemsg,
  1286. ahc_inb(ahc, SEQADDR0)
  1287. | (ahc_inb(ahc, SEQADDR1) << 8));
  1288. }
  1289. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1290. ahc_restart(ahc);
  1291. } else {
  1292. printf("%s: Missing case in ahc_handle_scsiint. status = %x\n",
  1293. ahc_name(ahc), status);
  1294. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1295. }
  1296. }
  1297. /*
  1298. * Force renegotiation to occur the next time we initiate
  1299. * a command to the current device.
  1300. */
  1301. static void
  1302. ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1303. {
  1304. struct ahc_initiator_tinfo *targ_info;
  1305. struct ahc_tmode_tstate *tstate;
  1306. targ_info = ahc_fetch_transinfo(ahc,
  1307. devinfo->channel,
  1308. devinfo->our_scsiid,
  1309. devinfo->target,
  1310. &tstate);
  1311. ahc_update_neg_request(ahc, devinfo, tstate,
  1312. targ_info, AHC_NEG_IF_NON_ASYNC);
  1313. }
  1314. #define AHC_MAX_STEPS 2000
  1315. void
  1316. ahc_clear_critical_section(struct ahc_softc *ahc)
  1317. {
  1318. int stepping;
  1319. int steps;
  1320. u_int simode0;
  1321. u_int simode1;
  1322. if (ahc->num_critical_sections == 0)
  1323. return;
  1324. stepping = FALSE;
  1325. steps = 0;
  1326. simode0 = 0;
  1327. simode1 = 0;
  1328. for (;;) {
  1329. struct cs *cs;
  1330. u_int seqaddr;
  1331. u_int i;
  1332. seqaddr = ahc_inb(ahc, SEQADDR0)
  1333. | (ahc_inb(ahc, SEQADDR1) << 8);
  1334. /*
  1335. * Seqaddr represents the next instruction to execute,
  1336. * so we are really executing the instruction just
  1337. * before it.
  1338. */
  1339. if (seqaddr != 0)
  1340. seqaddr -= 1;
  1341. cs = ahc->critical_sections;
  1342. for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
  1343. if (cs->begin < seqaddr && cs->end >= seqaddr)
  1344. break;
  1345. }
  1346. if (i == ahc->num_critical_sections)
  1347. break;
  1348. if (steps > AHC_MAX_STEPS) {
  1349. printf("%s: Infinite loop in critical section\n",
  1350. ahc_name(ahc));
  1351. ahc_dump_card_state(ahc);
  1352. panic("critical section loop");
  1353. }
  1354. steps++;
  1355. if (stepping == FALSE) {
  1356. /*
  1357. * Disable all interrupt sources so that the
  1358. * sequencer will not be stuck by a pausing
  1359. * interrupt condition while we attempt to
  1360. * leave a critical section.
  1361. */
  1362. simode0 = ahc_inb(ahc, SIMODE0);
  1363. ahc_outb(ahc, SIMODE0, 0);
  1364. simode1 = ahc_inb(ahc, SIMODE1);
  1365. if ((ahc->features & AHC_DT) != 0)
  1366. /*
  1367. * On DT class controllers, we
  1368. * use the enhanced busfree logic.
  1369. * Unfortunately we cannot re-enable
  1370. * busfree detection within the
  1371. * current connection, so we must
  1372. * leave it on while single stepping.
  1373. */
  1374. ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE);
  1375. else
  1376. ahc_outb(ahc, SIMODE1, 0);
  1377. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1378. ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP);
  1379. stepping = TRUE;
  1380. }
  1381. if ((ahc->features & AHC_DT) != 0) {
  1382. ahc_outb(ahc, CLRSINT1, CLRBUSFREE);
  1383. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1384. }
  1385. ahc_outb(ahc, HCNTRL, ahc->unpause);
  1386. while (!ahc_is_paused(ahc))
  1387. ahc_delay(200);
  1388. }
  1389. if (stepping) {
  1390. ahc_outb(ahc, SIMODE0, simode0);
  1391. ahc_outb(ahc, SIMODE1, simode1);
  1392. ahc_outb(ahc, SEQCTL, ahc->seqctl);
  1393. }
  1394. }
  1395. /*
  1396. * Clear any pending interrupt status.
  1397. */
  1398. void
  1399. ahc_clear_intstat(struct ahc_softc *ahc)
  1400. {
  1401. /* Clear any interrupt conditions this may have caused */
  1402. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  1403. |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
  1404. CLRREQINIT);
  1405. ahc_flush_device_writes(ahc);
  1406. ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
  1407. ahc_flush_device_writes(ahc);
  1408. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1409. ahc_flush_device_writes(ahc);
  1410. }
  1411. /**************************** Debugging Routines ******************************/
  1412. #ifdef AHC_DEBUG
  1413. uint32_t ahc_debug = AHC_DEBUG_OPTS;
  1414. #endif
  1415. void
  1416. ahc_print_scb(struct scb *scb)
  1417. {
  1418. int i;
  1419. struct hardware_scb *hscb = scb->hscb;
  1420. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
  1421. (void *)scb,
  1422. hscb->control,
  1423. hscb->scsiid,
  1424. hscb->lun,
  1425. hscb->cdb_len);
  1426. printf("Shared Data: ");
  1427. for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
  1428. printf("%#02x", hscb->shared_data.cdb[i]);
  1429. printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
  1430. ahc_le32toh(hscb->dataptr),
  1431. ahc_le32toh(hscb->datacnt),
  1432. ahc_le32toh(hscb->sgptr),
  1433. hscb->tag);
  1434. if (scb->sg_count > 0) {
  1435. for (i = 0; i < scb->sg_count; i++) {
  1436. printf("sg[%d] - Addr 0x%x%x : Length %d\n",
  1437. i,
  1438. (ahc_le32toh(scb->sg_list[i].len) >> 24
  1439. & SG_HIGH_ADDR_BITS),
  1440. ahc_le32toh(scb->sg_list[i].addr),
  1441. ahc_le32toh(scb->sg_list[i].len));
  1442. }
  1443. }
  1444. }
  1445. /************************* Transfer Negotiation *******************************/
  1446. /*
  1447. * Allocate per target mode instance (ID we respond to as a target)
  1448. * transfer negotiation data structures.
  1449. */
  1450. static struct ahc_tmode_tstate *
  1451. ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
  1452. {
  1453. struct ahc_tmode_tstate *master_tstate;
  1454. struct ahc_tmode_tstate *tstate;
  1455. int i;
  1456. master_tstate = ahc->enabled_targets[ahc->our_id];
  1457. if (channel == 'B') {
  1458. scsi_id += 8;
  1459. master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
  1460. }
  1461. if (ahc->enabled_targets[scsi_id] != NULL
  1462. && ahc->enabled_targets[scsi_id] != master_tstate)
  1463. panic("%s: ahc_alloc_tstate - Target already allocated",
  1464. ahc_name(ahc));
  1465. tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate),
  1466. M_DEVBUF, M_NOWAIT);
  1467. if (tstate == NULL)
  1468. return (NULL);
  1469. /*
  1470. * If we have allocated a master tstate, copy user settings from
  1471. * the master tstate (taken from SRAM or the EEPROM) for this
  1472. * channel, but reset our current and goal settings to async/narrow
  1473. * until an initiator talks to us.
  1474. */
  1475. if (master_tstate != NULL) {
  1476. memcpy(tstate, master_tstate, sizeof(*tstate));
  1477. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  1478. tstate->ultraenb = 0;
  1479. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1480. memset(&tstate->transinfo[i].curr, 0,
  1481. sizeof(tstate->transinfo[i].curr));
  1482. memset(&tstate->transinfo[i].goal, 0,
  1483. sizeof(tstate->transinfo[i].goal));
  1484. }
  1485. } else
  1486. memset(tstate, 0, sizeof(*tstate));
  1487. ahc->enabled_targets[scsi_id] = tstate;
  1488. return (tstate);
  1489. }
  1490. #ifdef AHC_TARGET_MODE
  1491. /*
  1492. * Free per target mode instance (ID we respond to as a target)
  1493. * transfer negotiation data structures.
  1494. */
  1495. static void
  1496. ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
  1497. {
  1498. struct ahc_tmode_tstate *tstate;
  1499. /*
  1500. * Don't clean up our "master" tstate.
  1501. * It has our default user settings.
  1502. */
  1503. if (((channel == 'B' && scsi_id == ahc->our_id_b)
  1504. || (channel == 'A' && scsi_id == ahc->our_id))
  1505. && force == FALSE)
  1506. return;
  1507. if (channel == 'B')
  1508. scsi_id += 8;
  1509. tstate = ahc->enabled_targets[scsi_id];
  1510. if (tstate != NULL)
  1511. free(tstate, M_DEVBUF);
  1512. ahc->enabled_targets[scsi_id] = NULL;
  1513. }
  1514. #endif
  1515. /*
  1516. * Called when we have an active connection to a target on the bus,
  1517. * this function finds the nearest syncrate to the input period limited
  1518. * by the capabilities of the bus connectivity of and sync settings for
  1519. * the target.
  1520. */
  1521. struct ahc_syncrate *
  1522. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  1523. struct ahc_initiator_tinfo *tinfo,
  1524. u_int *period, u_int *ppr_options, role_t role)
  1525. {
  1526. struct ahc_transinfo *transinfo;
  1527. u_int maxsync;
  1528. if ((ahc->features & AHC_ULTRA2) != 0) {
  1529. if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
  1530. && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
  1531. maxsync = AHC_SYNCRATE_DT;
  1532. } else {
  1533. maxsync = AHC_SYNCRATE_ULTRA;
  1534. /* Can't do DT on an SE bus */
  1535. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1536. }
  1537. } else if ((ahc->features & AHC_ULTRA) != 0) {
  1538. maxsync = AHC_SYNCRATE_ULTRA;
  1539. } else {
  1540. maxsync = AHC_SYNCRATE_FAST;
  1541. }
  1542. /*
  1543. * Never allow a value higher than our current goal
  1544. * period otherwise we may allow a target initiated
  1545. * negotiation to go above the limit as set by the
  1546. * user. In the case of an initiator initiated
  1547. * sync negotiation, we limit based on the user
  1548. * setting. This allows the system to still accept
  1549. * incoming negotiations even if target initiated
  1550. * negotiation is not performed.
  1551. */
  1552. if (role == ROLE_TARGET)
  1553. transinfo = &tinfo->user;
  1554. else
  1555. transinfo = &tinfo->goal;
  1556. *ppr_options &= transinfo->ppr_options;
  1557. if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
  1558. maxsync = MAX(maxsync, AHC_SYNCRATE_ULTRA2);
  1559. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1560. }
  1561. if (transinfo->period == 0) {
  1562. *period = 0;
  1563. *ppr_options = 0;
  1564. return (NULL);
  1565. }
  1566. *period = MAX(*period, transinfo->period);
  1567. return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
  1568. }
  1569. /*
  1570. * Look up the valid period to SCSIRATE conversion in our table.
  1571. * Return the period and offset that should be sent to the target
  1572. * if this was the beginning of an SDTR.
  1573. */
  1574. struct ahc_syncrate *
  1575. ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
  1576. u_int *ppr_options, u_int maxsync)
  1577. {
  1578. struct ahc_syncrate *syncrate;
  1579. if ((ahc->features & AHC_DT) == 0)
  1580. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1581. /* Skip all DT only entries if DT is not available */
  1582. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  1583. && maxsync < AHC_SYNCRATE_ULTRA2)
  1584. maxsync = AHC_SYNCRATE_ULTRA2;
  1585. for (syncrate = &ahc_syncrates[maxsync];
  1586. syncrate->rate != NULL;
  1587. syncrate++) {
  1588. /*
  1589. * The Ultra2 table doesn't go as low
  1590. * as for the Fast/Ultra cards.
  1591. */
  1592. if ((ahc->features & AHC_ULTRA2) != 0
  1593. && (syncrate->sxfr_u2 == 0))
  1594. break;
  1595. if (*period <= syncrate->period) {
  1596. /*
  1597. * When responding to a target that requests
  1598. * sync, the requested rate may fall between
  1599. * two rates that we can output, but still be
  1600. * a rate that we can receive. Because of this,
  1601. * we want to respond to the target with
  1602. * the same rate that it sent to us even
  1603. * if the period we use to send data to it
  1604. * is lower. Only lower the response period
  1605. * if we must.
  1606. */
  1607. if (syncrate == &ahc_syncrates[maxsync])
  1608. *period = syncrate->period;
  1609. /*
  1610. * At some speeds, we only support
  1611. * ST transfers.
  1612. */
  1613. if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
  1614. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1615. break;
  1616. }
  1617. }
  1618. if ((*period == 0)
  1619. || (syncrate->rate == NULL)
  1620. || ((ahc->features & AHC_ULTRA2) != 0
  1621. && (syncrate->sxfr_u2 == 0))) {
  1622. /* Use asynchronous transfers. */
  1623. *period = 0;
  1624. syncrate = NULL;
  1625. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1626. }
  1627. return (syncrate);
  1628. }
  1629. /*
  1630. * Convert from an entry in our syncrate table to the SCSI equivalent
  1631. * sync "period" factor.
  1632. */
  1633. u_int
  1634. ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
  1635. {
  1636. struct ahc_syncrate *syncrate;
  1637. if ((ahc->features & AHC_ULTRA2) != 0)
  1638. scsirate &= SXFR_ULTRA2;
  1639. else
  1640. scsirate &= SXFR;
  1641. syncrate = &ahc_syncrates[maxsync];
  1642. while (syncrate->rate != NULL) {
  1643. if ((ahc->features & AHC_ULTRA2) != 0) {
  1644. if (syncrate->sxfr_u2 == 0)
  1645. break;
  1646. else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
  1647. return (syncrate->period);
  1648. } else if (scsirate == (syncrate->sxfr & SXFR)) {
  1649. return (syncrate->period);
  1650. }
  1651. syncrate++;
  1652. }
  1653. return (0); /* async */
  1654. }
  1655. /*
  1656. * Truncate the given synchronous offset to a value the
  1657. * current adapter type and syncrate are capable of.
  1658. */
  1659. void
  1660. ahc_validate_offset(struct ahc_softc *ahc,
  1661. struct ahc_initiator_tinfo *tinfo,
  1662. struct ahc_syncrate *syncrate,
  1663. u_int *offset, int wide, role_t role)
  1664. {
  1665. u_int maxoffset;
  1666. /* Limit offset to what we can do */
  1667. if (syncrate == NULL) {
  1668. maxoffset = 0;
  1669. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  1670. maxoffset = MAX_OFFSET_ULTRA2;
  1671. } else {
  1672. if (wide)
  1673. maxoffset = MAX_OFFSET_16BIT;
  1674. else
  1675. maxoffset = MAX_OFFSET_8BIT;
  1676. }
  1677. *offset = MIN(*offset, maxoffset);
  1678. if (tinfo != NULL) {
  1679. if (role == ROLE_TARGET)
  1680. *offset = MIN(*offset, tinfo->user.offset);
  1681. else
  1682. *offset = MIN(*offset, tinfo->goal.offset);
  1683. }
  1684. }
  1685. /*
  1686. * Truncate the given transfer width parameter to a value the
  1687. * current adapter type is capable of.
  1688. */
  1689. void
  1690. ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
  1691. u_int *bus_width, role_t role)
  1692. {
  1693. switch (*bus_width) {
  1694. default:
  1695. if (ahc->features & AHC_WIDE) {
  1696. /* Respond Wide */
  1697. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  1698. break;
  1699. }
  1700. /* FALLTHROUGH */
  1701. case MSG_EXT_WDTR_BUS_8_BIT:
  1702. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  1703. break;
  1704. }
  1705. if (tinfo != NULL) {
  1706. if (role == ROLE_TARGET)
  1707. *bus_width = MIN(tinfo->user.width, *bus_width);
  1708. else
  1709. *bus_width = MIN(tinfo->goal.width, *bus_width);
  1710. }
  1711. }
  1712. /*
  1713. * Update the bitmask of targets for which the controller should
  1714. * negotiate with at the next convenient oportunity. This currently
  1715. * means the next time we send the initial identify messages for
  1716. * a new transaction.
  1717. */
  1718. int
  1719. ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1720. struct ahc_tmode_tstate *tstate,
  1721. struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
  1722. {
  1723. u_int auto_negotiate_orig;
  1724. auto_negotiate_orig = tstate->auto_negotiate;
  1725. if (neg_type == AHC_NEG_ALWAYS) {
  1726. /*
  1727. * Force our "current" settings to be
  1728. * unknown so that unless a bus reset
  1729. * occurs the need to renegotiate is
  1730. * recorded persistently.
  1731. */
  1732. if ((ahc->features & AHC_WIDE) != 0)
  1733. tinfo->curr.width = AHC_WIDTH_UNKNOWN;
  1734. tinfo->curr.period = AHC_PERIOD_UNKNOWN;
  1735. tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
  1736. }
  1737. if (tinfo->curr.period != tinfo->goal.period
  1738. || tinfo->curr.width != tinfo->goal.width
  1739. || tinfo->curr.offset != tinfo->goal.offset
  1740. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  1741. || (neg_type == AHC_NEG_IF_NON_ASYNC
  1742. && (tinfo->goal.offset != 0
  1743. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  1744. || tinfo->goal.ppr_options != 0)))
  1745. tstate->auto_negotiate |= devinfo->target_mask;
  1746. else
  1747. tstate->auto_negotiate &= ~devinfo->target_mask;
  1748. return (auto_negotiate_orig != tstate->auto_negotiate);
  1749. }
  1750. /*
  1751. * Update the user/goal/curr tables of synchronous negotiation
  1752. * parameters as well as, in the case of a current or active update,
  1753. * any data structures on the host controller. In the case of an
  1754. * active update, the specified target is currently talking to us on
  1755. * the bus, so the transfer parameter update must take effect
  1756. * immediately.
  1757. */
  1758. void
  1759. ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1760. struct ahc_syncrate *syncrate, u_int period,
  1761. u_int offset, u_int ppr_options, u_int type, int paused)
  1762. {
  1763. struct ahc_initiator_tinfo *tinfo;
  1764. struct ahc_tmode_tstate *tstate;
  1765. u_int old_period;
  1766. u_int old_offset;
  1767. u_int old_ppr;
  1768. int active;
  1769. int update_needed;
  1770. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1771. update_needed = 0;
  1772. if (syncrate == NULL) {
  1773. period = 0;
  1774. offset = 0;
  1775. }
  1776. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1777. devinfo->target, &tstate);
  1778. if ((type & AHC_TRANS_USER) != 0) {
  1779. tinfo->user.period = period;
  1780. tinfo->user.offset = offset;
  1781. tinfo->user.ppr_options = ppr_options;
  1782. }
  1783. if ((type & AHC_TRANS_GOAL) != 0) {
  1784. tinfo->goal.period = period;
  1785. tinfo->goal.offset = offset;
  1786. tinfo->goal.ppr_options = ppr_options;
  1787. }
  1788. old_period = tinfo->curr.period;
  1789. old_offset = tinfo->curr.offset;
  1790. old_ppr = tinfo->curr.ppr_options;
  1791. if ((type & AHC_TRANS_CUR) != 0
  1792. && (old_period != period
  1793. || old_offset != offset
  1794. || old_ppr != ppr_options)) {
  1795. u_int scsirate;
  1796. update_needed++;
  1797. scsirate = tinfo->scsirate;
  1798. if ((ahc->features & AHC_ULTRA2) != 0) {
  1799. scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
  1800. if (syncrate != NULL) {
  1801. scsirate |= syncrate->sxfr_u2;
  1802. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
  1803. scsirate |= ENABLE_CRC;
  1804. else
  1805. scsirate |= SINGLE_EDGE;
  1806. }
  1807. } else {
  1808. scsirate &= ~(SXFR|SOFS);
  1809. /*
  1810. * Ensure Ultra mode is set properly for
  1811. * this target.
  1812. */
  1813. tstate->ultraenb &= ~devinfo->target_mask;
  1814. if (syncrate != NULL) {
  1815. if (syncrate->sxfr & ULTRA_SXFR) {
  1816. tstate->ultraenb |=
  1817. devinfo->target_mask;
  1818. }
  1819. scsirate |= syncrate->sxfr & SXFR;
  1820. scsirate |= offset & SOFS;
  1821. }
  1822. if (active) {
  1823. u_int sxfrctl0;
  1824. sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
  1825. sxfrctl0 &= ~FAST20;
  1826. if (tstate->ultraenb & devinfo->target_mask)
  1827. sxfrctl0 |= FAST20;
  1828. ahc_outb(ahc, SXFRCTL0, sxfrctl0);
  1829. }
  1830. }
  1831. if (active) {
  1832. ahc_outb(ahc, SCSIRATE, scsirate);
  1833. if ((ahc->features & AHC_ULTRA2) != 0)
  1834. ahc_outb(ahc, SCSIOFFSET, offset);
  1835. }
  1836. tinfo->scsirate = scsirate;
  1837. tinfo->curr.period = period;
  1838. tinfo->curr.offset = offset;
  1839. tinfo->curr.ppr_options = ppr_options;
  1840. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1841. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  1842. if (bootverbose) {
  1843. if (offset != 0) {
  1844. printf("%s: target %d synchronous at %sMHz%s, "
  1845. "offset = 0x%x\n", ahc_name(ahc),
  1846. devinfo->target, syncrate->rate,
  1847. (ppr_options & MSG_EXT_PPR_DT_REQ)
  1848. ? " DT" : "", offset);
  1849. } else {
  1850. printf("%s: target %d using "
  1851. "asynchronous transfers\n",
  1852. ahc_name(ahc), devinfo->target);
  1853. }
  1854. }
  1855. }
  1856. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1857. tinfo, AHC_NEG_TO_GOAL);
  1858. if (update_needed)
  1859. ahc_update_pending_scbs(ahc);
  1860. }
  1861. /*
  1862. * Update the user/goal/curr tables of wide negotiation
  1863. * parameters as well as, in the case of a current or active update,
  1864. * any data structures on the host controller. In the case of an
  1865. * active update, the specified target is currently talking to us on
  1866. * the bus, so the transfer parameter update must take effect
  1867. * immediately.
  1868. */
  1869. void
  1870. ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1871. u_int width, u_int type, int paused)
  1872. {
  1873. struct ahc_initiator_tinfo *tinfo;
  1874. struct ahc_tmode_tstate *tstate;
  1875. u_int oldwidth;
  1876. int active;
  1877. int update_needed;
  1878. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1879. update_needed = 0;
  1880. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1881. devinfo->target, &tstate);
  1882. if ((type & AHC_TRANS_USER) != 0)
  1883. tinfo->user.width = width;
  1884. if ((type & AHC_TRANS_GOAL) != 0)
  1885. tinfo->goal.width = width;
  1886. oldwidth = tinfo->curr.width;
  1887. if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
  1888. u_int scsirate;
  1889. update_needed++;
  1890. scsirate = tinfo->scsirate;
  1891. scsirate &= ~WIDEXFER;
  1892. if (width == MSG_EXT_WDTR_BUS_16_BIT)
  1893. scsirate |= WIDEXFER;
  1894. tinfo->scsirate = scsirate;
  1895. if (active)
  1896. ahc_outb(ahc, SCSIRATE, scsirate);
  1897. tinfo->curr.width = width;
  1898. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1899. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  1900. if (bootverbose) {
  1901. printf("%s: target %d using %dbit transfers\n",
  1902. ahc_name(ahc), devinfo->target,
  1903. 8 * (0x01 << width));
  1904. }
  1905. }
  1906. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1907. tinfo, AHC_NEG_TO_GOAL);
  1908. if (update_needed)
  1909. ahc_update_pending_scbs(ahc);
  1910. }
  1911. /*
  1912. * Update the current state of tagged queuing for a given target.
  1913. */
  1914. void
  1915. ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1916. ahc_queue_alg alg)
  1917. {
  1918. ahc_platform_set_tags(ahc, devinfo, alg);
  1919. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1920. devinfo->lun, AC_TRANSFER_NEG, &alg);
  1921. }
  1922. /*
  1923. * When the transfer settings for a connection change, update any
  1924. * in-transit SCBs to contain the new data so the hardware will
  1925. * be set correctly during future (re)selections.
  1926. */
  1927. static void
  1928. ahc_update_pending_scbs(struct ahc_softc *ahc)
  1929. {
  1930. struct scb *pending_scb;
  1931. int pending_scb_count;
  1932. int i;
  1933. int paused;
  1934. u_int saved_scbptr;
  1935. /*
  1936. * Traverse the pending SCB list and ensure that all of the
  1937. * SCBs there have the proper settings.
  1938. */
  1939. pending_scb_count = 0;
  1940. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1941. struct ahc_devinfo devinfo;
  1942. struct hardware_scb *pending_hscb;
  1943. struct ahc_initiator_tinfo *tinfo;
  1944. struct ahc_tmode_tstate *tstate;
  1945. ahc_scb_devinfo(ahc, &devinfo, pending_scb);
  1946. tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  1947. devinfo.our_scsiid,
  1948. devinfo.target, &tstate);
  1949. pending_hscb = pending_scb->hscb;
  1950. pending_hscb->control &= ~ULTRAENB;
  1951. if ((tstate->ultraenb & devinfo.target_mask) != 0)
  1952. pending_hscb->control |= ULTRAENB;
  1953. pending_hscb->scsirate = tinfo->scsirate;
  1954. pending_hscb->scsioffset = tinfo->curr.offset;
  1955. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  1956. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  1957. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  1958. pending_hscb->control &= ~MK_MESSAGE;
  1959. }
  1960. ahc_sync_scb(ahc, pending_scb,
  1961. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  1962. pending_scb_count++;
  1963. }
  1964. if (pending_scb_count == 0)
  1965. return;
  1966. if (ahc_is_paused(ahc)) {
  1967. paused = 1;
  1968. } else {
  1969. paused = 0;
  1970. ahc_pause(ahc);
  1971. }
  1972. saved_scbptr = ahc_inb(ahc, SCBPTR);
  1973. /* Ensure that the hscbs down on the card match the new information */
  1974. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  1975. struct hardware_scb *pending_hscb;
  1976. u_int control;
  1977. u_int scb_tag;
  1978. ahc_outb(ahc, SCBPTR, i);
  1979. scb_tag = ahc_inb(ahc, SCB_TAG);
  1980. pending_scb = ahc_lookup_scb(ahc, scb_tag);
  1981. if (pending_scb == NULL)
  1982. continue;
  1983. pending_hscb = pending_scb->hscb;
  1984. control = ahc_inb(ahc, SCB_CONTROL);
  1985. control &= ~(ULTRAENB|MK_MESSAGE);
  1986. control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
  1987. ahc_outb(ahc, SCB_CONTROL, control);
  1988. ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
  1989. ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
  1990. }
  1991. ahc_outb(ahc, SCBPTR, saved_scbptr);
  1992. if (paused == 0)
  1993. ahc_unpause(ahc);
  1994. }
  1995. /**************************** Pathing Information *****************************/
  1996. static void
  1997. ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1998. {
  1999. u_int saved_scsiid;
  2000. role_t role;
  2001. int our_id;
  2002. if (ahc_inb(ahc, SSTAT0) & TARGET)
  2003. role = ROLE_TARGET;
  2004. else
  2005. role = ROLE_INITIATOR;
  2006. if (role == ROLE_TARGET
  2007. && (ahc->features & AHC_MULTI_TID) != 0
  2008. && (ahc_inb(ahc, SEQ_FLAGS)
  2009. & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
  2010. /* We were selected, so pull our id from TARGIDIN */
  2011. our_id = ahc_inb(ahc, TARGIDIN) & OID;
  2012. } else if ((ahc->features & AHC_ULTRA2) != 0)
  2013. our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
  2014. else
  2015. our_id = ahc_inb(ahc, SCSIID) & OID;
  2016. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2017. ahc_compile_devinfo(devinfo,
  2018. our_id,
  2019. SCSIID_TARGET(ahc, saved_scsiid),
  2020. ahc_inb(ahc, SAVED_LUN),
  2021. SCSIID_CHANNEL(ahc, saved_scsiid),
  2022. role);
  2023. }
  2024. struct ahc_phase_table_entry*
  2025. ahc_lookup_phase_entry(int phase)
  2026. {
  2027. struct ahc_phase_table_entry *entry;
  2028. struct ahc_phase_table_entry *last_entry;
  2029. /*
  2030. * num_phases doesn't include the default entry which
  2031. * will be returned if the phase doesn't match.
  2032. */
  2033. last_entry = &ahc_phase_table[num_phases];
  2034. for (entry = ahc_phase_table; entry < last_entry; entry++) {
  2035. if (phase == entry->phase)
  2036. break;
  2037. }
  2038. return (entry);
  2039. }
  2040. void
  2041. ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
  2042. u_int lun, char channel, role_t role)
  2043. {
  2044. devinfo->our_scsiid = our_id;
  2045. devinfo->target = target;
  2046. devinfo->lun = lun;
  2047. devinfo->target_offset = target;
  2048. devinfo->channel = channel;
  2049. devinfo->role = role;
  2050. if (channel == 'B')
  2051. devinfo->target_offset += 8;
  2052. devinfo->target_mask = (0x01 << devinfo->target_offset);
  2053. }
  2054. void
  2055. ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2056. {
  2057. printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
  2058. devinfo->target, devinfo->lun);
  2059. }
  2060. static void
  2061. ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2062. struct scb *scb)
  2063. {
  2064. role_t role;
  2065. int our_id;
  2066. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  2067. role = ROLE_INITIATOR;
  2068. if ((scb->flags & SCB_TARGET_SCB) != 0)
  2069. role = ROLE_TARGET;
  2070. ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
  2071. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
  2072. }
  2073. /************************ Message Phase Processing ****************************/
  2074. static void
  2075. ahc_assert_atn(struct ahc_softc *ahc)
  2076. {
  2077. u_int scsisigo;
  2078. scsisigo = ATNO;
  2079. if ((ahc->features & AHC_DT) == 0)
  2080. scsisigo |= ahc_inb(ahc, SCSISIGI);
  2081. ahc_outb(ahc, SCSISIGO, scsisigo);
  2082. }
  2083. /*
  2084. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  2085. * or enters the initial message out phase, we are interrupted. Fill our
  2086. * outgoing message buffer with the appropriate message and beging handing
  2087. * the message phase(s) manually.
  2088. */
  2089. static void
  2090. ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2091. struct scb *scb)
  2092. {
  2093. /*
  2094. * To facilitate adding multiple messages together,
  2095. * each routine should increment the index and len
  2096. * variables instead of setting them explicitly.
  2097. */
  2098. ahc->msgout_index = 0;
  2099. ahc->msgout_len = 0;
  2100. if ((scb->flags & SCB_DEVICE_RESET) == 0
  2101. && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
  2102. u_int identify_msg;
  2103. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  2104. if ((scb->hscb->control & DISCENB) != 0)
  2105. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  2106. ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
  2107. ahc->msgout_len++;
  2108. if ((scb->hscb->control & TAG_ENB) != 0) {
  2109. ahc->msgout_buf[ahc->msgout_index++] =
  2110. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  2111. ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
  2112. ahc->msgout_len += 2;
  2113. }
  2114. }
  2115. if (scb->flags & SCB_DEVICE_RESET) {
  2116. ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
  2117. ahc->msgout_len++;
  2118. ahc_print_path(ahc, scb);
  2119. printf("Bus Device Reset Message Sent\n");
  2120. /*
  2121. * Clear our selection hardware in advance of
  2122. * the busfree. We may have an entry in the waiting
  2123. * Q for this target, and we don't want to go about
  2124. * selecting while we handle the busfree and blow it
  2125. * away.
  2126. */
  2127. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2128. } else if ((scb->flags & SCB_ABORT) != 0) {
  2129. if ((scb->hscb->control & TAG_ENB) != 0)
  2130. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
  2131. else
  2132. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
  2133. ahc->msgout_len++;
  2134. ahc_print_path(ahc, scb);
  2135. printf("Abort%s Message Sent\n",
  2136. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  2137. /*
  2138. * Clear our selection hardware in advance of
  2139. * the busfree. We may have an entry in the waiting
  2140. * Q for this target, and we don't want to go about
  2141. * selecting while we handle the busfree and blow it
  2142. * away.
  2143. */
  2144. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2145. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  2146. ahc_build_transfer_msg(ahc, devinfo);
  2147. } else {
  2148. printf("ahc_intr: AWAITING_MSG for an SCB that "
  2149. "does not have a waiting message\n");
  2150. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  2151. devinfo->target_mask);
  2152. panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
  2153. "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
  2154. ahc_inb(ahc, MSG_OUT), scb->flags);
  2155. }
  2156. /*
  2157. * Clear the MK_MESSAGE flag from the SCB so we aren't
  2158. * asked to send this message again.
  2159. */
  2160. ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
  2161. scb->hscb->control &= ~MK_MESSAGE;
  2162. ahc->msgout_index = 0;
  2163. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2164. }
  2165. /*
  2166. * Build an appropriate transfer negotiation message for the
  2167. * currently active target.
  2168. */
  2169. static void
  2170. ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2171. {
  2172. /*
  2173. * We need to initiate transfer negotiations.
  2174. * If our current and goal settings are identical,
  2175. * we want to renegotiate due to a check condition.
  2176. */
  2177. struct ahc_initiator_tinfo *tinfo;
  2178. struct ahc_tmode_tstate *tstate;
  2179. struct ahc_syncrate *rate;
  2180. int dowide;
  2181. int dosync;
  2182. int doppr;
  2183. u_int period;
  2184. u_int ppr_options;
  2185. u_int offset;
  2186. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2187. devinfo->target, &tstate);
  2188. /*
  2189. * Filter our period based on the current connection.
  2190. * If we can't perform DT transfers on this segment (not in LVD
  2191. * mode for instance), then our decision to issue a PPR message
  2192. * may change.
  2193. */
  2194. period = tinfo->goal.period;
  2195. offset = tinfo->goal.offset;
  2196. ppr_options = tinfo->goal.ppr_options;
  2197. /* Target initiated PPR is not allowed in the SCSI spec */
  2198. if (devinfo->role == ROLE_TARGET)
  2199. ppr_options = 0;
  2200. rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2201. &ppr_options, devinfo->role);
  2202. dowide = tinfo->curr.width != tinfo->goal.width;
  2203. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  2204. /*
  2205. * Only use PPR if we have options that need it, even if the device
  2206. * claims to support it. There might be an expander in the way
  2207. * that doesn't.
  2208. */
  2209. doppr = ppr_options != 0;
  2210. if (!dowide && !dosync && !doppr) {
  2211. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  2212. dosync = tinfo->goal.offset != 0;
  2213. }
  2214. if (!dowide && !dosync && !doppr) {
  2215. /*
  2216. * Force async with a WDTR message if we have a wide bus,
  2217. * or just issue an SDTR with a 0 offset.
  2218. */
  2219. if ((ahc->features & AHC_WIDE) != 0)
  2220. dowide = 1;
  2221. else
  2222. dosync = 1;
  2223. if (bootverbose) {
  2224. ahc_print_devinfo(ahc, devinfo);
  2225. printf("Ensuring async\n");
  2226. }
  2227. }
  2228. /* Target initiated PPR is not allowed in the SCSI spec */
  2229. if (devinfo->role == ROLE_TARGET)
  2230. doppr = 0;
  2231. /*
  2232. * Both the PPR message and SDTR message require the
  2233. * goal syncrate to be limited to what the target device
  2234. * is capable of handling (based on whether an LVD->SE
  2235. * expander is on the bus), so combine these two cases.
  2236. * Regardless, guarantee that if we are using WDTR and SDTR
  2237. * messages that WDTR comes first.
  2238. */
  2239. if (doppr || (dosync && !dowide)) {
  2240. offset = tinfo->goal.offset;
  2241. ahc_validate_offset(ahc, tinfo, rate, &offset,
  2242. doppr ? tinfo->goal.width
  2243. : tinfo->curr.width,
  2244. devinfo->role);
  2245. if (doppr) {
  2246. ahc_construct_ppr(ahc, devinfo, period, offset,
  2247. tinfo->goal.width, ppr_options);
  2248. } else {
  2249. ahc_construct_sdtr(ahc, devinfo, period, offset);
  2250. }
  2251. } else {
  2252. ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
  2253. }
  2254. }
  2255. /*
  2256. * Build a synchronous negotiation message in our message
  2257. * buffer based on the input parameters.
  2258. */
  2259. static void
  2260. ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2261. u_int period, u_int offset)
  2262. {
  2263. if (offset == 0)
  2264. period = AHC_ASYNC_XFER_PERIOD;
  2265. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2266. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN;
  2267. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR;
  2268. ahc->msgout_buf[ahc->msgout_index++] = period;
  2269. ahc->msgout_buf[ahc->msgout_index++] = offset;
  2270. ahc->msgout_len += 5;
  2271. if (bootverbose) {
  2272. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  2273. ahc_name(ahc), devinfo->channel, devinfo->target,
  2274. devinfo->lun, period, offset);
  2275. }
  2276. }
  2277. /*
  2278. * Build a wide negotiation message in our message
  2279. * buffer based on the input parameters.
  2280. */
  2281. static void
  2282. ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2283. u_int bus_width)
  2284. {
  2285. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2286. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN;
  2287. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR;
  2288. ahc->msgout_buf[ahc->msgout_index++] = bus_width;
  2289. ahc->msgout_len += 4;
  2290. if (bootverbose) {
  2291. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  2292. ahc_name(ahc), devinfo->channel, devinfo->target,
  2293. devinfo->lun, bus_width);
  2294. }
  2295. }
  2296. /*
  2297. * Build a parallel protocol request message in our message
  2298. * buffer based on the input parameters.
  2299. */
  2300. static void
  2301. ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2302. u_int period, u_int offset, u_int bus_width,
  2303. u_int ppr_options)
  2304. {
  2305. if (offset == 0)
  2306. period = AHC_ASYNC_XFER_PERIOD;
  2307. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2308. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR_LEN;
  2309. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR;
  2310. ahc->msgout_buf[ahc->msgout_index++] = period;
  2311. ahc->msgout_buf[ahc->msgout_index++] = 0;
  2312. ahc->msgout_buf[ahc->msgout_index++] = offset;
  2313. ahc->msgout_buf[ahc->msgout_index++] = bus_width;
  2314. ahc->msgout_buf[ahc->msgout_index++] = ppr_options;
  2315. ahc->msgout_len += 8;
  2316. if (bootverbose) {
  2317. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  2318. "offset %x, ppr_options %x\n", ahc_name(ahc),
  2319. devinfo->channel, devinfo->target, devinfo->lun,
  2320. bus_width, period, offset, ppr_options);
  2321. }
  2322. }
  2323. /*
  2324. * Clear any active message state.
  2325. */
  2326. static void
  2327. ahc_clear_msg_state(struct ahc_softc *ahc)
  2328. {
  2329. ahc->msgout_len = 0;
  2330. ahc->msgin_index = 0;
  2331. ahc->msg_type = MSG_TYPE_NONE;
  2332. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
  2333. /*
  2334. * The target didn't care to respond to our
  2335. * message request, so clear ATN.
  2336. */
  2337. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2338. }
  2339. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  2340. ahc_outb(ahc, SEQ_FLAGS2,
  2341. ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  2342. }
  2343. static void
  2344. ahc_handle_proto_violation(struct ahc_softc *ahc)
  2345. {
  2346. struct ahc_devinfo devinfo;
  2347. struct scb *scb;
  2348. u_int scbid;
  2349. u_int seq_flags;
  2350. u_int curphase;
  2351. u_int lastphase;
  2352. int found;
  2353. ahc_fetch_devinfo(ahc, &devinfo);
  2354. scbid = ahc_inb(ahc, SCB_TAG);
  2355. scb = ahc_lookup_scb(ahc, scbid);
  2356. seq_flags = ahc_inb(ahc, SEQ_FLAGS);
  2357. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2358. lastphase = ahc_inb(ahc, LASTPHASE);
  2359. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2360. /*
  2361. * The reconnecting target either did not send an
  2362. * identify message, or did, but we didn't find an SCB
  2363. * to match.
  2364. */
  2365. ahc_print_devinfo(ahc, &devinfo);
  2366. printf("Target did not send an IDENTIFY message. "
  2367. "LASTPHASE = 0x%x.\n", lastphase);
  2368. scb = NULL;
  2369. } else if (scb == NULL) {
  2370. /*
  2371. * We don't seem to have an SCB active for this
  2372. * transaction. Print an error and reset the bus.
  2373. */
  2374. ahc_print_devinfo(ahc, &devinfo);
  2375. printf("No SCB found during protocol violation\n");
  2376. goto proto_violation_reset;
  2377. } else {
  2378. ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2379. if ((seq_flags & NO_CDB_SENT) != 0) {
  2380. ahc_print_path(ahc, scb);
  2381. printf("No or incomplete CDB sent to device.\n");
  2382. } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
  2383. /*
  2384. * The target never bothered to provide status to
  2385. * us prior to completing the command. Since we don't
  2386. * know the disposition of this command, we must attempt
  2387. * to abort it. Assert ATN and prepare to send an abort
  2388. * message.
  2389. */
  2390. ahc_print_path(ahc, scb);
  2391. printf("Completed command without status.\n");
  2392. } else {
  2393. ahc_print_path(ahc, scb);
  2394. printf("Unknown protocol violation.\n");
  2395. ahc_dump_card_state(ahc);
  2396. }
  2397. }
  2398. if ((lastphase & ~P_DATAIN_DT) == 0
  2399. || lastphase == P_COMMAND) {
  2400. proto_violation_reset:
  2401. /*
  2402. * Target either went directly to data/command
  2403. * phase or didn't respond to our ATN.
  2404. * The only safe thing to do is to blow
  2405. * it away with a bus reset.
  2406. */
  2407. found = ahc_reset_channel(ahc, 'A', TRUE);
  2408. printf("%s: Issued Channel %c Bus Reset. "
  2409. "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
  2410. } else {
  2411. /*
  2412. * Leave the selection hardware off in case
  2413. * this abort attempt will affect yet to
  2414. * be sent commands.
  2415. */
  2416. ahc_outb(ahc, SCSISEQ,
  2417. ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  2418. ahc_assert_atn(ahc);
  2419. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2420. if (scb == NULL) {
  2421. ahc_print_devinfo(ahc, &devinfo);
  2422. ahc->msgout_buf[0] = MSG_ABORT_TASK;
  2423. ahc->msgout_len = 1;
  2424. ahc->msgout_index = 0;
  2425. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2426. } else {
  2427. ahc_print_path(ahc, scb);
  2428. scb->flags |= SCB_ABORT;
  2429. }
  2430. printf("Protocol violation %s. Attempting to abort.\n",
  2431. ahc_lookup_phase_entry(curphase)->phasemsg);
  2432. }
  2433. }
  2434. /*
  2435. * Manual message loop handler.
  2436. */
  2437. static void
  2438. ahc_handle_message_phase(struct ahc_softc *ahc)
  2439. {
  2440. struct ahc_devinfo devinfo;
  2441. u_int bus_phase;
  2442. int end_session;
  2443. ahc_fetch_devinfo(ahc, &devinfo);
  2444. end_session = FALSE;
  2445. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2446. reswitch:
  2447. switch (ahc->msg_type) {
  2448. case MSG_TYPE_INITIATOR_MSGOUT:
  2449. {
  2450. int lastbyte;
  2451. int phasemis;
  2452. int msgdone;
  2453. if (ahc->msgout_len == 0)
  2454. panic("HOST_MSG_LOOP interrupt with no active message");
  2455. #ifdef AHC_DEBUG
  2456. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2457. ahc_print_devinfo(ahc, &devinfo);
  2458. printf("INITIATOR_MSG_OUT");
  2459. }
  2460. #endif
  2461. phasemis = bus_phase != P_MESGOUT;
  2462. if (phasemis) {
  2463. #ifdef AHC_DEBUG
  2464. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2465. printf(" PHASEMIS %s\n",
  2466. ahc_lookup_phase_entry(bus_phase)
  2467. ->phasemsg);
  2468. }
  2469. #endif
  2470. if (bus_phase == P_MESGIN) {
  2471. /*
  2472. * Change gears and see if
  2473. * this messages is of interest to
  2474. * us or should be passed back to
  2475. * the sequencer.
  2476. */
  2477. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2478. ahc->send_msg_perror = FALSE;
  2479. ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  2480. ahc->msgin_index = 0;
  2481. goto reswitch;
  2482. }
  2483. end_session = TRUE;
  2484. break;
  2485. }
  2486. if (ahc->send_msg_perror) {
  2487. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2488. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2489. #ifdef AHC_DEBUG
  2490. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2491. printf(" byte 0x%x\n", ahc->send_msg_perror);
  2492. #endif
  2493. ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
  2494. break;
  2495. }
  2496. msgdone = ahc->msgout_index == ahc->msgout_len;
  2497. if (msgdone) {
  2498. /*
  2499. * The target has requested a retry.
  2500. * Re-assert ATN, reset our message index to
  2501. * 0, and try again.
  2502. */
  2503. ahc->msgout_index = 0;
  2504. ahc_assert_atn(ahc);
  2505. }
  2506. lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
  2507. if (lastbyte) {
  2508. /* Last byte is signified by dropping ATN */
  2509. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2510. }
  2511. /*
  2512. * Clear our interrupt status and present
  2513. * the next byte on the bus.
  2514. */
  2515. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2516. #ifdef AHC_DEBUG
  2517. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2518. printf(" byte 0x%x\n",
  2519. ahc->msgout_buf[ahc->msgout_index]);
  2520. #endif
  2521. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2522. break;
  2523. }
  2524. case MSG_TYPE_INITIATOR_MSGIN:
  2525. {
  2526. int phasemis;
  2527. int message_done;
  2528. #ifdef AHC_DEBUG
  2529. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2530. ahc_print_devinfo(ahc, &devinfo);
  2531. printf("INITIATOR_MSG_IN");
  2532. }
  2533. #endif
  2534. phasemis = bus_phase != P_MESGIN;
  2535. if (phasemis) {
  2536. #ifdef AHC_DEBUG
  2537. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2538. printf(" PHASEMIS %s\n",
  2539. ahc_lookup_phase_entry(bus_phase)
  2540. ->phasemsg);
  2541. }
  2542. #endif
  2543. ahc->msgin_index = 0;
  2544. if (bus_phase == P_MESGOUT
  2545. && (ahc->send_msg_perror == TRUE
  2546. || (ahc->msgout_len != 0
  2547. && ahc->msgout_index == 0))) {
  2548. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2549. goto reswitch;
  2550. }
  2551. end_session = TRUE;
  2552. break;
  2553. }
  2554. /* Pull the byte in without acking it */
  2555. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
  2556. #ifdef AHC_DEBUG
  2557. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2558. printf(" byte 0x%x\n",
  2559. ahc->msgin_buf[ahc->msgin_index]);
  2560. #endif
  2561. message_done = ahc_parse_msg(ahc, &devinfo);
  2562. if (message_done) {
  2563. /*
  2564. * Clear our incoming message buffer in case there
  2565. * is another message following this one.
  2566. */
  2567. ahc->msgin_index = 0;
  2568. /*
  2569. * If this message illicited a response,
  2570. * assert ATN so the target takes us to the
  2571. * message out phase.
  2572. */
  2573. if (ahc->msgout_len != 0) {
  2574. #ifdef AHC_DEBUG
  2575. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2576. ahc_print_devinfo(ahc, &devinfo);
  2577. printf("Asserting ATN for response\n");
  2578. }
  2579. #endif
  2580. ahc_assert_atn(ahc);
  2581. }
  2582. } else
  2583. ahc->msgin_index++;
  2584. if (message_done == MSGLOOP_TERMINATED) {
  2585. end_session = TRUE;
  2586. } else {
  2587. /* Ack the byte */
  2588. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2589. ahc_inb(ahc, SCSIDATL);
  2590. }
  2591. break;
  2592. }
  2593. case MSG_TYPE_TARGET_MSGIN:
  2594. {
  2595. int msgdone;
  2596. int msgout_request;
  2597. if (ahc->msgout_len == 0)
  2598. panic("Target MSGIN with no active message");
  2599. /*
  2600. * If we interrupted a mesgout session, the initiator
  2601. * will not know this until our first REQ. So, we
  2602. * only honor mesgout requests after we've sent our
  2603. * first byte.
  2604. */
  2605. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
  2606. && ahc->msgout_index > 0)
  2607. msgout_request = TRUE;
  2608. else
  2609. msgout_request = FALSE;
  2610. if (msgout_request) {
  2611. /*
  2612. * Change gears and see if
  2613. * this messages is of interest to
  2614. * us or should be passed back to
  2615. * the sequencer.
  2616. */
  2617. ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
  2618. ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
  2619. ahc->msgin_index = 0;
  2620. /* Dummy read to REQ for first byte */
  2621. ahc_inb(ahc, SCSIDATL);
  2622. ahc_outb(ahc, SXFRCTL0,
  2623. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2624. break;
  2625. }
  2626. msgdone = ahc->msgout_index == ahc->msgout_len;
  2627. if (msgdone) {
  2628. ahc_outb(ahc, SXFRCTL0,
  2629. ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2630. end_session = TRUE;
  2631. break;
  2632. }
  2633. /*
  2634. * Present the next byte on the bus.
  2635. */
  2636. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2637. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2638. break;
  2639. }
  2640. case MSG_TYPE_TARGET_MSGOUT:
  2641. {
  2642. int lastbyte;
  2643. int msgdone;
  2644. /*
  2645. * The initiator signals that this is
  2646. * the last byte by dropping ATN.
  2647. */
  2648. lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
  2649. /*
  2650. * Read the latched byte, but turn off SPIOEN first
  2651. * so that we don't inadvertently cause a REQ for the
  2652. * next byte.
  2653. */
  2654. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2655. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
  2656. msgdone = ahc_parse_msg(ahc, &devinfo);
  2657. if (msgdone == MSGLOOP_TERMINATED) {
  2658. /*
  2659. * The message is *really* done in that it caused
  2660. * us to go to bus free. The sequencer has already
  2661. * been reset at this point, so pull the ejection
  2662. * handle.
  2663. */
  2664. return;
  2665. }
  2666. ahc->msgin_index++;
  2667. /*
  2668. * XXX Read spec about initiator dropping ATN too soon
  2669. * and use msgdone to detect it.
  2670. */
  2671. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  2672. ahc->msgin_index = 0;
  2673. /*
  2674. * If this message illicited a response, transition
  2675. * to the Message in phase and send it.
  2676. */
  2677. if (ahc->msgout_len != 0) {
  2678. ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
  2679. ahc_outb(ahc, SXFRCTL0,
  2680. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2681. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  2682. ahc->msgin_index = 0;
  2683. break;
  2684. }
  2685. }
  2686. if (lastbyte)
  2687. end_session = TRUE;
  2688. else {
  2689. /* Ask for the next byte. */
  2690. ahc_outb(ahc, SXFRCTL0,
  2691. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2692. }
  2693. break;
  2694. }
  2695. default:
  2696. panic("Unknown REQINIT message type");
  2697. }
  2698. if (end_session) {
  2699. ahc_clear_msg_state(ahc);
  2700. ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
  2701. } else
  2702. ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
  2703. }
  2704. /*
  2705. * See if we sent a particular extended message to the target.
  2706. * If "full" is true, return true only if the target saw the full
  2707. * message. If "full" is false, return true if the target saw at
  2708. * least the first byte of the message.
  2709. */
  2710. static int
  2711. ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
  2712. {
  2713. int found;
  2714. u_int index;
  2715. found = FALSE;
  2716. index = 0;
  2717. while (index < ahc->msgout_len) {
  2718. if (ahc->msgout_buf[index] == MSG_EXTENDED) {
  2719. u_int end_index;
  2720. end_index = index + 1 + ahc->msgout_buf[index + 1];
  2721. if (ahc->msgout_buf[index+2] == msgval
  2722. && type == AHCMSG_EXT) {
  2723. if (full) {
  2724. if (ahc->msgout_index > end_index)
  2725. found = TRUE;
  2726. } else if (ahc->msgout_index > index)
  2727. found = TRUE;
  2728. }
  2729. index = end_index;
  2730. } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
  2731. && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  2732. /* Skip tag type and tag id or residue param*/
  2733. index += 2;
  2734. } else {
  2735. /* Single byte message */
  2736. if (type == AHCMSG_1B
  2737. && ahc->msgout_buf[index] == msgval
  2738. && ahc->msgout_index > index)
  2739. found = TRUE;
  2740. index++;
  2741. }
  2742. if (found)
  2743. break;
  2744. }
  2745. return (found);
  2746. }
  2747. /*
  2748. * Wait for a complete incoming message, parse it, and respond accordingly.
  2749. */
  2750. static int
  2751. ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2752. {
  2753. struct ahc_initiator_tinfo *tinfo;
  2754. struct ahc_tmode_tstate *tstate;
  2755. int reject;
  2756. int done;
  2757. int response;
  2758. u_int targ_scsirate;
  2759. done = MSGLOOP_IN_PROG;
  2760. response = FALSE;
  2761. reject = FALSE;
  2762. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2763. devinfo->target, &tstate);
  2764. targ_scsirate = tinfo->scsirate;
  2765. /*
  2766. * Parse as much of the message as is available,
  2767. * rejecting it if we don't support it. When
  2768. * the entire message is available and has been
  2769. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  2770. * that we have parsed an entire message.
  2771. *
  2772. * In the case of extended messages, we accept the length
  2773. * byte outright and perform more checking once we know the
  2774. * extended message type.
  2775. */
  2776. switch (ahc->msgin_buf[0]) {
  2777. case MSG_DISCONNECT:
  2778. case MSG_SAVEDATAPOINTER:
  2779. case MSG_CMDCOMPLETE:
  2780. case MSG_RESTOREPOINTERS:
  2781. case MSG_IGN_WIDE_RESIDUE:
  2782. /*
  2783. * End our message loop as these are messages
  2784. * the sequencer handles on its own.
  2785. */
  2786. done = MSGLOOP_TERMINATED;
  2787. break;
  2788. case MSG_MESSAGE_REJECT:
  2789. response = ahc_handle_msg_reject(ahc, devinfo);
  2790. /* FALLTHROUGH */
  2791. case MSG_NOOP:
  2792. done = MSGLOOP_MSGCOMPLETE;
  2793. break;
  2794. case MSG_EXTENDED:
  2795. {
  2796. /* Wait for enough of the message to begin validation */
  2797. if (ahc->msgin_index < 2)
  2798. break;
  2799. switch (ahc->msgin_buf[2]) {
  2800. case MSG_EXT_SDTR:
  2801. {
  2802. struct ahc_syncrate *syncrate;
  2803. u_int period;
  2804. u_int ppr_options;
  2805. u_int offset;
  2806. u_int saved_offset;
  2807. if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  2808. reject = TRUE;
  2809. break;
  2810. }
  2811. /*
  2812. * Wait until we have both args before validating
  2813. * and acting on this message.
  2814. *
  2815. * Add one to MSG_EXT_SDTR_LEN to account for
  2816. * the extended message preamble.
  2817. */
  2818. if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  2819. break;
  2820. period = ahc->msgin_buf[3];
  2821. ppr_options = 0;
  2822. saved_offset = offset = ahc->msgin_buf[4];
  2823. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2824. &ppr_options,
  2825. devinfo->role);
  2826. ahc_validate_offset(ahc, tinfo, syncrate, &offset,
  2827. targ_scsirate & WIDEXFER,
  2828. devinfo->role);
  2829. if (bootverbose) {
  2830. printf("(%s:%c:%d:%d): Received "
  2831. "SDTR period %x, offset %x\n\t"
  2832. "Filtered to period %x, offset %x\n",
  2833. ahc_name(ahc), devinfo->channel,
  2834. devinfo->target, devinfo->lun,
  2835. ahc->msgin_buf[3], saved_offset,
  2836. period, offset);
  2837. }
  2838. ahc_set_syncrate(ahc, devinfo,
  2839. syncrate, period,
  2840. offset, ppr_options,
  2841. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2842. /*paused*/TRUE);
  2843. /*
  2844. * See if we initiated Sync Negotiation
  2845. * and didn't have to fall down to async
  2846. * transfers.
  2847. */
  2848. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  2849. /* We started it */
  2850. if (saved_offset != offset) {
  2851. /* Went too low - force async */
  2852. reject = TRUE;
  2853. }
  2854. } else {
  2855. /*
  2856. * Send our own SDTR in reply
  2857. */
  2858. if (bootverbose
  2859. && devinfo->role == ROLE_INITIATOR) {
  2860. printf("(%s:%c:%d:%d): Target "
  2861. "Initiated SDTR\n",
  2862. ahc_name(ahc), devinfo->channel,
  2863. devinfo->target, devinfo->lun);
  2864. }
  2865. ahc->msgout_index = 0;
  2866. ahc->msgout_len = 0;
  2867. ahc_construct_sdtr(ahc, devinfo,
  2868. period, offset);
  2869. ahc->msgout_index = 0;
  2870. response = TRUE;
  2871. }
  2872. done = MSGLOOP_MSGCOMPLETE;
  2873. break;
  2874. }
  2875. case MSG_EXT_WDTR:
  2876. {
  2877. u_int bus_width;
  2878. u_int saved_width;
  2879. u_int sending_reply;
  2880. sending_reply = FALSE;
  2881. if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  2882. reject = TRUE;
  2883. break;
  2884. }
  2885. /*
  2886. * Wait until we have our arg before validating
  2887. * and acting on this message.
  2888. *
  2889. * Add one to MSG_EXT_WDTR_LEN to account for
  2890. * the extended message preamble.
  2891. */
  2892. if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  2893. break;
  2894. bus_width = ahc->msgin_buf[3];
  2895. saved_width = bus_width;
  2896. ahc_validate_width(ahc, tinfo, &bus_width,
  2897. devinfo->role);
  2898. if (bootverbose) {
  2899. printf("(%s:%c:%d:%d): Received WDTR "
  2900. "%x filtered to %x\n",
  2901. ahc_name(ahc), devinfo->channel,
  2902. devinfo->target, devinfo->lun,
  2903. saved_width, bus_width);
  2904. }
  2905. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  2906. /*
  2907. * Don't send a WDTR back to the
  2908. * target, since we asked first.
  2909. * If the width went higher than our
  2910. * request, reject it.
  2911. */
  2912. if (saved_width > bus_width) {
  2913. reject = TRUE;
  2914. printf("(%s:%c:%d:%d): requested %dBit "
  2915. "transfers. Rejecting...\n",
  2916. ahc_name(ahc), devinfo->channel,
  2917. devinfo->target, devinfo->lun,
  2918. 8 * (0x01 << bus_width));
  2919. bus_width = 0;
  2920. }
  2921. } else {
  2922. /*
  2923. * Send our own WDTR in reply
  2924. */
  2925. if (bootverbose
  2926. && devinfo->role == ROLE_INITIATOR) {
  2927. printf("(%s:%c:%d:%d): Target "
  2928. "Initiated WDTR\n",
  2929. ahc_name(ahc), devinfo->channel,
  2930. devinfo->target, devinfo->lun);
  2931. }
  2932. ahc->msgout_index = 0;
  2933. ahc->msgout_len = 0;
  2934. ahc_construct_wdtr(ahc, devinfo, bus_width);
  2935. ahc->msgout_index = 0;
  2936. response = TRUE;
  2937. sending_reply = TRUE;
  2938. }
  2939. /*
  2940. * After a wide message, we are async, but
  2941. * some devices don't seem to honor this portion
  2942. * of the spec. Force a renegotiation of the
  2943. * sync component of our transfer agreement even
  2944. * if our goal is async. By updating our width
  2945. * after forcing the negotiation, we avoid
  2946. * renegotiating for width.
  2947. */
  2948. ahc_update_neg_request(ahc, devinfo, tstate,
  2949. tinfo, AHC_NEG_ALWAYS);
  2950. ahc_set_width(ahc, devinfo, bus_width,
  2951. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2952. /*paused*/TRUE);
  2953. if (sending_reply == FALSE && reject == FALSE) {
  2954. /*
  2955. * We will always have an SDTR to send.
  2956. */
  2957. ahc->msgout_index = 0;
  2958. ahc->msgout_len = 0;
  2959. ahc_build_transfer_msg(ahc, devinfo);
  2960. ahc->msgout_index = 0;
  2961. response = TRUE;
  2962. }
  2963. done = MSGLOOP_MSGCOMPLETE;
  2964. break;
  2965. }
  2966. case MSG_EXT_PPR:
  2967. {
  2968. struct ahc_syncrate *syncrate;
  2969. u_int period;
  2970. u_int offset;
  2971. u_int bus_width;
  2972. u_int ppr_options;
  2973. u_int saved_width;
  2974. u_int saved_offset;
  2975. u_int saved_ppr_options;
  2976. if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  2977. reject = TRUE;
  2978. break;
  2979. }
  2980. /*
  2981. * Wait until we have all args before validating
  2982. * and acting on this message.
  2983. *
  2984. * Add one to MSG_EXT_PPR_LEN to account for
  2985. * the extended message preamble.
  2986. */
  2987. if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
  2988. break;
  2989. period = ahc->msgin_buf[3];
  2990. offset = ahc->msgin_buf[5];
  2991. bus_width = ahc->msgin_buf[6];
  2992. saved_width = bus_width;
  2993. ppr_options = ahc->msgin_buf[7];
  2994. /*
  2995. * According to the spec, a DT only
  2996. * period factor with no DT option
  2997. * set implies async.
  2998. */
  2999. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  3000. && period == 9)
  3001. offset = 0;
  3002. saved_ppr_options = ppr_options;
  3003. saved_offset = offset;
  3004. /*
  3005. * Mask out any options we don't support
  3006. * on any controller. Transfer options are
  3007. * only available if we are negotiating wide.
  3008. */
  3009. ppr_options &= MSG_EXT_PPR_DT_REQ;
  3010. if (bus_width == 0)
  3011. ppr_options = 0;
  3012. ahc_validate_width(ahc, tinfo, &bus_width,
  3013. devinfo->role);
  3014. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  3015. &ppr_options,
  3016. devinfo->role);
  3017. ahc_validate_offset(ahc, tinfo, syncrate,
  3018. &offset, bus_width,
  3019. devinfo->role);
  3020. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
  3021. /*
  3022. * If we are unable to do any of the
  3023. * requested options (we went too low),
  3024. * then we'll have to reject the message.
  3025. */
  3026. if (saved_width > bus_width
  3027. || saved_offset != offset
  3028. || saved_ppr_options != ppr_options) {
  3029. reject = TRUE;
  3030. period = 0;
  3031. offset = 0;
  3032. bus_width = 0;
  3033. ppr_options = 0;
  3034. syncrate = NULL;
  3035. }
  3036. } else {
  3037. if (devinfo->role != ROLE_TARGET)
  3038. printf("(%s:%c:%d:%d): Target "
  3039. "Initiated PPR\n",
  3040. ahc_name(ahc), devinfo->channel,
  3041. devinfo->target, devinfo->lun);
  3042. else
  3043. printf("(%s:%c:%d:%d): Initiator "
  3044. "Initiated PPR\n",
  3045. ahc_name(ahc), devinfo->channel,
  3046. devinfo->target, devinfo->lun);
  3047. ahc->msgout_index = 0;
  3048. ahc->msgout_len = 0;
  3049. ahc_construct_ppr(ahc, devinfo, period, offset,
  3050. bus_width, ppr_options);
  3051. ahc->msgout_index = 0;
  3052. response = TRUE;
  3053. }
  3054. if (bootverbose) {
  3055. printf("(%s:%c:%d:%d): Received PPR width %x, "
  3056. "period %x, offset %x,options %x\n"
  3057. "\tFiltered to width %x, period %x, "
  3058. "offset %x, options %x\n",
  3059. ahc_name(ahc), devinfo->channel,
  3060. devinfo->target, devinfo->lun,
  3061. saved_width, ahc->msgin_buf[3],
  3062. saved_offset, saved_ppr_options,
  3063. bus_width, period, offset, ppr_options);
  3064. }
  3065. ahc_set_width(ahc, devinfo, bus_width,
  3066. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3067. /*paused*/TRUE);
  3068. ahc_set_syncrate(ahc, devinfo,
  3069. syncrate, period,
  3070. offset, ppr_options,
  3071. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3072. /*paused*/TRUE);
  3073. done = MSGLOOP_MSGCOMPLETE;
  3074. break;
  3075. }
  3076. default:
  3077. /* Unknown extended message. Reject it. */
  3078. reject = TRUE;
  3079. break;
  3080. }
  3081. break;
  3082. }
  3083. #ifdef AHC_TARGET_MODE
  3084. case MSG_BUS_DEV_RESET:
  3085. ahc_handle_devreset(ahc, devinfo,
  3086. CAM_BDR_SENT,
  3087. "Bus Device Reset Received",
  3088. /*verbose_level*/0);
  3089. ahc_restart(ahc);
  3090. done = MSGLOOP_TERMINATED;
  3091. break;
  3092. case MSG_ABORT_TAG:
  3093. case MSG_ABORT:
  3094. case MSG_CLEAR_QUEUE:
  3095. {
  3096. int tag;
  3097. /* Target mode messages */
  3098. if (devinfo->role != ROLE_TARGET) {
  3099. reject = TRUE;
  3100. break;
  3101. }
  3102. tag = SCB_LIST_NULL;
  3103. if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
  3104. tag = ahc_inb(ahc, INITIATOR_TAG);
  3105. ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3106. devinfo->lun, tag, ROLE_TARGET,
  3107. CAM_REQ_ABORTED);
  3108. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3109. if (tstate != NULL) {
  3110. struct ahc_tmode_lstate* lstate;
  3111. lstate = tstate->enabled_luns[devinfo->lun];
  3112. if (lstate != NULL) {
  3113. ahc_queue_lstate_event(ahc, lstate,
  3114. devinfo->our_scsiid,
  3115. ahc->msgin_buf[0],
  3116. /*arg*/tag);
  3117. ahc_send_lstate_events(ahc, lstate);
  3118. }
  3119. }
  3120. ahc_restart(ahc);
  3121. done = MSGLOOP_TERMINATED;
  3122. break;
  3123. }
  3124. #endif
  3125. case MSG_TERM_IO_PROC:
  3126. default:
  3127. reject = TRUE;
  3128. break;
  3129. }
  3130. if (reject) {
  3131. /*
  3132. * Setup to reject the message.
  3133. */
  3134. ahc->msgout_index = 0;
  3135. ahc->msgout_len = 1;
  3136. ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
  3137. done = MSGLOOP_MSGCOMPLETE;
  3138. response = TRUE;
  3139. }
  3140. if (done != MSGLOOP_IN_PROG && !response)
  3141. /* Clear the outgoing message buffer */
  3142. ahc->msgout_len = 0;
  3143. return (done);
  3144. }
  3145. /*
  3146. * Process a message reject message.
  3147. */
  3148. static int
  3149. ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3150. {
  3151. /*
  3152. * What we care about here is if we had an
  3153. * outstanding SDTR or WDTR message for this
  3154. * target. If we did, this is a signal that
  3155. * the target is refusing negotiation.
  3156. */
  3157. struct scb *scb;
  3158. struct ahc_initiator_tinfo *tinfo;
  3159. struct ahc_tmode_tstate *tstate;
  3160. u_int scb_index;
  3161. u_int last_msg;
  3162. int response = 0;
  3163. scb_index = ahc_inb(ahc, SCB_TAG);
  3164. scb = ahc_lookup_scb(ahc, scb_index);
  3165. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
  3166. devinfo->our_scsiid,
  3167. devinfo->target, &tstate);
  3168. /* Might be necessary */
  3169. last_msg = ahc_inb(ahc, LAST_MSG);
  3170. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  3171. /*
  3172. * Target does not support the PPR message.
  3173. * Attempt to negotiate SPI-2 style.
  3174. */
  3175. if (bootverbose) {
  3176. printf("(%s:%c:%d:%d): PPR Rejected. "
  3177. "Trying WDTR/SDTR\n",
  3178. ahc_name(ahc), devinfo->channel,
  3179. devinfo->target, devinfo->lun);
  3180. }
  3181. tinfo->goal.ppr_options = 0;
  3182. tinfo->curr.transport_version = 2;
  3183. tinfo->goal.transport_version = 2;
  3184. ahc->msgout_index = 0;
  3185. ahc->msgout_len = 0;
  3186. ahc_build_transfer_msg(ahc, devinfo);
  3187. ahc->msgout_index = 0;
  3188. response = 1;
  3189. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  3190. /* note 8bit xfers */
  3191. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  3192. "8bit transfers\n", ahc_name(ahc),
  3193. devinfo->channel, devinfo->target, devinfo->lun);
  3194. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3195. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3196. /*paused*/TRUE);
  3197. /*
  3198. * No need to clear the sync rate. If the target
  3199. * did not accept the command, our syncrate is
  3200. * unaffected. If the target started the negotiation,
  3201. * but rejected our response, we already cleared the
  3202. * sync rate before sending our WDTR.
  3203. */
  3204. if (tinfo->goal.offset != tinfo->curr.offset) {
  3205. /* Start the sync negotiation */
  3206. ahc->msgout_index = 0;
  3207. ahc->msgout_len = 0;
  3208. ahc_build_transfer_msg(ahc, devinfo);
  3209. ahc->msgout_index = 0;
  3210. response = 1;
  3211. }
  3212. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  3213. /* note asynch xfers and clear flag */
  3214. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
  3215. /*offset*/0, /*ppr_options*/0,
  3216. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3217. /*paused*/TRUE);
  3218. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  3219. "Using asynchronous transfers\n",
  3220. ahc_name(ahc), devinfo->channel,
  3221. devinfo->target, devinfo->lun);
  3222. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  3223. int tag_type;
  3224. int mask;
  3225. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  3226. if (tag_type == MSG_SIMPLE_TASK) {
  3227. printf("(%s:%c:%d:%d): refuses tagged commands. "
  3228. "Performing non-tagged I/O\n", ahc_name(ahc),
  3229. devinfo->channel, devinfo->target, devinfo->lun);
  3230. ahc_set_tags(ahc, devinfo, AHC_QUEUE_NONE);
  3231. mask = ~0x23;
  3232. } else {
  3233. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  3234. "Performing simple queue tagged I/O only\n",
  3235. ahc_name(ahc), devinfo->channel, devinfo->target,
  3236. devinfo->lun, tag_type == MSG_ORDERED_TASK
  3237. ? "ordered" : "head of queue");
  3238. ahc_set_tags(ahc, devinfo, AHC_QUEUE_BASIC);
  3239. mask = ~0x03;
  3240. }
  3241. /*
  3242. * Resend the identify for this CCB as the target
  3243. * may believe that the selection is invalid otherwise.
  3244. */
  3245. ahc_outb(ahc, SCB_CONTROL,
  3246. ahc_inb(ahc, SCB_CONTROL) & mask);
  3247. scb->hscb->control &= mask;
  3248. ahc_set_transaction_tag(scb, /*enabled*/FALSE,
  3249. /*type*/MSG_SIMPLE_TASK);
  3250. ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
  3251. ahc_assert_atn(ahc);
  3252. /*
  3253. * This transaction is now at the head of
  3254. * the untagged queue for this target.
  3255. */
  3256. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  3257. struct scb_tailq *untagged_q;
  3258. untagged_q =
  3259. &(ahc->untagged_queues[devinfo->target_offset]);
  3260. TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
  3261. scb->flags |= SCB_UNTAGGEDQ;
  3262. }
  3263. ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  3264. scb->hscb->tag);
  3265. /*
  3266. * Requeue all tagged commands for this target
  3267. * currently in our posession so they can be
  3268. * converted to untagged commands.
  3269. */
  3270. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  3271. SCB_GET_CHANNEL(ahc, scb),
  3272. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  3273. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  3274. SEARCH_COMPLETE);
  3275. } else {
  3276. /*
  3277. * Otherwise, we ignore it.
  3278. */
  3279. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  3280. ahc_name(ahc), devinfo->channel, devinfo->target,
  3281. last_msg);
  3282. }
  3283. return (response);
  3284. }
  3285. /*
  3286. * Process an ingnore wide residue message.
  3287. */
  3288. static void
  3289. ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3290. {
  3291. u_int scb_index;
  3292. struct scb *scb;
  3293. scb_index = ahc_inb(ahc, SCB_TAG);
  3294. scb = ahc_lookup_scb(ahc, scb_index);
  3295. /*
  3296. * XXX Actually check data direction in the sequencer?
  3297. * Perhaps add datadir to some spare bits in the hscb?
  3298. */
  3299. if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
  3300. || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
  3301. /*
  3302. * Ignore the message if we haven't
  3303. * seen an appropriate data phase yet.
  3304. */
  3305. } else {
  3306. /*
  3307. * If the residual occurred on the last
  3308. * transfer and the transfer request was
  3309. * expected to end on an odd count, do
  3310. * nothing. Otherwise, subtract a byte
  3311. * and update the residual count accordingly.
  3312. */
  3313. uint32_t sgptr;
  3314. sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3315. if ((sgptr & SG_LIST_NULL) != 0
  3316. && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
  3317. /*
  3318. * If the residual occurred on the last
  3319. * transfer and the transfer request was
  3320. * expected to end on an odd count, do
  3321. * nothing.
  3322. */
  3323. } else {
  3324. struct ahc_dma_seg *sg;
  3325. uint32_t data_cnt;
  3326. uint32_t data_addr;
  3327. uint32_t sglen;
  3328. /* Pull in all of the sgptr */
  3329. sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
  3330. data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
  3331. if ((sgptr & SG_LIST_NULL) != 0) {
  3332. /*
  3333. * The residual data count is not updated
  3334. * for the command run to completion case.
  3335. * Explicitly zero the count.
  3336. */
  3337. data_cnt &= ~AHC_SG_LEN_MASK;
  3338. }
  3339. data_addr = ahc_inl(ahc, SHADDR);
  3340. data_cnt += 1;
  3341. data_addr -= 1;
  3342. sgptr &= SG_PTR_MASK;
  3343. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3344. /*
  3345. * The residual sg ptr points to the next S/G
  3346. * to load so we must go back one.
  3347. */
  3348. sg--;
  3349. sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  3350. if (sg != scb->sg_list
  3351. && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
  3352. sg--;
  3353. sglen = ahc_le32toh(sg->len);
  3354. /*
  3355. * Preserve High Address and SG_LIST bits
  3356. * while setting the count to 1.
  3357. */
  3358. data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
  3359. data_addr = ahc_le32toh(sg->addr)
  3360. + (sglen & AHC_SG_LEN_MASK) - 1;
  3361. /*
  3362. * Increment sg so it points to the
  3363. * "next" sg.
  3364. */
  3365. sg++;
  3366. sgptr = ahc_sg_virt_to_bus(scb, sg);
  3367. }
  3368. ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
  3369. ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
  3370. /*
  3371. * Toggle the "oddness" of the transfer length
  3372. * to handle this mid-transfer ignore wide
  3373. * residue. This ensures that the oddness is
  3374. * correct for subsequent data transfers.
  3375. */
  3376. ahc_outb(ahc, SCB_LUN,
  3377. ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
  3378. }
  3379. }
  3380. }
  3381. /*
  3382. * Reinitialize the data pointers for the active transfer
  3383. * based on its current residual.
  3384. */
  3385. static void
  3386. ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
  3387. {
  3388. struct scb *scb;
  3389. struct ahc_dma_seg *sg;
  3390. u_int scb_index;
  3391. uint32_t sgptr;
  3392. uint32_t resid;
  3393. uint32_t dataptr;
  3394. scb_index = ahc_inb(ahc, SCB_TAG);
  3395. scb = ahc_lookup_scb(ahc, scb_index);
  3396. sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3397. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3398. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
  3399. | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3400. sgptr &= SG_PTR_MASK;
  3401. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3402. /* The residual sg_ptr always points to the next sg */
  3403. sg--;
  3404. resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
  3405. | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
  3406. | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
  3407. dataptr = ahc_le32toh(sg->addr)
  3408. + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
  3409. - resid;
  3410. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  3411. u_int dscommand1;
  3412. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  3413. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  3414. ahc_outb(ahc, HADDR,
  3415. (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
  3416. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  3417. }
  3418. ahc_outb(ahc, HADDR + 3, dataptr >> 24);
  3419. ahc_outb(ahc, HADDR + 2, dataptr >> 16);
  3420. ahc_outb(ahc, HADDR + 1, dataptr >> 8);
  3421. ahc_outb(ahc, HADDR, dataptr);
  3422. ahc_outb(ahc, HCNT + 2, resid >> 16);
  3423. ahc_outb(ahc, HCNT + 1, resid >> 8);
  3424. ahc_outb(ahc, HCNT, resid);
  3425. if ((ahc->features & AHC_ULTRA2) == 0) {
  3426. ahc_outb(ahc, STCNT + 2, resid >> 16);
  3427. ahc_outb(ahc, STCNT + 1, resid >> 8);
  3428. ahc_outb(ahc, STCNT, resid);
  3429. }
  3430. }
  3431. /*
  3432. * Handle the effects of issuing a bus device reset message.
  3433. */
  3434. static void
  3435. ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3436. cam_status status, char *message, int verbose_level)
  3437. {
  3438. #ifdef AHC_TARGET_MODE
  3439. struct ahc_tmode_tstate* tstate;
  3440. u_int lun;
  3441. #endif
  3442. int found;
  3443. found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3444. CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
  3445. status);
  3446. #ifdef AHC_TARGET_MODE
  3447. /*
  3448. * Send an immediate notify ccb to all target mord peripheral
  3449. * drivers affected by this action.
  3450. */
  3451. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3452. if (tstate != NULL) {
  3453. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  3454. struct ahc_tmode_lstate* lstate;
  3455. lstate = tstate->enabled_luns[lun];
  3456. if (lstate == NULL)
  3457. continue;
  3458. ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
  3459. MSG_BUS_DEV_RESET, /*arg*/0);
  3460. ahc_send_lstate_events(ahc, lstate);
  3461. }
  3462. }
  3463. #endif
  3464. /*
  3465. * Go back to async/narrow transfers and renegotiate.
  3466. */
  3467. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3468. AHC_TRANS_CUR, /*paused*/TRUE);
  3469. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
  3470. /*period*/0, /*offset*/0, /*ppr_options*/0,
  3471. AHC_TRANS_CUR, /*paused*/TRUE);
  3472. if (status != CAM_SEL_TIMEOUT)
  3473. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  3474. CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
  3475. if (message != NULL
  3476. && (verbose_level <= bootverbose))
  3477. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
  3478. message, devinfo->channel, devinfo->target, found);
  3479. }
  3480. #ifdef AHC_TARGET_MODE
  3481. static void
  3482. ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3483. struct scb *scb)
  3484. {
  3485. /*
  3486. * To facilitate adding multiple messages together,
  3487. * each routine should increment the index and len
  3488. * variables instead of setting them explicitly.
  3489. */
  3490. ahc->msgout_index = 0;
  3491. ahc->msgout_len = 0;
  3492. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  3493. ahc_build_transfer_msg(ahc, devinfo);
  3494. else
  3495. panic("ahc_intr: AWAITING target message with no message");
  3496. ahc->msgout_index = 0;
  3497. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  3498. }
  3499. #endif
  3500. /**************************** Initialization **********************************/
  3501. /*
  3502. * Allocate a controller structure for a new device
  3503. * and perform initial initializion.
  3504. */
  3505. struct ahc_softc *
  3506. ahc_alloc(void *platform_arg, char *name)
  3507. {
  3508. struct ahc_softc *ahc;
  3509. int i;
  3510. #ifndef __FreeBSD__
  3511. ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
  3512. if (!ahc) {
  3513. printf("aic7xxx: cannot malloc softc!\n");
  3514. free(name, M_DEVBUF);
  3515. return NULL;
  3516. }
  3517. #else
  3518. ahc = device_get_softc((device_t)platform_arg);
  3519. #endif
  3520. memset(ahc, 0, sizeof(*ahc));
  3521. ahc->seep_config = malloc(sizeof(*ahc->seep_config),
  3522. M_DEVBUF, M_NOWAIT);
  3523. if (ahc->seep_config == NULL) {
  3524. #ifndef __FreeBSD__
  3525. free(ahc, M_DEVBUF);
  3526. #endif
  3527. free(name, M_DEVBUF);
  3528. return (NULL);
  3529. }
  3530. LIST_INIT(&ahc->pending_scbs);
  3531. /* We don't know our unit number until the OSM sets it */
  3532. ahc->name = name;
  3533. ahc->unit = -1;
  3534. ahc->description = NULL;
  3535. ahc->channel = 'A';
  3536. ahc->channel_b = 'B';
  3537. ahc->chip = AHC_NONE;
  3538. ahc->features = AHC_FENONE;
  3539. ahc->bugs = AHC_BUGNONE;
  3540. ahc->flags = AHC_FNONE;
  3541. /*
  3542. * Default to all error reporting enabled with the
  3543. * sequencer operating at its fastest speed.
  3544. * The bus attach code may modify this.
  3545. */
  3546. ahc->seqctl = FASTMODE;
  3547. for (i = 0; i < AHC_NUM_TARGETS; i++)
  3548. TAILQ_INIT(&ahc->untagged_queues[i]);
  3549. if (ahc_platform_alloc(ahc, platform_arg) != 0) {
  3550. ahc_free(ahc);
  3551. ahc = NULL;
  3552. }
  3553. return (ahc);
  3554. }
  3555. int
  3556. ahc_softc_init(struct ahc_softc *ahc)
  3557. {
  3558. /* The IRQMS bit is only valid on VL and EISA chips */
  3559. if ((ahc->chip & AHC_PCI) == 0)
  3560. ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
  3561. else
  3562. ahc->unpause = 0;
  3563. ahc->pause = ahc->unpause | PAUSE;
  3564. /* XXX The shared scb data stuff should be deprecated */
  3565. if (ahc->scb_data == NULL) {
  3566. ahc->scb_data = malloc(sizeof(*ahc->scb_data),
  3567. M_DEVBUF, M_NOWAIT);
  3568. if (ahc->scb_data == NULL)
  3569. return (ENOMEM);
  3570. memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
  3571. }
  3572. return (0);
  3573. }
  3574. void
  3575. ahc_set_unit(struct ahc_softc *ahc, int unit)
  3576. {
  3577. ahc->unit = unit;
  3578. }
  3579. void
  3580. ahc_set_name(struct ahc_softc *ahc, char *name)
  3581. {
  3582. if (ahc->name != NULL)
  3583. free(ahc->name, M_DEVBUF);
  3584. ahc->name = name;
  3585. }
  3586. void
  3587. ahc_free(struct ahc_softc *ahc)
  3588. {
  3589. int i;
  3590. switch (ahc->init_level) {
  3591. default:
  3592. case 5:
  3593. ahc_shutdown(ahc);
  3594. /* FALLTHROUGH */
  3595. case 4:
  3596. ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
  3597. ahc->shared_data_dmamap);
  3598. /* FALLTHROUGH */
  3599. case 3:
  3600. ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
  3601. ahc->shared_data_dmamap);
  3602. ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
  3603. ahc->shared_data_dmamap);
  3604. /* FALLTHROUGH */
  3605. case 2:
  3606. ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
  3607. case 1:
  3608. #ifndef __linux__
  3609. ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
  3610. #endif
  3611. break;
  3612. case 0:
  3613. break;
  3614. }
  3615. #ifndef __linux__
  3616. ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
  3617. #endif
  3618. ahc_platform_free(ahc);
  3619. ahc_fini_scbdata(ahc);
  3620. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  3621. struct ahc_tmode_tstate *tstate;
  3622. tstate = ahc->enabled_targets[i];
  3623. if (tstate != NULL) {
  3624. #ifdef AHC_TARGET_MODE
  3625. int j;
  3626. for (j = 0; j < AHC_NUM_LUNS; j++) {
  3627. struct ahc_tmode_lstate *lstate;
  3628. lstate = tstate->enabled_luns[j];
  3629. if (lstate != NULL) {
  3630. xpt_free_path(lstate->path);
  3631. free(lstate, M_DEVBUF);
  3632. }
  3633. }
  3634. #endif
  3635. free(tstate, M_DEVBUF);
  3636. }
  3637. }
  3638. #ifdef AHC_TARGET_MODE
  3639. if (ahc->black_hole != NULL) {
  3640. xpt_free_path(ahc->black_hole->path);
  3641. free(ahc->black_hole, M_DEVBUF);
  3642. }
  3643. #endif
  3644. if (ahc->name != NULL)
  3645. free(ahc->name, M_DEVBUF);
  3646. if (ahc->seep_config != NULL)
  3647. free(ahc->seep_config, M_DEVBUF);
  3648. #ifndef __FreeBSD__
  3649. free(ahc, M_DEVBUF);
  3650. #endif
  3651. return;
  3652. }
  3653. void
  3654. ahc_shutdown(void *arg)
  3655. {
  3656. struct ahc_softc *ahc;
  3657. int i;
  3658. ahc = (struct ahc_softc *)arg;
  3659. /* This will reset most registers to 0, but not all */
  3660. ahc_reset(ahc, /*reinit*/FALSE);
  3661. ahc_outb(ahc, SCSISEQ, 0);
  3662. ahc_outb(ahc, SXFRCTL0, 0);
  3663. ahc_outb(ahc, DSPCISTATUS, 0);
  3664. for (i = TARG_SCSIRATE; i < SCSICONF; i++)
  3665. ahc_outb(ahc, i, 0);
  3666. }
  3667. /*
  3668. * Reset the controller and record some information about it
  3669. * that is only available just after a reset. If "reinit" is
  3670. * non-zero, this reset occured after initial configuration
  3671. * and the caller requests that the chip be fully reinitialized
  3672. * to a runable state. Chip interrupts are *not* enabled after
  3673. * a reinitialization. The caller must enable interrupts via
  3674. * ahc_intr_enable().
  3675. */
  3676. int
  3677. ahc_reset(struct ahc_softc *ahc, int reinit)
  3678. {
  3679. u_int sblkctl;
  3680. u_int sxfrctl1_a, sxfrctl1_b;
  3681. int error;
  3682. int wait;
  3683. /*
  3684. * Preserve the value of the SXFRCTL1 register for all channels.
  3685. * It contains settings that affect termination and we don't want
  3686. * to disturb the integrity of the bus.
  3687. */
  3688. ahc_pause(ahc);
  3689. sxfrctl1_b = 0;
  3690. if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
  3691. u_int sblkctl;
  3692. /*
  3693. * Save channel B's settings in case this chip
  3694. * is setup for TWIN channel operation.
  3695. */
  3696. sblkctl = ahc_inb(ahc, SBLKCTL);
  3697. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3698. sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
  3699. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3700. }
  3701. sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
  3702. ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
  3703. /*
  3704. * Ensure that the reset has finished. We delay 1000us
  3705. * prior to reading the register to make sure the chip
  3706. * has sufficiently completed its reset to handle register
  3707. * accesses.
  3708. */
  3709. wait = 1000;
  3710. do {
  3711. ahc_delay(1000);
  3712. } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
  3713. if (wait == 0) {
  3714. printf("%s: WARNING - Failed chip reset! "
  3715. "Trying to initialize anyway.\n", ahc_name(ahc));
  3716. }
  3717. ahc_outb(ahc, HCNTRL, ahc->pause);
  3718. /* Determine channel configuration */
  3719. sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
  3720. /* No Twin Channel PCI cards */
  3721. if ((ahc->chip & AHC_PCI) != 0)
  3722. sblkctl &= ~SELBUSB;
  3723. switch (sblkctl) {
  3724. case 0:
  3725. /* Single Narrow Channel */
  3726. break;
  3727. case 2:
  3728. /* Wide Channel */
  3729. ahc->features |= AHC_WIDE;
  3730. break;
  3731. case 8:
  3732. /* Twin Channel */
  3733. ahc->features |= AHC_TWIN;
  3734. break;
  3735. default:
  3736. printf(" Unsupported adapter type. Ignoring\n");
  3737. return(-1);
  3738. }
  3739. /*
  3740. * Reload sxfrctl1.
  3741. *
  3742. * We must always initialize STPWEN to 1 before we
  3743. * restore the saved values. STPWEN is initialized
  3744. * to a tri-state condition which can only be cleared
  3745. * by turning it on.
  3746. */
  3747. if ((ahc->features & AHC_TWIN) != 0) {
  3748. u_int sblkctl;
  3749. sblkctl = ahc_inb(ahc, SBLKCTL);
  3750. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3751. ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
  3752. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3753. }
  3754. ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
  3755. error = 0;
  3756. if (reinit != 0)
  3757. /*
  3758. * If a recovery action has forced a chip reset,
  3759. * re-initialize the chip to our liking.
  3760. */
  3761. error = ahc->bus_chip_init(ahc);
  3762. #ifdef AHC_DUMP_SEQ
  3763. else
  3764. ahc_dumpseq(ahc);
  3765. #endif
  3766. return (error);
  3767. }
  3768. /*
  3769. * Determine the number of SCBs available on the controller
  3770. */
  3771. int
  3772. ahc_probe_scbs(struct ahc_softc *ahc) {
  3773. int i;
  3774. for (i = 0; i < AHC_SCB_MAX; i++) {
  3775. ahc_outb(ahc, SCBPTR, i);
  3776. ahc_outb(ahc, SCB_BASE, i);
  3777. if (ahc_inb(ahc, SCB_BASE) != i)
  3778. break;
  3779. ahc_outb(ahc, SCBPTR, 0);
  3780. if (ahc_inb(ahc, SCB_BASE) != 0)
  3781. break;
  3782. }
  3783. return (i);
  3784. }
  3785. static void
  3786. ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  3787. {
  3788. dma_addr_t *baddr;
  3789. baddr = (dma_addr_t *)arg;
  3790. *baddr = segs->ds_addr;
  3791. }
  3792. static void
  3793. ahc_build_free_scb_list(struct ahc_softc *ahc)
  3794. {
  3795. int scbsize;
  3796. int i;
  3797. scbsize = 32;
  3798. if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
  3799. scbsize = 64;
  3800. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  3801. int j;
  3802. ahc_outb(ahc, SCBPTR, i);
  3803. /*
  3804. * Touch all SCB bytes to avoid parity errors
  3805. * should one of our debugging routines read
  3806. * an otherwise uninitiatlized byte.
  3807. */
  3808. for (j = 0; j < scbsize; j++)
  3809. ahc_outb(ahc, SCB_BASE+j, 0xFF);
  3810. /* Clear the control byte. */
  3811. ahc_outb(ahc, SCB_CONTROL, 0);
  3812. /* Set the next pointer */
  3813. if ((ahc->flags & AHC_PAGESCBS) != 0)
  3814. ahc_outb(ahc, SCB_NEXT, i+1);
  3815. else
  3816. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3817. /* Make the tag number, SCSIID, and lun invalid */
  3818. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  3819. ahc_outb(ahc, SCB_SCSIID, 0xFF);
  3820. ahc_outb(ahc, SCB_LUN, 0xFF);
  3821. }
  3822. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  3823. /* SCB 0 heads the free list. */
  3824. ahc_outb(ahc, FREE_SCBH, 0);
  3825. } else {
  3826. /* No free list. */
  3827. ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
  3828. }
  3829. /* Make sure that the last SCB terminates the free list */
  3830. ahc_outb(ahc, SCBPTR, i-1);
  3831. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3832. }
  3833. static int
  3834. ahc_init_scbdata(struct ahc_softc *ahc)
  3835. {
  3836. struct scb_data *scb_data;
  3837. scb_data = ahc->scb_data;
  3838. SLIST_INIT(&scb_data->free_scbs);
  3839. SLIST_INIT(&scb_data->sg_maps);
  3840. /* Allocate SCB resources */
  3841. scb_data->scbarray =
  3842. (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
  3843. M_DEVBUF, M_NOWAIT);
  3844. if (scb_data->scbarray == NULL)
  3845. return (ENOMEM);
  3846. memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
  3847. /* Determine the number of hardware SCBs and initialize them */
  3848. scb_data->maxhscbs = ahc_probe_scbs(ahc);
  3849. if (ahc->scb_data->maxhscbs == 0) {
  3850. printf("%s: No SCB space found\n", ahc_name(ahc));
  3851. return (ENXIO);
  3852. }
  3853. /*
  3854. * Create our DMA tags. These tags define the kinds of device
  3855. * accessible memory allocations and memory mappings we will
  3856. * need to perform during normal operation.
  3857. *
  3858. * Unless we need to further restrict the allocation, we rely
  3859. * on the restrictions of the parent dmat, hence the common
  3860. * use of MAXADDR and MAXSIZE.
  3861. */
  3862. /* DMA tag for our hardware scb structures */
  3863. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3864. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3865. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3866. /*highaddr*/BUS_SPACE_MAXADDR,
  3867. /*filter*/NULL, /*filterarg*/NULL,
  3868. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3869. /*nsegments*/1,
  3870. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3871. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  3872. goto error_exit;
  3873. }
  3874. scb_data->init_level++;
  3875. /* Allocation for our hscbs */
  3876. if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
  3877. (void **)&scb_data->hscbs,
  3878. BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
  3879. goto error_exit;
  3880. }
  3881. scb_data->init_level++;
  3882. /* And permanently map them */
  3883. ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
  3884. scb_data->hscbs,
  3885. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3886. ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
  3887. scb_data->init_level++;
  3888. /* DMA tag for our sense buffers */
  3889. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3890. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3891. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3892. /*highaddr*/BUS_SPACE_MAXADDR,
  3893. /*filter*/NULL, /*filterarg*/NULL,
  3894. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3895. /*nsegments*/1,
  3896. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3897. /*flags*/0, &scb_data->sense_dmat) != 0) {
  3898. goto error_exit;
  3899. }
  3900. scb_data->init_level++;
  3901. /* Allocate them */
  3902. if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
  3903. (void **)&scb_data->sense,
  3904. BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
  3905. goto error_exit;
  3906. }
  3907. scb_data->init_level++;
  3908. /* And permanently map them */
  3909. ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
  3910. scb_data->sense,
  3911. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3912. ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
  3913. scb_data->init_level++;
  3914. /* DMA tag for our S/G structures. We allocate in page sized chunks */
  3915. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
  3916. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3917. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3918. /*highaddr*/BUS_SPACE_MAXADDR,
  3919. /*filter*/NULL, /*filterarg*/NULL,
  3920. PAGE_SIZE, /*nsegments*/1,
  3921. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3922. /*flags*/0, &scb_data->sg_dmat) != 0) {
  3923. goto error_exit;
  3924. }
  3925. scb_data->init_level++;
  3926. /* Perform initial CCB allocation */
  3927. memset(scb_data->hscbs, 0,
  3928. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
  3929. ahc_alloc_scbs(ahc);
  3930. if (scb_data->numscbs == 0) {
  3931. printf("%s: ahc_init_scbdata - "
  3932. "Unable to allocate initial scbs\n",
  3933. ahc_name(ahc));
  3934. goto error_exit;
  3935. }
  3936. /*
  3937. * Reserve the next queued SCB.
  3938. */
  3939. ahc->next_queued_scb = ahc_get_scb(ahc);
  3940. /*
  3941. * Note that we were successfull
  3942. */
  3943. return (0);
  3944. error_exit:
  3945. return (ENOMEM);
  3946. }
  3947. static void
  3948. ahc_fini_scbdata(struct ahc_softc *ahc)
  3949. {
  3950. struct scb_data *scb_data;
  3951. scb_data = ahc->scb_data;
  3952. if (scb_data == NULL)
  3953. return;
  3954. switch (scb_data->init_level) {
  3955. default:
  3956. case 7:
  3957. {
  3958. struct sg_map_node *sg_map;
  3959. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
  3960. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  3961. ahc_dmamap_unload(ahc, scb_data->sg_dmat,
  3962. sg_map->sg_dmamap);
  3963. ahc_dmamem_free(ahc, scb_data->sg_dmat,
  3964. sg_map->sg_vaddr,
  3965. sg_map->sg_dmamap);
  3966. free(sg_map, M_DEVBUF);
  3967. }
  3968. ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
  3969. }
  3970. case 6:
  3971. ahc_dmamap_unload(ahc, scb_data->sense_dmat,
  3972. scb_data->sense_dmamap);
  3973. case 5:
  3974. ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
  3975. scb_data->sense_dmamap);
  3976. ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
  3977. scb_data->sense_dmamap);
  3978. case 4:
  3979. ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
  3980. case 3:
  3981. ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
  3982. scb_data->hscb_dmamap);
  3983. case 2:
  3984. ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
  3985. scb_data->hscb_dmamap);
  3986. ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
  3987. scb_data->hscb_dmamap);
  3988. case 1:
  3989. ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
  3990. break;
  3991. case 0:
  3992. break;
  3993. }
  3994. if (scb_data->scbarray != NULL)
  3995. free(scb_data->scbarray, M_DEVBUF);
  3996. }
  3997. void
  3998. ahc_alloc_scbs(struct ahc_softc *ahc)
  3999. {
  4000. struct scb_data *scb_data;
  4001. struct scb *next_scb;
  4002. struct sg_map_node *sg_map;
  4003. dma_addr_t physaddr;
  4004. struct ahc_dma_seg *segs;
  4005. int newcount;
  4006. int i;
  4007. scb_data = ahc->scb_data;
  4008. if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
  4009. /* Can't allocate any more */
  4010. return;
  4011. next_scb = &scb_data->scbarray[scb_data->numscbs];
  4012. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  4013. if (sg_map == NULL)
  4014. return;
  4015. /* Allocate S/G space for the next batch of SCBS */
  4016. if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
  4017. (void **)&sg_map->sg_vaddr,
  4018. BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  4019. free(sg_map, M_DEVBUF);
  4020. return;
  4021. }
  4022. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  4023. ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
  4024. sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
  4025. &sg_map->sg_physaddr, /*flags*/0);
  4026. segs = sg_map->sg_vaddr;
  4027. physaddr = sg_map->sg_physaddr;
  4028. newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
  4029. newcount = MIN(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
  4030. for (i = 0; i < newcount; i++) {
  4031. struct scb_platform_data *pdata;
  4032. #ifndef __linux__
  4033. int error;
  4034. #endif
  4035. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  4036. M_DEVBUF, M_NOWAIT);
  4037. if (pdata == NULL)
  4038. break;
  4039. next_scb->platform_data = pdata;
  4040. next_scb->sg_map = sg_map;
  4041. next_scb->sg_list = segs;
  4042. /*
  4043. * The sequencer always starts with the second entry.
  4044. * The first entry is embedded in the scb.
  4045. */
  4046. next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
  4047. next_scb->ahc_softc = ahc;
  4048. next_scb->flags = SCB_FREE;
  4049. #ifndef __linux__
  4050. error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
  4051. &next_scb->dmamap);
  4052. if (error != 0)
  4053. break;
  4054. #endif
  4055. next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
  4056. next_scb->hscb->tag = ahc->scb_data->numscbs;
  4057. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
  4058. next_scb, links.sle);
  4059. segs += AHC_NSEG;
  4060. physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
  4061. next_scb++;
  4062. ahc->scb_data->numscbs++;
  4063. }
  4064. }
  4065. void
  4066. ahc_controller_info(struct ahc_softc *ahc, char *buf)
  4067. {
  4068. int len;
  4069. len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
  4070. buf += len;
  4071. if ((ahc->features & AHC_TWIN) != 0)
  4072. len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
  4073. "B SCSI Id=%d, primary %c, ",
  4074. ahc->our_id, ahc->our_id_b,
  4075. (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
  4076. else {
  4077. const char *speed;
  4078. const char *type;
  4079. speed = "";
  4080. if ((ahc->features & AHC_ULTRA) != 0) {
  4081. speed = "Ultra ";
  4082. } else if ((ahc->features & AHC_DT) != 0) {
  4083. speed = "Ultra160 ";
  4084. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  4085. speed = "Ultra2 ";
  4086. }
  4087. if ((ahc->features & AHC_WIDE) != 0) {
  4088. type = "Wide";
  4089. } else {
  4090. type = "Single";
  4091. }
  4092. len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
  4093. speed, type, ahc->channel, ahc->our_id);
  4094. }
  4095. buf += len;
  4096. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4097. sprintf(buf, "%d/%d SCBs",
  4098. ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
  4099. else
  4100. sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
  4101. }
  4102. int
  4103. ahc_chip_init(struct ahc_softc *ahc)
  4104. {
  4105. int term;
  4106. int error;
  4107. u_int i;
  4108. u_int scsi_conf;
  4109. u_int scsiseq_template;
  4110. uint32_t physaddr;
  4111. ahc_outb(ahc, SEQ_FLAGS, 0);
  4112. ahc_outb(ahc, SEQ_FLAGS2, 0);
  4113. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
  4114. if (ahc->features & AHC_TWIN) {
  4115. /*
  4116. * Setup Channel B first.
  4117. */
  4118. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
  4119. term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
  4120. ahc_outb(ahc, SCSIID, ahc->our_id_b);
  4121. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4122. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4123. |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
  4124. if ((ahc->features & AHC_ULTRA2) != 0)
  4125. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4126. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4127. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4128. /* Select Channel A */
  4129. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
  4130. }
  4131. term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
  4132. if ((ahc->features & AHC_ULTRA2) != 0)
  4133. ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
  4134. else
  4135. ahc_outb(ahc, SCSIID, ahc->our_id);
  4136. scsi_conf = ahc_inb(ahc, SCSICONF);
  4137. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4138. |term|ahc->seltime
  4139. |ENSTIMER|ACTNEGEN);
  4140. if ((ahc->features & AHC_ULTRA2) != 0)
  4141. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4142. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4143. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4144. /* There are no untagged SCBs active yet. */
  4145. for (i = 0; i < 16; i++) {
  4146. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
  4147. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4148. int lun;
  4149. /*
  4150. * The SCB based BTT allows an entry per
  4151. * target and lun pair.
  4152. */
  4153. for (lun = 1; lun < AHC_NUM_LUNS; lun++)
  4154. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
  4155. }
  4156. }
  4157. /* All of our queues are empty */
  4158. for (i = 0; i < 256; i++)
  4159. ahc->qoutfifo[i] = SCB_LIST_NULL;
  4160. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
  4161. for (i = 0; i < 256; i++)
  4162. ahc->qinfifo[i] = SCB_LIST_NULL;
  4163. if ((ahc->features & AHC_MULTI_TID) != 0) {
  4164. ahc_outb(ahc, TARGID, 0);
  4165. ahc_outb(ahc, TARGID + 1, 0);
  4166. }
  4167. /*
  4168. * Tell the sequencer where it can find our arrays in memory.
  4169. */
  4170. physaddr = ahc->scb_data->hscb_busaddr;
  4171. ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
  4172. ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
  4173. ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
  4174. ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
  4175. physaddr = ahc->shared_data_busaddr;
  4176. ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
  4177. ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
  4178. ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
  4179. ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
  4180. /*
  4181. * Initialize the group code to command length table.
  4182. * This overrides the values in TARG_SCSIRATE, so only
  4183. * setup the table after we have processed that information.
  4184. */
  4185. ahc_outb(ahc, CMDSIZE_TABLE, 5);
  4186. ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
  4187. ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
  4188. ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
  4189. ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
  4190. ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
  4191. ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
  4192. ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
  4193. if ((ahc->features & AHC_HS_MAILBOX) != 0)
  4194. ahc_outb(ahc, HS_MAILBOX, 0);
  4195. /* Tell the sequencer of our initial queue positions */
  4196. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4197. ahc->tqinfifonext = 1;
  4198. ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
  4199. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  4200. }
  4201. ahc->qinfifonext = 0;
  4202. ahc->qoutfifonext = 0;
  4203. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4204. ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
  4205. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4206. ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
  4207. ahc_outb(ahc, SDSCB_QOFF, 0);
  4208. } else {
  4209. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4210. ahc_outb(ahc, QINPOS, ahc->qinfifonext);
  4211. ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
  4212. }
  4213. /* We don't have any waiting selections */
  4214. ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
  4215. /* Our disconnection list is empty too */
  4216. ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
  4217. /* Message out buffer starts empty */
  4218. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  4219. /*
  4220. * Setup the allowed SCSI Sequences based on operational mode.
  4221. * If we are a target, we'll enalbe select in operations once
  4222. * we've had a lun enabled.
  4223. */
  4224. scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
  4225. if ((ahc->flags & AHC_INITIATORROLE) != 0)
  4226. scsiseq_template |= ENRSELI;
  4227. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
  4228. /* Initialize our list of free SCBs. */
  4229. ahc_build_free_scb_list(ahc);
  4230. /*
  4231. * Tell the sequencer which SCB will be the next one it receives.
  4232. */
  4233. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4234. /*
  4235. * Load the Sequencer program and Enable the adapter
  4236. * in "fast" mode.
  4237. */
  4238. if (bootverbose)
  4239. printf("%s: Downloading Sequencer Program...",
  4240. ahc_name(ahc));
  4241. error = ahc_loadseq(ahc);
  4242. if (error != 0)
  4243. return (error);
  4244. if ((ahc->features & AHC_ULTRA2) != 0) {
  4245. int wait;
  4246. /*
  4247. * Wait for up to 500ms for our transceivers
  4248. * to settle. If the adapter does not have
  4249. * a cable attached, the transceivers may
  4250. * never settle, so don't complain if we
  4251. * fail here.
  4252. */
  4253. for (wait = 5000;
  4254. (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  4255. wait--)
  4256. ahc_delay(100);
  4257. }
  4258. ahc_restart(ahc);
  4259. return (0);
  4260. }
  4261. /*
  4262. * Start the board, ready for normal operation
  4263. */
  4264. int
  4265. ahc_init(struct ahc_softc *ahc)
  4266. {
  4267. int max_targ;
  4268. u_int i;
  4269. u_int scsi_conf;
  4270. u_int ultraenb;
  4271. u_int discenable;
  4272. u_int tagenable;
  4273. size_t driver_data_size;
  4274. #ifdef AHC_DEBUG
  4275. if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
  4276. ahc->flags |= AHC_SEQUENCER_DEBUG;
  4277. #endif
  4278. #ifdef AHC_PRINT_SRAM
  4279. printf("Scratch Ram:");
  4280. for (i = 0x20; i < 0x5f; i++) {
  4281. if (((i % 8) == 0) && (i != 0)) {
  4282. printf ("\n ");
  4283. }
  4284. printf (" 0x%x", ahc_inb(ahc, i));
  4285. }
  4286. if ((ahc->features & AHC_MORE_SRAM) != 0) {
  4287. for (i = 0x70; i < 0x7f; i++) {
  4288. if (((i % 8) == 0) && (i != 0)) {
  4289. printf ("\n ");
  4290. }
  4291. printf (" 0x%x", ahc_inb(ahc, i));
  4292. }
  4293. }
  4294. printf ("\n");
  4295. /*
  4296. * Reading uninitialized scratch ram may
  4297. * generate parity errors.
  4298. */
  4299. ahc_outb(ahc, CLRINT, CLRPARERR);
  4300. ahc_outb(ahc, CLRINT, CLRBRKADRINT);
  4301. #endif
  4302. max_targ = 15;
  4303. /*
  4304. * Assume we have a board at this stage and it has been reset.
  4305. */
  4306. if ((ahc->flags & AHC_USEDEFAULTS) != 0)
  4307. ahc->our_id = ahc->our_id_b = 7;
  4308. /*
  4309. * Default to allowing initiator operations.
  4310. */
  4311. ahc->flags |= AHC_INITIATORROLE;
  4312. /*
  4313. * Only allow target mode features if this unit has them enabled.
  4314. */
  4315. if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
  4316. ahc->features &= ~AHC_TARGETMODE;
  4317. #ifndef __linux__
  4318. /* DMA tag for mapping buffers into device visible space. */
  4319. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4320. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4321. /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
  4322. ? (dma_addr_t)0x7FFFFFFFFFULL
  4323. : BUS_SPACE_MAXADDR_32BIT,
  4324. /*highaddr*/BUS_SPACE_MAXADDR,
  4325. /*filter*/NULL, /*filterarg*/NULL,
  4326. /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
  4327. /*nsegments*/AHC_NSEG,
  4328. /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
  4329. /*flags*/BUS_DMA_ALLOCNOW,
  4330. &ahc->buffer_dmat) != 0) {
  4331. return (ENOMEM);
  4332. }
  4333. #endif
  4334. ahc->init_level++;
  4335. /*
  4336. * DMA tag for our command fifos and other data in system memory
  4337. * the card's sequencer must be able to access. For initiator
  4338. * roles, we need to allocate space for the qinfifo and qoutfifo.
  4339. * The qinfifo and qoutfifo are composed of 256 1 byte elements.
  4340. * When providing for the target mode role, we must additionally
  4341. * provide space for the incoming target command fifo and an extra
  4342. * byte to deal with a dma bug in some chip versions.
  4343. */
  4344. driver_data_size = 2 * 256 * sizeof(uint8_t);
  4345. if ((ahc->features & AHC_TARGETMODE) != 0)
  4346. driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
  4347. + /*DMA WideOdd Bug Buffer*/1;
  4348. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4349. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4350. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4351. /*highaddr*/BUS_SPACE_MAXADDR,
  4352. /*filter*/NULL, /*filterarg*/NULL,
  4353. driver_data_size,
  4354. /*nsegments*/1,
  4355. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4356. /*flags*/0, &ahc->shared_data_dmat) != 0) {
  4357. return (ENOMEM);
  4358. }
  4359. ahc->init_level++;
  4360. /* Allocation of driver data */
  4361. if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
  4362. (void **)&ahc->qoutfifo,
  4363. BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
  4364. return (ENOMEM);
  4365. }
  4366. ahc->init_level++;
  4367. /* And permanently map it in */
  4368. ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  4369. ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
  4370. &ahc->shared_data_busaddr, /*flags*/0);
  4371. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4372. ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
  4373. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
  4374. ahc->dma_bug_buf = ahc->shared_data_busaddr
  4375. + driver_data_size - 1;
  4376. /* All target command blocks start out invalid. */
  4377. for (i = 0; i < AHC_TMODE_CMDS; i++)
  4378. ahc->targetcmds[i].cmd_valid = 0;
  4379. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
  4380. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
  4381. }
  4382. ahc->qinfifo = &ahc->qoutfifo[256];
  4383. ahc->init_level++;
  4384. /* Allocate SCB data now that buffer_dmat is initialized */
  4385. if (ahc->scb_data->maxhscbs == 0)
  4386. if (ahc_init_scbdata(ahc) != 0)
  4387. return (ENOMEM);
  4388. /*
  4389. * Allocate a tstate to house information for our
  4390. * initiator presence on the bus as well as the user
  4391. * data for any target mode initiator.
  4392. */
  4393. if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
  4394. printf("%s: unable to allocate ahc_tmode_tstate. "
  4395. "Failing attach\n", ahc_name(ahc));
  4396. return (ENOMEM);
  4397. }
  4398. if ((ahc->features & AHC_TWIN) != 0) {
  4399. if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
  4400. printf("%s: unable to allocate ahc_tmode_tstate. "
  4401. "Failing attach\n", ahc_name(ahc));
  4402. return (ENOMEM);
  4403. }
  4404. }
  4405. if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
  4406. ahc->flags |= AHC_PAGESCBS;
  4407. } else {
  4408. ahc->flags &= ~AHC_PAGESCBS;
  4409. }
  4410. #ifdef AHC_DEBUG
  4411. if (ahc_debug & AHC_SHOW_MISC) {
  4412. printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
  4413. "ahc_dma %u bytes\n",
  4414. ahc_name(ahc),
  4415. (u_int)sizeof(struct hardware_scb),
  4416. (u_int)sizeof(struct scb),
  4417. (u_int)sizeof(struct ahc_dma_seg));
  4418. }
  4419. #endif /* AHC_DEBUG */
  4420. /*
  4421. * Look at the information that board initialization or
  4422. * the board bios has left us.
  4423. */
  4424. if (ahc->features & AHC_TWIN) {
  4425. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4426. if ((scsi_conf & RESET_SCSI) != 0
  4427. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4428. ahc->flags |= AHC_RESET_BUS_B;
  4429. }
  4430. scsi_conf = ahc_inb(ahc, SCSICONF);
  4431. if ((scsi_conf & RESET_SCSI) != 0
  4432. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4433. ahc->flags |= AHC_RESET_BUS_A;
  4434. ultraenb = 0;
  4435. tagenable = ALL_TARGETS_MASK;
  4436. /* Grab the disconnection disable table and invert it for our needs */
  4437. if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
  4438. printf("%s: Host Adapter Bios disabled. Using default SCSI "
  4439. "device parameters\n", ahc_name(ahc));
  4440. ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
  4441. AHC_TERM_ENB_A|AHC_TERM_ENB_B;
  4442. discenable = ALL_TARGETS_MASK;
  4443. if ((ahc->features & AHC_ULTRA) != 0)
  4444. ultraenb = ALL_TARGETS_MASK;
  4445. } else {
  4446. discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
  4447. | ahc_inb(ahc, DISC_DSB));
  4448. if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
  4449. ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
  4450. | ahc_inb(ahc, ULTRA_ENB);
  4451. }
  4452. if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
  4453. max_targ = 7;
  4454. for (i = 0; i <= max_targ; i++) {
  4455. struct ahc_initiator_tinfo *tinfo;
  4456. struct ahc_tmode_tstate *tstate;
  4457. u_int our_id;
  4458. u_int target_id;
  4459. char channel;
  4460. channel = 'A';
  4461. our_id = ahc->our_id;
  4462. target_id = i;
  4463. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  4464. channel = 'B';
  4465. our_id = ahc->our_id_b;
  4466. target_id = i % 8;
  4467. }
  4468. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  4469. target_id, &tstate);
  4470. /* Default to async narrow across the board */
  4471. memset(tinfo, 0, sizeof(*tinfo));
  4472. if (ahc->flags & AHC_USEDEFAULTS) {
  4473. if ((ahc->features & AHC_WIDE) != 0)
  4474. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4475. /*
  4476. * These will be truncated when we determine the
  4477. * connection type we have with the target.
  4478. */
  4479. tinfo->user.period = ahc_syncrates->period;
  4480. tinfo->user.offset = MAX_OFFSET;
  4481. } else {
  4482. u_int scsirate;
  4483. uint16_t mask;
  4484. /* Take the settings leftover in scratch RAM. */
  4485. scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
  4486. mask = (0x01 << i);
  4487. if ((ahc->features & AHC_ULTRA2) != 0) {
  4488. u_int offset;
  4489. u_int maxsync;
  4490. if ((scsirate & SOFS) == 0x0F) {
  4491. /*
  4492. * Haven't negotiated yet,
  4493. * so the format is different.
  4494. */
  4495. scsirate = (scsirate & SXFR) >> 4
  4496. | (ultraenb & mask)
  4497. ? 0x08 : 0x0
  4498. | (scsirate & WIDEXFER);
  4499. offset = MAX_OFFSET_ULTRA2;
  4500. } else
  4501. offset = ahc_inb(ahc, TARG_OFFSET + i);
  4502. if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
  4503. /* Set to the lowest sync rate, 5MHz */
  4504. scsirate |= 0x1c;
  4505. maxsync = AHC_SYNCRATE_ULTRA2;
  4506. if ((ahc->features & AHC_DT) != 0)
  4507. maxsync = AHC_SYNCRATE_DT;
  4508. tinfo->user.period =
  4509. ahc_find_period(ahc, scsirate, maxsync);
  4510. if (offset == 0)
  4511. tinfo->user.period = 0;
  4512. else
  4513. tinfo->user.offset = MAX_OFFSET;
  4514. if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
  4515. && (ahc->features & AHC_DT) != 0)
  4516. tinfo->user.ppr_options =
  4517. MSG_EXT_PPR_DT_REQ;
  4518. } else if ((scsirate & SOFS) != 0) {
  4519. if ((scsirate & SXFR) == 0x40
  4520. && (ultraenb & mask) != 0) {
  4521. /* Treat 10MHz as a non-ultra speed */
  4522. scsirate &= ~SXFR;
  4523. ultraenb &= ~mask;
  4524. }
  4525. tinfo->user.period =
  4526. ahc_find_period(ahc, scsirate,
  4527. (ultraenb & mask)
  4528. ? AHC_SYNCRATE_ULTRA
  4529. : AHC_SYNCRATE_FAST);
  4530. if (tinfo->user.period != 0)
  4531. tinfo->user.offset = MAX_OFFSET;
  4532. }
  4533. if (tinfo->user.period == 0)
  4534. tinfo->user.offset = 0;
  4535. if ((scsirate & WIDEXFER) != 0
  4536. && (ahc->features & AHC_WIDE) != 0)
  4537. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4538. tinfo->user.protocol_version = 4;
  4539. if ((ahc->features & AHC_DT) != 0)
  4540. tinfo->user.transport_version = 3;
  4541. else
  4542. tinfo->user.transport_version = 2;
  4543. tinfo->goal.protocol_version = 2;
  4544. tinfo->goal.transport_version = 2;
  4545. tinfo->curr.protocol_version = 2;
  4546. tinfo->curr.transport_version = 2;
  4547. }
  4548. tstate->ultraenb = 0;
  4549. }
  4550. ahc->user_discenable = discenable;
  4551. ahc->user_tagenable = tagenable;
  4552. return (ahc->bus_chip_init(ahc));
  4553. }
  4554. void
  4555. ahc_intr_enable(struct ahc_softc *ahc, int enable)
  4556. {
  4557. u_int hcntrl;
  4558. hcntrl = ahc_inb(ahc, HCNTRL);
  4559. hcntrl &= ~INTEN;
  4560. ahc->pause &= ~INTEN;
  4561. ahc->unpause &= ~INTEN;
  4562. if (enable) {
  4563. hcntrl |= INTEN;
  4564. ahc->pause |= INTEN;
  4565. ahc->unpause |= INTEN;
  4566. }
  4567. ahc_outb(ahc, HCNTRL, hcntrl);
  4568. }
  4569. /*
  4570. * Ensure that the card is paused in a location
  4571. * outside of all critical sections and that all
  4572. * pending work is completed prior to returning.
  4573. * This routine should only be called from outside
  4574. * an interrupt context.
  4575. */
  4576. void
  4577. ahc_pause_and_flushwork(struct ahc_softc *ahc)
  4578. {
  4579. int intstat;
  4580. int maxloops;
  4581. int paused;
  4582. maxloops = 1000;
  4583. ahc->flags |= AHC_ALL_INTERRUPTS;
  4584. paused = FALSE;
  4585. do {
  4586. if (paused) {
  4587. ahc_unpause(ahc);
  4588. /*
  4589. * Give the sequencer some time to service
  4590. * any active selections.
  4591. */
  4592. ahc_delay(500);
  4593. }
  4594. ahc_intr(ahc);
  4595. ahc_pause(ahc);
  4596. paused = TRUE;
  4597. ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  4598. intstat = ahc_inb(ahc, INTSTAT);
  4599. if ((intstat & INT_PEND) == 0) {
  4600. ahc_clear_critical_section(ahc);
  4601. intstat = ahc_inb(ahc, INTSTAT);
  4602. }
  4603. } while (--maxloops
  4604. && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
  4605. && ((intstat & INT_PEND) != 0
  4606. || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
  4607. if (maxloops == 0) {
  4608. printf("Infinite interrupt loop, INTSTAT = %x",
  4609. ahc_inb(ahc, INTSTAT));
  4610. }
  4611. ahc_platform_flushwork(ahc);
  4612. ahc->flags &= ~AHC_ALL_INTERRUPTS;
  4613. }
  4614. int
  4615. ahc_suspend(struct ahc_softc *ahc)
  4616. {
  4617. ahc_pause_and_flushwork(ahc);
  4618. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  4619. ahc_unpause(ahc);
  4620. return (EBUSY);
  4621. }
  4622. #ifdef AHC_TARGET_MODE
  4623. /*
  4624. * XXX What about ATIOs that have not yet been serviced?
  4625. * Perhaps we should just refuse to be suspended if we
  4626. * are acting in a target role.
  4627. */
  4628. if (ahc->pending_device != NULL) {
  4629. ahc_unpause(ahc);
  4630. return (EBUSY);
  4631. }
  4632. #endif
  4633. ahc_shutdown(ahc);
  4634. return (0);
  4635. }
  4636. int
  4637. ahc_resume(struct ahc_softc *ahc)
  4638. {
  4639. ahc_reset(ahc, /*reinit*/TRUE);
  4640. ahc_intr_enable(ahc, TRUE);
  4641. ahc_restart(ahc);
  4642. return (0);
  4643. }
  4644. /************************** Busy Target Table *********************************/
  4645. /*
  4646. * Return the untagged transaction id for a given target/channel lun.
  4647. * Optionally, clear the entry.
  4648. */
  4649. u_int
  4650. ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
  4651. {
  4652. u_int scbid;
  4653. u_int target_offset;
  4654. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4655. u_int saved_scbptr;
  4656. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4657. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4658. scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
  4659. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4660. } else {
  4661. target_offset = TCL_TARGET_OFFSET(tcl);
  4662. scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
  4663. }
  4664. return (scbid);
  4665. }
  4666. void
  4667. ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
  4668. {
  4669. u_int target_offset;
  4670. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4671. u_int saved_scbptr;
  4672. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4673. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4674. ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
  4675. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4676. } else {
  4677. target_offset = TCL_TARGET_OFFSET(tcl);
  4678. ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
  4679. }
  4680. }
  4681. void
  4682. ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
  4683. {
  4684. u_int target_offset;
  4685. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4686. u_int saved_scbptr;
  4687. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4688. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4689. ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
  4690. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4691. } else {
  4692. target_offset = TCL_TARGET_OFFSET(tcl);
  4693. ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
  4694. }
  4695. }
  4696. /************************** SCB and SCB queue management **********************/
  4697. int
  4698. ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
  4699. char channel, int lun, u_int tag, role_t role)
  4700. {
  4701. int targ = SCB_GET_TARGET(ahc, scb);
  4702. char chan = SCB_GET_CHANNEL(ahc, scb);
  4703. int slun = SCB_GET_LUN(scb);
  4704. int match;
  4705. match = ((chan == channel) || (channel == ALL_CHANNELS));
  4706. if (match != 0)
  4707. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  4708. if (match != 0)
  4709. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  4710. if (match != 0) {
  4711. #ifdef AHC_TARGET_MODE
  4712. int group;
  4713. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  4714. if (role == ROLE_INITIATOR) {
  4715. match = (group != XPT_FC_GROUP_TMODE)
  4716. && ((tag == scb->hscb->tag)
  4717. || (tag == SCB_LIST_NULL));
  4718. } else if (role == ROLE_TARGET) {
  4719. match = (group == XPT_FC_GROUP_TMODE)
  4720. && ((tag == scb->io_ctx->csio.tag_id)
  4721. || (tag == SCB_LIST_NULL));
  4722. }
  4723. #else /* !AHC_TARGET_MODE */
  4724. match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
  4725. #endif /* AHC_TARGET_MODE */
  4726. }
  4727. return match;
  4728. }
  4729. void
  4730. ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  4731. {
  4732. int target;
  4733. char channel;
  4734. int lun;
  4735. target = SCB_GET_TARGET(ahc, scb);
  4736. lun = SCB_GET_LUN(scb);
  4737. channel = SCB_GET_CHANNEL(ahc, scb);
  4738. ahc_search_qinfifo(ahc, target, channel, lun,
  4739. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  4740. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  4741. ahc_platform_freeze_devq(ahc, scb);
  4742. }
  4743. void
  4744. ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
  4745. {
  4746. struct scb *prev_scb;
  4747. prev_scb = NULL;
  4748. if (ahc_qinfifo_count(ahc) != 0) {
  4749. u_int prev_tag;
  4750. uint8_t prev_pos;
  4751. prev_pos = ahc->qinfifonext - 1;
  4752. prev_tag = ahc->qinfifo[prev_pos];
  4753. prev_scb = ahc_lookup_scb(ahc, prev_tag);
  4754. }
  4755. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4756. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4757. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4758. } else {
  4759. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4760. }
  4761. }
  4762. static void
  4763. ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
  4764. struct scb *scb)
  4765. {
  4766. if (prev_scb == NULL) {
  4767. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4768. } else {
  4769. prev_scb->hscb->next = scb->hscb->tag;
  4770. ahc_sync_scb(ahc, prev_scb,
  4771. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4772. }
  4773. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  4774. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4775. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4776. }
  4777. static int
  4778. ahc_qinfifo_count(struct ahc_softc *ahc)
  4779. {
  4780. uint8_t qinpos;
  4781. uint8_t diff;
  4782. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4783. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  4784. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  4785. } else
  4786. qinpos = ahc_inb(ahc, QINPOS);
  4787. diff = ahc->qinfifonext - qinpos;
  4788. return (diff);
  4789. }
  4790. int
  4791. ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
  4792. int lun, u_int tag, role_t role, uint32_t status,
  4793. ahc_search_action action)
  4794. {
  4795. struct scb *scb;
  4796. struct scb *prev_scb;
  4797. uint8_t qinstart;
  4798. uint8_t qinpos;
  4799. uint8_t qintail;
  4800. uint8_t next;
  4801. uint8_t prev;
  4802. uint8_t curscbptr;
  4803. int found;
  4804. int have_qregs;
  4805. qintail = ahc->qinfifonext;
  4806. have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
  4807. if (have_qregs) {
  4808. qinstart = ahc_inb(ahc, SNSCB_QOFF);
  4809. ahc_outb(ahc, SNSCB_QOFF, qinstart);
  4810. } else
  4811. qinstart = ahc_inb(ahc, QINPOS);
  4812. qinpos = qinstart;
  4813. found = 0;
  4814. prev_scb = NULL;
  4815. if (action == SEARCH_COMPLETE) {
  4816. /*
  4817. * Don't attempt to run any queued untagged transactions
  4818. * until we are done with the abort process.
  4819. */
  4820. ahc_freeze_untagged_queues(ahc);
  4821. }
  4822. /*
  4823. * Start with an empty queue. Entries that are not chosen
  4824. * for removal will be re-added to the queue as we go.
  4825. */
  4826. ahc->qinfifonext = qinpos;
  4827. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4828. while (qinpos != qintail) {
  4829. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
  4830. if (scb == NULL) {
  4831. printf("qinpos = %d, SCB index = %d\n",
  4832. qinpos, ahc->qinfifo[qinpos]);
  4833. panic("Loop 1\n");
  4834. }
  4835. if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
  4836. /*
  4837. * We found an scb that needs to be acted on.
  4838. */
  4839. found++;
  4840. switch (action) {
  4841. case SEARCH_COMPLETE:
  4842. {
  4843. cam_status ostat;
  4844. cam_status cstat;
  4845. ostat = ahc_get_transaction_status(scb);
  4846. if (ostat == CAM_REQ_INPROG)
  4847. ahc_set_transaction_status(scb, status);
  4848. cstat = ahc_get_transaction_status(scb);
  4849. if (cstat != CAM_REQ_CMP)
  4850. ahc_freeze_scb(scb);
  4851. if ((scb->flags & SCB_ACTIVE) == 0)
  4852. printf("Inactive SCB in qinfifo\n");
  4853. ahc_done(ahc, scb);
  4854. /* FALLTHROUGH */
  4855. }
  4856. case SEARCH_REMOVE:
  4857. break;
  4858. case SEARCH_COUNT:
  4859. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4860. prev_scb = scb;
  4861. break;
  4862. }
  4863. } else {
  4864. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4865. prev_scb = scb;
  4866. }
  4867. qinpos++;
  4868. }
  4869. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4870. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4871. } else {
  4872. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4873. }
  4874. if (action != SEARCH_COUNT
  4875. && (found != 0)
  4876. && (qinstart != ahc->qinfifonext)) {
  4877. /*
  4878. * The sequencer may be in the process of dmaing
  4879. * down the SCB at the beginning of the queue.
  4880. * This could be problematic if either the first,
  4881. * or the second SCB is removed from the queue
  4882. * (the first SCB includes a pointer to the "next"
  4883. * SCB to dma). If we have removed any entries, swap
  4884. * the first element in the queue with the next HSCB
  4885. * so the sequencer will notice that NEXT_QUEUED_SCB
  4886. * has changed during its dma attempt and will retry
  4887. * the DMA.
  4888. */
  4889. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
  4890. if (scb == NULL) {
  4891. printf("found = %d, qinstart = %d, qinfifionext = %d\n",
  4892. found, qinstart, ahc->qinfifonext);
  4893. panic("First/Second Qinfifo fixup\n");
  4894. }
  4895. /*
  4896. * ahc_swap_with_next_hscb forces our next pointer to
  4897. * point to the reserved SCB for future commands. Save
  4898. * and restore our original next pointer to maintain
  4899. * queue integrity.
  4900. */
  4901. next = scb->hscb->next;
  4902. ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
  4903. ahc_swap_with_next_hscb(ahc, scb);
  4904. scb->hscb->next = next;
  4905. ahc->qinfifo[qinstart] = scb->hscb->tag;
  4906. /* Tell the card about the new head of the qinfifo. */
  4907. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4908. /* Fixup the tail "next" pointer. */
  4909. qintail = ahc->qinfifonext - 1;
  4910. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
  4911. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4912. }
  4913. /*
  4914. * Search waiting for selection list.
  4915. */
  4916. curscbptr = ahc_inb(ahc, SCBPTR);
  4917. next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
  4918. prev = SCB_LIST_NULL;
  4919. while (next != SCB_LIST_NULL) {
  4920. uint8_t scb_index;
  4921. ahc_outb(ahc, SCBPTR, next);
  4922. scb_index = ahc_inb(ahc, SCB_TAG);
  4923. if (scb_index >= ahc->scb_data->numscbs) {
  4924. printf("Waiting List inconsistency. "
  4925. "SCB index == %d, yet numscbs == %d.",
  4926. scb_index, ahc->scb_data->numscbs);
  4927. ahc_dump_card_state(ahc);
  4928. panic("for safety");
  4929. }
  4930. scb = ahc_lookup_scb(ahc, scb_index);
  4931. if (scb == NULL) {
  4932. printf("scb_index = %d, next = %d\n",
  4933. scb_index, next);
  4934. panic("Waiting List traversal\n");
  4935. }
  4936. if (ahc_match_scb(ahc, scb, target, channel,
  4937. lun, SCB_LIST_NULL, role)) {
  4938. /*
  4939. * We found an scb that needs to be acted on.
  4940. */
  4941. found++;
  4942. switch (action) {
  4943. case SEARCH_COMPLETE:
  4944. {
  4945. cam_status ostat;
  4946. cam_status cstat;
  4947. ostat = ahc_get_transaction_status(scb);
  4948. if (ostat == CAM_REQ_INPROG)
  4949. ahc_set_transaction_status(scb,
  4950. status);
  4951. cstat = ahc_get_transaction_status(scb);
  4952. if (cstat != CAM_REQ_CMP)
  4953. ahc_freeze_scb(scb);
  4954. if ((scb->flags & SCB_ACTIVE) == 0)
  4955. printf("Inactive SCB in Waiting List\n");
  4956. ahc_done(ahc, scb);
  4957. /* FALLTHROUGH */
  4958. }
  4959. case SEARCH_REMOVE:
  4960. next = ahc_rem_wscb(ahc, next, prev);
  4961. break;
  4962. case SEARCH_COUNT:
  4963. prev = next;
  4964. next = ahc_inb(ahc, SCB_NEXT);
  4965. break;
  4966. }
  4967. } else {
  4968. prev = next;
  4969. next = ahc_inb(ahc, SCB_NEXT);
  4970. }
  4971. }
  4972. ahc_outb(ahc, SCBPTR, curscbptr);
  4973. found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
  4974. channel, lun, status, action);
  4975. if (action == SEARCH_COMPLETE)
  4976. ahc_release_untagged_queues(ahc);
  4977. return (found);
  4978. }
  4979. int
  4980. ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
  4981. int target, char channel, int lun, uint32_t status,
  4982. ahc_search_action action)
  4983. {
  4984. struct scb *scb;
  4985. int maxtarget;
  4986. int found;
  4987. int i;
  4988. if (action == SEARCH_COMPLETE) {
  4989. /*
  4990. * Don't attempt to run any queued untagged transactions
  4991. * until we are done with the abort process.
  4992. */
  4993. ahc_freeze_untagged_queues(ahc);
  4994. }
  4995. found = 0;
  4996. i = 0;
  4997. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  4998. maxtarget = 16;
  4999. if (target != CAM_TARGET_WILDCARD) {
  5000. i = target;
  5001. if (channel == 'B')
  5002. i += 8;
  5003. maxtarget = i + 1;
  5004. }
  5005. } else {
  5006. maxtarget = 0;
  5007. }
  5008. for (; i < maxtarget; i++) {
  5009. struct scb_tailq *untagged_q;
  5010. struct scb *next_scb;
  5011. untagged_q = &(ahc->untagged_queues[i]);
  5012. next_scb = TAILQ_FIRST(untagged_q);
  5013. while (next_scb != NULL) {
  5014. scb = next_scb;
  5015. next_scb = TAILQ_NEXT(scb, links.tqe);
  5016. /*
  5017. * The head of the list may be the currently
  5018. * active untagged command for a device.
  5019. * We're only searching for commands that
  5020. * have not been started. A transaction
  5021. * marked active but still in the qinfifo
  5022. * is removed by the qinfifo scanning code
  5023. * above.
  5024. */
  5025. if ((scb->flags & SCB_ACTIVE) != 0)
  5026. continue;
  5027. if (ahc_match_scb(ahc, scb, target, channel, lun,
  5028. SCB_LIST_NULL, ROLE_INITIATOR) == 0
  5029. || (ctx != NULL && ctx != scb->io_ctx))
  5030. continue;
  5031. /*
  5032. * We found an scb that needs to be acted on.
  5033. */
  5034. found++;
  5035. switch (action) {
  5036. case SEARCH_COMPLETE:
  5037. {
  5038. cam_status ostat;
  5039. cam_status cstat;
  5040. ostat = ahc_get_transaction_status(scb);
  5041. if (ostat == CAM_REQ_INPROG)
  5042. ahc_set_transaction_status(scb, status);
  5043. cstat = ahc_get_transaction_status(scb);
  5044. if (cstat != CAM_REQ_CMP)
  5045. ahc_freeze_scb(scb);
  5046. if ((scb->flags & SCB_ACTIVE) == 0)
  5047. printf("Inactive SCB in untaggedQ\n");
  5048. ahc_done(ahc, scb);
  5049. break;
  5050. }
  5051. case SEARCH_REMOVE:
  5052. scb->flags &= ~SCB_UNTAGGEDQ;
  5053. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  5054. break;
  5055. case SEARCH_COUNT:
  5056. break;
  5057. }
  5058. }
  5059. }
  5060. if (action == SEARCH_COMPLETE)
  5061. ahc_release_untagged_queues(ahc);
  5062. return (found);
  5063. }
  5064. int
  5065. ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
  5066. int lun, u_int tag, int stop_on_first, int remove,
  5067. int save_state)
  5068. {
  5069. struct scb *scbp;
  5070. u_int next;
  5071. u_int prev;
  5072. u_int count;
  5073. u_int active_scb;
  5074. count = 0;
  5075. next = ahc_inb(ahc, DISCONNECTED_SCBH);
  5076. prev = SCB_LIST_NULL;
  5077. if (save_state) {
  5078. /* restore this when we're done */
  5079. active_scb = ahc_inb(ahc, SCBPTR);
  5080. } else
  5081. /* Silence compiler */
  5082. active_scb = SCB_LIST_NULL;
  5083. while (next != SCB_LIST_NULL) {
  5084. u_int scb_index;
  5085. ahc_outb(ahc, SCBPTR, next);
  5086. scb_index = ahc_inb(ahc, SCB_TAG);
  5087. if (scb_index >= ahc->scb_data->numscbs) {
  5088. printf("Disconnected List inconsistency. "
  5089. "SCB index == %d, yet numscbs == %d.",
  5090. scb_index, ahc->scb_data->numscbs);
  5091. ahc_dump_card_state(ahc);
  5092. panic("for safety");
  5093. }
  5094. if (next == prev) {
  5095. panic("Disconnected List Loop. "
  5096. "cur SCBPTR == %x, prev SCBPTR == %x.",
  5097. next, prev);
  5098. }
  5099. scbp = ahc_lookup_scb(ahc, scb_index);
  5100. if (ahc_match_scb(ahc, scbp, target, channel, lun,
  5101. tag, ROLE_INITIATOR)) {
  5102. count++;
  5103. if (remove) {
  5104. next =
  5105. ahc_rem_scb_from_disc_list(ahc, prev, next);
  5106. } else {
  5107. prev = next;
  5108. next = ahc_inb(ahc, SCB_NEXT);
  5109. }
  5110. if (stop_on_first)
  5111. break;
  5112. } else {
  5113. prev = next;
  5114. next = ahc_inb(ahc, SCB_NEXT);
  5115. }
  5116. }
  5117. if (save_state)
  5118. ahc_outb(ahc, SCBPTR, active_scb);
  5119. return (count);
  5120. }
  5121. /*
  5122. * Remove an SCB from the on chip list of disconnected transactions.
  5123. * This is empty/unused if we are not performing SCB paging.
  5124. */
  5125. static u_int
  5126. ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
  5127. {
  5128. u_int next;
  5129. ahc_outb(ahc, SCBPTR, scbptr);
  5130. next = ahc_inb(ahc, SCB_NEXT);
  5131. ahc_outb(ahc, SCB_CONTROL, 0);
  5132. ahc_add_curscb_to_free_list(ahc);
  5133. if (prev != SCB_LIST_NULL) {
  5134. ahc_outb(ahc, SCBPTR, prev);
  5135. ahc_outb(ahc, SCB_NEXT, next);
  5136. } else
  5137. ahc_outb(ahc, DISCONNECTED_SCBH, next);
  5138. return (next);
  5139. }
  5140. /*
  5141. * Add the SCB as selected by SCBPTR onto the on chip list of
  5142. * free hardware SCBs. This list is empty/unused if we are not
  5143. * performing SCB paging.
  5144. */
  5145. static void
  5146. ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
  5147. {
  5148. /*
  5149. * Invalidate the tag so that our abort
  5150. * routines don't think it's active.
  5151. */
  5152. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  5153. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  5154. ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
  5155. ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
  5156. }
  5157. }
  5158. /*
  5159. * Manipulate the waiting for selection list and return the
  5160. * scb that follows the one that we remove.
  5161. */
  5162. static u_int
  5163. ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
  5164. {
  5165. u_int curscb, next;
  5166. /*
  5167. * Select the SCB we want to abort and
  5168. * pull the next pointer out of it.
  5169. */
  5170. curscb = ahc_inb(ahc, SCBPTR);
  5171. ahc_outb(ahc, SCBPTR, scbpos);
  5172. next = ahc_inb(ahc, SCB_NEXT);
  5173. /* Clear the necessary fields */
  5174. ahc_outb(ahc, SCB_CONTROL, 0);
  5175. ahc_add_curscb_to_free_list(ahc);
  5176. /* update the waiting list */
  5177. if (prev == SCB_LIST_NULL) {
  5178. /* First in the list */
  5179. ahc_outb(ahc, WAITING_SCBH, next);
  5180. /*
  5181. * Ensure we aren't attempting to perform
  5182. * selection for this entry.
  5183. */
  5184. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  5185. } else {
  5186. /*
  5187. * Select the scb that pointed to us
  5188. * and update its next pointer.
  5189. */
  5190. ahc_outb(ahc, SCBPTR, prev);
  5191. ahc_outb(ahc, SCB_NEXT, next);
  5192. }
  5193. /*
  5194. * Point us back at the original scb position.
  5195. */
  5196. ahc_outb(ahc, SCBPTR, curscb);
  5197. return next;
  5198. }
  5199. /******************************** Error Handling ******************************/
  5200. /*
  5201. * Abort all SCBs that match the given description (target/channel/lun/tag),
  5202. * setting their status to the passed in status if the status has not already
  5203. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  5204. * is paused before it is called.
  5205. */
  5206. int
  5207. ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  5208. int lun, u_int tag, role_t role, uint32_t status)
  5209. {
  5210. struct scb *scbp;
  5211. struct scb *scbp_next;
  5212. u_int active_scb;
  5213. int i, j;
  5214. int maxtarget;
  5215. int minlun;
  5216. int maxlun;
  5217. int found;
  5218. /*
  5219. * Don't attempt to run any queued untagged transactions
  5220. * until we are done with the abort process.
  5221. */
  5222. ahc_freeze_untagged_queues(ahc);
  5223. /* restore this when we're done */
  5224. active_scb = ahc_inb(ahc, SCBPTR);
  5225. found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
  5226. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5227. /*
  5228. * Clean out the busy target table for any untagged commands.
  5229. */
  5230. i = 0;
  5231. maxtarget = 16;
  5232. if (target != CAM_TARGET_WILDCARD) {
  5233. i = target;
  5234. if (channel == 'B')
  5235. i += 8;
  5236. maxtarget = i + 1;
  5237. }
  5238. if (lun == CAM_LUN_WILDCARD) {
  5239. /*
  5240. * Unless we are using an SCB based
  5241. * busy targets table, there is only
  5242. * one table entry for all luns of
  5243. * a target.
  5244. */
  5245. minlun = 0;
  5246. maxlun = 1;
  5247. if ((ahc->flags & AHC_SCB_BTT) != 0)
  5248. maxlun = AHC_NUM_LUNS;
  5249. } else {
  5250. minlun = lun;
  5251. maxlun = lun + 1;
  5252. }
  5253. if (role != ROLE_TARGET) {
  5254. for (;i < maxtarget; i++) {
  5255. for (j = minlun;j < maxlun; j++) {
  5256. u_int scbid;
  5257. u_int tcl;
  5258. tcl = BUILD_TCL(i << 4, j);
  5259. scbid = ahc_index_busy_tcl(ahc, tcl);
  5260. scbp = ahc_lookup_scb(ahc, scbid);
  5261. if (scbp == NULL
  5262. || ahc_match_scb(ahc, scbp, target, channel,
  5263. lun, tag, role) == 0)
  5264. continue;
  5265. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
  5266. }
  5267. }
  5268. /*
  5269. * Go through the disconnected list and remove any entries we
  5270. * have queued for completion, 0'ing their control byte too.
  5271. * We save the active SCB and restore it ourselves, so there
  5272. * is no reason for this search to restore it too.
  5273. */
  5274. ahc_search_disc_list(ahc, target, channel, lun, tag,
  5275. /*stop_on_first*/FALSE, /*remove*/TRUE,
  5276. /*save_state*/FALSE);
  5277. }
  5278. /*
  5279. * Go through the hardware SCB array looking for commands that
  5280. * were active but not on any list. In some cases, these remnants
  5281. * might not still have mappings in the scbindex array (e.g. unexpected
  5282. * bus free with the same scb queued for an abort). Don't hold this
  5283. * against them.
  5284. */
  5285. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  5286. u_int scbid;
  5287. ahc_outb(ahc, SCBPTR, i);
  5288. scbid = ahc_inb(ahc, SCB_TAG);
  5289. scbp = ahc_lookup_scb(ahc, scbid);
  5290. if ((scbp == NULL && scbid != SCB_LIST_NULL)
  5291. || (scbp != NULL
  5292. && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
  5293. ahc_add_curscb_to_free_list(ahc);
  5294. }
  5295. /*
  5296. * Go through the pending CCB list and look for
  5297. * commands for this target that are still active.
  5298. * These are other tagged commands that were
  5299. * disconnected when the reset occurred.
  5300. */
  5301. scbp_next = LIST_FIRST(&ahc->pending_scbs);
  5302. while (scbp_next != NULL) {
  5303. scbp = scbp_next;
  5304. scbp_next = LIST_NEXT(scbp, pending_links);
  5305. if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
  5306. cam_status ostat;
  5307. ostat = ahc_get_transaction_status(scbp);
  5308. if (ostat == CAM_REQ_INPROG)
  5309. ahc_set_transaction_status(scbp, status);
  5310. if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
  5311. ahc_freeze_scb(scbp);
  5312. if ((scbp->flags & SCB_ACTIVE) == 0)
  5313. printf("Inactive SCB on pending list\n");
  5314. ahc_done(ahc, scbp);
  5315. found++;
  5316. }
  5317. }
  5318. ahc_outb(ahc, SCBPTR, active_scb);
  5319. ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
  5320. ahc_release_untagged_queues(ahc);
  5321. return found;
  5322. }
  5323. static void
  5324. ahc_reset_current_bus(struct ahc_softc *ahc)
  5325. {
  5326. uint8_t scsiseq;
  5327. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
  5328. scsiseq = ahc_inb(ahc, SCSISEQ);
  5329. ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
  5330. ahc_flush_device_writes(ahc);
  5331. ahc_delay(AHC_BUSRESET_DELAY);
  5332. /* Turn off the bus reset */
  5333. ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
  5334. ahc_clear_intstat(ahc);
  5335. /* Re-enable reset interrupts */
  5336. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
  5337. }
  5338. int
  5339. ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
  5340. {
  5341. struct ahc_devinfo devinfo;
  5342. u_int initiator, target, max_scsiid;
  5343. u_int sblkctl;
  5344. u_int scsiseq;
  5345. u_int simode1;
  5346. int found;
  5347. int restart_needed;
  5348. char cur_channel;
  5349. ahc->pending_device = NULL;
  5350. ahc_compile_devinfo(&devinfo,
  5351. CAM_TARGET_WILDCARD,
  5352. CAM_TARGET_WILDCARD,
  5353. CAM_LUN_WILDCARD,
  5354. channel, ROLE_UNKNOWN);
  5355. ahc_pause(ahc);
  5356. /* Make sure the sequencer is in a safe location. */
  5357. ahc_clear_critical_section(ahc);
  5358. /*
  5359. * Run our command complete fifos to ensure that we perform
  5360. * completion processing on any commands that 'completed'
  5361. * before the reset occurred.
  5362. */
  5363. ahc_run_qoutfifo(ahc);
  5364. #ifdef AHC_TARGET_MODE
  5365. /*
  5366. * XXX - In Twin mode, the tqinfifo may have commands
  5367. * for an unaffected channel in it. However, if
  5368. * we have run out of ATIO resources to drain that
  5369. * queue, we may not get them all out here. Further,
  5370. * the blocked transactions for the reset channel
  5371. * should just be killed off, irrespecitve of whether
  5372. * we are blocked on ATIO resources. Write a routine
  5373. * to compact the tqinfifo appropriately.
  5374. */
  5375. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  5376. ahc_run_tqinfifo(ahc, /*paused*/TRUE);
  5377. }
  5378. #endif
  5379. /*
  5380. * Reset the bus if we are initiating this reset
  5381. */
  5382. sblkctl = ahc_inb(ahc, SBLKCTL);
  5383. cur_channel = 'A';
  5384. if ((ahc->features & AHC_TWIN) != 0
  5385. && ((sblkctl & SELBUSB) != 0))
  5386. cur_channel = 'B';
  5387. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  5388. if (cur_channel != channel) {
  5389. /* Case 1: Command for another bus is active
  5390. * Stealthily reset the other bus without
  5391. * upsetting the current bus.
  5392. */
  5393. ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
  5394. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5395. #ifdef AHC_TARGET_MODE
  5396. /*
  5397. * Bus resets clear ENSELI, so we cannot
  5398. * defer re-enabling bus reset interrupts
  5399. * if we are in target mode.
  5400. */
  5401. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5402. simode1 |= ENSCSIRST;
  5403. #endif
  5404. ahc_outb(ahc, SIMODE1, simode1);
  5405. if (initiate_reset)
  5406. ahc_reset_current_bus(ahc);
  5407. ahc_clear_intstat(ahc);
  5408. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5409. ahc_outb(ahc, SBLKCTL, sblkctl);
  5410. restart_needed = FALSE;
  5411. } else {
  5412. /* Case 2: A command from this bus is active or we're idle */
  5413. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5414. #ifdef AHC_TARGET_MODE
  5415. /*
  5416. * Bus resets clear ENSELI, so we cannot
  5417. * defer re-enabling bus reset interrupts
  5418. * if we are in target mode.
  5419. */
  5420. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5421. simode1 |= ENSCSIRST;
  5422. #endif
  5423. ahc_outb(ahc, SIMODE1, simode1);
  5424. if (initiate_reset)
  5425. ahc_reset_current_bus(ahc);
  5426. ahc_clear_intstat(ahc);
  5427. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5428. restart_needed = TRUE;
  5429. }
  5430. /*
  5431. * Clean up all the state information for the
  5432. * pending transactions on this bus.
  5433. */
  5434. found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
  5435. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  5436. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  5437. max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
  5438. #ifdef AHC_TARGET_MODE
  5439. /*
  5440. * Send an immediate notify ccb to all target more peripheral
  5441. * drivers affected by this action.
  5442. */
  5443. for (target = 0; target <= max_scsiid; target++) {
  5444. struct ahc_tmode_tstate* tstate;
  5445. u_int lun;
  5446. tstate = ahc->enabled_targets[target];
  5447. if (tstate == NULL)
  5448. continue;
  5449. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  5450. struct ahc_tmode_lstate* lstate;
  5451. lstate = tstate->enabled_luns[lun];
  5452. if (lstate == NULL)
  5453. continue;
  5454. ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
  5455. EVENT_TYPE_BUS_RESET, /*arg*/0);
  5456. ahc_send_lstate_events(ahc, lstate);
  5457. }
  5458. }
  5459. #endif
  5460. /* Notify the XPT that a bus reset occurred */
  5461. ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
  5462. CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
  5463. /*
  5464. * Revert to async/narrow transfers until we renegotiate.
  5465. */
  5466. for (target = 0; target <= max_scsiid; target++) {
  5467. if (ahc->enabled_targets[target] == NULL)
  5468. continue;
  5469. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  5470. struct ahc_devinfo devinfo;
  5471. ahc_compile_devinfo(&devinfo, target, initiator,
  5472. CAM_LUN_WILDCARD,
  5473. channel, ROLE_UNKNOWN);
  5474. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  5475. AHC_TRANS_CUR, /*paused*/TRUE);
  5476. ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  5477. /*period*/0, /*offset*/0,
  5478. /*ppr_options*/0, AHC_TRANS_CUR,
  5479. /*paused*/TRUE);
  5480. }
  5481. }
  5482. if (restart_needed)
  5483. ahc_restart(ahc);
  5484. else
  5485. ahc_unpause(ahc);
  5486. return found;
  5487. }
  5488. /***************************** Residual Processing ****************************/
  5489. /*
  5490. * Calculate the residual for a just completed SCB.
  5491. */
  5492. void
  5493. ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
  5494. {
  5495. struct hardware_scb *hscb;
  5496. struct status_pkt *spkt;
  5497. uint32_t sgptr;
  5498. uint32_t resid_sgptr;
  5499. uint32_t resid;
  5500. /*
  5501. * 5 cases.
  5502. * 1) No residual.
  5503. * SG_RESID_VALID clear in sgptr.
  5504. * 2) Transferless command
  5505. * 3) Never performed any transfers.
  5506. * sgptr has SG_FULL_RESID set.
  5507. * 4) No residual but target did not
  5508. * save data pointers after the
  5509. * last transfer, so sgptr was
  5510. * never updated.
  5511. * 5) We have a partial residual.
  5512. * Use residual_sgptr to determine
  5513. * where we are.
  5514. */
  5515. hscb = scb->hscb;
  5516. sgptr = ahc_le32toh(hscb->sgptr);
  5517. if ((sgptr & SG_RESID_VALID) == 0)
  5518. /* Case 1 */
  5519. return;
  5520. sgptr &= ~SG_RESID_VALID;
  5521. if ((sgptr & SG_LIST_NULL) != 0)
  5522. /* Case 2 */
  5523. return;
  5524. spkt = &hscb->shared_data.status;
  5525. resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
  5526. if ((sgptr & SG_FULL_RESID) != 0) {
  5527. /* Case 3 */
  5528. resid = ahc_get_transfer_length(scb);
  5529. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  5530. /* Case 4 */
  5531. return;
  5532. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  5533. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  5534. } else {
  5535. struct ahc_dma_seg *sg;
  5536. /*
  5537. * Remainder of the SG where the transfer
  5538. * stopped.
  5539. */
  5540. resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
  5541. sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
  5542. /* The residual sg_ptr always points to the next sg */
  5543. sg--;
  5544. /*
  5545. * Add up the contents of all residual
  5546. * SG segments that are after the SG where
  5547. * the transfer stopped.
  5548. */
  5549. while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
  5550. sg++;
  5551. resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  5552. }
  5553. }
  5554. if ((scb->flags & SCB_SENSE) == 0)
  5555. ahc_set_residual(scb, resid);
  5556. else
  5557. ahc_set_sense_residual(scb, resid);
  5558. #ifdef AHC_DEBUG
  5559. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  5560. ahc_print_path(ahc, scb);
  5561. printf("Handled %sResidual of %d bytes\n",
  5562. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  5563. }
  5564. #endif
  5565. }
  5566. /******************************* Target Mode **********************************/
  5567. #ifdef AHC_TARGET_MODE
  5568. /*
  5569. * Add a target mode event to this lun's queue
  5570. */
  5571. static void
  5572. ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
  5573. u_int initiator_id, u_int event_type, u_int event_arg)
  5574. {
  5575. struct ahc_tmode_event *event;
  5576. int pending;
  5577. xpt_freeze_devq(lstate->path, /*count*/1);
  5578. if (lstate->event_w_idx >= lstate->event_r_idx)
  5579. pending = lstate->event_w_idx - lstate->event_r_idx;
  5580. else
  5581. pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
  5582. - (lstate->event_r_idx - lstate->event_w_idx);
  5583. if (event_type == EVENT_TYPE_BUS_RESET
  5584. || event_type == MSG_BUS_DEV_RESET) {
  5585. /*
  5586. * Any earlier events are irrelevant, so reset our buffer.
  5587. * This has the effect of allowing us to deal with reset
  5588. * floods (an external device holding down the reset line)
  5589. * without losing the event that is really interesting.
  5590. */
  5591. lstate->event_r_idx = 0;
  5592. lstate->event_w_idx = 0;
  5593. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  5594. }
  5595. if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
  5596. xpt_print_path(lstate->path);
  5597. printf("immediate event %x:%x lost\n",
  5598. lstate->event_buffer[lstate->event_r_idx].event_type,
  5599. lstate->event_buffer[lstate->event_r_idx].event_arg);
  5600. lstate->event_r_idx++;
  5601. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5602. lstate->event_r_idx = 0;
  5603. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  5604. }
  5605. event = &lstate->event_buffer[lstate->event_w_idx];
  5606. event->initiator_id = initiator_id;
  5607. event->event_type = event_type;
  5608. event->event_arg = event_arg;
  5609. lstate->event_w_idx++;
  5610. if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5611. lstate->event_w_idx = 0;
  5612. }
  5613. /*
  5614. * Send any target mode events queued up waiting
  5615. * for immediate notify resources.
  5616. */
  5617. void
  5618. ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
  5619. {
  5620. struct ccb_hdr *ccbh;
  5621. struct ccb_immed_notify *inot;
  5622. while (lstate->event_r_idx != lstate->event_w_idx
  5623. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  5624. struct ahc_tmode_event *event;
  5625. event = &lstate->event_buffer[lstate->event_r_idx];
  5626. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  5627. inot = (struct ccb_immed_notify *)ccbh;
  5628. switch (event->event_type) {
  5629. case EVENT_TYPE_BUS_RESET:
  5630. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  5631. break;
  5632. default:
  5633. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  5634. inot->message_args[0] = event->event_type;
  5635. inot->message_args[1] = event->event_arg;
  5636. break;
  5637. }
  5638. inot->initiator_id = event->initiator_id;
  5639. inot->sense_len = 0;
  5640. xpt_done((union ccb *)inot);
  5641. lstate->event_r_idx++;
  5642. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5643. lstate->event_r_idx = 0;
  5644. }
  5645. }
  5646. #endif
  5647. /******************** Sequencer Program Patching/Download *********************/
  5648. #ifdef AHC_DUMP_SEQ
  5649. void
  5650. ahc_dumpseq(struct ahc_softc* ahc)
  5651. {
  5652. int i;
  5653. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5654. ahc_outb(ahc, SEQADDR0, 0);
  5655. ahc_outb(ahc, SEQADDR1, 0);
  5656. for (i = 0; i < ahc->instruction_ram_size; i++) {
  5657. uint8_t ins_bytes[4];
  5658. ahc_insb(ahc, SEQRAM, ins_bytes, 4);
  5659. printf("0x%08x\n", ins_bytes[0] << 24
  5660. | ins_bytes[1] << 16
  5661. | ins_bytes[2] << 8
  5662. | ins_bytes[3]);
  5663. }
  5664. }
  5665. #endif
  5666. static int
  5667. ahc_loadseq(struct ahc_softc *ahc)
  5668. {
  5669. struct cs cs_table[num_critical_sections];
  5670. u_int begin_set[num_critical_sections];
  5671. u_int end_set[num_critical_sections];
  5672. struct patch *cur_patch;
  5673. u_int cs_count;
  5674. u_int cur_cs;
  5675. u_int i;
  5676. u_int skip_addr;
  5677. u_int sg_prefetch_cnt;
  5678. int downloaded;
  5679. uint8_t download_consts[7];
  5680. /*
  5681. * Start out with 0 critical sections
  5682. * that apply to this firmware load.
  5683. */
  5684. cs_count = 0;
  5685. cur_cs = 0;
  5686. memset(begin_set, 0, sizeof(begin_set));
  5687. memset(end_set, 0, sizeof(end_set));
  5688. /* Setup downloadable constant table */
  5689. download_consts[QOUTFIFO_OFFSET] = 0;
  5690. if (ahc->targetcmds != NULL)
  5691. download_consts[QOUTFIFO_OFFSET] += 32;
  5692. download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
  5693. download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
  5694. download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
  5695. sg_prefetch_cnt = ahc->pci_cachesize;
  5696. if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
  5697. sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
  5698. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  5699. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
  5700. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
  5701. cur_patch = patches;
  5702. downloaded = 0;
  5703. skip_addr = 0;
  5704. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5705. ahc_outb(ahc, SEQADDR0, 0);
  5706. ahc_outb(ahc, SEQADDR1, 0);
  5707. for (i = 0; i < sizeof(seqprog)/4; i++) {
  5708. if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
  5709. /*
  5710. * Don't download this instruction as it
  5711. * is in a patch that was removed.
  5712. */
  5713. continue;
  5714. }
  5715. if (downloaded == ahc->instruction_ram_size) {
  5716. /*
  5717. * We're about to exceed the instruction
  5718. * storage capacity for this chip. Fail
  5719. * the load.
  5720. */
  5721. printf("\n%s: Program too large for instruction memory "
  5722. "size of %d!\n", ahc_name(ahc),
  5723. ahc->instruction_ram_size);
  5724. return (ENOMEM);
  5725. }
  5726. /*
  5727. * Move through the CS table until we find a CS
  5728. * that might apply to this instruction.
  5729. */
  5730. for (; cur_cs < num_critical_sections; cur_cs++) {
  5731. if (critical_sections[cur_cs].end <= i) {
  5732. if (begin_set[cs_count] == TRUE
  5733. && end_set[cs_count] == FALSE) {
  5734. cs_table[cs_count].end = downloaded;
  5735. end_set[cs_count] = TRUE;
  5736. cs_count++;
  5737. }
  5738. continue;
  5739. }
  5740. if (critical_sections[cur_cs].begin <= i
  5741. && begin_set[cs_count] == FALSE) {
  5742. cs_table[cs_count].begin = downloaded;
  5743. begin_set[cs_count] = TRUE;
  5744. }
  5745. break;
  5746. }
  5747. ahc_download_instr(ahc, i, download_consts);
  5748. downloaded++;
  5749. }
  5750. ahc->num_critical_sections = cs_count;
  5751. if (cs_count != 0) {
  5752. cs_count *= sizeof(struct cs);
  5753. ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  5754. if (ahc->critical_sections == NULL)
  5755. panic("ahc_loadseq: Could not malloc");
  5756. memcpy(ahc->critical_sections, cs_table, cs_count);
  5757. }
  5758. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
  5759. if (bootverbose) {
  5760. printf(" %d instructions downloaded\n", downloaded);
  5761. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  5762. ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
  5763. }
  5764. return (0);
  5765. }
  5766. static int
  5767. ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
  5768. u_int start_instr, u_int *skip_addr)
  5769. {
  5770. struct patch *cur_patch;
  5771. struct patch *last_patch;
  5772. u_int num_patches;
  5773. num_patches = sizeof(patches)/sizeof(struct patch);
  5774. last_patch = &patches[num_patches];
  5775. cur_patch = *start_patch;
  5776. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  5777. if (cur_patch->patch_func(ahc) == 0) {
  5778. /* Start rejecting code */
  5779. *skip_addr = start_instr + cur_patch->skip_instr;
  5780. cur_patch += cur_patch->skip_patch;
  5781. } else {
  5782. /* Accepted this patch. Advance to the next
  5783. * one and wait for our intruction pointer to
  5784. * hit this point.
  5785. */
  5786. cur_patch++;
  5787. }
  5788. }
  5789. *start_patch = cur_patch;
  5790. if (start_instr < *skip_addr)
  5791. /* Still skipping */
  5792. return (0);
  5793. return (1);
  5794. }
  5795. static void
  5796. ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
  5797. {
  5798. union ins_formats instr;
  5799. struct ins_format1 *fmt1_ins;
  5800. struct ins_format3 *fmt3_ins;
  5801. u_int opcode;
  5802. /*
  5803. * The firmware is always compiled into a little endian format.
  5804. */
  5805. instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  5806. fmt1_ins = &instr.format1;
  5807. fmt3_ins = NULL;
  5808. /* Pull the opcode */
  5809. opcode = instr.format1.opcode;
  5810. switch (opcode) {
  5811. case AIC_OP_JMP:
  5812. case AIC_OP_JC:
  5813. case AIC_OP_JNC:
  5814. case AIC_OP_CALL:
  5815. case AIC_OP_JNE:
  5816. case AIC_OP_JNZ:
  5817. case AIC_OP_JE:
  5818. case AIC_OP_JZ:
  5819. {
  5820. struct patch *cur_patch;
  5821. int address_offset;
  5822. u_int address;
  5823. u_int skip_addr;
  5824. u_int i;
  5825. fmt3_ins = &instr.format3;
  5826. address_offset = 0;
  5827. address = fmt3_ins->address;
  5828. cur_patch = patches;
  5829. skip_addr = 0;
  5830. for (i = 0; i < address;) {
  5831. ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
  5832. if (skip_addr > i) {
  5833. int end_addr;
  5834. end_addr = MIN(address, skip_addr);
  5835. address_offset += end_addr - i;
  5836. i = skip_addr;
  5837. } else {
  5838. i++;
  5839. }
  5840. }
  5841. address -= address_offset;
  5842. fmt3_ins->address = address;
  5843. /* FALLTHROUGH */
  5844. }
  5845. case AIC_OP_OR:
  5846. case AIC_OP_AND:
  5847. case AIC_OP_XOR:
  5848. case AIC_OP_ADD:
  5849. case AIC_OP_ADC:
  5850. case AIC_OP_BMOV:
  5851. if (fmt1_ins->parity != 0) {
  5852. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  5853. }
  5854. fmt1_ins->parity = 0;
  5855. if ((ahc->features & AHC_CMD_CHAN) == 0
  5856. && opcode == AIC_OP_BMOV) {
  5857. /*
  5858. * Block move was added at the same time
  5859. * as the command channel. Verify that
  5860. * this is only a move of a single element
  5861. * and convert the BMOV to a MOV
  5862. * (AND with an immediate of FF).
  5863. */
  5864. if (fmt1_ins->immediate != 1)
  5865. panic("%s: BMOV not supported\n",
  5866. ahc_name(ahc));
  5867. fmt1_ins->opcode = AIC_OP_AND;
  5868. fmt1_ins->immediate = 0xff;
  5869. }
  5870. /* FALLTHROUGH */
  5871. case AIC_OP_ROL:
  5872. if ((ahc->features & AHC_ULTRA2) != 0) {
  5873. int i, count;
  5874. /* Calculate odd parity for the instruction */
  5875. for (i = 0, count = 0; i < 31; i++) {
  5876. uint32_t mask;
  5877. mask = 0x01 << i;
  5878. if ((instr.integer & mask) != 0)
  5879. count++;
  5880. }
  5881. if ((count & 0x01) == 0)
  5882. instr.format1.parity = 1;
  5883. } else {
  5884. /* Compress the instruction for older sequencers */
  5885. if (fmt3_ins != NULL) {
  5886. instr.integer =
  5887. fmt3_ins->immediate
  5888. | (fmt3_ins->source << 8)
  5889. | (fmt3_ins->address << 16)
  5890. | (fmt3_ins->opcode << 25);
  5891. } else {
  5892. instr.integer =
  5893. fmt1_ins->immediate
  5894. | (fmt1_ins->source << 8)
  5895. | (fmt1_ins->destination << 16)
  5896. | (fmt1_ins->ret << 24)
  5897. | (fmt1_ins->opcode << 25);
  5898. }
  5899. }
  5900. /* The sequencer is a little endian cpu */
  5901. instr.integer = ahc_htole32(instr.integer);
  5902. ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
  5903. break;
  5904. default:
  5905. panic("Unknown opcode encountered in seq program");
  5906. break;
  5907. }
  5908. }
  5909. int
  5910. ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries,
  5911. const char *name, u_int address, u_int value,
  5912. u_int *cur_column, u_int wrap_point)
  5913. {
  5914. int printed;
  5915. u_int printed_mask;
  5916. if (cur_column != NULL && *cur_column >= wrap_point) {
  5917. printf("\n");
  5918. *cur_column = 0;
  5919. }
  5920. printed = printf("%s[0x%x]", name, value);
  5921. if (table == NULL) {
  5922. printed += printf(" ");
  5923. *cur_column += printed;
  5924. return (printed);
  5925. }
  5926. printed_mask = 0;
  5927. while (printed_mask != 0xFF) {
  5928. int entry;
  5929. for (entry = 0; entry < num_entries; entry++) {
  5930. if (((value & table[entry].mask)
  5931. != table[entry].value)
  5932. || ((printed_mask & table[entry].mask)
  5933. == table[entry].mask))
  5934. continue;
  5935. printed += printf("%s%s",
  5936. printed_mask == 0 ? ":(" : "|",
  5937. table[entry].name);
  5938. printed_mask |= table[entry].mask;
  5939. break;
  5940. }
  5941. if (entry >= num_entries)
  5942. break;
  5943. }
  5944. if (printed_mask != 0)
  5945. printed += printf(") ");
  5946. else
  5947. printed += printf(" ");
  5948. if (cur_column != NULL)
  5949. *cur_column += printed;
  5950. return (printed);
  5951. }
  5952. void
  5953. ahc_dump_card_state(struct ahc_softc *ahc)
  5954. {
  5955. struct scb *scb;
  5956. struct scb_tailq *untagged_q;
  5957. u_int cur_col;
  5958. int paused;
  5959. int target;
  5960. int maxtarget;
  5961. int i;
  5962. uint8_t last_phase;
  5963. uint8_t qinpos;
  5964. uint8_t qintail;
  5965. uint8_t qoutpos;
  5966. uint8_t scb_index;
  5967. uint8_t saved_scbptr;
  5968. if (ahc_is_paused(ahc)) {
  5969. paused = 1;
  5970. } else {
  5971. paused = 0;
  5972. ahc_pause(ahc);
  5973. }
  5974. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5975. last_phase = ahc_inb(ahc, LASTPHASE);
  5976. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  5977. "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
  5978. ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
  5979. ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
  5980. if (paused)
  5981. printf("Card was paused\n");
  5982. printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
  5983. ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
  5984. ahc_inb(ahc, ARG_2));
  5985. printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
  5986. ahc_inb(ahc, SCBPTR));
  5987. cur_col = 0;
  5988. if ((ahc->features & AHC_DT) != 0)
  5989. ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
  5990. ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
  5991. ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
  5992. ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
  5993. ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
  5994. ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
  5995. ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
  5996. ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
  5997. ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
  5998. ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
  5999. ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
  6000. ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
  6001. ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
  6002. ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
  6003. ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
  6004. ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
  6005. ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
  6006. ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
  6007. ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
  6008. if (cur_col != 0)
  6009. printf("\n");
  6010. printf("STACK:");
  6011. for (i = 0; i < STACK_SIZE; i++)
  6012. printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
  6013. printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
  6014. printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
  6015. printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
  6016. /* QINFIFO */
  6017. printf("QINFIFO entries: ");
  6018. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  6019. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  6020. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  6021. } else
  6022. qinpos = ahc_inb(ahc, QINPOS);
  6023. qintail = ahc->qinfifonext;
  6024. while (qinpos != qintail) {
  6025. printf("%d ", ahc->qinfifo[qinpos]);
  6026. qinpos++;
  6027. }
  6028. printf("\n");
  6029. printf("Waiting Queue entries: ");
  6030. scb_index = ahc_inb(ahc, WAITING_SCBH);
  6031. i = 0;
  6032. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6033. ahc_outb(ahc, SCBPTR, scb_index);
  6034. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6035. scb_index = ahc_inb(ahc, SCB_NEXT);
  6036. }
  6037. printf("\n");
  6038. printf("Disconnected Queue entries: ");
  6039. scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
  6040. i = 0;
  6041. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6042. ahc_outb(ahc, SCBPTR, scb_index);
  6043. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6044. scb_index = ahc_inb(ahc, SCB_NEXT);
  6045. }
  6046. printf("\n");
  6047. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  6048. printf("QOUTFIFO entries: ");
  6049. qoutpos = ahc->qoutfifonext;
  6050. i = 0;
  6051. while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
  6052. printf("%d ", ahc->qoutfifo[qoutpos]);
  6053. qoutpos++;
  6054. }
  6055. printf("\n");
  6056. printf("Sequencer Free SCB List: ");
  6057. scb_index = ahc_inb(ahc, FREE_SCBH);
  6058. i = 0;
  6059. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6060. ahc_outb(ahc, SCBPTR, scb_index);
  6061. printf("%d ", scb_index);
  6062. scb_index = ahc_inb(ahc, SCB_NEXT);
  6063. }
  6064. printf("\n");
  6065. printf("Sequencer SCB Info: ");
  6066. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  6067. ahc_outb(ahc, SCBPTR, i);
  6068. cur_col = printf("\n%3d ", i);
  6069. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
  6070. ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
  6071. ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
  6072. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6073. }
  6074. printf("\n");
  6075. printf("Pending list: ");
  6076. i = 0;
  6077. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6078. if (i++ > 256)
  6079. break;
  6080. cur_col = printf("\n%3d ", scb->hscb->tag);
  6081. ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
  6082. ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
  6083. ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
  6084. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  6085. ahc_outb(ahc, SCBPTR, scb->hscb->tag);
  6086. printf("(");
  6087. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
  6088. &cur_col, 60);
  6089. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6090. printf(")");
  6091. }
  6092. }
  6093. printf("\n");
  6094. printf("Kernel Free SCB list: ");
  6095. i = 0;
  6096. SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
  6097. if (i++ > 256)
  6098. break;
  6099. printf("%d ", scb->hscb->tag);
  6100. }
  6101. printf("\n");
  6102. maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
  6103. for (target = 0; target <= maxtarget; target++) {
  6104. untagged_q = &ahc->untagged_queues[target];
  6105. if (TAILQ_FIRST(untagged_q) == NULL)
  6106. continue;
  6107. printf("Untagged Q(%d): ", target);
  6108. i = 0;
  6109. TAILQ_FOREACH(scb, untagged_q, links.tqe) {
  6110. if (i++ > 256)
  6111. break;
  6112. printf("%d ", scb->hscb->tag);
  6113. }
  6114. printf("\n");
  6115. }
  6116. ahc_platform_dump_card_state(ahc);
  6117. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  6118. ahc_outb(ahc, SCBPTR, saved_scbptr);
  6119. if (paused == 0)
  6120. ahc_unpause(ahc);
  6121. }
  6122. /************************* Target Mode ****************************************/
  6123. #ifdef AHC_TARGET_MODE
  6124. cam_status
  6125. ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
  6126. struct ahc_tmode_tstate **tstate,
  6127. struct ahc_tmode_lstate **lstate,
  6128. int notfound_failure)
  6129. {
  6130. if ((ahc->features & AHC_TARGETMODE) == 0)
  6131. return (CAM_REQ_INVALID);
  6132. /*
  6133. * Handle the 'black hole' device that sucks up
  6134. * requests to unattached luns on enabled targets.
  6135. */
  6136. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  6137. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  6138. *tstate = NULL;
  6139. *lstate = ahc->black_hole;
  6140. } else {
  6141. u_int max_id;
  6142. max_id = (ahc->features & AHC_WIDE) ? 15 : 7;
  6143. if (ccb->ccb_h.target_id > max_id)
  6144. return (CAM_TID_INVALID);
  6145. if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
  6146. return (CAM_LUN_INVALID);
  6147. *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
  6148. *lstate = NULL;
  6149. if (*tstate != NULL)
  6150. *lstate =
  6151. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  6152. }
  6153. if (notfound_failure != 0 && *lstate == NULL)
  6154. return (CAM_PATH_INVALID);
  6155. return (CAM_REQ_CMP);
  6156. }
  6157. void
  6158. ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
  6159. {
  6160. struct ahc_tmode_tstate *tstate;
  6161. struct ahc_tmode_lstate *lstate;
  6162. struct ccb_en_lun *cel;
  6163. cam_status status;
  6164. u_long s;
  6165. u_int target;
  6166. u_int lun;
  6167. u_int target_mask;
  6168. u_int our_id;
  6169. int error;
  6170. char channel;
  6171. status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
  6172. /*notfound_failure*/FALSE);
  6173. if (status != CAM_REQ_CMP) {
  6174. ccb->ccb_h.status = status;
  6175. return;
  6176. }
  6177. if (cam_sim_bus(sim) == 0)
  6178. our_id = ahc->our_id;
  6179. else
  6180. our_id = ahc->our_id_b;
  6181. if (ccb->ccb_h.target_id != our_id) {
  6182. /*
  6183. * our_id represents our initiator ID, or
  6184. * the ID of the first target to have an
  6185. * enabled lun in target mode. There are
  6186. * two cases that may preclude enabling a
  6187. * target id other than our_id.
  6188. *
  6189. * o our_id is for an active initiator role.
  6190. * Since the hardware does not support
  6191. * reselections to the initiator role at
  6192. * anything other than our_id, and our_id
  6193. * is used by the hardware to indicate the
  6194. * ID to use for both select-out and
  6195. * reselect-out operations, the only target
  6196. * ID we can support in this mode is our_id.
  6197. *
  6198. * o The MULTARGID feature is not available and
  6199. * a previous target mode ID has been enabled.
  6200. */
  6201. if ((ahc->features & AHC_MULTIROLE) != 0) {
  6202. if ((ahc->features & AHC_MULTI_TID) != 0
  6203. && (ahc->flags & AHC_INITIATORROLE) != 0) {
  6204. /*
  6205. * Only allow additional targets if
  6206. * the initiator role is disabled.
  6207. * The hardware cannot handle a re-select-in
  6208. * on the initiator id during a re-select-out
  6209. * on a different target id.
  6210. */
  6211. status = CAM_TID_INVALID;
  6212. } else if ((ahc->flags & AHC_INITIATORROLE) != 0
  6213. || ahc->enabled_luns > 0) {
  6214. /*
  6215. * Only allow our target id to change
  6216. * if the initiator role is not configured
  6217. * and there are no enabled luns which
  6218. * are attached to the currently registered
  6219. * scsi id.
  6220. */
  6221. status = CAM_TID_INVALID;
  6222. }
  6223. } else if ((ahc->features & AHC_MULTI_TID) == 0
  6224. && ahc->enabled_luns > 0) {
  6225. status = CAM_TID_INVALID;
  6226. }
  6227. }
  6228. if (status != CAM_REQ_CMP) {
  6229. ccb->ccb_h.status = status;
  6230. return;
  6231. }
  6232. /*
  6233. * We now have an id that is valid.
  6234. * If we aren't in target mode, switch modes.
  6235. */
  6236. if ((ahc->flags & AHC_TARGETROLE) == 0
  6237. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  6238. u_long s;
  6239. ahc_flag saved_flags;
  6240. printf("Configuring Target Mode\n");
  6241. ahc_lock(ahc, &s);
  6242. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  6243. ccb->ccb_h.status = CAM_BUSY;
  6244. ahc_unlock(ahc, &s);
  6245. return;
  6246. }
  6247. saved_flags = ahc->flags;
  6248. ahc->flags |= AHC_TARGETROLE;
  6249. if ((ahc->features & AHC_MULTIROLE) == 0)
  6250. ahc->flags &= ~AHC_INITIATORROLE;
  6251. ahc_pause(ahc);
  6252. error = ahc_loadseq(ahc);
  6253. if (error != 0) {
  6254. /*
  6255. * Restore original configuration and notify
  6256. * the caller that we cannot support target mode.
  6257. * Since the adapter started out in this
  6258. * configuration, the firmware load will succeed,
  6259. * so there is no point in checking ahc_loadseq's
  6260. * return value.
  6261. */
  6262. ahc->flags = saved_flags;
  6263. (void)ahc_loadseq(ahc);
  6264. ahc_restart(ahc);
  6265. ahc_unlock(ahc, &s);
  6266. ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  6267. return;
  6268. }
  6269. ahc_restart(ahc);
  6270. ahc_unlock(ahc, &s);
  6271. }
  6272. cel = &ccb->cel;
  6273. target = ccb->ccb_h.target_id;
  6274. lun = ccb->ccb_h.target_lun;
  6275. channel = SIM_CHANNEL(ahc, sim);
  6276. target_mask = 0x01 << target;
  6277. if (channel == 'B')
  6278. target_mask <<= 8;
  6279. if (cel->enable != 0) {
  6280. u_int scsiseq;
  6281. /* Are we already enabled?? */
  6282. if (lstate != NULL) {
  6283. xpt_print_path(ccb->ccb_h.path);
  6284. printf("Lun already enabled\n");
  6285. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  6286. return;
  6287. }
  6288. if (cel->grp6_len != 0
  6289. || cel->grp7_len != 0) {
  6290. /*
  6291. * Don't (yet?) support vendor
  6292. * specific commands.
  6293. */
  6294. ccb->ccb_h.status = CAM_REQ_INVALID;
  6295. printf("Non-zero Group Codes\n");
  6296. return;
  6297. }
  6298. /*
  6299. * Seems to be okay.
  6300. * Setup our data structures.
  6301. */
  6302. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  6303. tstate = ahc_alloc_tstate(ahc, target, channel);
  6304. if (tstate == NULL) {
  6305. xpt_print_path(ccb->ccb_h.path);
  6306. printf("Couldn't allocate tstate\n");
  6307. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6308. return;
  6309. }
  6310. }
  6311. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  6312. if (lstate == NULL) {
  6313. xpt_print_path(ccb->ccb_h.path);
  6314. printf("Couldn't allocate lstate\n");
  6315. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6316. return;
  6317. }
  6318. memset(lstate, 0, sizeof(*lstate));
  6319. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  6320. xpt_path_path_id(ccb->ccb_h.path),
  6321. xpt_path_target_id(ccb->ccb_h.path),
  6322. xpt_path_lun_id(ccb->ccb_h.path));
  6323. if (status != CAM_REQ_CMP) {
  6324. free(lstate, M_DEVBUF);
  6325. xpt_print_path(ccb->ccb_h.path);
  6326. printf("Couldn't allocate path\n");
  6327. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6328. return;
  6329. }
  6330. SLIST_INIT(&lstate->accept_tios);
  6331. SLIST_INIT(&lstate->immed_notifies);
  6332. ahc_lock(ahc, &s);
  6333. ahc_pause(ahc);
  6334. if (target != CAM_TARGET_WILDCARD) {
  6335. tstate->enabled_luns[lun] = lstate;
  6336. ahc->enabled_luns++;
  6337. if ((ahc->features & AHC_MULTI_TID) != 0) {
  6338. u_int targid_mask;
  6339. targid_mask = ahc_inb(ahc, TARGID)
  6340. | (ahc_inb(ahc, TARGID + 1) << 8);
  6341. targid_mask |= target_mask;
  6342. ahc_outb(ahc, TARGID, targid_mask);
  6343. ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
  6344. ahc_update_scsiid(ahc, targid_mask);
  6345. } else {
  6346. u_int our_id;
  6347. char channel;
  6348. channel = SIM_CHANNEL(ahc, sim);
  6349. our_id = SIM_SCSI_ID(ahc, sim);
  6350. /*
  6351. * This can only happen if selections
  6352. * are not enabled
  6353. */
  6354. if (target != our_id) {
  6355. u_int sblkctl;
  6356. char cur_channel;
  6357. int swap;
  6358. sblkctl = ahc_inb(ahc, SBLKCTL);
  6359. cur_channel = (sblkctl & SELBUSB)
  6360. ? 'B' : 'A';
  6361. if ((ahc->features & AHC_TWIN) == 0)
  6362. cur_channel = 'A';
  6363. swap = cur_channel != channel;
  6364. if (channel == 'A')
  6365. ahc->our_id = target;
  6366. else
  6367. ahc->our_id_b = target;
  6368. if (swap)
  6369. ahc_outb(ahc, SBLKCTL,
  6370. sblkctl ^ SELBUSB);
  6371. ahc_outb(ahc, SCSIID, target);
  6372. if (swap)
  6373. ahc_outb(ahc, SBLKCTL, sblkctl);
  6374. }
  6375. }
  6376. } else
  6377. ahc->black_hole = lstate;
  6378. /* Allow select-in operations */
  6379. if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
  6380. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6381. scsiseq |= ENSELI;
  6382. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6383. scsiseq = ahc_inb(ahc, SCSISEQ);
  6384. scsiseq |= ENSELI;
  6385. ahc_outb(ahc, SCSISEQ, scsiseq);
  6386. }
  6387. ahc_unpause(ahc);
  6388. ahc_unlock(ahc, &s);
  6389. ccb->ccb_h.status = CAM_REQ_CMP;
  6390. xpt_print_path(ccb->ccb_h.path);
  6391. printf("Lun now enabled for target mode\n");
  6392. } else {
  6393. struct scb *scb;
  6394. int i, empty;
  6395. if (lstate == NULL) {
  6396. ccb->ccb_h.status = CAM_LUN_INVALID;
  6397. return;
  6398. }
  6399. ahc_lock(ahc, &s);
  6400. ccb->ccb_h.status = CAM_REQ_CMP;
  6401. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6402. struct ccb_hdr *ccbh;
  6403. ccbh = &scb->io_ctx->ccb_h;
  6404. if (ccbh->func_code == XPT_CONT_TARGET_IO
  6405. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  6406. printf("CTIO pending\n");
  6407. ccb->ccb_h.status = CAM_REQ_INVALID;
  6408. ahc_unlock(ahc, &s);
  6409. return;
  6410. }
  6411. }
  6412. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  6413. printf("ATIOs pending\n");
  6414. ccb->ccb_h.status = CAM_REQ_INVALID;
  6415. }
  6416. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  6417. printf("INOTs pending\n");
  6418. ccb->ccb_h.status = CAM_REQ_INVALID;
  6419. }
  6420. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  6421. ahc_unlock(ahc, &s);
  6422. return;
  6423. }
  6424. xpt_print_path(ccb->ccb_h.path);
  6425. printf("Target mode disabled\n");
  6426. xpt_free_path(lstate->path);
  6427. free(lstate, M_DEVBUF);
  6428. ahc_pause(ahc);
  6429. /* Can we clean up the target too? */
  6430. if (target != CAM_TARGET_WILDCARD) {
  6431. tstate->enabled_luns[lun] = NULL;
  6432. ahc->enabled_luns--;
  6433. for (empty = 1, i = 0; i < 8; i++)
  6434. if (tstate->enabled_luns[i] != NULL) {
  6435. empty = 0;
  6436. break;
  6437. }
  6438. if (empty) {
  6439. ahc_free_tstate(ahc, target, channel,
  6440. /*force*/FALSE);
  6441. if (ahc->features & AHC_MULTI_TID) {
  6442. u_int targid_mask;
  6443. targid_mask = ahc_inb(ahc, TARGID)
  6444. | (ahc_inb(ahc, TARGID + 1)
  6445. << 8);
  6446. targid_mask &= ~target_mask;
  6447. ahc_outb(ahc, TARGID, targid_mask);
  6448. ahc_outb(ahc, TARGID+1,
  6449. (targid_mask >> 8));
  6450. ahc_update_scsiid(ahc, targid_mask);
  6451. }
  6452. }
  6453. } else {
  6454. ahc->black_hole = NULL;
  6455. /*
  6456. * We can't allow selections without
  6457. * our black hole device.
  6458. */
  6459. empty = TRUE;
  6460. }
  6461. if (ahc->enabled_luns == 0) {
  6462. /* Disallow select-in */
  6463. u_int scsiseq;
  6464. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6465. scsiseq &= ~ENSELI;
  6466. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6467. scsiseq = ahc_inb(ahc, SCSISEQ);
  6468. scsiseq &= ~ENSELI;
  6469. ahc_outb(ahc, SCSISEQ, scsiseq);
  6470. if ((ahc->features & AHC_MULTIROLE) == 0) {
  6471. printf("Configuring Initiator Mode\n");
  6472. ahc->flags &= ~AHC_TARGETROLE;
  6473. ahc->flags |= AHC_INITIATORROLE;
  6474. /*
  6475. * Returning to a configuration that
  6476. * fit previously will always succeed.
  6477. */
  6478. (void)ahc_loadseq(ahc);
  6479. ahc_restart(ahc);
  6480. /*
  6481. * Unpaused. The extra unpause
  6482. * that follows is harmless.
  6483. */
  6484. }
  6485. }
  6486. ahc_unpause(ahc);
  6487. ahc_unlock(ahc, &s);
  6488. }
  6489. }
  6490. static void
  6491. ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
  6492. {
  6493. u_int scsiid_mask;
  6494. u_int scsiid;
  6495. if ((ahc->features & AHC_MULTI_TID) == 0)
  6496. panic("ahc_update_scsiid called on non-multitid unit\n");
  6497. /*
  6498. * Since we will rely on the TARGID mask
  6499. * for selection enables, ensure that OID
  6500. * in SCSIID is not set to some other ID
  6501. * that we don't want to allow selections on.
  6502. */
  6503. if ((ahc->features & AHC_ULTRA2) != 0)
  6504. scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
  6505. else
  6506. scsiid = ahc_inb(ahc, SCSIID);
  6507. scsiid_mask = 0x1 << (scsiid & OID);
  6508. if ((targid_mask & scsiid_mask) == 0) {
  6509. u_int our_id;
  6510. /* ffs counts from 1 */
  6511. our_id = ffs(targid_mask);
  6512. if (our_id == 0)
  6513. our_id = ahc->our_id;
  6514. else
  6515. our_id--;
  6516. scsiid &= TID;
  6517. scsiid |= our_id;
  6518. }
  6519. if ((ahc->features & AHC_ULTRA2) != 0)
  6520. ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
  6521. else
  6522. ahc_outb(ahc, SCSIID, scsiid);
  6523. }
  6524. void
  6525. ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
  6526. {
  6527. struct target_cmd *cmd;
  6528. /*
  6529. * If the card supports auto-access pause,
  6530. * we can access the card directly regardless
  6531. * of whether it is paused or not.
  6532. */
  6533. if ((ahc->features & AHC_AUTOPAUSE) != 0)
  6534. paused = TRUE;
  6535. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
  6536. while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
  6537. /*
  6538. * Only advance through the queue if we
  6539. * have the resources to process the command.
  6540. */
  6541. if (ahc_handle_target_cmd(ahc, cmd) != 0)
  6542. break;
  6543. cmd->cmd_valid = 0;
  6544. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  6545. ahc->shared_data_dmamap,
  6546. ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
  6547. sizeof(struct target_cmd),
  6548. BUS_DMASYNC_PREREAD);
  6549. ahc->tqinfifonext++;
  6550. /*
  6551. * Lazily update our position in the target mode incoming
  6552. * command queue as seen by the sequencer.
  6553. */
  6554. if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  6555. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  6556. u_int hs_mailbox;
  6557. hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
  6558. hs_mailbox &= ~HOST_TQINPOS;
  6559. hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
  6560. ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
  6561. } else {
  6562. if (!paused)
  6563. ahc_pause(ahc);
  6564. ahc_outb(ahc, KERNEL_TQINPOS,
  6565. ahc->tqinfifonext & HOST_TQINPOS);
  6566. if (!paused)
  6567. ahc_unpause(ahc);
  6568. }
  6569. }
  6570. }
  6571. }
  6572. static int
  6573. ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
  6574. {
  6575. struct ahc_tmode_tstate *tstate;
  6576. struct ahc_tmode_lstate *lstate;
  6577. struct ccb_accept_tio *atio;
  6578. uint8_t *byte;
  6579. int initiator;
  6580. int target;
  6581. int lun;
  6582. initiator = SCSIID_TARGET(ahc, cmd->scsiid);
  6583. target = SCSIID_OUR_ID(cmd->scsiid);
  6584. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  6585. byte = cmd->bytes;
  6586. tstate = ahc->enabled_targets[target];
  6587. lstate = NULL;
  6588. if (tstate != NULL)
  6589. lstate = tstate->enabled_luns[lun];
  6590. /*
  6591. * Commands for disabled luns go to the black hole driver.
  6592. */
  6593. if (lstate == NULL)
  6594. lstate = ahc->black_hole;
  6595. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  6596. if (atio == NULL) {
  6597. ahc->flags |= AHC_TQINFIFO_BLOCKED;
  6598. /*
  6599. * Wait for more ATIOs from the peripheral driver for this lun.
  6600. */
  6601. if (bootverbose)
  6602. printf("%s: ATIOs exhausted\n", ahc_name(ahc));
  6603. return (1);
  6604. } else
  6605. ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
  6606. #if 0
  6607. printf("Incoming command from %d for %d:%d%s\n",
  6608. initiator, target, lun,
  6609. lstate == ahc->black_hole ? "(Black Holed)" : "");
  6610. #endif
  6611. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  6612. if (lstate == ahc->black_hole) {
  6613. /* Fill in the wildcards */
  6614. atio->ccb_h.target_id = target;
  6615. atio->ccb_h.target_lun = lun;
  6616. }
  6617. /*
  6618. * Package it up and send it off to
  6619. * whomever has this lun enabled.
  6620. */
  6621. atio->sense_len = 0;
  6622. atio->init_id = initiator;
  6623. if (byte[0] != 0xFF) {
  6624. /* Tag was included */
  6625. atio->tag_action = *byte++;
  6626. atio->tag_id = *byte++;
  6627. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  6628. } else {
  6629. atio->ccb_h.flags = 0;
  6630. }
  6631. byte++;
  6632. /* Okay. Now determine the cdb size based on the command code */
  6633. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  6634. case 0:
  6635. atio->cdb_len = 6;
  6636. break;
  6637. case 1:
  6638. case 2:
  6639. atio->cdb_len = 10;
  6640. break;
  6641. case 4:
  6642. atio->cdb_len = 16;
  6643. break;
  6644. case 5:
  6645. atio->cdb_len = 12;
  6646. break;
  6647. case 3:
  6648. default:
  6649. /* Only copy the opcode. */
  6650. atio->cdb_len = 1;
  6651. printf("Reserved or VU command code type encountered\n");
  6652. break;
  6653. }
  6654. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  6655. atio->ccb_h.status |= CAM_CDB_RECVD;
  6656. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  6657. /*
  6658. * We weren't allowed to disconnect.
  6659. * We're hanging on the bus until a
  6660. * continue target I/O comes in response
  6661. * to this accept tio.
  6662. */
  6663. #if 0
  6664. printf("Received Immediate Command %d:%d:%d - %p\n",
  6665. initiator, target, lun, ahc->pending_device);
  6666. #endif
  6667. ahc->pending_device = lstate;
  6668. ahc_freeze_ccb((union ccb *)atio);
  6669. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  6670. }
  6671. xpt_done((union ccb*)atio);
  6672. return (0);
  6673. }
  6674. #endif