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