aic7xxx_core.c 195 KB

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