aic7xxx_core.c 195 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398
  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 = ARRAY_SIZE(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 = ARRAY_SIZE(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 = ARRAY_SIZE(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, (u_int)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, (u_int)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. /* Now set the maxsync based on the card capabilities
  1586. * DT is already done above */
  1587. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  1588. && maxsync < AHC_SYNCRATE_ULTRA)
  1589. maxsync = AHC_SYNCRATE_ULTRA;
  1590. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  1591. && maxsync < AHC_SYNCRATE_FAST)
  1592. maxsync = AHC_SYNCRATE_FAST;
  1593. for (syncrate = &ahc_syncrates[maxsync];
  1594. syncrate->rate != NULL;
  1595. syncrate++) {
  1596. /*
  1597. * The Ultra2 table doesn't go as low
  1598. * as for the Fast/Ultra cards.
  1599. */
  1600. if ((ahc->features & AHC_ULTRA2) != 0
  1601. && (syncrate->sxfr_u2 == 0))
  1602. break;
  1603. if (*period <= syncrate->period) {
  1604. /*
  1605. * When responding to a target that requests
  1606. * sync, the requested rate may fall between
  1607. * two rates that we can output, but still be
  1608. * a rate that we can receive. Because of this,
  1609. * we want to respond to the target with
  1610. * the same rate that it sent to us even
  1611. * if the period we use to send data to it
  1612. * is lower. Only lower the response period
  1613. * if we must.
  1614. */
  1615. if (syncrate == &ahc_syncrates[maxsync])
  1616. *period = syncrate->period;
  1617. /*
  1618. * At some speeds, we only support
  1619. * ST transfers.
  1620. */
  1621. if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
  1622. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1623. break;
  1624. }
  1625. }
  1626. if ((*period == 0)
  1627. || (syncrate->rate == NULL)
  1628. || ((ahc->features & AHC_ULTRA2) != 0
  1629. && (syncrate->sxfr_u2 == 0))) {
  1630. /* Use asynchronous transfers. */
  1631. *period = 0;
  1632. syncrate = NULL;
  1633. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1634. }
  1635. return (syncrate);
  1636. }
  1637. /*
  1638. * Convert from an entry in our syncrate table to the SCSI equivalent
  1639. * sync "period" factor.
  1640. */
  1641. u_int
  1642. ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
  1643. {
  1644. struct ahc_syncrate *syncrate;
  1645. if ((ahc->features & AHC_ULTRA2) != 0)
  1646. scsirate &= SXFR_ULTRA2;
  1647. else
  1648. scsirate &= SXFR;
  1649. /* now set maxsync based on card capabilities */
  1650. if ((ahc->features & AHC_DT) == 0 && maxsync < AHC_SYNCRATE_ULTRA2)
  1651. maxsync = AHC_SYNCRATE_ULTRA2;
  1652. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  1653. && maxsync < AHC_SYNCRATE_ULTRA)
  1654. maxsync = AHC_SYNCRATE_ULTRA;
  1655. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  1656. && maxsync < AHC_SYNCRATE_FAST)
  1657. maxsync = AHC_SYNCRATE_FAST;
  1658. syncrate = &ahc_syncrates[maxsync];
  1659. while (syncrate->rate != NULL) {
  1660. if ((ahc->features & AHC_ULTRA2) != 0) {
  1661. if (syncrate->sxfr_u2 == 0)
  1662. break;
  1663. else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
  1664. return (syncrate->period);
  1665. } else if (scsirate == (syncrate->sxfr & SXFR)) {
  1666. return (syncrate->period);
  1667. }
  1668. syncrate++;
  1669. }
  1670. return (0); /* async */
  1671. }
  1672. /*
  1673. * Truncate the given synchronous offset to a value the
  1674. * current adapter type and syncrate are capable of.
  1675. */
  1676. void
  1677. ahc_validate_offset(struct ahc_softc *ahc,
  1678. struct ahc_initiator_tinfo *tinfo,
  1679. struct ahc_syncrate *syncrate,
  1680. u_int *offset, int wide, role_t role)
  1681. {
  1682. u_int maxoffset;
  1683. /* Limit offset to what we can do */
  1684. if (syncrate == NULL) {
  1685. maxoffset = 0;
  1686. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  1687. maxoffset = MAX_OFFSET_ULTRA2;
  1688. } else {
  1689. if (wide)
  1690. maxoffset = MAX_OFFSET_16BIT;
  1691. else
  1692. maxoffset = MAX_OFFSET_8BIT;
  1693. }
  1694. *offset = min(*offset, maxoffset);
  1695. if (tinfo != NULL) {
  1696. if (role == ROLE_TARGET)
  1697. *offset = min(*offset, (u_int)tinfo->user.offset);
  1698. else
  1699. *offset = min(*offset, (u_int)tinfo->goal.offset);
  1700. }
  1701. }
  1702. /*
  1703. * Truncate the given transfer width parameter to a value the
  1704. * current adapter type is capable of.
  1705. */
  1706. void
  1707. ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
  1708. u_int *bus_width, role_t role)
  1709. {
  1710. switch (*bus_width) {
  1711. default:
  1712. if (ahc->features & AHC_WIDE) {
  1713. /* Respond Wide */
  1714. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  1715. break;
  1716. }
  1717. /* FALLTHROUGH */
  1718. case MSG_EXT_WDTR_BUS_8_BIT:
  1719. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  1720. break;
  1721. }
  1722. if (tinfo != NULL) {
  1723. if (role == ROLE_TARGET)
  1724. *bus_width = min((u_int)tinfo->user.width, *bus_width);
  1725. else
  1726. *bus_width = min((u_int)tinfo->goal.width, *bus_width);
  1727. }
  1728. }
  1729. /*
  1730. * Update the bitmask of targets for which the controller should
  1731. * negotiate with at the next convenient oportunity. This currently
  1732. * means the next time we send the initial identify messages for
  1733. * a new transaction.
  1734. */
  1735. int
  1736. ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1737. struct ahc_tmode_tstate *tstate,
  1738. struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
  1739. {
  1740. u_int auto_negotiate_orig;
  1741. auto_negotiate_orig = tstate->auto_negotiate;
  1742. if (neg_type == AHC_NEG_ALWAYS) {
  1743. /*
  1744. * Force our "current" settings to be
  1745. * unknown so that unless a bus reset
  1746. * occurs the need to renegotiate is
  1747. * recorded persistently.
  1748. */
  1749. if ((ahc->features & AHC_WIDE) != 0)
  1750. tinfo->curr.width = AHC_WIDTH_UNKNOWN;
  1751. tinfo->curr.period = AHC_PERIOD_UNKNOWN;
  1752. tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
  1753. }
  1754. if (tinfo->curr.period != tinfo->goal.period
  1755. || tinfo->curr.width != tinfo->goal.width
  1756. || tinfo->curr.offset != tinfo->goal.offset
  1757. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  1758. || (neg_type == AHC_NEG_IF_NON_ASYNC
  1759. && (tinfo->goal.offset != 0
  1760. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  1761. || tinfo->goal.ppr_options != 0)))
  1762. tstate->auto_negotiate |= devinfo->target_mask;
  1763. else
  1764. tstate->auto_negotiate &= ~devinfo->target_mask;
  1765. return (auto_negotiate_orig != tstate->auto_negotiate);
  1766. }
  1767. /*
  1768. * Update the user/goal/curr tables of synchronous negotiation
  1769. * parameters as well as, in the case of a current or active update,
  1770. * any data structures on the host controller. In the case of an
  1771. * active update, the specified target is currently talking to us on
  1772. * the bus, so the transfer parameter update must take effect
  1773. * immediately.
  1774. */
  1775. void
  1776. ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1777. struct ahc_syncrate *syncrate, u_int period,
  1778. u_int offset, u_int ppr_options, u_int type, int paused)
  1779. {
  1780. struct ahc_initiator_tinfo *tinfo;
  1781. struct ahc_tmode_tstate *tstate;
  1782. u_int old_period;
  1783. u_int old_offset;
  1784. u_int old_ppr;
  1785. int active;
  1786. int update_needed;
  1787. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1788. update_needed = 0;
  1789. if (syncrate == NULL) {
  1790. period = 0;
  1791. offset = 0;
  1792. }
  1793. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1794. devinfo->target, &tstate);
  1795. if ((type & AHC_TRANS_USER) != 0) {
  1796. tinfo->user.period = period;
  1797. tinfo->user.offset = offset;
  1798. tinfo->user.ppr_options = ppr_options;
  1799. }
  1800. if ((type & AHC_TRANS_GOAL) != 0) {
  1801. tinfo->goal.period = period;
  1802. tinfo->goal.offset = offset;
  1803. tinfo->goal.ppr_options = ppr_options;
  1804. }
  1805. old_period = tinfo->curr.period;
  1806. old_offset = tinfo->curr.offset;
  1807. old_ppr = tinfo->curr.ppr_options;
  1808. if ((type & AHC_TRANS_CUR) != 0
  1809. && (old_period != period
  1810. || old_offset != offset
  1811. || old_ppr != ppr_options)) {
  1812. u_int scsirate;
  1813. update_needed++;
  1814. scsirate = tinfo->scsirate;
  1815. if ((ahc->features & AHC_ULTRA2) != 0) {
  1816. scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
  1817. if (syncrate != NULL) {
  1818. scsirate |= syncrate->sxfr_u2;
  1819. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
  1820. scsirate |= ENABLE_CRC;
  1821. else
  1822. scsirate |= SINGLE_EDGE;
  1823. }
  1824. } else {
  1825. scsirate &= ~(SXFR|SOFS);
  1826. /*
  1827. * Ensure Ultra mode is set properly for
  1828. * this target.
  1829. */
  1830. tstate->ultraenb &= ~devinfo->target_mask;
  1831. if (syncrate != NULL) {
  1832. if (syncrate->sxfr & ULTRA_SXFR) {
  1833. tstate->ultraenb |=
  1834. devinfo->target_mask;
  1835. }
  1836. scsirate |= syncrate->sxfr & SXFR;
  1837. scsirate |= offset & SOFS;
  1838. }
  1839. if (active) {
  1840. u_int sxfrctl0;
  1841. sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
  1842. sxfrctl0 &= ~FAST20;
  1843. if (tstate->ultraenb & devinfo->target_mask)
  1844. sxfrctl0 |= FAST20;
  1845. ahc_outb(ahc, SXFRCTL0, sxfrctl0);
  1846. }
  1847. }
  1848. if (active) {
  1849. ahc_outb(ahc, SCSIRATE, scsirate);
  1850. if ((ahc->features & AHC_ULTRA2) != 0)
  1851. ahc_outb(ahc, SCSIOFFSET, offset);
  1852. }
  1853. tinfo->scsirate = scsirate;
  1854. tinfo->curr.period = period;
  1855. tinfo->curr.offset = offset;
  1856. tinfo->curr.ppr_options = ppr_options;
  1857. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1858. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  1859. if (bootverbose) {
  1860. if (offset != 0) {
  1861. printf("%s: target %d synchronous at %sMHz%s, "
  1862. "offset = 0x%x\n", ahc_name(ahc),
  1863. devinfo->target, syncrate->rate,
  1864. (ppr_options & MSG_EXT_PPR_DT_REQ)
  1865. ? " DT" : "", offset);
  1866. } else {
  1867. printf("%s: target %d using "
  1868. "asynchronous transfers\n",
  1869. ahc_name(ahc), devinfo->target);
  1870. }
  1871. }
  1872. }
  1873. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1874. tinfo, AHC_NEG_TO_GOAL);
  1875. if (update_needed)
  1876. ahc_update_pending_scbs(ahc);
  1877. }
  1878. /*
  1879. * Update the user/goal/curr tables of wide negotiation
  1880. * parameters as well as, in the case of a current or active update,
  1881. * any data structures on the host controller. In the case of an
  1882. * active update, the specified target is currently talking to us on
  1883. * the bus, so the transfer parameter update must take effect
  1884. * immediately.
  1885. */
  1886. void
  1887. ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1888. u_int width, u_int type, int paused)
  1889. {
  1890. struct ahc_initiator_tinfo *tinfo;
  1891. struct ahc_tmode_tstate *tstate;
  1892. u_int oldwidth;
  1893. int active;
  1894. int update_needed;
  1895. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1896. update_needed = 0;
  1897. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1898. devinfo->target, &tstate);
  1899. if ((type & AHC_TRANS_USER) != 0)
  1900. tinfo->user.width = width;
  1901. if ((type & AHC_TRANS_GOAL) != 0)
  1902. tinfo->goal.width = width;
  1903. oldwidth = tinfo->curr.width;
  1904. if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
  1905. u_int scsirate;
  1906. update_needed++;
  1907. scsirate = tinfo->scsirate;
  1908. scsirate &= ~WIDEXFER;
  1909. if (width == MSG_EXT_WDTR_BUS_16_BIT)
  1910. scsirate |= WIDEXFER;
  1911. tinfo->scsirate = scsirate;
  1912. if (active)
  1913. ahc_outb(ahc, SCSIRATE, scsirate);
  1914. tinfo->curr.width = width;
  1915. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1916. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  1917. if (bootverbose) {
  1918. printf("%s: target %d using %dbit transfers\n",
  1919. ahc_name(ahc), devinfo->target,
  1920. 8 * (0x01 << width));
  1921. }
  1922. }
  1923. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1924. tinfo, AHC_NEG_TO_GOAL);
  1925. if (update_needed)
  1926. ahc_update_pending_scbs(ahc);
  1927. }
  1928. /*
  1929. * Update the current state of tagged queuing for a given target.
  1930. */
  1931. static void
  1932. ahc_set_tags(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
  1933. struct ahc_devinfo *devinfo, ahc_queue_alg alg)
  1934. {
  1935. struct scsi_device *sdev = cmd->device;
  1936. ahc_platform_set_tags(ahc, sdev, devinfo, alg);
  1937. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1938. devinfo->lun, AC_TRANSFER_NEG);
  1939. }
  1940. /*
  1941. * When the transfer settings for a connection change, update any
  1942. * in-transit SCBs to contain the new data so the hardware will
  1943. * be set correctly during future (re)selections.
  1944. */
  1945. static void
  1946. ahc_update_pending_scbs(struct ahc_softc *ahc)
  1947. {
  1948. struct scb *pending_scb;
  1949. int pending_scb_count;
  1950. int i;
  1951. int paused;
  1952. u_int saved_scbptr;
  1953. /*
  1954. * Traverse the pending SCB list and ensure that all of the
  1955. * SCBs there have the proper settings.
  1956. */
  1957. pending_scb_count = 0;
  1958. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1959. struct ahc_devinfo devinfo;
  1960. struct hardware_scb *pending_hscb;
  1961. struct ahc_initiator_tinfo *tinfo;
  1962. struct ahc_tmode_tstate *tstate;
  1963. ahc_scb_devinfo(ahc, &devinfo, pending_scb);
  1964. tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  1965. devinfo.our_scsiid,
  1966. devinfo.target, &tstate);
  1967. pending_hscb = pending_scb->hscb;
  1968. pending_hscb->control &= ~ULTRAENB;
  1969. if ((tstate->ultraenb & devinfo.target_mask) != 0)
  1970. pending_hscb->control |= ULTRAENB;
  1971. pending_hscb->scsirate = tinfo->scsirate;
  1972. pending_hscb->scsioffset = tinfo->curr.offset;
  1973. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  1974. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  1975. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  1976. pending_hscb->control &= ~MK_MESSAGE;
  1977. }
  1978. ahc_sync_scb(ahc, pending_scb,
  1979. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  1980. pending_scb_count++;
  1981. }
  1982. if (pending_scb_count == 0)
  1983. return;
  1984. if (ahc_is_paused(ahc)) {
  1985. paused = 1;
  1986. } else {
  1987. paused = 0;
  1988. ahc_pause(ahc);
  1989. }
  1990. saved_scbptr = ahc_inb(ahc, SCBPTR);
  1991. /* Ensure that the hscbs down on the card match the new information */
  1992. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  1993. struct hardware_scb *pending_hscb;
  1994. u_int control;
  1995. u_int scb_tag;
  1996. ahc_outb(ahc, SCBPTR, i);
  1997. scb_tag = ahc_inb(ahc, SCB_TAG);
  1998. pending_scb = ahc_lookup_scb(ahc, scb_tag);
  1999. if (pending_scb == NULL)
  2000. continue;
  2001. pending_hscb = pending_scb->hscb;
  2002. control = ahc_inb(ahc, SCB_CONTROL);
  2003. control &= ~(ULTRAENB|MK_MESSAGE);
  2004. control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
  2005. ahc_outb(ahc, SCB_CONTROL, control);
  2006. ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
  2007. ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
  2008. }
  2009. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2010. if (paused == 0)
  2011. ahc_unpause(ahc);
  2012. }
  2013. /**************************** Pathing Information *****************************/
  2014. static void
  2015. ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2016. {
  2017. u_int saved_scsiid;
  2018. role_t role;
  2019. int our_id;
  2020. if (ahc_inb(ahc, SSTAT0) & TARGET)
  2021. role = ROLE_TARGET;
  2022. else
  2023. role = ROLE_INITIATOR;
  2024. if (role == ROLE_TARGET
  2025. && (ahc->features & AHC_MULTI_TID) != 0
  2026. && (ahc_inb(ahc, SEQ_FLAGS)
  2027. & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
  2028. /* We were selected, so pull our id from TARGIDIN */
  2029. our_id = ahc_inb(ahc, TARGIDIN) & OID;
  2030. } else if ((ahc->features & AHC_ULTRA2) != 0)
  2031. our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
  2032. else
  2033. our_id = ahc_inb(ahc, SCSIID) & OID;
  2034. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2035. ahc_compile_devinfo(devinfo,
  2036. our_id,
  2037. SCSIID_TARGET(ahc, saved_scsiid),
  2038. ahc_inb(ahc, SAVED_LUN),
  2039. SCSIID_CHANNEL(ahc, saved_scsiid),
  2040. role);
  2041. }
  2042. struct ahc_phase_table_entry*
  2043. ahc_lookup_phase_entry(int phase)
  2044. {
  2045. struct ahc_phase_table_entry *entry;
  2046. struct ahc_phase_table_entry *last_entry;
  2047. /*
  2048. * num_phases doesn't include the default entry which
  2049. * will be returned if the phase doesn't match.
  2050. */
  2051. last_entry = &ahc_phase_table[num_phases];
  2052. for (entry = ahc_phase_table; entry < last_entry; entry++) {
  2053. if (phase == entry->phase)
  2054. break;
  2055. }
  2056. return (entry);
  2057. }
  2058. void
  2059. ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
  2060. u_int lun, char channel, role_t role)
  2061. {
  2062. devinfo->our_scsiid = our_id;
  2063. devinfo->target = target;
  2064. devinfo->lun = lun;
  2065. devinfo->target_offset = target;
  2066. devinfo->channel = channel;
  2067. devinfo->role = role;
  2068. if (channel == 'B')
  2069. devinfo->target_offset += 8;
  2070. devinfo->target_mask = (0x01 << devinfo->target_offset);
  2071. }
  2072. void
  2073. ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2074. {
  2075. printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
  2076. devinfo->target, devinfo->lun);
  2077. }
  2078. static void
  2079. ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2080. struct scb *scb)
  2081. {
  2082. role_t role;
  2083. int our_id;
  2084. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  2085. role = ROLE_INITIATOR;
  2086. if ((scb->flags & SCB_TARGET_SCB) != 0)
  2087. role = ROLE_TARGET;
  2088. ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
  2089. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
  2090. }
  2091. /************************ Message Phase Processing ****************************/
  2092. static void
  2093. ahc_assert_atn(struct ahc_softc *ahc)
  2094. {
  2095. u_int scsisigo;
  2096. scsisigo = ATNO;
  2097. if ((ahc->features & AHC_DT) == 0)
  2098. scsisigo |= ahc_inb(ahc, SCSISIGI);
  2099. ahc_outb(ahc, SCSISIGO, scsisigo);
  2100. }
  2101. /*
  2102. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  2103. * or enters the initial message out phase, we are interrupted. Fill our
  2104. * outgoing message buffer with the appropriate message and beging handing
  2105. * the message phase(s) manually.
  2106. */
  2107. static void
  2108. ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2109. struct scb *scb)
  2110. {
  2111. /*
  2112. * To facilitate adding multiple messages together,
  2113. * each routine should increment the index and len
  2114. * variables instead of setting them explicitly.
  2115. */
  2116. ahc->msgout_index = 0;
  2117. ahc->msgout_len = 0;
  2118. if ((scb->flags & SCB_DEVICE_RESET) == 0
  2119. && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
  2120. u_int identify_msg;
  2121. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  2122. if ((scb->hscb->control & DISCENB) != 0)
  2123. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  2124. ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
  2125. ahc->msgout_len++;
  2126. if ((scb->hscb->control & TAG_ENB) != 0) {
  2127. ahc->msgout_buf[ahc->msgout_index++] =
  2128. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  2129. ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
  2130. ahc->msgout_len += 2;
  2131. }
  2132. }
  2133. if (scb->flags & SCB_DEVICE_RESET) {
  2134. ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
  2135. ahc->msgout_len++;
  2136. ahc_print_path(ahc, scb);
  2137. printf("Bus Device Reset Message Sent\n");
  2138. /*
  2139. * Clear our selection hardware in advance of
  2140. * the busfree. We may have an entry in the waiting
  2141. * Q for this target, and we don't want to go about
  2142. * selecting while we handle the busfree and blow it
  2143. * away.
  2144. */
  2145. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2146. } else if ((scb->flags & SCB_ABORT) != 0) {
  2147. if ((scb->hscb->control & TAG_ENB) != 0)
  2148. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
  2149. else
  2150. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
  2151. ahc->msgout_len++;
  2152. ahc_print_path(ahc, scb);
  2153. printf("Abort%s Message Sent\n",
  2154. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  2155. /*
  2156. * Clear our selection hardware in advance of
  2157. * the busfree. We may have an entry in the waiting
  2158. * Q for this target, and we don't want to go about
  2159. * selecting while we handle the busfree and blow it
  2160. * away.
  2161. */
  2162. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2163. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  2164. ahc_build_transfer_msg(ahc, devinfo);
  2165. } else {
  2166. printf("ahc_intr: AWAITING_MSG for an SCB that "
  2167. "does not have a waiting message\n");
  2168. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  2169. devinfo->target_mask);
  2170. panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
  2171. "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
  2172. ahc_inb(ahc, MSG_OUT), scb->flags);
  2173. }
  2174. /*
  2175. * Clear the MK_MESSAGE flag from the SCB so we aren't
  2176. * asked to send this message again.
  2177. */
  2178. ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
  2179. scb->hscb->control &= ~MK_MESSAGE;
  2180. ahc->msgout_index = 0;
  2181. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2182. }
  2183. /*
  2184. * Build an appropriate transfer negotiation message for the
  2185. * currently active target.
  2186. */
  2187. static void
  2188. ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2189. {
  2190. /*
  2191. * We need to initiate transfer negotiations.
  2192. * If our current and goal settings are identical,
  2193. * we want to renegotiate due to a check condition.
  2194. */
  2195. struct ahc_initiator_tinfo *tinfo;
  2196. struct ahc_tmode_tstate *tstate;
  2197. struct ahc_syncrate *rate;
  2198. int dowide;
  2199. int dosync;
  2200. int doppr;
  2201. u_int period;
  2202. u_int ppr_options;
  2203. u_int offset;
  2204. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2205. devinfo->target, &tstate);
  2206. /*
  2207. * Filter our period based on the current connection.
  2208. * If we can't perform DT transfers on this segment (not in LVD
  2209. * mode for instance), then our decision to issue a PPR message
  2210. * may change.
  2211. */
  2212. period = tinfo->goal.period;
  2213. offset = tinfo->goal.offset;
  2214. ppr_options = tinfo->goal.ppr_options;
  2215. /* Target initiated PPR is not allowed in the SCSI spec */
  2216. if (devinfo->role == ROLE_TARGET)
  2217. ppr_options = 0;
  2218. rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2219. &ppr_options, devinfo->role);
  2220. dowide = tinfo->curr.width != tinfo->goal.width;
  2221. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  2222. /*
  2223. * Only use PPR if we have options that need it, even if the device
  2224. * claims to support it. There might be an expander in the way
  2225. * that doesn't.
  2226. */
  2227. doppr = ppr_options != 0;
  2228. if (!dowide && !dosync && !doppr) {
  2229. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  2230. dosync = tinfo->goal.offset != 0;
  2231. }
  2232. if (!dowide && !dosync && !doppr) {
  2233. /*
  2234. * Force async with a WDTR message if we have a wide bus,
  2235. * or just issue an SDTR with a 0 offset.
  2236. */
  2237. if ((ahc->features & AHC_WIDE) != 0)
  2238. dowide = 1;
  2239. else
  2240. dosync = 1;
  2241. if (bootverbose) {
  2242. ahc_print_devinfo(ahc, devinfo);
  2243. printf("Ensuring async\n");
  2244. }
  2245. }
  2246. /* Target initiated PPR is not allowed in the SCSI spec */
  2247. if (devinfo->role == ROLE_TARGET)
  2248. doppr = 0;
  2249. /*
  2250. * Both the PPR message and SDTR message require the
  2251. * goal syncrate to be limited to what the target device
  2252. * is capable of handling (based on whether an LVD->SE
  2253. * expander is on the bus), so combine these two cases.
  2254. * Regardless, guarantee that if we are using WDTR and SDTR
  2255. * messages that WDTR comes first.
  2256. */
  2257. if (doppr || (dosync && !dowide)) {
  2258. offset = tinfo->goal.offset;
  2259. ahc_validate_offset(ahc, tinfo, rate, &offset,
  2260. doppr ? tinfo->goal.width
  2261. : tinfo->curr.width,
  2262. devinfo->role);
  2263. if (doppr) {
  2264. ahc_construct_ppr(ahc, devinfo, period, offset,
  2265. tinfo->goal.width, ppr_options);
  2266. } else {
  2267. ahc_construct_sdtr(ahc, devinfo, period, offset);
  2268. }
  2269. } else {
  2270. ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
  2271. }
  2272. }
  2273. /*
  2274. * Build a synchronous negotiation message in our message
  2275. * buffer based on the input parameters.
  2276. */
  2277. static void
  2278. ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2279. u_int period, u_int offset)
  2280. {
  2281. if (offset == 0)
  2282. period = AHC_ASYNC_XFER_PERIOD;
  2283. ahc->msgout_index += spi_populate_sync_msg(
  2284. ahc->msgout_buf + ahc->msgout_index, period, offset);
  2285. ahc->msgout_len += 5;
  2286. if (bootverbose) {
  2287. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  2288. ahc_name(ahc), devinfo->channel, devinfo->target,
  2289. devinfo->lun, period, offset);
  2290. }
  2291. }
  2292. /*
  2293. * Build a wide negotiation message in our message
  2294. * buffer based on the input parameters.
  2295. */
  2296. static void
  2297. ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2298. u_int bus_width)
  2299. {
  2300. ahc->msgout_index += spi_populate_width_msg(
  2301. ahc->msgout_buf + ahc->msgout_index, bus_width);
  2302. ahc->msgout_len += 4;
  2303. if (bootverbose) {
  2304. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  2305. ahc_name(ahc), devinfo->channel, devinfo->target,
  2306. devinfo->lun, bus_width);
  2307. }
  2308. }
  2309. /*
  2310. * Build a parallel protocol request message in our message
  2311. * buffer based on the input parameters.
  2312. */
  2313. static void
  2314. ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2315. u_int period, u_int offset, u_int bus_width,
  2316. u_int ppr_options)
  2317. {
  2318. if (offset == 0)
  2319. period = AHC_ASYNC_XFER_PERIOD;
  2320. ahc->msgout_index += spi_populate_ppr_msg(
  2321. ahc->msgout_buf + ahc->msgout_index, period, offset,
  2322. bus_width, ppr_options);
  2323. ahc->msgout_len += 8;
  2324. if (bootverbose) {
  2325. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  2326. "offset %x, ppr_options %x\n", ahc_name(ahc),
  2327. devinfo->channel, devinfo->target, devinfo->lun,
  2328. bus_width, period, offset, ppr_options);
  2329. }
  2330. }
  2331. /*
  2332. * Clear any active message state.
  2333. */
  2334. static void
  2335. ahc_clear_msg_state(struct ahc_softc *ahc)
  2336. {
  2337. ahc->msgout_len = 0;
  2338. ahc->msgin_index = 0;
  2339. ahc->msg_type = MSG_TYPE_NONE;
  2340. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
  2341. /*
  2342. * The target didn't care to respond to our
  2343. * message request, so clear ATN.
  2344. */
  2345. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2346. }
  2347. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  2348. ahc_outb(ahc, SEQ_FLAGS2,
  2349. ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  2350. }
  2351. static void
  2352. ahc_handle_proto_violation(struct ahc_softc *ahc)
  2353. {
  2354. struct ahc_devinfo devinfo;
  2355. struct scb *scb;
  2356. u_int scbid;
  2357. u_int seq_flags;
  2358. u_int curphase;
  2359. u_int lastphase;
  2360. int found;
  2361. ahc_fetch_devinfo(ahc, &devinfo);
  2362. scbid = ahc_inb(ahc, SCB_TAG);
  2363. scb = ahc_lookup_scb(ahc, scbid);
  2364. seq_flags = ahc_inb(ahc, SEQ_FLAGS);
  2365. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2366. lastphase = ahc_inb(ahc, LASTPHASE);
  2367. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2368. /*
  2369. * The reconnecting target either did not send an
  2370. * identify message, or did, but we didn't find an SCB
  2371. * to match.
  2372. */
  2373. ahc_print_devinfo(ahc, &devinfo);
  2374. printf("Target did not send an IDENTIFY message. "
  2375. "LASTPHASE = 0x%x.\n", lastphase);
  2376. scb = NULL;
  2377. } else if (scb == NULL) {
  2378. /*
  2379. * We don't seem to have an SCB active for this
  2380. * transaction. Print an error and reset the bus.
  2381. */
  2382. ahc_print_devinfo(ahc, &devinfo);
  2383. printf("No SCB found during protocol violation\n");
  2384. goto proto_violation_reset;
  2385. } else {
  2386. ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2387. if ((seq_flags & NO_CDB_SENT) != 0) {
  2388. ahc_print_path(ahc, scb);
  2389. printf("No or incomplete CDB sent to device.\n");
  2390. } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
  2391. /*
  2392. * The target never bothered to provide status to
  2393. * us prior to completing the command. Since we don't
  2394. * know the disposition of this command, we must attempt
  2395. * to abort it. Assert ATN and prepare to send an abort
  2396. * message.
  2397. */
  2398. ahc_print_path(ahc, scb);
  2399. printf("Completed command without status.\n");
  2400. } else {
  2401. ahc_print_path(ahc, scb);
  2402. printf("Unknown protocol violation.\n");
  2403. ahc_dump_card_state(ahc);
  2404. }
  2405. }
  2406. if ((lastphase & ~P_DATAIN_DT) == 0
  2407. || lastphase == P_COMMAND) {
  2408. proto_violation_reset:
  2409. /*
  2410. * Target either went directly to data/command
  2411. * phase or didn't respond to our ATN.
  2412. * The only safe thing to do is to blow
  2413. * it away with a bus reset.
  2414. */
  2415. found = ahc_reset_channel(ahc, 'A', TRUE);
  2416. printf("%s: Issued Channel %c Bus Reset. "
  2417. "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
  2418. } else {
  2419. /*
  2420. * Leave the selection hardware off in case
  2421. * this abort attempt will affect yet to
  2422. * be sent commands.
  2423. */
  2424. ahc_outb(ahc, SCSISEQ,
  2425. ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  2426. ahc_assert_atn(ahc);
  2427. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2428. if (scb == NULL) {
  2429. ahc_print_devinfo(ahc, &devinfo);
  2430. ahc->msgout_buf[0] = MSG_ABORT_TASK;
  2431. ahc->msgout_len = 1;
  2432. ahc->msgout_index = 0;
  2433. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2434. } else {
  2435. ahc_print_path(ahc, scb);
  2436. scb->flags |= SCB_ABORT;
  2437. }
  2438. printf("Protocol violation %s. Attempting to abort.\n",
  2439. ahc_lookup_phase_entry(curphase)->phasemsg);
  2440. }
  2441. }
  2442. /*
  2443. * Manual message loop handler.
  2444. */
  2445. static void
  2446. ahc_handle_message_phase(struct ahc_softc *ahc)
  2447. {
  2448. struct ahc_devinfo devinfo;
  2449. u_int bus_phase;
  2450. int end_session;
  2451. ahc_fetch_devinfo(ahc, &devinfo);
  2452. end_session = FALSE;
  2453. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2454. reswitch:
  2455. switch (ahc->msg_type) {
  2456. case MSG_TYPE_INITIATOR_MSGOUT:
  2457. {
  2458. int lastbyte;
  2459. int phasemis;
  2460. int msgdone;
  2461. if (ahc->msgout_len == 0)
  2462. panic("HOST_MSG_LOOP interrupt with no active message");
  2463. #ifdef AHC_DEBUG
  2464. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2465. ahc_print_devinfo(ahc, &devinfo);
  2466. printf("INITIATOR_MSG_OUT");
  2467. }
  2468. #endif
  2469. phasemis = bus_phase != P_MESGOUT;
  2470. if (phasemis) {
  2471. #ifdef AHC_DEBUG
  2472. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2473. printf(" PHASEMIS %s\n",
  2474. ahc_lookup_phase_entry(bus_phase)
  2475. ->phasemsg);
  2476. }
  2477. #endif
  2478. if (bus_phase == P_MESGIN) {
  2479. /*
  2480. * Change gears and see if
  2481. * this messages is of interest to
  2482. * us or should be passed back to
  2483. * the sequencer.
  2484. */
  2485. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2486. ahc->send_msg_perror = FALSE;
  2487. ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  2488. ahc->msgin_index = 0;
  2489. goto reswitch;
  2490. }
  2491. end_session = TRUE;
  2492. break;
  2493. }
  2494. if (ahc->send_msg_perror) {
  2495. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2496. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2497. #ifdef AHC_DEBUG
  2498. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2499. printf(" byte 0x%x\n", ahc->send_msg_perror);
  2500. #endif
  2501. ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
  2502. break;
  2503. }
  2504. msgdone = ahc->msgout_index == ahc->msgout_len;
  2505. if (msgdone) {
  2506. /*
  2507. * The target has requested a retry.
  2508. * Re-assert ATN, reset our message index to
  2509. * 0, and try again.
  2510. */
  2511. ahc->msgout_index = 0;
  2512. ahc_assert_atn(ahc);
  2513. }
  2514. lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
  2515. if (lastbyte) {
  2516. /* Last byte is signified by dropping ATN */
  2517. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2518. }
  2519. /*
  2520. * Clear our interrupt status and present
  2521. * the next byte on the bus.
  2522. */
  2523. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2524. #ifdef AHC_DEBUG
  2525. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2526. printf(" byte 0x%x\n",
  2527. ahc->msgout_buf[ahc->msgout_index]);
  2528. #endif
  2529. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2530. break;
  2531. }
  2532. case MSG_TYPE_INITIATOR_MSGIN:
  2533. {
  2534. int phasemis;
  2535. int message_done;
  2536. #ifdef AHC_DEBUG
  2537. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2538. ahc_print_devinfo(ahc, &devinfo);
  2539. printf("INITIATOR_MSG_IN");
  2540. }
  2541. #endif
  2542. phasemis = bus_phase != P_MESGIN;
  2543. if (phasemis) {
  2544. #ifdef AHC_DEBUG
  2545. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2546. printf(" PHASEMIS %s\n",
  2547. ahc_lookup_phase_entry(bus_phase)
  2548. ->phasemsg);
  2549. }
  2550. #endif
  2551. ahc->msgin_index = 0;
  2552. if (bus_phase == P_MESGOUT
  2553. && (ahc->send_msg_perror == TRUE
  2554. || (ahc->msgout_len != 0
  2555. && ahc->msgout_index == 0))) {
  2556. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2557. goto reswitch;
  2558. }
  2559. end_session = TRUE;
  2560. break;
  2561. }
  2562. /* Pull the byte in without acking it */
  2563. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
  2564. #ifdef AHC_DEBUG
  2565. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2566. printf(" byte 0x%x\n",
  2567. ahc->msgin_buf[ahc->msgin_index]);
  2568. #endif
  2569. message_done = ahc_parse_msg(ahc, &devinfo);
  2570. if (message_done) {
  2571. /*
  2572. * Clear our incoming message buffer in case there
  2573. * is another message following this one.
  2574. */
  2575. ahc->msgin_index = 0;
  2576. /*
  2577. * If this message illicited a response,
  2578. * assert ATN so the target takes us to the
  2579. * message out phase.
  2580. */
  2581. if (ahc->msgout_len != 0) {
  2582. #ifdef AHC_DEBUG
  2583. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2584. ahc_print_devinfo(ahc, &devinfo);
  2585. printf("Asserting ATN for response\n");
  2586. }
  2587. #endif
  2588. ahc_assert_atn(ahc);
  2589. }
  2590. } else
  2591. ahc->msgin_index++;
  2592. if (message_done == MSGLOOP_TERMINATED) {
  2593. end_session = TRUE;
  2594. } else {
  2595. /* Ack the byte */
  2596. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2597. ahc_inb(ahc, SCSIDATL);
  2598. }
  2599. break;
  2600. }
  2601. case MSG_TYPE_TARGET_MSGIN:
  2602. {
  2603. int msgdone;
  2604. int msgout_request;
  2605. if (ahc->msgout_len == 0)
  2606. panic("Target MSGIN with no active message");
  2607. /*
  2608. * If we interrupted a mesgout session, the initiator
  2609. * will not know this until our first REQ. So, we
  2610. * only honor mesgout requests after we've sent our
  2611. * first byte.
  2612. */
  2613. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
  2614. && ahc->msgout_index > 0)
  2615. msgout_request = TRUE;
  2616. else
  2617. msgout_request = FALSE;
  2618. if (msgout_request) {
  2619. /*
  2620. * Change gears and see if
  2621. * this messages is of interest to
  2622. * us or should be passed back to
  2623. * the sequencer.
  2624. */
  2625. ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
  2626. ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
  2627. ahc->msgin_index = 0;
  2628. /* Dummy read to REQ for first byte */
  2629. ahc_inb(ahc, SCSIDATL);
  2630. ahc_outb(ahc, SXFRCTL0,
  2631. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2632. break;
  2633. }
  2634. msgdone = ahc->msgout_index == ahc->msgout_len;
  2635. if (msgdone) {
  2636. ahc_outb(ahc, SXFRCTL0,
  2637. ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2638. end_session = TRUE;
  2639. break;
  2640. }
  2641. /*
  2642. * Present the next byte on the bus.
  2643. */
  2644. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2645. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2646. break;
  2647. }
  2648. case MSG_TYPE_TARGET_MSGOUT:
  2649. {
  2650. int lastbyte;
  2651. int msgdone;
  2652. /*
  2653. * The initiator signals that this is
  2654. * the last byte by dropping ATN.
  2655. */
  2656. lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
  2657. /*
  2658. * Read the latched byte, but turn off SPIOEN first
  2659. * so that we don't inadvertently cause a REQ for the
  2660. * next byte.
  2661. */
  2662. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2663. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
  2664. msgdone = ahc_parse_msg(ahc, &devinfo);
  2665. if (msgdone == MSGLOOP_TERMINATED) {
  2666. /*
  2667. * The message is *really* done in that it caused
  2668. * us to go to bus free. The sequencer has already
  2669. * been reset at this point, so pull the ejection
  2670. * handle.
  2671. */
  2672. return;
  2673. }
  2674. ahc->msgin_index++;
  2675. /*
  2676. * XXX Read spec about initiator dropping ATN too soon
  2677. * and use msgdone to detect it.
  2678. */
  2679. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  2680. ahc->msgin_index = 0;
  2681. /*
  2682. * If this message illicited a response, transition
  2683. * to the Message in phase and send it.
  2684. */
  2685. if (ahc->msgout_len != 0) {
  2686. ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
  2687. ahc_outb(ahc, SXFRCTL0,
  2688. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2689. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  2690. ahc->msgin_index = 0;
  2691. break;
  2692. }
  2693. }
  2694. if (lastbyte)
  2695. end_session = TRUE;
  2696. else {
  2697. /* Ask for the next byte. */
  2698. ahc_outb(ahc, SXFRCTL0,
  2699. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2700. }
  2701. break;
  2702. }
  2703. default:
  2704. panic("Unknown REQINIT message type");
  2705. }
  2706. if (end_session) {
  2707. ahc_clear_msg_state(ahc);
  2708. ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
  2709. } else
  2710. ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
  2711. }
  2712. /*
  2713. * See if we sent a particular extended message to the target.
  2714. * If "full" is true, return true only if the target saw the full
  2715. * message. If "full" is false, return true if the target saw at
  2716. * least the first byte of the message.
  2717. */
  2718. static int
  2719. ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
  2720. {
  2721. int found;
  2722. u_int index;
  2723. found = FALSE;
  2724. index = 0;
  2725. while (index < ahc->msgout_len) {
  2726. if (ahc->msgout_buf[index] == MSG_EXTENDED) {
  2727. u_int end_index;
  2728. end_index = index + 1 + ahc->msgout_buf[index + 1];
  2729. if (ahc->msgout_buf[index+2] == msgval
  2730. && type == AHCMSG_EXT) {
  2731. if (full) {
  2732. if (ahc->msgout_index > end_index)
  2733. found = TRUE;
  2734. } else if (ahc->msgout_index > index)
  2735. found = TRUE;
  2736. }
  2737. index = end_index;
  2738. } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
  2739. && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  2740. /* Skip tag type and tag id or residue param*/
  2741. index += 2;
  2742. } else {
  2743. /* Single byte message */
  2744. if (type == AHCMSG_1B
  2745. && ahc->msgout_buf[index] == msgval
  2746. && ahc->msgout_index > index)
  2747. found = TRUE;
  2748. index++;
  2749. }
  2750. if (found)
  2751. break;
  2752. }
  2753. return (found);
  2754. }
  2755. /*
  2756. * Wait for a complete incoming message, parse it, and respond accordingly.
  2757. */
  2758. static int
  2759. ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2760. {
  2761. struct ahc_initiator_tinfo *tinfo;
  2762. struct ahc_tmode_tstate *tstate;
  2763. int reject;
  2764. int done;
  2765. int response;
  2766. u_int targ_scsirate;
  2767. done = MSGLOOP_IN_PROG;
  2768. response = FALSE;
  2769. reject = FALSE;
  2770. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2771. devinfo->target, &tstate);
  2772. targ_scsirate = tinfo->scsirate;
  2773. /*
  2774. * Parse as much of the message as is available,
  2775. * rejecting it if we don't support it. When
  2776. * the entire message is available and has been
  2777. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  2778. * that we have parsed an entire message.
  2779. *
  2780. * In the case of extended messages, we accept the length
  2781. * byte outright and perform more checking once we know the
  2782. * extended message type.
  2783. */
  2784. switch (ahc->msgin_buf[0]) {
  2785. case MSG_DISCONNECT:
  2786. case MSG_SAVEDATAPOINTER:
  2787. case MSG_CMDCOMPLETE:
  2788. case MSG_RESTOREPOINTERS:
  2789. case MSG_IGN_WIDE_RESIDUE:
  2790. /*
  2791. * End our message loop as these are messages
  2792. * the sequencer handles on its own.
  2793. */
  2794. done = MSGLOOP_TERMINATED;
  2795. break;
  2796. case MSG_MESSAGE_REJECT:
  2797. response = ahc_handle_msg_reject(ahc, devinfo);
  2798. /* FALLTHROUGH */
  2799. case MSG_NOOP:
  2800. done = MSGLOOP_MSGCOMPLETE;
  2801. break;
  2802. case MSG_EXTENDED:
  2803. {
  2804. /* Wait for enough of the message to begin validation */
  2805. if (ahc->msgin_index < 2)
  2806. break;
  2807. switch (ahc->msgin_buf[2]) {
  2808. case MSG_EXT_SDTR:
  2809. {
  2810. struct ahc_syncrate *syncrate;
  2811. u_int period;
  2812. u_int ppr_options;
  2813. u_int offset;
  2814. u_int saved_offset;
  2815. if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  2816. reject = TRUE;
  2817. break;
  2818. }
  2819. /*
  2820. * Wait until we have both args before validating
  2821. * and acting on this message.
  2822. *
  2823. * Add one to MSG_EXT_SDTR_LEN to account for
  2824. * the extended message preamble.
  2825. */
  2826. if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  2827. break;
  2828. period = ahc->msgin_buf[3];
  2829. ppr_options = 0;
  2830. saved_offset = offset = ahc->msgin_buf[4];
  2831. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2832. &ppr_options,
  2833. devinfo->role);
  2834. ahc_validate_offset(ahc, tinfo, syncrate, &offset,
  2835. targ_scsirate & WIDEXFER,
  2836. devinfo->role);
  2837. if (bootverbose) {
  2838. printf("(%s:%c:%d:%d): Received "
  2839. "SDTR period %x, offset %x\n\t"
  2840. "Filtered to period %x, offset %x\n",
  2841. ahc_name(ahc), devinfo->channel,
  2842. devinfo->target, devinfo->lun,
  2843. ahc->msgin_buf[3], saved_offset,
  2844. period, offset);
  2845. }
  2846. ahc_set_syncrate(ahc, devinfo,
  2847. syncrate, period,
  2848. offset, ppr_options,
  2849. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2850. /*paused*/TRUE);
  2851. /*
  2852. * See if we initiated Sync Negotiation
  2853. * and didn't have to fall down to async
  2854. * transfers.
  2855. */
  2856. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  2857. /* We started it */
  2858. if (saved_offset != offset) {
  2859. /* Went too low - force async */
  2860. reject = TRUE;
  2861. }
  2862. } else {
  2863. /*
  2864. * Send our own SDTR in reply
  2865. */
  2866. if (bootverbose
  2867. && devinfo->role == ROLE_INITIATOR) {
  2868. printf("(%s:%c:%d:%d): Target "
  2869. "Initiated SDTR\n",
  2870. ahc_name(ahc), devinfo->channel,
  2871. devinfo->target, devinfo->lun);
  2872. }
  2873. ahc->msgout_index = 0;
  2874. ahc->msgout_len = 0;
  2875. ahc_construct_sdtr(ahc, devinfo,
  2876. period, offset);
  2877. ahc->msgout_index = 0;
  2878. response = TRUE;
  2879. }
  2880. done = MSGLOOP_MSGCOMPLETE;
  2881. break;
  2882. }
  2883. case MSG_EXT_WDTR:
  2884. {
  2885. u_int bus_width;
  2886. u_int saved_width;
  2887. u_int sending_reply;
  2888. sending_reply = FALSE;
  2889. if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  2890. reject = TRUE;
  2891. break;
  2892. }
  2893. /*
  2894. * Wait until we have our arg before validating
  2895. * and acting on this message.
  2896. *
  2897. * Add one to MSG_EXT_WDTR_LEN to account for
  2898. * the extended message preamble.
  2899. */
  2900. if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  2901. break;
  2902. bus_width = ahc->msgin_buf[3];
  2903. saved_width = bus_width;
  2904. ahc_validate_width(ahc, tinfo, &bus_width,
  2905. devinfo->role);
  2906. if (bootverbose) {
  2907. printf("(%s:%c:%d:%d): Received WDTR "
  2908. "%x filtered to %x\n",
  2909. ahc_name(ahc), devinfo->channel,
  2910. devinfo->target, devinfo->lun,
  2911. saved_width, bus_width);
  2912. }
  2913. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  2914. /*
  2915. * Don't send a WDTR back to the
  2916. * target, since we asked first.
  2917. * If the width went higher than our
  2918. * request, reject it.
  2919. */
  2920. if (saved_width > bus_width) {
  2921. reject = TRUE;
  2922. printf("(%s:%c:%d:%d): requested %dBit "
  2923. "transfers. Rejecting...\n",
  2924. ahc_name(ahc), devinfo->channel,
  2925. devinfo->target, devinfo->lun,
  2926. 8 * (0x01 << bus_width));
  2927. bus_width = 0;
  2928. }
  2929. } else {
  2930. /*
  2931. * Send our own WDTR in reply
  2932. */
  2933. if (bootverbose
  2934. && devinfo->role == ROLE_INITIATOR) {
  2935. printf("(%s:%c:%d:%d): Target "
  2936. "Initiated WDTR\n",
  2937. ahc_name(ahc), devinfo->channel,
  2938. devinfo->target, devinfo->lun);
  2939. }
  2940. ahc->msgout_index = 0;
  2941. ahc->msgout_len = 0;
  2942. ahc_construct_wdtr(ahc, devinfo, bus_width);
  2943. ahc->msgout_index = 0;
  2944. response = TRUE;
  2945. sending_reply = TRUE;
  2946. }
  2947. /*
  2948. * After a wide message, we are async, but
  2949. * some devices don't seem to honor this portion
  2950. * of the spec. Force a renegotiation of the
  2951. * sync component of our transfer agreement even
  2952. * if our goal is async. By updating our width
  2953. * after forcing the negotiation, we avoid
  2954. * renegotiating for width.
  2955. */
  2956. ahc_update_neg_request(ahc, devinfo, tstate,
  2957. tinfo, AHC_NEG_ALWAYS);
  2958. ahc_set_width(ahc, devinfo, bus_width,
  2959. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2960. /*paused*/TRUE);
  2961. if (sending_reply == FALSE && reject == FALSE) {
  2962. /*
  2963. * We will always have an SDTR to send.
  2964. */
  2965. ahc->msgout_index = 0;
  2966. ahc->msgout_len = 0;
  2967. ahc_build_transfer_msg(ahc, devinfo);
  2968. ahc->msgout_index = 0;
  2969. response = TRUE;
  2970. }
  2971. done = MSGLOOP_MSGCOMPLETE;
  2972. break;
  2973. }
  2974. case MSG_EXT_PPR:
  2975. {
  2976. struct ahc_syncrate *syncrate;
  2977. u_int period;
  2978. u_int offset;
  2979. u_int bus_width;
  2980. u_int ppr_options;
  2981. u_int saved_width;
  2982. u_int saved_offset;
  2983. u_int saved_ppr_options;
  2984. if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  2985. reject = TRUE;
  2986. break;
  2987. }
  2988. /*
  2989. * Wait until we have all args before validating
  2990. * and acting on this message.
  2991. *
  2992. * Add one to MSG_EXT_PPR_LEN to account for
  2993. * the extended message preamble.
  2994. */
  2995. if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
  2996. break;
  2997. period = ahc->msgin_buf[3];
  2998. offset = ahc->msgin_buf[5];
  2999. bus_width = ahc->msgin_buf[6];
  3000. saved_width = bus_width;
  3001. ppr_options = ahc->msgin_buf[7];
  3002. /*
  3003. * According to the spec, a DT only
  3004. * period factor with no DT option
  3005. * set implies async.
  3006. */
  3007. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  3008. && period == 9)
  3009. offset = 0;
  3010. saved_ppr_options = ppr_options;
  3011. saved_offset = offset;
  3012. /*
  3013. * Mask out any options we don't support
  3014. * on any controller. Transfer options are
  3015. * only available if we are negotiating wide.
  3016. */
  3017. ppr_options &= MSG_EXT_PPR_DT_REQ;
  3018. if (bus_width == 0)
  3019. ppr_options = 0;
  3020. ahc_validate_width(ahc, tinfo, &bus_width,
  3021. devinfo->role);
  3022. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  3023. &ppr_options,
  3024. devinfo->role);
  3025. ahc_validate_offset(ahc, tinfo, syncrate,
  3026. &offset, bus_width,
  3027. devinfo->role);
  3028. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
  3029. /*
  3030. * If we are unable to do any of the
  3031. * requested options (we went too low),
  3032. * then we'll have to reject the message.
  3033. */
  3034. if (saved_width > bus_width
  3035. || saved_offset != offset
  3036. || saved_ppr_options != ppr_options) {
  3037. reject = TRUE;
  3038. period = 0;
  3039. offset = 0;
  3040. bus_width = 0;
  3041. ppr_options = 0;
  3042. syncrate = NULL;
  3043. }
  3044. } else {
  3045. if (devinfo->role != ROLE_TARGET)
  3046. printf("(%s:%c:%d:%d): Target "
  3047. "Initiated PPR\n",
  3048. ahc_name(ahc), devinfo->channel,
  3049. devinfo->target, devinfo->lun);
  3050. else
  3051. printf("(%s:%c:%d:%d): Initiator "
  3052. "Initiated PPR\n",
  3053. ahc_name(ahc), devinfo->channel,
  3054. devinfo->target, devinfo->lun);
  3055. ahc->msgout_index = 0;
  3056. ahc->msgout_len = 0;
  3057. ahc_construct_ppr(ahc, devinfo, period, offset,
  3058. bus_width, ppr_options);
  3059. ahc->msgout_index = 0;
  3060. response = TRUE;
  3061. }
  3062. if (bootverbose) {
  3063. printf("(%s:%c:%d:%d): Received PPR width %x, "
  3064. "period %x, offset %x,options %x\n"
  3065. "\tFiltered to width %x, period %x, "
  3066. "offset %x, options %x\n",
  3067. ahc_name(ahc), devinfo->channel,
  3068. devinfo->target, devinfo->lun,
  3069. saved_width, ahc->msgin_buf[3],
  3070. saved_offset, saved_ppr_options,
  3071. bus_width, period, offset, ppr_options);
  3072. }
  3073. ahc_set_width(ahc, devinfo, bus_width,
  3074. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3075. /*paused*/TRUE);
  3076. ahc_set_syncrate(ahc, devinfo,
  3077. syncrate, period,
  3078. offset, ppr_options,
  3079. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3080. /*paused*/TRUE);
  3081. done = MSGLOOP_MSGCOMPLETE;
  3082. break;
  3083. }
  3084. default:
  3085. /* Unknown extended message. Reject it. */
  3086. reject = TRUE;
  3087. break;
  3088. }
  3089. break;
  3090. }
  3091. #ifdef AHC_TARGET_MODE
  3092. case MSG_BUS_DEV_RESET:
  3093. ahc_handle_devreset(ahc, devinfo,
  3094. CAM_BDR_SENT,
  3095. "Bus Device Reset Received",
  3096. /*verbose_level*/0);
  3097. ahc_restart(ahc);
  3098. done = MSGLOOP_TERMINATED;
  3099. break;
  3100. case MSG_ABORT_TAG:
  3101. case MSG_ABORT:
  3102. case MSG_CLEAR_QUEUE:
  3103. {
  3104. int tag;
  3105. /* Target mode messages */
  3106. if (devinfo->role != ROLE_TARGET) {
  3107. reject = TRUE;
  3108. break;
  3109. }
  3110. tag = SCB_LIST_NULL;
  3111. if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
  3112. tag = ahc_inb(ahc, INITIATOR_TAG);
  3113. ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3114. devinfo->lun, tag, ROLE_TARGET,
  3115. CAM_REQ_ABORTED);
  3116. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3117. if (tstate != NULL) {
  3118. struct ahc_tmode_lstate* lstate;
  3119. lstate = tstate->enabled_luns[devinfo->lun];
  3120. if (lstate != NULL) {
  3121. ahc_queue_lstate_event(ahc, lstate,
  3122. devinfo->our_scsiid,
  3123. ahc->msgin_buf[0],
  3124. /*arg*/tag);
  3125. ahc_send_lstate_events(ahc, lstate);
  3126. }
  3127. }
  3128. ahc_restart(ahc);
  3129. done = MSGLOOP_TERMINATED;
  3130. break;
  3131. }
  3132. #endif
  3133. case MSG_TERM_IO_PROC:
  3134. default:
  3135. reject = TRUE;
  3136. break;
  3137. }
  3138. if (reject) {
  3139. /*
  3140. * Setup to reject the message.
  3141. */
  3142. ahc->msgout_index = 0;
  3143. ahc->msgout_len = 1;
  3144. ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
  3145. done = MSGLOOP_MSGCOMPLETE;
  3146. response = TRUE;
  3147. }
  3148. if (done != MSGLOOP_IN_PROG && !response)
  3149. /* Clear the outgoing message buffer */
  3150. ahc->msgout_len = 0;
  3151. return (done);
  3152. }
  3153. /*
  3154. * Process a message reject message.
  3155. */
  3156. static int
  3157. ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3158. {
  3159. /*
  3160. * What we care about here is if we had an
  3161. * outstanding SDTR or WDTR message for this
  3162. * target. If we did, this is a signal that
  3163. * the target is refusing negotiation.
  3164. */
  3165. struct scb *scb;
  3166. struct ahc_initiator_tinfo *tinfo;
  3167. struct ahc_tmode_tstate *tstate;
  3168. u_int scb_index;
  3169. u_int last_msg;
  3170. int response = 0;
  3171. scb_index = ahc_inb(ahc, SCB_TAG);
  3172. scb = ahc_lookup_scb(ahc, scb_index);
  3173. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
  3174. devinfo->our_scsiid,
  3175. devinfo->target, &tstate);
  3176. /* Might be necessary */
  3177. last_msg = ahc_inb(ahc, LAST_MSG);
  3178. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  3179. /*
  3180. * Target does not support the PPR message.
  3181. * Attempt to negotiate SPI-2 style.
  3182. */
  3183. if (bootverbose) {
  3184. printf("(%s:%c:%d:%d): PPR Rejected. "
  3185. "Trying WDTR/SDTR\n",
  3186. ahc_name(ahc), devinfo->channel,
  3187. devinfo->target, devinfo->lun);
  3188. }
  3189. tinfo->goal.ppr_options = 0;
  3190. tinfo->curr.transport_version = 2;
  3191. tinfo->goal.transport_version = 2;
  3192. ahc->msgout_index = 0;
  3193. ahc->msgout_len = 0;
  3194. ahc_build_transfer_msg(ahc, devinfo);
  3195. ahc->msgout_index = 0;
  3196. response = 1;
  3197. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  3198. /* note 8bit xfers */
  3199. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  3200. "8bit transfers\n", ahc_name(ahc),
  3201. devinfo->channel, devinfo->target, devinfo->lun);
  3202. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3203. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3204. /*paused*/TRUE);
  3205. /*
  3206. * No need to clear the sync rate. If the target
  3207. * did not accept the command, our syncrate is
  3208. * unaffected. If the target started the negotiation,
  3209. * but rejected our response, we already cleared the
  3210. * sync rate before sending our WDTR.
  3211. */
  3212. if (tinfo->goal.offset != tinfo->curr.offset) {
  3213. /* Start the sync negotiation */
  3214. ahc->msgout_index = 0;
  3215. ahc->msgout_len = 0;
  3216. ahc_build_transfer_msg(ahc, devinfo);
  3217. ahc->msgout_index = 0;
  3218. response = 1;
  3219. }
  3220. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  3221. /* note asynch xfers and clear flag */
  3222. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
  3223. /*offset*/0, /*ppr_options*/0,
  3224. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3225. /*paused*/TRUE);
  3226. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  3227. "Using asynchronous transfers\n",
  3228. ahc_name(ahc), devinfo->channel,
  3229. devinfo->target, devinfo->lun);
  3230. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  3231. int tag_type;
  3232. int mask;
  3233. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  3234. if (tag_type == MSG_SIMPLE_TASK) {
  3235. printf("(%s:%c:%d:%d): refuses tagged commands. "
  3236. "Performing non-tagged I/O\n", ahc_name(ahc),
  3237. devinfo->channel, devinfo->target, devinfo->lun);
  3238. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_NONE);
  3239. mask = ~0x23;
  3240. } else {
  3241. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  3242. "Performing simple queue tagged I/O only\n",
  3243. ahc_name(ahc), devinfo->channel, devinfo->target,
  3244. devinfo->lun, tag_type == MSG_ORDERED_TASK
  3245. ? "ordered" : "head of queue");
  3246. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_BASIC);
  3247. mask = ~0x03;
  3248. }
  3249. /*
  3250. * Resend the identify for this CCB as the target
  3251. * may believe that the selection is invalid otherwise.
  3252. */
  3253. ahc_outb(ahc, SCB_CONTROL,
  3254. ahc_inb(ahc, SCB_CONTROL) & mask);
  3255. scb->hscb->control &= mask;
  3256. ahc_set_transaction_tag(scb, /*enabled*/FALSE,
  3257. /*type*/MSG_SIMPLE_TASK);
  3258. ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
  3259. ahc_assert_atn(ahc);
  3260. /*
  3261. * This transaction is now at the head of
  3262. * the untagged queue for this target.
  3263. */
  3264. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  3265. struct scb_tailq *untagged_q;
  3266. untagged_q =
  3267. &(ahc->untagged_queues[devinfo->target_offset]);
  3268. TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
  3269. scb->flags |= SCB_UNTAGGEDQ;
  3270. }
  3271. ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  3272. scb->hscb->tag);
  3273. /*
  3274. * Requeue all tagged commands for this target
  3275. * currently in our posession so they can be
  3276. * converted to untagged commands.
  3277. */
  3278. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  3279. SCB_GET_CHANNEL(ahc, scb),
  3280. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  3281. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  3282. SEARCH_COMPLETE);
  3283. } else {
  3284. /*
  3285. * Otherwise, we ignore it.
  3286. */
  3287. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  3288. ahc_name(ahc), devinfo->channel, devinfo->target,
  3289. last_msg);
  3290. }
  3291. return (response);
  3292. }
  3293. /*
  3294. * Process an ingnore wide residue message.
  3295. */
  3296. static void
  3297. ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3298. {
  3299. u_int scb_index;
  3300. struct scb *scb;
  3301. scb_index = ahc_inb(ahc, SCB_TAG);
  3302. scb = ahc_lookup_scb(ahc, scb_index);
  3303. /*
  3304. * XXX Actually check data direction in the sequencer?
  3305. * Perhaps add datadir to some spare bits in the hscb?
  3306. */
  3307. if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
  3308. || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
  3309. /*
  3310. * Ignore the message if we haven't
  3311. * seen an appropriate data phase yet.
  3312. */
  3313. } else {
  3314. /*
  3315. * If the residual occurred on the last
  3316. * transfer and the transfer request was
  3317. * expected to end on an odd count, do
  3318. * nothing. Otherwise, subtract a byte
  3319. * and update the residual count accordingly.
  3320. */
  3321. uint32_t sgptr;
  3322. sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3323. if ((sgptr & SG_LIST_NULL) != 0
  3324. && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
  3325. /*
  3326. * If the residual occurred on the last
  3327. * transfer and the transfer request was
  3328. * expected to end on an odd count, do
  3329. * nothing.
  3330. */
  3331. } else {
  3332. struct ahc_dma_seg *sg;
  3333. uint32_t data_cnt;
  3334. uint32_t data_addr;
  3335. uint32_t sglen;
  3336. /* Pull in all of the sgptr */
  3337. sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
  3338. data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
  3339. if ((sgptr & SG_LIST_NULL) != 0) {
  3340. /*
  3341. * The residual data count is not updated
  3342. * for the command run to completion case.
  3343. * Explicitly zero the count.
  3344. */
  3345. data_cnt &= ~AHC_SG_LEN_MASK;
  3346. }
  3347. data_addr = ahc_inl(ahc, SHADDR);
  3348. data_cnt += 1;
  3349. data_addr -= 1;
  3350. sgptr &= SG_PTR_MASK;
  3351. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3352. /*
  3353. * The residual sg ptr points to the next S/G
  3354. * to load so we must go back one.
  3355. */
  3356. sg--;
  3357. sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  3358. if (sg != scb->sg_list
  3359. && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
  3360. sg--;
  3361. sglen = ahc_le32toh(sg->len);
  3362. /*
  3363. * Preserve High Address and SG_LIST bits
  3364. * while setting the count to 1.
  3365. */
  3366. data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
  3367. data_addr = ahc_le32toh(sg->addr)
  3368. + (sglen & AHC_SG_LEN_MASK) - 1;
  3369. /*
  3370. * Increment sg so it points to the
  3371. * "next" sg.
  3372. */
  3373. sg++;
  3374. sgptr = ahc_sg_virt_to_bus(scb, sg);
  3375. }
  3376. ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
  3377. ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
  3378. /*
  3379. * Toggle the "oddness" of the transfer length
  3380. * to handle this mid-transfer ignore wide
  3381. * residue. This ensures that the oddness is
  3382. * correct for subsequent data transfers.
  3383. */
  3384. ahc_outb(ahc, SCB_LUN,
  3385. ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
  3386. }
  3387. }
  3388. }
  3389. /*
  3390. * Reinitialize the data pointers for the active transfer
  3391. * based on its current residual.
  3392. */
  3393. static void
  3394. ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
  3395. {
  3396. struct scb *scb;
  3397. struct ahc_dma_seg *sg;
  3398. u_int scb_index;
  3399. uint32_t sgptr;
  3400. uint32_t resid;
  3401. uint32_t dataptr;
  3402. scb_index = ahc_inb(ahc, SCB_TAG);
  3403. scb = ahc_lookup_scb(ahc, scb_index);
  3404. sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3405. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3406. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
  3407. | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3408. sgptr &= SG_PTR_MASK;
  3409. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3410. /* The residual sg_ptr always points to the next sg */
  3411. sg--;
  3412. resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
  3413. | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
  3414. | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
  3415. dataptr = ahc_le32toh(sg->addr)
  3416. + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
  3417. - resid;
  3418. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  3419. u_int dscommand1;
  3420. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  3421. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  3422. ahc_outb(ahc, HADDR,
  3423. (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
  3424. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  3425. }
  3426. ahc_outb(ahc, HADDR + 3, dataptr >> 24);
  3427. ahc_outb(ahc, HADDR + 2, dataptr >> 16);
  3428. ahc_outb(ahc, HADDR + 1, dataptr >> 8);
  3429. ahc_outb(ahc, HADDR, dataptr);
  3430. ahc_outb(ahc, HCNT + 2, resid >> 16);
  3431. ahc_outb(ahc, HCNT + 1, resid >> 8);
  3432. ahc_outb(ahc, HCNT, resid);
  3433. if ((ahc->features & AHC_ULTRA2) == 0) {
  3434. ahc_outb(ahc, STCNT + 2, resid >> 16);
  3435. ahc_outb(ahc, STCNT + 1, resid >> 8);
  3436. ahc_outb(ahc, STCNT, resid);
  3437. }
  3438. }
  3439. /*
  3440. * Handle the effects of issuing a bus device reset message.
  3441. */
  3442. static void
  3443. ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3444. cam_status status, char *message, int verbose_level)
  3445. {
  3446. #ifdef AHC_TARGET_MODE
  3447. struct ahc_tmode_tstate* tstate;
  3448. u_int lun;
  3449. #endif
  3450. int found;
  3451. found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3452. CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
  3453. status);
  3454. #ifdef AHC_TARGET_MODE
  3455. /*
  3456. * Send an immediate notify ccb to all target mord peripheral
  3457. * drivers affected by this action.
  3458. */
  3459. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3460. if (tstate != NULL) {
  3461. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  3462. struct ahc_tmode_lstate* lstate;
  3463. lstate = tstate->enabled_luns[lun];
  3464. if (lstate == NULL)
  3465. continue;
  3466. ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
  3467. MSG_BUS_DEV_RESET, /*arg*/0);
  3468. ahc_send_lstate_events(ahc, lstate);
  3469. }
  3470. }
  3471. #endif
  3472. /*
  3473. * Go back to async/narrow transfers and renegotiate.
  3474. */
  3475. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3476. AHC_TRANS_CUR, /*paused*/TRUE);
  3477. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
  3478. /*period*/0, /*offset*/0, /*ppr_options*/0,
  3479. AHC_TRANS_CUR, /*paused*/TRUE);
  3480. if (status != CAM_SEL_TIMEOUT)
  3481. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  3482. CAM_LUN_WILDCARD, AC_SENT_BDR);
  3483. if (message != NULL
  3484. && (verbose_level <= bootverbose))
  3485. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
  3486. message, devinfo->channel, devinfo->target, found);
  3487. }
  3488. #ifdef AHC_TARGET_MODE
  3489. static void
  3490. ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3491. struct scb *scb)
  3492. {
  3493. /*
  3494. * To facilitate adding multiple messages together,
  3495. * each routine should increment the index and len
  3496. * variables instead of setting them explicitly.
  3497. */
  3498. ahc->msgout_index = 0;
  3499. ahc->msgout_len = 0;
  3500. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  3501. ahc_build_transfer_msg(ahc, devinfo);
  3502. else
  3503. panic("ahc_intr: AWAITING target message with no message");
  3504. ahc->msgout_index = 0;
  3505. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  3506. }
  3507. #endif
  3508. /**************************** Initialization **********************************/
  3509. /*
  3510. * Allocate a controller structure for a new device
  3511. * and perform initial initializion.
  3512. */
  3513. struct ahc_softc *
  3514. ahc_alloc(void *platform_arg, char *name)
  3515. {
  3516. struct ahc_softc *ahc;
  3517. int i;
  3518. #ifndef __FreeBSD__
  3519. ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
  3520. if (!ahc) {
  3521. printf("aic7xxx: cannot malloc softc!\n");
  3522. free(name, M_DEVBUF);
  3523. return NULL;
  3524. }
  3525. #else
  3526. ahc = device_get_softc((device_t)platform_arg);
  3527. #endif
  3528. memset(ahc, 0, sizeof(*ahc));
  3529. ahc->seep_config = malloc(sizeof(*ahc->seep_config),
  3530. M_DEVBUF, M_NOWAIT);
  3531. if (ahc->seep_config == NULL) {
  3532. #ifndef __FreeBSD__
  3533. free(ahc, M_DEVBUF);
  3534. #endif
  3535. free(name, M_DEVBUF);
  3536. return (NULL);
  3537. }
  3538. LIST_INIT(&ahc->pending_scbs);
  3539. /* We don't know our unit number until the OSM sets it */
  3540. ahc->name = name;
  3541. ahc->unit = -1;
  3542. ahc->description = NULL;
  3543. ahc->channel = 'A';
  3544. ahc->channel_b = 'B';
  3545. ahc->chip = AHC_NONE;
  3546. ahc->features = AHC_FENONE;
  3547. ahc->bugs = AHC_BUGNONE;
  3548. ahc->flags = AHC_FNONE;
  3549. /*
  3550. * Default to all error reporting enabled with the
  3551. * sequencer operating at its fastest speed.
  3552. * The bus attach code may modify this.
  3553. */
  3554. ahc->seqctl = FASTMODE;
  3555. for (i = 0; i < AHC_NUM_TARGETS; i++)
  3556. TAILQ_INIT(&ahc->untagged_queues[i]);
  3557. if (ahc_platform_alloc(ahc, platform_arg) != 0) {
  3558. ahc_free(ahc);
  3559. ahc = NULL;
  3560. }
  3561. return (ahc);
  3562. }
  3563. int
  3564. ahc_softc_init(struct ahc_softc *ahc)
  3565. {
  3566. /* The IRQMS bit is only valid on VL and EISA chips */
  3567. if ((ahc->chip & AHC_PCI) == 0)
  3568. ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
  3569. else
  3570. ahc->unpause = 0;
  3571. ahc->pause = ahc->unpause | PAUSE;
  3572. /* XXX The shared scb data stuff should be deprecated */
  3573. if (ahc->scb_data == NULL) {
  3574. ahc->scb_data = malloc(sizeof(*ahc->scb_data),
  3575. M_DEVBUF, M_NOWAIT);
  3576. if (ahc->scb_data == NULL)
  3577. return (ENOMEM);
  3578. memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
  3579. }
  3580. return (0);
  3581. }
  3582. void
  3583. ahc_set_unit(struct ahc_softc *ahc, int unit)
  3584. {
  3585. ahc->unit = unit;
  3586. }
  3587. void
  3588. ahc_set_name(struct ahc_softc *ahc, char *name)
  3589. {
  3590. if (ahc->name != NULL)
  3591. free(ahc->name, M_DEVBUF);
  3592. ahc->name = name;
  3593. }
  3594. void
  3595. ahc_free(struct ahc_softc *ahc)
  3596. {
  3597. int i;
  3598. switch (ahc->init_level) {
  3599. default:
  3600. case 5:
  3601. ahc_shutdown(ahc);
  3602. /* FALLTHROUGH */
  3603. case 4:
  3604. ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
  3605. ahc->shared_data_dmamap);
  3606. /* FALLTHROUGH */
  3607. case 3:
  3608. ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
  3609. ahc->shared_data_dmamap);
  3610. ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
  3611. ahc->shared_data_dmamap);
  3612. /* FALLTHROUGH */
  3613. case 2:
  3614. ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
  3615. case 1:
  3616. #ifndef __linux__
  3617. ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
  3618. #endif
  3619. break;
  3620. case 0:
  3621. break;
  3622. }
  3623. #ifndef __linux__
  3624. ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
  3625. #endif
  3626. ahc_platform_free(ahc);
  3627. ahc_fini_scbdata(ahc);
  3628. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  3629. struct ahc_tmode_tstate *tstate;
  3630. tstate = ahc->enabled_targets[i];
  3631. if (tstate != NULL) {
  3632. #ifdef AHC_TARGET_MODE
  3633. int j;
  3634. for (j = 0; j < AHC_NUM_LUNS; j++) {
  3635. struct ahc_tmode_lstate *lstate;
  3636. lstate = tstate->enabled_luns[j];
  3637. if (lstate != NULL) {
  3638. xpt_free_path(lstate->path);
  3639. free(lstate, M_DEVBUF);
  3640. }
  3641. }
  3642. #endif
  3643. free(tstate, M_DEVBUF);
  3644. }
  3645. }
  3646. #ifdef AHC_TARGET_MODE
  3647. if (ahc->black_hole != NULL) {
  3648. xpt_free_path(ahc->black_hole->path);
  3649. free(ahc->black_hole, M_DEVBUF);
  3650. }
  3651. #endif
  3652. if (ahc->name != NULL)
  3653. free(ahc->name, M_DEVBUF);
  3654. if (ahc->seep_config != NULL)
  3655. free(ahc->seep_config, M_DEVBUF);
  3656. #ifndef __FreeBSD__
  3657. free(ahc, M_DEVBUF);
  3658. #endif
  3659. return;
  3660. }
  3661. void
  3662. ahc_shutdown(void *arg)
  3663. {
  3664. struct ahc_softc *ahc;
  3665. int i;
  3666. ahc = (struct ahc_softc *)arg;
  3667. /* This will reset most registers to 0, but not all */
  3668. ahc_reset(ahc, /*reinit*/FALSE);
  3669. ahc_outb(ahc, SCSISEQ, 0);
  3670. ahc_outb(ahc, SXFRCTL0, 0);
  3671. ahc_outb(ahc, DSPCISTATUS, 0);
  3672. for (i = TARG_SCSIRATE; i < SCSICONF; i++)
  3673. ahc_outb(ahc, i, 0);
  3674. }
  3675. /*
  3676. * Reset the controller and record some information about it
  3677. * that is only available just after a reset. If "reinit" is
  3678. * non-zero, this reset occured after initial configuration
  3679. * and the caller requests that the chip be fully reinitialized
  3680. * to a runable state. Chip interrupts are *not* enabled after
  3681. * a reinitialization. The caller must enable interrupts via
  3682. * ahc_intr_enable().
  3683. */
  3684. int
  3685. ahc_reset(struct ahc_softc *ahc, int reinit)
  3686. {
  3687. u_int sblkctl;
  3688. u_int sxfrctl1_a, sxfrctl1_b;
  3689. int error;
  3690. int wait;
  3691. /*
  3692. * Preserve the value of the SXFRCTL1 register for all channels.
  3693. * It contains settings that affect termination and we don't want
  3694. * to disturb the integrity of the bus.
  3695. */
  3696. ahc_pause(ahc);
  3697. sxfrctl1_b = 0;
  3698. if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
  3699. u_int sblkctl;
  3700. /*
  3701. * Save channel B's settings in case this chip
  3702. * is setup for TWIN channel operation.
  3703. */
  3704. sblkctl = ahc_inb(ahc, SBLKCTL);
  3705. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3706. sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
  3707. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3708. }
  3709. sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
  3710. ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
  3711. /*
  3712. * Ensure that the reset has finished. We delay 1000us
  3713. * prior to reading the register to make sure the chip
  3714. * has sufficiently completed its reset to handle register
  3715. * accesses.
  3716. */
  3717. wait = 1000;
  3718. do {
  3719. ahc_delay(1000);
  3720. } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
  3721. if (wait == 0) {
  3722. printf("%s: WARNING - Failed chip reset! "
  3723. "Trying to initialize anyway.\n", ahc_name(ahc));
  3724. }
  3725. ahc_outb(ahc, HCNTRL, ahc->pause);
  3726. /* Determine channel configuration */
  3727. sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
  3728. /* No Twin Channel PCI cards */
  3729. if ((ahc->chip & AHC_PCI) != 0)
  3730. sblkctl &= ~SELBUSB;
  3731. switch (sblkctl) {
  3732. case 0:
  3733. /* Single Narrow Channel */
  3734. break;
  3735. case 2:
  3736. /* Wide Channel */
  3737. ahc->features |= AHC_WIDE;
  3738. break;
  3739. case 8:
  3740. /* Twin Channel */
  3741. ahc->features |= AHC_TWIN;
  3742. break;
  3743. default:
  3744. printf(" Unsupported adapter type. Ignoring\n");
  3745. return(-1);
  3746. }
  3747. /*
  3748. * Reload sxfrctl1.
  3749. *
  3750. * We must always initialize STPWEN to 1 before we
  3751. * restore the saved values. STPWEN is initialized
  3752. * to a tri-state condition which can only be cleared
  3753. * by turning it on.
  3754. */
  3755. if ((ahc->features & AHC_TWIN) != 0) {
  3756. u_int sblkctl;
  3757. sblkctl = ahc_inb(ahc, SBLKCTL);
  3758. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3759. ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
  3760. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3761. }
  3762. ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
  3763. error = 0;
  3764. if (reinit != 0)
  3765. /*
  3766. * If a recovery action has forced a chip reset,
  3767. * re-initialize the chip to our liking.
  3768. */
  3769. error = ahc->bus_chip_init(ahc);
  3770. #ifdef AHC_DUMP_SEQ
  3771. else
  3772. ahc_dumpseq(ahc);
  3773. #endif
  3774. return (error);
  3775. }
  3776. /*
  3777. * Determine the number of SCBs available on the controller
  3778. */
  3779. int
  3780. ahc_probe_scbs(struct ahc_softc *ahc) {
  3781. int i;
  3782. for (i = 0; i < AHC_SCB_MAX; i++) {
  3783. ahc_outb(ahc, SCBPTR, i);
  3784. ahc_outb(ahc, SCB_BASE, i);
  3785. if (ahc_inb(ahc, SCB_BASE) != i)
  3786. break;
  3787. ahc_outb(ahc, SCBPTR, 0);
  3788. if (ahc_inb(ahc, SCB_BASE) != 0)
  3789. break;
  3790. }
  3791. return (i);
  3792. }
  3793. static void
  3794. ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  3795. {
  3796. dma_addr_t *baddr;
  3797. baddr = (dma_addr_t *)arg;
  3798. *baddr = segs->ds_addr;
  3799. }
  3800. static void
  3801. ahc_build_free_scb_list(struct ahc_softc *ahc)
  3802. {
  3803. int scbsize;
  3804. int i;
  3805. scbsize = 32;
  3806. if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
  3807. scbsize = 64;
  3808. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  3809. int j;
  3810. ahc_outb(ahc, SCBPTR, i);
  3811. /*
  3812. * Touch all SCB bytes to avoid parity errors
  3813. * should one of our debugging routines read
  3814. * an otherwise uninitiatlized byte.
  3815. */
  3816. for (j = 0; j < scbsize; j++)
  3817. ahc_outb(ahc, SCB_BASE+j, 0xFF);
  3818. /* Clear the control byte. */
  3819. ahc_outb(ahc, SCB_CONTROL, 0);
  3820. /* Set the next pointer */
  3821. if ((ahc->flags & AHC_PAGESCBS) != 0)
  3822. ahc_outb(ahc, SCB_NEXT, i+1);
  3823. else
  3824. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3825. /* Make the tag number, SCSIID, and lun invalid */
  3826. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  3827. ahc_outb(ahc, SCB_SCSIID, 0xFF);
  3828. ahc_outb(ahc, SCB_LUN, 0xFF);
  3829. }
  3830. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  3831. /* SCB 0 heads the free list. */
  3832. ahc_outb(ahc, FREE_SCBH, 0);
  3833. } else {
  3834. /* No free list. */
  3835. ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
  3836. }
  3837. /* Make sure that the last SCB terminates the free list */
  3838. ahc_outb(ahc, SCBPTR, i-1);
  3839. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3840. }
  3841. static int
  3842. ahc_init_scbdata(struct ahc_softc *ahc)
  3843. {
  3844. struct scb_data *scb_data;
  3845. scb_data = ahc->scb_data;
  3846. SLIST_INIT(&scb_data->free_scbs);
  3847. SLIST_INIT(&scb_data->sg_maps);
  3848. /* Allocate SCB resources */
  3849. scb_data->scbarray =
  3850. (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
  3851. M_DEVBUF, M_NOWAIT);
  3852. if (scb_data->scbarray == NULL)
  3853. return (ENOMEM);
  3854. memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
  3855. /* Determine the number of hardware SCBs and initialize them */
  3856. scb_data->maxhscbs = ahc_probe_scbs(ahc);
  3857. if (ahc->scb_data->maxhscbs == 0) {
  3858. printf("%s: No SCB space found\n", ahc_name(ahc));
  3859. return (ENXIO);
  3860. }
  3861. /*
  3862. * Create our DMA tags. These tags define the kinds of device
  3863. * accessible memory allocations and memory mappings we will
  3864. * need to perform during normal operation.
  3865. *
  3866. * Unless we need to further restrict the allocation, we rely
  3867. * on the restrictions of the parent dmat, hence the common
  3868. * use of MAXADDR and MAXSIZE.
  3869. */
  3870. /* DMA tag for our hardware scb structures */
  3871. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3872. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3873. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3874. /*highaddr*/BUS_SPACE_MAXADDR,
  3875. /*filter*/NULL, /*filterarg*/NULL,
  3876. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3877. /*nsegments*/1,
  3878. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3879. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  3880. goto error_exit;
  3881. }
  3882. scb_data->init_level++;
  3883. /* Allocation for our hscbs */
  3884. if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
  3885. (void **)&scb_data->hscbs,
  3886. BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
  3887. goto error_exit;
  3888. }
  3889. scb_data->init_level++;
  3890. /* And permanently map them */
  3891. ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
  3892. scb_data->hscbs,
  3893. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3894. ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
  3895. scb_data->init_level++;
  3896. /* DMA tag for our sense buffers */
  3897. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3898. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3899. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3900. /*highaddr*/BUS_SPACE_MAXADDR,
  3901. /*filter*/NULL, /*filterarg*/NULL,
  3902. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3903. /*nsegments*/1,
  3904. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3905. /*flags*/0, &scb_data->sense_dmat) != 0) {
  3906. goto error_exit;
  3907. }
  3908. scb_data->init_level++;
  3909. /* Allocate them */
  3910. if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
  3911. (void **)&scb_data->sense,
  3912. BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
  3913. goto error_exit;
  3914. }
  3915. scb_data->init_level++;
  3916. /* And permanently map them */
  3917. ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
  3918. scb_data->sense,
  3919. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3920. ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
  3921. scb_data->init_level++;
  3922. /* DMA tag for our S/G structures. We allocate in page sized chunks */
  3923. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
  3924. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3925. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3926. /*highaddr*/BUS_SPACE_MAXADDR,
  3927. /*filter*/NULL, /*filterarg*/NULL,
  3928. PAGE_SIZE, /*nsegments*/1,
  3929. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3930. /*flags*/0, &scb_data->sg_dmat) != 0) {
  3931. goto error_exit;
  3932. }
  3933. scb_data->init_level++;
  3934. /* Perform initial CCB allocation */
  3935. memset(scb_data->hscbs, 0,
  3936. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
  3937. ahc_alloc_scbs(ahc);
  3938. if (scb_data->numscbs == 0) {
  3939. printf("%s: ahc_init_scbdata - "
  3940. "Unable to allocate initial scbs\n",
  3941. ahc_name(ahc));
  3942. goto error_exit;
  3943. }
  3944. /*
  3945. * Reserve the next queued SCB.
  3946. */
  3947. ahc->next_queued_scb = ahc_get_scb(ahc);
  3948. /*
  3949. * Note that we were successfull
  3950. */
  3951. return (0);
  3952. error_exit:
  3953. return (ENOMEM);
  3954. }
  3955. static void
  3956. ahc_fini_scbdata(struct ahc_softc *ahc)
  3957. {
  3958. struct scb_data *scb_data;
  3959. scb_data = ahc->scb_data;
  3960. if (scb_data == NULL)
  3961. return;
  3962. switch (scb_data->init_level) {
  3963. default:
  3964. case 7:
  3965. {
  3966. struct sg_map_node *sg_map;
  3967. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
  3968. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  3969. ahc_dmamap_unload(ahc, scb_data->sg_dmat,
  3970. sg_map->sg_dmamap);
  3971. ahc_dmamem_free(ahc, scb_data->sg_dmat,
  3972. sg_map->sg_vaddr,
  3973. sg_map->sg_dmamap);
  3974. free(sg_map, M_DEVBUF);
  3975. }
  3976. ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
  3977. }
  3978. case 6:
  3979. ahc_dmamap_unload(ahc, scb_data->sense_dmat,
  3980. scb_data->sense_dmamap);
  3981. case 5:
  3982. ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
  3983. scb_data->sense_dmamap);
  3984. ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
  3985. scb_data->sense_dmamap);
  3986. case 4:
  3987. ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
  3988. case 3:
  3989. ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
  3990. scb_data->hscb_dmamap);
  3991. case 2:
  3992. ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
  3993. scb_data->hscb_dmamap);
  3994. ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
  3995. scb_data->hscb_dmamap);
  3996. case 1:
  3997. ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
  3998. break;
  3999. case 0:
  4000. break;
  4001. }
  4002. if (scb_data->scbarray != NULL)
  4003. free(scb_data->scbarray, M_DEVBUF);
  4004. }
  4005. void
  4006. ahc_alloc_scbs(struct ahc_softc *ahc)
  4007. {
  4008. struct scb_data *scb_data;
  4009. struct scb *next_scb;
  4010. struct sg_map_node *sg_map;
  4011. dma_addr_t physaddr;
  4012. struct ahc_dma_seg *segs;
  4013. int newcount;
  4014. int i;
  4015. scb_data = ahc->scb_data;
  4016. if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
  4017. /* Can't allocate any more */
  4018. return;
  4019. next_scb = &scb_data->scbarray[scb_data->numscbs];
  4020. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  4021. if (sg_map == NULL)
  4022. return;
  4023. /* Allocate S/G space for the next batch of SCBS */
  4024. if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
  4025. (void **)&sg_map->sg_vaddr,
  4026. BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  4027. free(sg_map, M_DEVBUF);
  4028. return;
  4029. }
  4030. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  4031. ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
  4032. sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
  4033. &sg_map->sg_physaddr, /*flags*/0);
  4034. segs = sg_map->sg_vaddr;
  4035. physaddr = sg_map->sg_physaddr;
  4036. newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
  4037. newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
  4038. for (i = 0; i < newcount; i++) {
  4039. struct scb_platform_data *pdata;
  4040. #ifndef __linux__
  4041. int error;
  4042. #endif
  4043. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  4044. M_DEVBUF, M_NOWAIT);
  4045. if (pdata == NULL)
  4046. break;
  4047. next_scb->platform_data = pdata;
  4048. next_scb->sg_map = sg_map;
  4049. next_scb->sg_list = segs;
  4050. /*
  4051. * The sequencer always starts with the second entry.
  4052. * The first entry is embedded in the scb.
  4053. */
  4054. next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
  4055. next_scb->ahc_softc = ahc;
  4056. next_scb->flags = SCB_FREE;
  4057. #ifndef __linux__
  4058. error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
  4059. &next_scb->dmamap);
  4060. if (error != 0)
  4061. break;
  4062. #endif
  4063. next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
  4064. next_scb->hscb->tag = ahc->scb_data->numscbs;
  4065. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
  4066. next_scb, links.sle);
  4067. segs += AHC_NSEG;
  4068. physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
  4069. next_scb++;
  4070. ahc->scb_data->numscbs++;
  4071. }
  4072. }
  4073. void
  4074. ahc_controller_info(struct ahc_softc *ahc, char *buf)
  4075. {
  4076. int len;
  4077. len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
  4078. buf += len;
  4079. if ((ahc->features & AHC_TWIN) != 0)
  4080. len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
  4081. "B SCSI Id=%d, primary %c, ",
  4082. ahc->our_id, ahc->our_id_b,
  4083. (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
  4084. else {
  4085. const char *speed;
  4086. const char *type;
  4087. speed = "";
  4088. if ((ahc->features & AHC_ULTRA) != 0) {
  4089. speed = "Ultra ";
  4090. } else if ((ahc->features & AHC_DT) != 0) {
  4091. speed = "Ultra160 ";
  4092. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  4093. speed = "Ultra2 ";
  4094. }
  4095. if ((ahc->features & AHC_WIDE) != 0) {
  4096. type = "Wide";
  4097. } else {
  4098. type = "Single";
  4099. }
  4100. len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
  4101. speed, type, ahc->channel, ahc->our_id);
  4102. }
  4103. buf += len;
  4104. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4105. sprintf(buf, "%d/%d SCBs",
  4106. ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
  4107. else
  4108. sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
  4109. }
  4110. int
  4111. ahc_chip_init(struct ahc_softc *ahc)
  4112. {
  4113. int term;
  4114. int error;
  4115. u_int i;
  4116. u_int scsi_conf;
  4117. u_int scsiseq_template;
  4118. uint32_t physaddr;
  4119. ahc_outb(ahc, SEQ_FLAGS, 0);
  4120. ahc_outb(ahc, SEQ_FLAGS2, 0);
  4121. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
  4122. if (ahc->features & AHC_TWIN) {
  4123. /*
  4124. * Setup Channel B first.
  4125. */
  4126. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
  4127. term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
  4128. ahc_outb(ahc, SCSIID, ahc->our_id_b);
  4129. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4130. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4131. |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
  4132. if ((ahc->features & AHC_ULTRA2) != 0)
  4133. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4134. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4135. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4136. /* Select Channel A */
  4137. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
  4138. }
  4139. term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
  4140. if ((ahc->features & AHC_ULTRA2) != 0)
  4141. ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
  4142. else
  4143. ahc_outb(ahc, SCSIID, ahc->our_id);
  4144. scsi_conf = ahc_inb(ahc, SCSICONF);
  4145. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4146. |term|ahc->seltime
  4147. |ENSTIMER|ACTNEGEN);
  4148. if ((ahc->features & AHC_ULTRA2) != 0)
  4149. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4150. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4151. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4152. /* There are no untagged SCBs active yet. */
  4153. for (i = 0; i < 16; i++) {
  4154. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
  4155. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4156. int lun;
  4157. /*
  4158. * The SCB based BTT allows an entry per
  4159. * target and lun pair.
  4160. */
  4161. for (lun = 1; lun < AHC_NUM_LUNS; lun++)
  4162. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
  4163. }
  4164. }
  4165. /* All of our queues are empty */
  4166. for (i = 0; i < 256; i++)
  4167. ahc->qoutfifo[i] = SCB_LIST_NULL;
  4168. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
  4169. for (i = 0; i < 256; i++)
  4170. ahc->qinfifo[i] = SCB_LIST_NULL;
  4171. if ((ahc->features & AHC_MULTI_TID) != 0) {
  4172. ahc_outb(ahc, TARGID, 0);
  4173. ahc_outb(ahc, TARGID + 1, 0);
  4174. }
  4175. /*
  4176. * Tell the sequencer where it can find our arrays in memory.
  4177. */
  4178. physaddr = ahc->scb_data->hscb_busaddr;
  4179. ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
  4180. ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
  4181. ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
  4182. ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
  4183. physaddr = ahc->shared_data_busaddr;
  4184. ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
  4185. ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
  4186. ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
  4187. ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
  4188. /*
  4189. * Initialize the group code to command length table.
  4190. * This overrides the values in TARG_SCSIRATE, so only
  4191. * setup the table after we have processed that information.
  4192. */
  4193. ahc_outb(ahc, CMDSIZE_TABLE, 5);
  4194. ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
  4195. ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
  4196. ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
  4197. ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
  4198. ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
  4199. ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
  4200. ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
  4201. if ((ahc->features & AHC_HS_MAILBOX) != 0)
  4202. ahc_outb(ahc, HS_MAILBOX, 0);
  4203. /* Tell the sequencer of our initial queue positions */
  4204. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4205. ahc->tqinfifonext = 1;
  4206. ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
  4207. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  4208. }
  4209. ahc->qinfifonext = 0;
  4210. ahc->qoutfifonext = 0;
  4211. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4212. ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
  4213. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4214. ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
  4215. ahc_outb(ahc, SDSCB_QOFF, 0);
  4216. } else {
  4217. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4218. ahc_outb(ahc, QINPOS, ahc->qinfifonext);
  4219. ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
  4220. }
  4221. /* We don't have any waiting selections */
  4222. ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
  4223. /* Our disconnection list is empty too */
  4224. ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
  4225. /* Message out buffer starts empty */
  4226. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  4227. /*
  4228. * Setup the allowed SCSI Sequences based on operational mode.
  4229. * If we are a target, we'll enalbe select in operations once
  4230. * we've had a lun enabled.
  4231. */
  4232. scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
  4233. if ((ahc->flags & AHC_INITIATORROLE) != 0)
  4234. scsiseq_template |= ENRSELI;
  4235. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
  4236. /* Initialize our list of free SCBs. */
  4237. ahc_build_free_scb_list(ahc);
  4238. /*
  4239. * Tell the sequencer which SCB will be the next one it receives.
  4240. */
  4241. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4242. /*
  4243. * Load the Sequencer program and Enable the adapter
  4244. * in "fast" mode.
  4245. */
  4246. if (bootverbose)
  4247. printf("%s: Downloading Sequencer Program...",
  4248. ahc_name(ahc));
  4249. error = ahc_loadseq(ahc);
  4250. if (error != 0)
  4251. return (error);
  4252. if ((ahc->features & AHC_ULTRA2) != 0) {
  4253. int wait;
  4254. /*
  4255. * Wait for up to 500ms for our transceivers
  4256. * to settle. If the adapter does not have
  4257. * a cable attached, the transceivers may
  4258. * never settle, so don't complain if we
  4259. * fail here.
  4260. */
  4261. for (wait = 5000;
  4262. (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  4263. wait--)
  4264. ahc_delay(100);
  4265. }
  4266. ahc_restart(ahc);
  4267. return (0);
  4268. }
  4269. /*
  4270. * Start the board, ready for normal operation
  4271. */
  4272. int
  4273. ahc_init(struct ahc_softc *ahc)
  4274. {
  4275. int max_targ;
  4276. u_int i;
  4277. u_int scsi_conf;
  4278. u_int ultraenb;
  4279. u_int discenable;
  4280. u_int tagenable;
  4281. size_t driver_data_size;
  4282. #ifdef AHC_DEBUG
  4283. if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
  4284. ahc->flags |= AHC_SEQUENCER_DEBUG;
  4285. #endif
  4286. #ifdef AHC_PRINT_SRAM
  4287. printf("Scratch Ram:");
  4288. for (i = 0x20; i < 0x5f; i++) {
  4289. if (((i % 8) == 0) && (i != 0)) {
  4290. printf ("\n ");
  4291. }
  4292. printf (" 0x%x", ahc_inb(ahc, i));
  4293. }
  4294. if ((ahc->features & AHC_MORE_SRAM) != 0) {
  4295. for (i = 0x70; i < 0x7f; i++) {
  4296. if (((i % 8) == 0) && (i != 0)) {
  4297. printf ("\n ");
  4298. }
  4299. printf (" 0x%x", ahc_inb(ahc, i));
  4300. }
  4301. }
  4302. printf ("\n");
  4303. /*
  4304. * Reading uninitialized scratch ram may
  4305. * generate parity errors.
  4306. */
  4307. ahc_outb(ahc, CLRINT, CLRPARERR);
  4308. ahc_outb(ahc, CLRINT, CLRBRKADRINT);
  4309. #endif
  4310. max_targ = 15;
  4311. /*
  4312. * Assume we have a board at this stage and it has been reset.
  4313. */
  4314. if ((ahc->flags & AHC_USEDEFAULTS) != 0)
  4315. ahc->our_id = ahc->our_id_b = 7;
  4316. /*
  4317. * Default to allowing initiator operations.
  4318. */
  4319. ahc->flags |= AHC_INITIATORROLE;
  4320. /*
  4321. * Only allow target mode features if this unit has them enabled.
  4322. */
  4323. if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
  4324. ahc->features &= ~AHC_TARGETMODE;
  4325. #ifndef __linux__
  4326. /* DMA tag for mapping buffers into device visible space. */
  4327. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4328. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4329. /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
  4330. ? (dma_addr_t)0x7FFFFFFFFFULL
  4331. : BUS_SPACE_MAXADDR_32BIT,
  4332. /*highaddr*/BUS_SPACE_MAXADDR,
  4333. /*filter*/NULL, /*filterarg*/NULL,
  4334. /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
  4335. /*nsegments*/AHC_NSEG,
  4336. /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
  4337. /*flags*/BUS_DMA_ALLOCNOW,
  4338. &ahc->buffer_dmat) != 0) {
  4339. return (ENOMEM);
  4340. }
  4341. #endif
  4342. ahc->init_level++;
  4343. /*
  4344. * DMA tag for our command fifos and other data in system memory
  4345. * the card's sequencer must be able to access. For initiator
  4346. * roles, we need to allocate space for the qinfifo and qoutfifo.
  4347. * The qinfifo and qoutfifo are composed of 256 1 byte elements.
  4348. * When providing for the target mode role, we must additionally
  4349. * provide space for the incoming target command fifo and an extra
  4350. * byte to deal with a dma bug in some chip versions.
  4351. */
  4352. driver_data_size = 2 * 256 * sizeof(uint8_t);
  4353. if ((ahc->features & AHC_TARGETMODE) != 0)
  4354. driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
  4355. + /*DMA WideOdd Bug Buffer*/1;
  4356. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4357. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4358. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4359. /*highaddr*/BUS_SPACE_MAXADDR,
  4360. /*filter*/NULL, /*filterarg*/NULL,
  4361. driver_data_size,
  4362. /*nsegments*/1,
  4363. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4364. /*flags*/0, &ahc->shared_data_dmat) != 0) {
  4365. return (ENOMEM);
  4366. }
  4367. ahc->init_level++;
  4368. /* Allocation of driver data */
  4369. if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
  4370. (void **)&ahc->qoutfifo,
  4371. BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
  4372. return (ENOMEM);
  4373. }
  4374. ahc->init_level++;
  4375. /* And permanently map it in */
  4376. ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  4377. ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
  4378. &ahc->shared_data_busaddr, /*flags*/0);
  4379. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4380. ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
  4381. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
  4382. ahc->dma_bug_buf = ahc->shared_data_busaddr
  4383. + driver_data_size - 1;
  4384. /* All target command blocks start out invalid. */
  4385. for (i = 0; i < AHC_TMODE_CMDS; i++)
  4386. ahc->targetcmds[i].cmd_valid = 0;
  4387. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
  4388. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
  4389. }
  4390. ahc->qinfifo = &ahc->qoutfifo[256];
  4391. ahc->init_level++;
  4392. /* Allocate SCB data now that buffer_dmat is initialized */
  4393. if (ahc->scb_data->maxhscbs == 0)
  4394. if (ahc_init_scbdata(ahc) != 0)
  4395. return (ENOMEM);
  4396. /*
  4397. * Allocate a tstate to house information for our
  4398. * initiator presence on the bus as well as the user
  4399. * data for any target mode initiator.
  4400. */
  4401. if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
  4402. printf("%s: unable to allocate ahc_tmode_tstate. "
  4403. "Failing attach\n", ahc_name(ahc));
  4404. return (ENOMEM);
  4405. }
  4406. if ((ahc->features & AHC_TWIN) != 0) {
  4407. if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
  4408. printf("%s: unable to allocate ahc_tmode_tstate. "
  4409. "Failing attach\n", ahc_name(ahc));
  4410. return (ENOMEM);
  4411. }
  4412. }
  4413. if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
  4414. ahc->flags |= AHC_PAGESCBS;
  4415. } else {
  4416. ahc->flags &= ~AHC_PAGESCBS;
  4417. }
  4418. #ifdef AHC_DEBUG
  4419. if (ahc_debug & AHC_SHOW_MISC) {
  4420. printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
  4421. "ahc_dma %u bytes\n",
  4422. ahc_name(ahc),
  4423. (u_int)sizeof(struct hardware_scb),
  4424. (u_int)sizeof(struct scb),
  4425. (u_int)sizeof(struct ahc_dma_seg));
  4426. }
  4427. #endif /* AHC_DEBUG */
  4428. /*
  4429. * Look at the information that board initialization or
  4430. * the board bios has left us.
  4431. */
  4432. if (ahc->features & AHC_TWIN) {
  4433. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4434. if ((scsi_conf & RESET_SCSI) != 0
  4435. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4436. ahc->flags |= AHC_RESET_BUS_B;
  4437. }
  4438. scsi_conf = ahc_inb(ahc, SCSICONF);
  4439. if ((scsi_conf & RESET_SCSI) != 0
  4440. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4441. ahc->flags |= AHC_RESET_BUS_A;
  4442. ultraenb = 0;
  4443. tagenable = ALL_TARGETS_MASK;
  4444. /* Grab the disconnection disable table and invert it for our needs */
  4445. if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
  4446. printf("%s: Host Adapter Bios disabled. Using default SCSI "
  4447. "device parameters\n", ahc_name(ahc));
  4448. ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
  4449. AHC_TERM_ENB_A|AHC_TERM_ENB_B;
  4450. discenable = ALL_TARGETS_MASK;
  4451. if ((ahc->features & AHC_ULTRA) != 0)
  4452. ultraenb = ALL_TARGETS_MASK;
  4453. } else {
  4454. discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
  4455. | ahc_inb(ahc, DISC_DSB));
  4456. if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
  4457. ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
  4458. | ahc_inb(ahc, ULTRA_ENB);
  4459. }
  4460. if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
  4461. max_targ = 7;
  4462. for (i = 0; i <= max_targ; i++) {
  4463. struct ahc_initiator_tinfo *tinfo;
  4464. struct ahc_tmode_tstate *tstate;
  4465. u_int our_id;
  4466. u_int target_id;
  4467. char channel;
  4468. channel = 'A';
  4469. our_id = ahc->our_id;
  4470. target_id = i;
  4471. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  4472. channel = 'B';
  4473. our_id = ahc->our_id_b;
  4474. target_id = i % 8;
  4475. }
  4476. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  4477. target_id, &tstate);
  4478. /* Default to async narrow across the board */
  4479. memset(tinfo, 0, sizeof(*tinfo));
  4480. if (ahc->flags & AHC_USEDEFAULTS) {
  4481. if ((ahc->features & AHC_WIDE) != 0)
  4482. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4483. /*
  4484. * These will be truncated when we determine the
  4485. * connection type we have with the target.
  4486. */
  4487. tinfo->user.period = ahc_syncrates->period;
  4488. tinfo->user.offset = MAX_OFFSET;
  4489. } else {
  4490. u_int scsirate;
  4491. uint16_t mask;
  4492. /* Take the settings leftover in scratch RAM. */
  4493. scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
  4494. mask = (0x01 << i);
  4495. if ((ahc->features & AHC_ULTRA2) != 0) {
  4496. u_int offset;
  4497. u_int maxsync;
  4498. if ((scsirate & SOFS) == 0x0F) {
  4499. /*
  4500. * Haven't negotiated yet,
  4501. * so the format is different.
  4502. */
  4503. scsirate = (scsirate & SXFR) >> 4
  4504. | (ultraenb & mask)
  4505. ? 0x08 : 0x0
  4506. | (scsirate & WIDEXFER);
  4507. offset = MAX_OFFSET_ULTRA2;
  4508. } else
  4509. offset = ahc_inb(ahc, TARG_OFFSET + i);
  4510. if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
  4511. /* Set to the lowest sync rate, 5MHz */
  4512. scsirate |= 0x1c;
  4513. maxsync = AHC_SYNCRATE_ULTRA2;
  4514. if ((ahc->features & AHC_DT) != 0)
  4515. maxsync = AHC_SYNCRATE_DT;
  4516. tinfo->user.period =
  4517. ahc_find_period(ahc, scsirate, maxsync);
  4518. if (offset == 0)
  4519. tinfo->user.period = 0;
  4520. else
  4521. tinfo->user.offset = MAX_OFFSET;
  4522. if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
  4523. && (ahc->features & AHC_DT) != 0)
  4524. tinfo->user.ppr_options =
  4525. MSG_EXT_PPR_DT_REQ;
  4526. } else if ((scsirate & SOFS) != 0) {
  4527. if ((scsirate & SXFR) == 0x40
  4528. && (ultraenb & mask) != 0) {
  4529. /* Treat 10MHz as a non-ultra speed */
  4530. scsirate &= ~SXFR;
  4531. ultraenb &= ~mask;
  4532. }
  4533. tinfo->user.period =
  4534. ahc_find_period(ahc, scsirate,
  4535. (ultraenb & mask)
  4536. ? AHC_SYNCRATE_ULTRA
  4537. : AHC_SYNCRATE_FAST);
  4538. if (tinfo->user.period != 0)
  4539. tinfo->user.offset = MAX_OFFSET;
  4540. }
  4541. if (tinfo->user.period == 0)
  4542. tinfo->user.offset = 0;
  4543. if ((scsirate & WIDEXFER) != 0
  4544. && (ahc->features & AHC_WIDE) != 0)
  4545. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4546. tinfo->user.protocol_version = 4;
  4547. if ((ahc->features & AHC_DT) != 0)
  4548. tinfo->user.transport_version = 3;
  4549. else
  4550. tinfo->user.transport_version = 2;
  4551. tinfo->goal.protocol_version = 2;
  4552. tinfo->goal.transport_version = 2;
  4553. tinfo->curr.protocol_version = 2;
  4554. tinfo->curr.transport_version = 2;
  4555. }
  4556. tstate->ultraenb = 0;
  4557. }
  4558. ahc->user_discenable = discenable;
  4559. ahc->user_tagenable = tagenable;
  4560. return (ahc->bus_chip_init(ahc));
  4561. }
  4562. void
  4563. ahc_intr_enable(struct ahc_softc *ahc, int enable)
  4564. {
  4565. u_int hcntrl;
  4566. hcntrl = ahc_inb(ahc, HCNTRL);
  4567. hcntrl &= ~INTEN;
  4568. ahc->pause &= ~INTEN;
  4569. ahc->unpause &= ~INTEN;
  4570. if (enable) {
  4571. hcntrl |= INTEN;
  4572. ahc->pause |= INTEN;
  4573. ahc->unpause |= INTEN;
  4574. }
  4575. ahc_outb(ahc, HCNTRL, hcntrl);
  4576. }
  4577. /*
  4578. * Ensure that the card is paused in a location
  4579. * outside of all critical sections and that all
  4580. * pending work is completed prior to returning.
  4581. * This routine should only be called from outside
  4582. * an interrupt context.
  4583. */
  4584. void
  4585. ahc_pause_and_flushwork(struct ahc_softc *ahc)
  4586. {
  4587. int intstat;
  4588. int maxloops;
  4589. int paused;
  4590. maxloops = 1000;
  4591. ahc->flags |= AHC_ALL_INTERRUPTS;
  4592. paused = FALSE;
  4593. do {
  4594. if (paused) {
  4595. ahc_unpause(ahc);
  4596. /*
  4597. * Give the sequencer some time to service
  4598. * any active selections.
  4599. */
  4600. ahc_delay(500);
  4601. }
  4602. ahc_intr(ahc);
  4603. ahc_pause(ahc);
  4604. paused = TRUE;
  4605. ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  4606. intstat = ahc_inb(ahc, INTSTAT);
  4607. if ((intstat & INT_PEND) == 0) {
  4608. ahc_clear_critical_section(ahc);
  4609. intstat = ahc_inb(ahc, INTSTAT);
  4610. }
  4611. } while (--maxloops
  4612. && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
  4613. && ((intstat & INT_PEND) != 0
  4614. || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
  4615. if (maxloops == 0) {
  4616. printf("Infinite interrupt loop, INTSTAT = %x",
  4617. ahc_inb(ahc, INTSTAT));
  4618. }
  4619. ahc_platform_flushwork(ahc);
  4620. ahc->flags &= ~AHC_ALL_INTERRUPTS;
  4621. }
  4622. int
  4623. ahc_suspend(struct ahc_softc *ahc)
  4624. {
  4625. ahc_pause_and_flushwork(ahc);
  4626. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  4627. ahc_unpause(ahc);
  4628. return (EBUSY);
  4629. }
  4630. #ifdef AHC_TARGET_MODE
  4631. /*
  4632. * XXX What about ATIOs that have not yet been serviced?
  4633. * Perhaps we should just refuse to be suspended if we
  4634. * are acting in a target role.
  4635. */
  4636. if (ahc->pending_device != NULL) {
  4637. ahc_unpause(ahc);
  4638. return (EBUSY);
  4639. }
  4640. #endif
  4641. ahc_shutdown(ahc);
  4642. return (0);
  4643. }
  4644. int
  4645. ahc_resume(struct ahc_softc *ahc)
  4646. {
  4647. ahc_reset(ahc, /*reinit*/TRUE);
  4648. ahc_intr_enable(ahc, TRUE);
  4649. ahc_restart(ahc);
  4650. return (0);
  4651. }
  4652. /************************** Busy Target Table *********************************/
  4653. /*
  4654. * Return the untagged transaction id for a given target/channel lun.
  4655. * Optionally, clear the entry.
  4656. */
  4657. u_int
  4658. ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
  4659. {
  4660. u_int scbid;
  4661. u_int target_offset;
  4662. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4663. u_int saved_scbptr;
  4664. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4665. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4666. scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
  4667. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4668. } else {
  4669. target_offset = TCL_TARGET_OFFSET(tcl);
  4670. scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
  4671. }
  4672. return (scbid);
  4673. }
  4674. void
  4675. ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
  4676. {
  4677. u_int target_offset;
  4678. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4679. u_int saved_scbptr;
  4680. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4681. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4682. ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
  4683. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4684. } else {
  4685. target_offset = TCL_TARGET_OFFSET(tcl);
  4686. ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
  4687. }
  4688. }
  4689. void
  4690. ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
  4691. {
  4692. u_int target_offset;
  4693. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4694. u_int saved_scbptr;
  4695. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4696. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4697. ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
  4698. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4699. } else {
  4700. target_offset = TCL_TARGET_OFFSET(tcl);
  4701. ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
  4702. }
  4703. }
  4704. /************************** SCB and SCB queue management **********************/
  4705. int
  4706. ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
  4707. char channel, int lun, u_int tag, role_t role)
  4708. {
  4709. int targ = SCB_GET_TARGET(ahc, scb);
  4710. char chan = SCB_GET_CHANNEL(ahc, scb);
  4711. int slun = SCB_GET_LUN(scb);
  4712. int match;
  4713. match = ((chan == channel) || (channel == ALL_CHANNELS));
  4714. if (match != 0)
  4715. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  4716. if (match != 0)
  4717. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  4718. if (match != 0) {
  4719. #ifdef AHC_TARGET_MODE
  4720. int group;
  4721. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  4722. if (role == ROLE_INITIATOR) {
  4723. match = (group != XPT_FC_GROUP_TMODE)
  4724. && ((tag == scb->hscb->tag)
  4725. || (tag == SCB_LIST_NULL));
  4726. } else if (role == ROLE_TARGET) {
  4727. match = (group == XPT_FC_GROUP_TMODE)
  4728. && ((tag == scb->io_ctx->csio.tag_id)
  4729. || (tag == SCB_LIST_NULL));
  4730. }
  4731. #else /* !AHC_TARGET_MODE */
  4732. match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
  4733. #endif /* AHC_TARGET_MODE */
  4734. }
  4735. return match;
  4736. }
  4737. void
  4738. ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  4739. {
  4740. int target;
  4741. char channel;
  4742. int lun;
  4743. target = SCB_GET_TARGET(ahc, scb);
  4744. lun = SCB_GET_LUN(scb);
  4745. channel = SCB_GET_CHANNEL(ahc, scb);
  4746. ahc_search_qinfifo(ahc, target, channel, lun,
  4747. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  4748. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  4749. ahc_platform_freeze_devq(ahc, scb);
  4750. }
  4751. void
  4752. ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
  4753. {
  4754. struct scb *prev_scb;
  4755. prev_scb = NULL;
  4756. if (ahc_qinfifo_count(ahc) != 0) {
  4757. u_int prev_tag;
  4758. uint8_t prev_pos;
  4759. prev_pos = ahc->qinfifonext - 1;
  4760. prev_tag = ahc->qinfifo[prev_pos];
  4761. prev_scb = ahc_lookup_scb(ahc, prev_tag);
  4762. }
  4763. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4764. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4765. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4766. } else {
  4767. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4768. }
  4769. }
  4770. static void
  4771. ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
  4772. struct scb *scb)
  4773. {
  4774. if (prev_scb == NULL) {
  4775. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4776. } else {
  4777. prev_scb->hscb->next = scb->hscb->tag;
  4778. ahc_sync_scb(ahc, prev_scb,
  4779. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4780. }
  4781. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  4782. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4783. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4784. }
  4785. static int
  4786. ahc_qinfifo_count(struct ahc_softc *ahc)
  4787. {
  4788. uint8_t qinpos;
  4789. uint8_t diff;
  4790. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4791. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  4792. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  4793. } else
  4794. qinpos = ahc_inb(ahc, QINPOS);
  4795. diff = ahc->qinfifonext - qinpos;
  4796. return (diff);
  4797. }
  4798. int
  4799. ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
  4800. int lun, u_int tag, role_t role, uint32_t status,
  4801. ahc_search_action action)
  4802. {
  4803. struct scb *scb;
  4804. struct scb *prev_scb;
  4805. uint8_t qinstart;
  4806. uint8_t qinpos;
  4807. uint8_t qintail;
  4808. uint8_t next;
  4809. uint8_t prev;
  4810. uint8_t curscbptr;
  4811. int found;
  4812. int have_qregs;
  4813. qintail = ahc->qinfifonext;
  4814. have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
  4815. if (have_qregs) {
  4816. qinstart = ahc_inb(ahc, SNSCB_QOFF);
  4817. ahc_outb(ahc, SNSCB_QOFF, qinstart);
  4818. } else
  4819. qinstart = ahc_inb(ahc, QINPOS);
  4820. qinpos = qinstart;
  4821. found = 0;
  4822. prev_scb = NULL;
  4823. if (action == SEARCH_COMPLETE) {
  4824. /*
  4825. * Don't attempt to run any queued untagged transactions
  4826. * until we are done with the abort process.
  4827. */
  4828. ahc_freeze_untagged_queues(ahc);
  4829. }
  4830. /*
  4831. * Start with an empty queue. Entries that are not chosen
  4832. * for removal will be re-added to the queue as we go.
  4833. */
  4834. ahc->qinfifonext = qinpos;
  4835. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4836. while (qinpos != qintail) {
  4837. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
  4838. if (scb == NULL) {
  4839. printf("qinpos = %d, SCB index = %d\n",
  4840. qinpos, ahc->qinfifo[qinpos]);
  4841. panic("Loop 1\n");
  4842. }
  4843. if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
  4844. /*
  4845. * We found an scb that needs to be acted on.
  4846. */
  4847. found++;
  4848. switch (action) {
  4849. case SEARCH_COMPLETE:
  4850. {
  4851. cam_status ostat;
  4852. cam_status cstat;
  4853. ostat = ahc_get_transaction_status(scb);
  4854. if (ostat == CAM_REQ_INPROG)
  4855. ahc_set_transaction_status(scb, status);
  4856. cstat = ahc_get_transaction_status(scb);
  4857. if (cstat != CAM_REQ_CMP)
  4858. ahc_freeze_scb(scb);
  4859. if ((scb->flags & SCB_ACTIVE) == 0)
  4860. printf("Inactive SCB in qinfifo\n");
  4861. ahc_done(ahc, scb);
  4862. /* FALLTHROUGH */
  4863. }
  4864. case SEARCH_REMOVE:
  4865. break;
  4866. case SEARCH_COUNT:
  4867. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4868. prev_scb = scb;
  4869. break;
  4870. }
  4871. } else {
  4872. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4873. prev_scb = scb;
  4874. }
  4875. qinpos++;
  4876. }
  4877. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4878. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4879. } else {
  4880. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4881. }
  4882. if (action != SEARCH_COUNT
  4883. && (found != 0)
  4884. && (qinstart != ahc->qinfifonext)) {
  4885. /*
  4886. * The sequencer may be in the process of dmaing
  4887. * down the SCB at the beginning of the queue.
  4888. * This could be problematic if either the first,
  4889. * or the second SCB is removed from the queue
  4890. * (the first SCB includes a pointer to the "next"
  4891. * SCB to dma). If we have removed any entries, swap
  4892. * the first element in the queue with the next HSCB
  4893. * so the sequencer will notice that NEXT_QUEUED_SCB
  4894. * has changed during its dma attempt and will retry
  4895. * the DMA.
  4896. */
  4897. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
  4898. if (scb == NULL) {
  4899. printf("found = %d, qinstart = %d, qinfifionext = %d\n",
  4900. found, qinstart, ahc->qinfifonext);
  4901. panic("First/Second Qinfifo fixup\n");
  4902. }
  4903. /*
  4904. * ahc_swap_with_next_hscb forces our next pointer to
  4905. * point to the reserved SCB for future commands. Save
  4906. * and restore our original next pointer to maintain
  4907. * queue integrity.
  4908. */
  4909. next = scb->hscb->next;
  4910. ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
  4911. ahc_swap_with_next_hscb(ahc, scb);
  4912. scb->hscb->next = next;
  4913. ahc->qinfifo[qinstart] = scb->hscb->tag;
  4914. /* Tell the card about the new head of the qinfifo. */
  4915. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4916. /* Fixup the tail "next" pointer. */
  4917. qintail = ahc->qinfifonext - 1;
  4918. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
  4919. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4920. }
  4921. /*
  4922. * Search waiting for selection list.
  4923. */
  4924. curscbptr = ahc_inb(ahc, SCBPTR);
  4925. next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
  4926. prev = SCB_LIST_NULL;
  4927. while (next != SCB_LIST_NULL) {
  4928. uint8_t scb_index;
  4929. ahc_outb(ahc, SCBPTR, next);
  4930. scb_index = ahc_inb(ahc, SCB_TAG);
  4931. if (scb_index >= ahc->scb_data->numscbs) {
  4932. printf("Waiting List inconsistency. "
  4933. "SCB index == %d, yet numscbs == %d.",
  4934. scb_index, ahc->scb_data->numscbs);
  4935. ahc_dump_card_state(ahc);
  4936. panic("for safety");
  4937. }
  4938. scb = ahc_lookup_scb(ahc, scb_index);
  4939. if (scb == NULL) {
  4940. printf("scb_index = %d, next = %d\n",
  4941. scb_index, next);
  4942. panic("Waiting List traversal\n");
  4943. }
  4944. if (ahc_match_scb(ahc, scb, target, channel,
  4945. lun, SCB_LIST_NULL, role)) {
  4946. /*
  4947. * We found an scb that needs to be acted on.
  4948. */
  4949. found++;
  4950. switch (action) {
  4951. case SEARCH_COMPLETE:
  4952. {
  4953. cam_status ostat;
  4954. cam_status cstat;
  4955. ostat = ahc_get_transaction_status(scb);
  4956. if (ostat == CAM_REQ_INPROG)
  4957. ahc_set_transaction_status(scb,
  4958. status);
  4959. cstat = ahc_get_transaction_status(scb);
  4960. if (cstat != CAM_REQ_CMP)
  4961. ahc_freeze_scb(scb);
  4962. if ((scb->flags & SCB_ACTIVE) == 0)
  4963. printf("Inactive SCB in Waiting List\n");
  4964. ahc_done(ahc, scb);
  4965. /* FALLTHROUGH */
  4966. }
  4967. case SEARCH_REMOVE:
  4968. next = ahc_rem_wscb(ahc, next, prev);
  4969. break;
  4970. case SEARCH_COUNT:
  4971. prev = next;
  4972. next = ahc_inb(ahc, SCB_NEXT);
  4973. break;
  4974. }
  4975. } else {
  4976. prev = next;
  4977. next = ahc_inb(ahc, SCB_NEXT);
  4978. }
  4979. }
  4980. ahc_outb(ahc, SCBPTR, curscbptr);
  4981. found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
  4982. channel, lun, status, action);
  4983. if (action == SEARCH_COMPLETE)
  4984. ahc_release_untagged_queues(ahc);
  4985. return (found);
  4986. }
  4987. int
  4988. ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
  4989. int target, char channel, int lun, uint32_t status,
  4990. ahc_search_action action)
  4991. {
  4992. struct scb *scb;
  4993. int maxtarget;
  4994. int found;
  4995. int i;
  4996. if (action == SEARCH_COMPLETE) {
  4997. /*
  4998. * Don't attempt to run any queued untagged transactions
  4999. * until we are done with the abort process.
  5000. */
  5001. ahc_freeze_untagged_queues(ahc);
  5002. }
  5003. found = 0;
  5004. i = 0;
  5005. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  5006. maxtarget = 16;
  5007. if (target != CAM_TARGET_WILDCARD) {
  5008. i = target;
  5009. if (channel == 'B')
  5010. i += 8;
  5011. maxtarget = i + 1;
  5012. }
  5013. } else {
  5014. maxtarget = 0;
  5015. }
  5016. for (; i < maxtarget; i++) {
  5017. struct scb_tailq *untagged_q;
  5018. struct scb *next_scb;
  5019. untagged_q = &(ahc->untagged_queues[i]);
  5020. next_scb = TAILQ_FIRST(untagged_q);
  5021. while (next_scb != NULL) {
  5022. scb = next_scb;
  5023. next_scb = TAILQ_NEXT(scb, links.tqe);
  5024. /*
  5025. * The head of the list may be the currently
  5026. * active untagged command for a device.
  5027. * We're only searching for commands that
  5028. * have not been started. A transaction
  5029. * marked active but still in the qinfifo
  5030. * is removed by the qinfifo scanning code
  5031. * above.
  5032. */
  5033. if ((scb->flags & SCB_ACTIVE) != 0)
  5034. continue;
  5035. if (ahc_match_scb(ahc, scb, target, channel, lun,
  5036. SCB_LIST_NULL, ROLE_INITIATOR) == 0
  5037. || (ctx != NULL && ctx != scb->io_ctx))
  5038. continue;
  5039. /*
  5040. * We found an scb that needs to be acted on.
  5041. */
  5042. found++;
  5043. switch (action) {
  5044. case SEARCH_COMPLETE:
  5045. {
  5046. cam_status ostat;
  5047. cam_status cstat;
  5048. ostat = ahc_get_transaction_status(scb);
  5049. if (ostat == CAM_REQ_INPROG)
  5050. ahc_set_transaction_status(scb, status);
  5051. cstat = ahc_get_transaction_status(scb);
  5052. if (cstat != CAM_REQ_CMP)
  5053. ahc_freeze_scb(scb);
  5054. if ((scb->flags & SCB_ACTIVE) == 0)
  5055. printf("Inactive SCB in untaggedQ\n");
  5056. ahc_done(ahc, scb);
  5057. break;
  5058. }
  5059. case SEARCH_REMOVE:
  5060. scb->flags &= ~SCB_UNTAGGEDQ;
  5061. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  5062. break;
  5063. case SEARCH_COUNT:
  5064. break;
  5065. }
  5066. }
  5067. }
  5068. if (action == SEARCH_COMPLETE)
  5069. ahc_release_untagged_queues(ahc);
  5070. return (found);
  5071. }
  5072. int
  5073. ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
  5074. int lun, u_int tag, int stop_on_first, int remove,
  5075. int save_state)
  5076. {
  5077. struct scb *scbp;
  5078. u_int next;
  5079. u_int prev;
  5080. u_int count;
  5081. u_int active_scb;
  5082. count = 0;
  5083. next = ahc_inb(ahc, DISCONNECTED_SCBH);
  5084. prev = SCB_LIST_NULL;
  5085. if (save_state) {
  5086. /* restore this when we're done */
  5087. active_scb = ahc_inb(ahc, SCBPTR);
  5088. } else
  5089. /* Silence compiler */
  5090. active_scb = SCB_LIST_NULL;
  5091. while (next != SCB_LIST_NULL) {
  5092. u_int scb_index;
  5093. ahc_outb(ahc, SCBPTR, next);
  5094. scb_index = ahc_inb(ahc, SCB_TAG);
  5095. if (scb_index >= ahc->scb_data->numscbs) {
  5096. printf("Disconnected List inconsistency. "
  5097. "SCB index == %d, yet numscbs == %d.",
  5098. scb_index, ahc->scb_data->numscbs);
  5099. ahc_dump_card_state(ahc);
  5100. panic("for safety");
  5101. }
  5102. if (next == prev) {
  5103. panic("Disconnected List Loop. "
  5104. "cur SCBPTR == %x, prev SCBPTR == %x.",
  5105. next, prev);
  5106. }
  5107. scbp = ahc_lookup_scb(ahc, scb_index);
  5108. if (ahc_match_scb(ahc, scbp, target, channel, lun,
  5109. tag, ROLE_INITIATOR)) {
  5110. count++;
  5111. if (remove) {
  5112. next =
  5113. ahc_rem_scb_from_disc_list(ahc, prev, next);
  5114. } else {
  5115. prev = next;
  5116. next = ahc_inb(ahc, SCB_NEXT);
  5117. }
  5118. if (stop_on_first)
  5119. break;
  5120. } else {
  5121. prev = next;
  5122. next = ahc_inb(ahc, SCB_NEXT);
  5123. }
  5124. }
  5125. if (save_state)
  5126. ahc_outb(ahc, SCBPTR, active_scb);
  5127. return (count);
  5128. }
  5129. /*
  5130. * Remove an SCB from the on chip list of disconnected transactions.
  5131. * This is empty/unused if we are not performing SCB paging.
  5132. */
  5133. static u_int
  5134. ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
  5135. {
  5136. u_int next;
  5137. ahc_outb(ahc, SCBPTR, scbptr);
  5138. next = ahc_inb(ahc, SCB_NEXT);
  5139. ahc_outb(ahc, SCB_CONTROL, 0);
  5140. ahc_add_curscb_to_free_list(ahc);
  5141. if (prev != SCB_LIST_NULL) {
  5142. ahc_outb(ahc, SCBPTR, prev);
  5143. ahc_outb(ahc, SCB_NEXT, next);
  5144. } else
  5145. ahc_outb(ahc, DISCONNECTED_SCBH, next);
  5146. return (next);
  5147. }
  5148. /*
  5149. * Add the SCB as selected by SCBPTR onto the on chip list of
  5150. * free hardware SCBs. This list is empty/unused if we are not
  5151. * performing SCB paging.
  5152. */
  5153. static void
  5154. ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
  5155. {
  5156. /*
  5157. * Invalidate the tag so that our abort
  5158. * routines don't think it's active.
  5159. */
  5160. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  5161. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  5162. ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
  5163. ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
  5164. }
  5165. }
  5166. /*
  5167. * Manipulate the waiting for selection list and return the
  5168. * scb that follows the one that we remove.
  5169. */
  5170. static u_int
  5171. ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
  5172. {
  5173. u_int curscb, next;
  5174. /*
  5175. * Select the SCB we want to abort and
  5176. * pull the next pointer out of it.
  5177. */
  5178. curscb = ahc_inb(ahc, SCBPTR);
  5179. ahc_outb(ahc, SCBPTR, scbpos);
  5180. next = ahc_inb(ahc, SCB_NEXT);
  5181. /* Clear the necessary fields */
  5182. ahc_outb(ahc, SCB_CONTROL, 0);
  5183. ahc_add_curscb_to_free_list(ahc);
  5184. /* update the waiting list */
  5185. if (prev == SCB_LIST_NULL) {
  5186. /* First in the list */
  5187. ahc_outb(ahc, WAITING_SCBH, next);
  5188. /*
  5189. * Ensure we aren't attempting to perform
  5190. * selection for this entry.
  5191. */
  5192. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  5193. } else {
  5194. /*
  5195. * Select the scb that pointed to us
  5196. * and update its next pointer.
  5197. */
  5198. ahc_outb(ahc, SCBPTR, prev);
  5199. ahc_outb(ahc, SCB_NEXT, next);
  5200. }
  5201. /*
  5202. * Point us back at the original scb position.
  5203. */
  5204. ahc_outb(ahc, SCBPTR, curscb);
  5205. return next;
  5206. }
  5207. /******************************** Error Handling ******************************/
  5208. /*
  5209. * Abort all SCBs that match the given description (target/channel/lun/tag),
  5210. * setting their status to the passed in status if the status has not already
  5211. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  5212. * is paused before it is called.
  5213. */
  5214. int
  5215. ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  5216. int lun, u_int tag, role_t role, uint32_t status)
  5217. {
  5218. struct scb *scbp;
  5219. struct scb *scbp_next;
  5220. u_int active_scb;
  5221. int i, j;
  5222. int maxtarget;
  5223. int minlun;
  5224. int maxlun;
  5225. int found;
  5226. /*
  5227. * Don't attempt to run any queued untagged transactions
  5228. * until we are done with the abort process.
  5229. */
  5230. ahc_freeze_untagged_queues(ahc);
  5231. /* restore this when we're done */
  5232. active_scb = ahc_inb(ahc, SCBPTR);
  5233. found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
  5234. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5235. /*
  5236. * Clean out the busy target table for any untagged commands.
  5237. */
  5238. i = 0;
  5239. maxtarget = 16;
  5240. if (target != CAM_TARGET_WILDCARD) {
  5241. i = target;
  5242. if (channel == 'B')
  5243. i += 8;
  5244. maxtarget = i + 1;
  5245. }
  5246. if (lun == CAM_LUN_WILDCARD) {
  5247. /*
  5248. * Unless we are using an SCB based
  5249. * busy targets table, there is only
  5250. * one table entry for all luns of
  5251. * a target.
  5252. */
  5253. minlun = 0;
  5254. maxlun = 1;
  5255. if ((ahc->flags & AHC_SCB_BTT) != 0)
  5256. maxlun = AHC_NUM_LUNS;
  5257. } else {
  5258. minlun = lun;
  5259. maxlun = lun + 1;
  5260. }
  5261. if (role != ROLE_TARGET) {
  5262. for (;i < maxtarget; i++) {
  5263. for (j = minlun;j < maxlun; j++) {
  5264. u_int scbid;
  5265. u_int tcl;
  5266. tcl = BUILD_TCL(i << 4, j);
  5267. scbid = ahc_index_busy_tcl(ahc, tcl);
  5268. scbp = ahc_lookup_scb(ahc, scbid);
  5269. if (scbp == NULL
  5270. || ahc_match_scb(ahc, scbp, target, channel,
  5271. lun, tag, role) == 0)
  5272. continue;
  5273. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
  5274. }
  5275. }
  5276. /*
  5277. * Go through the disconnected list and remove any entries we
  5278. * have queued for completion, 0'ing their control byte too.
  5279. * We save the active SCB and restore it ourselves, so there
  5280. * is no reason for this search to restore it too.
  5281. */
  5282. ahc_search_disc_list(ahc, target, channel, lun, tag,
  5283. /*stop_on_first*/FALSE, /*remove*/TRUE,
  5284. /*save_state*/FALSE);
  5285. }
  5286. /*
  5287. * Go through the hardware SCB array looking for commands that
  5288. * were active but not on any list. In some cases, these remnants
  5289. * might not still have mappings in the scbindex array (e.g. unexpected
  5290. * bus free with the same scb queued for an abort). Don't hold this
  5291. * against them.
  5292. */
  5293. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  5294. u_int scbid;
  5295. ahc_outb(ahc, SCBPTR, i);
  5296. scbid = ahc_inb(ahc, SCB_TAG);
  5297. scbp = ahc_lookup_scb(ahc, scbid);
  5298. if ((scbp == NULL && scbid != SCB_LIST_NULL)
  5299. || (scbp != NULL
  5300. && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
  5301. ahc_add_curscb_to_free_list(ahc);
  5302. }
  5303. /*
  5304. * Go through the pending CCB list and look for
  5305. * commands for this target that are still active.
  5306. * These are other tagged commands that were
  5307. * disconnected when the reset occurred.
  5308. */
  5309. scbp_next = LIST_FIRST(&ahc->pending_scbs);
  5310. while (scbp_next != NULL) {
  5311. scbp = scbp_next;
  5312. scbp_next = LIST_NEXT(scbp, pending_links);
  5313. if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
  5314. cam_status ostat;
  5315. ostat = ahc_get_transaction_status(scbp);
  5316. if (ostat == CAM_REQ_INPROG)
  5317. ahc_set_transaction_status(scbp, status);
  5318. if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
  5319. ahc_freeze_scb(scbp);
  5320. if ((scbp->flags & SCB_ACTIVE) == 0)
  5321. printf("Inactive SCB on pending list\n");
  5322. ahc_done(ahc, scbp);
  5323. found++;
  5324. }
  5325. }
  5326. ahc_outb(ahc, SCBPTR, active_scb);
  5327. ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
  5328. ahc_release_untagged_queues(ahc);
  5329. return found;
  5330. }
  5331. static void
  5332. ahc_reset_current_bus(struct ahc_softc *ahc)
  5333. {
  5334. uint8_t scsiseq;
  5335. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
  5336. scsiseq = ahc_inb(ahc, SCSISEQ);
  5337. ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
  5338. ahc_flush_device_writes(ahc);
  5339. ahc_delay(AHC_BUSRESET_DELAY);
  5340. /* Turn off the bus reset */
  5341. ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
  5342. ahc_clear_intstat(ahc);
  5343. /* Re-enable reset interrupts */
  5344. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
  5345. }
  5346. int
  5347. ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
  5348. {
  5349. struct ahc_devinfo devinfo;
  5350. u_int initiator, target, max_scsiid;
  5351. u_int sblkctl;
  5352. u_int scsiseq;
  5353. u_int simode1;
  5354. int found;
  5355. int restart_needed;
  5356. char cur_channel;
  5357. ahc->pending_device = NULL;
  5358. ahc_compile_devinfo(&devinfo,
  5359. CAM_TARGET_WILDCARD,
  5360. CAM_TARGET_WILDCARD,
  5361. CAM_LUN_WILDCARD,
  5362. channel, ROLE_UNKNOWN);
  5363. ahc_pause(ahc);
  5364. /* Make sure the sequencer is in a safe location. */
  5365. ahc_clear_critical_section(ahc);
  5366. /*
  5367. * Run our command complete fifos to ensure that we perform
  5368. * completion processing on any commands that 'completed'
  5369. * before the reset occurred.
  5370. */
  5371. ahc_run_qoutfifo(ahc);
  5372. #ifdef AHC_TARGET_MODE
  5373. /*
  5374. * XXX - In Twin mode, the tqinfifo may have commands
  5375. * for an unaffected channel in it. However, if
  5376. * we have run out of ATIO resources to drain that
  5377. * queue, we may not get them all out here. Further,
  5378. * the blocked transactions for the reset channel
  5379. * should just be killed off, irrespecitve of whether
  5380. * we are blocked on ATIO resources. Write a routine
  5381. * to compact the tqinfifo appropriately.
  5382. */
  5383. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  5384. ahc_run_tqinfifo(ahc, /*paused*/TRUE);
  5385. }
  5386. #endif
  5387. /*
  5388. * Reset the bus if we are initiating this reset
  5389. */
  5390. sblkctl = ahc_inb(ahc, SBLKCTL);
  5391. cur_channel = 'A';
  5392. if ((ahc->features & AHC_TWIN) != 0
  5393. && ((sblkctl & SELBUSB) != 0))
  5394. cur_channel = 'B';
  5395. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  5396. if (cur_channel != channel) {
  5397. /* Case 1: Command for another bus is active
  5398. * Stealthily reset the other bus without
  5399. * upsetting the current bus.
  5400. */
  5401. ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
  5402. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5403. #ifdef AHC_TARGET_MODE
  5404. /*
  5405. * Bus resets clear ENSELI, so we cannot
  5406. * defer re-enabling bus reset interrupts
  5407. * if we are in target mode.
  5408. */
  5409. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5410. simode1 |= ENSCSIRST;
  5411. #endif
  5412. ahc_outb(ahc, SIMODE1, simode1);
  5413. if (initiate_reset)
  5414. ahc_reset_current_bus(ahc);
  5415. ahc_clear_intstat(ahc);
  5416. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5417. ahc_outb(ahc, SBLKCTL, sblkctl);
  5418. restart_needed = FALSE;
  5419. } else {
  5420. /* Case 2: A command from this bus is active or we're idle */
  5421. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5422. #ifdef AHC_TARGET_MODE
  5423. /*
  5424. * Bus resets clear ENSELI, so we cannot
  5425. * defer re-enabling bus reset interrupts
  5426. * if we are in target mode.
  5427. */
  5428. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5429. simode1 |= ENSCSIRST;
  5430. #endif
  5431. ahc_outb(ahc, SIMODE1, simode1);
  5432. if (initiate_reset)
  5433. ahc_reset_current_bus(ahc);
  5434. ahc_clear_intstat(ahc);
  5435. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5436. restart_needed = TRUE;
  5437. }
  5438. /*
  5439. * Clean up all the state information for the
  5440. * pending transactions on this bus.
  5441. */
  5442. found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
  5443. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  5444. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  5445. max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
  5446. #ifdef AHC_TARGET_MODE
  5447. /*
  5448. * Send an immediate notify ccb to all target more peripheral
  5449. * drivers affected by this action.
  5450. */
  5451. for (target = 0; target <= max_scsiid; target++) {
  5452. struct ahc_tmode_tstate* tstate;
  5453. u_int lun;
  5454. tstate = ahc->enabled_targets[target];
  5455. if (tstate == NULL)
  5456. continue;
  5457. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  5458. struct ahc_tmode_lstate* lstate;
  5459. lstate = tstate->enabled_luns[lun];
  5460. if (lstate == NULL)
  5461. continue;
  5462. ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
  5463. EVENT_TYPE_BUS_RESET, /*arg*/0);
  5464. ahc_send_lstate_events(ahc, lstate);
  5465. }
  5466. }
  5467. #endif
  5468. /* Notify the XPT that a bus reset occurred */
  5469. ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
  5470. CAM_LUN_WILDCARD, AC_BUS_RESET);
  5471. /*
  5472. * Revert to async/narrow transfers until we renegotiate.
  5473. */
  5474. for (target = 0; target <= max_scsiid; target++) {
  5475. if (ahc->enabled_targets[target] == NULL)
  5476. continue;
  5477. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  5478. struct ahc_devinfo devinfo;
  5479. ahc_compile_devinfo(&devinfo, target, initiator,
  5480. CAM_LUN_WILDCARD,
  5481. channel, ROLE_UNKNOWN);
  5482. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  5483. AHC_TRANS_CUR, /*paused*/TRUE);
  5484. ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  5485. /*period*/0, /*offset*/0,
  5486. /*ppr_options*/0, AHC_TRANS_CUR,
  5487. /*paused*/TRUE);
  5488. }
  5489. }
  5490. if (restart_needed)
  5491. ahc_restart(ahc);
  5492. else
  5493. ahc_unpause(ahc);
  5494. return found;
  5495. }
  5496. /***************************** Residual Processing ****************************/
  5497. /*
  5498. * Calculate the residual for a just completed SCB.
  5499. */
  5500. void
  5501. ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
  5502. {
  5503. struct hardware_scb *hscb;
  5504. struct status_pkt *spkt;
  5505. uint32_t sgptr;
  5506. uint32_t resid_sgptr;
  5507. uint32_t resid;
  5508. /*
  5509. * 5 cases.
  5510. * 1) No residual.
  5511. * SG_RESID_VALID clear in sgptr.
  5512. * 2) Transferless command
  5513. * 3) Never performed any transfers.
  5514. * sgptr has SG_FULL_RESID set.
  5515. * 4) No residual but target did not
  5516. * save data pointers after the
  5517. * last transfer, so sgptr was
  5518. * never updated.
  5519. * 5) We have a partial residual.
  5520. * Use residual_sgptr to determine
  5521. * where we are.
  5522. */
  5523. hscb = scb->hscb;
  5524. sgptr = ahc_le32toh(hscb->sgptr);
  5525. if ((sgptr & SG_RESID_VALID) == 0)
  5526. /* Case 1 */
  5527. return;
  5528. sgptr &= ~SG_RESID_VALID;
  5529. if ((sgptr & SG_LIST_NULL) != 0)
  5530. /* Case 2 */
  5531. return;
  5532. spkt = &hscb->shared_data.status;
  5533. resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
  5534. if ((sgptr & SG_FULL_RESID) != 0) {
  5535. /* Case 3 */
  5536. resid = ahc_get_transfer_length(scb);
  5537. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  5538. /* Case 4 */
  5539. return;
  5540. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  5541. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  5542. } else {
  5543. struct ahc_dma_seg *sg;
  5544. /*
  5545. * Remainder of the SG where the transfer
  5546. * stopped.
  5547. */
  5548. resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
  5549. sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
  5550. /* The residual sg_ptr always points to the next sg */
  5551. sg--;
  5552. /*
  5553. * Add up the contents of all residual
  5554. * SG segments that are after the SG where
  5555. * the transfer stopped.
  5556. */
  5557. while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
  5558. sg++;
  5559. resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  5560. }
  5561. }
  5562. if ((scb->flags & SCB_SENSE) == 0)
  5563. ahc_set_residual(scb, resid);
  5564. else
  5565. ahc_set_sense_residual(scb, resid);
  5566. #ifdef AHC_DEBUG
  5567. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  5568. ahc_print_path(ahc, scb);
  5569. printf("Handled %sResidual of %d bytes\n",
  5570. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  5571. }
  5572. #endif
  5573. }
  5574. /******************************* Target Mode **********************************/
  5575. #ifdef AHC_TARGET_MODE
  5576. /*
  5577. * Add a target mode event to this lun's queue
  5578. */
  5579. static void
  5580. ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
  5581. u_int initiator_id, u_int event_type, u_int event_arg)
  5582. {
  5583. struct ahc_tmode_event *event;
  5584. int pending;
  5585. xpt_freeze_devq(lstate->path, /*count*/1);
  5586. if (lstate->event_w_idx >= lstate->event_r_idx)
  5587. pending = lstate->event_w_idx - lstate->event_r_idx;
  5588. else
  5589. pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
  5590. - (lstate->event_r_idx - lstate->event_w_idx);
  5591. if (event_type == EVENT_TYPE_BUS_RESET
  5592. || event_type == MSG_BUS_DEV_RESET) {
  5593. /*
  5594. * Any earlier events are irrelevant, so reset our buffer.
  5595. * This has the effect of allowing us to deal with reset
  5596. * floods (an external device holding down the reset line)
  5597. * without losing the event that is really interesting.
  5598. */
  5599. lstate->event_r_idx = 0;
  5600. lstate->event_w_idx = 0;
  5601. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  5602. }
  5603. if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
  5604. xpt_print_path(lstate->path);
  5605. printf("immediate event %x:%x lost\n",
  5606. lstate->event_buffer[lstate->event_r_idx].event_type,
  5607. lstate->event_buffer[lstate->event_r_idx].event_arg);
  5608. lstate->event_r_idx++;
  5609. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5610. lstate->event_r_idx = 0;
  5611. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  5612. }
  5613. event = &lstate->event_buffer[lstate->event_w_idx];
  5614. event->initiator_id = initiator_id;
  5615. event->event_type = event_type;
  5616. event->event_arg = event_arg;
  5617. lstate->event_w_idx++;
  5618. if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5619. lstate->event_w_idx = 0;
  5620. }
  5621. /*
  5622. * Send any target mode events queued up waiting
  5623. * for immediate notify resources.
  5624. */
  5625. void
  5626. ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
  5627. {
  5628. struct ccb_hdr *ccbh;
  5629. struct ccb_immed_notify *inot;
  5630. while (lstate->event_r_idx != lstate->event_w_idx
  5631. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  5632. struct ahc_tmode_event *event;
  5633. event = &lstate->event_buffer[lstate->event_r_idx];
  5634. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  5635. inot = (struct ccb_immed_notify *)ccbh;
  5636. switch (event->event_type) {
  5637. case EVENT_TYPE_BUS_RESET:
  5638. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  5639. break;
  5640. default:
  5641. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  5642. inot->message_args[0] = event->event_type;
  5643. inot->message_args[1] = event->event_arg;
  5644. break;
  5645. }
  5646. inot->initiator_id = event->initiator_id;
  5647. inot->sense_len = 0;
  5648. xpt_done((union ccb *)inot);
  5649. lstate->event_r_idx++;
  5650. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5651. lstate->event_r_idx = 0;
  5652. }
  5653. }
  5654. #endif
  5655. /******************** Sequencer Program Patching/Download *********************/
  5656. #ifdef AHC_DUMP_SEQ
  5657. void
  5658. ahc_dumpseq(struct ahc_softc* ahc)
  5659. {
  5660. int i;
  5661. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5662. ahc_outb(ahc, SEQADDR0, 0);
  5663. ahc_outb(ahc, SEQADDR1, 0);
  5664. for (i = 0; i < ahc->instruction_ram_size; i++) {
  5665. uint8_t ins_bytes[4];
  5666. ahc_insb(ahc, SEQRAM, ins_bytes, 4);
  5667. printf("0x%08x\n", ins_bytes[0] << 24
  5668. | ins_bytes[1] << 16
  5669. | ins_bytes[2] << 8
  5670. | ins_bytes[3]);
  5671. }
  5672. }
  5673. #endif
  5674. static int
  5675. ahc_loadseq(struct ahc_softc *ahc)
  5676. {
  5677. struct cs cs_table[num_critical_sections];
  5678. u_int begin_set[num_critical_sections];
  5679. u_int end_set[num_critical_sections];
  5680. struct patch *cur_patch;
  5681. u_int cs_count;
  5682. u_int cur_cs;
  5683. u_int i;
  5684. u_int skip_addr;
  5685. u_int sg_prefetch_cnt;
  5686. int downloaded;
  5687. uint8_t download_consts[7];
  5688. /*
  5689. * Start out with 0 critical sections
  5690. * that apply to this firmware load.
  5691. */
  5692. cs_count = 0;
  5693. cur_cs = 0;
  5694. memset(begin_set, 0, sizeof(begin_set));
  5695. memset(end_set, 0, sizeof(end_set));
  5696. /* Setup downloadable constant table */
  5697. download_consts[QOUTFIFO_OFFSET] = 0;
  5698. if (ahc->targetcmds != NULL)
  5699. download_consts[QOUTFIFO_OFFSET] += 32;
  5700. download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
  5701. download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
  5702. download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
  5703. sg_prefetch_cnt = ahc->pci_cachesize;
  5704. if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
  5705. sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
  5706. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  5707. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
  5708. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
  5709. cur_patch = patches;
  5710. downloaded = 0;
  5711. skip_addr = 0;
  5712. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5713. ahc_outb(ahc, SEQADDR0, 0);
  5714. ahc_outb(ahc, SEQADDR1, 0);
  5715. for (i = 0; i < sizeof(seqprog)/4; i++) {
  5716. if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
  5717. /*
  5718. * Don't download this instruction as it
  5719. * is in a patch that was removed.
  5720. */
  5721. continue;
  5722. }
  5723. if (downloaded == ahc->instruction_ram_size) {
  5724. /*
  5725. * We're about to exceed the instruction
  5726. * storage capacity for this chip. Fail
  5727. * the load.
  5728. */
  5729. printf("\n%s: Program too large for instruction memory "
  5730. "size of %d!\n", ahc_name(ahc),
  5731. ahc->instruction_ram_size);
  5732. return (ENOMEM);
  5733. }
  5734. /*
  5735. * Move through the CS table until we find a CS
  5736. * that might apply to this instruction.
  5737. */
  5738. for (; cur_cs < num_critical_sections; cur_cs++) {
  5739. if (critical_sections[cur_cs].end <= i) {
  5740. if (begin_set[cs_count] == TRUE
  5741. && end_set[cs_count] == FALSE) {
  5742. cs_table[cs_count].end = downloaded;
  5743. end_set[cs_count] = TRUE;
  5744. cs_count++;
  5745. }
  5746. continue;
  5747. }
  5748. if (critical_sections[cur_cs].begin <= i
  5749. && begin_set[cs_count] == FALSE) {
  5750. cs_table[cs_count].begin = downloaded;
  5751. begin_set[cs_count] = TRUE;
  5752. }
  5753. break;
  5754. }
  5755. ahc_download_instr(ahc, i, download_consts);
  5756. downloaded++;
  5757. }
  5758. ahc->num_critical_sections = cs_count;
  5759. if (cs_count != 0) {
  5760. cs_count *= sizeof(struct cs);
  5761. ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  5762. if (ahc->critical_sections == NULL)
  5763. panic("ahc_loadseq: Could not malloc");
  5764. memcpy(ahc->critical_sections, cs_table, cs_count);
  5765. }
  5766. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
  5767. if (bootverbose) {
  5768. printf(" %d instructions downloaded\n", downloaded);
  5769. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  5770. ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
  5771. }
  5772. return (0);
  5773. }
  5774. static int
  5775. ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
  5776. u_int start_instr, u_int *skip_addr)
  5777. {
  5778. struct patch *cur_patch;
  5779. struct patch *last_patch;
  5780. u_int num_patches;
  5781. num_patches = ARRAY_SIZE(patches);
  5782. last_patch = &patches[num_patches];
  5783. cur_patch = *start_patch;
  5784. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  5785. if (cur_patch->patch_func(ahc) == 0) {
  5786. /* Start rejecting code */
  5787. *skip_addr = start_instr + cur_patch->skip_instr;
  5788. cur_patch += cur_patch->skip_patch;
  5789. } else {
  5790. /* Accepted this patch. Advance to the next
  5791. * one and wait for our intruction pointer to
  5792. * hit this point.
  5793. */
  5794. cur_patch++;
  5795. }
  5796. }
  5797. *start_patch = cur_patch;
  5798. if (start_instr < *skip_addr)
  5799. /* Still skipping */
  5800. return (0);
  5801. return (1);
  5802. }
  5803. static void
  5804. ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
  5805. {
  5806. union ins_formats instr;
  5807. struct ins_format1 *fmt1_ins;
  5808. struct ins_format3 *fmt3_ins;
  5809. u_int opcode;
  5810. /*
  5811. * The firmware is always compiled into a little endian format.
  5812. */
  5813. instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  5814. fmt1_ins = &instr.format1;
  5815. fmt3_ins = NULL;
  5816. /* Pull the opcode */
  5817. opcode = instr.format1.opcode;
  5818. switch (opcode) {
  5819. case AIC_OP_JMP:
  5820. case AIC_OP_JC:
  5821. case AIC_OP_JNC:
  5822. case AIC_OP_CALL:
  5823. case AIC_OP_JNE:
  5824. case AIC_OP_JNZ:
  5825. case AIC_OP_JE:
  5826. case AIC_OP_JZ:
  5827. {
  5828. struct patch *cur_patch;
  5829. int address_offset;
  5830. u_int address;
  5831. u_int skip_addr;
  5832. u_int i;
  5833. fmt3_ins = &instr.format3;
  5834. address_offset = 0;
  5835. address = fmt3_ins->address;
  5836. cur_patch = patches;
  5837. skip_addr = 0;
  5838. for (i = 0; i < address;) {
  5839. ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
  5840. if (skip_addr > i) {
  5841. int end_addr;
  5842. end_addr = min(address, skip_addr);
  5843. address_offset += end_addr - i;
  5844. i = skip_addr;
  5845. } else {
  5846. i++;
  5847. }
  5848. }
  5849. address -= address_offset;
  5850. fmt3_ins->address = address;
  5851. /* FALLTHROUGH */
  5852. }
  5853. case AIC_OP_OR:
  5854. case AIC_OP_AND:
  5855. case AIC_OP_XOR:
  5856. case AIC_OP_ADD:
  5857. case AIC_OP_ADC:
  5858. case AIC_OP_BMOV:
  5859. if (fmt1_ins->parity != 0) {
  5860. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  5861. }
  5862. fmt1_ins->parity = 0;
  5863. if ((ahc->features & AHC_CMD_CHAN) == 0
  5864. && opcode == AIC_OP_BMOV) {
  5865. /*
  5866. * Block move was added at the same time
  5867. * as the command channel. Verify that
  5868. * this is only a move of a single element
  5869. * and convert the BMOV to a MOV
  5870. * (AND with an immediate of FF).
  5871. */
  5872. if (fmt1_ins->immediate != 1)
  5873. panic("%s: BMOV not supported\n",
  5874. ahc_name(ahc));
  5875. fmt1_ins->opcode = AIC_OP_AND;
  5876. fmt1_ins->immediate = 0xff;
  5877. }
  5878. /* FALLTHROUGH */
  5879. case AIC_OP_ROL:
  5880. if ((ahc->features & AHC_ULTRA2) != 0) {
  5881. int i, count;
  5882. /* Calculate odd parity for the instruction */
  5883. for (i = 0, count = 0; i < 31; i++) {
  5884. uint32_t mask;
  5885. mask = 0x01 << i;
  5886. if ((instr.integer & mask) != 0)
  5887. count++;
  5888. }
  5889. if ((count & 0x01) == 0)
  5890. instr.format1.parity = 1;
  5891. } else {
  5892. /* Compress the instruction for older sequencers */
  5893. if (fmt3_ins != NULL) {
  5894. instr.integer =
  5895. fmt3_ins->immediate
  5896. | (fmt3_ins->source << 8)
  5897. | (fmt3_ins->address << 16)
  5898. | (fmt3_ins->opcode << 25);
  5899. } else {
  5900. instr.integer =
  5901. fmt1_ins->immediate
  5902. | (fmt1_ins->source << 8)
  5903. | (fmt1_ins->destination << 16)
  5904. | (fmt1_ins->ret << 24)
  5905. | (fmt1_ins->opcode << 25);
  5906. }
  5907. }
  5908. /* The sequencer is a little endian cpu */
  5909. instr.integer = ahc_htole32(instr.integer);
  5910. ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
  5911. break;
  5912. default:
  5913. panic("Unknown opcode encountered in seq program");
  5914. break;
  5915. }
  5916. }
  5917. int
  5918. ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries,
  5919. const char *name, u_int address, u_int value,
  5920. u_int *cur_column, u_int wrap_point)
  5921. {
  5922. int printed;
  5923. u_int printed_mask;
  5924. if (cur_column != NULL && *cur_column >= wrap_point) {
  5925. printf("\n");
  5926. *cur_column = 0;
  5927. }
  5928. printed = printf("%s[0x%x]", name, value);
  5929. if (table == NULL) {
  5930. printed += printf(" ");
  5931. *cur_column += printed;
  5932. return (printed);
  5933. }
  5934. printed_mask = 0;
  5935. while (printed_mask != 0xFF) {
  5936. int entry;
  5937. for (entry = 0; entry < num_entries; entry++) {
  5938. if (((value & table[entry].mask)
  5939. != table[entry].value)
  5940. || ((printed_mask & table[entry].mask)
  5941. == table[entry].mask))
  5942. continue;
  5943. printed += printf("%s%s",
  5944. printed_mask == 0 ? ":(" : "|",
  5945. table[entry].name);
  5946. printed_mask |= table[entry].mask;
  5947. break;
  5948. }
  5949. if (entry >= num_entries)
  5950. break;
  5951. }
  5952. if (printed_mask != 0)
  5953. printed += printf(") ");
  5954. else
  5955. printed += printf(" ");
  5956. if (cur_column != NULL)
  5957. *cur_column += printed;
  5958. return (printed);
  5959. }
  5960. void
  5961. ahc_dump_card_state(struct ahc_softc *ahc)
  5962. {
  5963. struct scb *scb;
  5964. struct scb_tailq *untagged_q;
  5965. u_int cur_col;
  5966. int paused;
  5967. int target;
  5968. int maxtarget;
  5969. int i;
  5970. uint8_t last_phase;
  5971. uint8_t qinpos;
  5972. uint8_t qintail;
  5973. uint8_t qoutpos;
  5974. uint8_t scb_index;
  5975. uint8_t saved_scbptr;
  5976. if (ahc_is_paused(ahc)) {
  5977. paused = 1;
  5978. } else {
  5979. paused = 0;
  5980. ahc_pause(ahc);
  5981. }
  5982. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5983. last_phase = ahc_inb(ahc, LASTPHASE);
  5984. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  5985. "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
  5986. ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
  5987. ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
  5988. if (paused)
  5989. printf("Card was paused\n");
  5990. printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
  5991. ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
  5992. ahc_inb(ahc, ARG_2));
  5993. printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
  5994. ahc_inb(ahc, SCBPTR));
  5995. cur_col = 0;
  5996. if ((ahc->features & AHC_DT) != 0)
  5997. ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
  5998. ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
  5999. ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
  6000. ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
  6001. ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
  6002. ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
  6003. ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
  6004. ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
  6005. ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
  6006. ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
  6007. ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
  6008. ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
  6009. ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
  6010. ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
  6011. ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
  6012. ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
  6013. ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
  6014. ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
  6015. ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
  6016. if (cur_col != 0)
  6017. printf("\n");
  6018. printf("STACK:");
  6019. for (i = 0; i < STACK_SIZE; i++)
  6020. printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
  6021. printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
  6022. printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
  6023. printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
  6024. /* QINFIFO */
  6025. printf("QINFIFO entries: ");
  6026. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  6027. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  6028. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  6029. } else
  6030. qinpos = ahc_inb(ahc, QINPOS);
  6031. qintail = ahc->qinfifonext;
  6032. while (qinpos != qintail) {
  6033. printf("%d ", ahc->qinfifo[qinpos]);
  6034. qinpos++;
  6035. }
  6036. printf("\n");
  6037. printf("Waiting Queue entries: ");
  6038. scb_index = ahc_inb(ahc, WAITING_SCBH);
  6039. i = 0;
  6040. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6041. ahc_outb(ahc, SCBPTR, scb_index);
  6042. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6043. scb_index = ahc_inb(ahc, SCB_NEXT);
  6044. }
  6045. printf("\n");
  6046. printf("Disconnected Queue entries: ");
  6047. scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
  6048. i = 0;
  6049. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6050. ahc_outb(ahc, SCBPTR, scb_index);
  6051. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6052. scb_index = ahc_inb(ahc, SCB_NEXT);
  6053. }
  6054. printf("\n");
  6055. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  6056. printf("QOUTFIFO entries: ");
  6057. qoutpos = ahc->qoutfifonext;
  6058. i = 0;
  6059. while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
  6060. printf("%d ", ahc->qoutfifo[qoutpos]);
  6061. qoutpos++;
  6062. }
  6063. printf("\n");
  6064. printf("Sequencer Free SCB List: ");
  6065. scb_index = ahc_inb(ahc, FREE_SCBH);
  6066. i = 0;
  6067. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6068. ahc_outb(ahc, SCBPTR, scb_index);
  6069. printf("%d ", scb_index);
  6070. scb_index = ahc_inb(ahc, SCB_NEXT);
  6071. }
  6072. printf("\n");
  6073. printf("Sequencer SCB Info: ");
  6074. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  6075. ahc_outb(ahc, SCBPTR, i);
  6076. cur_col = printf("\n%3d ", i);
  6077. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
  6078. ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
  6079. ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
  6080. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6081. }
  6082. printf("\n");
  6083. printf("Pending list: ");
  6084. i = 0;
  6085. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6086. if (i++ > 256)
  6087. break;
  6088. cur_col = printf("\n%3d ", scb->hscb->tag);
  6089. ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
  6090. ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
  6091. ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
  6092. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  6093. ahc_outb(ahc, SCBPTR, scb->hscb->tag);
  6094. printf("(");
  6095. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
  6096. &cur_col, 60);
  6097. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6098. printf(")");
  6099. }
  6100. }
  6101. printf("\n");
  6102. printf("Kernel Free SCB list: ");
  6103. i = 0;
  6104. SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
  6105. if (i++ > 256)
  6106. break;
  6107. printf("%d ", scb->hscb->tag);
  6108. }
  6109. printf("\n");
  6110. maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
  6111. for (target = 0; target <= maxtarget; target++) {
  6112. untagged_q = &ahc->untagged_queues[target];
  6113. if (TAILQ_FIRST(untagged_q) == NULL)
  6114. continue;
  6115. printf("Untagged Q(%d): ", target);
  6116. i = 0;
  6117. TAILQ_FOREACH(scb, untagged_q, links.tqe) {
  6118. if (i++ > 256)
  6119. break;
  6120. printf("%d ", scb->hscb->tag);
  6121. }
  6122. printf("\n");
  6123. }
  6124. ahc_platform_dump_card_state(ahc);
  6125. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  6126. ahc_outb(ahc, SCBPTR, saved_scbptr);
  6127. if (paused == 0)
  6128. ahc_unpause(ahc);
  6129. }
  6130. /************************* Target Mode ****************************************/
  6131. #ifdef AHC_TARGET_MODE
  6132. cam_status
  6133. ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
  6134. struct ahc_tmode_tstate **tstate,
  6135. struct ahc_tmode_lstate **lstate,
  6136. int notfound_failure)
  6137. {
  6138. if ((ahc->features & AHC_TARGETMODE) == 0)
  6139. return (CAM_REQ_INVALID);
  6140. /*
  6141. * Handle the 'black hole' device that sucks up
  6142. * requests to unattached luns on enabled targets.
  6143. */
  6144. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  6145. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  6146. *tstate = NULL;
  6147. *lstate = ahc->black_hole;
  6148. } else {
  6149. u_int max_id;
  6150. max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  6151. if (ccb->ccb_h.target_id >= max_id)
  6152. return (CAM_TID_INVALID);
  6153. if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
  6154. return (CAM_LUN_INVALID);
  6155. *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
  6156. *lstate = NULL;
  6157. if (*tstate != NULL)
  6158. *lstate =
  6159. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  6160. }
  6161. if (notfound_failure != 0 && *lstate == NULL)
  6162. return (CAM_PATH_INVALID);
  6163. return (CAM_REQ_CMP);
  6164. }
  6165. void
  6166. ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
  6167. {
  6168. struct ahc_tmode_tstate *tstate;
  6169. struct ahc_tmode_lstate *lstate;
  6170. struct ccb_en_lun *cel;
  6171. cam_status status;
  6172. u_long s;
  6173. u_int target;
  6174. u_int lun;
  6175. u_int target_mask;
  6176. u_int our_id;
  6177. int error;
  6178. char channel;
  6179. status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
  6180. /*notfound_failure*/FALSE);
  6181. if (status != CAM_REQ_CMP) {
  6182. ccb->ccb_h.status = status;
  6183. return;
  6184. }
  6185. if (cam_sim_bus(sim) == 0)
  6186. our_id = ahc->our_id;
  6187. else
  6188. our_id = ahc->our_id_b;
  6189. if (ccb->ccb_h.target_id != our_id) {
  6190. /*
  6191. * our_id represents our initiator ID, or
  6192. * the ID of the first target to have an
  6193. * enabled lun in target mode. There are
  6194. * two cases that may preclude enabling a
  6195. * target id other than our_id.
  6196. *
  6197. * o our_id is for an active initiator role.
  6198. * Since the hardware does not support
  6199. * reselections to the initiator role at
  6200. * anything other than our_id, and our_id
  6201. * is used by the hardware to indicate the
  6202. * ID to use for both select-out and
  6203. * reselect-out operations, the only target
  6204. * ID we can support in this mode is our_id.
  6205. *
  6206. * o The MULTARGID feature is not available and
  6207. * a previous target mode ID has been enabled.
  6208. */
  6209. if ((ahc->features & AHC_MULTIROLE) != 0) {
  6210. if ((ahc->features & AHC_MULTI_TID) != 0
  6211. && (ahc->flags & AHC_INITIATORROLE) != 0) {
  6212. /*
  6213. * Only allow additional targets if
  6214. * the initiator role is disabled.
  6215. * The hardware cannot handle a re-select-in
  6216. * on the initiator id during a re-select-out
  6217. * on a different target id.
  6218. */
  6219. status = CAM_TID_INVALID;
  6220. } else if ((ahc->flags & AHC_INITIATORROLE) != 0
  6221. || ahc->enabled_luns > 0) {
  6222. /*
  6223. * Only allow our target id to change
  6224. * if the initiator role is not configured
  6225. * and there are no enabled luns which
  6226. * are attached to the currently registered
  6227. * scsi id.
  6228. */
  6229. status = CAM_TID_INVALID;
  6230. }
  6231. } else if ((ahc->features & AHC_MULTI_TID) == 0
  6232. && ahc->enabled_luns > 0) {
  6233. status = CAM_TID_INVALID;
  6234. }
  6235. }
  6236. if (status != CAM_REQ_CMP) {
  6237. ccb->ccb_h.status = status;
  6238. return;
  6239. }
  6240. /*
  6241. * We now have an id that is valid.
  6242. * If we aren't in target mode, switch modes.
  6243. */
  6244. if ((ahc->flags & AHC_TARGETROLE) == 0
  6245. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  6246. u_long s;
  6247. ahc_flag saved_flags;
  6248. printf("Configuring Target Mode\n");
  6249. ahc_lock(ahc, &s);
  6250. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  6251. ccb->ccb_h.status = CAM_BUSY;
  6252. ahc_unlock(ahc, &s);
  6253. return;
  6254. }
  6255. saved_flags = ahc->flags;
  6256. ahc->flags |= AHC_TARGETROLE;
  6257. if ((ahc->features & AHC_MULTIROLE) == 0)
  6258. ahc->flags &= ~AHC_INITIATORROLE;
  6259. ahc_pause(ahc);
  6260. error = ahc_loadseq(ahc);
  6261. if (error != 0) {
  6262. /*
  6263. * Restore original configuration and notify
  6264. * the caller that we cannot support target mode.
  6265. * Since the adapter started out in this
  6266. * configuration, the firmware load will succeed,
  6267. * so there is no point in checking ahc_loadseq's
  6268. * return value.
  6269. */
  6270. ahc->flags = saved_flags;
  6271. (void)ahc_loadseq(ahc);
  6272. ahc_restart(ahc);
  6273. ahc_unlock(ahc, &s);
  6274. ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  6275. return;
  6276. }
  6277. ahc_restart(ahc);
  6278. ahc_unlock(ahc, &s);
  6279. }
  6280. cel = &ccb->cel;
  6281. target = ccb->ccb_h.target_id;
  6282. lun = ccb->ccb_h.target_lun;
  6283. channel = SIM_CHANNEL(ahc, sim);
  6284. target_mask = 0x01 << target;
  6285. if (channel == 'B')
  6286. target_mask <<= 8;
  6287. if (cel->enable != 0) {
  6288. u_int scsiseq;
  6289. /* Are we already enabled?? */
  6290. if (lstate != NULL) {
  6291. xpt_print_path(ccb->ccb_h.path);
  6292. printf("Lun already enabled\n");
  6293. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  6294. return;
  6295. }
  6296. if (cel->grp6_len != 0
  6297. || cel->grp7_len != 0) {
  6298. /*
  6299. * Don't (yet?) support vendor
  6300. * specific commands.
  6301. */
  6302. ccb->ccb_h.status = CAM_REQ_INVALID;
  6303. printf("Non-zero Group Codes\n");
  6304. return;
  6305. }
  6306. /*
  6307. * Seems to be okay.
  6308. * Setup our data structures.
  6309. */
  6310. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  6311. tstate = ahc_alloc_tstate(ahc, target, channel);
  6312. if (tstate == NULL) {
  6313. xpt_print_path(ccb->ccb_h.path);
  6314. printf("Couldn't allocate tstate\n");
  6315. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6316. return;
  6317. }
  6318. }
  6319. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  6320. if (lstate == NULL) {
  6321. xpt_print_path(ccb->ccb_h.path);
  6322. printf("Couldn't allocate lstate\n");
  6323. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6324. return;
  6325. }
  6326. memset(lstate, 0, sizeof(*lstate));
  6327. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  6328. xpt_path_path_id(ccb->ccb_h.path),
  6329. xpt_path_target_id(ccb->ccb_h.path),
  6330. xpt_path_lun_id(ccb->ccb_h.path));
  6331. if (status != CAM_REQ_CMP) {
  6332. free(lstate, M_DEVBUF);
  6333. xpt_print_path(ccb->ccb_h.path);
  6334. printf("Couldn't allocate path\n");
  6335. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6336. return;
  6337. }
  6338. SLIST_INIT(&lstate->accept_tios);
  6339. SLIST_INIT(&lstate->immed_notifies);
  6340. ahc_lock(ahc, &s);
  6341. ahc_pause(ahc);
  6342. if (target != CAM_TARGET_WILDCARD) {
  6343. tstate->enabled_luns[lun] = lstate;
  6344. ahc->enabled_luns++;
  6345. if ((ahc->features & AHC_MULTI_TID) != 0) {
  6346. u_int targid_mask;
  6347. targid_mask = ahc_inb(ahc, TARGID)
  6348. | (ahc_inb(ahc, TARGID + 1) << 8);
  6349. targid_mask |= target_mask;
  6350. ahc_outb(ahc, TARGID, targid_mask);
  6351. ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
  6352. ahc_update_scsiid(ahc, targid_mask);
  6353. } else {
  6354. u_int our_id;
  6355. char channel;
  6356. channel = SIM_CHANNEL(ahc, sim);
  6357. our_id = SIM_SCSI_ID(ahc, sim);
  6358. /*
  6359. * This can only happen if selections
  6360. * are not enabled
  6361. */
  6362. if (target != our_id) {
  6363. u_int sblkctl;
  6364. char cur_channel;
  6365. int swap;
  6366. sblkctl = ahc_inb(ahc, SBLKCTL);
  6367. cur_channel = (sblkctl & SELBUSB)
  6368. ? 'B' : 'A';
  6369. if ((ahc->features & AHC_TWIN) == 0)
  6370. cur_channel = 'A';
  6371. swap = cur_channel != channel;
  6372. if (channel == 'A')
  6373. ahc->our_id = target;
  6374. else
  6375. ahc->our_id_b = target;
  6376. if (swap)
  6377. ahc_outb(ahc, SBLKCTL,
  6378. sblkctl ^ SELBUSB);
  6379. ahc_outb(ahc, SCSIID, target);
  6380. if (swap)
  6381. ahc_outb(ahc, SBLKCTL, sblkctl);
  6382. }
  6383. }
  6384. } else
  6385. ahc->black_hole = lstate;
  6386. /* Allow select-in operations */
  6387. if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
  6388. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6389. scsiseq |= ENSELI;
  6390. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6391. scsiseq = ahc_inb(ahc, SCSISEQ);
  6392. scsiseq |= ENSELI;
  6393. ahc_outb(ahc, SCSISEQ, scsiseq);
  6394. }
  6395. ahc_unpause(ahc);
  6396. ahc_unlock(ahc, &s);
  6397. ccb->ccb_h.status = CAM_REQ_CMP;
  6398. xpt_print_path(ccb->ccb_h.path);
  6399. printf("Lun now enabled for target mode\n");
  6400. } else {
  6401. struct scb *scb;
  6402. int i, empty;
  6403. if (lstate == NULL) {
  6404. ccb->ccb_h.status = CAM_LUN_INVALID;
  6405. return;
  6406. }
  6407. ahc_lock(ahc, &s);
  6408. ccb->ccb_h.status = CAM_REQ_CMP;
  6409. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6410. struct ccb_hdr *ccbh;
  6411. ccbh = &scb->io_ctx->ccb_h;
  6412. if (ccbh->func_code == XPT_CONT_TARGET_IO
  6413. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  6414. printf("CTIO pending\n");
  6415. ccb->ccb_h.status = CAM_REQ_INVALID;
  6416. ahc_unlock(ahc, &s);
  6417. return;
  6418. }
  6419. }
  6420. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  6421. printf("ATIOs pending\n");
  6422. ccb->ccb_h.status = CAM_REQ_INVALID;
  6423. }
  6424. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  6425. printf("INOTs pending\n");
  6426. ccb->ccb_h.status = CAM_REQ_INVALID;
  6427. }
  6428. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  6429. ahc_unlock(ahc, &s);
  6430. return;
  6431. }
  6432. xpt_print_path(ccb->ccb_h.path);
  6433. printf("Target mode disabled\n");
  6434. xpt_free_path(lstate->path);
  6435. free(lstate, M_DEVBUF);
  6436. ahc_pause(ahc);
  6437. /* Can we clean up the target too? */
  6438. if (target != CAM_TARGET_WILDCARD) {
  6439. tstate->enabled_luns[lun] = NULL;
  6440. ahc->enabled_luns--;
  6441. for (empty = 1, i = 0; i < 8; i++)
  6442. if (tstate->enabled_luns[i] != NULL) {
  6443. empty = 0;
  6444. break;
  6445. }
  6446. if (empty) {
  6447. ahc_free_tstate(ahc, target, channel,
  6448. /*force*/FALSE);
  6449. if (ahc->features & AHC_MULTI_TID) {
  6450. u_int targid_mask;
  6451. targid_mask = ahc_inb(ahc, TARGID)
  6452. | (ahc_inb(ahc, TARGID + 1)
  6453. << 8);
  6454. targid_mask &= ~target_mask;
  6455. ahc_outb(ahc, TARGID, targid_mask);
  6456. ahc_outb(ahc, TARGID+1,
  6457. (targid_mask >> 8));
  6458. ahc_update_scsiid(ahc, targid_mask);
  6459. }
  6460. }
  6461. } else {
  6462. ahc->black_hole = NULL;
  6463. /*
  6464. * We can't allow selections without
  6465. * our black hole device.
  6466. */
  6467. empty = TRUE;
  6468. }
  6469. if (ahc->enabled_luns == 0) {
  6470. /* Disallow select-in */
  6471. u_int scsiseq;
  6472. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6473. scsiseq &= ~ENSELI;
  6474. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6475. scsiseq = ahc_inb(ahc, SCSISEQ);
  6476. scsiseq &= ~ENSELI;
  6477. ahc_outb(ahc, SCSISEQ, scsiseq);
  6478. if ((ahc->features & AHC_MULTIROLE) == 0) {
  6479. printf("Configuring Initiator Mode\n");
  6480. ahc->flags &= ~AHC_TARGETROLE;
  6481. ahc->flags |= AHC_INITIATORROLE;
  6482. /*
  6483. * Returning to a configuration that
  6484. * fit previously will always succeed.
  6485. */
  6486. (void)ahc_loadseq(ahc);
  6487. ahc_restart(ahc);
  6488. /*
  6489. * Unpaused. The extra unpause
  6490. * that follows is harmless.
  6491. */
  6492. }
  6493. }
  6494. ahc_unpause(ahc);
  6495. ahc_unlock(ahc, &s);
  6496. }
  6497. }
  6498. static void
  6499. ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
  6500. {
  6501. u_int scsiid_mask;
  6502. u_int scsiid;
  6503. if ((ahc->features & AHC_MULTI_TID) == 0)
  6504. panic("ahc_update_scsiid called on non-multitid unit\n");
  6505. /*
  6506. * Since we will rely on the TARGID mask
  6507. * for selection enables, ensure that OID
  6508. * in SCSIID is not set to some other ID
  6509. * that we don't want to allow selections on.
  6510. */
  6511. if ((ahc->features & AHC_ULTRA2) != 0)
  6512. scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
  6513. else
  6514. scsiid = ahc_inb(ahc, SCSIID);
  6515. scsiid_mask = 0x1 << (scsiid & OID);
  6516. if ((targid_mask & scsiid_mask) == 0) {
  6517. u_int our_id;
  6518. /* ffs counts from 1 */
  6519. our_id = ffs(targid_mask);
  6520. if (our_id == 0)
  6521. our_id = ahc->our_id;
  6522. else
  6523. our_id--;
  6524. scsiid &= TID;
  6525. scsiid |= our_id;
  6526. }
  6527. if ((ahc->features & AHC_ULTRA2) != 0)
  6528. ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
  6529. else
  6530. ahc_outb(ahc, SCSIID, scsiid);
  6531. }
  6532. void
  6533. ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
  6534. {
  6535. struct target_cmd *cmd;
  6536. /*
  6537. * If the card supports auto-access pause,
  6538. * we can access the card directly regardless
  6539. * of whether it is paused or not.
  6540. */
  6541. if ((ahc->features & AHC_AUTOPAUSE) != 0)
  6542. paused = TRUE;
  6543. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
  6544. while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
  6545. /*
  6546. * Only advance through the queue if we
  6547. * have the resources to process the command.
  6548. */
  6549. if (ahc_handle_target_cmd(ahc, cmd) != 0)
  6550. break;
  6551. cmd->cmd_valid = 0;
  6552. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  6553. ahc->shared_data_dmamap,
  6554. ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
  6555. sizeof(struct target_cmd),
  6556. BUS_DMASYNC_PREREAD);
  6557. ahc->tqinfifonext++;
  6558. /*
  6559. * Lazily update our position in the target mode incoming
  6560. * command queue as seen by the sequencer.
  6561. */
  6562. if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  6563. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  6564. u_int hs_mailbox;
  6565. hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
  6566. hs_mailbox &= ~HOST_TQINPOS;
  6567. hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
  6568. ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
  6569. } else {
  6570. if (!paused)
  6571. ahc_pause(ahc);
  6572. ahc_outb(ahc, KERNEL_TQINPOS,
  6573. ahc->tqinfifonext & HOST_TQINPOS);
  6574. if (!paused)
  6575. ahc_unpause(ahc);
  6576. }
  6577. }
  6578. }
  6579. }
  6580. static int
  6581. ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
  6582. {
  6583. struct ahc_tmode_tstate *tstate;
  6584. struct ahc_tmode_lstate *lstate;
  6585. struct ccb_accept_tio *atio;
  6586. uint8_t *byte;
  6587. int initiator;
  6588. int target;
  6589. int lun;
  6590. initiator = SCSIID_TARGET(ahc, cmd->scsiid);
  6591. target = SCSIID_OUR_ID(cmd->scsiid);
  6592. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  6593. byte = cmd->bytes;
  6594. tstate = ahc->enabled_targets[target];
  6595. lstate = NULL;
  6596. if (tstate != NULL)
  6597. lstate = tstate->enabled_luns[lun];
  6598. /*
  6599. * Commands for disabled luns go to the black hole driver.
  6600. */
  6601. if (lstate == NULL)
  6602. lstate = ahc->black_hole;
  6603. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  6604. if (atio == NULL) {
  6605. ahc->flags |= AHC_TQINFIFO_BLOCKED;
  6606. /*
  6607. * Wait for more ATIOs from the peripheral driver for this lun.
  6608. */
  6609. if (bootverbose)
  6610. printf("%s: ATIOs exhausted\n", ahc_name(ahc));
  6611. return (1);
  6612. } else
  6613. ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
  6614. #if 0
  6615. printf("Incoming command from %d for %d:%d%s\n",
  6616. initiator, target, lun,
  6617. lstate == ahc->black_hole ? "(Black Holed)" : "");
  6618. #endif
  6619. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  6620. if (lstate == ahc->black_hole) {
  6621. /* Fill in the wildcards */
  6622. atio->ccb_h.target_id = target;
  6623. atio->ccb_h.target_lun = lun;
  6624. }
  6625. /*
  6626. * Package it up and send it off to
  6627. * whomever has this lun enabled.
  6628. */
  6629. atio->sense_len = 0;
  6630. atio->init_id = initiator;
  6631. if (byte[0] != 0xFF) {
  6632. /* Tag was included */
  6633. atio->tag_action = *byte++;
  6634. atio->tag_id = *byte++;
  6635. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  6636. } else {
  6637. atio->ccb_h.flags = 0;
  6638. }
  6639. byte++;
  6640. /* Okay. Now determine the cdb size based on the command code */
  6641. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  6642. case 0:
  6643. atio->cdb_len = 6;
  6644. break;
  6645. case 1:
  6646. case 2:
  6647. atio->cdb_len = 10;
  6648. break;
  6649. case 4:
  6650. atio->cdb_len = 16;
  6651. break;
  6652. case 5:
  6653. atio->cdb_len = 12;
  6654. break;
  6655. case 3:
  6656. default:
  6657. /* Only copy the opcode. */
  6658. atio->cdb_len = 1;
  6659. printf("Reserved or VU command code type encountered\n");
  6660. break;
  6661. }
  6662. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  6663. atio->ccb_h.status |= CAM_CDB_RECVD;
  6664. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  6665. /*
  6666. * We weren't allowed to disconnect.
  6667. * We're hanging on the bus until a
  6668. * continue target I/O comes in response
  6669. * to this accept tio.
  6670. */
  6671. #if 0
  6672. printf("Received Immediate Command %d:%d:%d - %p\n",
  6673. initiator, target, lun, ahc->pending_device);
  6674. #endif
  6675. ahc->pending_device = lstate;
  6676. ahc_freeze_ccb((union ccb *)atio);
  6677. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  6678. }
  6679. xpt_done((union ccb*)atio);
  6680. return (0);
  6681. }
  6682. #endif