aic7xxx_core.c 211 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974
  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 referrenced" },
  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 staus 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 = kmalloc(sizeof(*ahc->scb_data), GFP_ATOMIC);
  4082. if (ahc->scb_data == NULL)
  4083. return (ENOMEM);
  4084. memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
  4085. }
  4086. return (0);
  4087. }
  4088. void
  4089. ahc_set_unit(struct ahc_softc *ahc, int unit)
  4090. {
  4091. ahc->unit = unit;
  4092. }
  4093. void
  4094. ahc_set_name(struct ahc_softc *ahc, char *name)
  4095. {
  4096. if (ahc->name != NULL)
  4097. kfree(ahc->name);
  4098. ahc->name = name;
  4099. }
  4100. void
  4101. ahc_free(struct ahc_softc *ahc)
  4102. {
  4103. int i;
  4104. switch (ahc->init_level) {
  4105. default:
  4106. case 5:
  4107. ahc_shutdown(ahc);
  4108. /* FALLTHROUGH */
  4109. case 4:
  4110. ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
  4111. ahc->shared_data_dmamap);
  4112. /* FALLTHROUGH */
  4113. case 3:
  4114. ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
  4115. ahc->shared_data_dmamap);
  4116. ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
  4117. ahc->shared_data_dmamap);
  4118. /* FALLTHROUGH */
  4119. case 2:
  4120. ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
  4121. case 1:
  4122. #ifndef __linux__
  4123. ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
  4124. #endif
  4125. break;
  4126. case 0:
  4127. break;
  4128. }
  4129. #ifndef __linux__
  4130. ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
  4131. #endif
  4132. ahc_platform_free(ahc);
  4133. ahc_fini_scbdata(ahc);
  4134. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  4135. struct ahc_tmode_tstate *tstate;
  4136. tstate = ahc->enabled_targets[i];
  4137. if (tstate != NULL) {
  4138. #ifdef AHC_TARGET_MODE
  4139. int j;
  4140. for (j = 0; j < AHC_NUM_LUNS; j++) {
  4141. struct ahc_tmode_lstate *lstate;
  4142. lstate = tstate->enabled_luns[j];
  4143. if (lstate != NULL) {
  4144. xpt_free_path(lstate->path);
  4145. kfree(lstate);
  4146. }
  4147. }
  4148. #endif
  4149. kfree(tstate);
  4150. }
  4151. }
  4152. #ifdef AHC_TARGET_MODE
  4153. if (ahc->black_hole != NULL) {
  4154. xpt_free_path(ahc->black_hole->path);
  4155. kfree(ahc->black_hole);
  4156. }
  4157. #endif
  4158. if (ahc->name != NULL)
  4159. kfree(ahc->name);
  4160. if (ahc->seep_config != NULL)
  4161. kfree(ahc->seep_config);
  4162. #ifndef __FreeBSD__
  4163. kfree(ahc);
  4164. #endif
  4165. return;
  4166. }
  4167. static void
  4168. ahc_shutdown(void *arg)
  4169. {
  4170. struct ahc_softc *ahc;
  4171. int i;
  4172. ahc = (struct ahc_softc *)arg;
  4173. /* This will reset most registers to 0, but not all */
  4174. ahc_reset(ahc, /*reinit*/FALSE);
  4175. ahc_outb(ahc, SCSISEQ, 0);
  4176. ahc_outb(ahc, SXFRCTL0, 0);
  4177. ahc_outb(ahc, DSPCISTATUS, 0);
  4178. for (i = TARG_SCSIRATE; i < SCSICONF; i++)
  4179. ahc_outb(ahc, i, 0);
  4180. }
  4181. /*
  4182. * Reset the controller and record some information about it
  4183. * that is only available just after a reset. If "reinit" is
  4184. * non-zero, this reset occurred after initial configuration
  4185. * and the caller requests that the chip be fully reinitialized
  4186. * to a runable state. Chip interrupts are *not* enabled after
  4187. * a reinitialization. The caller must enable interrupts via
  4188. * ahc_intr_enable().
  4189. */
  4190. int
  4191. ahc_reset(struct ahc_softc *ahc, int reinit)
  4192. {
  4193. u_int sblkctl;
  4194. u_int sxfrctl1_a, sxfrctl1_b;
  4195. int error;
  4196. int wait;
  4197. /*
  4198. * Preserve the value of the SXFRCTL1 register for all channels.
  4199. * It contains settings that affect termination and we don't want
  4200. * to disturb the integrity of the bus.
  4201. */
  4202. ahc_pause(ahc);
  4203. sxfrctl1_b = 0;
  4204. if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
  4205. u_int sblkctl;
  4206. /*
  4207. * Save channel B's settings in case this chip
  4208. * is setup for TWIN channel operation.
  4209. */
  4210. sblkctl = ahc_inb(ahc, SBLKCTL);
  4211. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  4212. sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
  4213. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  4214. }
  4215. sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
  4216. ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
  4217. /*
  4218. * Ensure that the reset has finished. We delay 1000us
  4219. * prior to reading the register to make sure the chip
  4220. * has sufficiently completed its reset to handle register
  4221. * accesses.
  4222. */
  4223. wait = 1000;
  4224. do {
  4225. ahc_delay(1000);
  4226. } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
  4227. if (wait == 0) {
  4228. printk("%s: WARNING - Failed chip reset! "
  4229. "Trying to initialize anyway.\n", ahc_name(ahc));
  4230. }
  4231. ahc_outb(ahc, HCNTRL, ahc->pause);
  4232. /* Determine channel configuration */
  4233. sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
  4234. /* No Twin Channel PCI cards */
  4235. if ((ahc->chip & AHC_PCI) != 0)
  4236. sblkctl &= ~SELBUSB;
  4237. switch (sblkctl) {
  4238. case 0:
  4239. /* Single Narrow Channel */
  4240. break;
  4241. case 2:
  4242. /* Wide Channel */
  4243. ahc->features |= AHC_WIDE;
  4244. break;
  4245. case 8:
  4246. /* Twin Channel */
  4247. ahc->features |= AHC_TWIN;
  4248. break;
  4249. default:
  4250. printk(" Unsupported adapter type. Ignoring\n");
  4251. return(-1);
  4252. }
  4253. /*
  4254. * Reload sxfrctl1.
  4255. *
  4256. * We must always initialize STPWEN to 1 before we
  4257. * restore the saved values. STPWEN is initialized
  4258. * to a tri-state condition which can only be cleared
  4259. * by turning it on.
  4260. */
  4261. if ((ahc->features & AHC_TWIN) != 0) {
  4262. u_int sblkctl;
  4263. sblkctl = ahc_inb(ahc, SBLKCTL);
  4264. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  4265. ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
  4266. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  4267. }
  4268. ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
  4269. error = 0;
  4270. if (reinit != 0)
  4271. /*
  4272. * If a recovery action has forced a chip reset,
  4273. * re-initialize the chip to our liking.
  4274. */
  4275. error = ahc->bus_chip_init(ahc);
  4276. #ifdef AHC_DUMP_SEQ
  4277. else
  4278. ahc_dumpseq(ahc);
  4279. #endif
  4280. return (error);
  4281. }
  4282. /*
  4283. * Determine the number of SCBs available on the controller
  4284. */
  4285. int
  4286. ahc_probe_scbs(struct ahc_softc *ahc) {
  4287. int i;
  4288. for (i = 0; i < AHC_SCB_MAX; i++) {
  4289. ahc_outb(ahc, SCBPTR, i);
  4290. ahc_outb(ahc, SCB_BASE, i);
  4291. if (ahc_inb(ahc, SCB_BASE) != i)
  4292. break;
  4293. ahc_outb(ahc, SCBPTR, 0);
  4294. if (ahc_inb(ahc, SCB_BASE) != 0)
  4295. break;
  4296. }
  4297. return (i);
  4298. }
  4299. static void
  4300. ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  4301. {
  4302. dma_addr_t *baddr;
  4303. baddr = (dma_addr_t *)arg;
  4304. *baddr = segs->ds_addr;
  4305. }
  4306. static void
  4307. ahc_build_free_scb_list(struct ahc_softc *ahc)
  4308. {
  4309. int scbsize;
  4310. int i;
  4311. scbsize = 32;
  4312. if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
  4313. scbsize = 64;
  4314. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  4315. int j;
  4316. ahc_outb(ahc, SCBPTR, i);
  4317. /*
  4318. * Touch all SCB bytes to avoid parity errors
  4319. * should one of our debugging routines read
  4320. * an otherwise uninitiatlized byte.
  4321. */
  4322. for (j = 0; j < scbsize; j++)
  4323. ahc_outb(ahc, SCB_BASE+j, 0xFF);
  4324. /* Clear the control byte. */
  4325. ahc_outb(ahc, SCB_CONTROL, 0);
  4326. /* Set the next pointer */
  4327. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4328. ahc_outb(ahc, SCB_NEXT, i+1);
  4329. else
  4330. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  4331. /* Make the tag number, SCSIID, and lun invalid */
  4332. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  4333. ahc_outb(ahc, SCB_SCSIID, 0xFF);
  4334. ahc_outb(ahc, SCB_LUN, 0xFF);
  4335. }
  4336. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  4337. /* SCB 0 heads the free list. */
  4338. ahc_outb(ahc, FREE_SCBH, 0);
  4339. } else {
  4340. /* No free list. */
  4341. ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
  4342. }
  4343. /* Make sure that the last SCB terminates the free list */
  4344. ahc_outb(ahc, SCBPTR, i-1);
  4345. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  4346. }
  4347. static int
  4348. ahc_init_scbdata(struct ahc_softc *ahc)
  4349. {
  4350. struct scb_data *scb_data;
  4351. scb_data = ahc->scb_data;
  4352. SLIST_INIT(&scb_data->free_scbs);
  4353. SLIST_INIT(&scb_data->sg_maps);
  4354. /* Allocate SCB resources */
  4355. scb_data->scbarray = kmalloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC, GFP_ATOMIC);
  4356. if (scb_data->scbarray == NULL)
  4357. return (ENOMEM);
  4358. memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
  4359. /* Determine the number of hardware SCBs and initialize them */
  4360. scb_data->maxhscbs = ahc_probe_scbs(ahc);
  4361. if (ahc->scb_data->maxhscbs == 0) {
  4362. printk("%s: No SCB space found\n", ahc_name(ahc));
  4363. return (ENXIO);
  4364. }
  4365. /*
  4366. * Create our DMA tags. These tags define the kinds of device
  4367. * accessible memory allocations and memory mappings we will
  4368. * need to perform during normal operation.
  4369. *
  4370. * Unless we need to further restrict the allocation, we rely
  4371. * on the restrictions of the parent dmat, hence the common
  4372. * use of MAXADDR and MAXSIZE.
  4373. */
  4374. /* DMA tag for our hardware scb structures */
  4375. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4376. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4377. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4378. /*highaddr*/BUS_SPACE_MAXADDR,
  4379. /*filter*/NULL, /*filterarg*/NULL,
  4380. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  4381. /*nsegments*/1,
  4382. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4383. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  4384. goto error_exit;
  4385. }
  4386. scb_data->init_level++;
  4387. /* Allocation for our hscbs */
  4388. if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
  4389. (void **)&scb_data->hscbs,
  4390. BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
  4391. goto error_exit;
  4392. }
  4393. scb_data->init_level++;
  4394. /* And permanently map them */
  4395. ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
  4396. scb_data->hscbs,
  4397. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  4398. ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
  4399. scb_data->init_level++;
  4400. /* DMA tag for our sense buffers */
  4401. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4402. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4403. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4404. /*highaddr*/BUS_SPACE_MAXADDR,
  4405. /*filter*/NULL, /*filterarg*/NULL,
  4406. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  4407. /*nsegments*/1,
  4408. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4409. /*flags*/0, &scb_data->sense_dmat) != 0) {
  4410. goto error_exit;
  4411. }
  4412. scb_data->init_level++;
  4413. /* Allocate them */
  4414. if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
  4415. (void **)&scb_data->sense,
  4416. BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
  4417. goto error_exit;
  4418. }
  4419. scb_data->init_level++;
  4420. /* And permanently map them */
  4421. ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
  4422. scb_data->sense,
  4423. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  4424. ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
  4425. scb_data->init_level++;
  4426. /* DMA tag for our S/G structures. We allocate in page sized chunks */
  4427. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
  4428. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4429. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4430. /*highaddr*/BUS_SPACE_MAXADDR,
  4431. /*filter*/NULL, /*filterarg*/NULL,
  4432. PAGE_SIZE, /*nsegments*/1,
  4433. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4434. /*flags*/0, &scb_data->sg_dmat) != 0) {
  4435. goto error_exit;
  4436. }
  4437. scb_data->init_level++;
  4438. /* Perform initial CCB allocation */
  4439. memset(scb_data->hscbs, 0,
  4440. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
  4441. ahc_alloc_scbs(ahc);
  4442. if (scb_data->numscbs == 0) {
  4443. printk("%s: ahc_init_scbdata - "
  4444. "Unable to allocate initial scbs\n",
  4445. ahc_name(ahc));
  4446. goto error_exit;
  4447. }
  4448. /*
  4449. * Reserve the next queued SCB.
  4450. */
  4451. ahc->next_queued_scb = ahc_get_scb(ahc);
  4452. /*
  4453. * Note that we were successful
  4454. */
  4455. return (0);
  4456. error_exit:
  4457. return (ENOMEM);
  4458. }
  4459. static void
  4460. ahc_fini_scbdata(struct ahc_softc *ahc)
  4461. {
  4462. struct scb_data *scb_data;
  4463. scb_data = ahc->scb_data;
  4464. if (scb_data == NULL)
  4465. return;
  4466. switch (scb_data->init_level) {
  4467. default:
  4468. case 7:
  4469. {
  4470. struct sg_map_node *sg_map;
  4471. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
  4472. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  4473. ahc_dmamap_unload(ahc, scb_data->sg_dmat,
  4474. sg_map->sg_dmamap);
  4475. ahc_dmamem_free(ahc, scb_data->sg_dmat,
  4476. sg_map->sg_vaddr,
  4477. sg_map->sg_dmamap);
  4478. kfree(sg_map);
  4479. }
  4480. ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
  4481. }
  4482. case 6:
  4483. ahc_dmamap_unload(ahc, scb_data->sense_dmat,
  4484. scb_data->sense_dmamap);
  4485. case 5:
  4486. ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
  4487. scb_data->sense_dmamap);
  4488. ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
  4489. scb_data->sense_dmamap);
  4490. case 4:
  4491. ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
  4492. case 3:
  4493. ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
  4494. scb_data->hscb_dmamap);
  4495. case 2:
  4496. ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
  4497. scb_data->hscb_dmamap);
  4498. ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
  4499. scb_data->hscb_dmamap);
  4500. case 1:
  4501. ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
  4502. break;
  4503. case 0:
  4504. break;
  4505. }
  4506. if (scb_data->scbarray != NULL)
  4507. kfree(scb_data->scbarray);
  4508. }
  4509. static void
  4510. ahc_alloc_scbs(struct ahc_softc *ahc)
  4511. {
  4512. struct scb_data *scb_data;
  4513. struct scb *next_scb;
  4514. struct sg_map_node *sg_map;
  4515. dma_addr_t physaddr;
  4516. struct ahc_dma_seg *segs;
  4517. int newcount;
  4518. int i;
  4519. scb_data = ahc->scb_data;
  4520. if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
  4521. /* Can't allocate any more */
  4522. return;
  4523. next_scb = &scb_data->scbarray[scb_data->numscbs];
  4524. sg_map = kmalloc(sizeof(*sg_map), GFP_ATOMIC);
  4525. if (sg_map == NULL)
  4526. return;
  4527. /* Allocate S/G space for the next batch of SCBS */
  4528. if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
  4529. (void **)&sg_map->sg_vaddr,
  4530. BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  4531. kfree(sg_map);
  4532. return;
  4533. }
  4534. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  4535. ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
  4536. sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
  4537. &sg_map->sg_physaddr, /*flags*/0);
  4538. segs = sg_map->sg_vaddr;
  4539. physaddr = sg_map->sg_physaddr;
  4540. newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
  4541. newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
  4542. for (i = 0; i < newcount; i++) {
  4543. struct scb_platform_data *pdata;
  4544. #ifndef __linux__
  4545. int error;
  4546. #endif
  4547. pdata = kmalloc(sizeof(*pdata), GFP_ATOMIC);
  4548. if (pdata == NULL)
  4549. break;
  4550. next_scb->platform_data = pdata;
  4551. next_scb->sg_map = sg_map;
  4552. next_scb->sg_list = segs;
  4553. /*
  4554. * The sequencer always starts with the second entry.
  4555. * The first entry is embedded in the scb.
  4556. */
  4557. next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
  4558. next_scb->ahc_softc = ahc;
  4559. next_scb->flags = SCB_FREE;
  4560. #ifndef __linux__
  4561. error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
  4562. &next_scb->dmamap);
  4563. if (error != 0)
  4564. break;
  4565. #endif
  4566. next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
  4567. next_scb->hscb->tag = ahc->scb_data->numscbs;
  4568. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
  4569. next_scb, links.sle);
  4570. segs += AHC_NSEG;
  4571. physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
  4572. next_scb++;
  4573. ahc->scb_data->numscbs++;
  4574. }
  4575. }
  4576. void
  4577. ahc_controller_info(struct ahc_softc *ahc, char *buf)
  4578. {
  4579. int len;
  4580. len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
  4581. buf += len;
  4582. if ((ahc->features & AHC_TWIN) != 0)
  4583. len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
  4584. "B SCSI Id=%d, primary %c, ",
  4585. ahc->our_id, ahc->our_id_b,
  4586. (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
  4587. else {
  4588. const char *speed;
  4589. const char *type;
  4590. speed = "";
  4591. if ((ahc->features & AHC_ULTRA) != 0) {
  4592. speed = "Ultra ";
  4593. } else if ((ahc->features & AHC_DT) != 0) {
  4594. speed = "Ultra160 ";
  4595. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  4596. speed = "Ultra2 ";
  4597. }
  4598. if ((ahc->features & AHC_WIDE) != 0) {
  4599. type = "Wide";
  4600. } else {
  4601. type = "Single";
  4602. }
  4603. len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
  4604. speed, type, ahc->channel, ahc->our_id);
  4605. }
  4606. buf += len;
  4607. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4608. sprintf(buf, "%d/%d SCBs",
  4609. ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
  4610. else
  4611. sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
  4612. }
  4613. int
  4614. ahc_chip_init(struct ahc_softc *ahc)
  4615. {
  4616. int term;
  4617. int error;
  4618. u_int i;
  4619. u_int scsi_conf;
  4620. u_int scsiseq_template;
  4621. uint32_t physaddr;
  4622. ahc_outb(ahc, SEQ_FLAGS, 0);
  4623. ahc_outb(ahc, SEQ_FLAGS2, 0);
  4624. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
  4625. if (ahc->features & AHC_TWIN) {
  4626. /*
  4627. * Setup Channel B first.
  4628. */
  4629. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
  4630. term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
  4631. ahc_outb(ahc, SCSIID, ahc->our_id_b);
  4632. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4633. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4634. |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
  4635. if ((ahc->features & AHC_ULTRA2) != 0)
  4636. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4637. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4638. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4639. /* Select Channel A */
  4640. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
  4641. }
  4642. term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
  4643. if ((ahc->features & AHC_ULTRA2) != 0)
  4644. ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
  4645. else
  4646. ahc_outb(ahc, SCSIID, ahc->our_id);
  4647. scsi_conf = ahc_inb(ahc, SCSICONF);
  4648. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4649. |term|ahc->seltime
  4650. |ENSTIMER|ACTNEGEN);
  4651. if ((ahc->features & AHC_ULTRA2) != 0)
  4652. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4653. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4654. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4655. /* There are no untagged SCBs active yet. */
  4656. for (i = 0; i < 16; i++) {
  4657. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
  4658. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4659. int lun;
  4660. /*
  4661. * The SCB based BTT allows an entry per
  4662. * target and lun pair.
  4663. */
  4664. for (lun = 1; lun < AHC_NUM_LUNS; lun++)
  4665. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
  4666. }
  4667. }
  4668. /* All of our queues are empty */
  4669. for (i = 0; i < 256; i++)
  4670. ahc->qoutfifo[i] = SCB_LIST_NULL;
  4671. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
  4672. for (i = 0; i < 256; i++)
  4673. ahc->qinfifo[i] = SCB_LIST_NULL;
  4674. if ((ahc->features & AHC_MULTI_TID) != 0) {
  4675. ahc_outb(ahc, TARGID, 0);
  4676. ahc_outb(ahc, TARGID + 1, 0);
  4677. }
  4678. /*
  4679. * Tell the sequencer where it can find our arrays in memory.
  4680. */
  4681. physaddr = ahc->scb_data->hscb_busaddr;
  4682. ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
  4683. ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
  4684. ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
  4685. ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
  4686. physaddr = ahc->shared_data_busaddr;
  4687. ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
  4688. ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
  4689. ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
  4690. ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
  4691. /*
  4692. * Initialize the group code to command length table.
  4693. * This overrides the values in TARG_SCSIRATE, so only
  4694. * setup the table after we have processed that information.
  4695. */
  4696. ahc_outb(ahc, CMDSIZE_TABLE, 5);
  4697. ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
  4698. ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
  4699. ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
  4700. ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
  4701. ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
  4702. ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
  4703. ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
  4704. if ((ahc->features & AHC_HS_MAILBOX) != 0)
  4705. ahc_outb(ahc, HS_MAILBOX, 0);
  4706. /* Tell the sequencer of our initial queue positions */
  4707. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4708. ahc->tqinfifonext = 1;
  4709. ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
  4710. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  4711. }
  4712. ahc->qinfifonext = 0;
  4713. ahc->qoutfifonext = 0;
  4714. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4715. ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
  4716. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4717. ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
  4718. ahc_outb(ahc, SDSCB_QOFF, 0);
  4719. } else {
  4720. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4721. ahc_outb(ahc, QINPOS, ahc->qinfifonext);
  4722. ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
  4723. }
  4724. /* We don't have any waiting selections */
  4725. ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
  4726. /* Our disconnection list is empty too */
  4727. ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
  4728. /* Message out buffer starts empty */
  4729. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  4730. /*
  4731. * Setup the allowed SCSI Sequences based on operational mode.
  4732. * If we are a target, we'll enable select in operations once
  4733. * we've had a lun enabled.
  4734. */
  4735. scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
  4736. if ((ahc->flags & AHC_INITIATORROLE) != 0)
  4737. scsiseq_template |= ENRSELI;
  4738. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
  4739. /* Initialize our list of free SCBs. */
  4740. ahc_build_free_scb_list(ahc);
  4741. /*
  4742. * Tell the sequencer which SCB will be the next one it receives.
  4743. */
  4744. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4745. /*
  4746. * Load the Sequencer program and Enable the adapter
  4747. * in "fast" mode.
  4748. */
  4749. if (bootverbose)
  4750. printk("%s: Downloading Sequencer Program...",
  4751. ahc_name(ahc));
  4752. error = ahc_loadseq(ahc);
  4753. if (error != 0)
  4754. return (error);
  4755. if ((ahc->features & AHC_ULTRA2) != 0) {
  4756. int wait;
  4757. /*
  4758. * Wait for up to 500ms for our transceivers
  4759. * to settle. If the adapter does not have
  4760. * a cable attached, the transceivers may
  4761. * never settle, so don't complain if we
  4762. * fail here.
  4763. */
  4764. for (wait = 5000;
  4765. (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  4766. wait--)
  4767. ahc_delay(100);
  4768. }
  4769. ahc_restart(ahc);
  4770. return (0);
  4771. }
  4772. /*
  4773. * Start the board, ready for normal operation
  4774. */
  4775. int
  4776. ahc_init(struct ahc_softc *ahc)
  4777. {
  4778. int max_targ;
  4779. u_int i;
  4780. u_int scsi_conf;
  4781. u_int ultraenb;
  4782. u_int discenable;
  4783. u_int tagenable;
  4784. size_t driver_data_size;
  4785. #ifdef AHC_DEBUG
  4786. if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
  4787. ahc->flags |= AHC_SEQUENCER_DEBUG;
  4788. #endif
  4789. #ifdef AHC_PRINT_SRAM
  4790. printk("Scratch Ram:");
  4791. for (i = 0x20; i < 0x5f; i++) {
  4792. if (((i % 8) == 0) && (i != 0)) {
  4793. printk ("\n ");
  4794. }
  4795. printk (" 0x%x", ahc_inb(ahc, i));
  4796. }
  4797. if ((ahc->features & AHC_MORE_SRAM) != 0) {
  4798. for (i = 0x70; i < 0x7f; i++) {
  4799. if (((i % 8) == 0) && (i != 0)) {
  4800. printk ("\n ");
  4801. }
  4802. printk (" 0x%x", ahc_inb(ahc, i));
  4803. }
  4804. }
  4805. printk ("\n");
  4806. /*
  4807. * Reading uninitialized scratch ram may
  4808. * generate parity errors.
  4809. */
  4810. ahc_outb(ahc, CLRINT, CLRPARERR);
  4811. ahc_outb(ahc, CLRINT, CLRBRKADRINT);
  4812. #endif
  4813. max_targ = 15;
  4814. /*
  4815. * Assume we have a board at this stage and it has been reset.
  4816. */
  4817. if ((ahc->flags & AHC_USEDEFAULTS) != 0)
  4818. ahc->our_id = ahc->our_id_b = 7;
  4819. /*
  4820. * Default to allowing initiator operations.
  4821. */
  4822. ahc->flags |= AHC_INITIATORROLE;
  4823. /*
  4824. * Only allow target mode features if this unit has them enabled.
  4825. */
  4826. if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
  4827. ahc->features &= ~AHC_TARGETMODE;
  4828. #ifndef __linux__
  4829. /* DMA tag for mapping buffers into device visible space. */
  4830. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4831. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4832. /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
  4833. ? (dma_addr_t)0x7FFFFFFFFFULL
  4834. : BUS_SPACE_MAXADDR_32BIT,
  4835. /*highaddr*/BUS_SPACE_MAXADDR,
  4836. /*filter*/NULL, /*filterarg*/NULL,
  4837. /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
  4838. /*nsegments*/AHC_NSEG,
  4839. /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
  4840. /*flags*/BUS_DMA_ALLOCNOW,
  4841. &ahc->buffer_dmat) != 0) {
  4842. return (ENOMEM);
  4843. }
  4844. #endif
  4845. ahc->init_level++;
  4846. /*
  4847. * DMA tag for our command fifos and other data in system memory
  4848. * the card's sequencer must be able to access. For initiator
  4849. * roles, we need to allocate space for the qinfifo and qoutfifo.
  4850. * The qinfifo and qoutfifo are composed of 256 1 byte elements.
  4851. * When providing for the target mode role, we must additionally
  4852. * provide space for the incoming target command fifo and an extra
  4853. * byte to deal with a dma bug in some chip versions.
  4854. */
  4855. driver_data_size = 2 * 256 * sizeof(uint8_t);
  4856. if ((ahc->features & AHC_TARGETMODE) != 0)
  4857. driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
  4858. + /*DMA WideOdd Bug Buffer*/1;
  4859. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4860. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4861. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4862. /*highaddr*/BUS_SPACE_MAXADDR,
  4863. /*filter*/NULL, /*filterarg*/NULL,
  4864. driver_data_size,
  4865. /*nsegments*/1,
  4866. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4867. /*flags*/0, &ahc->shared_data_dmat) != 0) {
  4868. return (ENOMEM);
  4869. }
  4870. ahc->init_level++;
  4871. /* Allocation of driver data */
  4872. if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
  4873. (void **)&ahc->qoutfifo,
  4874. BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
  4875. return (ENOMEM);
  4876. }
  4877. ahc->init_level++;
  4878. /* And permanently map it in */
  4879. ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  4880. ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
  4881. &ahc->shared_data_busaddr, /*flags*/0);
  4882. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4883. ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
  4884. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
  4885. ahc->dma_bug_buf = ahc->shared_data_busaddr
  4886. + driver_data_size - 1;
  4887. /* All target command blocks start out invalid. */
  4888. for (i = 0; i < AHC_TMODE_CMDS; i++)
  4889. ahc->targetcmds[i].cmd_valid = 0;
  4890. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
  4891. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
  4892. }
  4893. ahc->qinfifo = &ahc->qoutfifo[256];
  4894. ahc->init_level++;
  4895. /* Allocate SCB data now that buffer_dmat is initialized */
  4896. if (ahc->scb_data->maxhscbs == 0)
  4897. if (ahc_init_scbdata(ahc) != 0)
  4898. return (ENOMEM);
  4899. /*
  4900. * Allocate a tstate to house information for our
  4901. * initiator presence on the bus as well as the user
  4902. * data for any target mode initiator.
  4903. */
  4904. if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
  4905. printk("%s: unable to allocate ahc_tmode_tstate. "
  4906. "Failing attach\n", ahc_name(ahc));
  4907. return (ENOMEM);
  4908. }
  4909. if ((ahc->features & AHC_TWIN) != 0) {
  4910. if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
  4911. printk("%s: unable to allocate ahc_tmode_tstate. "
  4912. "Failing attach\n", ahc_name(ahc));
  4913. return (ENOMEM);
  4914. }
  4915. }
  4916. if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
  4917. ahc->flags |= AHC_PAGESCBS;
  4918. } else {
  4919. ahc->flags &= ~AHC_PAGESCBS;
  4920. }
  4921. #ifdef AHC_DEBUG
  4922. if (ahc_debug & AHC_SHOW_MISC) {
  4923. printk("%s: hardware scb %u bytes; kernel scb %u bytes; "
  4924. "ahc_dma %u bytes\n",
  4925. ahc_name(ahc),
  4926. (u_int)sizeof(struct hardware_scb),
  4927. (u_int)sizeof(struct scb),
  4928. (u_int)sizeof(struct ahc_dma_seg));
  4929. }
  4930. #endif /* AHC_DEBUG */
  4931. /*
  4932. * Look at the information that board initialization or
  4933. * the board bios has left us.
  4934. */
  4935. if (ahc->features & AHC_TWIN) {
  4936. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4937. if ((scsi_conf & RESET_SCSI) != 0
  4938. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4939. ahc->flags |= AHC_RESET_BUS_B;
  4940. }
  4941. scsi_conf = ahc_inb(ahc, SCSICONF);
  4942. if ((scsi_conf & RESET_SCSI) != 0
  4943. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4944. ahc->flags |= AHC_RESET_BUS_A;
  4945. ultraenb = 0;
  4946. tagenable = ALL_TARGETS_MASK;
  4947. /* Grab the disconnection disable table and invert it for our needs */
  4948. if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
  4949. printk("%s: Host Adapter Bios disabled. Using default SCSI "
  4950. "device parameters\n", ahc_name(ahc));
  4951. ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
  4952. AHC_TERM_ENB_A|AHC_TERM_ENB_B;
  4953. discenable = ALL_TARGETS_MASK;
  4954. if ((ahc->features & AHC_ULTRA) != 0)
  4955. ultraenb = ALL_TARGETS_MASK;
  4956. } else {
  4957. discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
  4958. | ahc_inb(ahc, DISC_DSB));
  4959. if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
  4960. ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
  4961. | ahc_inb(ahc, ULTRA_ENB);
  4962. }
  4963. if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
  4964. max_targ = 7;
  4965. for (i = 0; i <= max_targ; i++) {
  4966. struct ahc_initiator_tinfo *tinfo;
  4967. struct ahc_tmode_tstate *tstate;
  4968. u_int our_id;
  4969. u_int target_id;
  4970. char channel;
  4971. channel = 'A';
  4972. our_id = ahc->our_id;
  4973. target_id = i;
  4974. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  4975. channel = 'B';
  4976. our_id = ahc->our_id_b;
  4977. target_id = i % 8;
  4978. }
  4979. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  4980. target_id, &tstate);
  4981. /* Default to async narrow across the board */
  4982. memset(tinfo, 0, sizeof(*tinfo));
  4983. if (ahc->flags & AHC_USEDEFAULTS) {
  4984. if ((ahc->features & AHC_WIDE) != 0)
  4985. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4986. /*
  4987. * These will be truncated when we determine the
  4988. * connection type we have with the target.
  4989. */
  4990. tinfo->user.period = ahc_syncrates->period;
  4991. tinfo->user.offset = MAX_OFFSET;
  4992. } else {
  4993. u_int scsirate;
  4994. uint16_t mask;
  4995. /* Take the settings leftover in scratch RAM. */
  4996. scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
  4997. mask = (0x01 << i);
  4998. if ((ahc->features & AHC_ULTRA2) != 0) {
  4999. u_int offset;
  5000. u_int maxsync;
  5001. if ((scsirate & SOFS) == 0x0F) {
  5002. /*
  5003. * Haven't negotiated yet,
  5004. * so the format is different.
  5005. */
  5006. scsirate = (scsirate & SXFR) >> 4
  5007. | (ultraenb & mask)
  5008. ? 0x08 : 0x0
  5009. | (scsirate & WIDEXFER);
  5010. offset = MAX_OFFSET_ULTRA2;
  5011. } else
  5012. offset = ahc_inb(ahc, TARG_OFFSET + i);
  5013. if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
  5014. /* Set to the lowest sync rate, 5MHz */
  5015. scsirate |= 0x1c;
  5016. maxsync = AHC_SYNCRATE_ULTRA2;
  5017. if ((ahc->features & AHC_DT) != 0)
  5018. maxsync = AHC_SYNCRATE_DT;
  5019. tinfo->user.period =
  5020. ahc_find_period(ahc, scsirate, maxsync);
  5021. if (offset == 0)
  5022. tinfo->user.period = 0;
  5023. else
  5024. tinfo->user.offset = MAX_OFFSET;
  5025. if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
  5026. && (ahc->features & AHC_DT) != 0)
  5027. tinfo->user.ppr_options =
  5028. MSG_EXT_PPR_DT_REQ;
  5029. } else if ((scsirate & SOFS) != 0) {
  5030. if ((scsirate & SXFR) == 0x40
  5031. && (ultraenb & mask) != 0) {
  5032. /* Treat 10MHz as a non-ultra speed */
  5033. scsirate &= ~SXFR;
  5034. ultraenb &= ~mask;
  5035. }
  5036. tinfo->user.period =
  5037. ahc_find_period(ahc, scsirate,
  5038. (ultraenb & mask)
  5039. ? AHC_SYNCRATE_ULTRA
  5040. : AHC_SYNCRATE_FAST);
  5041. if (tinfo->user.period != 0)
  5042. tinfo->user.offset = MAX_OFFSET;
  5043. }
  5044. if (tinfo->user.period == 0)
  5045. tinfo->user.offset = 0;
  5046. if ((scsirate & WIDEXFER) != 0
  5047. && (ahc->features & AHC_WIDE) != 0)
  5048. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  5049. tinfo->user.protocol_version = 4;
  5050. if ((ahc->features & AHC_DT) != 0)
  5051. tinfo->user.transport_version = 3;
  5052. else
  5053. tinfo->user.transport_version = 2;
  5054. tinfo->goal.protocol_version = 2;
  5055. tinfo->goal.transport_version = 2;
  5056. tinfo->curr.protocol_version = 2;
  5057. tinfo->curr.transport_version = 2;
  5058. }
  5059. tstate->ultraenb = 0;
  5060. }
  5061. ahc->user_discenable = discenable;
  5062. ahc->user_tagenable = tagenable;
  5063. return (ahc->bus_chip_init(ahc));
  5064. }
  5065. void
  5066. ahc_intr_enable(struct ahc_softc *ahc, int enable)
  5067. {
  5068. u_int hcntrl;
  5069. hcntrl = ahc_inb(ahc, HCNTRL);
  5070. hcntrl &= ~INTEN;
  5071. ahc->pause &= ~INTEN;
  5072. ahc->unpause &= ~INTEN;
  5073. if (enable) {
  5074. hcntrl |= INTEN;
  5075. ahc->pause |= INTEN;
  5076. ahc->unpause |= INTEN;
  5077. }
  5078. ahc_outb(ahc, HCNTRL, hcntrl);
  5079. }
  5080. /*
  5081. * Ensure that the card is paused in a location
  5082. * outside of all critical sections and that all
  5083. * pending work is completed prior to returning.
  5084. * This routine should only be called from outside
  5085. * an interrupt context.
  5086. */
  5087. void
  5088. ahc_pause_and_flushwork(struct ahc_softc *ahc)
  5089. {
  5090. int intstat;
  5091. int maxloops;
  5092. int paused;
  5093. maxloops = 1000;
  5094. ahc->flags |= AHC_ALL_INTERRUPTS;
  5095. paused = FALSE;
  5096. do {
  5097. if (paused) {
  5098. ahc_unpause(ahc);
  5099. /*
  5100. * Give the sequencer some time to service
  5101. * any active selections.
  5102. */
  5103. ahc_delay(500);
  5104. }
  5105. ahc_intr(ahc);
  5106. ahc_pause(ahc);
  5107. paused = TRUE;
  5108. ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  5109. intstat = ahc_inb(ahc, INTSTAT);
  5110. if ((intstat & INT_PEND) == 0) {
  5111. ahc_clear_critical_section(ahc);
  5112. intstat = ahc_inb(ahc, INTSTAT);
  5113. }
  5114. } while (--maxloops
  5115. && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
  5116. && ((intstat & INT_PEND) != 0
  5117. || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
  5118. if (maxloops == 0) {
  5119. printk("Infinite interrupt loop, INTSTAT = %x",
  5120. ahc_inb(ahc, INTSTAT));
  5121. }
  5122. ahc_platform_flushwork(ahc);
  5123. ahc->flags &= ~AHC_ALL_INTERRUPTS;
  5124. }
  5125. #ifdef CONFIG_PM
  5126. int
  5127. ahc_suspend(struct ahc_softc *ahc)
  5128. {
  5129. ahc_pause_and_flushwork(ahc);
  5130. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  5131. ahc_unpause(ahc);
  5132. return (EBUSY);
  5133. }
  5134. #ifdef AHC_TARGET_MODE
  5135. /*
  5136. * XXX What about ATIOs that have not yet been serviced?
  5137. * Perhaps we should just refuse to be suspended if we
  5138. * are acting in a target role.
  5139. */
  5140. if (ahc->pending_device != NULL) {
  5141. ahc_unpause(ahc);
  5142. return (EBUSY);
  5143. }
  5144. #endif
  5145. ahc_shutdown(ahc);
  5146. return (0);
  5147. }
  5148. int
  5149. ahc_resume(struct ahc_softc *ahc)
  5150. {
  5151. ahc_reset(ahc, /*reinit*/TRUE);
  5152. ahc_intr_enable(ahc, TRUE);
  5153. ahc_restart(ahc);
  5154. return (0);
  5155. }
  5156. #endif
  5157. /************************** Busy Target Table *********************************/
  5158. /*
  5159. * Return the untagged transaction id for a given target/channel lun.
  5160. * Optionally, clear the entry.
  5161. */
  5162. static u_int
  5163. ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
  5164. {
  5165. u_int scbid;
  5166. u_int target_offset;
  5167. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5168. u_int saved_scbptr;
  5169. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5170. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5171. scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
  5172. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5173. } else {
  5174. target_offset = TCL_TARGET_OFFSET(tcl);
  5175. scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
  5176. }
  5177. return (scbid);
  5178. }
  5179. static void
  5180. ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
  5181. {
  5182. u_int target_offset;
  5183. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5184. u_int saved_scbptr;
  5185. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5186. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5187. ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
  5188. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5189. } else {
  5190. target_offset = TCL_TARGET_OFFSET(tcl);
  5191. ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
  5192. }
  5193. }
  5194. static void
  5195. ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
  5196. {
  5197. u_int target_offset;
  5198. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5199. u_int saved_scbptr;
  5200. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5201. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5202. ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
  5203. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5204. } else {
  5205. target_offset = TCL_TARGET_OFFSET(tcl);
  5206. ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
  5207. }
  5208. }
  5209. /************************** SCB and SCB queue management **********************/
  5210. int
  5211. ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
  5212. char channel, int lun, u_int tag, role_t role)
  5213. {
  5214. int targ = SCB_GET_TARGET(ahc, scb);
  5215. char chan = SCB_GET_CHANNEL(ahc, scb);
  5216. int slun = SCB_GET_LUN(scb);
  5217. int match;
  5218. match = ((chan == channel) || (channel == ALL_CHANNELS));
  5219. if (match != 0)
  5220. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  5221. if (match != 0)
  5222. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  5223. if (match != 0) {
  5224. #ifdef AHC_TARGET_MODE
  5225. int group;
  5226. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  5227. if (role == ROLE_INITIATOR) {
  5228. match = (group != XPT_FC_GROUP_TMODE)
  5229. && ((tag == scb->hscb->tag)
  5230. || (tag == SCB_LIST_NULL));
  5231. } else if (role == ROLE_TARGET) {
  5232. match = (group == XPT_FC_GROUP_TMODE)
  5233. && ((tag == scb->io_ctx->csio.tag_id)
  5234. || (tag == SCB_LIST_NULL));
  5235. }
  5236. #else /* !AHC_TARGET_MODE */
  5237. match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
  5238. #endif /* AHC_TARGET_MODE */
  5239. }
  5240. return match;
  5241. }
  5242. static void
  5243. ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  5244. {
  5245. int target;
  5246. char channel;
  5247. int lun;
  5248. target = SCB_GET_TARGET(ahc, scb);
  5249. lun = SCB_GET_LUN(scb);
  5250. channel = SCB_GET_CHANNEL(ahc, scb);
  5251. ahc_search_qinfifo(ahc, target, channel, lun,
  5252. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  5253. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5254. ahc_platform_freeze_devq(ahc, scb);
  5255. }
  5256. void
  5257. ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
  5258. {
  5259. struct scb *prev_scb;
  5260. prev_scb = NULL;
  5261. if (ahc_qinfifo_count(ahc) != 0) {
  5262. u_int prev_tag;
  5263. uint8_t prev_pos;
  5264. prev_pos = ahc->qinfifonext - 1;
  5265. prev_tag = ahc->qinfifo[prev_pos];
  5266. prev_scb = ahc_lookup_scb(ahc, prev_tag);
  5267. }
  5268. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5269. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5270. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  5271. } else {
  5272. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  5273. }
  5274. }
  5275. static void
  5276. ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
  5277. struct scb *scb)
  5278. {
  5279. if (prev_scb == NULL) {
  5280. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  5281. } else {
  5282. prev_scb->hscb->next = scb->hscb->tag;
  5283. ahc_sync_scb(ahc, prev_scb,
  5284. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  5285. }
  5286. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  5287. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  5288. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  5289. }
  5290. static int
  5291. ahc_qinfifo_count(struct ahc_softc *ahc)
  5292. {
  5293. uint8_t qinpos;
  5294. uint8_t diff;
  5295. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5296. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  5297. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  5298. } else
  5299. qinpos = ahc_inb(ahc, QINPOS);
  5300. diff = ahc->qinfifonext - qinpos;
  5301. return (diff);
  5302. }
  5303. int
  5304. ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
  5305. int lun, u_int tag, role_t role, uint32_t status,
  5306. ahc_search_action action)
  5307. {
  5308. struct scb *scb;
  5309. struct scb *prev_scb;
  5310. uint8_t qinstart;
  5311. uint8_t qinpos;
  5312. uint8_t qintail;
  5313. uint8_t next;
  5314. uint8_t prev;
  5315. uint8_t curscbptr;
  5316. int found;
  5317. int have_qregs;
  5318. qintail = ahc->qinfifonext;
  5319. have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
  5320. if (have_qregs) {
  5321. qinstart = ahc_inb(ahc, SNSCB_QOFF);
  5322. ahc_outb(ahc, SNSCB_QOFF, qinstart);
  5323. } else
  5324. qinstart = ahc_inb(ahc, QINPOS);
  5325. qinpos = qinstart;
  5326. found = 0;
  5327. prev_scb = NULL;
  5328. if (action == SEARCH_COMPLETE) {
  5329. /*
  5330. * Don't attempt to run any queued untagged transactions
  5331. * until we are done with the abort process.
  5332. */
  5333. ahc_freeze_untagged_queues(ahc);
  5334. }
  5335. /*
  5336. * Start with an empty queue. Entries that are not chosen
  5337. * for removal will be re-added to the queue as we go.
  5338. */
  5339. ahc->qinfifonext = qinpos;
  5340. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  5341. while (qinpos != qintail) {
  5342. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
  5343. if (scb == NULL) {
  5344. printk("qinpos = %d, SCB index = %d\n",
  5345. qinpos, ahc->qinfifo[qinpos]);
  5346. panic("Loop 1\n");
  5347. }
  5348. if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
  5349. /*
  5350. * We found an scb that needs to be acted on.
  5351. */
  5352. found++;
  5353. switch (action) {
  5354. case SEARCH_COMPLETE:
  5355. {
  5356. cam_status ostat;
  5357. cam_status cstat;
  5358. ostat = ahc_get_transaction_status(scb);
  5359. if (ostat == CAM_REQ_INPROG)
  5360. ahc_set_transaction_status(scb, status);
  5361. cstat = ahc_get_transaction_status(scb);
  5362. if (cstat != CAM_REQ_CMP)
  5363. ahc_freeze_scb(scb);
  5364. if ((scb->flags & SCB_ACTIVE) == 0)
  5365. printk("Inactive SCB in qinfifo\n");
  5366. ahc_done(ahc, scb);
  5367. /* FALLTHROUGH */
  5368. }
  5369. case SEARCH_REMOVE:
  5370. break;
  5371. case SEARCH_COUNT:
  5372. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5373. prev_scb = scb;
  5374. break;
  5375. }
  5376. } else {
  5377. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5378. prev_scb = scb;
  5379. }
  5380. qinpos++;
  5381. }
  5382. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5383. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  5384. } else {
  5385. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  5386. }
  5387. if (action != SEARCH_COUNT
  5388. && (found != 0)
  5389. && (qinstart != ahc->qinfifonext)) {
  5390. /*
  5391. * The sequencer may be in the process of dmaing
  5392. * down the SCB at the beginning of the queue.
  5393. * This could be problematic if either the first,
  5394. * or the second SCB is removed from the queue
  5395. * (the first SCB includes a pointer to the "next"
  5396. * SCB to dma). If we have removed any entries, swap
  5397. * the first element in the queue with the next HSCB
  5398. * so the sequencer will notice that NEXT_QUEUED_SCB
  5399. * has changed during its dma attempt and will retry
  5400. * the DMA.
  5401. */
  5402. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
  5403. if (scb == NULL) {
  5404. printk("found = %d, qinstart = %d, qinfifionext = %d\n",
  5405. found, qinstart, ahc->qinfifonext);
  5406. panic("First/Second Qinfifo fixup\n");
  5407. }
  5408. /*
  5409. * ahc_swap_with_next_hscb forces our next pointer to
  5410. * point to the reserved SCB for future commands. Save
  5411. * and restore our original next pointer to maintain
  5412. * queue integrity.
  5413. */
  5414. next = scb->hscb->next;
  5415. ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
  5416. ahc_swap_with_next_hscb(ahc, scb);
  5417. scb->hscb->next = next;
  5418. ahc->qinfifo[qinstart] = scb->hscb->tag;
  5419. /* Tell the card about the new head of the qinfifo. */
  5420. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  5421. /* Fixup the tail "next" pointer. */
  5422. qintail = ahc->qinfifonext - 1;
  5423. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
  5424. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  5425. }
  5426. /*
  5427. * Search waiting for selection list.
  5428. */
  5429. curscbptr = ahc_inb(ahc, SCBPTR);
  5430. next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
  5431. prev = SCB_LIST_NULL;
  5432. while (next != SCB_LIST_NULL) {
  5433. uint8_t scb_index;
  5434. ahc_outb(ahc, SCBPTR, next);
  5435. scb_index = ahc_inb(ahc, SCB_TAG);
  5436. if (scb_index >= ahc->scb_data->numscbs) {
  5437. printk("Waiting List inconsistency. "
  5438. "SCB index == %d, yet numscbs == %d.",
  5439. scb_index, ahc->scb_data->numscbs);
  5440. ahc_dump_card_state(ahc);
  5441. panic("for safety");
  5442. }
  5443. scb = ahc_lookup_scb(ahc, scb_index);
  5444. if (scb == NULL) {
  5445. printk("scb_index = %d, next = %d\n",
  5446. scb_index, next);
  5447. panic("Waiting List traversal\n");
  5448. }
  5449. if (ahc_match_scb(ahc, scb, target, channel,
  5450. lun, SCB_LIST_NULL, role)) {
  5451. /*
  5452. * We found an scb that needs to be acted on.
  5453. */
  5454. found++;
  5455. switch (action) {
  5456. case SEARCH_COMPLETE:
  5457. {
  5458. cam_status ostat;
  5459. cam_status cstat;
  5460. ostat = ahc_get_transaction_status(scb);
  5461. if (ostat == CAM_REQ_INPROG)
  5462. ahc_set_transaction_status(scb,
  5463. status);
  5464. cstat = ahc_get_transaction_status(scb);
  5465. if (cstat != CAM_REQ_CMP)
  5466. ahc_freeze_scb(scb);
  5467. if ((scb->flags & SCB_ACTIVE) == 0)
  5468. printk("Inactive SCB in Waiting List\n");
  5469. ahc_done(ahc, scb);
  5470. /* FALLTHROUGH */
  5471. }
  5472. case SEARCH_REMOVE:
  5473. next = ahc_rem_wscb(ahc, next, prev);
  5474. break;
  5475. case SEARCH_COUNT:
  5476. prev = next;
  5477. next = ahc_inb(ahc, SCB_NEXT);
  5478. break;
  5479. }
  5480. } else {
  5481. prev = next;
  5482. next = ahc_inb(ahc, SCB_NEXT);
  5483. }
  5484. }
  5485. ahc_outb(ahc, SCBPTR, curscbptr);
  5486. found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
  5487. channel, lun, status, action);
  5488. if (action == SEARCH_COMPLETE)
  5489. ahc_release_untagged_queues(ahc);
  5490. return (found);
  5491. }
  5492. int
  5493. ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
  5494. int target, char channel, int lun, uint32_t status,
  5495. ahc_search_action action)
  5496. {
  5497. struct scb *scb;
  5498. int maxtarget;
  5499. int found;
  5500. int i;
  5501. if (action == SEARCH_COMPLETE) {
  5502. /*
  5503. * Don't attempt to run any queued untagged transactions
  5504. * until we are done with the abort process.
  5505. */
  5506. ahc_freeze_untagged_queues(ahc);
  5507. }
  5508. found = 0;
  5509. i = 0;
  5510. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  5511. maxtarget = 16;
  5512. if (target != CAM_TARGET_WILDCARD) {
  5513. i = target;
  5514. if (channel == 'B')
  5515. i += 8;
  5516. maxtarget = i + 1;
  5517. }
  5518. } else {
  5519. maxtarget = 0;
  5520. }
  5521. for (; i < maxtarget; i++) {
  5522. struct scb_tailq *untagged_q;
  5523. struct scb *next_scb;
  5524. untagged_q = &(ahc->untagged_queues[i]);
  5525. next_scb = TAILQ_FIRST(untagged_q);
  5526. while (next_scb != NULL) {
  5527. scb = next_scb;
  5528. next_scb = TAILQ_NEXT(scb, links.tqe);
  5529. /*
  5530. * The head of the list may be the currently
  5531. * active untagged command for a device.
  5532. * We're only searching for commands that
  5533. * have not been started. A transaction
  5534. * marked active but still in the qinfifo
  5535. * is removed by the qinfifo scanning code
  5536. * above.
  5537. */
  5538. if ((scb->flags & SCB_ACTIVE) != 0)
  5539. continue;
  5540. if (ahc_match_scb(ahc, scb, target, channel, lun,
  5541. SCB_LIST_NULL, ROLE_INITIATOR) == 0
  5542. || (ctx != NULL && ctx != scb->io_ctx))
  5543. continue;
  5544. /*
  5545. * We found an scb that needs to be acted on.
  5546. */
  5547. found++;
  5548. switch (action) {
  5549. case SEARCH_COMPLETE:
  5550. {
  5551. cam_status ostat;
  5552. cam_status cstat;
  5553. ostat = ahc_get_transaction_status(scb);
  5554. if (ostat == CAM_REQ_INPROG)
  5555. ahc_set_transaction_status(scb, status);
  5556. cstat = ahc_get_transaction_status(scb);
  5557. if (cstat != CAM_REQ_CMP)
  5558. ahc_freeze_scb(scb);
  5559. if ((scb->flags & SCB_ACTIVE) == 0)
  5560. printk("Inactive SCB in untaggedQ\n");
  5561. ahc_done(ahc, scb);
  5562. break;
  5563. }
  5564. case SEARCH_REMOVE:
  5565. scb->flags &= ~SCB_UNTAGGEDQ;
  5566. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  5567. break;
  5568. case SEARCH_COUNT:
  5569. break;
  5570. }
  5571. }
  5572. }
  5573. if (action == SEARCH_COMPLETE)
  5574. ahc_release_untagged_queues(ahc);
  5575. return (found);
  5576. }
  5577. int
  5578. ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
  5579. int lun, u_int tag, int stop_on_first, int remove,
  5580. int save_state)
  5581. {
  5582. struct scb *scbp;
  5583. u_int next;
  5584. u_int prev;
  5585. u_int count;
  5586. u_int active_scb;
  5587. count = 0;
  5588. next = ahc_inb(ahc, DISCONNECTED_SCBH);
  5589. prev = SCB_LIST_NULL;
  5590. if (save_state) {
  5591. /* restore this when we're done */
  5592. active_scb = ahc_inb(ahc, SCBPTR);
  5593. } else
  5594. /* Silence compiler */
  5595. active_scb = SCB_LIST_NULL;
  5596. while (next != SCB_LIST_NULL) {
  5597. u_int scb_index;
  5598. ahc_outb(ahc, SCBPTR, next);
  5599. scb_index = ahc_inb(ahc, SCB_TAG);
  5600. if (scb_index >= ahc->scb_data->numscbs) {
  5601. printk("Disconnected List inconsistency. "
  5602. "SCB index == %d, yet numscbs == %d.",
  5603. scb_index, ahc->scb_data->numscbs);
  5604. ahc_dump_card_state(ahc);
  5605. panic("for safety");
  5606. }
  5607. if (next == prev) {
  5608. panic("Disconnected List Loop. "
  5609. "cur SCBPTR == %x, prev SCBPTR == %x.",
  5610. next, prev);
  5611. }
  5612. scbp = ahc_lookup_scb(ahc, scb_index);
  5613. if (ahc_match_scb(ahc, scbp, target, channel, lun,
  5614. tag, ROLE_INITIATOR)) {
  5615. count++;
  5616. if (remove) {
  5617. next =
  5618. ahc_rem_scb_from_disc_list(ahc, prev, next);
  5619. } else {
  5620. prev = next;
  5621. next = ahc_inb(ahc, SCB_NEXT);
  5622. }
  5623. if (stop_on_first)
  5624. break;
  5625. } else {
  5626. prev = next;
  5627. next = ahc_inb(ahc, SCB_NEXT);
  5628. }
  5629. }
  5630. if (save_state)
  5631. ahc_outb(ahc, SCBPTR, active_scb);
  5632. return (count);
  5633. }
  5634. /*
  5635. * Remove an SCB from the on chip list of disconnected transactions.
  5636. * This is empty/unused if we are not performing SCB paging.
  5637. */
  5638. static u_int
  5639. ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
  5640. {
  5641. u_int next;
  5642. ahc_outb(ahc, SCBPTR, scbptr);
  5643. next = ahc_inb(ahc, SCB_NEXT);
  5644. ahc_outb(ahc, SCB_CONTROL, 0);
  5645. ahc_add_curscb_to_free_list(ahc);
  5646. if (prev != SCB_LIST_NULL) {
  5647. ahc_outb(ahc, SCBPTR, prev);
  5648. ahc_outb(ahc, SCB_NEXT, next);
  5649. } else
  5650. ahc_outb(ahc, DISCONNECTED_SCBH, next);
  5651. return (next);
  5652. }
  5653. /*
  5654. * Add the SCB as selected by SCBPTR onto the on chip list of
  5655. * free hardware SCBs. This list is empty/unused if we are not
  5656. * performing SCB paging.
  5657. */
  5658. static void
  5659. ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
  5660. {
  5661. /*
  5662. * Invalidate the tag so that our abort
  5663. * routines don't think it's active.
  5664. */
  5665. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  5666. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  5667. ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
  5668. ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
  5669. }
  5670. }
  5671. /*
  5672. * Manipulate the waiting for selection list and return the
  5673. * scb that follows the one that we remove.
  5674. */
  5675. static u_int
  5676. ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
  5677. {
  5678. u_int curscb, next;
  5679. /*
  5680. * Select the SCB we want to abort and
  5681. * pull the next pointer out of it.
  5682. */
  5683. curscb = ahc_inb(ahc, SCBPTR);
  5684. ahc_outb(ahc, SCBPTR, scbpos);
  5685. next = ahc_inb(ahc, SCB_NEXT);
  5686. /* Clear the necessary fields */
  5687. ahc_outb(ahc, SCB_CONTROL, 0);
  5688. ahc_add_curscb_to_free_list(ahc);
  5689. /* update the waiting list */
  5690. if (prev == SCB_LIST_NULL) {
  5691. /* First in the list */
  5692. ahc_outb(ahc, WAITING_SCBH, next);
  5693. /*
  5694. * Ensure we aren't attempting to perform
  5695. * selection for this entry.
  5696. */
  5697. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  5698. } else {
  5699. /*
  5700. * Select the scb that pointed to us
  5701. * and update its next pointer.
  5702. */
  5703. ahc_outb(ahc, SCBPTR, prev);
  5704. ahc_outb(ahc, SCB_NEXT, next);
  5705. }
  5706. /*
  5707. * Point us back at the original scb position.
  5708. */
  5709. ahc_outb(ahc, SCBPTR, curscb);
  5710. return next;
  5711. }
  5712. /******************************** Error Handling ******************************/
  5713. /*
  5714. * Abort all SCBs that match the given description (target/channel/lun/tag),
  5715. * setting their status to the passed in status if the status has not already
  5716. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  5717. * is paused before it is called.
  5718. */
  5719. static int
  5720. ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  5721. int lun, u_int tag, role_t role, uint32_t status)
  5722. {
  5723. struct scb *scbp;
  5724. struct scb *scbp_next;
  5725. u_int active_scb;
  5726. int i, j;
  5727. int maxtarget;
  5728. int minlun;
  5729. int maxlun;
  5730. int found;
  5731. /*
  5732. * Don't attempt to run any queued untagged transactions
  5733. * until we are done with the abort process.
  5734. */
  5735. ahc_freeze_untagged_queues(ahc);
  5736. /* restore this when we're done */
  5737. active_scb = ahc_inb(ahc, SCBPTR);
  5738. found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
  5739. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5740. /*
  5741. * Clean out the busy target table for any untagged commands.
  5742. */
  5743. i = 0;
  5744. maxtarget = 16;
  5745. if (target != CAM_TARGET_WILDCARD) {
  5746. i = target;
  5747. if (channel == 'B')
  5748. i += 8;
  5749. maxtarget = i + 1;
  5750. }
  5751. if (lun == CAM_LUN_WILDCARD) {
  5752. /*
  5753. * Unless we are using an SCB based
  5754. * busy targets table, there is only
  5755. * one table entry for all luns of
  5756. * a target.
  5757. */
  5758. minlun = 0;
  5759. maxlun = 1;
  5760. if ((ahc->flags & AHC_SCB_BTT) != 0)
  5761. maxlun = AHC_NUM_LUNS;
  5762. } else {
  5763. minlun = lun;
  5764. maxlun = lun + 1;
  5765. }
  5766. if (role != ROLE_TARGET) {
  5767. for (;i < maxtarget; i++) {
  5768. for (j = minlun;j < maxlun; j++) {
  5769. u_int scbid;
  5770. u_int tcl;
  5771. tcl = BUILD_TCL(i << 4, j);
  5772. scbid = ahc_index_busy_tcl(ahc, tcl);
  5773. scbp = ahc_lookup_scb(ahc, scbid);
  5774. if (scbp == NULL
  5775. || ahc_match_scb(ahc, scbp, target, channel,
  5776. lun, tag, role) == 0)
  5777. continue;
  5778. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
  5779. }
  5780. }
  5781. /*
  5782. * Go through the disconnected list and remove any entries we
  5783. * have queued for completion, 0'ing their control byte too.
  5784. * We save the active SCB and restore it ourselves, so there
  5785. * is no reason for this search to restore it too.
  5786. */
  5787. ahc_search_disc_list(ahc, target, channel, lun, tag,
  5788. /*stop_on_first*/FALSE, /*remove*/TRUE,
  5789. /*save_state*/FALSE);
  5790. }
  5791. /*
  5792. * Go through the hardware SCB array looking for commands that
  5793. * were active but not on any list. In some cases, these remnants
  5794. * might not still have mappings in the scbindex array (e.g. unexpected
  5795. * bus free with the same scb queued for an abort). Don't hold this
  5796. * against them.
  5797. */
  5798. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  5799. u_int scbid;
  5800. ahc_outb(ahc, SCBPTR, i);
  5801. scbid = ahc_inb(ahc, SCB_TAG);
  5802. scbp = ahc_lookup_scb(ahc, scbid);
  5803. if ((scbp == NULL && scbid != SCB_LIST_NULL)
  5804. || (scbp != NULL
  5805. && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
  5806. ahc_add_curscb_to_free_list(ahc);
  5807. }
  5808. /*
  5809. * Go through the pending CCB list and look for
  5810. * commands for this target that are still active.
  5811. * These are other tagged commands that were
  5812. * disconnected when the reset occurred.
  5813. */
  5814. scbp_next = LIST_FIRST(&ahc->pending_scbs);
  5815. while (scbp_next != NULL) {
  5816. scbp = scbp_next;
  5817. scbp_next = LIST_NEXT(scbp, pending_links);
  5818. if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
  5819. cam_status ostat;
  5820. ostat = ahc_get_transaction_status(scbp);
  5821. if (ostat == CAM_REQ_INPROG)
  5822. ahc_set_transaction_status(scbp, status);
  5823. if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
  5824. ahc_freeze_scb(scbp);
  5825. if ((scbp->flags & SCB_ACTIVE) == 0)
  5826. printk("Inactive SCB on pending list\n");
  5827. ahc_done(ahc, scbp);
  5828. found++;
  5829. }
  5830. }
  5831. ahc_outb(ahc, SCBPTR, active_scb);
  5832. ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
  5833. ahc_release_untagged_queues(ahc);
  5834. return found;
  5835. }
  5836. static void
  5837. ahc_reset_current_bus(struct ahc_softc *ahc)
  5838. {
  5839. uint8_t scsiseq;
  5840. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
  5841. scsiseq = ahc_inb(ahc, SCSISEQ);
  5842. ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
  5843. ahc_flush_device_writes(ahc);
  5844. ahc_delay(AHC_BUSRESET_DELAY);
  5845. /* Turn off the bus reset */
  5846. ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
  5847. ahc_clear_intstat(ahc);
  5848. /* Re-enable reset interrupts */
  5849. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
  5850. }
  5851. int
  5852. ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
  5853. {
  5854. struct ahc_devinfo devinfo;
  5855. u_int initiator, target, max_scsiid;
  5856. u_int sblkctl;
  5857. u_int scsiseq;
  5858. u_int simode1;
  5859. int found;
  5860. int restart_needed;
  5861. char cur_channel;
  5862. ahc->pending_device = NULL;
  5863. ahc_compile_devinfo(&devinfo,
  5864. CAM_TARGET_WILDCARD,
  5865. CAM_TARGET_WILDCARD,
  5866. CAM_LUN_WILDCARD,
  5867. channel, ROLE_UNKNOWN);
  5868. ahc_pause(ahc);
  5869. /* Make sure the sequencer is in a safe location. */
  5870. ahc_clear_critical_section(ahc);
  5871. /*
  5872. * Run our command complete fifos to ensure that we perform
  5873. * completion processing on any commands that 'completed'
  5874. * before the reset occurred.
  5875. */
  5876. ahc_run_qoutfifo(ahc);
  5877. #ifdef AHC_TARGET_MODE
  5878. /*
  5879. * XXX - In Twin mode, the tqinfifo may have commands
  5880. * for an unaffected channel in it. However, if
  5881. * we have run out of ATIO resources to drain that
  5882. * queue, we may not get them all out here. Further,
  5883. * the blocked transactions for the reset channel
  5884. * should just be killed off, irrespecitve of whether
  5885. * we are blocked on ATIO resources. Write a routine
  5886. * to compact the tqinfifo appropriately.
  5887. */
  5888. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  5889. ahc_run_tqinfifo(ahc, /*paused*/TRUE);
  5890. }
  5891. #endif
  5892. /*
  5893. * Reset the bus if we are initiating this reset
  5894. */
  5895. sblkctl = ahc_inb(ahc, SBLKCTL);
  5896. cur_channel = 'A';
  5897. if ((ahc->features & AHC_TWIN) != 0
  5898. && ((sblkctl & SELBUSB) != 0))
  5899. cur_channel = 'B';
  5900. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  5901. if (cur_channel != channel) {
  5902. /* Case 1: Command for another bus is active
  5903. * Stealthily reset the other bus without
  5904. * upsetting the current bus.
  5905. */
  5906. ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
  5907. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5908. #ifdef AHC_TARGET_MODE
  5909. /*
  5910. * Bus resets clear ENSELI, so we cannot
  5911. * defer re-enabling bus reset interrupts
  5912. * if we are in target mode.
  5913. */
  5914. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5915. simode1 |= ENSCSIRST;
  5916. #endif
  5917. ahc_outb(ahc, SIMODE1, simode1);
  5918. if (initiate_reset)
  5919. ahc_reset_current_bus(ahc);
  5920. ahc_clear_intstat(ahc);
  5921. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5922. ahc_outb(ahc, SBLKCTL, sblkctl);
  5923. restart_needed = FALSE;
  5924. } else {
  5925. /* Case 2: A command from this bus is active or we're idle */
  5926. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5927. #ifdef AHC_TARGET_MODE
  5928. /*
  5929. * Bus resets clear ENSELI, so we cannot
  5930. * defer re-enabling bus reset interrupts
  5931. * if we are in target mode.
  5932. */
  5933. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5934. simode1 |= ENSCSIRST;
  5935. #endif
  5936. ahc_outb(ahc, SIMODE1, simode1);
  5937. if (initiate_reset)
  5938. ahc_reset_current_bus(ahc);
  5939. ahc_clear_intstat(ahc);
  5940. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5941. restart_needed = TRUE;
  5942. }
  5943. /*
  5944. * Clean up all the state information for the
  5945. * pending transactions on this bus.
  5946. */
  5947. found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
  5948. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  5949. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  5950. max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
  5951. #ifdef AHC_TARGET_MODE
  5952. /*
  5953. * Send an immediate notify ccb to all target more peripheral
  5954. * drivers affected by this action.
  5955. */
  5956. for (target = 0; target <= max_scsiid; target++) {
  5957. struct ahc_tmode_tstate* tstate;
  5958. u_int lun;
  5959. tstate = ahc->enabled_targets[target];
  5960. if (tstate == NULL)
  5961. continue;
  5962. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  5963. struct ahc_tmode_lstate* lstate;
  5964. lstate = tstate->enabled_luns[lun];
  5965. if (lstate == NULL)
  5966. continue;
  5967. ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
  5968. EVENT_TYPE_BUS_RESET, /*arg*/0);
  5969. ahc_send_lstate_events(ahc, lstate);
  5970. }
  5971. }
  5972. #endif
  5973. /* Notify the XPT that a bus reset occurred */
  5974. ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
  5975. CAM_LUN_WILDCARD, AC_BUS_RESET);
  5976. /*
  5977. * Revert to async/narrow transfers until we renegotiate.
  5978. */
  5979. for (target = 0; target <= max_scsiid; target++) {
  5980. if (ahc->enabled_targets[target] == NULL)
  5981. continue;
  5982. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  5983. struct ahc_devinfo devinfo;
  5984. ahc_compile_devinfo(&devinfo, target, initiator,
  5985. CAM_LUN_WILDCARD,
  5986. channel, ROLE_UNKNOWN);
  5987. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  5988. AHC_TRANS_CUR, /*paused*/TRUE);
  5989. ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  5990. /*period*/0, /*offset*/0,
  5991. /*ppr_options*/0, AHC_TRANS_CUR,
  5992. /*paused*/TRUE);
  5993. }
  5994. }
  5995. if (restart_needed)
  5996. ahc_restart(ahc);
  5997. else
  5998. ahc_unpause(ahc);
  5999. return found;
  6000. }
  6001. /***************************** Residual Processing ****************************/
  6002. /*
  6003. * Calculate the residual for a just completed SCB.
  6004. */
  6005. static void
  6006. ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
  6007. {
  6008. struct hardware_scb *hscb;
  6009. struct status_pkt *spkt;
  6010. uint32_t sgptr;
  6011. uint32_t resid_sgptr;
  6012. uint32_t resid;
  6013. /*
  6014. * 5 cases.
  6015. * 1) No residual.
  6016. * SG_RESID_VALID clear in sgptr.
  6017. * 2) Transferless command
  6018. * 3) Never performed any transfers.
  6019. * sgptr has SG_FULL_RESID set.
  6020. * 4) No residual but target did not
  6021. * save data pointers after the
  6022. * last transfer, so sgptr was
  6023. * never updated.
  6024. * 5) We have a partial residual.
  6025. * Use residual_sgptr to determine
  6026. * where we are.
  6027. */
  6028. hscb = scb->hscb;
  6029. sgptr = ahc_le32toh(hscb->sgptr);
  6030. if ((sgptr & SG_RESID_VALID) == 0)
  6031. /* Case 1 */
  6032. return;
  6033. sgptr &= ~SG_RESID_VALID;
  6034. if ((sgptr & SG_LIST_NULL) != 0)
  6035. /* Case 2 */
  6036. return;
  6037. spkt = &hscb->shared_data.status;
  6038. resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
  6039. if ((sgptr & SG_FULL_RESID) != 0) {
  6040. /* Case 3 */
  6041. resid = ahc_get_transfer_length(scb);
  6042. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  6043. /* Case 4 */
  6044. return;
  6045. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  6046. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  6047. } else {
  6048. struct ahc_dma_seg *sg;
  6049. /*
  6050. * Remainder of the SG where the transfer
  6051. * stopped.
  6052. */
  6053. resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
  6054. sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
  6055. /* The residual sg_ptr always points to the next sg */
  6056. sg--;
  6057. /*
  6058. * Add up the contents of all residual
  6059. * SG segments that are after the SG where
  6060. * the transfer stopped.
  6061. */
  6062. while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
  6063. sg++;
  6064. resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  6065. }
  6066. }
  6067. if ((scb->flags & SCB_SENSE) == 0)
  6068. ahc_set_residual(scb, resid);
  6069. else
  6070. ahc_set_sense_residual(scb, resid);
  6071. #ifdef AHC_DEBUG
  6072. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  6073. ahc_print_path(ahc, scb);
  6074. printk("Handled %sResidual of %d bytes\n",
  6075. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  6076. }
  6077. #endif
  6078. }
  6079. /******************************* Target Mode **********************************/
  6080. #ifdef AHC_TARGET_MODE
  6081. /*
  6082. * Add a target mode event to this lun's queue
  6083. */
  6084. static void
  6085. ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
  6086. u_int initiator_id, u_int event_type, u_int event_arg)
  6087. {
  6088. struct ahc_tmode_event *event;
  6089. int pending;
  6090. xpt_freeze_devq(lstate->path, /*count*/1);
  6091. if (lstate->event_w_idx >= lstate->event_r_idx)
  6092. pending = lstate->event_w_idx - lstate->event_r_idx;
  6093. else
  6094. pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
  6095. - (lstate->event_r_idx - lstate->event_w_idx);
  6096. if (event_type == EVENT_TYPE_BUS_RESET
  6097. || event_type == MSG_BUS_DEV_RESET) {
  6098. /*
  6099. * Any earlier events are irrelevant, so reset our buffer.
  6100. * This has the effect of allowing us to deal with reset
  6101. * floods (an external device holding down the reset line)
  6102. * without losing the event that is really interesting.
  6103. */
  6104. lstate->event_r_idx = 0;
  6105. lstate->event_w_idx = 0;
  6106. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  6107. }
  6108. if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
  6109. xpt_print_path(lstate->path);
  6110. printk("immediate event %x:%x lost\n",
  6111. lstate->event_buffer[lstate->event_r_idx].event_type,
  6112. lstate->event_buffer[lstate->event_r_idx].event_arg);
  6113. lstate->event_r_idx++;
  6114. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6115. lstate->event_r_idx = 0;
  6116. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  6117. }
  6118. event = &lstate->event_buffer[lstate->event_w_idx];
  6119. event->initiator_id = initiator_id;
  6120. event->event_type = event_type;
  6121. event->event_arg = event_arg;
  6122. lstate->event_w_idx++;
  6123. if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6124. lstate->event_w_idx = 0;
  6125. }
  6126. /*
  6127. * Send any target mode events queued up waiting
  6128. * for immediate notify resources.
  6129. */
  6130. void
  6131. ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
  6132. {
  6133. struct ccb_hdr *ccbh;
  6134. struct ccb_immed_notify *inot;
  6135. while (lstate->event_r_idx != lstate->event_w_idx
  6136. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  6137. struct ahc_tmode_event *event;
  6138. event = &lstate->event_buffer[lstate->event_r_idx];
  6139. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  6140. inot = (struct ccb_immed_notify *)ccbh;
  6141. switch (event->event_type) {
  6142. case EVENT_TYPE_BUS_RESET:
  6143. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  6144. break;
  6145. default:
  6146. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  6147. inot->message_args[0] = event->event_type;
  6148. inot->message_args[1] = event->event_arg;
  6149. break;
  6150. }
  6151. inot->initiator_id = event->initiator_id;
  6152. inot->sense_len = 0;
  6153. xpt_done((union ccb *)inot);
  6154. lstate->event_r_idx++;
  6155. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6156. lstate->event_r_idx = 0;
  6157. }
  6158. }
  6159. #endif
  6160. /******************** Sequencer Program Patching/Download *********************/
  6161. #ifdef AHC_DUMP_SEQ
  6162. void
  6163. ahc_dumpseq(struct ahc_softc* ahc)
  6164. {
  6165. int i;
  6166. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  6167. ahc_outb(ahc, SEQADDR0, 0);
  6168. ahc_outb(ahc, SEQADDR1, 0);
  6169. for (i = 0; i < ahc->instruction_ram_size; i++) {
  6170. uint8_t ins_bytes[4];
  6171. ahc_insb(ahc, SEQRAM, ins_bytes, 4);
  6172. printk("0x%08x\n", ins_bytes[0] << 24
  6173. | ins_bytes[1] << 16
  6174. | ins_bytes[2] << 8
  6175. | ins_bytes[3]);
  6176. }
  6177. }
  6178. #endif
  6179. static int
  6180. ahc_loadseq(struct ahc_softc *ahc)
  6181. {
  6182. struct cs cs_table[num_critical_sections];
  6183. u_int begin_set[num_critical_sections];
  6184. u_int end_set[num_critical_sections];
  6185. const struct patch *cur_patch;
  6186. u_int cs_count;
  6187. u_int cur_cs;
  6188. u_int i;
  6189. u_int skip_addr;
  6190. u_int sg_prefetch_cnt;
  6191. int downloaded;
  6192. uint8_t download_consts[7];
  6193. /*
  6194. * Start out with 0 critical sections
  6195. * that apply to this firmware load.
  6196. */
  6197. cs_count = 0;
  6198. cur_cs = 0;
  6199. memset(begin_set, 0, sizeof(begin_set));
  6200. memset(end_set, 0, sizeof(end_set));
  6201. /* Setup downloadable constant table */
  6202. download_consts[QOUTFIFO_OFFSET] = 0;
  6203. if (ahc->targetcmds != NULL)
  6204. download_consts[QOUTFIFO_OFFSET] += 32;
  6205. download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
  6206. download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
  6207. download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
  6208. sg_prefetch_cnt = ahc->pci_cachesize;
  6209. if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
  6210. sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
  6211. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  6212. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
  6213. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
  6214. cur_patch = patches;
  6215. downloaded = 0;
  6216. skip_addr = 0;
  6217. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  6218. ahc_outb(ahc, SEQADDR0, 0);
  6219. ahc_outb(ahc, SEQADDR1, 0);
  6220. for (i = 0; i < sizeof(seqprog)/4; i++) {
  6221. if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
  6222. /*
  6223. * Don't download this instruction as it
  6224. * is in a patch that was removed.
  6225. */
  6226. continue;
  6227. }
  6228. if (downloaded == ahc->instruction_ram_size) {
  6229. /*
  6230. * We're about to exceed the instruction
  6231. * storage capacity for this chip. Fail
  6232. * the load.
  6233. */
  6234. printk("\n%s: Program too large for instruction memory "
  6235. "size of %d!\n", ahc_name(ahc),
  6236. ahc->instruction_ram_size);
  6237. return (ENOMEM);
  6238. }
  6239. /*
  6240. * Move through the CS table until we find a CS
  6241. * that might apply to this instruction.
  6242. */
  6243. for (; cur_cs < num_critical_sections; cur_cs++) {
  6244. if (critical_sections[cur_cs].end <= i) {
  6245. if (begin_set[cs_count] == TRUE
  6246. && end_set[cs_count] == FALSE) {
  6247. cs_table[cs_count].end = downloaded;
  6248. end_set[cs_count] = TRUE;
  6249. cs_count++;
  6250. }
  6251. continue;
  6252. }
  6253. if (critical_sections[cur_cs].begin <= i
  6254. && begin_set[cs_count] == FALSE) {
  6255. cs_table[cs_count].begin = downloaded;
  6256. begin_set[cs_count] = TRUE;
  6257. }
  6258. break;
  6259. }
  6260. ahc_download_instr(ahc, i, download_consts);
  6261. downloaded++;
  6262. }
  6263. ahc->num_critical_sections = cs_count;
  6264. if (cs_count != 0) {
  6265. cs_count *= sizeof(struct cs);
  6266. ahc->critical_sections = kmalloc(cs_count, GFP_ATOMIC);
  6267. if (ahc->critical_sections == NULL)
  6268. panic("ahc_loadseq: Could not malloc");
  6269. memcpy(ahc->critical_sections, cs_table, cs_count);
  6270. }
  6271. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
  6272. if (bootverbose) {
  6273. printk(" %d instructions downloaded\n", downloaded);
  6274. printk("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  6275. ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
  6276. }
  6277. return (0);
  6278. }
  6279. static int
  6280. ahc_check_patch(struct ahc_softc *ahc, const struct patch **start_patch,
  6281. u_int start_instr, u_int *skip_addr)
  6282. {
  6283. const struct patch *cur_patch;
  6284. const struct patch *last_patch;
  6285. u_int num_patches;
  6286. num_patches = ARRAY_SIZE(patches);
  6287. last_patch = &patches[num_patches];
  6288. cur_patch = *start_patch;
  6289. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  6290. if (cur_patch->patch_func(ahc) == 0) {
  6291. /* Start rejecting code */
  6292. *skip_addr = start_instr + cur_patch->skip_instr;
  6293. cur_patch += cur_patch->skip_patch;
  6294. } else {
  6295. /* Accepted this patch. Advance to the next
  6296. * one and wait for our intruction pointer to
  6297. * hit this point.
  6298. */
  6299. cur_patch++;
  6300. }
  6301. }
  6302. *start_patch = cur_patch;
  6303. if (start_instr < *skip_addr)
  6304. /* Still skipping */
  6305. return (0);
  6306. return (1);
  6307. }
  6308. static void
  6309. ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
  6310. {
  6311. union ins_formats instr;
  6312. struct ins_format1 *fmt1_ins;
  6313. struct ins_format3 *fmt3_ins;
  6314. u_int opcode;
  6315. /*
  6316. * The firmware is always compiled into a little endian format.
  6317. */
  6318. instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  6319. fmt1_ins = &instr.format1;
  6320. fmt3_ins = NULL;
  6321. /* Pull the opcode */
  6322. opcode = instr.format1.opcode;
  6323. switch (opcode) {
  6324. case AIC_OP_JMP:
  6325. case AIC_OP_JC:
  6326. case AIC_OP_JNC:
  6327. case AIC_OP_CALL:
  6328. case AIC_OP_JNE:
  6329. case AIC_OP_JNZ:
  6330. case AIC_OP_JE:
  6331. case AIC_OP_JZ:
  6332. {
  6333. const struct patch *cur_patch;
  6334. int address_offset;
  6335. u_int address;
  6336. u_int skip_addr;
  6337. u_int i;
  6338. fmt3_ins = &instr.format3;
  6339. address_offset = 0;
  6340. address = fmt3_ins->address;
  6341. cur_patch = patches;
  6342. skip_addr = 0;
  6343. for (i = 0; i < address;) {
  6344. ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
  6345. if (skip_addr > i) {
  6346. int end_addr;
  6347. end_addr = min(address, skip_addr);
  6348. address_offset += end_addr - i;
  6349. i = skip_addr;
  6350. } else {
  6351. i++;
  6352. }
  6353. }
  6354. address -= address_offset;
  6355. fmt3_ins->address = address;
  6356. /* FALLTHROUGH */
  6357. }
  6358. case AIC_OP_OR:
  6359. case AIC_OP_AND:
  6360. case AIC_OP_XOR:
  6361. case AIC_OP_ADD:
  6362. case AIC_OP_ADC:
  6363. case AIC_OP_BMOV:
  6364. if (fmt1_ins->parity != 0) {
  6365. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  6366. }
  6367. fmt1_ins->parity = 0;
  6368. if ((ahc->features & AHC_CMD_CHAN) == 0
  6369. && opcode == AIC_OP_BMOV) {
  6370. /*
  6371. * Block move was added at the same time
  6372. * as the command channel. Verify that
  6373. * this is only a move of a single element
  6374. * and convert the BMOV to a MOV
  6375. * (AND with an immediate of FF).
  6376. */
  6377. if (fmt1_ins->immediate != 1)
  6378. panic("%s: BMOV not supported\n",
  6379. ahc_name(ahc));
  6380. fmt1_ins->opcode = AIC_OP_AND;
  6381. fmt1_ins->immediate = 0xff;
  6382. }
  6383. /* FALLTHROUGH */
  6384. case AIC_OP_ROL:
  6385. if ((ahc->features & AHC_ULTRA2) != 0) {
  6386. int i, count;
  6387. /* Calculate odd parity for the instruction */
  6388. for (i = 0, count = 0; i < 31; i++) {
  6389. uint32_t mask;
  6390. mask = 0x01 << i;
  6391. if ((instr.integer & mask) != 0)
  6392. count++;
  6393. }
  6394. if ((count & 0x01) == 0)
  6395. instr.format1.parity = 1;
  6396. } else {
  6397. /* Compress the instruction for older sequencers */
  6398. if (fmt3_ins != NULL) {
  6399. instr.integer =
  6400. fmt3_ins->immediate
  6401. | (fmt3_ins->source << 8)
  6402. | (fmt3_ins->address << 16)
  6403. | (fmt3_ins->opcode << 25);
  6404. } else {
  6405. instr.integer =
  6406. fmt1_ins->immediate
  6407. | (fmt1_ins->source << 8)
  6408. | (fmt1_ins->destination << 16)
  6409. | (fmt1_ins->ret << 24)
  6410. | (fmt1_ins->opcode << 25);
  6411. }
  6412. }
  6413. /* The sequencer is a little endian cpu */
  6414. instr.integer = ahc_htole32(instr.integer);
  6415. ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
  6416. break;
  6417. default:
  6418. panic("Unknown opcode encountered in seq program");
  6419. break;
  6420. }
  6421. }
  6422. int
  6423. ahc_print_register(const ahc_reg_parse_entry_t *table, u_int num_entries,
  6424. const char *name, u_int address, u_int value,
  6425. u_int *cur_column, u_int wrap_point)
  6426. {
  6427. int printed;
  6428. u_int printed_mask;
  6429. if (cur_column != NULL && *cur_column >= wrap_point) {
  6430. printk("\n");
  6431. *cur_column = 0;
  6432. }
  6433. printed = printk("%s[0x%x]", name, value);
  6434. if (table == NULL) {
  6435. printed += printk(" ");
  6436. *cur_column += printed;
  6437. return (printed);
  6438. }
  6439. printed_mask = 0;
  6440. while (printed_mask != 0xFF) {
  6441. int entry;
  6442. for (entry = 0; entry < num_entries; entry++) {
  6443. if (((value & table[entry].mask)
  6444. != table[entry].value)
  6445. || ((printed_mask & table[entry].mask)
  6446. == table[entry].mask))
  6447. continue;
  6448. printed += printk("%s%s",
  6449. printed_mask == 0 ? ":(" : "|",
  6450. table[entry].name);
  6451. printed_mask |= table[entry].mask;
  6452. break;
  6453. }
  6454. if (entry >= num_entries)
  6455. break;
  6456. }
  6457. if (printed_mask != 0)
  6458. printed += printk(") ");
  6459. else
  6460. printed += printk(" ");
  6461. if (cur_column != NULL)
  6462. *cur_column += printed;
  6463. return (printed);
  6464. }
  6465. void
  6466. ahc_dump_card_state(struct ahc_softc *ahc)
  6467. {
  6468. struct scb *scb;
  6469. struct scb_tailq *untagged_q;
  6470. u_int cur_col;
  6471. int paused;
  6472. int target;
  6473. int maxtarget;
  6474. int i;
  6475. uint8_t last_phase;
  6476. uint8_t qinpos;
  6477. uint8_t qintail;
  6478. uint8_t qoutpos;
  6479. uint8_t scb_index;
  6480. uint8_t saved_scbptr;
  6481. if (ahc_is_paused(ahc)) {
  6482. paused = 1;
  6483. } else {
  6484. paused = 0;
  6485. ahc_pause(ahc);
  6486. }
  6487. saved_scbptr = ahc_inb(ahc, SCBPTR);
  6488. last_phase = ahc_inb(ahc, LASTPHASE);
  6489. printk(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  6490. "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
  6491. ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
  6492. ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
  6493. if (paused)
  6494. printk("Card was paused\n");
  6495. printk("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
  6496. ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
  6497. ahc_inb(ahc, ARG_2));
  6498. printk("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
  6499. ahc_inb(ahc, SCBPTR));
  6500. cur_col = 0;
  6501. if ((ahc->features & AHC_DT) != 0)
  6502. ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
  6503. ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
  6504. ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
  6505. ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
  6506. ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
  6507. ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
  6508. ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
  6509. ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
  6510. ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
  6511. ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
  6512. ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
  6513. ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
  6514. ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
  6515. ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
  6516. ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
  6517. ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
  6518. ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
  6519. ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
  6520. ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
  6521. if (cur_col != 0)
  6522. printk("\n");
  6523. printk("STACK:");
  6524. for (i = 0; i < STACK_SIZE; i++)
  6525. printk(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
  6526. printk("\nSCB count = %d\n", ahc->scb_data->numscbs);
  6527. printk("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
  6528. printk("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
  6529. /* QINFIFO */
  6530. printk("QINFIFO entries: ");
  6531. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  6532. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  6533. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  6534. } else
  6535. qinpos = ahc_inb(ahc, QINPOS);
  6536. qintail = ahc->qinfifonext;
  6537. while (qinpos != qintail) {
  6538. printk("%d ", ahc->qinfifo[qinpos]);
  6539. qinpos++;
  6540. }
  6541. printk("\n");
  6542. printk("Waiting Queue entries: ");
  6543. scb_index = ahc_inb(ahc, WAITING_SCBH);
  6544. i = 0;
  6545. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6546. ahc_outb(ahc, SCBPTR, scb_index);
  6547. printk("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6548. scb_index = ahc_inb(ahc, SCB_NEXT);
  6549. }
  6550. printk("\n");
  6551. printk("Disconnected Queue entries: ");
  6552. scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
  6553. i = 0;
  6554. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6555. ahc_outb(ahc, SCBPTR, scb_index);
  6556. printk("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6557. scb_index = ahc_inb(ahc, SCB_NEXT);
  6558. }
  6559. printk("\n");
  6560. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  6561. printk("QOUTFIFO entries: ");
  6562. qoutpos = ahc->qoutfifonext;
  6563. i = 0;
  6564. while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
  6565. printk("%d ", ahc->qoutfifo[qoutpos]);
  6566. qoutpos++;
  6567. }
  6568. printk("\n");
  6569. printk("Sequencer Free SCB List: ");
  6570. scb_index = ahc_inb(ahc, FREE_SCBH);
  6571. i = 0;
  6572. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6573. ahc_outb(ahc, SCBPTR, scb_index);
  6574. printk("%d ", scb_index);
  6575. scb_index = ahc_inb(ahc, SCB_NEXT);
  6576. }
  6577. printk("\n");
  6578. printk("Sequencer SCB Info: ");
  6579. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  6580. ahc_outb(ahc, SCBPTR, i);
  6581. cur_col = printk("\n%3d ", i);
  6582. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
  6583. ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
  6584. ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
  6585. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6586. }
  6587. printk("\n");
  6588. printk("Pending list: ");
  6589. i = 0;
  6590. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6591. if (i++ > 256)
  6592. break;
  6593. cur_col = printk("\n%3d ", scb->hscb->tag);
  6594. ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
  6595. ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
  6596. ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
  6597. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  6598. ahc_outb(ahc, SCBPTR, scb->hscb->tag);
  6599. printk("(");
  6600. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
  6601. &cur_col, 60);
  6602. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6603. printk(")");
  6604. }
  6605. }
  6606. printk("\n");
  6607. printk("Kernel Free SCB list: ");
  6608. i = 0;
  6609. SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
  6610. if (i++ > 256)
  6611. break;
  6612. printk("%d ", scb->hscb->tag);
  6613. }
  6614. printk("\n");
  6615. maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
  6616. for (target = 0; target <= maxtarget; target++) {
  6617. untagged_q = &ahc->untagged_queues[target];
  6618. if (TAILQ_FIRST(untagged_q) == NULL)
  6619. continue;
  6620. printk("Untagged Q(%d): ", target);
  6621. i = 0;
  6622. TAILQ_FOREACH(scb, untagged_q, links.tqe) {
  6623. if (i++ > 256)
  6624. break;
  6625. printk("%d ", scb->hscb->tag);
  6626. }
  6627. printk("\n");
  6628. }
  6629. ahc_platform_dump_card_state(ahc);
  6630. printk("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  6631. ahc_outb(ahc, SCBPTR, saved_scbptr);
  6632. if (paused == 0)
  6633. ahc_unpause(ahc);
  6634. }
  6635. /************************* Target Mode ****************************************/
  6636. #ifdef AHC_TARGET_MODE
  6637. cam_status
  6638. ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
  6639. struct ahc_tmode_tstate **tstate,
  6640. struct ahc_tmode_lstate **lstate,
  6641. int notfound_failure)
  6642. {
  6643. if ((ahc->features & AHC_TARGETMODE) == 0)
  6644. return (CAM_REQ_INVALID);
  6645. /*
  6646. * Handle the 'black hole' device that sucks up
  6647. * requests to unattached luns on enabled targets.
  6648. */
  6649. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  6650. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  6651. *tstate = NULL;
  6652. *lstate = ahc->black_hole;
  6653. } else {
  6654. u_int max_id;
  6655. max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  6656. if (ccb->ccb_h.target_id >= max_id)
  6657. return (CAM_TID_INVALID);
  6658. if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
  6659. return (CAM_LUN_INVALID);
  6660. *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
  6661. *lstate = NULL;
  6662. if (*tstate != NULL)
  6663. *lstate =
  6664. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  6665. }
  6666. if (notfound_failure != 0 && *lstate == NULL)
  6667. return (CAM_PATH_INVALID);
  6668. return (CAM_REQ_CMP);
  6669. }
  6670. void
  6671. ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
  6672. {
  6673. struct ahc_tmode_tstate *tstate;
  6674. struct ahc_tmode_lstate *lstate;
  6675. struct ccb_en_lun *cel;
  6676. cam_status status;
  6677. u_long s;
  6678. u_int target;
  6679. u_int lun;
  6680. u_int target_mask;
  6681. u_int our_id;
  6682. int error;
  6683. char channel;
  6684. status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
  6685. /*notfound_failure*/FALSE);
  6686. if (status != CAM_REQ_CMP) {
  6687. ccb->ccb_h.status = status;
  6688. return;
  6689. }
  6690. if (cam_sim_bus(sim) == 0)
  6691. our_id = ahc->our_id;
  6692. else
  6693. our_id = ahc->our_id_b;
  6694. if (ccb->ccb_h.target_id != our_id) {
  6695. /*
  6696. * our_id represents our initiator ID, or
  6697. * the ID of the first target to have an
  6698. * enabled lun in target mode. There are
  6699. * two cases that may preclude enabling a
  6700. * target id other than our_id.
  6701. *
  6702. * o our_id is for an active initiator role.
  6703. * Since the hardware does not support
  6704. * reselections to the initiator role at
  6705. * anything other than our_id, and our_id
  6706. * is used by the hardware to indicate the
  6707. * ID to use for both select-out and
  6708. * reselect-out operations, the only target
  6709. * ID we can support in this mode is our_id.
  6710. *
  6711. * o The MULTARGID feature is not available and
  6712. * a previous target mode ID has been enabled.
  6713. */
  6714. if ((ahc->features & AHC_MULTIROLE) != 0) {
  6715. if ((ahc->features & AHC_MULTI_TID) != 0
  6716. && (ahc->flags & AHC_INITIATORROLE) != 0) {
  6717. /*
  6718. * Only allow additional targets if
  6719. * the initiator role is disabled.
  6720. * The hardware cannot handle a re-select-in
  6721. * on the initiator id during a re-select-out
  6722. * on a different target id.
  6723. */
  6724. status = CAM_TID_INVALID;
  6725. } else if ((ahc->flags & AHC_INITIATORROLE) != 0
  6726. || ahc->enabled_luns > 0) {
  6727. /*
  6728. * Only allow our target id to change
  6729. * if the initiator role is not configured
  6730. * and there are no enabled luns which
  6731. * are attached to the currently registered
  6732. * scsi id.
  6733. */
  6734. status = CAM_TID_INVALID;
  6735. }
  6736. } else if ((ahc->features & AHC_MULTI_TID) == 0
  6737. && ahc->enabled_luns > 0) {
  6738. status = CAM_TID_INVALID;
  6739. }
  6740. }
  6741. if (status != CAM_REQ_CMP) {
  6742. ccb->ccb_h.status = status;
  6743. return;
  6744. }
  6745. /*
  6746. * We now have an id that is valid.
  6747. * If we aren't in target mode, switch modes.
  6748. */
  6749. if ((ahc->flags & AHC_TARGETROLE) == 0
  6750. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  6751. u_long s;
  6752. ahc_flag saved_flags;
  6753. printk("Configuring Target Mode\n");
  6754. ahc_lock(ahc, &s);
  6755. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  6756. ccb->ccb_h.status = CAM_BUSY;
  6757. ahc_unlock(ahc, &s);
  6758. return;
  6759. }
  6760. saved_flags = ahc->flags;
  6761. ahc->flags |= AHC_TARGETROLE;
  6762. if ((ahc->features & AHC_MULTIROLE) == 0)
  6763. ahc->flags &= ~AHC_INITIATORROLE;
  6764. ahc_pause(ahc);
  6765. error = ahc_loadseq(ahc);
  6766. if (error != 0) {
  6767. /*
  6768. * Restore original configuration and notify
  6769. * the caller that we cannot support target mode.
  6770. * Since the adapter started out in this
  6771. * configuration, the firmware load will succeed,
  6772. * so there is no point in checking ahc_loadseq's
  6773. * return value.
  6774. */
  6775. ahc->flags = saved_flags;
  6776. (void)ahc_loadseq(ahc);
  6777. ahc_restart(ahc);
  6778. ahc_unlock(ahc, &s);
  6779. ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  6780. return;
  6781. }
  6782. ahc_restart(ahc);
  6783. ahc_unlock(ahc, &s);
  6784. }
  6785. cel = &ccb->cel;
  6786. target = ccb->ccb_h.target_id;
  6787. lun = ccb->ccb_h.target_lun;
  6788. channel = SIM_CHANNEL(ahc, sim);
  6789. target_mask = 0x01 << target;
  6790. if (channel == 'B')
  6791. target_mask <<= 8;
  6792. if (cel->enable != 0) {
  6793. u_int scsiseq;
  6794. /* Are we already enabled?? */
  6795. if (lstate != NULL) {
  6796. xpt_print_path(ccb->ccb_h.path);
  6797. printk("Lun already enabled\n");
  6798. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  6799. return;
  6800. }
  6801. if (cel->grp6_len != 0
  6802. || cel->grp7_len != 0) {
  6803. /*
  6804. * Don't (yet?) support vendor
  6805. * specific commands.
  6806. */
  6807. ccb->ccb_h.status = CAM_REQ_INVALID;
  6808. printk("Non-zero Group Codes\n");
  6809. return;
  6810. }
  6811. /*
  6812. * Seems to be okay.
  6813. * Setup our data structures.
  6814. */
  6815. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  6816. tstate = ahc_alloc_tstate(ahc, target, channel);
  6817. if (tstate == NULL) {
  6818. xpt_print_path(ccb->ccb_h.path);
  6819. printk("Couldn't allocate tstate\n");
  6820. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6821. return;
  6822. }
  6823. }
  6824. lstate = kmalloc(sizeof(*lstate), GFP_ATOMIC);
  6825. if (lstate == NULL) {
  6826. xpt_print_path(ccb->ccb_h.path);
  6827. printk("Couldn't allocate lstate\n");
  6828. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6829. return;
  6830. }
  6831. memset(lstate, 0, sizeof(*lstate));
  6832. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  6833. xpt_path_path_id(ccb->ccb_h.path),
  6834. xpt_path_target_id(ccb->ccb_h.path),
  6835. xpt_path_lun_id(ccb->ccb_h.path));
  6836. if (status != CAM_REQ_CMP) {
  6837. kfree(lstate);
  6838. xpt_print_path(ccb->ccb_h.path);
  6839. printk("Couldn't allocate path\n");
  6840. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6841. return;
  6842. }
  6843. SLIST_INIT(&lstate->accept_tios);
  6844. SLIST_INIT(&lstate->immed_notifies);
  6845. ahc_lock(ahc, &s);
  6846. ahc_pause(ahc);
  6847. if (target != CAM_TARGET_WILDCARD) {
  6848. tstate->enabled_luns[lun] = lstate;
  6849. ahc->enabled_luns++;
  6850. if ((ahc->features & AHC_MULTI_TID) != 0) {
  6851. u_int targid_mask;
  6852. targid_mask = ahc_inb(ahc, TARGID)
  6853. | (ahc_inb(ahc, TARGID + 1) << 8);
  6854. targid_mask |= target_mask;
  6855. ahc_outb(ahc, TARGID, targid_mask);
  6856. ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
  6857. ahc_update_scsiid(ahc, targid_mask);
  6858. } else {
  6859. u_int our_id;
  6860. char channel;
  6861. channel = SIM_CHANNEL(ahc, sim);
  6862. our_id = SIM_SCSI_ID(ahc, sim);
  6863. /*
  6864. * This can only happen if selections
  6865. * are not enabled
  6866. */
  6867. if (target != our_id) {
  6868. u_int sblkctl;
  6869. char cur_channel;
  6870. int swap;
  6871. sblkctl = ahc_inb(ahc, SBLKCTL);
  6872. cur_channel = (sblkctl & SELBUSB)
  6873. ? 'B' : 'A';
  6874. if ((ahc->features & AHC_TWIN) == 0)
  6875. cur_channel = 'A';
  6876. swap = cur_channel != channel;
  6877. if (channel == 'A')
  6878. ahc->our_id = target;
  6879. else
  6880. ahc->our_id_b = target;
  6881. if (swap)
  6882. ahc_outb(ahc, SBLKCTL,
  6883. sblkctl ^ SELBUSB);
  6884. ahc_outb(ahc, SCSIID, target);
  6885. if (swap)
  6886. ahc_outb(ahc, SBLKCTL, sblkctl);
  6887. }
  6888. }
  6889. } else
  6890. ahc->black_hole = lstate;
  6891. /* Allow select-in operations */
  6892. if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
  6893. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6894. scsiseq |= ENSELI;
  6895. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6896. scsiseq = ahc_inb(ahc, SCSISEQ);
  6897. scsiseq |= ENSELI;
  6898. ahc_outb(ahc, SCSISEQ, scsiseq);
  6899. }
  6900. ahc_unpause(ahc);
  6901. ahc_unlock(ahc, &s);
  6902. ccb->ccb_h.status = CAM_REQ_CMP;
  6903. xpt_print_path(ccb->ccb_h.path);
  6904. printk("Lun now enabled for target mode\n");
  6905. } else {
  6906. struct scb *scb;
  6907. int i, empty;
  6908. if (lstate == NULL) {
  6909. ccb->ccb_h.status = CAM_LUN_INVALID;
  6910. return;
  6911. }
  6912. ahc_lock(ahc, &s);
  6913. ccb->ccb_h.status = CAM_REQ_CMP;
  6914. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6915. struct ccb_hdr *ccbh;
  6916. ccbh = &scb->io_ctx->ccb_h;
  6917. if (ccbh->func_code == XPT_CONT_TARGET_IO
  6918. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  6919. printk("CTIO pending\n");
  6920. ccb->ccb_h.status = CAM_REQ_INVALID;
  6921. ahc_unlock(ahc, &s);
  6922. return;
  6923. }
  6924. }
  6925. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  6926. printk("ATIOs pending\n");
  6927. ccb->ccb_h.status = CAM_REQ_INVALID;
  6928. }
  6929. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  6930. printk("INOTs pending\n");
  6931. ccb->ccb_h.status = CAM_REQ_INVALID;
  6932. }
  6933. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  6934. ahc_unlock(ahc, &s);
  6935. return;
  6936. }
  6937. xpt_print_path(ccb->ccb_h.path);
  6938. printk("Target mode disabled\n");
  6939. xpt_free_path(lstate->path);
  6940. kfree(lstate);
  6941. ahc_pause(ahc);
  6942. /* Can we clean up the target too? */
  6943. if (target != CAM_TARGET_WILDCARD) {
  6944. tstate->enabled_luns[lun] = NULL;
  6945. ahc->enabled_luns--;
  6946. for (empty = 1, i = 0; i < 8; i++)
  6947. if (tstate->enabled_luns[i] != NULL) {
  6948. empty = 0;
  6949. break;
  6950. }
  6951. if (empty) {
  6952. ahc_free_tstate(ahc, target, channel,
  6953. /*force*/FALSE);
  6954. if (ahc->features & AHC_MULTI_TID) {
  6955. u_int targid_mask;
  6956. targid_mask = ahc_inb(ahc, TARGID)
  6957. | (ahc_inb(ahc, TARGID + 1)
  6958. << 8);
  6959. targid_mask &= ~target_mask;
  6960. ahc_outb(ahc, TARGID, targid_mask);
  6961. ahc_outb(ahc, TARGID+1,
  6962. (targid_mask >> 8));
  6963. ahc_update_scsiid(ahc, targid_mask);
  6964. }
  6965. }
  6966. } else {
  6967. ahc->black_hole = NULL;
  6968. /*
  6969. * We can't allow selections without
  6970. * our black hole device.
  6971. */
  6972. empty = TRUE;
  6973. }
  6974. if (ahc->enabled_luns == 0) {
  6975. /* Disallow select-in */
  6976. u_int scsiseq;
  6977. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6978. scsiseq &= ~ENSELI;
  6979. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6980. scsiseq = ahc_inb(ahc, SCSISEQ);
  6981. scsiseq &= ~ENSELI;
  6982. ahc_outb(ahc, SCSISEQ, scsiseq);
  6983. if ((ahc->features & AHC_MULTIROLE) == 0) {
  6984. printk("Configuring Initiator Mode\n");
  6985. ahc->flags &= ~AHC_TARGETROLE;
  6986. ahc->flags |= AHC_INITIATORROLE;
  6987. /*
  6988. * Returning to a configuration that
  6989. * fit previously will always succeed.
  6990. */
  6991. (void)ahc_loadseq(ahc);
  6992. ahc_restart(ahc);
  6993. /*
  6994. * Unpaused. The extra unpause
  6995. * that follows is harmless.
  6996. */
  6997. }
  6998. }
  6999. ahc_unpause(ahc);
  7000. ahc_unlock(ahc, &s);
  7001. }
  7002. }
  7003. static void
  7004. ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
  7005. {
  7006. u_int scsiid_mask;
  7007. u_int scsiid;
  7008. if ((ahc->features & AHC_MULTI_TID) == 0)
  7009. panic("ahc_update_scsiid called on non-multitid unit\n");
  7010. /*
  7011. * Since we will rely on the TARGID mask
  7012. * for selection enables, ensure that OID
  7013. * in SCSIID is not set to some other ID
  7014. * that we don't want to allow selections on.
  7015. */
  7016. if ((ahc->features & AHC_ULTRA2) != 0)
  7017. scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
  7018. else
  7019. scsiid = ahc_inb(ahc, SCSIID);
  7020. scsiid_mask = 0x1 << (scsiid & OID);
  7021. if ((targid_mask & scsiid_mask) == 0) {
  7022. u_int our_id;
  7023. /* ffs counts from 1 */
  7024. our_id = ffs(targid_mask);
  7025. if (our_id == 0)
  7026. our_id = ahc->our_id;
  7027. else
  7028. our_id--;
  7029. scsiid &= TID;
  7030. scsiid |= our_id;
  7031. }
  7032. if ((ahc->features & AHC_ULTRA2) != 0)
  7033. ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
  7034. else
  7035. ahc_outb(ahc, SCSIID, scsiid);
  7036. }
  7037. static void
  7038. ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
  7039. {
  7040. struct target_cmd *cmd;
  7041. /*
  7042. * If the card supports auto-access pause,
  7043. * we can access the card directly regardless
  7044. * of whether it is paused or not.
  7045. */
  7046. if ((ahc->features & AHC_AUTOPAUSE) != 0)
  7047. paused = TRUE;
  7048. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
  7049. while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
  7050. /*
  7051. * Only advance through the queue if we
  7052. * have the resources to process the command.
  7053. */
  7054. if (ahc_handle_target_cmd(ahc, cmd) != 0)
  7055. break;
  7056. cmd->cmd_valid = 0;
  7057. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  7058. ahc->shared_data_dmamap,
  7059. ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
  7060. sizeof(struct target_cmd),
  7061. BUS_DMASYNC_PREREAD);
  7062. ahc->tqinfifonext++;
  7063. /*
  7064. * Lazily update our position in the target mode incoming
  7065. * command queue as seen by the sequencer.
  7066. */
  7067. if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  7068. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  7069. u_int hs_mailbox;
  7070. hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
  7071. hs_mailbox &= ~HOST_TQINPOS;
  7072. hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
  7073. ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
  7074. } else {
  7075. if (!paused)
  7076. ahc_pause(ahc);
  7077. ahc_outb(ahc, KERNEL_TQINPOS,
  7078. ahc->tqinfifonext & HOST_TQINPOS);
  7079. if (!paused)
  7080. ahc_unpause(ahc);
  7081. }
  7082. }
  7083. }
  7084. }
  7085. static int
  7086. ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
  7087. {
  7088. struct ahc_tmode_tstate *tstate;
  7089. struct ahc_tmode_lstate *lstate;
  7090. struct ccb_accept_tio *atio;
  7091. uint8_t *byte;
  7092. int initiator;
  7093. int target;
  7094. int lun;
  7095. initiator = SCSIID_TARGET(ahc, cmd->scsiid);
  7096. target = SCSIID_OUR_ID(cmd->scsiid);
  7097. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  7098. byte = cmd->bytes;
  7099. tstate = ahc->enabled_targets[target];
  7100. lstate = NULL;
  7101. if (tstate != NULL)
  7102. lstate = tstate->enabled_luns[lun];
  7103. /*
  7104. * Commands for disabled luns go to the black hole driver.
  7105. */
  7106. if (lstate == NULL)
  7107. lstate = ahc->black_hole;
  7108. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  7109. if (atio == NULL) {
  7110. ahc->flags |= AHC_TQINFIFO_BLOCKED;
  7111. /*
  7112. * Wait for more ATIOs from the peripheral driver for this lun.
  7113. */
  7114. if (bootverbose)
  7115. printk("%s: ATIOs exhausted\n", ahc_name(ahc));
  7116. return (1);
  7117. } else
  7118. ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
  7119. #if 0
  7120. printk("Incoming command from %d for %d:%d%s\n",
  7121. initiator, target, lun,
  7122. lstate == ahc->black_hole ? "(Black Holed)" : "");
  7123. #endif
  7124. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  7125. if (lstate == ahc->black_hole) {
  7126. /* Fill in the wildcards */
  7127. atio->ccb_h.target_id = target;
  7128. atio->ccb_h.target_lun = lun;
  7129. }
  7130. /*
  7131. * Package it up and send it off to
  7132. * whomever has this lun enabled.
  7133. */
  7134. atio->sense_len = 0;
  7135. atio->init_id = initiator;
  7136. if (byte[0] != 0xFF) {
  7137. /* Tag was included */
  7138. atio->tag_action = *byte++;
  7139. atio->tag_id = *byte++;
  7140. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  7141. } else {
  7142. atio->ccb_h.flags = 0;
  7143. }
  7144. byte++;
  7145. /* Okay. Now determine the cdb size based on the command code */
  7146. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  7147. case 0:
  7148. atio->cdb_len = 6;
  7149. break;
  7150. case 1:
  7151. case 2:
  7152. atio->cdb_len = 10;
  7153. break;
  7154. case 4:
  7155. atio->cdb_len = 16;
  7156. break;
  7157. case 5:
  7158. atio->cdb_len = 12;
  7159. break;
  7160. case 3:
  7161. default:
  7162. /* Only copy the opcode. */
  7163. atio->cdb_len = 1;
  7164. printk("Reserved or VU command code type encountered\n");
  7165. break;
  7166. }
  7167. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  7168. atio->ccb_h.status |= CAM_CDB_RECVD;
  7169. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  7170. /*
  7171. * We weren't allowed to disconnect.
  7172. * We're hanging on the bus until a
  7173. * continue target I/O comes in response
  7174. * to this accept tio.
  7175. */
  7176. #if 0
  7177. printk("Received Immediate Command %d:%d:%d - %p\n",
  7178. initiator, target, lun, ahc->pending_device);
  7179. #endif
  7180. ahc->pending_device = lstate;
  7181. ahc_freeze_ccb((union ccb *)atio);
  7182. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  7183. }
  7184. xpt_done((union ccb*)atio);
  7185. return (0);
  7186. }
  7187. #endif