aic7xxx_core.c 211 KB

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