aic7xxx_core.c 196 KB

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