aic79xx_core.c 271 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973
  1. /*
  2. * Core routines and tables shareable across OS platforms.
  3. *
  4. * Copyright (c) 1994-2002 Justin T. Gibbs.
  5. * Copyright (c) 2000-2003 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/aic79xx.c#250 $
  41. */
  42. #ifdef __linux__
  43. #include "aic79xx_osm.h"
  44. #include "aic79xx_inline.h"
  45. #include "aicasm/aicasm_insformat.h"
  46. #else
  47. #include <dev/aic7xxx/aic79xx_osm.h>
  48. #include <dev/aic7xxx/aic79xx_inline.h>
  49. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  50. #endif
  51. /***************************** Lookup Tables **********************************/
  52. char *ahd_chip_names[] =
  53. {
  54. "NONE",
  55. "aic7901",
  56. "aic7902",
  57. "aic7901A"
  58. };
  59. static const u_int num_chip_names = NUM_ELEMENTS(ahd_chip_names);
  60. /*
  61. * Hardware error codes.
  62. */
  63. struct ahd_hard_error_entry {
  64. uint8_t errno;
  65. char *errmesg;
  66. };
  67. static struct ahd_hard_error_entry ahd_hard_errors[] = {
  68. { DSCTMOUT, "Discard Timer has timed out" },
  69. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  70. { SQPARERR, "Sequencer Parity Error" },
  71. { DPARERR, "Data-path Parity Error" },
  72. { MPARERR, "Scratch or SCB Memory Parity Error" },
  73. { CIOPARERR, "CIOBUS Parity Error" },
  74. };
  75. static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
  76. static struct ahd_phase_table_entry ahd_phase_table[] =
  77. {
  78. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  79. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  80. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  81. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  82. { P_COMMAND, MSG_NOOP, "in Command phase" },
  83. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  84. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  85. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  86. { P_BUSFREE, MSG_NOOP, "while idle" },
  87. { 0, MSG_NOOP, "in unknown phase" }
  88. };
  89. /*
  90. * In most cases we only wish to itterate over real phases, so
  91. * exclude the last element from the count.
  92. */
  93. static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
  94. /* Our Sequencer Program */
  95. #include "aic79xx_seq.h"
  96. /**************************** Function Declarations ***************************/
  97. static void ahd_handle_transmission_error(struct ahd_softc *ahd);
  98. static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
  99. u_int lqistat1);
  100. static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
  101. u_int busfreetime);
  102. static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
  103. static void ahd_handle_proto_violation(struct ahd_softc *ahd);
  104. static void ahd_force_renegotiation(struct ahd_softc *ahd,
  105. struct ahd_devinfo *devinfo);
  106. static struct ahd_tmode_tstate*
  107. ahd_alloc_tstate(struct ahd_softc *ahd,
  108. u_int scsi_id, char channel);
  109. #ifdef AHD_TARGET_MODE
  110. static void ahd_free_tstate(struct ahd_softc *ahd,
  111. u_int scsi_id, char channel, int force);
  112. #endif
  113. static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
  114. struct ahd_initiator_tinfo *,
  115. u_int *period,
  116. u_int *ppr_options,
  117. role_t role);
  118. static void ahd_update_neg_table(struct ahd_softc *ahd,
  119. struct ahd_devinfo *devinfo,
  120. struct ahd_transinfo *tinfo);
  121. static void ahd_update_pending_scbs(struct ahd_softc *ahd);
  122. static void ahd_fetch_devinfo(struct ahd_softc *ahd,
  123. struct ahd_devinfo *devinfo);
  124. static void ahd_scb_devinfo(struct ahd_softc *ahd,
  125. struct ahd_devinfo *devinfo,
  126. struct scb *scb);
  127. static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
  128. struct ahd_devinfo *devinfo,
  129. struct scb *scb);
  130. static void ahd_build_transfer_msg(struct ahd_softc *ahd,
  131. struct ahd_devinfo *devinfo);
  132. static void ahd_construct_sdtr(struct ahd_softc *ahd,
  133. struct ahd_devinfo *devinfo,
  134. u_int period, u_int offset);
  135. static void ahd_construct_wdtr(struct ahd_softc *ahd,
  136. struct ahd_devinfo *devinfo,
  137. u_int bus_width);
  138. static void ahd_construct_ppr(struct ahd_softc *ahd,
  139. struct ahd_devinfo *devinfo,
  140. u_int period, u_int offset,
  141. u_int bus_width, u_int ppr_options);
  142. static void ahd_clear_msg_state(struct ahd_softc *ahd);
  143. static void ahd_handle_message_phase(struct ahd_softc *ahd);
  144. typedef enum {
  145. AHDMSG_1B,
  146. AHDMSG_2B,
  147. AHDMSG_EXT
  148. } ahd_msgtype;
  149. static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
  150. u_int msgval, int full);
  151. static int ahd_parse_msg(struct ahd_softc *ahd,
  152. struct ahd_devinfo *devinfo);
  153. static int ahd_handle_msg_reject(struct ahd_softc *ahd,
  154. struct ahd_devinfo *devinfo);
  155. static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
  156. struct ahd_devinfo *devinfo);
  157. static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
  158. static void ahd_handle_devreset(struct ahd_softc *ahd,
  159. struct ahd_devinfo *devinfo,
  160. u_int lun, cam_status status,
  161. char *message, int verbose_level);
  162. #ifdef AHD_TARGET_MODE
  163. static void ahd_setup_target_msgin(struct ahd_softc *ahd,
  164. struct ahd_devinfo *devinfo,
  165. struct scb *scb);
  166. #endif
  167. static u_int ahd_sglist_size(struct ahd_softc *ahd);
  168. static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
  169. static bus_dmamap_callback_t
  170. ahd_dmamap_cb;
  171. static void ahd_initialize_hscbs(struct ahd_softc *ahd);
  172. static int ahd_init_scbdata(struct ahd_softc *ahd);
  173. static void ahd_fini_scbdata(struct ahd_softc *ahd);
  174. static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
  175. static void ahd_iocell_first_selection(struct ahd_softc *ahd);
  176. static void ahd_add_col_list(struct ahd_softc *ahd,
  177. struct scb *scb, u_int col_idx);
  178. static void ahd_rem_col_list(struct ahd_softc *ahd,
  179. struct scb *scb);
  180. static void ahd_chip_init(struct ahd_softc *ahd);
  181. static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
  182. struct scb *prev_scb,
  183. struct scb *scb);
  184. static int ahd_qinfifo_count(struct ahd_softc *ahd);
  185. static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
  186. char channel, int lun, u_int tag,
  187. role_t role, uint32_t status,
  188. ahd_search_action action,
  189. u_int *list_head, u_int *list_tail,
  190. u_int tid);
  191. static void ahd_stitch_tid_list(struct ahd_softc *ahd,
  192. u_int tid_prev, u_int tid_cur,
  193. u_int tid_next);
  194. static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
  195. u_int scbid);
  196. static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  197. u_int prev, u_int next, u_int tid);
  198. static void ahd_reset_current_bus(struct ahd_softc *ahd);
  199. static ahd_callback_t ahd_reset_poll;
  200. static ahd_callback_t ahd_stat_timer;
  201. #ifdef AHD_DUMP_SEQ
  202. static void ahd_dumpseq(struct ahd_softc *ahd);
  203. #endif
  204. static void ahd_loadseq(struct ahd_softc *ahd);
  205. static int ahd_check_patch(struct ahd_softc *ahd,
  206. struct patch **start_patch,
  207. u_int start_instr, u_int *skip_addr);
  208. static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
  209. u_int address);
  210. static void ahd_download_instr(struct ahd_softc *ahd,
  211. u_int instrptr, uint8_t *dconsts);
  212. static int ahd_probe_stack_size(struct ahd_softc *ahd);
  213. static int ahd_scb_active_in_fifo(struct ahd_softc *ahd,
  214. struct scb *scb);
  215. static void ahd_run_data_fifo(struct ahd_softc *ahd,
  216. struct scb *scb);
  217. #ifdef AHD_TARGET_MODE
  218. static void ahd_queue_lstate_event(struct ahd_softc *ahd,
  219. struct ahd_tmode_lstate *lstate,
  220. u_int initiator_id,
  221. u_int event_type,
  222. u_int event_arg);
  223. static void ahd_update_scsiid(struct ahd_softc *ahd,
  224. u_int targid_mask);
  225. static int ahd_handle_target_cmd(struct ahd_softc *ahd,
  226. struct target_cmd *cmd);
  227. #endif
  228. /******************************** Private Inlines *****************************/
  229. static __inline void ahd_assert_atn(struct ahd_softc *ahd);
  230. static __inline int ahd_currently_packetized(struct ahd_softc *ahd);
  231. static __inline int ahd_set_active_fifo(struct ahd_softc *ahd);
  232. static __inline void
  233. ahd_assert_atn(struct ahd_softc *ahd)
  234. {
  235. ahd_outb(ahd, SCSISIGO, ATNO);
  236. }
  237. /*
  238. * Determine if the current connection has a packetized
  239. * agreement. This does not necessarily mean that we
  240. * are currently in a packetized transfer. We could
  241. * just as easily be sending or receiving a message.
  242. */
  243. static __inline int
  244. ahd_currently_packetized(struct ahd_softc *ahd)
  245. {
  246. ahd_mode_state saved_modes;
  247. int packetized;
  248. saved_modes = ahd_save_modes(ahd);
  249. if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
  250. /*
  251. * The packetized bit refers to the last
  252. * connection, not the current one. Check
  253. * for non-zero LQISTATE instead.
  254. */
  255. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  256. packetized = ahd_inb(ahd, LQISTATE) != 0;
  257. } else {
  258. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  259. packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
  260. }
  261. ahd_restore_modes(ahd, saved_modes);
  262. return (packetized);
  263. }
  264. static __inline int
  265. ahd_set_active_fifo(struct ahd_softc *ahd)
  266. {
  267. u_int active_fifo;
  268. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  269. active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  270. switch (active_fifo) {
  271. case 0:
  272. case 1:
  273. ahd_set_modes(ahd, active_fifo, active_fifo);
  274. return (1);
  275. default:
  276. return (0);
  277. }
  278. }
  279. /************************* Sequencer Execution Control ************************/
  280. /*
  281. * Restart the sequencer program from address zero
  282. */
  283. void
  284. ahd_restart(struct ahd_softc *ahd)
  285. {
  286. ahd_pause(ahd);
  287. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  288. /* No more pending messages */
  289. ahd_clear_msg_state(ahd);
  290. ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
  291. ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
  292. ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
  293. ahd_outb(ahd, SEQINTCTL, 0);
  294. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  295. ahd_outb(ahd, SEQ_FLAGS, 0);
  296. ahd_outb(ahd, SAVED_SCSIID, 0xFF);
  297. ahd_outb(ahd, SAVED_LUN, 0xFF);
  298. /*
  299. * Ensure that the sequencer's idea of TQINPOS
  300. * matches our own. The sequencer increments TQINPOS
  301. * only after it sees a DMA complete and a reset could
  302. * occur before the increment leaving the kernel to believe
  303. * the command arrived but the sequencer to not.
  304. */
  305. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  306. /* Always allow reselection */
  307. ahd_outb(ahd, SCSISEQ1,
  308. ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  309. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  310. /*
  311. * Clear any pending sequencer interrupt. It is no
  312. * longer relevant since we're resetting the Program
  313. * Counter.
  314. */
  315. ahd_outb(ahd, CLRINT, CLRSEQINT);
  316. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  317. ahd_unpause(ahd);
  318. }
  319. void
  320. ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
  321. {
  322. ahd_mode_state saved_modes;
  323. #ifdef AHD_DEBUG
  324. if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
  325. printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
  326. #endif
  327. saved_modes = ahd_save_modes(ahd);
  328. ahd_set_modes(ahd, fifo, fifo);
  329. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  330. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  331. ahd_outb(ahd, CCSGCTL, CCSGRESET);
  332. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  333. ahd_outb(ahd, SG_STATE, 0);
  334. ahd_restore_modes(ahd, saved_modes);
  335. }
  336. /************************* Input/Output Queues ********************************/
  337. /*
  338. * Flush and completed commands that are sitting in the command
  339. * complete queues down on the chip but have yet to be dma'ed back up.
  340. */
  341. void
  342. ahd_flush_qoutfifo(struct ahd_softc *ahd)
  343. {
  344. struct scb *scb;
  345. ahd_mode_state saved_modes;
  346. u_int saved_scbptr;
  347. u_int ccscbctl;
  348. u_int scbid;
  349. u_int next_scbid;
  350. saved_modes = ahd_save_modes(ahd);
  351. /*
  352. * Flush the good status FIFO for completed packetized commands.
  353. */
  354. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  355. saved_scbptr = ahd_get_scbptr(ahd);
  356. while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
  357. u_int fifo_mode;
  358. u_int i;
  359. scbid = ahd_inw(ahd, GSFIFO);
  360. scb = ahd_lookup_scb(ahd, scbid);
  361. if (scb == NULL) {
  362. printf("%s: Warning - GSFIFO SCB %d invalid\n",
  363. ahd_name(ahd), scbid);
  364. continue;
  365. }
  366. /*
  367. * Determine if this transaction is still active in
  368. * any FIFO. If it is, we must flush that FIFO to
  369. * the host before completing the command.
  370. */
  371. fifo_mode = 0;
  372. rescan_fifos:
  373. for (i = 0; i < 2; i++) {
  374. /* Toggle to the other mode. */
  375. fifo_mode ^= 1;
  376. ahd_set_modes(ahd, fifo_mode, fifo_mode);
  377. if (ahd_scb_active_in_fifo(ahd, scb) == 0)
  378. continue;
  379. ahd_run_data_fifo(ahd, scb);
  380. /*
  381. * Running this FIFO may cause a CFG4DATA for
  382. * this same transaction to assert in the other
  383. * FIFO or a new snapshot SAVEPTRS interrupt
  384. * in this FIFO. Even running a FIFO may not
  385. * clear the transaction if we are still waiting
  386. * for data to drain to the host. We must loop
  387. * until the transaction is not active in either
  388. * FIFO just to be sure. Reset our loop counter
  389. * so we will visit both FIFOs again before
  390. * declaring this transaction finished. We
  391. * also delay a bit so that status has a chance
  392. * to change before we look at this FIFO again.
  393. */
  394. ahd_delay(200);
  395. goto rescan_fifos;
  396. }
  397. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  398. ahd_set_scbptr(ahd, scbid);
  399. if ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_LIST_NULL) == 0
  400. && ((ahd_inb_scbram(ahd, SCB_SGPTR) & SG_FULL_RESID) != 0
  401. || (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR)
  402. & SG_LIST_NULL) != 0)) {
  403. u_int comp_head;
  404. /*
  405. * The transfer completed with a residual.
  406. * Place this SCB on the complete DMA list
  407. * so that we update our in-core copy of the
  408. * SCB before completing the command.
  409. */
  410. ahd_outb(ahd, SCB_SCSI_STATUS, 0);
  411. ahd_outb(ahd, SCB_SGPTR,
  412. ahd_inb_scbram(ahd, SCB_SGPTR)
  413. | SG_STATUS_VALID);
  414. ahd_outw(ahd, SCB_TAG, scbid);
  415. ahd_outw(ahd, SCB_NEXT_COMPLETE, SCB_LIST_NULL);
  416. comp_head = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  417. if (SCBID_IS_NULL(comp_head)) {
  418. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, scbid);
  419. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
  420. } else {
  421. u_int tail;
  422. tail = ahd_inw(ahd, COMPLETE_DMA_SCB_TAIL);
  423. ahd_set_scbptr(ahd, tail);
  424. ahd_outw(ahd, SCB_NEXT_COMPLETE, scbid);
  425. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, scbid);
  426. ahd_set_scbptr(ahd, scbid);
  427. }
  428. } else
  429. ahd_complete_scb(ahd, scb);
  430. }
  431. ahd_set_scbptr(ahd, saved_scbptr);
  432. /*
  433. * Setup for command channel portion of flush.
  434. */
  435. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  436. /*
  437. * Wait for any inprogress DMA to complete and clear DMA state
  438. * if this if for an SCB in the qinfifo.
  439. */
  440. while (((ccscbctl = ahd_inb(ahd, CCSCBCTL)) & (CCARREN|CCSCBEN)) != 0) {
  441. if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
  442. if ((ccscbctl & ARRDONE) != 0)
  443. break;
  444. } else if ((ccscbctl & CCSCBDONE) != 0)
  445. break;
  446. ahd_delay(200);
  447. }
  448. /*
  449. * We leave the sequencer to cleanup in the case of DMA's to
  450. * update the qoutfifo. In all other cases (DMA's to the
  451. * chip or a push of an SCB from the COMPLETE_DMA_SCB list),
  452. * we disable the DMA engine so that the sequencer will not
  453. * attempt to handle the DMA completion.
  454. */
  455. if ((ccscbctl & CCSCBDIR) != 0 || (ccscbctl & ARRDONE) != 0)
  456. ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
  457. /*
  458. * Complete any SCBs that just finished
  459. * being DMA'ed into the qoutfifo.
  460. */
  461. ahd_run_qoutfifo(ahd);
  462. saved_scbptr = ahd_get_scbptr(ahd);
  463. /*
  464. * Manually update/complete any completed SCBs that are waiting to be
  465. * DMA'ed back up to the host.
  466. */
  467. scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  468. while (!SCBID_IS_NULL(scbid)) {
  469. uint8_t *hscb_ptr;
  470. u_int i;
  471. ahd_set_scbptr(ahd, scbid);
  472. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  473. scb = ahd_lookup_scb(ahd, scbid);
  474. if (scb == NULL) {
  475. printf("%s: Warning - DMA-up and complete "
  476. "SCB %d invalid\n", ahd_name(ahd), scbid);
  477. continue;
  478. }
  479. hscb_ptr = (uint8_t *)scb->hscb;
  480. for (i = 0; i < sizeof(struct hardware_scb); i++)
  481. *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
  482. ahd_complete_scb(ahd, scb);
  483. scbid = next_scbid;
  484. }
  485. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  486. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
  487. scbid = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
  488. while (!SCBID_IS_NULL(scbid)) {
  489. ahd_set_scbptr(ahd, scbid);
  490. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  491. scb = ahd_lookup_scb(ahd, scbid);
  492. if (scb == NULL) {
  493. printf("%s: Warning - Complete Qfrz SCB %d invalid\n",
  494. ahd_name(ahd), scbid);
  495. continue;
  496. }
  497. ahd_complete_scb(ahd, scb);
  498. scbid = next_scbid;
  499. }
  500. ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
  501. scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  502. while (!SCBID_IS_NULL(scbid)) {
  503. ahd_set_scbptr(ahd, scbid);
  504. next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  505. scb = ahd_lookup_scb(ahd, scbid);
  506. if (scb == NULL) {
  507. printf("%s: Warning - Complete SCB %d invalid\n",
  508. ahd_name(ahd), scbid);
  509. continue;
  510. }
  511. ahd_complete_scb(ahd, scb);
  512. scbid = next_scbid;
  513. }
  514. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  515. /*
  516. * Restore state.
  517. */
  518. ahd_set_scbptr(ahd, saved_scbptr);
  519. ahd_restore_modes(ahd, saved_modes);
  520. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  521. }
  522. /*
  523. * Determine if an SCB for a packetized transaction
  524. * is active in a FIFO.
  525. */
  526. static int
  527. ahd_scb_active_in_fifo(struct ahd_softc *ahd, struct scb *scb)
  528. {
  529. /*
  530. * The FIFO is only active for our transaction if
  531. * the SCBPTR matches the SCB's ID and the firmware
  532. * has installed a handler for the FIFO or we have
  533. * a pending SAVEPTRS or CFG4DATA interrupt.
  534. */
  535. if (ahd_get_scbptr(ahd) != SCB_GET_TAG(scb)
  536. || ((ahd_inb(ahd, LONGJMP_ADDR+1) & INVALID_ADDR) != 0
  537. && (ahd_inb(ahd, SEQINTSRC) & (CFG4DATA|SAVEPTRS)) == 0))
  538. return (0);
  539. return (1);
  540. }
  541. /*
  542. * Run a data fifo to completion for a transaction we know
  543. * has completed across the SCSI bus (good status has been
  544. * received). We are already set to the correct FIFO mode
  545. * on entry to this routine.
  546. *
  547. * This function attempts to operate exactly as the firmware
  548. * would when running this FIFO. Care must be taken to update
  549. * this routine any time the firmware's FIFO algorithm is
  550. * changed.
  551. */
  552. static void
  553. ahd_run_data_fifo(struct ahd_softc *ahd, struct scb *scb)
  554. {
  555. u_int seqintsrc;
  556. seqintsrc = ahd_inb(ahd, SEQINTSRC);
  557. if ((seqintsrc & CFG4DATA) != 0) {
  558. uint32_t datacnt;
  559. uint32_t sgptr;
  560. /*
  561. * Clear full residual flag.
  562. */
  563. sgptr = ahd_inl_scbram(ahd, SCB_SGPTR) & ~SG_FULL_RESID;
  564. ahd_outb(ahd, SCB_SGPTR, sgptr);
  565. /*
  566. * Load datacnt and address.
  567. */
  568. datacnt = ahd_inl_scbram(ahd, SCB_DATACNT);
  569. if ((datacnt & AHD_DMA_LAST_SEG) != 0) {
  570. sgptr |= LAST_SEG;
  571. ahd_outb(ahd, SG_STATE, 0);
  572. } else
  573. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  574. ahd_outq(ahd, HADDR, ahd_inq_scbram(ahd, SCB_DATAPTR));
  575. ahd_outl(ahd, HCNT, datacnt & AHD_SG_LEN_MASK);
  576. ahd_outb(ahd, SG_CACHE_PRE, sgptr);
  577. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  578. /*
  579. * Initialize Residual Fields.
  580. */
  581. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, datacnt >> 24);
  582. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr & SG_PTR_MASK);
  583. /*
  584. * Mark the SCB as having a FIFO in use.
  585. */
  586. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  587. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) + 1);
  588. /*
  589. * Install a "fake" handler for this FIFO.
  590. */
  591. ahd_outw(ahd, LONGJMP_ADDR, 0);
  592. /*
  593. * Notify the hardware that we have satisfied
  594. * this sequencer interrupt.
  595. */
  596. ahd_outb(ahd, CLRSEQINTSRC, CLRCFG4DATA);
  597. } else if ((seqintsrc & SAVEPTRS) != 0) {
  598. uint32_t sgptr;
  599. uint32_t resid;
  600. if ((ahd_inb(ahd, LONGJMP_ADDR+1)&INVALID_ADDR) != 0) {
  601. /*
  602. * Snapshot Save Pointers. All that
  603. * is necessary to clear the snapshot
  604. * is a CLRCHN.
  605. */
  606. goto clrchn;
  607. }
  608. /*
  609. * Disable S/G fetch so the DMA engine
  610. * is available to future users.
  611. */
  612. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
  613. ahd_outb(ahd, CCSGCTL, 0);
  614. ahd_outb(ahd, SG_STATE, 0);
  615. /*
  616. * Flush the data FIFO. Strickly only
  617. * necessary for Rev A parts.
  618. */
  619. ahd_outb(ahd, DFCNTRL, ahd_inb(ahd, DFCNTRL) | FIFOFLUSH);
  620. /*
  621. * Calculate residual.
  622. */
  623. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  624. resid = ahd_inl(ahd, SHCNT);
  625. resid |= ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24;
  626. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, resid);
  627. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG) == 0) {
  628. /*
  629. * Must back up to the correct S/G element.
  630. * Typically this just means resetting our
  631. * low byte to the offset in the SG_CACHE,
  632. * but if we wrapped, we have to correct
  633. * the other bytes of the sgptr too.
  634. */
  635. if ((ahd_inb(ahd, SG_CACHE_SHADOW) & 0x80) != 0
  636. && (sgptr & 0x80) == 0)
  637. sgptr -= 0x100;
  638. sgptr &= ~0xFF;
  639. sgptr |= ahd_inb(ahd, SG_CACHE_SHADOW)
  640. & SG_ADDR_MASK;
  641. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  642. ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, 0);
  643. } else if ((resid & AHD_SG_LEN_MASK) == 0) {
  644. ahd_outb(ahd, SCB_RESIDUAL_SGPTR,
  645. sgptr | SG_LIST_NULL);
  646. }
  647. /*
  648. * Save Pointers.
  649. */
  650. ahd_outq(ahd, SCB_DATAPTR, ahd_inq(ahd, SHADDR));
  651. ahd_outl(ahd, SCB_DATACNT, resid);
  652. ahd_outl(ahd, SCB_SGPTR, sgptr);
  653. ahd_outb(ahd, CLRSEQINTSRC, CLRSAVEPTRS);
  654. ahd_outb(ahd, SEQIMODE,
  655. ahd_inb(ahd, SEQIMODE) | ENSAVEPTRS);
  656. /*
  657. * If the data is to the SCSI bus, we are
  658. * done, otherwise wait for FIFOEMP.
  659. */
  660. if ((ahd_inb(ahd, DFCNTRL) & DIRECTION) != 0)
  661. goto clrchn;
  662. } else if ((ahd_inb(ahd, SG_STATE) & LOADING_NEEDED) != 0) {
  663. uint32_t sgptr;
  664. uint64_t data_addr;
  665. uint32_t data_len;
  666. u_int dfcntrl;
  667. /*
  668. * Disable S/G fetch so the DMA engine
  669. * is available to future users. We won't
  670. * be using the DMA engine to load segments.
  671. */
  672. if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0) {
  673. ahd_outb(ahd, CCSGCTL, 0);
  674. ahd_outb(ahd, SG_STATE, LOADING_NEEDED);
  675. }
  676. /*
  677. * Wait for the DMA engine to notice that the
  678. * host transfer is enabled and that there is
  679. * space in the S/G FIFO for new segments before
  680. * loading more segments.
  681. */
  682. if ((ahd_inb(ahd, DFSTATUS) & PRELOAD_AVAIL) != 0
  683. && (ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0) {
  684. /*
  685. * Determine the offset of the next S/G
  686. * element to load.
  687. */
  688. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  689. sgptr &= SG_PTR_MASK;
  690. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  691. struct ahd_dma64_seg *sg;
  692. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  693. data_addr = sg->addr;
  694. data_len = sg->len;
  695. sgptr += sizeof(*sg);
  696. } else {
  697. struct ahd_dma_seg *sg;
  698. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  699. data_addr = sg->len & AHD_SG_HIGH_ADDR_MASK;
  700. data_addr <<= 8;
  701. data_addr |= sg->addr;
  702. data_len = sg->len;
  703. sgptr += sizeof(*sg);
  704. }
  705. /*
  706. * Update residual information.
  707. */
  708. ahd_outb(ahd, SCB_RESIDUAL_DATACNT+3, data_len >> 24);
  709. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  710. /*
  711. * Load the S/G.
  712. */
  713. if (data_len & AHD_DMA_LAST_SEG) {
  714. sgptr |= LAST_SEG;
  715. ahd_outb(ahd, SG_STATE, 0);
  716. }
  717. ahd_outq(ahd, HADDR, data_addr);
  718. ahd_outl(ahd, HCNT, data_len & AHD_SG_LEN_MASK);
  719. ahd_outb(ahd, SG_CACHE_PRE, sgptr & 0xFF);
  720. /*
  721. * Advertise the segment to the hardware.
  722. */
  723. dfcntrl = ahd_inb(ahd, DFCNTRL)|PRELOADEN|HDMAEN;
  724. if ((ahd->features & AHD_NEW_DFCNTRL_OPTS) != 0) {
  725. /*
  726. * Use SCSIENWRDIS so that SCSIEN
  727. * is never modified by this
  728. * operation.
  729. */
  730. dfcntrl |= SCSIENWRDIS;
  731. }
  732. ahd_outb(ahd, DFCNTRL, dfcntrl);
  733. }
  734. } else if ((ahd_inb(ahd, SG_CACHE_SHADOW) & LAST_SEG_DONE) != 0) {
  735. /*
  736. * Transfer completed to the end of SG list
  737. * and has flushed to the host.
  738. */
  739. ahd_outb(ahd, SCB_SGPTR,
  740. ahd_inb_scbram(ahd, SCB_SGPTR) | SG_LIST_NULL);
  741. goto clrchn;
  742. } else if ((ahd_inb(ahd, DFSTATUS) & FIFOEMP) != 0) {
  743. clrchn:
  744. /*
  745. * Clear any handler for this FIFO, decrement
  746. * the FIFO use count for the SCB, and release
  747. * the FIFO.
  748. */
  749. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  750. ahd_outb(ahd, SCB_FIFO_USE_COUNT,
  751. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT) - 1);
  752. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  753. }
  754. }
  755. /*
  756. * Look for entries in the QoutFIFO that have completed.
  757. * The valid_tag completion field indicates the validity
  758. * of the entry - the valid value toggles each time through
  759. * the queue. We use the sg_status field in the completion
  760. * entry to avoid referencing the hscb if the completion
  761. * occurred with no errors and no residual. sg_status is
  762. * a copy of the first byte (little endian) of the sgptr
  763. * hscb field.
  764. */
  765. void
  766. ahd_run_qoutfifo(struct ahd_softc *ahd)
  767. {
  768. struct ahd_completion *completion;
  769. struct scb *scb;
  770. u_int scb_index;
  771. if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
  772. panic("ahd_run_qoutfifo recursion");
  773. ahd->flags |= AHD_RUNNING_QOUTFIFO;
  774. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
  775. for (;;) {
  776. completion = &ahd->qoutfifo[ahd->qoutfifonext];
  777. if (completion->valid_tag != ahd->qoutfifonext_valid_tag)
  778. break;
  779. scb_index = ahd_le16toh(completion->tag);
  780. scb = ahd_lookup_scb(ahd, scb_index);
  781. if (scb == NULL) {
  782. printf("%s: WARNING no command for scb %d "
  783. "(cmdcmplt)\nQOUTPOS = %d\n",
  784. ahd_name(ahd), scb_index,
  785. ahd->qoutfifonext);
  786. ahd_dump_card_state(ahd);
  787. } else if ((completion->sg_status & SG_STATUS_VALID) != 0) {
  788. ahd_handle_scb_status(ahd, scb);
  789. } else {
  790. ahd_done(ahd, scb);
  791. }
  792. ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
  793. if (ahd->qoutfifonext == 0)
  794. ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID;
  795. }
  796. ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
  797. }
  798. /************************* Interrupt Handling *********************************/
  799. void
  800. ahd_handle_hwerrint(struct ahd_softc *ahd)
  801. {
  802. /*
  803. * Some catastrophic hardware error has occurred.
  804. * Print it for the user and disable the controller.
  805. */
  806. int i;
  807. int error;
  808. error = ahd_inb(ahd, ERROR);
  809. for (i = 0; i < num_errors; i++) {
  810. if ((error & ahd_hard_errors[i].errno) != 0)
  811. printf("%s: hwerrint, %s\n",
  812. ahd_name(ahd), ahd_hard_errors[i].errmesg);
  813. }
  814. ahd_dump_card_state(ahd);
  815. panic("BRKADRINT");
  816. /* Tell everyone that this HBA is no longer available */
  817. ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  818. CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  819. CAM_NO_HBA);
  820. /* Tell the system that this controller has gone away. */
  821. ahd_free(ahd);
  822. }
  823. void
  824. ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
  825. {
  826. u_int seqintcode;
  827. /*
  828. * Save the sequencer interrupt code and clear the SEQINT
  829. * bit. We will unpause the sequencer, if appropriate,
  830. * after servicing the request.
  831. */
  832. seqintcode = ahd_inb(ahd, SEQINTCODE);
  833. ahd_outb(ahd, CLRINT, CLRSEQINT);
  834. if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
  835. /*
  836. * Unpause the sequencer and let it clear
  837. * SEQINT by writing NO_SEQINT to it. This
  838. * will cause the sequencer to be paused again,
  839. * which is the expected state of this routine.
  840. */
  841. ahd_unpause(ahd);
  842. while (!ahd_is_paused(ahd))
  843. ;
  844. ahd_outb(ahd, CLRINT, CLRSEQINT);
  845. }
  846. ahd_update_modes(ahd);
  847. #ifdef AHD_DEBUG
  848. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  849. printf("%s: Handle Seqint Called for code %d\n",
  850. ahd_name(ahd), seqintcode);
  851. #endif
  852. switch (seqintcode) {
  853. case ENTERING_NONPACK:
  854. {
  855. struct scb *scb;
  856. u_int scbid;
  857. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  858. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  859. scbid = ahd_get_scbptr(ahd);
  860. scb = ahd_lookup_scb(ahd, scbid);
  861. if (scb == NULL) {
  862. /*
  863. * Somehow need to know if this
  864. * is from a selection or reselection.
  865. * From that, we can determine target
  866. * ID so we at least have an I_T nexus.
  867. */
  868. } else {
  869. ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
  870. ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
  871. ahd_outb(ahd, SEQ_FLAGS, 0x0);
  872. }
  873. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
  874. && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  875. /*
  876. * Phase change after read stream with
  877. * CRC error with P0 asserted on last
  878. * packet.
  879. */
  880. #ifdef AHD_DEBUG
  881. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  882. printf("%s: Assuming LQIPHASE_NLQ with "
  883. "P0 assertion\n", ahd_name(ahd));
  884. #endif
  885. }
  886. #ifdef AHD_DEBUG
  887. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
  888. printf("%s: Entering NONPACK\n", ahd_name(ahd));
  889. #endif
  890. break;
  891. }
  892. case INVALID_SEQINT:
  893. printf("%s: Invalid Sequencer interrupt occurred.\n",
  894. ahd_name(ahd));
  895. ahd_dump_card_state(ahd);
  896. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  897. break;
  898. case STATUS_OVERRUN:
  899. {
  900. struct scb *scb;
  901. u_int scbid;
  902. scbid = ahd_get_scbptr(ahd);
  903. scb = ahd_lookup_scb(ahd, scbid);
  904. if (scb != NULL)
  905. ahd_print_path(ahd, scb);
  906. else
  907. printf("%s: ", ahd_name(ahd));
  908. printf("SCB %d Packetized Status Overrun", scbid);
  909. ahd_dump_card_state(ahd);
  910. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  911. break;
  912. }
  913. case CFG4ISTAT_INTR:
  914. {
  915. struct scb *scb;
  916. u_int scbid;
  917. scbid = ahd_get_scbptr(ahd);
  918. scb = ahd_lookup_scb(ahd, scbid);
  919. if (scb == NULL) {
  920. ahd_dump_card_state(ahd);
  921. printf("CFG4ISTAT: Free SCB %d referenced", scbid);
  922. panic("For safety");
  923. }
  924. ahd_outq(ahd, HADDR, scb->sense_busaddr);
  925. ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
  926. ahd_outb(ahd, HCNT + 2, 0);
  927. ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
  928. ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
  929. break;
  930. }
  931. case ILLEGAL_PHASE:
  932. {
  933. u_int bus_phase;
  934. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  935. printf("%s: ILLEGAL_PHASE 0x%x\n",
  936. ahd_name(ahd), bus_phase);
  937. switch (bus_phase) {
  938. case P_DATAOUT:
  939. case P_DATAIN:
  940. case P_DATAOUT_DT:
  941. case P_DATAIN_DT:
  942. case P_MESGOUT:
  943. case P_STATUS:
  944. case P_MESGIN:
  945. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  946. printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
  947. break;
  948. case P_COMMAND:
  949. {
  950. struct ahd_devinfo devinfo;
  951. struct scb *scb;
  952. struct ahd_initiator_tinfo *targ_info;
  953. struct ahd_tmode_tstate *tstate;
  954. struct ahd_transinfo *tinfo;
  955. u_int scbid;
  956. /*
  957. * If a target takes us into the command phase
  958. * assume that it has been externally reset and
  959. * has thus lost our previous packetized negotiation
  960. * agreement. Since we have not sent an identify
  961. * message and may not have fully qualified the
  962. * connection, we change our command to TUR, assert
  963. * ATN and ABORT the task when we go to message in
  964. * phase. The OSM will see the REQUEUE_REQUEST
  965. * status and retry the command.
  966. */
  967. scbid = ahd_get_scbptr(ahd);
  968. scb = ahd_lookup_scb(ahd, scbid);
  969. if (scb == NULL) {
  970. printf("Invalid phase with no valid SCB. "
  971. "Resetting bus.\n");
  972. ahd_reset_channel(ahd, 'A',
  973. /*Initiate Reset*/TRUE);
  974. break;
  975. }
  976. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  977. SCB_GET_TARGET(ahd, scb),
  978. SCB_GET_LUN(scb),
  979. SCB_GET_CHANNEL(ahd, scb),
  980. ROLE_INITIATOR);
  981. targ_info = ahd_fetch_transinfo(ahd,
  982. devinfo.channel,
  983. devinfo.our_scsiid,
  984. devinfo.target,
  985. &tstate);
  986. tinfo = &targ_info->curr;
  987. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  988. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  989. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  990. /*offset*/0, /*ppr_options*/0,
  991. AHD_TRANS_ACTIVE, /*paused*/TRUE);
  992. ahd_outb(ahd, SCB_CDB_STORE, 0);
  993. ahd_outb(ahd, SCB_CDB_STORE+1, 0);
  994. ahd_outb(ahd, SCB_CDB_STORE+2, 0);
  995. ahd_outb(ahd, SCB_CDB_STORE+3, 0);
  996. ahd_outb(ahd, SCB_CDB_STORE+4, 0);
  997. ahd_outb(ahd, SCB_CDB_STORE+5, 0);
  998. ahd_outb(ahd, SCB_CDB_LEN, 6);
  999. scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
  1000. scb->hscb->control |= MK_MESSAGE;
  1001. ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
  1002. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1003. ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
  1004. /*
  1005. * The lun is 0, regardless of the SCB's lun
  1006. * as we have not sent an identify message.
  1007. */
  1008. ahd_outb(ahd, SAVED_LUN, 0);
  1009. ahd_outb(ahd, SEQ_FLAGS, 0);
  1010. ahd_assert_atn(ahd);
  1011. scb->flags &= ~SCB_PACKETIZED;
  1012. scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
  1013. ahd_freeze_devq(ahd, scb);
  1014. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  1015. ahd_freeze_scb(scb);
  1016. /*
  1017. * Allow the sequencer to continue with
  1018. * non-pack processing.
  1019. */
  1020. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1021. ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
  1022. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  1023. ahd_outb(ahd, CLRLQOINT1, 0);
  1024. }
  1025. #ifdef AHD_DEBUG
  1026. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1027. ahd_print_path(ahd, scb);
  1028. printf("Unexpected command phase from "
  1029. "packetized target\n");
  1030. }
  1031. #endif
  1032. break;
  1033. }
  1034. }
  1035. break;
  1036. }
  1037. case CFG4OVERRUN:
  1038. {
  1039. struct scb *scb;
  1040. u_int scb_index;
  1041. #ifdef AHD_DEBUG
  1042. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1043. printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
  1044. ahd_inb(ahd, MODE_PTR));
  1045. }
  1046. #endif
  1047. scb_index = ahd_get_scbptr(ahd);
  1048. scb = ahd_lookup_scb(ahd, scb_index);
  1049. if (scb == NULL) {
  1050. /*
  1051. * Attempt to transfer to an SCB that is
  1052. * not outstanding.
  1053. */
  1054. ahd_assert_atn(ahd);
  1055. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1056. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  1057. ahd->msgout_len = 1;
  1058. ahd->msgout_index = 0;
  1059. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1060. /*
  1061. * Clear status received flag to prevent any
  1062. * attempt to complete this bogus SCB.
  1063. */
  1064. ahd_outb(ahd, SCB_CONTROL,
  1065. ahd_inb_scbram(ahd, SCB_CONTROL)
  1066. & ~STATUS_RCVD);
  1067. }
  1068. break;
  1069. }
  1070. case DUMP_CARD_STATE:
  1071. {
  1072. ahd_dump_card_state(ahd);
  1073. break;
  1074. }
  1075. case PDATA_REINIT:
  1076. {
  1077. #ifdef AHD_DEBUG
  1078. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1079. printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
  1080. "SG_CACHE_SHADOW = 0x%x\n",
  1081. ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
  1082. ahd_inb(ahd, SG_CACHE_SHADOW));
  1083. }
  1084. #endif
  1085. ahd_reinitialize_dataptrs(ahd);
  1086. break;
  1087. }
  1088. case HOST_MSG_LOOP:
  1089. {
  1090. struct ahd_devinfo devinfo;
  1091. /*
  1092. * The sequencer has encountered a message phase
  1093. * that requires host assistance for completion.
  1094. * While handling the message phase(s), we will be
  1095. * notified by the sequencer after each byte is
  1096. * transfered so we can track bus phase changes.
  1097. *
  1098. * If this is the first time we've seen a HOST_MSG_LOOP
  1099. * interrupt, initialize the state of the host message
  1100. * loop.
  1101. */
  1102. ahd_fetch_devinfo(ahd, &devinfo);
  1103. if (ahd->msg_type == MSG_TYPE_NONE) {
  1104. struct scb *scb;
  1105. u_int scb_index;
  1106. u_int bus_phase;
  1107. bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1108. if (bus_phase != P_MESGIN
  1109. && bus_phase != P_MESGOUT) {
  1110. printf("ahd_intr: HOST_MSG_LOOP bad "
  1111. "phase 0x%x\n", bus_phase);
  1112. /*
  1113. * Probably transitioned to bus free before
  1114. * we got here. Just punt the message.
  1115. */
  1116. ahd_dump_card_state(ahd);
  1117. ahd_clear_intstat(ahd);
  1118. ahd_restart(ahd);
  1119. return;
  1120. }
  1121. scb_index = ahd_get_scbptr(ahd);
  1122. scb = ahd_lookup_scb(ahd, scb_index);
  1123. if (devinfo.role == ROLE_INITIATOR) {
  1124. if (bus_phase == P_MESGOUT)
  1125. ahd_setup_initiator_msgout(ahd,
  1126. &devinfo,
  1127. scb);
  1128. else {
  1129. ahd->msg_type =
  1130. MSG_TYPE_INITIATOR_MSGIN;
  1131. ahd->msgin_index = 0;
  1132. }
  1133. }
  1134. #ifdef AHD_TARGET_MODE
  1135. else {
  1136. if (bus_phase == P_MESGOUT) {
  1137. ahd->msg_type =
  1138. MSG_TYPE_TARGET_MSGOUT;
  1139. ahd->msgin_index = 0;
  1140. }
  1141. else
  1142. ahd_setup_target_msgin(ahd,
  1143. &devinfo,
  1144. scb);
  1145. }
  1146. #endif
  1147. }
  1148. ahd_handle_message_phase(ahd);
  1149. break;
  1150. }
  1151. case NO_MATCH:
  1152. {
  1153. /* Ensure we don't leave the selection hardware on */
  1154. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  1155. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1156. printf("%s:%c:%d: no active SCB for reconnecting "
  1157. "target - issuing BUS DEVICE RESET\n",
  1158. ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
  1159. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  1160. "REG0 == 0x%x ACCUM = 0x%x\n",
  1161. ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
  1162. ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
  1163. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  1164. "SINDEX == 0x%x\n",
  1165. ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
  1166. ahd_find_busy_tcl(ahd,
  1167. BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
  1168. ahd_inb(ahd, SAVED_LUN))),
  1169. ahd_inw(ahd, SINDEX));
  1170. printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  1171. "SCB_CONTROL == 0x%x\n",
  1172. ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
  1173. ahd_inb_scbram(ahd, SCB_LUN),
  1174. ahd_inb_scbram(ahd, SCB_CONTROL));
  1175. printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
  1176. ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
  1177. printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
  1178. printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
  1179. ahd_dump_card_state(ahd);
  1180. ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
  1181. ahd->msgout_len = 1;
  1182. ahd->msgout_index = 0;
  1183. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1184. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1185. ahd_assert_atn(ahd);
  1186. break;
  1187. }
  1188. case PROTO_VIOLATION:
  1189. {
  1190. ahd_handle_proto_violation(ahd);
  1191. break;
  1192. }
  1193. case IGN_WIDE_RES:
  1194. {
  1195. struct ahd_devinfo devinfo;
  1196. ahd_fetch_devinfo(ahd, &devinfo);
  1197. ahd_handle_ign_wide_residue(ahd, &devinfo);
  1198. break;
  1199. }
  1200. case BAD_PHASE:
  1201. {
  1202. u_int lastphase;
  1203. lastphase = ahd_inb(ahd, LASTPHASE);
  1204. printf("%s:%c:%d: unknown scsi bus phase %x, "
  1205. "lastphase = 0x%x. Attempting to continue\n",
  1206. ahd_name(ahd), 'A',
  1207. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1208. lastphase, ahd_inb(ahd, SCSISIGI));
  1209. break;
  1210. }
  1211. case MISSED_BUSFREE:
  1212. {
  1213. u_int lastphase;
  1214. lastphase = ahd_inb(ahd, LASTPHASE);
  1215. printf("%s:%c:%d: Missed busfree. "
  1216. "Lastphase = 0x%x, Curphase = 0x%x\n",
  1217. ahd_name(ahd), 'A',
  1218. SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
  1219. lastphase, ahd_inb(ahd, SCSISIGI));
  1220. ahd_restart(ahd);
  1221. return;
  1222. }
  1223. case DATA_OVERRUN:
  1224. {
  1225. /*
  1226. * When the sequencer detects an overrun, it
  1227. * places the controller in "BITBUCKET" mode
  1228. * and allows the target to complete its transfer.
  1229. * Unfortunately, none of the counters get updated
  1230. * when the controller is in this mode, so we have
  1231. * no way of knowing how large the overrun was.
  1232. */
  1233. struct scb *scb;
  1234. u_int scbindex;
  1235. #ifdef AHD_DEBUG
  1236. u_int lastphase;
  1237. #endif
  1238. scbindex = ahd_get_scbptr(ahd);
  1239. scb = ahd_lookup_scb(ahd, scbindex);
  1240. #ifdef AHD_DEBUG
  1241. lastphase = ahd_inb(ahd, LASTPHASE);
  1242. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1243. ahd_print_path(ahd, scb);
  1244. printf("data overrun detected %s. Tag == 0x%x.\n",
  1245. ahd_lookup_phase_entry(lastphase)->phasemsg,
  1246. SCB_GET_TAG(scb));
  1247. ahd_print_path(ahd, scb);
  1248. printf("%s seen Data Phase. Length = %ld. "
  1249. "NumSGs = %d.\n",
  1250. ahd_inb(ahd, SEQ_FLAGS) & DPHASE
  1251. ? "Have" : "Haven't",
  1252. ahd_get_transfer_length(scb), scb->sg_count);
  1253. ahd_dump_sglist(scb);
  1254. }
  1255. #endif
  1256. /*
  1257. * Set this and it will take effect when the
  1258. * target does a command complete.
  1259. */
  1260. ahd_freeze_devq(ahd, scb);
  1261. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1262. ahd_freeze_scb(scb);
  1263. break;
  1264. }
  1265. case MKMSG_FAILED:
  1266. {
  1267. struct ahd_devinfo devinfo;
  1268. struct scb *scb;
  1269. u_int scbid;
  1270. ahd_fetch_devinfo(ahd, &devinfo);
  1271. printf("%s:%c:%d:%d: Attempt to issue message failed\n",
  1272. ahd_name(ahd), devinfo.channel, devinfo.target,
  1273. devinfo.lun);
  1274. scbid = ahd_get_scbptr(ahd);
  1275. scb = ahd_lookup_scb(ahd, scbid);
  1276. if (scb != NULL
  1277. && (scb->flags & SCB_RECOVERY_SCB) != 0)
  1278. /*
  1279. * Ensure that we didn't put a second instance of this
  1280. * SCB into the QINFIFO.
  1281. */
  1282. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1283. SCB_GET_CHANNEL(ahd, scb),
  1284. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1285. ROLE_INITIATOR, /*status*/0,
  1286. SEARCH_REMOVE);
  1287. ahd_outb(ahd, SCB_CONTROL,
  1288. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  1289. break;
  1290. }
  1291. case TASKMGMT_FUNC_COMPLETE:
  1292. {
  1293. u_int scbid;
  1294. struct scb *scb;
  1295. scbid = ahd_get_scbptr(ahd);
  1296. scb = ahd_lookup_scb(ahd, scbid);
  1297. if (scb != NULL) {
  1298. u_int lun;
  1299. u_int tag;
  1300. cam_status error;
  1301. ahd_print_path(ahd, scb);
  1302. printf("Task Management Func 0x%x Complete\n",
  1303. scb->hscb->task_management);
  1304. lun = CAM_LUN_WILDCARD;
  1305. tag = SCB_LIST_NULL;
  1306. switch (scb->hscb->task_management) {
  1307. case SIU_TASKMGMT_ABORT_TASK:
  1308. tag = SCB_GET_TAG(scb);
  1309. case SIU_TASKMGMT_ABORT_TASK_SET:
  1310. case SIU_TASKMGMT_CLEAR_TASK_SET:
  1311. lun = scb->hscb->lun;
  1312. error = CAM_REQ_ABORTED;
  1313. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  1314. 'A', lun, tag, ROLE_INITIATOR,
  1315. error);
  1316. break;
  1317. case SIU_TASKMGMT_LUN_RESET:
  1318. lun = scb->hscb->lun;
  1319. case SIU_TASKMGMT_TARGET_RESET:
  1320. {
  1321. struct ahd_devinfo devinfo;
  1322. ahd_scb_devinfo(ahd, &devinfo, scb);
  1323. error = CAM_BDR_SENT;
  1324. ahd_handle_devreset(ahd, &devinfo, lun,
  1325. CAM_BDR_SENT,
  1326. lun != CAM_LUN_WILDCARD
  1327. ? "Lun Reset"
  1328. : "Target Reset",
  1329. /*verbose_level*/0);
  1330. break;
  1331. }
  1332. default:
  1333. panic("Unexpected TaskMgmt Func\n");
  1334. break;
  1335. }
  1336. }
  1337. break;
  1338. }
  1339. case TASKMGMT_CMD_CMPLT_OKAY:
  1340. {
  1341. u_int scbid;
  1342. struct scb *scb;
  1343. /*
  1344. * An ABORT TASK TMF failed to be delivered before
  1345. * the targeted command completed normally.
  1346. */
  1347. scbid = ahd_get_scbptr(ahd);
  1348. scb = ahd_lookup_scb(ahd, scbid);
  1349. if (scb != NULL) {
  1350. /*
  1351. * Remove the second instance of this SCB from
  1352. * the QINFIFO if it is still there.
  1353. */
  1354. ahd_print_path(ahd, scb);
  1355. printf("SCB completes before TMF\n");
  1356. /*
  1357. * Handle losing the race. Wait until any
  1358. * current selection completes. We will then
  1359. * set the TMF back to zero in this SCB so that
  1360. * the sequencer doesn't bother to issue another
  1361. * sequencer interrupt for its completion.
  1362. */
  1363. while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  1364. && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
  1365. && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
  1366. ;
  1367. ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
  1368. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  1369. SCB_GET_CHANNEL(ahd, scb),
  1370. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1371. ROLE_INITIATOR, /*status*/0,
  1372. SEARCH_REMOVE);
  1373. }
  1374. break;
  1375. }
  1376. case TRACEPOINT0:
  1377. case TRACEPOINT1:
  1378. case TRACEPOINT2:
  1379. case TRACEPOINT3:
  1380. printf("%s: Tracepoint %d\n", ahd_name(ahd),
  1381. seqintcode - TRACEPOINT0);
  1382. break;
  1383. case NO_SEQINT:
  1384. break;
  1385. case SAW_HWERR:
  1386. ahd_handle_hwerrint(ahd);
  1387. break;
  1388. default:
  1389. printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
  1390. seqintcode);
  1391. break;
  1392. }
  1393. /*
  1394. * The sequencer is paused immediately on
  1395. * a SEQINT, so we should restart it when
  1396. * we're done.
  1397. */
  1398. ahd_unpause(ahd);
  1399. }
  1400. void
  1401. ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
  1402. {
  1403. struct scb *scb;
  1404. u_int status0;
  1405. u_int status3;
  1406. u_int status;
  1407. u_int lqistat1;
  1408. u_int lqostat0;
  1409. u_int scbid;
  1410. u_int busfreetime;
  1411. ahd_update_modes(ahd);
  1412. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1413. status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
  1414. status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
  1415. status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  1416. lqistat1 = ahd_inb(ahd, LQISTAT1);
  1417. lqostat0 = ahd_inb(ahd, LQOSTAT0);
  1418. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1419. if ((status0 & (SELDI|SELDO)) != 0) {
  1420. u_int simode0;
  1421. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1422. simode0 = ahd_inb(ahd, SIMODE0);
  1423. status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
  1424. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1425. }
  1426. scbid = ahd_get_scbptr(ahd);
  1427. scb = ahd_lookup_scb(ahd, scbid);
  1428. if (scb != NULL
  1429. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  1430. scb = NULL;
  1431. if ((status0 & IOERR) != 0) {
  1432. u_int now_lvd;
  1433. now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
  1434. printf("%s: Transceiver State Has Changed to %s mode\n",
  1435. ahd_name(ahd), now_lvd ? "LVD" : "SE");
  1436. ahd_outb(ahd, CLRSINT0, CLRIOERR);
  1437. /*
  1438. * A change in I/O mode is equivalent to a bus reset.
  1439. */
  1440. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1441. ahd_pause(ahd);
  1442. ahd_setup_iocell_workaround(ahd);
  1443. ahd_unpause(ahd);
  1444. } else if ((status0 & OVERRUN) != 0) {
  1445. printf("%s: SCSI offset overrun detected. Resetting bus.\n",
  1446. ahd_name(ahd));
  1447. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1448. } else if ((status & SCSIRSTI) != 0) {
  1449. printf("%s: Someone reset channel A\n", ahd_name(ahd));
  1450. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
  1451. } else if ((status & SCSIPERR) != 0) {
  1452. /* Make sure the sequencer is in a safe location. */
  1453. ahd_clear_critical_section(ahd);
  1454. ahd_handle_transmission_error(ahd);
  1455. } else if (lqostat0 != 0) {
  1456. printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
  1457. ahd_outb(ahd, CLRLQOINT0, lqostat0);
  1458. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
  1459. ahd_outb(ahd, CLRLQOINT1, 0);
  1460. } else if ((status & SELTO) != 0) {
  1461. u_int scbid;
  1462. /* Stop the selection */
  1463. ahd_outb(ahd, SCSISEQ0, 0);
  1464. /* Make sure the sequencer is in a safe location. */
  1465. ahd_clear_critical_section(ahd);
  1466. /* No more pending messages */
  1467. ahd_clear_msg_state(ahd);
  1468. /* Clear interrupt state */
  1469. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1470. /*
  1471. * Although the driver does not care about the
  1472. * 'Selection in Progress' status bit, the busy
  1473. * LED does. SELINGO is only cleared by a sucessfull
  1474. * selection, so we must manually clear it to insure
  1475. * the LED turns off just incase no future successful
  1476. * selections occur (e.g. no devices on the bus).
  1477. */
  1478. ahd_outb(ahd, CLRSINT0, CLRSELINGO);
  1479. scbid = ahd_inw(ahd, WAITING_TID_HEAD);
  1480. scb = ahd_lookup_scb(ahd, scbid);
  1481. if (scb == NULL) {
  1482. printf("%s: ahd_intr - referenced scb not "
  1483. "valid during SELTO scb(0x%x)\n",
  1484. ahd_name(ahd), scbid);
  1485. ahd_dump_card_state(ahd);
  1486. } else {
  1487. struct ahd_devinfo devinfo;
  1488. #ifdef AHD_DEBUG
  1489. if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
  1490. ahd_print_path(ahd, scb);
  1491. printf("Saw Selection Timeout for SCB 0x%x\n",
  1492. scbid);
  1493. }
  1494. #endif
  1495. ahd_scb_devinfo(ahd, &devinfo, scb);
  1496. ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1497. ahd_freeze_devq(ahd, scb);
  1498. /*
  1499. * Cancel any pending transactions on the device
  1500. * now that it seems to be missing. This will
  1501. * also revert us to async/narrow transfers until
  1502. * we can renegotiate with the device.
  1503. */
  1504. ahd_handle_devreset(ahd, &devinfo,
  1505. CAM_LUN_WILDCARD,
  1506. CAM_SEL_TIMEOUT,
  1507. "Selection Timeout",
  1508. /*verbose_level*/1);
  1509. }
  1510. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1511. ahd_iocell_first_selection(ahd);
  1512. ahd_unpause(ahd);
  1513. } else if ((status0 & (SELDI|SELDO)) != 0) {
  1514. ahd_iocell_first_selection(ahd);
  1515. ahd_unpause(ahd);
  1516. } else if (status3 != 0) {
  1517. printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
  1518. ahd_name(ahd), status3);
  1519. ahd_outb(ahd, CLRSINT3, status3);
  1520. } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
  1521. /* Make sure the sequencer is in a safe location. */
  1522. ahd_clear_critical_section(ahd);
  1523. ahd_handle_lqiphase_error(ahd, lqistat1);
  1524. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1525. /*
  1526. * This status can be delayed during some
  1527. * streaming operations. The SCSIPHASE
  1528. * handler has already dealt with this case
  1529. * so just clear the error.
  1530. */
  1531. ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
  1532. } else if ((status & BUSFREE) != 0
  1533. || (lqistat1 & LQOBUSFREE) != 0) {
  1534. u_int lqostat1;
  1535. int restart;
  1536. int clear_fifo;
  1537. int packetized;
  1538. u_int mode;
  1539. /*
  1540. * Clear our selection hardware as soon as possible.
  1541. * We may have an entry in the waiting Q for this target,
  1542. * that is affected by this busfree and we don't want to
  1543. * go about selecting the target while we handle the event.
  1544. */
  1545. ahd_outb(ahd, SCSISEQ0, 0);
  1546. /* Make sure the sequencer is in a safe location. */
  1547. ahd_clear_critical_section(ahd);
  1548. /*
  1549. * Determine what we were up to at the time of
  1550. * the busfree.
  1551. */
  1552. mode = AHD_MODE_SCSI;
  1553. busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
  1554. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1555. switch (busfreetime) {
  1556. case BUSFREE_DFF0:
  1557. case BUSFREE_DFF1:
  1558. {
  1559. u_int scbid;
  1560. struct scb *scb;
  1561. mode = busfreetime == BUSFREE_DFF0
  1562. ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
  1563. ahd_set_modes(ahd, mode, mode);
  1564. scbid = ahd_get_scbptr(ahd);
  1565. scb = ahd_lookup_scb(ahd, scbid);
  1566. if (scb == NULL) {
  1567. printf("%s: Invalid SCB %d in DFF%d "
  1568. "during unexpected busfree\n",
  1569. ahd_name(ahd), scbid, mode);
  1570. packetized = 0;
  1571. } else
  1572. packetized = (scb->flags & SCB_PACKETIZED) != 0;
  1573. clear_fifo = 1;
  1574. break;
  1575. }
  1576. case BUSFREE_LQO:
  1577. clear_fifo = 0;
  1578. packetized = 1;
  1579. break;
  1580. default:
  1581. clear_fifo = 0;
  1582. packetized = (lqostat1 & LQOBUSFREE) != 0;
  1583. if (!packetized
  1584. && ahd_inb(ahd, LASTPHASE) == P_BUSFREE
  1585. && (ahd_inb(ahd, SSTAT0) & SELDI) == 0
  1586. && ((ahd_inb(ahd, SSTAT0) & SELDO) == 0
  1587. || (ahd_inb(ahd, SCSISEQ0) & ENSELO) == 0))
  1588. /*
  1589. * Assume packetized if we are not
  1590. * on the bus in a non-packetized
  1591. * capacity and any pending selection
  1592. * was a packetized selection.
  1593. */
  1594. packetized = 1;
  1595. break;
  1596. }
  1597. #ifdef AHD_DEBUG
  1598. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  1599. printf("Saw Busfree. Busfreetime = 0x%x.\n",
  1600. busfreetime);
  1601. #endif
  1602. /*
  1603. * Busfrees that occur in non-packetized phases are
  1604. * handled by the nonpkt_busfree handler.
  1605. */
  1606. if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
  1607. restart = ahd_handle_pkt_busfree(ahd, busfreetime);
  1608. } else {
  1609. packetized = 0;
  1610. restart = ahd_handle_nonpkt_busfree(ahd);
  1611. }
  1612. /*
  1613. * Clear the busfree interrupt status. The setting of
  1614. * the interrupt is a pulse, so in a perfect world, we
  1615. * would not need to muck with the ENBUSFREE logic. This
  1616. * would ensure that if the bus moves on to another
  1617. * connection, busfree protection is still in force. If
  1618. * BUSFREEREV is broken, however, we must manually clear
  1619. * the ENBUSFREE if the busfree occurred during a non-pack
  1620. * connection so that we don't get false positives during
  1621. * future, packetized, connections.
  1622. */
  1623. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  1624. if (packetized == 0
  1625. && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
  1626. ahd_outb(ahd, SIMODE1,
  1627. ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
  1628. if (clear_fifo)
  1629. ahd_clear_fifo(ahd, mode);
  1630. ahd_clear_msg_state(ahd);
  1631. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1632. if (restart) {
  1633. ahd_restart(ahd);
  1634. } else {
  1635. ahd_unpause(ahd);
  1636. }
  1637. } else {
  1638. printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
  1639. ahd_name(ahd), status);
  1640. ahd_dump_card_state(ahd);
  1641. ahd_clear_intstat(ahd);
  1642. ahd_unpause(ahd);
  1643. }
  1644. }
  1645. static void
  1646. ahd_handle_transmission_error(struct ahd_softc *ahd)
  1647. {
  1648. struct scb *scb;
  1649. u_int scbid;
  1650. u_int lqistat1;
  1651. u_int lqistat2;
  1652. u_int msg_out;
  1653. u_int curphase;
  1654. u_int lastphase;
  1655. u_int perrdiag;
  1656. u_int cur_col;
  1657. int silent;
  1658. scb = NULL;
  1659. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1660. lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
  1661. lqistat2 = ahd_inb(ahd, LQISTAT2);
  1662. if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
  1663. && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
  1664. u_int lqistate;
  1665. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  1666. lqistate = ahd_inb(ahd, LQISTATE);
  1667. if ((lqistate >= 0x1E && lqistate <= 0x24)
  1668. || (lqistate == 0x29)) {
  1669. #ifdef AHD_DEBUG
  1670. if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
  1671. printf("%s: NLQCRC found via LQISTATE\n",
  1672. ahd_name(ahd));
  1673. }
  1674. #endif
  1675. lqistat1 |= LQICRCI_NLQ;
  1676. }
  1677. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1678. }
  1679. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1680. lastphase = ahd_inb(ahd, LASTPHASE);
  1681. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  1682. perrdiag = ahd_inb(ahd, PERRDIAG);
  1683. msg_out = MSG_INITIATOR_DET_ERR;
  1684. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
  1685. /*
  1686. * Try to find the SCB associated with this error.
  1687. */
  1688. silent = FALSE;
  1689. if (lqistat1 == 0
  1690. || (lqistat1 & LQICRCI_NLQ) != 0) {
  1691. if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
  1692. ahd_set_active_fifo(ahd);
  1693. scbid = ahd_get_scbptr(ahd);
  1694. scb = ahd_lookup_scb(ahd, scbid);
  1695. if (scb != NULL && SCB_IS_SILENT(scb))
  1696. silent = TRUE;
  1697. }
  1698. cur_col = 0;
  1699. if (silent == FALSE) {
  1700. printf("%s: Transmission error detected\n", ahd_name(ahd));
  1701. ahd_lqistat1_print(lqistat1, &cur_col, 50);
  1702. ahd_lastphase_print(lastphase, &cur_col, 50);
  1703. ahd_scsisigi_print(curphase, &cur_col, 50);
  1704. ahd_perrdiag_print(perrdiag, &cur_col, 50);
  1705. printf("\n");
  1706. ahd_dump_card_state(ahd);
  1707. }
  1708. if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
  1709. if (silent == FALSE) {
  1710. printf("%s: Gross protocol error during incoming "
  1711. "packet. lqistat1 == 0x%x. Resetting bus.\n",
  1712. ahd_name(ahd), lqistat1);
  1713. }
  1714. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1715. return;
  1716. } else if ((lqistat1 & LQICRCI_LQ) != 0) {
  1717. /*
  1718. * A CRC error has been detected on an incoming LQ.
  1719. * The bus is currently hung on the last ACK.
  1720. * Hit LQIRETRY to release the last ack, and
  1721. * wait for the sequencer to determine that ATNO
  1722. * is asserted while in message out to take us
  1723. * to our host message loop. No NONPACKREQ or
  1724. * LQIPHASE type errors will occur in this
  1725. * scenario. After this first LQIRETRY, the LQI
  1726. * manager will be in ISELO where it will
  1727. * happily sit until another packet phase begins.
  1728. * Unexpected bus free detection is enabled
  1729. * through any phases that occur after we release
  1730. * this last ack until the LQI manager sees a
  1731. * packet phase. This implies we may have to
  1732. * ignore a perfectly valid "unexected busfree"
  1733. * after our "initiator detected error" message is
  1734. * sent. A busfree is the expected response after
  1735. * we tell the target that it's L_Q was corrupted.
  1736. * (SPI4R09 10.7.3.3.3)
  1737. */
  1738. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1739. printf("LQIRetry for LQICRCI_LQ to release ACK\n");
  1740. } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
  1741. /*
  1742. * We detected a CRC error in a NON-LQ packet.
  1743. * The hardware has varying behavior in this situation
  1744. * depending on whether this packet was part of a
  1745. * stream or not.
  1746. *
  1747. * PKT by PKT mode:
  1748. * The hardware has already acked the complete packet.
  1749. * If the target honors our outstanding ATN condition,
  1750. * we should be (or soon will be) in MSGOUT phase.
  1751. * This will trigger the LQIPHASE_LQ status bit as the
  1752. * hardware was expecting another LQ. Unexpected
  1753. * busfree detection is enabled. Once LQIPHASE_LQ is
  1754. * true (first entry into host message loop is much
  1755. * the same), we must clear LQIPHASE_LQ and hit
  1756. * LQIRETRY so the hardware is ready to handle
  1757. * a future LQ. NONPACKREQ will not be asserted again
  1758. * once we hit LQIRETRY until another packet is
  1759. * processed. The target may either go busfree
  1760. * or start another packet in response to our message.
  1761. *
  1762. * Read Streaming P0 asserted:
  1763. * If we raise ATN and the target completes the entire
  1764. * stream (P0 asserted during the last packet), the
  1765. * hardware will ack all data and return to the ISTART
  1766. * state. When the target reponds to our ATN condition,
  1767. * LQIPHASE_LQ will be asserted. We should respond to
  1768. * this with an LQIRETRY to prepare for any future
  1769. * packets. NONPACKREQ will not be asserted again
  1770. * once we hit LQIRETRY until another packet is
  1771. * processed. The target may either go busfree or
  1772. * start another packet in response to our message.
  1773. * Busfree detection is enabled.
  1774. *
  1775. * Read Streaming P0 not asserted:
  1776. * If we raise ATN and the target transitions to
  1777. * MSGOUT in or after a packet where P0 is not
  1778. * asserted, the hardware will assert LQIPHASE_NLQ.
  1779. * We should respond to the LQIPHASE_NLQ with an
  1780. * LQIRETRY. Should the target stay in a non-pkt
  1781. * phase after we send our message, the hardware
  1782. * will assert LQIPHASE_LQ. Recovery is then just as
  1783. * listed above for the read streaming with P0 asserted.
  1784. * Busfree detection is enabled.
  1785. */
  1786. if (silent == FALSE)
  1787. printf("LQICRC_NLQ\n");
  1788. if (scb == NULL) {
  1789. printf("%s: No SCB valid for LQICRC_NLQ. "
  1790. "Resetting bus\n", ahd_name(ahd));
  1791. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1792. return;
  1793. }
  1794. } else if ((lqistat1 & LQIBADLQI) != 0) {
  1795. printf("Need to handle BADLQI!\n");
  1796. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1797. return;
  1798. } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
  1799. if ((curphase & ~P_DATAIN_DT) != 0) {
  1800. /* Ack the byte. So we can continue. */
  1801. if (silent == FALSE)
  1802. printf("Acking %s to clear perror\n",
  1803. ahd_lookup_phase_entry(curphase)->phasemsg);
  1804. ahd_inb(ahd, SCSIDAT);
  1805. }
  1806. if (curphase == P_MESGIN)
  1807. msg_out = MSG_PARITY_ERROR;
  1808. }
  1809. /*
  1810. * We've set the hardware to assert ATN if we
  1811. * get a parity error on "in" phases, so all we
  1812. * need to do is stuff the message buffer with
  1813. * the appropriate message. "In" phases have set
  1814. * mesg_out to something other than MSG_NOP.
  1815. */
  1816. ahd->send_msg_perror = msg_out;
  1817. if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
  1818. scb->flags |= SCB_TRANSMISSION_ERROR;
  1819. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  1820. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1821. ahd_unpause(ahd);
  1822. }
  1823. static void
  1824. ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
  1825. {
  1826. /*
  1827. * Clear the sources of the interrupts.
  1828. */
  1829. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1830. ahd_outb(ahd, CLRLQIINT1, lqistat1);
  1831. /*
  1832. * If the "illegal" phase changes were in response
  1833. * to our ATN to flag a CRC error, AND we ended up
  1834. * on packet boundaries, clear the error, restart the
  1835. * LQI manager as appropriate, and go on our merry
  1836. * way toward sending the message. Otherwise, reset
  1837. * the bus to clear the error.
  1838. */
  1839. ahd_set_active_fifo(ahd);
  1840. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
  1841. && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
  1842. if ((lqistat1 & LQIPHASE_LQ) != 0) {
  1843. printf("LQIRETRY for LQIPHASE_LQ\n");
  1844. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1845. } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
  1846. printf("LQIRETRY for LQIPHASE_NLQ\n");
  1847. ahd_outb(ahd, LQCTL2, LQIRETRY);
  1848. } else
  1849. panic("ahd_handle_lqiphase_error: No phase errors\n");
  1850. ahd_dump_card_state(ahd);
  1851. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  1852. ahd_unpause(ahd);
  1853. } else {
  1854. printf("Reseting Channel for LQI Phase error\n");
  1855. ahd_dump_card_state(ahd);
  1856. ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
  1857. }
  1858. }
  1859. /*
  1860. * Packetized unexpected or expected busfree.
  1861. * Entered in mode based on busfreetime.
  1862. */
  1863. static int
  1864. ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
  1865. {
  1866. u_int lqostat1;
  1867. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  1868. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  1869. lqostat1 = ahd_inb(ahd, LQOSTAT1);
  1870. if ((lqostat1 & LQOBUSFREE) != 0) {
  1871. struct scb *scb;
  1872. u_int scbid;
  1873. u_int saved_scbptr;
  1874. u_int waiting_h;
  1875. u_int waiting_t;
  1876. u_int next;
  1877. /*
  1878. * The LQO manager detected an unexpected busfree
  1879. * either:
  1880. *
  1881. * 1) During an outgoing LQ.
  1882. * 2) After an outgoing LQ but before the first
  1883. * REQ of the command packet.
  1884. * 3) During an outgoing command packet.
  1885. *
  1886. * In all cases, CURRSCB is pointing to the
  1887. * SCB that encountered the failure. Clean
  1888. * up the queue, clear SELDO and LQOBUSFREE,
  1889. * and allow the sequencer to restart the select
  1890. * out at its lesure.
  1891. */
  1892. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  1893. scbid = ahd_inw(ahd, CURRSCB);
  1894. scb = ahd_lookup_scb(ahd, scbid);
  1895. if (scb == NULL)
  1896. panic("SCB not valid during LQOBUSFREE");
  1897. /*
  1898. * Clear the status.
  1899. */
  1900. ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
  1901. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
  1902. ahd_outb(ahd, CLRLQOINT1, 0);
  1903. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  1904. ahd_flush_device_writes(ahd);
  1905. ahd_outb(ahd, CLRSINT0, CLRSELDO);
  1906. /*
  1907. * Return the LQO manager to its idle loop. It will
  1908. * not do this automatically if the busfree occurs
  1909. * after the first REQ of either the LQ or command
  1910. * packet or between the LQ and command packet.
  1911. */
  1912. ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
  1913. /*
  1914. * Update the waiting for selection queue so
  1915. * we restart on the correct SCB.
  1916. */
  1917. waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
  1918. saved_scbptr = ahd_get_scbptr(ahd);
  1919. if (waiting_h != scbid) {
  1920. ahd_outw(ahd, WAITING_TID_HEAD, scbid);
  1921. waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
  1922. if (waiting_t == waiting_h) {
  1923. ahd_outw(ahd, WAITING_TID_TAIL, scbid);
  1924. next = SCB_LIST_NULL;
  1925. } else {
  1926. ahd_set_scbptr(ahd, waiting_h);
  1927. next = ahd_inw_scbram(ahd, SCB_NEXT2);
  1928. }
  1929. ahd_set_scbptr(ahd, scbid);
  1930. ahd_outw(ahd, SCB_NEXT2, next);
  1931. }
  1932. ahd_set_scbptr(ahd, saved_scbptr);
  1933. if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
  1934. if (SCB_IS_SILENT(scb) == FALSE) {
  1935. ahd_print_path(ahd, scb);
  1936. printf("Probable outgoing LQ CRC error. "
  1937. "Retrying command\n");
  1938. }
  1939. scb->crc_retry_count++;
  1940. } else {
  1941. ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
  1942. ahd_freeze_scb(scb);
  1943. ahd_freeze_devq(ahd, scb);
  1944. }
  1945. /* Return unpausing the sequencer. */
  1946. return (0);
  1947. } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
  1948. /*
  1949. * Ignore what are really parity errors that
  1950. * occur on the last REQ of a free running
  1951. * clock prior to going busfree. Some drives
  1952. * do not properly active negate just before
  1953. * going busfree resulting in a parity glitch.
  1954. */
  1955. ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
  1956. #ifdef AHD_DEBUG
  1957. if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
  1958. printf("%s: Parity on last REQ detected "
  1959. "during busfree phase.\n",
  1960. ahd_name(ahd));
  1961. #endif
  1962. /* Return unpausing the sequencer. */
  1963. return (0);
  1964. }
  1965. if (ahd->src_mode != AHD_MODE_SCSI) {
  1966. u_int scbid;
  1967. struct scb *scb;
  1968. scbid = ahd_get_scbptr(ahd);
  1969. scb = ahd_lookup_scb(ahd, scbid);
  1970. ahd_print_path(ahd, scb);
  1971. printf("Unexpected PKT busfree condition\n");
  1972. ahd_dump_card_state(ahd);
  1973. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
  1974. SCB_GET_LUN(scb), SCB_GET_TAG(scb),
  1975. ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
  1976. /* Return restarting the sequencer. */
  1977. return (1);
  1978. }
  1979. printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
  1980. ahd_dump_card_state(ahd);
  1981. /* Restart the sequencer. */
  1982. return (1);
  1983. }
  1984. /*
  1985. * Non-packetized unexpected or expected busfree.
  1986. */
  1987. static int
  1988. ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
  1989. {
  1990. struct ahd_devinfo devinfo;
  1991. struct scb *scb;
  1992. u_int lastphase;
  1993. u_int saved_scsiid;
  1994. u_int saved_lun;
  1995. u_int target;
  1996. u_int initiator_role_id;
  1997. u_int scbid;
  1998. u_int ppr_busfree;
  1999. int printerror;
  2000. /*
  2001. * Look at what phase we were last in. If its message out,
  2002. * chances are pretty good that the busfree was in response
  2003. * to one of our abort requests.
  2004. */
  2005. lastphase = ahd_inb(ahd, LASTPHASE);
  2006. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  2007. saved_lun = ahd_inb(ahd, SAVED_LUN);
  2008. target = SCSIID_TARGET(ahd, saved_scsiid);
  2009. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  2010. ahd_compile_devinfo(&devinfo, initiator_role_id,
  2011. target, saved_lun, 'A', ROLE_INITIATOR);
  2012. printerror = 1;
  2013. scbid = ahd_get_scbptr(ahd);
  2014. scb = ahd_lookup_scb(ahd, scbid);
  2015. if (scb != NULL
  2016. && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  2017. scb = NULL;
  2018. ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
  2019. if (lastphase == P_MESGOUT) {
  2020. u_int tag;
  2021. tag = SCB_LIST_NULL;
  2022. if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
  2023. || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
  2024. int found;
  2025. int sent_msg;
  2026. if (scb == NULL) {
  2027. ahd_print_devinfo(ahd, &devinfo);
  2028. printf("Abort for unidentified "
  2029. "connection completed.\n");
  2030. /* restart the sequencer. */
  2031. return (1);
  2032. }
  2033. sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
  2034. ahd_print_path(ahd, scb);
  2035. printf("SCB %d - Abort%s Completed.\n",
  2036. SCB_GET_TAG(scb),
  2037. sent_msg == MSG_ABORT_TAG ? "" : " Tag");
  2038. if (sent_msg == MSG_ABORT_TAG)
  2039. tag = SCB_GET_TAG(scb);
  2040. if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
  2041. /*
  2042. * This abort is in response to an
  2043. * unexpected switch to command phase
  2044. * for a packetized connection. Since
  2045. * the identify message was never sent,
  2046. * "saved lun" is 0. We really want to
  2047. * abort only the SCB that encountered
  2048. * this error, which could have a different
  2049. * lun. The SCB will be retried so the OS
  2050. * will see the UA after renegotiating to
  2051. * packetized.
  2052. */
  2053. tag = SCB_GET_TAG(scb);
  2054. saved_lun = scb->hscb->lun;
  2055. }
  2056. found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
  2057. tag, ROLE_INITIATOR,
  2058. CAM_REQ_ABORTED);
  2059. printf("found == 0x%x\n", found);
  2060. printerror = 0;
  2061. } else if (ahd_sent_msg(ahd, AHDMSG_1B,
  2062. MSG_BUS_DEV_RESET, TRUE)) {
  2063. #ifdef __FreeBSD__
  2064. /*
  2065. * Don't mark the user's request for this BDR
  2066. * as completing with CAM_BDR_SENT. CAM3
  2067. * specifies CAM_REQ_CMP.
  2068. */
  2069. if (scb != NULL
  2070. && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  2071. && ahd_match_scb(ahd, scb, target, 'A',
  2072. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  2073. ROLE_INITIATOR))
  2074. ahd_set_transaction_status(scb, CAM_REQ_CMP);
  2075. #endif
  2076. ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
  2077. CAM_BDR_SENT, "Bus Device Reset",
  2078. /*verbose_level*/0);
  2079. printerror = 0;
  2080. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
  2081. && ppr_busfree == 0) {
  2082. struct ahd_initiator_tinfo *tinfo;
  2083. struct ahd_tmode_tstate *tstate;
  2084. /*
  2085. * PPR Rejected.
  2086. *
  2087. * If the previous negotiation was packetized,
  2088. * this could be because the device has been
  2089. * reset without our knowledge. Force our
  2090. * current negotiation to async and retry the
  2091. * negotiation. Otherwise retry the command
  2092. * with non-ppr negotiation.
  2093. */
  2094. #ifdef AHD_DEBUG
  2095. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2096. printf("PPR negotiation rejected busfree.\n");
  2097. #endif
  2098. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  2099. devinfo.our_scsiid,
  2100. devinfo.target, &tstate);
  2101. if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ)!=0) {
  2102. ahd_set_width(ahd, &devinfo,
  2103. MSG_EXT_WDTR_BUS_8_BIT,
  2104. AHD_TRANS_CUR,
  2105. /*paused*/TRUE);
  2106. ahd_set_syncrate(ahd, &devinfo,
  2107. /*period*/0, /*offset*/0,
  2108. /*ppr_options*/0,
  2109. AHD_TRANS_CUR,
  2110. /*paused*/TRUE);
  2111. /*
  2112. * The expect PPR busfree handler below
  2113. * will effect the retry and necessary
  2114. * abort.
  2115. */
  2116. } else {
  2117. tinfo->curr.transport_version = 2;
  2118. tinfo->goal.transport_version = 2;
  2119. tinfo->goal.ppr_options = 0;
  2120. /*
  2121. * Remove any SCBs in the waiting for selection
  2122. * queue that may also be for this target so
  2123. * that command ordering is preserved.
  2124. */
  2125. ahd_freeze_devq(ahd, scb);
  2126. ahd_qinfifo_requeue_tail(ahd, scb);
  2127. printerror = 0;
  2128. }
  2129. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
  2130. && ppr_busfree == 0) {
  2131. /*
  2132. * Negotiation Rejected. Go-narrow and
  2133. * retry command.
  2134. */
  2135. #ifdef AHD_DEBUG
  2136. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2137. printf("WDTR negotiation rejected busfree.\n");
  2138. #endif
  2139. ahd_set_width(ahd, &devinfo,
  2140. MSG_EXT_WDTR_BUS_8_BIT,
  2141. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2142. /*paused*/TRUE);
  2143. /*
  2144. * Remove any SCBs in the waiting for selection
  2145. * queue that may also be for this target so that
  2146. * command ordering is preserved.
  2147. */
  2148. ahd_freeze_devq(ahd, scb);
  2149. ahd_qinfifo_requeue_tail(ahd, scb);
  2150. printerror = 0;
  2151. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE)
  2152. && ppr_busfree == 0) {
  2153. /*
  2154. * Negotiation Rejected. Go-async and
  2155. * retry command.
  2156. */
  2157. #ifdef AHD_DEBUG
  2158. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2159. printf("SDTR negotiation rejected busfree.\n");
  2160. #endif
  2161. ahd_set_syncrate(ahd, &devinfo,
  2162. /*period*/0, /*offset*/0,
  2163. /*ppr_options*/0,
  2164. AHD_TRANS_CUR|AHD_TRANS_GOAL,
  2165. /*paused*/TRUE);
  2166. /*
  2167. * Remove any SCBs in the waiting for selection
  2168. * queue that may also be for this target so that
  2169. * command ordering is preserved.
  2170. */
  2171. ahd_freeze_devq(ahd, scb);
  2172. ahd_qinfifo_requeue_tail(ahd, scb);
  2173. printerror = 0;
  2174. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
  2175. && ahd_sent_msg(ahd, AHDMSG_1B,
  2176. MSG_INITIATOR_DET_ERR, TRUE)) {
  2177. #ifdef AHD_DEBUG
  2178. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2179. printf("Expected IDE Busfree\n");
  2180. #endif
  2181. printerror = 0;
  2182. } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
  2183. && ahd_sent_msg(ahd, AHDMSG_1B,
  2184. MSG_MESSAGE_REJECT, TRUE)) {
  2185. #ifdef AHD_DEBUG
  2186. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2187. printf("Expected QAS Reject Busfree\n");
  2188. #endif
  2189. printerror = 0;
  2190. }
  2191. }
  2192. /*
  2193. * The busfree required flag is honored at the end of
  2194. * the message phases. We check it last in case we
  2195. * had to send some other message that caused a busfree.
  2196. */
  2197. if (printerror != 0
  2198. && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
  2199. && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
  2200. ahd_freeze_devq(ahd, scb);
  2201. ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
  2202. ahd_freeze_scb(scb);
  2203. if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
  2204. ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
  2205. SCB_GET_CHANNEL(ahd, scb),
  2206. SCB_GET_LUN(scb), SCB_LIST_NULL,
  2207. ROLE_INITIATOR, CAM_REQ_ABORTED);
  2208. } else {
  2209. #ifdef AHD_DEBUG
  2210. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2211. printf("PPR Negotiation Busfree.\n");
  2212. #endif
  2213. ahd_done(ahd, scb);
  2214. }
  2215. printerror = 0;
  2216. }
  2217. if (printerror != 0) {
  2218. int aborted;
  2219. aborted = 0;
  2220. if (scb != NULL) {
  2221. u_int tag;
  2222. if ((scb->hscb->control & TAG_ENB) != 0)
  2223. tag = SCB_GET_TAG(scb);
  2224. else
  2225. tag = SCB_LIST_NULL;
  2226. ahd_print_path(ahd, scb);
  2227. aborted = ahd_abort_scbs(ahd, target, 'A',
  2228. SCB_GET_LUN(scb), tag,
  2229. ROLE_INITIATOR,
  2230. CAM_UNEXP_BUSFREE);
  2231. } else {
  2232. /*
  2233. * We had not fully identified this connection,
  2234. * so we cannot abort anything.
  2235. */
  2236. printf("%s: ", ahd_name(ahd));
  2237. }
  2238. printf("Unexpected busfree %s, %d SCBs aborted, "
  2239. "PRGMCNT == 0x%x\n",
  2240. ahd_lookup_phase_entry(lastphase)->phasemsg,
  2241. aborted,
  2242. ahd_inw(ahd, PRGMCNT));
  2243. ahd_dump_card_state(ahd);
  2244. if (lastphase != P_BUSFREE)
  2245. ahd_force_renegotiation(ahd, &devinfo);
  2246. }
  2247. /* Always restart the sequencer. */
  2248. return (1);
  2249. }
  2250. static void
  2251. ahd_handle_proto_violation(struct ahd_softc *ahd)
  2252. {
  2253. struct ahd_devinfo devinfo;
  2254. struct scb *scb;
  2255. u_int scbid;
  2256. u_int seq_flags;
  2257. u_int curphase;
  2258. u_int lastphase;
  2259. int found;
  2260. ahd_fetch_devinfo(ahd, &devinfo);
  2261. scbid = ahd_get_scbptr(ahd);
  2262. scb = ahd_lookup_scb(ahd, scbid);
  2263. seq_flags = ahd_inb(ahd, SEQ_FLAGS);
  2264. curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
  2265. lastphase = ahd_inb(ahd, LASTPHASE);
  2266. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2267. /*
  2268. * The reconnecting target either did not send an
  2269. * identify message, or did, but we didn't find an SCB
  2270. * to match.
  2271. */
  2272. ahd_print_devinfo(ahd, &devinfo);
  2273. printf("Target did not send an IDENTIFY message. "
  2274. "LASTPHASE = 0x%x.\n", lastphase);
  2275. scb = NULL;
  2276. } else if (scb == NULL) {
  2277. /*
  2278. * We don't seem to have an SCB active for this
  2279. * transaction. Print an error and reset the bus.
  2280. */
  2281. ahd_print_devinfo(ahd, &devinfo);
  2282. printf("No SCB found during protocol violation\n");
  2283. goto proto_violation_reset;
  2284. } else {
  2285. ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2286. if ((seq_flags & NO_CDB_SENT) != 0) {
  2287. ahd_print_path(ahd, scb);
  2288. printf("No or incomplete CDB sent to device.\n");
  2289. } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
  2290. & STATUS_RCVD) == 0) {
  2291. /*
  2292. * The target never bothered to provide status to
  2293. * us prior to completing the command. Since we don't
  2294. * know the disposition of this command, we must attempt
  2295. * to abort it. Assert ATN and prepare to send an abort
  2296. * message.
  2297. */
  2298. ahd_print_path(ahd, scb);
  2299. printf("Completed command without status.\n");
  2300. } else {
  2301. ahd_print_path(ahd, scb);
  2302. printf("Unknown protocol violation.\n");
  2303. ahd_dump_card_state(ahd);
  2304. }
  2305. }
  2306. if ((lastphase & ~P_DATAIN_DT) == 0
  2307. || lastphase == P_COMMAND) {
  2308. proto_violation_reset:
  2309. /*
  2310. * Target either went directly to data
  2311. * phase or didn't respond to our ATN.
  2312. * The only safe thing to do is to blow
  2313. * it away with a bus reset.
  2314. */
  2315. found = ahd_reset_channel(ahd, 'A', TRUE);
  2316. printf("%s: Issued Channel %c Bus Reset. "
  2317. "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
  2318. } else {
  2319. /*
  2320. * Leave the selection hardware off in case
  2321. * this abort attempt will affect yet to
  2322. * be sent commands.
  2323. */
  2324. ahd_outb(ahd, SCSISEQ0,
  2325. ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  2326. ahd_assert_atn(ahd);
  2327. ahd_outb(ahd, MSG_OUT, HOST_MSG);
  2328. if (scb == NULL) {
  2329. ahd_print_devinfo(ahd, &devinfo);
  2330. ahd->msgout_buf[0] = MSG_ABORT_TASK;
  2331. ahd->msgout_len = 1;
  2332. ahd->msgout_index = 0;
  2333. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2334. } else {
  2335. ahd_print_path(ahd, scb);
  2336. scb->flags |= SCB_ABORT;
  2337. }
  2338. printf("Protocol violation %s. Attempting to abort.\n",
  2339. ahd_lookup_phase_entry(curphase)->phasemsg);
  2340. }
  2341. }
  2342. /*
  2343. * Force renegotiation to occur the next time we initiate
  2344. * a command to the current device.
  2345. */
  2346. static void
  2347. ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  2348. {
  2349. struct ahd_initiator_tinfo *targ_info;
  2350. struct ahd_tmode_tstate *tstate;
  2351. #ifdef AHD_DEBUG
  2352. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2353. ahd_print_devinfo(ahd, devinfo);
  2354. printf("Forcing renegotiation\n");
  2355. }
  2356. #endif
  2357. targ_info = ahd_fetch_transinfo(ahd,
  2358. devinfo->channel,
  2359. devinfo->our_scsiid,
  2360. devinfo->target,
  2361. &tstate);
  2362. ahd_update_neg_request(ahd, devinfo, tstate,
  2363. targ_info, AHD_NEG_IF_NON_ASYNC);
  2364. }
  2365. #define AHD_MAX_STEPS 2000
  2366. void
  2367. ahd_clear_critical_section(struct ahd_softc *ahd)
  2368. {
  2369. ahd_mode_state saved_modes;
  2370. int stepping;
  2371. int steps;
  2372. int first_instr;
  2373. u_int simode0;
  2374. u_int simode1;
  2375. u_int simode3;
  2376. u_int lqimode0;
  2377. u_int lqimode1;
  2378. u_int lqomode0;
  2379. u_int lqomode1;
  2380. if (ahd->num_critical_sections == 0)
  2381. return;
  2382. stepping = FALSE;
  2383. steps = 0;
  2384. first_instr = 0;
  2385. simode0 = 0;
  2386. simode1 = 0;
  2387. simode3 = 0;
  2388. lqimode0 = 0;
  2389. lqimode1 = 0;
  2390. lqomode0 = 0;
  2391. lqomode1 = 0;
  2392. saved_modes = ahd_save_modes(ahd);
  2393. for (;;) {
  2394. struct cs *cs;
  2395. u_int seqaddr;
  2396. u_int i;
  2397. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2398. seqaddr = ahd_inw(ahd, CURADDR);
  2399. cs = ahd->critical_sections;
  2400. for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
  2401. if (cs->begin < seqaddr && cs->end >= seqaddr)
  2402. break;
  2403. }
  2404. if (i == ahd->num_critical_sections)
  2405. break;
  2406. if (steps > AHD_MAX_STEPS) {
  2407. printf("%s: Infinite loop in critical section\n"
  2408. "%s: First Instruction 0x%x now 0x%x\n",
  2409. ahd_name(ahd), ahd_name(ahd), first_instr,
  2410. seqaddr);
  2411. ahd_dump_card_state(ahd);
  2412. panic("critical section loop");
  2413. }
  2414. steps++;
  2415. #ifdef AHD_DEBUG
  2416. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  2417. printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
  2418. seqaddr);
  2419. #endif
  2420. if (stepping == FALSE) {
  2421. first_instr = seqaddr;
  2422. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2423. simode0 = ahd_inb(ahd, SIMODE0);
  2424. simode3 = ahd_inb(ahd, SIMODE3);
  2425. lqimode0 = ahd_inb(ahd, LQIMODE0);
  2426. lqimode1 = ahd_inb(ahd, LQIMODE1);
  2427. lqomode0 = ahd_inb(ahd, LQOMODE0);
  2428. lqomode1 = ahd_inb(ahd, LQOMODE1);
  2429. ahd_outb(ahd, SIMODE0, 0);
  2430. ahd_outb(ahd, SIMODE3, 0);
  2431. ahd_outb(ahd, LQIMODE0, 0);
  2432. ahd_outb(ahd, LQIMODE1, 0);
  2433. ahd_outb(ahd, LQOMODE0, 0);
  2434. ahd_outb(ahd, LQOMODE1, 0);
  2435. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2436. simode1 = ahd_inb(ahd, SIMODE1);
  2437. /*
  2438. * We don't clear ENBUSFREE. Unfortunately
  2439. * we cannot re-enable busfree detection within
  2440. * the current connection, so we must leave it
  2441. * on while single stepping.
  2442. */
  2443. ahd_outb(ahd, SIMODE1, simode1 & ENBUSFREE);
  2444. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
  2445. stepping = TRUE;
  2446. }
  2447. ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
  2448. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2449. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  2450. ahd_outb(ahd, HCNTRL, ahd->unpause);
  2451. while (!ahd_is_paused(ahd))
  2452. ahd_delay(200);
  2453. ahd_update_modes(ahd);
  2454. }
  2455. if (stepping) {
  2456. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  2457. ahd_outb(ahd, SIMODE0, simode0);
  2458. ahd_outb(ahd, SIMODE3, simode3);
  2459. ahd_outb(ahd, LQIMODE0, lqimode0);
  2460. ahd_outb(ahd, LQIMODE1, lqimode1);
  2461. ahd_outb(ahd, LQOMODE0, lqomode0);
  2462. ahd_outb(ahd, LQOMODE1, lqomode1);
  2463. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  2464. ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
  2465. ahd_outb(ahd, SIMODE1, simode1);
  2466. /*
  2467. * SCSIINT seems to glitch occassionally when
  2468. * the interrupt masks are restored. Clear SCSIINT
  2469. * one more time so that only persistent errors
  2470. * are seen as a real interrupt.
  2471. */
  2472. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2473. }
  2474. ahd_restore_modes(ahd, saved_modes);
  2475. }
  2476. /*
  2477. * Clear any pending interrupt status.
  2478. */
  2479. void
  2480. ahd_clear_intstat(struct ahd_softc *ahd)
  2481. {
  2482. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  2483. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  2484. /* Clear any interrupt conditions this may have caused */
  2485. ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
  2486. |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
  2487. ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
  2488. |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
  2489. |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
  2490. ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
  2491. |CLRLQOATNPKT|CLRLQOTCRC);
  2492. ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
  2493. |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
  2494. if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
  2495. ahd_outb(ahd, CLRLQOINT0, 0);
  2496. ahd_outb(ahd, CLRLQOINT1, 0);
  2497. }
  2498. ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
  2499. ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  2500. |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
  2501. ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
  2502. |CLRIOERR|CLROVERRUN);
  2503. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  2504. }
  2505. /**************************** Debugging Routines ******************************/
  2506. #ifdef AHD_DEBUG
  2507. uint32_t ahd_debug = AHD_DEBUG_OPTS;
  2508. #endif
  2509. void
  2510. ahd_print_scb(struct scb *scb)
  2511. {
  2512. struct hardware_scb *hscb;
  2513. int i;
  2514. hscb = scb->hscb;
  2515. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
  2516. (void *)scb,
  2517. hscb->control,
  2518. hscb->scsiid,
  2519. hscb->lun,
  2520. hscb->cdb_len);
  2521. printf("Shared Data: ");
  2522. for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
  2523. printf("%#02x", hscb->shared_data.idata.cdb[i]);
  2524. printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
  2525. (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
  2526. (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
  2527. ahd_le32toh(hscb->datacnt),
  2528. ahd_le32toh(hscb->sgptr),
  2529. SCB_GET_TAG(scb));
  2530. ahd_dump_sglist(scb);
  2531. }
  2532. void
  2533. ahd_dump_sglist(struct scb *scb)
  2534. {
  2535. int i;
  2536. if (scb->sg_count > 0) {
  2537. if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
  2538. struct ahd_dma64_seg *sg_list;
  2539. sg_list = (struct ahd_dma64_seg*)scb->sg_list;
  2540. for (i = 0; i < scb->sg_count; i++) {
  2541. uint64_t addr;
  2542. uint32_t len;
  2543. addr = ahd_le64toh(sg_list[i].addr);
  2544. len = ahd_le32toh(sg_list[i].len);
  2545. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2546. i,
  2547. (uint32_t)((addr >> 32) & 0xFFFFFFFF),
  2548. (uint32_t)(addr & 0xFFFFFFFF),
  2549. sg_list[i].len & AHD_SG_LEN_MASK,
  2550. (sg_list[i].len & AHD_DMA_LAST_SEG)
  2551. ? " Last" : "");
  2552. }
  2553. } else {
  2554. struct ahd_dma_seg *sg_list;
  2555. sg_list = (struct ahd_dma_seg*)scb->sg_list;
  2556. for (i = 0; i < scb->sg_count; i++) {
  2557. uint32_t len;
  2558. len = ahd_le32toh(sg_list[i].len);
  2559. printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
  2560. i,
  2561. (len & AHD_SG_HIGH_ADDR_MASK) >> 24,
  2562. ahd_le32toh(sg_list[i].addr),
  2563. len & AHD_SG_LEN_MASK,
  2564. len & AHD_DMA_LAST_SEG ? " Last" : "");
  2565. }
  2566. }
  2567. }
  2568. }
  2569. /************************* Transfer Negotiation *******************************/
  2570. /*
  2571. * Allocate per target mode instance (ID we respond to as a target)
  2572. * transfer negotiation data structures.
  2573. */
  2574. static struct ahd_tmode_tstate *
  2575. ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
  2576. {
  2577. struct ahd_tmode_tstate *master_tstate;
  2578. struct ahd_tmode_tstate *tstate;
  2579. int i;
  2580. master_tstate = ahd->enabled_targets[ahd->our_id];
  2581. if (ahd->enabled_targets[scsi_id] != NULL
  2582. && ahd->enabled_targets[scsi_id] != master_tstate)
  2583. panic("%s: ahd_alloc_tstate - Target already allocated",
  2584. ahd_name(ahd));
  2585. tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
  2586. if (tstate == NULL)
  2587. return (NULL);
  2588. /*
  2589. * If we have allocated a master tstate, copy user settings from
  2590. * the master tstate (taken from SRAM or the EEPROM) for this
  2591. * channel, but reset our current and goal settings to async/narrow
  2592. * until an initiator talks to us.
  2593. */
  2594. if (master_tstate != NULL) {
  2595. memcpy(tstate, master_tstate, sizeof(*tstate));
  2596. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  2597. for (i = 0; i < 16; i++) {
  2598. memset(&tstate->transinfo[i].curr, 0,
  2599. sizeof(tstate->transinfo[i].curr));
  2600. memset(&tstate->transinfo[i].goal, 0,
  2601. sizeof(tstate->transinfo[i].goal));
  2602. }
  2603. } else
  2604. memset(tstate, 0, sizeof(*tstate));
  2605. ahd->enabled_targets[scsi_id] = tstate;
  2606. return (tstate);
  2607. }
  2608. #ifdef AHD_TARGET_MODE
  2609. /*
  2610. * Free per target mode instance (ID we respond to as a target)
  2611. * transfer negotiation data structures.
  2612. */
  2613. static void
  2614. ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
  2615. {
  2616. struct ahd_tmode_tstate *tstate;
  2617. /*
  2618. * Don't clean up our "master" tstate.
  2619. * It has our default user settings.
  2620. */
  2621. if (scsi_id == ahd->our_id
  2622. && force == FALSE)
  2623. return;
  2624. tstate = ahd->enabled_targets[scsi_id];
  2625. if (tstate != NULL)
  2626. free(tstate, M_DEVBUF);
  2627. ahd->enabled_targets[scsi_id] = NULL;
  2628. }
  2629. #endif
  2630. /*
  2631. * Called when we have an active connection to a target on the bus,
  2632. * this function finds the nearest period to the input period limited
  2633. * by the capabilities of the bus connectivity of and sync settings for
  2634. * the target.
  2635. */
  2636. void
  2637. ahd_devlimited_syncrate(struct ahd_softc *ahd,
  2638. struct ahd_initiator_tinfo *tinfo,
  2639. u_int *period, u_int *ppr_options, role_t role)
  2640. {
  2641. struct ahd_transinfo *transinfo;
  2642. u_int maxsync;
  2643. if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
  2644. && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
  2645. maxsync = AHD_SYNCRATE_PACED;
  2646. } else {
  2647. maxsync = AHD_SYNCRATE_ULTRA;
  2648. /* Can't do DT related options on an SE bus */
  2649. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2650. }
  2651. /*
  2652. * Never allow a value higher than our current goal
  2653. * period otherwise we may allow a target initiated
  2654. * negotiation to go above the limit as set by the
  2655. * user. In the case of an initiator initiated
  2656. * sync negotiation, we limit based on the user
  2657. * setting. This allows the system to still accept
  2658. * incoming negotiations even if target initiated
  2659. * negotiation is not performed.
  2660. */
  2661. if (role == ROLE_TARGET)
  2662. transinfo = &tinfo->user;
  2663. else
  2664. transinfo = &tinfo->goal;
  2665. *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
  2666. if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
  2667. maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
  2668. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2669. }
  2670. if (transinfo->period == 0) {
  2671. *period = 0;
  2672. *ppr_options = 0;
  2673. } else {
  2674. *period = MAX(*period, transinfo->period);
  2675. ahd_find_syncrate(ahd, period, ppr_options, maxsync);
  2676. }
  2677. }
  2678. /*
  2679. * Look up the valid period to SCSIRATE conversion in our table.
  2680. * Return the period and offset that should be sent to the target
  2681. * if this was the beginning of an SDTR.
  2682. */
  2683. void
  2684. ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
  2685. u_int *ppr_options, u_int maxsync)
  2686. {
  2687. if (*period < maxsync)
  2688. *period = maxsync;
  2689. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
  2690. && *period > AHD_SYNCRATE_MIN_DT)
  2691. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2692. if (*period > AHD_SYNCRATE_MIN)
  2693. *period = 0;
  2694. /* Honor PPR option conformance rules. */
  2695. if (*period > AHD_SYNCRATE_PACED)
  2696. *ppr_options &= ~MSG_EXT_PPR_RTI;
  2697. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  2698. *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
  2699. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
  2700. *ppr_options &= MSG_EXT_PPR_QAS_REQ;
  2701. /* Skip all PACED only entries if IU is not available */
  2702. if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
  2703. && *period < AHD_SYNCRATE_DT)
  2704. *period = AHD_SYNCRATE_DT;
  2705. /* Skip all DT only entries if DT is not available */
  2706. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  2707. && *period < AHD_SYNCRATE_ULTRA2)
  2708. *period = AHD_SYNCRATE_ULTRA2;
  2709. }
  2710. /*
  2711. * Truncate the given synchronous offset to a value the
  2712. * current adapter type and syncrate are capable of.
  2713. */
  2714. void
  2715. ahd_validate_offset(struct ahd_softc *ahd,
  2716. struct ahd_initiator_tinfo *tinfo,
  2717. u_int period, u_int *offset, int wide,
  2718. role_t role)
  2719. {
  2720. u_int maxoffset;
  2721. /* Limit offset to what we can do */
  2722. if (period == 0)
  2723. maxoffset = 0;
  2724. else if (period <= AHD_SYNCRATE_PACED) {
  2725. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
  2726. maxoffset = MAX_OFFSET_PACED_BUG;
  2727. else
  2728. maxoffset = MAX_OFFSET_PACED;
  2729. } else
  2730. maxoffset = MAX_OFFSET_NON_PACED;
  2731. *offset = MIN(*offset, maxoffset);
  2732. if (tinfo != NULL) {
  2733. if (role == ROLE_TARGET)
  2734. *offset = MIN(*offset, tinfo->user.offset);
  2735. else
  2736. *offset = MIN(*offset, tinfo->goal.offset);
  2737. }
  2738. }
  2739. /*
  2740. * Truncate the given transfer width parameter to a value the
  2741. * current adapter type is capable of.
  2742. */
  2743. void
  2744. ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
  2745. u_int *bus_width, role_t role)
  2746. {
  2747. switch (*bus_width) {
  2748. default:
  2749. if (ahd->features & AHD_WIDE) {
  2750. /* Respond Wide */
  2751. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  2752. break;
  2753. }
  2754. /* FALLTHROUGH */
  2755. case MSG_EXT_WDTR_BUS_8_BIT:
  2756. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  2757. break;
  2758. }
  2759. if (tinfo != NULL) {
  2760. if (role == ROLE_TARGET)
  2761. *bus_width = MIN(tinfo->user.width, *bus_width);
  2762. else
  2763. *bus_width = MIN(tinfo->goal.width, *bus_width);
  2764. }
  2765. }
  2766. /*
  2767. * Update the bitmask of targets for which the controller should
  2768. * negotiate with at the next convenient oportunity. This currently
  2769. * means the next time we send the initial identify messages for
  2770. * a new transaction.
  2771. */
  2772. int
  2773. ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2774. struct ahd_tmode_tstate *tstate,
  2775. struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
  2776. {
  2777. u_int auto_negotiate_orig;
  2778. auto_negotiate_orig = tstate->auto_negotiate;
  2779. if (neg_type == AHD_NEG_ALWAYS) {
  2780. /*
  2781. * Force our "current" settings to be
  2782. * unknown so that unless a bus reset
  2783. * occurs the need to renegotiate is
  2784. * recorded persistently.
  2785. */
  2786. if ((ahd->features & AHD_WIDE) != 0)
  2787. tinfo->curr.width = AHD_WIDTH_UNKNOWN;
  2788. tinfo->curr.period = AHD_PERIOD_UNKNOWN;
  2789. tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
  2790. }
  2791. if (tinfo->curr.period != tinfo->goal.period
  2792. || tinfo->curr.width != tinfo->goal.width
  2793. || tinfo->curr.offset != tinfo->goal.offset
  2794. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  2795. || (neg_type == AHD_NEG_IF_NON_ASYNC
  2796. && (tinfo->goal.offset != 0
  2797. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  2798. || tinfo->goal.ppr_options != 0)))
  2799. tstate->auto_negotiate |= devinfo->target_mask;
  2800. else
  2801. tstate->auto_negotiate &= ~devinfo->target_mask;
  2802. return (auto_negotiate_orig != tstate->auto_negotiate);
  2803. }
  2804. /*
  2805. * Update the user/goal/curr tables of synchronous negotiation
  2806. * parameters as well as, in the case of a current or active update,
  2807. * any data structures on the host controller. In the case of an
  2808. * active update, the specified target is currently talking to us on
  2809. * the bus, so the transfer parameter update must take effect
  2810. * immediately.
  2811. */
  2812. void
  2813. ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2814. u_int period, u_int offset, u_int ppr_options,
  2815. u_int type, int paused)
  2816. {
  2817. struct ahd_initiator_tinfo *tinfo;
  2818. struct ahd_tmode_tstate *tstate;
  2819. u_int old_period;
  2820. u_int old_offset;
  2821. u_int old_ppr;
  2822. int active;
  2823. int update_needed;
  2824. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2825. update_needed = 0;
  2826. if (period == 0 || offset == 0) {
  2827. period = 0;
  2828. offset = 0;
  2829. }
  2830. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2831. devinfo->target, &tstate);
  2832. if ((type & AHD_TRANS_USER) != 0) {
  2833. tinfo->user.period = period;
  2834. tinfo->user.offset = offset;
  2835. tinfo->user.ppr_options = ppr_options;
  2836. }
  2837. if ((type & AHD_TRANS_GOAL) != 0) {
  2838. tinfo->goal.period = period;
  2839. tinfo->goal.offset = offset;
  2840. tinfo->goal.ppr_options = ppr_options;
  2841. }
  2842. old_period = tinfo->curr.period;
  2843. old_offset = tinfo->curr.offset;
  2844. old_ppr = tinfo->curr.ppr_options;
  2845. if ((type & AHD_TRANS_CUR) != 0
  2846. && (old_period != period
  2847. || old_offset != offset
  2848. || old_ppr != ppr_options)) {
  2849. update_needed++;
  2850. tinfo->curr.period = period;
  2851. tinfo->curr.offset = offset;
  2852. tinfo->curr.ppr_options = ppr_options;
  2853. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2854. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2855. if (bootverbose) {
  2856. if (offset != 0) {
  2857. int options;
  2858. printf("%s: target %d synchronous with "
  2859. "period = 0x%x, offset = 0x%x",
  2860. ahd_name(ahd), devinfo->target,
  2861. period, offset);
  2862. options = 0;
  2863. if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
  2864. printf("(RDSTRM");
  2865. options++;
  2866. }
  2867. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
  2868. printf("%s", options ? "|DT" : "(DT");
  2869. options++;
  2870. }
  2871. if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
  2872. printf("%s", options ? "|IU" : "(IU");
  2873. options++;
  2874. }
  2875. if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
  2876. printf("%s", options ? "|RTI" : "(RTI");
  2877. options++;
  2878. }
  2879. if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
  2880. printf("%s", options ? "|QAS" : "(QAS");
  2881. options++;
  2882. }
  2883. if (options != 0)
  2884. printf(")\n");
  2885. else
  2886. printf("\n");
  2887. } else {
  2888. printf("%s: target %d using "
  2889. "asynchronous transfers%s\n",
  2890. ahd_name(ahd), devinfo->target,
  2891. (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
  2892. ? "(QAS)" : "");
  2893. }
  2894. }
  2895. }
  2896. /*
  2897. * Always refresh the neg-table to handle the case of the
  2898. * sequencer setting the ENATNO bit for a MK_MESSAGE request.
  2899. * We will always renegotiate in that case if this is a
  2900. * packetized request. Also manage the busfree expected flag
  2901. * from this common routine so that we catch changes due to
  2902. * WDTR or SDTR messages.
  2903. */
  2904. if ((type & AHD_TRANS_CUR) != 0) {
  2905. if (!paused)
  2906. ahd_pause(ahd);
  2907. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2908. if (!paused)
  2909. ahd_unpause(ahd);
  2910. if (ahd->msg_type != MSG_TYPE_NONE) {
  2911. if ((old_ppr & MSG_EXT_PPR_IU_REQ)
  2912. != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
  2913. #ifdef AHD_DEBUG
  2914. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  2915. ahd_print_devinfo(ahd, devinfo);
  2916. printf("Expecting IU Change busfree\n");
  2917. }
  2918. #endif
  2919. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  2920. | MSG_FLAG_IU_REQ_CHANGED;
  2921. }
  2922. if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
  2923. #ifdef AHD_DEBUG
  2924. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  2925. printf("PPR with IU_REQ outstanding\n");
  2926. #endif
  2927. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
  2928. }
  2929. }
  2930. }
  2931. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2932. tinfo, AHD_NEG_TO_GOAL);
  2933. if (update_needed && active)
  2934. ahd_update_pending_scbs(ahd);
  2935. }
  2936. /*
  2937. * Update the user/goal/curr tables of wide negotiation
  2938. * parameters as well as, in the case of a current or active update,
  2939. * any data structures on the host controller. In the case of an
  2940. * active update, the specified target is currently talking to us on
  2941. * the bus, so the transfer parameter update must take effect
  2942. * immediately.
  2943. */
  2944. void
  2945. ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2946. u_int width, u_int type, int paused)
  2947. {
  2948. struct ahd_initiator_tinfo *tinfo;
  2949. struct ahd_tmode_tstate *tstate;
  2950. u_int oldwidth;
  2951. int active;
  2952. int update_needed;
  2953. active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
  2954. update_needed = 0;
  2955. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  2956. devinfo->target, &tstate);
  2957. if ((type & AHD_TRANS_USER) != 0)
  2958. tinfo->user.width = width;
  2959. if ((type & AHD_TRANS_GOAL) != 0)
  2960. tinfo->goal.width = width;
  2961. oldwidth = tinfo->curr.width;
  2962. if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
  2963. update_needed++;
  2964. tinfo->curr.width = width;
  2965. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2966. CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  2967. if (bootverbose) {
  2968. printf("%s: target %d using %dbit transfers\n",
  2969. ahd_name(ahd), devinfo->target,
  2970. 8 * (0x01 << width));
  2971. }
  2972. }
  2973. if ((type & AHD_TRANS_CUR) != 0) {
  2974. if (!paused)
  2975. ahd_pause(ahd);
  2976. ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
  2977. if (!paused)
  2978. ahd_unpause(ahd);
  2979. }
  2980. update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
  2981. tinfo, AHD_NEG_TO_GOAL);
  2982. if (update_needed && active)
  2983. ahd_update_pending_scbs(ahd);
  2984. }
  2985. /*
  2986. * Update the current state of tagged queuing for a given target.
  2987. */
  2988. void
  2989. ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2990. ahd_queue_alg alg)
  2991. {
  2992. ahd_platform_set_tags(ahd, devinfo, alg);
  2993. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  2994. devinfo->lun, AC_TRANSFER_NEG, &alg);
  2995. }
  2996. static void
  2997. ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  2998. struct ahd_transinfo *tinfo)
  2999. {
  3000. ahd_mode_state saved_modes;
  3001. u_int period;
  3002. u_int ppr_opts;
  3003. u_int con_opts;
  3004. u_int offset;
  3005. u_int saved_negoaddr;
  3006. uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
  3007. saved_modes = ahd_save_modes(ahd);
  3008. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3009. saved_negoaddr = ahd_inb(ahd, NEGOADDR);
  3010. ahd_outb(ahd, NEGOADDR, devinfo->target);
  3011. period = tinfo->period;
  3012. offset = tinfo->offset;
  3013. memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
  3014. ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
  3015. |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
  3016. con_opts = 0;
  3017. if (period == 0)
  3018. period = AHD_SYNCRATE_ASYNC;
  3019. if (period == AHD_SYNCRATE_160) {
  3020. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
  3021. /*
  3022. * When the SPI4 spec was finalized, PACE transfers
  3023. * was not made a configurable option in the PPR
  3024. * message. Instead it is assumed to be enabled for
  3025. * any syncrate faster than 80MHz. Nevertheless,
  3026. * Harpoon2A4 allows this to be configurable.
  3027. *
  3028. * Harpoon2A4 also assumes at most 2 data bytes per
  3029. * negotiated REQ/ACK offset. Paced transfers take
  3030. * 4, so we must adjust our offset.
  3031. */
  3032. ppr_opts |= PPROPT_PACE;
  3033. offset *= 2;
  3034. /*
  3035. * Harpoon2A assumed that there would be a
  3036. * fallback rate between 160MHz and 80Mhz,
  3037. * so 7 is used as the period factor rather
  3038. * than 8 for 160MHz.
  3039. */
  3040. period = AHD_SYNCRATE_REVA_160;
  3041. }
  3042. if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
  3043. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
  3044. ~AHD_PRECOMP_MASK;
  3045. } else {
  3046. /*
  3047. * Precomp should be disabled for non-paced transfers.
  3048. */
  3049. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
  3050. if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
  3051. && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0
  3052. && (ppr_opts & MSG_EXT_PPR_IU_REQ) == 0) {
  3053. /*
  3054. * Slow down our CRC interval to be
  3055. * compatible with non-packetized
  3056. * U160 devices that can't handle a
  3057. * CRC at full speed.
  3058. */
  3059. con_opts |= ENSLOWCRC;
  3060. }
  3061. if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
  3062. /*
  3063. * On H2A4, revert to a slower slewrate
  3064. * on non-paced transfers.
  3065. */
  3066. iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
  3067. ~AHD_SLEWRATE_MASK;
  3068. }
  3069. }
  3070. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
  3071. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
  3072. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
  3073. ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
  3074. ahd_outb(ahd, NEGPERIOD, period);
  3075. ahd_outb(ahd, NEGPPROPTS, ppr_opts);
  3076. ahd_outb(ahd, NEGOFFSET, offset);
  3077. if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
  3078. con_opts |= WIDEXFER;
  3079. /*
  3080. * Slow down our CRC interval to be
  3081. * compatible with packetized U320 devices
  3082. * that can't handle a CRC at full speed
  3083. */
  3084. if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
  3085. con_opts |= ENSLOWCRC;
  3086. }
  3087. /*
  3088. * During packetized transfers, the target will
  3089. * give us the oportunity to send command packets
  3090. * without us asserting attention.
  3091. */
  3092. if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
  3093. con_opts |= ENAUTOATNO;
  3094. ahd_outb(ahd, NEGCONOPTS, con_opts);
  3095. ahd_outb(ahd, NEGOADDR, saved_negoaddr);
  3096. ahd_restore_modes(ahd, saved_modes);
  3097. }
  3098. /*
  3099. * When the transfer settings for a connection change, setup for
  3100. * negotiation in pending SCBs to effect the change as quickly as
  3101. * possible. We also cancel any negotiations that are scheduled
  3102. * for inflight SCBs that have not been started yet.
  3103. */
  3104. static void
  3105. ahd_update_pending_scbs(struct ahd_softc *ahd)
  3106. {
  3107. struct scb *pending_scb;
  3108. int pending_scb_count;
  3109. int paused;
  3110. u_int saved_scbptr;
  3111. ahd_mode_state saved_modes;
  3112. /*
  3113. * Traverse the pending SCB list and ensure that all of the
  3114. * SCBs there have the proper settings. We can only safely
  3115. * clear the negotiation required flag (setting requires the
  3116. * execution queue to be modified) and this is only possible
  3117. * if we are not already attempting to select out for this
  3118. * SCB. For this reason, all callers only call this routine
  3119. * if we are changing the negotiation settings for the currently
  3120. * active transaction on the bus.
  3121. */
  3122. pending_scb_count = 0;
  3123. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  3124. struct ahd_devinfo devinfo;
  3125. struct ahd_initiator_tinfo *tinfo;
  3126. struct ahd_tmode_tstate *tstate;
  3127. ahd_scb_devinfo(ahd, &devinfo, pending_scb);
  3128. tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
  3129. devinfo.our_scsiid,
  3130. devinfo.target, &tstate);
  3131. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  3132. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  3133. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  3134. pending_scb->hscb->control &= ~MK_MESSAGE;
  3135. }
  3136. ahd_sync_scb(ahd, pending_scb,
  3137. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  3138. pending_scb_count++;
  3139. }
  3140. if (pending_scb_count == 0)
  3141. return;
  3142. if (ahd_is_paused(ahd)) {
  3143. paused = 1;
  3144. } else {
  3145. paused = 0;
  3146. ahd_pause(ahd);
  3147. }
  3148. /*
  3149. * Force the sequencer to reinitialize the selection for
  3150. * the command at the head of the execution queue if it
  3151. * has already been setup. The negotiation changes may
  3152. * effect whether we select-out with ATN. It is only
  3153. * safe to clear ENSELO when the bus is not free and no
  3154. * selection is in progres or completed.
  3155. */
  3156. saved_modes = ahd_save_modes(ahd);
  3157. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3158. if ((ahd_inb(ahd, SCSISIGI) & BSYI) != 0
  3159. && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
  3160. ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
  3161. saved_scbptr = ahd_get_scbptr(ahd);
  3162. /* Ensure that the hscbs down on the card match the new information */
  3163. LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
  3164. u_int scb_tag;
  3165. u_int control;
  3166. scb_tag = SCB_GET_TAG(pending_scb);
  3167. ahd_set_scbptr(ahd, scb_tag);
  3168. control = ahd_inb_scbram(ahd, SCB_CONTROL);
  3169. control &= ~MK_MESSAGE;
  3170. control |= pending_scb->hscb->control & MK_MESSAGE;
  3171. ahd_outb(ahd, SCB_CONTROL, control);
  3172. }
  3173. ahd_set_scbptr(ahd, saved_scbptr);
  3174. ahd_restore_modes(ahd, saved_modes);
  3175. if (paused == 0)
  3176. ahd_unpause(ahd);
  3177. }
  3178. /**************************** Pathing Information *****************************/
  3179. static void
  3180. ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3181. {
  3182. ahd_mode_state saved_modes;
  3183. u_int saved_scsiid;
  3184. role_t role;
  3185. int our_id;
  3186. saved_modes = ahd_save_modes(ahd);
  3187. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3188. if (ahd_inb(ahd, SSTAT0) & TARGET)
  3189. role = ROLE_TARGET;
  3190. else
  3191. role = ROLE_INITIATOR;
  3192. if (role == ROLE_TARGET
  3193. && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
  3194. /* We were selected, so pull our id from TARGIDIN */
  3195. our_id = ahd_inb(ahd, TARGIDIN) & OID;
  3196. } else if (role == ROLE_TARGET)
  3197. our_id = ahd_inb(ahd, TOWNID);
  3198. else
  3199. our_id = ahd_inb(ahd, IOWNID);
  3200. saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
  3201. ahd_compile_devinfo(devinfo,
  3202. our_id,
  3203. SCSIID_TARGET(ahd, saved_scsiid),
  3204. ahd_inb(ahd, SAVED_LUN),
  3205. SCSIID_CHANNEL(ahd, saved_scsiid),
  3206. role);
  3207. ahd_restore_modes(ahd, saved_modes);
  3208. }
  3209. void
  3210. ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3211. {
  3212. printf("%s:%c:%d:%d: ", ahd_name(ahd), 'A',
  3213. devinfo->target, devinfo->lun);
  3214. }
  3215. struct ahd_phase_table_entry*
  3216. ahd_lookup_phase_entry(int phase)
  3217. {
  3218. struct ahd_phase_table_entry *entry;
  3219. struct ahd_phase_table_entry *last_entry;
  3220. /*
  3221. * num_phases doesn't include the default entry which
  3222. * will be returned if the phase doesn't match.
  3223. */
  3224. last_entry = &ahd_phase_table[num_phases];
  3225. for (entry = ahd_phase_table; entry < last_entry; entry++) {
  3226. if (phase == entry->phase)
  3227. break;
  3228. }
  3229. return (entry);
  3230. }
  3231. void
  3232. ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
  3233. u_int lun, char channel, role_t role)
  3234. {
  3235. devinfo->our_scsiid = our_id;
  3236. devinfo->target = target;
  3237. devinfo->lun = lun;
  3238. devinfo->target_offset = target;
  3239. devinfo->channel = channel;
  3240. devinfo->role = role;
  3241. if (channel == 'B')
  3242. devinfo->target_offset += 8;
  3243. devinfo->target_mask = (0x01 << devinfo->target_offset);
  3244. }
  3245. static void
  3246. ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3247. struct scb *scb)
  3248. {
  3249. role_t role;
  3250. int our_id;
  3251. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  3252. role = ROLE_INITIATOR;
  3253. if ((scb->hscb->control & TARGET_SCB) != 0)
  3254. role = ROLE_TARGET;
  3255. ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
  3256. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
  3257. }
  3258. /************************ Message Phase Processing ****************************/
  3259. /*
  3260. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  3261. * or enters the initial message out phase, we are interrupted. Fill our
  3262. * outgoing message buffer with the appropriate message and beging handing
  3263. * the message phase(s) manually.
  3264. */
  3265. static void
  3266. ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3267. struct scb *scb)
  3268. {
  3269. /*
  3270. * To facilitate adding multiple messages together,
  3271. * each routine should increment the index and len
  3272. * variables instead of setting them explicitly.
  3273. */
  3274. ahd->msgout_index = 0;
  3275. ahd->msgout_len = 0;
  3276. if (ahd_currently_packetized(ahd))
  3277. ahd->msg_flags |= MSG_FLAG_PACKETIZED;
  3278. if (ahd->send_msg_perror
  3279. && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
  3280. ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
  3281. ahd->msgout_len++;
  3282. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3283. #ifdef AHD_DEBUG
  3284. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3285. printf("Setting up for Parity Error delivery\n");
  3286. #endif
  3287. return;
  3288. } else if (scb == NULL) {
  3289. printf("%s: WARNING. No pending message for "
  3290. "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
  3291. ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
  3292. ahd->msgout_len++;
  3293. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3294. return;
  3295. }
  3296. if ((scb->flags & SCB_DEVICE_RESET) == 0
  3297. && (scb->flags & SCB_PACKETIZED) == 0
  3298. && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
  3299. u_int identify_msg;
  3300. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  3301. if ((scb->hscb->control & DISCENB) != 0)
  3302. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  3303. ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
  3304. ahd->msgout_len++;
  3305. if ((scb->hscb->control & TAG_ENB) != 0) {
  3306. ahd->msgout_buf[ahd->msgout_index++] =
  3307. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  3308. ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
  3309. ahd->msgout_len += 2;
  3310. }
  3311. }
  3312. if (scb->flags & SCB_DEVICE_RESET) {
  3313. ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
  3314. ahd->msgout_len++;
  3315. ahd_print_path(ahd, scb);
  3316. printf("Bus Device Reset Message Sent\n");
  3317. /*
  3318. * Clear our selection hardware in advance of
  3319. * the busfree. We may have an entry in the waiting
  3320. * Q for this target, and we don't want to go about
  3321. * selecting while we handle the busfree and blow it
  3322. * away.
  3323. */
  3324. ahd_outb(ahd, SCSISEQ0, 0);
  3325. } else if ((scb->flags & SCB_ABORT) != 0) {
  3326. if ((scb->hscb->control & TAG_ENB) != 0) {
  3327. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
  3328. } else {
  3329. ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
  3330. }
  3331. ahd->msgout_len++;
  3332. ahd_print_path(ahd, scb);
  3333. printf("Abort%s Message Sent\n",
  3334. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  3335. /*
  3336. * Clear our selection hardware in advance of
  3337. * the busfree. We may have an entry in the waiting
  3338. * Q for this target, and we don't want to go about
  3339. * selecting while we handle the busfree and blow it
  3340. * away.
  3341. */
  3342. ahd_outb(ahd, SCSISEQ0, 0);
  3343. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  3344. ahd_build_transfer_msg(ahd, devinfo);
  3345. /*
  3346. * Clear our selection hardware in advance of potential
  3347. * PPR IU status change busfree. We may have an entry in
  3348. * the waiting Q for this target, and we don't want to go
  3349. * about selecting while we handle the busfree and blow
  3350. * it away.
  3351. */
  3352. ahd_outb(ahd, SCSISEQ0, 0);
  3353. } else {
  3354. printf("ahd_intr: AWAITING_MSG for an SCB that "
  3355. "does not have a waiting message\n");
  3356. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  3357. devinfo->target_mask);
  3358. panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
  3359. "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
  3360. ahd_inb_scbram(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
  3361. scb->flags);
  3362. }
  3363. /*
  3364. * Clear the MK_MESSAGE flag from the SCB so we aren't
  3365. * asked to send this message again.
  3366. */
  3367. ahd_outb(ahd, SCB_CONTROL,
  3368. ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
  3369. scb->hscb->control &= ~MK_MESSAGE;
  3370. ahd->msgout_index = 0;
  3371. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3372. }
  3373. /*
  3374. * Build an appropriate transfer negotiation message for the
  3375. * currently active target.
  3376. */
  3377. static void
  3378. ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3379. {
  3380. /*
  3381. * We need to initiate transfer negotiations.
  3382. * If our current and goal settings are identical,
  3383. * we want to renegotiate due to a check condition.
  3384. */
  3385. struct ahd_initiator_tinfo *tinfo;
  3386. struct ahd_tmode_tstate *tstate;
  3387. int dowide;
  3388. int dosync;
  3389. int doppr;
  3390. u_int period;
  3391. u_int ppr_options;
  3392. u_int offset;
  3393. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3394. devinfo->target, &tstate);
  3395. /*
  3396. * Filter our period based on the current connection.
  3397. * If we can't perform DT transfers on this segment (not in LVD
  3398. * mode for instance), then our decision to issue a PPR message
  3399. * may change.
  3400. */
  3401. period = tinfo->goal.period;
  3402. offset = tinfo->goal.offset;
  3403. ppr_options = tinfo->goal.ppr_options;
  3404. /* Target initiated PPR is not allowed in the SCSI spec */
  3405. if (devinfo->role == ROLE_TARGET)
  3406. ppr_options = 0;
  3407. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3408. &ppr_options, devinfo->role);
  3409. dowide = tinfo->curr.width != tinfo->goal.width;
  3410. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  3411. /*
  3412. * Only use PPR if we have options that need it, even if the device
  3413. * claims to support it. There might be an expander in the way
  3414. * that doesn't.
  3415. */
  3416. doppr = ppr_options != 0;
  3417. if (!dowide && !dosync && !doppr) {
  3418. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  3419. dosync = tinfo->goal.offset != 0;
  3420. }
  3421. if (!dowide && !dosync && !doppr) {
  3422. /*
  3423. * Force async with a WDTR message if we have a wide bus,
  3424. * or just issue an SDTR with a 0 offset.
  3425. */
  3426. if ((ahd->features & AHD_WIDE) != 0)
  3427. dowide = 1;
  3428. else
  3429. dosync = 1;
  3430. if (bootverbose) {
  3431. ahd_print_devinfo(ahd, devinfo);
  3432. printf("Ensuring async\n");
  3433. }
  3434. }
  3435. /* Target initiated PPR is not allowed in the SCSI spec */
  3436. if (devinfo->role == ROLE_TARGET)
  3437. doppr = 0;
  3438. /*
  3439. * Both the PPR message and SDTR message require the
  3440. * goal syncrate to be limited to what the target device
  3441. * is capable of handling (based on whether an LVD->SE
  3442. * expander is on the bus), so combine these two cases.
  3443. * Regardless, guarantee that if we are using WDTR and SDTR
  3444. * messages that WDTR comes first.
  3445. */
  3446. if (doppr || (dosync && !dowide)) {
  3447. offset = tinfo->goal.offset;
  3448. ahd_validate_offset(ahd, tinfo, period, &offset,
  3449. doppr ? tinfo->goal.width
  3450. : tinfo->curr.width,
  3451. devinfo->role);
  3452. if (doppr) {
  3453. ahd_construct_ppr(ahd, devinfo, period, offset,
  3454. tinfo->goal.width, ppr_options);
  3455. } else {
  3456. ahd_construct_sdtr(ahd, devinfo, period, offset);
  3457. }
  3458. } else {
  3459. ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
  3460. }
  3461. }
  3462. /*
  3463. * Build a synchronous negotiation message in our message
  3464. * buffer based on the input parameters.
  3465. */
  3466. static void
  3467. ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3468. u_int period, u_int offset)
  3469. {
  3470. if (offset == 0)
  3471. period = AHD_ASYNC_XFER_PERIOD;
  3472. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3473. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
  3474. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
  3475. ahd->msgout_buf[ahd->msgout_index++] = period;
  3476. ahd->msgout_buf[ahd->msgout_index++] = offset;
  3477. ahd->msgout_len += 5;
  3478. if (bootverbose) {
  3479. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  3480. ahd_name(ahd), devinfo->channel, devinfo->target,
  3481. devinfo->lun, period, offset);
  3482. }
  3483. }
  3484. /*
  3485. * Build a wide negotiateion message in our message
  3486. * buffer based on the input parameters.
  3487. */
  3488. static void
  3489. ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3490. u_int bus_width)
  3491. {
  3492. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3493. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
  3494. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
  3495. ahd->msgout_buf[ahd->msgout_index++] = bus_width;
  3496. ahd->msgout_len += 4;
  3497. if (bootverbose) {
  3498. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  3499. ahd_name(ahd), devinfo->channel, devinfo->target,
  3500. devinfo->lun, bus_width);
  3501. }
  3502. }
  3503. /*
  3504. * Build a parallel protocol request message in our message
  3505. * buffer based on the input parameters.
  3506. */
  3507. static void
  3508. ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  3509. u_int period, u_int offset, u_int bus_width,
  3510. u_int ppr_options)
  3511. {
  3512. /*
  3513. * Always request precompensation from
  3514. * the other target if we are running
  3515. * at paced syncrates.
  3516. */
  3517. if (period <= AHD_SYNCRATE_PACED)
  3518. ppr_options |= MSG_EXT_PPR_PCOMP_EN;
  3519. if (offset == 0)
  3520. period = AHD_ASYNC_XFER_PERIOD;
  3521. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
  3522. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
  3523. ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
  3524. ahd->msgout_buf[ahd->msgout_index++] = period;
  3525. ahd->msgout_buf[ahd->msgout_index++] = 0;
  3526. ahd->msgout_buf[ahd->msgout_index++] = offset;
  3527. ahd->msgout_buf[ahd->msgout_index++] = bus_width;
  3528. ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
  3529. ahd->msgout_len += 8;
  3530. if (bootverbose) {
  3531. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  3532. "offset %x, ppr_options %x\n", ahd_name(ahd),
  3533. devinfo->channel, devinfo->target, devinfo->lun,
  3534. bus_width, period, offset, ppr_options);
  3535. }
  3536. }
  3537. /*
  3538. * Clear any active message state.
  3539. */
  3540. static void
  3541. ahd_clear_msg_state(struct ahd_softc *ahd)
  3542. {
  3543. ahd_mode_state saved_modes;
  3544. saved_modes = ahd_save_modes(ahd);
  3545. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  3546. ahd->send_msg_perror = 0;
  3547. ahd->msg_flags = MSG_FLAG_NONE;
  3548. ahd->msgout_len = 0;
  3549. ahd->msgin_index = 0;
  3550. ahd->msg_type = MSG_TYPE_NONE;
  3551. if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
  3552. /*
  3553. * The target didn't care to respond to our
  3554. * message request, so clear ATN.
  3555. */
  3556. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3557. }
  3558. ahd_outb(ahd, MSG_OUT, MSG_NOOP);
  3559. ahd_outb(ahd, SEQ_FLAGS2,
  3560. ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  3561. ahd_restore_modes(ahd, saved_modes);
  3562. }
  3563. /*
  3564. * Manual message loop handler.
  3565. */
  3566. static void
  3567. ahd_handle_message_phase(struct ahd_softc *ahd)
  3568. {
  3569. struct ahd_devinfo devinfo;
  3570. u_int bus_phase;
  3571. int end_session;
  3572. ahd_fetch_devinfo(ahd, &devinfo);
  3573. end_session = FALSE;
  3574. bus_phase = ahd_inb(ahd, LASTPHASE);
  3575. if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
  3576. printf("LQIRETRY for LQIPHASE_OUTPKT\n");
  3577. ahd_outb(ahd, LQCTL2, LQIRETRY);
  3578. }
  3579. reswitch:
  3580. switch (ahd->msg_type) {
  3581. case MSG_TYPE_INITIATOR_MSGOUT:
  3582. {
  3583. int lastbyte;
  3584. int phasemis;
  3585. int msgdone;
  3586. if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
  3587. panic("HOST_MSG_LOOP interrupt with no active message");
  3588. #ifdef AHD_DEBUG
  3589. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3590. ahd_print_devinfo(ahd, &devinfo);
  3591. printf("INITIATOR_MSG_OUT");
  3592. }
  3593. #endif
  3594. phasemis = bus_phase != P_MESGOUT;
  3595. if (phasemis) {
  3596. #ifdef AHD_DEBUG
  3597. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3598. printf(" PHASEMIS %s\n",
  3599. ahd_lookup_phase_entry(bus_phase)
  3600. ->phasemsg);
  3601. }
  3602. #endif
  3603. if (bus_phase == P_MESGIN) {
  3604. /*
  3605. * Change gears and see if
  3606. * this messages is of interest to
  3607. * us or should be passed back to
  3608. * the sequencer.
  3609. */
  3610. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3611. ahd->send_msg_perror = 0;
  3612. ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  3613. ahd->msgin_index = 0;
  3614. goto reswitch;
  3615. }
  3616. end_session = TRUE;
  3617. break;
  3618. }
  3619. if (ahd->send_msg_perror) {
  3620. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3621. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3622. #ifdef AHD_DEBUG
  3623. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3624. printf(" byte 0x%x\n", ahd->send_msg_perror);
  3625. #endif
  3626. /*
  3627. * If we are notifying the target of a CRC error
  3628. * during packetized operations, the target is
  3629. * within its rights to acknowledge our message
  3630. * with a busfree.
  3631. */
  3632. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
  3633. && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
  3634. ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
  3635. ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
  3636. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3637. break;
  3638. }
  3639. msgdone = ahd->msgout_index == ahd->msgout_len;
  3640. if (msgdone) {
  3641. /*
  3642. * The target has requested a retry.
  3643. * Re-assert ATN, reset our message index to
  3644. * 0, and try again.
  3645. */
  3646. ahd->msgout_index = 0;
  3647. ahd_assert_atn(ahd);
  3648. }
  3649. lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
  3650. if (lastbyte) {
  3651. /* Last byte is signified by dropping ATN */
  3652. ahd_outb(ahd, CLRSINT1, CLRATNO);
  3653. }
  3654. /*
  3655. * Clear our interrupt status and present
  3656. * the next byte on the bus.
  3657. */
  3658. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3659. #ifdef AHD_DEBUG
  3660. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3661. printf(" byte 0x%x\n",
  3662. ahd->msgout_buf[ahd->msgout_index]);
  3663. #endif
  3664. ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
  3665. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
  3666. break;
  3667. }
  3668. case MSG_TYPE_INITIATOR_MSGIN:
  3669. {
  3670. int phasemis;
  3671. int message_done;
  3672. #ifdef AHD_DEBUG
  3673. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3674. ahd_print_devinfo(ahd, &devinfo);
  3675. printf("INITIATOR_MSG_IN");
  3676. }
  3677. #endif
  3678. phasemis = bus_phase != P_MESGIN;
  3679. if (phasemis) {
  3680. #ifdef AHD_DEBUG
  3681. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3682. printf(" PHASEMIS %s\n",
  3683. ahd_lookup_phase_entry(bus_phase)
  3684. ->phasemsg);
  3685. }
  3686. #endif
  3687. ahd->msgin_index = 0;
  3688. if (bus_phase == P_MESGOUT
  3689. && (ahd->send_msg_perror != 0
  3690. || (ahd->msgout_len != 0
  3691. && ahd->msgout_index == 0))) {
  3692. ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3693. goto reswitch;
  3694. }
  3695. end_session = TRUE;
  3696. break;
  3697. }
  3698. /* Pull the byte in without acking it */
  3699. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
  3700. #ifdef AHD_DEBUG
  3701. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  3702. printf(" byte 0x%x\n",
  3703. ahd->msgin_buf[ahd->msgin_index]);
  3704. #endif
  3705. message_done = ahd_parse_msg(ahd, &devinfo);
  3706. if (message_done) {
  3707. /*
  3708. * Clear our incoming message buffer in case there
  3709. * is another message following this one.
  3710. */
  3711. ahd->msgin_index = 0;
  3712. /*
  3713. * If this message illicited a response,
  3714. * assert ATN so the target takes us to the
  3715. * message out phase.
  3716. */
  3717. if (ahd->msgout_len != 0) {
  3718. #ifdef AHD_DEBUG
  3719. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
  3720. ahd_print_devinfo(ahd, &devinfo);
  3721. printf("Asserting ATN for response\n");
  3722. }
  3723. #endif
  3724. ahd_assert_atn(ahd);
  3725. }
  3726. } else
  3727. ahd->msgin_index++;
  3728. if (message_done == MSGLOOP_TERMINATED) {
  3729. end_session = TRUE;
  3730. } else {
  3731. /* Ack the byte */
  3732. ahd_outb(ahd, CLRSINT1, CLRREQINIT);
  3733. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
  3734. }
  3735. break;
  3736. }
  3737. case MSG_TYPE_TARGET_MSGIN:
  3738. {
  3739. int msgdone;
  3740. int msgout_request;
  3741. /*
  3742. * By default, the message loop will continue.
  3743. */
  3744. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3745. if (ahd->msgout_len == 0)
  3746. panic("Target MSGIN with no active message");
  3747. /*
  3748. * If we interrupted a mesgout session, the initiator
  3749. * will not know this until our first REQ. So, we
  3750. * only honor mesgout requests after we've sent our
  3751. * first byte.
  3752. */
  3753. if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
  3754. && ahd->msgout_index > 0)
  3755. msgout_request = TRUE;
  3756. else
  3757. msgout_request = FALSE;
  3758. if (msgout_request) {
  3759. /*
  3760. * Change gears and see if
  3761. * this messages is of interest to
  3762. * us or should be passed back to
  3763. * the sequencer.
  3764. */
  3765. ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
  3766. ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
  3767. ahd->msgin_index = 0;
  3768. /* Dummy read to REQ for first byte */
  3769. ahd_inb(ahd, SCSIDAT);
  3770. ahd_outb(ahd, SXFRCTL0,
  3771. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3772. break;
  3773. }
  3774. msgdone = ahd->msgout_index == ahd->msgout_len;
  3775. if (msgdone) {
  3776. ahd_outb(ahd, SXFRCTL0,
  3777. ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3778. end_session = TRUE;
  3779. break;
  3780. }
  3781. /*
  3782. * Present the next byte on the bus.
  3783. */
  3784. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3785. ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
  3786. break;
  3787. }
  3788. case MSG_TYPE_TARGET_MSGOUT:
  3789. {
  3790. int lastbyte;
  3791. int msgdone;
  3792. /*
  3793. * By default, the message loop will continue.
  3794. */
  3795. ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
  3796. /*
  3797. * The initiator signals that this is
  3798. * the last byte by dropping ATN.
  3799. */
  3800. lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
  3801. /*
  3802. * Read the latched byte, but turn off SPIOEN first
  3803. * so that we don't inadvertently cause a REQ for the
  3804. * next byte.
  3805. */
  3806. ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
  3807. ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
  3808. msgdone = ahd_parse_msg(ahd, &devinfo);
  3809. if (msgdone == MSGLOOP_TERMINATED) {
  3810. /*
  3811. * The message is *really* done in that it caused
  3812. * us to go to bus free. The sequencer has already
  3813. * been reset at this point, so pull the ejection
  3814. * handle.
  3815. */
  3816. return;
  3817. }
  3818. ahd->msgin_index++;
  3819. /*
  3820. * XXX Read spec about initiator dropping ATN too soon
  3821. * and use msgdone to detect it.
  3822. */
  3823. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  3824. ahd->msgin_index = 0;
  3825. /*
  3826. * If this message illicited a response, transition
  3827. * to the Message in phase and send it.
  3828. */
  3829. if (ahd->msgout_len != 0) {
  3830. ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
  3831. ahd_outb(ahd, SXFRCTL0,
  3832. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3833. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  3834. ahd->msgin_index = 0;
  3835. break;
  3836. }
  3837. }
  3838. if (lastbyte)
  3839. end_session = TRUE;
  3840. else {
  3841. /* Ask for the next byte. */
  3842. ahd_outb(ahd, SXFRCTL0,
  3843. ahd_inb(ahd, SXFRCTL0) | SPIOEN);
  3844. }
  3845. break;
  3846. }
  3847. default:
  3848. panic("Unknown REQINIT message type");
  3849. }
  3850. if (end_session) {
  3851. if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
  3852. printf("%s: Returning to Idle Loop\n",
  3853. ahd_name(ahd));
  3854. ahd_clear_msg_state(ahd);
  3855. /*
  3856. * Perform the equivalent of a clear_target_state.
  3857. */
  3858. ahd_outb(ahd, LASTPHASE, P_BUSFREE);
  3859. ahd_outb(ahd, SEQ_FLAGS, NOT_IDENTIFIED|NO_CDB_SENT);
  3860. ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
  3861. } else {
  3862. ahd_clear_msg_state(ahd);
  3863. ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
  3864. }
  3865. }
  3866. }
  3867. /*
  3868. * See if we sent a particular extended message to the target.
  3869. * If "full" is true, return true only if the target saw the full
  3870. * message. If "full" is false, return true if the target saw at
  3871. * least the first byte of the message.
  3872. */
  3873. static int
  3874. ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
  3875. {
  3876. int found;
  3877. u_int index;
  3878. found = FALSE;
  3879. index = 0;
  3880. while (index < ahd->msgout_len) {
  3881. if (ahd->msgout_buf[index] == MSG_EXTENDED) {
  3882. u_int end_index;
  3883. end_index = index + 1 + ahd->msgout_buf[index + 1];
  3884. if (ahd->msgout_buf[index+2] == msgval
  3885. && type == AHDMSG_EXT) {
  3886. if (full) {
  3887. if (ahd->msgout_index > end_index)
  3888. found = TRUE;
  3889. } else if (ahd->msgout_index > index)
  3890. found = TRUE;
  3891. }
  3892. index = end_index;
  3893. } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
  3894. && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  3895. /* Skip tag type and tag id or residue param*/
  3896. index += 2;
  3897. } else {
  3898. /* Single byte message */
  3899. if (type == AHDMSG_1B
  3900. && ahd->msgout_index > index
  3901. && (ahd->msgout_buf[index] == msgval
  3902. || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
  3903. && msgval == MSG_IDENTIFYFLAG)))
  3904. found = TRUE;
  3905. index++;
  3906. }
  3907. if (found)
  3908. break;
  3909. }
  3910. return (found);
  3911. }
  3912. /*
  3913. * Wait for a complete incoming message, parse it, and respond accordingly.
  3914. */
  3915. static int
  3916. ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  3917. {
  3918. struct ahd_initiator_tinfo *tinfo;
  3919. struct ahd_tmode_tstate *tstate;
  3920. int reject;
  3921. int done;
  3922. int response;
  3923. done = MSGLOOP_IN_PROG;
  3924. response = FALSE;
  3925. reject = FALSE;
  3926. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
  3927. devinfo->target, &tstate);
  3928. /*
  3929. * Parse as much of the message as is available,
  3930. * rejecting it if we don't support it. When
  3931. * the entire message is available and has been
  3932. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  3933. * that we have parsed an entire message.
  3934. *
  3935. * In the case of extended messages, we accept the length
  3936. * byte outright and perform more checking once we know the
  3937. * extended message type.
  3938. */
  3939. switch (ahd->msgin_buf[0]) {
  3940. case MSG_DISCONNECT:
  3941. case MSG_SAVEDATAPOINTER:
  3942. case MSG_CMDCOMPLETE:
  3943. case MSG_RESTOREPOINTERS:
  3944. case MSG_IGN_WIDE_RESIDUE:
  3945. /*
  3946. * End our message loop as these are messages
  3947. * the sequencer handles on its own.
  3948. */
  3949. done = MSGLOOP_TERMINATED;
  3950. break;
  3951. case MSG_MESSAGE_REJECT:
  3952. response = ahd_handle_msg_reject(ahd, devinfo);
  3953. /* FALLTHROUGH */
  3954. case MSG_NOOP:
  3955. done = MSGLOOP_MSGCOMPLETE;
  3956. break;
  3957. case MSG_EXTENDED:
  3958. {
  3959. /* Wait for enough of the message to begin validation */
  3960. if (ahd->msgin_index < 2)
  3961. break;
  3962. switch (ahd->msgin_buf[2]) {
  3963. case MSG_EXT_SDTR:
  3964. {
  3965. u_int period;
  3966. u_int ppr_options;
  3967. u_int offset;
  3968. u_int saved_offset;
  3969. if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  3970. reject = TRUE;
  3971. break;
  3972. }
  3973. /*
  3974. * Wait until we have both args before validating
  3975. * and acting on this message.
  3976. *
  3977. * Add one to MSG_EXT_SDTR_LEN to account for
  3978. * the extended message preamble.
  3979. */
  3980. if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  3981. break;
  3982. period = ahd->msgin_buf[3];
  3983. ppr_options = 0;
  3984. saved_offset = offset = ahd->msgin_buf[4];
  3985. ahd_devlimited_syncrate(ahd, tinfo, &period,
  3986. &ppr_options, devinfo->role);
  3987. ahd_validate_offset(ahd, tinfo, period, &offset,
  3988. tinfo->curr.width, devinfo->role);
  3989. if (bootverbose) {
  3990. printf("(%s:%c:%d:%d): Received "
  3991. "SDTR period %x, offset %x\n\t"
  3992. "Filtered to period %x, offset %x\n",
  3993. ahd_name(ahd), devinfo->channel,
  3994. devinfo->target, devinfo->lun,
  3995. ahd->msgin_buf[3], saved_offset,
  3996. period, offset);
  3997. }
  3998. ahd_set_syncrate(ahd, devinfo, period,
  3999. offset, ppr_options,
  4000. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4001. /*paused*/TRUE);
  4002. /*
  4003. * See if we initiated Sync Negotiation
  4004. * and didn't have to fall down to async
  4005. * transfers.
  4006. */
  4007. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  4008. /* We started it */
  4009. if (saved_offset != offset) {
  4010. /* Went too low - force async */
  4011. reject = TRUE;
  4012. }
  4013. } else {
  4014. /*
  4015. * Send our own SDTR in reply
  4016. */
  4017. if (bootverbose
  4018. && devinfo->role == ROLE_INITIATOR) {
  4019. printf("(%s:%c:%d:%d): Target "
  4020. "Initiated SDTR\n",
  4021. ahd_name(ahd), devinfo->channel,
  4022. devinfo->target, devinfo->lun);
  4023. }
  4024. ahd->msgout_index = 0;
  4025. ahd->msgout_len = 0;
  4026. ahd_construct_sdtr(ahd, devinfo,
  4027. period, offset);
  4028. ahd->msgout_index = 0;
  4029. response = TRUE;
  4030. }
  4031. done = MSGLOOP_MSGCOMPLETE;
  4032. break;
  4033. }
  4034. case MSG_EXT_WDTR:
  4035. {
  4036. u_int bus_width;
  4037. u_int saved_width;
  4038. u_int sending_reply;
  4039. sending_reply = FALSE;
  4040. if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  4041. reject = TRUE;
  4042. break;
  4043. }
  4044. /*
  4045. * Wait until we have our arg before validating
  4046. * and acting on this message.
  4047. *
  4048. * Add one to MSG_EXT_WDTR_LEN to account for
  4049. * the extended message preamble.
  4050. */
  4051. if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  4052. break;
  4053. bus_width = ahd->msgin_buf[3];
  4054. saved_width = bus_width;
  4055. ahd_validate_width(ahd, tinfo, &bus_width,
  4056. devinfo->role);
  4057. if (bootverbose) {
  4058. printf("(%s:%c:%d:%d): Received WDTR "
  4059. "%x filtered to %x\n",
  4060. ahd_name(ahd), devinfo->channel,
  4061. devinfo->target, devinfo->lun,
  4062. saved_width, bus_width);
  4063. }
  4064. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  4065. /*
  4066. * Don't send a WDTR back to the
  4067. * target, since we asked first.
  4068. * If the width went higher than our
  4069. * request, reject it.
  4070. */
  4071. if (saved_width > bus_width) {
  4072. reject = TRUE;
  4073. printf("(%s:%c:%d:%d): requested %dBit "
  4074. "transfers. Rejecting...\n",
  4075. ahd_name(ahd), devinfo->channel,
  4076. devinfo->target, devinfo->lun,
  4077. 8 * (0x01 << bus_width));
  4078. bus_width = 0;
  4079. }
  4080. } else {
  4081. /*
  4082. * Send our own WDTR in reply
  4083. */
  4084. if (bootverbose
  4085. && devinfo->role == ROLE_INITIATOR) {
  4086. printf("(%s:%c:%d:%d): Target "
  4087. "Initiated WDTR\n",
  4088. ahd_name(ahd), devinfo->channel,
  4089. devinfo->target, devinfo->lun);
  4090. }
  4091. ahd->msgout_index = 0;
  4092. ahd->msgout_len = 0;
  4093. ahd_construct_wdtr(ahd, devinfo, bus_width);
  4094. ahd->msgout_index = 0;
  4095. response = TRUE;
  4096. sending_reply = TRUE;
  4097. }
  4098. /*
  4099. * After a wide message, we are async, but
  4100. * some devices don't seem to honor this portion
  4101. * of the spec. Force a renegotiation of the
  4102. * sync component of our transfer agreement even
  4103. * if our goal is async. By updating our width
  4104. * after forcing the negotiation, we avoid
  4105. * renegotiating for width.
  4106. */
  4107. ahd_update_neg_request(ahd, devinfo, tstate,
  4108. tinfo, AHD_NEG_ALWAYS);
  4109. ahd_set_width(ahd, devinfo, bus_width,
  4110. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4111. /*paused*/TRUE);
  4112. if (sending_reply == FALSE && reject == FALSE) {
  4113. /*
  4114. * We will always have an SDTR to send.
  4115. */
  4116. ahd->msgout_index = 0;
  4117. ahd->msgout_len = 0;
  4118. ahd_build_transfer_msg(ahd, devinfo);
  4119. ahd->msgout_index = 0;
  4120. response = TRUE;
  4121. }
  4122. done = MSGLOOP_MSGCOMPLETE;
  4123. break;
  4124. }
  4125. case MSG_EXT_PPR:
  4126. {
  4127. u_int period;
  4128. u_int offset;
  4129. u_int bus_width;
  4130. u_int ppr_options;
  4131. u_int saved_width;
  4132. u_int saved_offset;
  4133. u_int saved_ppr_options;
  4134. if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  4135. reject = TRUE;
  4136. break;
  4137. }
  4138. /*
  4139. * Wait until we have all args before validating
  4140. * and acting on this message.
  4141. *
  4142. * Add one to MSG_EXT_PPR_LEN to account for
  4143. * the extended message preamble.
  4144. */
  4145. if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
  4146. break;
  4147. period = ahd->msgin_buf[3];
  4148. offset = ahd->msgin_buf[5];
  4149. bus_width = ahd->msgin_buf[6];
  4150. saved_width = bus_width;
  4151. ppr_options = ahd->msgin_buf[7];
  4152. /*
  4153. * According to the spec, a DT only
  4154. * period factor with no DT option
  4155. * set implies async.
  4156. */
  4157. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  4158. && period <= 9)
  4159. offset = 0;
  4160. saved_ppr_options = ppr_options;
  4161. saved_offset = offset;
  4162. /*
  4163. * Transfer options are only available if we
  4164. * are negotiating wide.
  4165. */
  4166. if (bus_width == 0)
  4167. ppr_options &= MSG_EXT_PPR_QAS_REQ;
  4168. ahd_validate_width(ahd, tinfo, &bus_width,
  4169. devinfo->role);
  4170. ahd_devlimited_syncrate(ahd, tinfo, &period,
  4171. &ppr_options, devinfo->role);
  4172. ahd_validate_offset(ahd, tinfo, period, &offset,
  4173. bus_width, devinfo->role);
  4174. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
  4175. /*
  4176. * If we are unable to do any of the
  4177. * requested options (we went too low),
  4178. * then we'll have to reject the message.
  4179. */
  4180. if (saved_width > bus_width
  4181. || saved_offset != offset
  4182. || saved_ppr_options != ppr_options) {
  4183. reject = TRUE;
  4184. period = 0;
  4185. offset = 0;
  4186. bus_width = 0;
  4187. ppr_options = 0;
  4188. }
  4189. } else {
  4190. if (devinfo->role != ROLE_TARGET)
  4191. printf("(%s:%c:%d:%d): Target "
  4192. "Initiated PPR\n",
  4193. ahd_name(ahd), devinfo->channel,
  4194. devinfo->target, devinfo->lun);
  4195. else
  4196. printf("(%s:%c:%d:%d): Initiator "
  4197. "Initiated PPR\n",
  4198. ahd_name(ahd), devinfo->channel,
  4199. devinfo->target, devinfo->lun);
  4200. ahd->msgout_index = 0;
  4201. ahd->msgout_len = 0;
  4202. ahd_construct_ppr(ahd, devinfo, period, offset,
  4203. bus_width, ppr_options);
  4204. ahd->msgout_index = 0;
  4205. response = TRUE;
  4206. }
  4207. if (bootverbose) {
  4208. printf("(%s:%c:%d:%d): Received PPR width %x, "
  4209. "period %x, offset %x,options %x\n"
  4210. "\tFiltered to width %x, period %x, "
  4211. "offset %x, options %x\n",
  4212. ahd_name(ahd), devinfo->channel,
  4213. devinfo->target, devinfo->lun,
  4214. saved_width, ahd->msgin_buf[3],
  4215. saved_offset, saved_ppr_options,
  4216. bus_width, period, offset, ppr_options);
  4217. }
  4218. ahd_set_width(ahd, devinfo, bus_width,
  4219. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4220. /*paused*/TRUE);
  4221. ahd_set_syncrate(ahd, devinfo, period,
  4222. offset, ppr_options,
  4223. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4224. /*paused*/TRUE);
  4225. done = MSGLOOP_MSGCOMPLETE;
  4226. break;
  4227. }
  4228. default:
  4229. /* Unknown extended message. Reject it. */
  4230. reject = TRUE;
  4231. break;
  4232. }
  4233. break;
  4234. }
  4235. #ifdef AHD_TARGET_MODE
  4236. case MSG_BUS_DEV_RESET:
  4237. ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
  4238. CAM_BDR_SENT,
  4239. "Bus Device Reset Received",
  4240. /*verbose_level*/0);
  4241. ahd_restart(ahd);
  4242. done = MSGLOOP_TERMINATED;
  4243. break;
  4244. case MSG_ABORT_TAG:
  4245. case MSG_ABORT:
  4246. case MSG_CLEAR_QUEUE:
  4247. {
  4248. int tag;
  4249. /* Target mode messages */
  4250. if (devinfo->role != ROLE_TARGET) {
  4251. reject = TRUE;
  4252. break;
  4253. }
  4254. tag = SCB_LIST_NULL;
  4255. if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
  4256. tag = ahd_inb(ahd, INITIATOR_TAG);
  4257. ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4258. devinfo->lun, tag, ROLE_TARGET,
  4259. CAM_REQ_ABORTED);
  4260. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4261. if (tstate != NULL) {
  4262. struct ahd_tmode_lstate* lstate;
  4263. lstate = tstate->enabled_luns[devinfo->lun];
  4264. if (lstate != NULL) {
  4265. ahd_queue_lstate_event(ahd, lstate,
  4266. devinfo->our_scsiid,
  4267. ahd->msgin_buf[0],
  4268. /*arg*/tag);
  4269. ahd_send_lstate_events(ahd, lstate);
  4270. }
  4271. }
  4272. ahd_restart(ahd);
  4273. done = MSGLOOP_TERMINATED;
  4274. break;
  4275. }
  4276. #endif
  4277. case MSG_QAS_REQUEST:
  4278. #ifdef AHD_DEBUG
  4279. if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
  4280. printf("%s: QAS request. SCSISIGI == 0x%x\n",
  4281. ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
  4282. #endif
  4283. ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
  4284. /* FALLTHROUGH */
  4285. case MSG_TERM_IO_PROC:
  4286. default:
  4287. reject = TRUE;
  4288. break;
  4289. }
  4290. if (reject) {
  4291. /*
  4292. * Setup to reject the message.
  4293. */
  4294. ahd->msgout_index = 0;
  4295. ahd->msgout_len = 1;
  4296. ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
  4297. done = MSGLOOP_MSGCOMPLETE;
  4298. response = TRUE;
  4299. }
  4300. if (done != MSGLOOP_IN_PROG && !response)
  4301. /* Clear the outgoing message buffer */
  4302. ahd->msgout_len = 0;
  4303. return (done);
  4304. }
  4305. /*
  4306. * Process a message reject message.
  4307. */
  4308. static int
  4309. ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4310. {
  4311. /*
  4312. * What we care about here is if we had an
  4313. * outstanding SDTR or WDTR message for this
  4314. * target. If we did, this is a signal that
  4315. * the target is refusing negotiation.
  4316. */
  4317. struct scb *scb;
  4318. struct ahd_initiator_tinfo *tinfo;
  4319. struct ahd_tmode_tstate *tstate;
  4320. u_int scb_index;
  4321. u_int last_msg;
  4322. int response = 0;
  4323. scb_index = ahd_get_scbptr(ahd);
  4324. scb = ahd_lookup_scb(ahd, scb_index);
  4325. tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
  4326. devinfo->our_scsiid,
  4327. devinfo->target, &tstate);
  4328. /* Might be necessary */
  4329. last_msg = ahd_inb(ahd, LAST_MSG);
  4330. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  4331. if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
  4332. && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
  4333. /*
  4334. * Target may not like our SPI-4 PPR Options.
  4335. * Attempt to negotiate 80MHz which will turn
  4336. * off these options.
  4337. */
  4338. if (bootverbose) {
  4339. printf("(%s:%c:%d:%d): PPR Rejected. "
  4340. "Trying simple U160 PPR\n",
  4341. ahd_name(ahd), devinfo->channel,
  4342. devinfo->target, devinfo->lun);
  4343. }
  4344. tinfo->goal.period = AHD_SYNCRATE_DT;
  4345. tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
  4346. | MSG_EXT_PPR_QAS_REQ
  4347. | MSG_EXT_PPR_DT_REQ;
  4348. } else {
  4349. /*
  4350. * Target does not support the PPR message.
  4351. * Attempt to negotiate SPI-2 style.
  4352. */
  4353. if (bootverbose) {
  4354. printf("(%s:%c:%d:%d): PPR Rejected. "
  4355. "Trying WDTR/SDTR\n",
  4356. ahd_name(ahd), devinfo->channel,
  4357. devinfo->target, devinfo->lun);
  4358. }
  4359. tinfo->goal.ppr_options = 0;
  4360. tinfo->curr.transport_version = 2;
  4361. tinfo->goal.transport_version = 2;
  4362. }
  4363. ahd->msgout_index = 0;
  4364. ahd->msgout_len = 0;
  4365. ahd_build_transfer_msg(ahd, devinfo);
  4366. ahd->msgout_index = 0;
  4367. response = 1;
  4368. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  4369. /* note 8bit xfers */
  4370. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  4371. "8bit transfers\n", ahd_name(ahd),
  4372. devinfo->channel, devinfo->target, devinfo->lun);
  4373. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4374. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4375. /*paused*/TRUE);
  4376. /*
  4377. * No need to clear the sync rate. If the target
  4378. * did not accept the command, our syncrate is
  4379. * unaffected. If the target started the negotiation,
  4380. * but rejected our response, we already cleared the
  4381. * sync rate before sending our WDTR.
  4382. */
  4383. if (tinfo->goal.offset != tinfo->curr.offset) {
  4384. /* Start the sync negotiation */
  4385. ahd->msgout_index = 0;
  4386. ahd->msgout_len = 0;
  4387. ahd_build_transfer_msg(ahd, devinfo);
  4388. ahd->msgout_index = 0;
  4389. response = 1;
  4390. }
  4391. } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  4392. /* note asynch xfers and clear flag */
  4393. ahd_set_syncrate(ahd, devinfo, /*period*/0,
  4394. /*offset*/0, /*ppr_options*/0,
  4395. AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
  4396. /*paused*/TRUE);
  4397. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  4398. "Using asynchronous transfers\n",
  4399. ahd_name(ahd), devinfo->channel,
  4400. devinfo->target, devinfo->lun);
  4401. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  4402. int tag_type;
  4403. int mask;
  4404. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  4405. if (tag_type == MSG_SIMPLE_TASK) {
  4406. printf("(%s:%c:%d:%d): refuses tagged commands. "
  4407. "Performing non-tagged I/O\n", ahd_name(ahd),
  4408. devinfo->channel, devinfo->target, devinfo->lun);
  4409. ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE);
  4410. mask = ~0x23;
  4411. } else {
  4412. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  4413. "Performing simple queue tagged I/O only\n",
  4414. ahd_name(ahd), devinfo->channel, devinfo->target,
  4415. devinfo->lun, tag_type == MSG_ORDERED_TASK
  4416. ? "ordered" : "head of queue");
  4417. ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC);
  4418. mask = ~0x03;
  4419. }
  4420. /*
  4421. * Resend the identify for this CCB as the target
  4422. * may believe that the selection is invalid otherwise.
  4423. */
  4424. ahd_outb(ahd, SCB_CONTROL,
  4425. ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
  4426. scb->hscb->control &= mask;
  4427. ahd_set_transaction_tag(scb, /*enabled*/FALSE,
  4428. /*type*/MSG_SIMPLE_TASK);
  4429. ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
  4430. ahd_assert_atn(ahd);
  4431. ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  4432. SCB_GET_TAG(scb));
  4433. /*
  4434. * Requeue all tagged commands for this target
  4435. * currently in our posession so they can be
  4436. * converted to untagged commands.
  4437. */
  4438. ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
  4439. SCB_GET_CHANNEL(ahd, scb),
  4440. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  4441. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  4442. SEARCH_COMPLETE);
  4443. } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
  4444. /*
  4445. * Most likely the device believes that we had
  4446. * previously negotiated packetized.
  4447. */
  4448. ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
  4449. | MSG_FLAG_IU_REQ_CHANGED;
  4450. ahd_force_renegotiation(ahd, devinfo);
  4451. ahd->msgout_index = 0;
  4452. ahd->msgout_len = 0;
  4453. ahd_build_transfer_msg(ahd, devinfo);
  4454. ahd->msgout_index = 0;
  4455. response = 1;
  4456. } else {
  4457. /*
  4458. * Otherwise, we ignore it.
  4459. */
  4460. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  4461. ahd_name(ahd), devinfo->channel, devinfo->target,
  4462. last_msg);
  4463. }
  4464. return (response);
  4465. }
  4466. /*
  4467. * Process an ingnore wide residue message.
  4468. */
  4469. static void
  4470. ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
  4471. {
  4472. u_int scb_index;
  4473. struct scb *scb;
  4474. scb_index = ahd_get_scbptr(ahd);
  4475. scb = ahd_lookup_scb(ahd, scb_index);
  4476. /*
  4477. * XXX Actually check data direction in the sequencer?
  4478. * Perhaps add datadir to some spare bits in the hscb?
  4479. */
  4480. if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
  4481. || ahd_get_transfer_dir(scb) != CAM_DIR_IN) {
  4482. /*
  4483. * Ignore the message if we haven't
  4484. * seen an appropriate data phase yet.
  4485. */
  4486. } else {
  4487. /*
  4488. * If the residual occurred on the last
  4489. * transfer and the transfer request was
  4490. * expected to end on an odd count, do
  4491. * nothing. Otherwise, subtract a byte
  4492. * and update the residual count accordingly.
  4493. */
  4494. uint32_t sgptr;
  4495. sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4496. if ((sgptr & SG_LIST_NULL) != 0
  4497. && (ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4498. & SCB_XFERLEN_ODD) != 0) {
  4499. /*
  4500. * If the residual occurred on the last
  4501. * transfer and the transfer request was
  4502. * expected to end on an odd count, do
  4503. * nothing.
  4504. */
  4505. } else {
  4506. uint32_t data_cnt;
  4507. uint64_t data_addr;
  4508. uint32_t sglen;
  4509. /* Pull in the rest of the sgptr */
  4510. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4511. data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4512. if ((sgptr & SG_LIST_NULL) != 0) {
  4513. /*
  4514. * The residual data count is not updated
  4515. * for the command run to completion case.
  4516. * Explicitly zero the count.
  4517. */
  4518. data_cnt &= ~AHD_SG_LEN_MASK;
  4519. }
  4520. data_addr = ahd_inq(ahd, SHADDR);
  4521. data_cnt += 1;
  4522. data_addr -= 1;
  4523. sgptr &= SG_PTR_MASK;
  4524. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4525. struct ahd_dma64_seg *sg;
  4526. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4527. /*
  4528. * The residual sg ptr points to the next S/G
  4529. * to load so we must go back one.
  4530. */
  4531. sg--;
  4532. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4533. if (sg != scb->sg_list
  4534. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4535. sg--;
  4536. sglen = ahd_le32toh(sg->len);
  4537. /*
  4538. * Preserve High Address and SG_LIST
  4539. * bits while setting the count to 1.
  4540. */
  4541. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4542. data_addr = ahd_le64toh(sg->addr)
  4543. + (sglen & AHD_SG_LEN_MASK)
  4544. - 1;
  4545. /*
  4546. * Increment sg so it points to the
  4547. * "next" sg.
  4548. */
  4549. sg++;
  4550. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4551. sg);
  4552. }
  4553. } else {
  4554. struct ahd_dma_seg *sg;
  4555. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4556. /*
  4557. * The residual sg ptr points to the next S/G
  4558. * to load so we must go back one.
  4559. */
  4560. sg--;
  4561. sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  4562. if (sg != scb->sg_list
  4563. && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
  4564. sg--;
  4565. sglen = ahd_le32toh(sg->len);
  4566. /*
  4567. * Preserve High Address and SG_LIST
  4568. * bits while setting the count to 1.
  4569. */
  4570. data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
  4571. data_addr = ahd_le32toh(sg->addr)
  4572. + (sglen & AHD_SG_LEN_MASK)
  4573. - 1;
  4574. /*
  4575. * Increment sg so it points to the
  4576. * "next" sg.
  4577. */
  4578. sg++;
  4579. sgptr = ahd_sg_virt_to_bus(ahd, scb,
  4580. sg);
  4581. }
  4582. }
  4583. /*
  4584. * Toggle the "oddness" of the transfer length
  4585. * to handle this mid-transfer ignore wide
  4586. * residue. This ensures that the oddness is
  4587. * correct for subsequent data transfers.
  4588. */
  4589. ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
  4590. ahd_inb_scbram(ahd, SCB_TASK_ATTRIBUTE)
  4591. ^ SCB_XFERLEN_ODD);
  4592. ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
  4593. ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
  4594. /*
  4595. * The FIFO's pointers will be updated if/when the
  4596. * sequencer re-enters a data phase.
  4597. */
  4598. }
  4599. }
  4600. }
  4601. /*
  4602. * Reinitialize the data pointers for the active transfer
  4603. * based on its current residual.
  4604. */
  4605. static void
  4606. ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
  4607. {
  4608. struct scb *scb;
  4609. ahd_mode_state saved_modes;
  4610. u_int scb_index;
  4611. u_int wait;
  4612. uint32_t sgptr;
  4613. uint32_t resid;
  4614. uint64_t dataptr;
  4615. AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
  4616. AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
  4617. scb_index = ahd_get_scbptr(ahd);
  4618. scb = ahd_lookup_scb(ahd, scb_index);
  4619. /*
  4620. * Release and reacquire the FIFO so we
  4621. * have a clean slate.
  4622. */
  4623. ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
  4624. wait = 1000;
  4625. while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE))
  4626. ahd_delay(100);
  4627. if (wait == 0) {
  4628. ahd_print_path(ahd, scb);
  4629. printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
  4630. ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
  4631. }
  4632. saved_modes = ahd_save_modes(ahd);
  4633. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4634. ahd_outb(ahd, DFFSTAT,
  4635. ahd_inb(ahd, DFFSTAT)
  4636. | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
  4637. /*
  4638. * Determine initial values for data_addr and data_cnt
  4639. * for resuming the data phase.
  4640. */
  4641. sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
  4642. sgptr &= SG_PTR_MASK;
  4643. resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
  4644. | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
  4645. | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
  4646. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
  4647. struct ahd_dma64_seg *sg;
  4648. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4649. /* The residual sg_ptr always points to the next sg */
  4650. sg--;
  4651. dataptr = ahd_le64toh(sg->addr)
  4652. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4653. - resid;
  4654. ahd_outl(ahd, HADDR + 4, dataptr >> 32);
  4655. } else {
  4656. struct ahd_dma_seg *sg;
  4657. sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
  4658. /* The residual sg_ptr always points to the next sg */
  4659. sg--;
  4660. dataptr = ahd_le32toh(sg->addr)
  4661. + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
  4662. - resid;
  4663. ahd_outb(ahd, HADDR + 4,
  4664. (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
  4665. }
  4666. ahd_outl(ahd, HADDR, dataptr);
  4667. ahd_outb(ahd, HCNT + 2, resid >> 16);
  4668. ahd_outb(ahd, HCNT + 1, resid >> 8);
  4669. ahd_outb(ahd, HCNT, resid);
  4670. }
  4671. /*
  4672. * Handle the effects of issuing a bus device reset message.
  4673. */
  4674. static void
  4675. ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4676. u_int lun, cam_status status, char *message,
  4677. int verbose_level)
  4678. {
  4679. #ifdef AHD_TARGET_MODE
  4680. struct ahd_tmode_tstate* tstate;
  4681. #endif
  4682. int found;
  4683. found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
  4684. lun, SCB_LIST_NULL, devinfo->role,
  4685. status);
  4686. #ifdef AHD_TARGET_MODE
  4687. /*
  4688. * Send an immediate notify ccb to all target mord peripheral
  4689. * drivers affected by this action.
  4690. */
  4691. tstate = ahd->enabled_targets[devinfo->our_scsiid];
  4692. if (tstate != NULL) {
  4693. u_int cur_lun;
  4694. u_int max_lun;
  4695. if (lun != CAM_LUN_WILDCARD) {
  4696. cur_lun = 0;
  4697. max_lun = AHD_NUM_LUNS - 1;
  4698. } else {
  4699. cur_lun = lun;
  4700. max_lun = lun;
  4701. }
  4702. for (cur_lun <= max_lun; cur_lun++) {
  4703. struct ahd_tmode_lstate* lstate;
  4704. lstate = tstate->enabled_luns[cur_lun];
  4705. if (lstate == NULL)
  4706. continue;
  4707. ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
  4708. MSG_BUS_DEV_RESET, /*arg*/0);
  4709. ahd_send_lstate_events(ahd, lstate);
  4710. }
  4711. }
  4712. #endif
  4713. /*
  4714. * Go back to async/narrow transfers and renegotiate.
  4715. */
  4716. ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  4717. AHD_TRANS_CUR, /*paused*/TRUE);
  4718. ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
  4719. /*ppr_options*/0, AHD_TRANS_CUR,
  4720. /*paused*/TRUE);
  4721. if (status != CAM_SEL_TIMEOUT)
  4722. ahd_send_async(ahd, devinfo->channel, devinfo->target,
  4723. CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
  4724. if (message != NULL && bootverbose)
  4725. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
  4726. message, devinfo->channel, devinfo->target, found);
  4727. }
  4728. #ifdef AHD_TARGET_MODE
  4729. static void
  4730. ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
  4731. struct scb *scb)
  4732. {
  4733. /*
  4734. * To facilitate adding multiple messages together,
  4735. * each routine should increment the index and len
  4736. * variables instead of setting them explicitly.
  4737. */
  4738. ahd->msgout_index = 0;
  4739. ahd->msgout_len = 0;
  4740. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  4741. ahd_build_transfer_msg(ahd, devinfo);
  4742. else
  4743. panic("ahd_intr: AWAITING target message with no message");
  4744. ahd->msgout_index = 0;
  4745. ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
  4746. }
  4747. #endif
  4748. /**************************** Initialization **********************************/
  4749. static u_int
  4750. ahd_sglist_size(struct ahd_softc *ahd)
  4751. {
  4752. bus_size_t list_size;
  4753. list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
  4754. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  4755. list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
  4756. return (list_size);
  4757. }
  4758. /*
  4759. * Calculate the optimum S/G List allocation size. S/G elements used
  4760. * for a given transaction must be physically contiguous. Assume the
  4761. * OS will allocate full pages to us, so it doesn't make sense to request
  4762. * less than a page.
  4763. */
  4764. static u_int
  4765. ahd_sglist_allocsize(struct ahd_softc *ahd)
  4766. {
  4767. bus_size_t sg_list_increment;
  4768. bus_size_t sg_list_size;
  4769. bus_size_t max_list_size;
  4770. bus_size_t best_list_size;
  4771. /* Start out with the minimum required for AHD_NSEG. */
  4772. sg_list_increment = ahd_sglist_size(ahd);
  4773. sg_list_size = sg_list_increment;
  4774. /* Get us as close as possible to a page in size. */
  4775. while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
  4776. sg_list_size += sg_list_increment;
  4777. /*
  4778. * Try to reduce the amount of wastage by allocating
  4779. * multiple pages.
  4780. */
  4781. best_list_size = sg_list_size;
  4782. max_list_size = roundup(sg_list_increment, PAGE_SIZE);
  4783. if (max_list_size < 4 * PAGE_SIZE)
  4784. max_list_size = 4 * PAGE_SIZE;
  4785. if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
  4786. max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
  4787. while ((sg_list_size + sg_list_increment) <= max_list_size
  4788. && (sg_list_size % PAGE_SIZE) != 0) {
  4789. bus_size_t new_mod;
  4790. bus_size_t best_mod;
  4791. sg_list_size += sg_list_increment;
  4792. new_mod = sg_list_size % PAGE_SIZE;
  4793. best_mod = best_list_size % PAGE_SIZE;
  4794. if (new_mod > best_mod || new_mod == 0) {
  4795. best_list_size = sg_list_size;
  4796. }
  4797. }
  4798. return (best_list_size);
  4799. }
  4800. /*
  4801. * Allocate a controller structure for a new device
  4802. * and perform initial initializion.
  4803. */
  4804. struct ahd_softc *
  4805. ahd_alloc(void *platform_arg, char *name)
  4806. {
  4807. struct ahd_softc *ahd;
  4808. #ifndef __FreeBSD__
  4809. ahd = malloc(sizeof(*ahd), M_DEVBUF, M_NOWAIT);
  4810. if (!ahd) {
  4811. printf("aic7xxx: cannot malloc softc!\n");
  4812. free(name, M_DEVBUF);
  4813. return NULL;
  4814. }
  4815. #else
  4816. ahd = device_get_softc((device_t)platform_arg);
  4817. #endif
  4818. memset(ahd, 0, sizeof(*ahd));
  4819. ahd->seep_config = malloc(sizeof(*ahd->seep_config),
  4820. M_DEVBUF, M_NOWAIT);
  4821. if (ahd->seep_config == NULL) {
  4822. #ifndef __FreeBSD__
  4823. free(ahd, M_DEVBUF);
  4824. #endif
  4825. free(name, M_DEVBUF);
  4826. return (NULL);
  4827. }
  4828. LIST_INIT(&ahd->pending_scbs);
  4829. /* We don't know our unit number until the OSM sets it */
  4830. ahd->name = name;
  4831. ahd->unit = -1;
  4832. ahd->description = NULL;
  4833. ahd->bus_description = NULL;
  4834. ahd->channel = 'A';
  4835. ahd->chip = AHD_NONE;
  4836. ahd->features = AHD_FENONE;
  4837. ahd->bugs = AHD_BUGNONE;
  4838. ahd->flags = AHD_SPCHK_ENB_A|AHD_RESET_BUS_A|AHD_TERM_ENB_A
  4839. | AHD_EXTENDED_TRANS_A|AHD_STPWLEVEL_A;
  4840. ahd_timer_init(&ahd->reset_timer);
  4841. ahd_timer_init(&ahd->stat_timer);
  4842. ahd->int_coalescing_timer = AHD_INT_COALESCING_TIMER_DEFAULT;
  4843. ahd->int_coalescing_maxcmds = AHD_INT_COALESCING_MAXCMDS_DEFAULT;
  4844. ahd->int_coalescing_mincmds = AHD_INT_COALESCING_MINCMDS_DEFAULT;
  4845. ahd->int_coalescing_threshold = AHD_INT_COALESCING_THRESHOLD_DEFAULT;
  4846. ahd->int_coalescing_stop_threshold =
  4847. AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT;
  4848. if (ahd_platform_alloc(ahd, platform_arg) != 0) {
  4849. ahd_free(ahd);
  4850. ahd = NULL;
  4851. }
  4852. #ifdef AHD_DEBUG
  4853. if ((ahd_debug & AHD_SHOW_MEMORY) != 0) {
  4854. printf("%s: scb size = 0x%x, hscb size = 0x%x\n",
  4855. ahd_name(ahd), (u_int)sizeof(struct scb),
  4856. (u_int)sizeof(struct hardware_scb));
  4857. }
  4858. #endif
  4859. return (ahd);
  4860. }
  4861. int
  4862. ahd_softc_init(struct ahd_softc *ahd)
  4863. {
  4864. ahd->unpause = 0;
  4865. ahd->pause = PAUSE;
  4866. return (0);
  4867. }
  4868. void
  4869. ahd_set_unit(struct ahd_softc *ahd, int unit)
  4870. {
  4871. ahd->unit = unit;
  4872. }
  4873. void
  4874. ahd_set_name(struct ahd_softc *ahd, char *name)
  4875. {
  4876. if (ahd->name != NULL)
  4877. free(ahd->name, M_DEVBUF);
  4878. ahd->name = name;
  4879. }
  4880. void
  4881. ahd_free(struct ahd_softc *ahd)
  4882. {
  4883. int i;
  4884. switch (ahd->init_level) {
  4885. default:
  4886. case 5:
  4887. ahd_shutdown(ahd);
  4888. /* FALLTHROUGH */
  4889. case 4:
  4890. ahd_dmamap_unload(ahd, ahd->shared_data_dmat,
  4891. ahd->shared_data_map.dmamap);
  4892. /* FALLTHROUGH */
  4893. case 3:
  4894. ahd_dmamem_free(ahd, ahd->shared_data_dmat, ahd->qoutfifo,
  4895. ahd->shared_data_map.dmamap);
  4896. ahd_dmamap_destroy(ahd, ahd->shared_data_dmat,
  4897. ahd->shared_data_map.dmamap);
  4898. /* FALLTHROUGH */
  4899. case 2:
  4900. ahd_dma_tag_destroy(ahd, ahd->shared_data_dmat);
  4901. case 1:
  4902. #ifndef __linux__
  4903. ahd_dma_tag_destroy(ahd, ahd->buffer_dmat);
  4904. #endif
  4905. break;
  4906. case 0:
  4907. break;
  4908. }
  4909. #ifndef __linux__
  4910. ahd_dma_tag_destroy(ahd, ahd->parent_dmat);
  4911. #endif
  4912. ahd_platform_free(ahd);
  4913. ahd_fini_scbdata(ahd);
  4914. for (i = 0; i < AHD_NUM_TARGETS; i++) {
  4915. struct ahd_tmode_tstate *tstate;
  4916. tstate = ahd->enabled_targets[i];
  4917. if (tstate != NULL) {
  4918. #ifdef AHD_TARGET_MODE
  4919. int j;
  4920. for (j = 0; j < AHD_NUM_LUNS; j++) {
  4921. struct ahd_tmode_lstate *lstate;
  4922. lstate = tstate->enabled_luns[j];
  4923. if (lstate != NULL) {
  4924. xpt_free_path(lstate->path);
  4925. free(lstate, M_DEVBUF);
  4926. }
  4927. }
  4928. #endif
  4929. free(tstate, M_DEVBUF);
  4930. }
  4931. }
  4932. #ifdef AHD_TARGET_MODE
  4933. if (ahd->black_hole != NULL) {
  4934. xpt_free_path(ahd->black_hole->path);
  4935. free(ahd->black_hole, M_DEVBUF);
  4936. }
  4937. #endif
  4938. if (ahd->name != NULL)
  4939. free(ahd->name, M_DEVBUF);
  4940. if (ahd->seep_config != NULL)
  4941. free(ahd->seep_config, M_DEVBUF);
  4942. if (ahd->saved_stack != NULL)
  4943. free(ahd->saved_stack, M_DEVBUF);
  4944. #ifndef __FreeBSD__
  4945. free(ahd, M_DEVBUF);
  4946. #endif
  4947. return;
  4948. }
  4949. void
  4950. ahd_shutdown(void *arg)
  4951. {
  4952. struct ahd_softc *ahd;
  4953. ahd = (struct ahd_softc *)arg;
  4954. /*
  4955. * Stop periodic timer callbacks.
  4956. */
  4957. ahd_timer_stop(&ahd->reset_timer);
  4958. ahd_timer_stop(&ahd->stat_timer);
  4959. /* This will reset most registers to 0, but not all */
  4960. ahd_reset(ahd, /*reinit*/FALSE);
  4961. }
  4962. /*
  4963. * Reset the controller and record some information about it
  4964. * that is only available just after a reset. If "reinit" is
  4965. * non-zero, this reset occured after initial configuration
  4966. * and the caller requests that the chip be fully reinitialized
  4967. * to a runable state. Chip interrupts are *not* enabled after
  4968. * a reinitialization. The caller must enable interrupts via
  4969. * ahd_intr_enable().
  4970. */
  4971. int
  4972. ahd_reset(struct ahd_softc *ahd, int reinit)
  4973. {
  4974. u_int sxfrctl1;
  4975. int wait;
  4976. uint32_t cmd;
  4977. /*
  4978. * Preserve the value of the SXFRCTL1 register for all channels.
  4979. * It contains settings that affect termination and we don't want
  4980. * to disturb the integrity of the bus.
  4981. */
  4982. ahd_pause(ahd);
  4983. ahd_update_modes(ahd);
  4984. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  4985. sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
  4986. cmd = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, /*bytes*/2);
  4987. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  4988. uint32_t mod_cmd;
  4989. /*
  4990. * A4 Razor #632
  4991. * During the assertion of CHIPRST, the chip
  4992. * does not disable its parity logic prior to
  4993. * the start of the reset. This may cause a
  4994. * parity error to be detected and thus a
  4995. * spurious SERR or PERR assertion. Disble
  4996. * PERR and SERR responses during the CHIPRST.
  4997. */
  4998. mod_cmd = cmd & ~(PCIM_CMD_PERRESPEN|PCIM_CMD_SERRESPEN);
  4999. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  5000. mod_cmd, /*bytes*/2);
  5001. }
  5002. ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
  5003. /*
  5004. * Ensure that the reset has finished. We delay 1000us
  5005. * prior to reading the register to make sure the chip
  5006. * has sufficiently completed its reset to handle register
  5007. * accesses.
  5008. */
  5009. wait = 1000;
  5010. do {
  5011. ahd_delay(1000);
  5012. } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
  5013. if (wait == 0) {
  5014. printf("%s: WARNING - Failed chip reset! "
  5015. "Trying to initialize anyway.\n", ahd_name(ahd));
  5016. }
  5017. ahd_outb(ahd, HCNTRL, ahd->pause);
  5018. if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
  5019. /*
  5020. * Clear any latched PCI error status and restore
  5021. * previous SERR and PERR response enables.
  5022. */
  5023. ahd_pci_write_config(ahd->dev_softc, PCIR_STATUS + 1,
  5024. 0xFF, /*bytes*/1);
  5025. ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
  5026. cmd, /*bytes*/2);
  5027. }
  5028. /*
  5029. * Mode should be SCSI after a chip reset, but lets
  5030. * set it just to be safe. We touch the MODE_PTR
  5031. * register directly so as to bypass the lazy update
  5032. * code in ahd_set_modes().
  5033. */
  5034. ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5035. ahd_outb(ahd, MODE_PTR,
  5036. ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
  5037. /*
  5038. * Restore SXFRCTL1.
  5039. *
  5040. * We must always initialize STPWEN to 1 before we
  5041. * restore the saved values. STPWEN is initialized
  5042. * to a tri-state condition which can only be cleared
  5043. * by turning it on.
  5044. */
  5045. ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
  5046. ahd_outb(ahd, SXFRCTL1, sxfrctl1);
  5047. /* Determine chip configuration */
  5048. ahd->features &= ~AHD_WIDE;
  5049. if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
  5050. ahd->features |= AHD_WIDE;
  5051. /*
  5052. * If a recovery action has forced a chip reset,
  5053. * re-initialize the chip to our liking.
  5054. */
  5055. if (reinit != 0)
  5056. ahd_chip_init(ahd);
  5057. return (0);
  5058. }
  5059. /*
  5060. * Determine the number of SCBs available on the controller
  5061. */
  5062. int
  5063. ahd_probe_scbs(struct ahd_softc *ahd) {
  5064. int i;
  5065. AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
  5066. ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
  5067. for (i = 0; i < AHD_SCB_MAX; i++) {
  5068. int j;
  5069. ahd_set_scbptr(ahd, i);
  5070. ahd_outw(ahd, SCB_BASE, i);
  5071. for (j = 2; j < 64; j++)
  5072. ahd_outb(ahd, SCB_BASE+j, 0);
  5073. /* Start out life as unallocated (needing an abort) */
  5074. ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
  5075. if (ahd_inw_scbram(ahd, SCB_BASE) != i)
  5076. break;
  5077. ahd_set_scbptr(ahd, 0);
  5078. if (ahd_inw_scbram(ahd, SCB_BASE) != 0)
  5079. break;
  5080. }
  5081. return (i);
  5082. }
  5083. static void
  5084. ahd_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  5085. {
  5086. dma_addr_t *baddr;
  5087. baddr = (dma_addr_t *)arg;
  5088. *baddr = segs->ds_addr;
  5089. }
  5090. static void
  5091. ahd_initialize_hscbs(struct ahd_softc *ahd)
  5092. {
  5093. int i;
  5094. for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
  5095. ahd_set_scbptr(ahd, i);
  5096. /* Clear the control byte. */
  5097. ahd_outb(ahd, SCB_CONTROL, 0);
  5098. /* Set the next pointer */
  5099. ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
  5100. }
  5101. }
  5102. static int
  5103. ahd_init_scbdata(struct ahd_softc *ahd)
  5104. {
  5105. struct scb_data *scb_data;
  5106. int i;
  5107. scb_data = &ahd->scb_data;
  5108. TAILQ_INIT(&scb_data->free_scbs);
  5109. for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
  5110. LIST_INIT(&scb_data->free_scb_lists[i]);
  5111. LIST_INIT(&scb_data->any_dev_free_scb_list);
  5112. SLIST_INIT(&scb_data->hscb_maps);
  5113. SLIST_INIT(&scb_data->sg_maps);
  5114. SLIST_INIT(&scb_data->sense_maps);
  5115. /* Determine the number of hardware SCBs and initialize them */
  5116. scb_data->maxhscbs = ahd_probe_scbs(ahd);
  5117. if (scb_data->maxhscbs == 0) {
  5118. printf("%s: No SCB space found\n", ahd_name(ahd));
  5119. return (ENXIO);
  5120. }
  5121. ahd_initialize_hscbs(ahd);
  5122. /*
  5123. * Create our DMA tags. These tags define the kinds of device
  5124. * accessible memory allocations and memory mappings we will
  5125. * need to perform during normal operation.
  5126. *
  5127. * Unless we need to further restrict the allocation, we rely
  5128. * on the restrictions of the parent dmat, hence the common
  5129. * use of MAXADDR and MAXSIZE.
  5130. */
  5131. /* DMA tag for our hardware scb structures */
  5132. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5133. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5134. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5135. /*highaddr*/BUS_SPACE_MAXADDR,
  5136. /*filter*/NULL, /*filterarg*/NULL,
  5137. PAGE_SIZE, /*nsegments*/1,
  5138. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5139. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  5140. goto error_exit;
  5141. }
  5142. scb_data->init_level++;
  5143. /* DMA tag for our S/G structures. */
  5144. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/8,
  5145. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5146. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5147. /*highaddr*/BUS_SPACE_MAXADDR,
  5148. /*filter*/NULL, /*filterarg*/NULL,
  5149. ahd_sglist_allocsize(ahd), /*nsegments*/1,
  5150. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5151. /*flags*/0, &scb_data->sg_dmat) != 0) {
  5152. goto error_exit;
  5153. }
  5154. #ifdef AHD_DEBUG
  5155. if ((ahd_debug & AHD_SHOW_MEMORY) != 0)
  5156. printf("%s: ahd_sglist_allocsize = 0x%x\n", ahd_name(ahd),
  5157. ahd_sglist_allocsize(ahd));
  5158. #endif
  5159. scb_data->init_level++;
  5160. /* DMA tag for our sense buffers. We allocate in page sized chunks */
  5161. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5162. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5163. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5164. /*highaddr*/BUS_SPACE_MAXADDR,
  5165. /*filter*/NULL, /*filterarg*/NULL,
  5166. PAGE_SIZE, /*nsegments*/1,
  5167. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5168. /*flags*/0, &scb_data->sense_dmat) != 0) {
  5169. goto error_exit;
  5170. }
  5171. scb_data->init_level++;
  5172. /* Perform initial CCB allocation */
  5173. ahd_alloc_scbs(ahd);
  5174. if (scb_data->numscbs == 0) {
  5175. printf("%s: ahd_init_scbdata - "
  5176. "Unable to allocate initial scbs\n",
  5177. ahd_name(ahd));
  5178. goto error_exit;
  5179. }
  5180. /*
  5181. * Note that we were successfull
  5182. */
  5183. return (0);
  5184. error_exit:
  5185. return (ENOMEM);
  5186. }
  5187. static struct scb *
  5188. ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
  5189. {
  5190. struct scb *scb;
  5191. /*
  5192. * Look on the pending list.
  5193. */
  5194. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  5195. if (SCB_GET_TAG(scb) == tag)
  5196. return (scb);
  5197. }
  5198. /*
  5199. * Then on all of the collision free lists.
  5200. */
  5201. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5202. struct scb *list_scb;
  5203. list_scb = scb;
  5204. do {
  5205. if (SCB_GET_TAG(list_scb) == tag)
  5206. return (list_scb);
  5207. list_scb = LIST_NEXT(list_scb, collision_links);
  5208. } while (list_scb);
  5209. }
  5210. /*
  5211. * And finally on the generic free list.
  5212. */
  5213. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  5214. if (SCB_GET_TAG(scb) == tag)
  5215. return (scb);
  5216. }
  5217. return (NULL);
  5218. }
  5219. static void
  5220. ahd_fini_scbdata(struct ahd_softc *ahd)
  5221. {
  5222. struct scb_data *scb_data;
  5223. scb_data = &ahd->scb_data;
  5224. if (scb_data == NULL)
  5225. return;
  5226. switch (scb_data->init_level) {
  5227. default:
  5228. case 7:
  5229. {
  5230. struct map_node *sns_map;
  5231. while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
  5232. SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
  5233. ahd_dmamap_unload(ahd, scb_data->sense_dmat,
  5234. sns_map->dmamap);
  5235. ahd_dmamem_free(ahd, scb_data->sense_dmat,
  5236. sns_map->vaddr, sns_map->dmamap);
  5237. free(sns_map, M_DEVBUF);
  5238. }
  5239. ahd_dma_tag_destroy(ahd, scb_data->sense_dmat);
  5240. /* FALLTHROUGH */
  5241. }
  5242. case 6:
  5243. {
  5244. struct map_node *sg_map;
  5245. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
  5246. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  5247. ahd_dmamap_unload(ahd, scb_data->sg_dmat,
  5248. sg_map->dmamap);
  5249. ahd_dmamem_free(ahd, scb_data->sg_dmat,
  5250. sg_map->vaddr, sg_map->dmamap);
  5251. free(sg_map, M_DEVBUF);
  5252. }
  5253. ahd_dma_tag_destroy(ahd, scb_data->sg_dmat);
  5254. /* FALLTHROUGH */
  5255. }
  5256. case 5:
  5257. {
  5258. struct map_node *hscb_map;
  5259. while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
  5260. SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
  5261. ahd_dmamap_unload(ahd, scb_data->hscb_dmat,
  5262. hscb_map->dmamap);
  5263. ahd_dmamem_free(ahd, scb_data->hscb_dmat,
  5264. hscb_map->vaddr, hscb_map->dmamap);
  5265. free(hscb_map, M_DEVBUF);
  5266. }
  5267. ahd_dma_tag_destroy(ahd, scb_data->hscb_dmat);
  5268. /* FALLTHROUGH */
  5269. }
  5270. case 4:
  5271. case 3:
  5272. case 2:
  5273. case 1:
  5274. case 0:
  5275. break;
  5276. }
  5277. }
  5278. /*
  5279. * DSP filter Bypass must be enabled until the first selection
  5280. * after a change in bus mode (Razor #491 and #493).
  5281. */
  5282. static void
  5283. ahd_setup_iocell_workaround(struct ahd_softc *ahd)
  5284. {
  5285. ahd_mode_state saved_modes;
  5286. saved_modes = ahd_save_modes(ahd);
  5287. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5288. ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
  5289. | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
  5290. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
  5291. #ifdef AHD_DEBUG
  5292. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5293. printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
  5294. #endif
  5295. ahd_restore_modes(ahd, saved_modes);
  5296. ahd->flags &= ~AHD_HAD_FIRST_SEL;
  5297. }
  5298. static void
  5299. ahd_iocell_first_selection(struct ahd_softc *ahd)
  5300. {
  5301. ahd_mode_state saved_modes;
  5302. u_int sblkctl;
  5303. if ((ahd->flags & AHD_HAD_FIRST_SEL) != 0)
  5304. return;
  5305. saved_modes = ahd_save_modes(ahd);
  5306. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5307. sblkctl = ahd_inb(ahd, SBLKCTL);
  5308. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5309. #ifdef AHD_DEBUG
  5310. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5311. printf("%s: iocell first selection\n", ahd_name(ahd));
  5312. #endif
  5313. if ((sblkctl & ENAB40) != 0) {
  5314. ahd_outb(ahd, DSPDATACTL,
  5315. ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
  5316. #ifdef AHD_DEBUG
  5317. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5318. printf("%s: BYPASS now disabled\n", ahd_name(ahd));
  5319. #endif
  5320. }
  5321. ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
  5322. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5323. ahd_restore_modes(ahd, saved_modes);
  5324. ahd->flags |= AHD_HAD_FIRST_SEL;
  5325. }
  5326. /*************************** SCB Management ***********************************/
  5327. static void
  5328. ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
  5329. {
  5330. struct scb_list *free_list;
  5331. struct scb_tailq *free_tailq;
  5332. struct scb *first_scb;
  5333. scb->flags |= SCB_ON_COL_LIST;
  5334. AHD_SET_SCB_COL_IDX(scb, col_idx);
  5335. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5336. free_tailq = &ahd->scb_data.free_scbs;
  5337. first_scb = LIST_FIRST(free_list);
  5338. if (first_scb != NULL) {
  5339. LIST_INSERT_AFTER(first_scb, scb, collision_links);
  5340. } else {
  5341. LIST_INSERT_HEAD(free_list, scb, collision_links);
  5342. TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
  5343. }
  5344. }
  5345. static void
  5346. ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
  5347. {
  5348. struct scb_list *free_list;
  5349. struct scb_tailq *free_tailq;
  5350. struct scb *first_scb;
  5351. u_int col_idx;
  5352. scb->flags &= ~SCB_ON_COL_LIST;
  5353. col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
  5354. free_list = &ahd->scb_data.free_scb_lists[col_idx];
  5355. free_tailq = &ahd->scb_data.free_scbs;
  5356. first_scb = LIST_FIRST(free_list);
  5357. if (first_scb == scb) {
  5358. struct scb *next_scb;
  5359. /*
  5360. * Maintain order in the collision free
  5361. * lists for fairness if this device has
  5362. * other colliding tags active.
  5363. */
  5364. next_scb = LIST_NEXT(scb, collision_links);
  5365. if (next_scb != NULL) {
  5366. TAILQ_INSERT_AFTER(free_tailq, scb,
  5367. next_scb, links.tqe);
  5368. }
  5369. TAILQ_REMOVE(free_tailq, scb, links.tqe);
  5370. }
  5371. LIST_REMOVE(scb, collision_links);
  5372. }
  5373. /*
  5374. * Get a free scb. If there are none, see if we can allocate a new SCB.
  5375. */
  5376. struct scb *
  5377. ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
  5378. {
  5379. struct scb *scb;
  5380. int tries;
  5381. tries = 0;
  5382. look_again:
  5383. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  5384. if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
  5385. ahd_rem_col_list(ahd, scb);
  5386. goto found;
  5387. }
  5388. }
  5389. if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
  5390. if (tries++ != 0)
  5391. return (NULL);
  5392. ahd_alloc_scbs(ahd);
  5393. goto look_again;
  5394. }
  5395. LIST_REMOVE(scb, links.le);
  5396. if (col_idx != AHD_NEVER_COL_IDX
  5397. && (scb->col_scb != NULL)
  5398. && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
  5399. LIST_REMOVE(scb->col_scb, links.le);
  5400. ahd_add_col_list(ahd, scb->col_scb, col_idx);
  5401. }
  5402. found:
  5403. scb->flags |= SCB_ACTIVE;
  5404. return (scb);
  5405. }
  5406. /*
  5407. * Return an SCB resource to the free list.
  5408. */
  5409. void
  5410. ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
  5411. {
  5412. /* Clean up for the next user */
  5413. scb->flags = SCB_FLAG_NONE;
  5414. scb->hscb->control = 0;
  5415. ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
  5416. if (scb->col_scb == NULL) {
  5417. /*
  5418. * No collision possible. Just free normally.
  5419. */
  5420. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5421. scb, links.le);
  5422. } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
  5423. /*
  5424. * The SCB we might have collided with is on
  5425. * a free collision list. Put both SCBs on
  5426. * the generic list.
  5427. */
  5428. ahd_rem_col_list(ahd, scb->col_scb);
  5429. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5430. scb, links.le);
  5431. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5432. scb->col_scb, links.le);
  5433. } else if ((scb->col_scb->flags
  5434. & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
  5435. && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
  5436. /*
  5437. * The SCB we might collide with on the next allocation
  5438. * is still active in a non-packetized, tagged, context.
  5439. * Put us on the SCB collision list.
  5440. */
  5441. ahd_add_col_list(ahd, scb,
  5442. AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
  5443. } else {
  5444. /*
  5445. * The SCB we might collide with on the next allocation
  5446. * is either active in a packetized context, or free.
  5447. * Since we can't collide, put this SCB on the generic
  5448. * free list.
  5449. */
  5450. LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
  5451. scb, links.le);
  5452. }
  5453. ahd_platform_scb_free(ahd, scb);
  5454. }
  5455. void
  5456. ahd_alloc_scbs(struct ahd_softc *ahd)
  5457. {
  5458. struct scb_data *scb_data;
  5459. struct scb *next_scb;
  5460. struct hardware_scb *hscb;
  5461. struct map_node *hscb_map;
  5462. struct map_node *sg_map;
  5463. struct map_node *sense_map;
  5464. uint8_t *segs;
  5465. uint8_t *sense_data;
  5466. dma_addr_t hscb_busaddr;
  5467. dma_addr_t sg_busaddr;
  5468. dma_addr_t sense_busaddr;
  5469. int newcount;
  5470. int i;
  5471. scb_data = &ahd->scb_data;
  5472. if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
  5473. /* Can't allocate any more */
  5474. return;
  5475. if (scb_data->scbs_left != 0) {
  5476. int offset;
  5477. offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
  5478. hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
  5479. hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
  5480. hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
  5481. } else {
  5482. hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
  5483. if (hscb_map == NULL)
  5484. return;
  5485. /* Allocate the next batch of hardware SCBs */
  5486. if (ahd_dmamem_alloc(ahd, scb_data->hscb_dmat,
  5487. (void **)&hscb_map->vaddr,
  5488. BUS_DMA_NOWAIT, &hscb_map->dmamap) != 0) {
  5489. free(hscb_map, M_DEVBUF);
  5490. return;
  5491. }
  5492. SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
  5493. ahd_dmamap_load(ahd, scb_data->hscb_dmat, hscb_map->dmamap,
  5494. hscb_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5495. &hscb_map->physaddr, /*flags*/0);
  5496. hscb = (struct hardware_scb *)hscb_map->vaddr;
  5497. hscb_busaddr = hscb_map->physaddr;
  5498. scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
  5499. }
  5500. if (scb_data->sgs_left != 0) {
  5501. int offset;
  5502. offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
  5503. - scb_data->sgs_left) * ahd_sglist_size(ahd);
  5504. sg_map = SLIST_FIRST(&scb_data->sg_maps);
  5505. segs = sg_map->vaddr + offset;
  5506. sg_busaddr = sg_map->physaddr + offset;
  5507. } else {
  5508. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  5509. if (sg_map == NULL)
  5510. return;
  5511. /* Allocate the next batch of S/G lists */
  5512. if (ahd_dmamem_alloc(ahd, scb_data->sg_dmat,
  5513. (void **)&sg_map->vaddr,
  5514. BUS_DMA_NOWAIT, &sg_map->dmamap) != 0) {
  5515. free(sg_map, M_DEVBUF);
  5516. return;
  5517. }
  5518. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  5519. ahd_dmamap_load(ahd, scb_data->sg_dmat, sg_map->dmamap,
  5520. sg_map->vaddr, ahd_sglist_allocsize(ahd),
  5521. ahd_dmamap_cb, &sg_map->physaddr, /*flags*/0);
  5522. segs = sg_map->vaddr;
  5523. sg_busaddr = sg_map->physaddr;
  5524. scb_data->sgs_left =
  5525. ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
  5526. #ifdef AHD_DEBUG
  5527. if (ahd_debug & AHD_SHOW_MEMORY)
  5528. printf("Mapped SG data\n");
  5529. #endif
  5530. }
  5531. if (scb_data->sense_left != 0) {
  5532. int offset;
  5533. offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
  5534. sense_map = SLIST_FIRST(&scb_data->sense_maps);
  5535. sense_data = sense_map->vaddr + offset;
  5536. sense_busaddr = sense_map->physaddr + offset;
  5537. } else {
  5538. sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
  5539. if (sense_map == NULL)
  5540. return;
  5541. /* Allocate the next batch of sense buffers */
  5542. if (ahd_dmamem_alloc(ahd, scb_data->sense_dmat,
  5543. (void **)&sense_map->vaddr,
  5544. BUS_DMA_NOWAIT, &sense_map->dmamap) != 0) {
  5545. free(sense_map, M_DEVBUF);
  5546. return;
  5547. }
  5548. SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
  5549. ahd_dmamap_load(ahd, scb_data->sense_dmat, sense_map->dmamap,
  5550. sense_map->vaddr, PAGE_SIZE, ahd_dmamap_cb,
  5551. &sense_map->physaddr, /*flags*/0);
  5552. sense_data = sense_map->vaddr;
  5553. sense_busaddr = sense_map->physaddr;
  5554. scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
  5555. #ifdef AHD_DEBUG
  5556. if (ahd_debug & AHD_SHOW_MEMORY)
  5557. printf("Mapped sense data\n");
  5558. #endif
  5559. }
  5560. newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
  5561. newcount = MIN(newcount, scb_data->sgs_left);
  5562. newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
  5563. for (i = 0; i < newcount; i++) {
  5564. struct scb_platform_data *pdata;
  5565. u_int col_tag;
  5566. #ifndef __linux__
  5567. int error;
  5568. #endif
  5569. next_scb = (struct scb *)malloc(sizeof(*next_scb),
  5570. M_DEVBUF, M_NOWAIT);
  5571. if (next_scb == NULL)
  5572. break;
  5573. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  5574. M_DEVBUF, M_NOWAIT);
  5575. if (pdata == NULL) {
  5576. free(next_scb, M_DEVBUF);
  5577. break;
  5578. }
  5579. next_scb->platform_data = pdata;
  5580. next_scb->hscb_map = hscb_map;
  5581. next_scb->sg_map = sg_map;
  5582. next_scb->sense_map = sense_map;
  5583. next_scb->sg_list = segs;
  5584. next_scb->sense_data = sense_data;
  5585. next_scb->sense_busaddr = sense_busaddr;
  5586. memset(hscb, 0, sizeof(*hscb));
  5587. next_scb->hscb = hscb;
  5588. hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
  5589. /*
  5590. * The sequencer always starts with the second entry.
  5591. * The first entry is embedded in the scb.
  5592. */
  5593. next_scb->sg_list_busaddr = sg_busaddr;
  5594. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  5595. next_scb->sg_list_busaddr
  5596. += sizeof(struct ahd_dma64_seg);
  5597. else
  5598. next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
  5599. next_scb->ahd_softc = ahd;
  5600. next_scb->flags = SCB_FLAG_NONE;
  5601. #ifndef __linux__
  5602. error = ahd_dmamap_create(ahd, ahd->buffer_dmat, /*flags*/0,
  5603. &next_scb->dmamap);
  5604. if (error != 0) {
  5605. free(next_scb, M_DEVBUF);
  5606. free(pdata, M_DEVBUF);
  5607. break;
  5608. }
  5609. #endif
  5610. next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
  5611. col_tag = scb_data->numscbs ^ 0x100;
  5612. next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
  5613. if (next_scb->col_scb != NULL)
  5614. next_scb->col_scb->col_scb = next_scb;
  5615. ahd_free_scb(ahd, next_scb);
  5616. hscb++;
  5617. hscb_busaddr += sizeof(*hscb);
  5618. segs += ahd_sglist_size(ahd);
  5619. sg_busaddr += ahd_sglist_size(ahd);
  5620. sense_data += AHD_SENSE_BUFSIZE;
  5621. sense_busaddr += AHD_SENSE_BUFSIZE;
  5622. scb_data->numscbs++;
  5623. scb_data->sense_left--;
  5624. scb_data->scbs_left--;
  5625. scb_data->sgs_left--;
  5626. }
  5627. }
  5628. void
  5629. ahd_controller_info(struct ahd_softc *ahd, char *buf)
  5630. {
  5631. const char *speed;
  5632. const char *type;
  5633. int len;
  5634. len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
  5635. buf += len;
  5636. speed = "Ultra320 ";
  5637. if ((ahd->features & AHD_WIDE) != 0) {
  5638. type = "Wide ";
  5639. } else {
  5640. type = "Single ";
  5641. }
  5642. len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
  5643. speed, type, ahd->channel, ahd->our_id);
  5644. buf += len;
  5645. sprintf(buf, "%s, %d SCBs", ahd->bus_description,
  5646. ahd->scb_data.maxhscbs);
  5647. }
  5648. static const char *channel_strings[] = {
  5649. "Primary Low",
  5650. "Primary High",
  5651. "Secondary Low",
  5652. "Secondary High"
  5653. };
  5654. static const char *termstat_strings[] = {
  5655. "Terminated Correctly",
  5656. "Over Terminated",
  5657. "Under Terminated",
  5658. "Not Configured"
  5659. };
  5660. /*
  5661. * Start the board, ready for normal operation
  5662. */
  5663. int
  5664. ahd_init(struct ahd_softc *ahd)
  5665. {
  5666. uint8_t *next_vaddr;
  5667. dma_addr_t next_baddr;
  5668. size_t driver_data_size;
  5669. int i;
  5670. int error;
  5671. u_int warn_user;
  5672. uint8_t current_sensing;
  5673. uint8_t fstat;
  5674. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5675. ahd->stack_size = ahd_probe_stack_size(ahd);
  5676. ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
  5677. M_DEVBUF, M_NOWAIT);
  5678. if (ahd->saved_stack == NULL)
  5679. return (ENOMEM);
  5680. /*
  5681. * Verify that the compiler hasn't over-agressively
  5682. * padded important structures.
  5683. */
  5684. if (sizeof(struct hardware_scb) != 64)
  5685. panic("Hardware SCB size is incorrect");
  5686. #ifdef AHD_DEBUG
  5687. if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
  5688. ahd->flags |= AHD_SEQUENCER_DEBUG;
  5689. #endif
  5690. /*
  5691. * Default to allowing initiator operations.
  5692. */
  5693. ahd->flags |= AHD_INITIATORROLE;
  5694. /*
  5695. * Only allow target mode features if this unit has them enabled.
  5696. */
  5697. if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
  5698. ahd->features &= ~AHD_TARGETMODE;
  5699. #ifndef __linux__
  5700. /* DMA tag for mapping buffers into device visible space. */
  5701. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5702. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5703. /*lowaddr*/ahd->flags & AHD_39BIT_ADDRESSING
  5704. ? (dma_addr_t)0x7FFFFFFFFFULL
  5705. : BUS_SPACE_MAXADDR_32BIT,
  5706. /*highaddr*/BUS_SPACE_MAXADDR,
  5707. /*filter*/NULL, /*filterarg*/NULL,
  5708. /*maxsize*/(AHD_NSEG - 1) * PAGE_SIZE,
  5709. /*nsegments*/AHD_NSEG,
  5710. /*maxsegsz*/AHD_MAXTRANSFER_SIZE,
  5711. /*flags*/BUS_DMA_ALLOCNOW,
  5712. &ahd->buffer_dmat) != 0) {
  5713. return (ENOMEM);
  5714. }
  5715. #endif
  5716. ahd->init_level++;
  5717. /*
  5718. * DMA tag for our command fifos and other data in system memory
  5719. * the card's sequencer must be able to access. For initiator
  5720. * roles, we need to allocate space for the qoutfifo. When providing
  5721. * for the target mode role, we must additionally provide space for
  5722. * the incoming target command fifo.
  5723. */
  5724. driver_data_size = AHD_SCB_MAX * sizeof(*ahd->qoutfifo)
  5725. + sizeof(struct hardware_scb);
  5726. if ((ahd->features & AHD_TARGETMODE) != 0)
  5727. driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5728. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
  5729. driver_data_size += PKT_OVERRUN_BUFSIZE;
  5730. if (ahd_dma_tag_create(ahd, ahd->parent_dmat, /*alignment*/1,
  5731. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  5732. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  5733. /*highaddr*/BUS_SPACE_MAXADDR,
  5734. /*filter*/NULL, /*filterarg*/NULL,
  5735. driver_data_size,
  5736. /*nsegments*/1,
  5737. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  5738. /*flags*/0, &ahd->shared_data_dmat) != 0) {
  5739. return (ENOMEM);
  5740. }
  5741. ahd->init_level++;
  5742. /* Allocation of driver data */
  5743. if (ahd_dmamem_alloc(ahd, ahd->shared_data_dmat,
  5744. (void **)&ahd->shared_data_map.vaddr,
  5745. BUS_DMA_NOWAIT,
  5746. &ahd->shared_data_map.dmamap) != 0) {
  5747. return (ENOMEM);
  5748. }
  5749. ahd->init_level++;
  5750. /* And permanently map it in */
  5751. ahd_dmamap_load(ahd, ahd->shared_data_dmat, ahd->shared_data_map.dmamap,
  5752. ahd->shared_data_map.vaddr, driver_data_size,
  5753. ahd_dmamap_cb, &ahd->shared_data_map.physaddr,
  5754. /*flags*/0);
  5755. ahd->qoutfifo = (struct ahd_completion *)ahd->shared_data_map.vaddr;
  5756. next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
  5757. next_baddr = ahd->shared_data_map.physaddr
  5758. + AHD_QOUT_SIZE*sizeof(struct ahd_completion);
  5759. if ((ahd->features & AHD_TARGETMODE) != 0) {
  5760. ahd->targetcmds = (struct target_cmd *)next_vaddr;
  5761. next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5762. next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
  5763. }
  5764. if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
  5765. ahd->overrun_buf = next_vaddr;
  5766. next_vaddr += PKT_OVERRUN_BUFSIZE;
  5767. next_baddr += PKT_OVERRUN_BUFSIZE;
  5768. }
  5769. /*
  5770. * We need one SCB to serve as the "next SCB". Since the
  5771. * tag identifier in this SCB will never be used, there is
  5772. * no point in using a valid HSCB tag from an SCB pulled from
  5773. * the standard free pool. So, we allocate this "sentinel"
  5774. * specially from the DMA safe memory chunk used for the QOUTFIFO.
  5775. */
  5776. ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
  5777. ahd->next_queued_hscb_map = &ahd->shared_data_map;
  5778. ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
  5779. ahd->init_level++;
  5780. /* Allocate SCB data now that buffer_dmat is initialized */
  5781. if (ahd_init_scbdata(ahd) != 0)
  5782. return (ENOMEM);
  5783. if ((ahd->flags & AHD_INITIATORROLE) == 0)
  5784. ahd->flags &= ~AHD_RESET_BUS_A;
  5785. /*
  5786. * Before committing these settings to the chip, give
  5787. * the OSM one last chance to modify our configuration.
  5788. */
  5789. ahd_platform_init(ahd);
  5790. /* Bring up the chip. */
  5791. ahd_chip_init(ahd);
  5792. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  5793. if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
  5794. goto init_done;
  5795. /*
  5796. * Verify termination based on current draw and
  5797. * warn user if the bus is over/under terminated.
  5798. */
  5799. error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
  5800. CURSENSE_ENB);
  5801. if (error != 0) {
  5802. printf("%s: current sensing timeout 1\n", ahd_name(ahd));
  5803. goto init_done;
  5804. }
  5805. for (i = 20, fstat = FLX_FSTAT_BUSY;
  5806. (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
  5807. error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
  5808. if (error != 0) {
  5809. printf("%s: current sensing timeout 2\n",
  5810. ahd_name(ahd));
  5811. goto init_done;
  5812. }
  5813. }
  5814. if (i == 0) {
  5815. printf("%s: Timedout during current-sensing test\n",
  5816. ahd_name(ahd));
  5817. goto init_done;
  5818. }
  5819. /* Latch Current Sensing status. */
  5820. error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, &current_sensing);
  5821. if (error != 0) {
  5822. printf("%s: current sensing timeout 3\n", ahd_name(ahd));
  5823. goto init_done;
  5824. }
  5825. /* Diable current sensing. */
  5826. ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
  5827. #ifdef AHD_DEBUG
  5828. if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
  5829. printf("%s: current_sensing == 0x%x\n",
  5830. ahd_name(ahd), current_sensing);
  5831. }
  5832. #endif
  5833. warn_user = 0;
  5834. for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
  5835. u_int term_stat;
  5836. term_stat = (current_sensing & FLX_CSTAT_MASK);
  5837. switch (term_stat) {
  5838. case FLX_CSTAT_OVER:
  5839. case FLX_CSTAT_UNDER:
  5840. warn_user++;
  5841. case FLX_CSTAT_INVALID:
  5842. case FLX_CSTAT_OKAY:
  5843. if (warn_user == 0 && bootverbose == 0)
  5844. break;
  5845. printf("%s: %s Channel %s\n", ahd_name(ahd),
  5846. channel_strings[i], termstat_strings[term_stat]);
  5847. break;
  5848. }
  5849. }
  5850. if (warn_user) {
  5851. printf("%s: WARNING. Termination is not configured correctly.\n"
  5852. "%s: WARNING. SCSI bus operations may FAIL.\n",
  5853. ahd_name(ahd), ahd_name(ahd));
  5854. }
  5855. init_done:
  5856. ahd_restart(ahd);
  5857. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  5858. ahd_stat_timer, ahd);
  5859. return (0);
  5860. }
  5861. /*
  5862. * (Re)initialize chip state after a chip reset.
  5863. */
  5864. static void
  5865. ahd_chip_init(struct ahd_softc *ahd)
  5866. {
  5867. uint32_t busaddr;
  5868. u_int sxfrctl1;
  5869. u_int scsiseq_template;
  5870. u_int wait;
  5871. u_int i;
  5872. u_int target;
  5873. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  5874. /*
  5875. * Take the LED out of diagnostic mode
  5876. */
  5877. ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
  5878. /*
  5879. * Return HS_MAILBOX to its default value.
  5880. */
  5881. ahd->hs_mailbox = 0;
  5882. ahd_outb(ahd, HS_MAILBOX, 0);
  5883. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
  5884. ahd_outb(ahd, IOWNID, ahd->our_id);
  5885. ahd_outb(ahd, TOWNID, ahd->our_id);
  5886. sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
  5887. sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
  5888. if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
  5889. && (ahd->seltime != STIMESEL_MIN)) {
  5890. /*
  5891. * The selection timer duration is twice as long
  5892. * as it should be. Halve it by adding "1" to
  5893. * the user specified setting.
  5894. */
  5895. sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
  5896. } else {
  5897. sxfrctl1 |= ahd->seltime;
  5898. }
  5899. ahd_outb(ahd, SXFRCTL0, DFON);
  5900. ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
  5901. ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  5902. /*
  5903. * Now that termination is set, wait for up
  5904. * to 500ms for our transceivers to settle. If
  5905. * the adapter does not have a cable attached,
  5906. * the transceivers may never settle, so don't
  5907. * complain if we fail here.
  5908. */
  5909. for (wait = 10000;
  5910. (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  5911. wait--)
  5912. ahd_delay(100);
  5913. /* Clear any false bus resets due to the transceivers settling */
  5914. ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
  5915. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  5916. /* Initialize mode specific S/G state. */
  5917. for (i = 0; i < 2; i++) {
  5918. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  5919. ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
  5920. ahd_outb(ahd, SG_STATE, 0);
  5921. ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
  5922. ahd_outb(ahd, SEQIMODE,
  5923. ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
  5924. |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
  5925. }
  5926. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  5927. ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
  5928. ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
  5929. ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
  5930. ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
  5931. if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
  5932. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
  5933. } else {
  5934. ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
  5935. }
  5936. ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
  5937. if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
  5938. /*
  5939. * Do not issue a target abort when a split completion
  5940. * error occurs. Let our PCIX interrupt handler deal
  5941. * with it instead. H2A4 Razor #625
  5942. */
  5943. ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
  5944. if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
  5945. ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
  5946. /*
  5947. * Tweak IOCELL settings.
  5948. */
  5949. if ((ahd->flags & AHD_HP_BOARD) != 0) {
  5950. for (i = 0; i < NUMDSPS; i++) {
  5951. ahd_outb(ahd, DSPSELECT, i);
  5952. ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
  5953. }
  5954. #ifdef AHD_DEBUG
  5955. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  5956. printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
  5957. WRTBIASCTL_HP_DEFAULT);
  5958. #endif
  5959. }
  5960. ahd_setup_iocell_workaround(ahd);
  5961. /*
  5962. * Enable LQI Manager interrupts.
  5963. */
  5964. ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
  5965. | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
  5966. | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
  5967. ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
  5968. /*
  5969. * We choose to have the sequencer catch LQOPHCHGINPKT errors
  5970. * manually for the command phase at the start of a packetized
  5971. * selection case. ENLQOBUSFREE should be made redundant by
  5972. * the BUSFREE interrupt, but it seems that some LQOBUSFREE
  5973. * events fail to assert the BUSFREE interrupt so we must
  5974. * also enable LQOBUSFREE interrupts.
  5975. */
  5976. ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE);
  5977. /*
  5978. * Setup sequencer interrupt handlers.
  5979. */
  5980. ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
  5981. ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
  5982. /*
  5983. * Setup SCB Offset registers.
  5984. */
  5985. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  5986. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
  5987. pkt_long_lun));
  5988. } else {
  5989. ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
  5990. }
  5991. ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
  5992. ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
  5993. ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
  5994. ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
  5995. shared_data.idata.cdb));
  5996. ahd_outb(ahd, QNEXTPTR,
  5997. offsetof(struct hardware_scb, next_hscb_busaddr));
  5998. ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
  5999. ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
  6000. if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
  6001. ahd_outb(ahd, LUNLEN,
  6002. sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
  6003. } else {
  6004. ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
  6005. }
  6006. ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
  6007. ahd_outb(ahd, MAXCMD, 0xFF);
  6008. ahd_outb(ahd, SCBAUTOPTR,
  6009. AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
  6010. /* We haven't been enabled for target mode yet. */
  6011. ahd_outb(ahd, MULTARGID, 0);
  6012. ahd_outb(ahd, MULTARGID + 1, 0);
  6013. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6014. /* Initialize the negotiation table. */
  6015. if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
  6016. /*
  6017. * Clear the spare bytes in the neg table to avoid
  6018. * spurious parity errors.
  6019. */
  6020. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  6021. ahd_outb(ahd, NEGOADDR, target);
  6022. ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
  6023. for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
  6024. ahd_outb(ahd, ANNEXDAT, 0);
  6025. }
  6026. }
  6027. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  6028. struct ahd_devinfo devinfo;
  6029. struct ahd_initiator_tinfo *tinfo;
  6030. struct ahd_tmode_tstate *tstate;
  6031. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6032. target, &tstate);
  6033. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6034. target, CAM_LUN_WILDCARD,
  6035. 'A', ROLE_INITIATOR);
  6036. ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
  6037. }
  6038. ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
  6039. ahd_outb(ahd, CLRINT, CLRSCSIINT);
  6040. #ifdef NEEDS_MORE_TESTING
  6041. /*
  6042. * Always enable abort on incoming L_Qs if this feature is
  6043. * supported. We use this to catch invalid SCB references.
  6044. */
  6045. if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
  6046. ahd_outb(ahd, LQCTL1, ABORTPENDING);
  6047. else
  6048. #endif
  6049. ahd_outb(ahd, LQCTL1, 0);
  6050. /* All of our queues are empty */
  6051. ahd->qoutfifonext = 0;
  6052. ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID;
  6053. ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID);
  6054. for (i = 0; i < AHD_QOUT_SIZE; i++)
  6055. ahd->qoutfifo[i].valid_tag = 0;
  6056. ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
  6057. ahd->qinfifonext = 0;
  6058. for (i = 0; i < AHD_QIN_SIZE; i++)
  6059. ahd->qinfifo[i] = SCB_LIST_NULL;
  6060. if ((ahd->features & AHD_TARGETMODE) != 0) {
  6061. /* All target command blocks start out invalid. */
  6062. for (i = 0; i < AHD_TMODE_CMDS; i++)
  6063. ahd->targetcmds[i].cmd_valid = 0;
  6064. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
  6065. ahd->tqinfifonext = 1;
  6066. ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
  6067. ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
  6068. }
  6069. /* Initialize Scratch Ram. */
  6070. ahd_outb(ahd, SEQ_FLAGS, 0);
  6071. ahd_outb(ahd, SEQ_FLAGS2, 0);
  6072. /* We don't have any waiting selections */
  6073. ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
  6074. ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
  6075. ahd_outw(ahd, MK_MESSAGE_SCB, SCB_LIST_NULL);
  6076. ahd_outw(ahd, MK_MESSAGE_SCSIID, 0xFF);
  6077. for (i = 0; i < AHD_NUM_TARGETS; i++)
  6078. ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
  6079. /*
  6080. * Nobody is waiting to be DMAed into the QOUTFIFO.
  6081. */
  6082. ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
  6083. ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
  6084. ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
  6085. ahd_outw(ahd, COMPLETE_DMA_SCB_TAIL, SCB_LIST_NULL);
  6086. ahd_outw(ahd, COMPLETE_ON_QFREEZE_HEAD, SCB_LIST_NULL);
  6087. /*
  6088. * The Freeze Count is 0.
  6089. */
  6090. ahd->qfreeze_cnt = 0;
  6091. ahd_outw(ahd, QFREEZE_COUNT, 0);
  6092. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, 0);
  6093. /*
  6094. * Tell the sequencer where it can find our arrays in memory.
  6095. */
  6096. busaddr = ahd->shared_data_map.physaddr;
  6097. ahd_outl(ahd, SHARED_DATA_ADDR, busaddr);
  6098. ahd_outl(ahd, QOUTFIFO_NEXT_ADDR, busaddr);
  6099. /*
  6100. * Setup the allowed SCSI Sequences based on operational mode.
  6101. * If we are a target, we'll enable select in operations once
  6102. * we've had a lun enabled.
  6103. */
  6104. scsiseq_template = ENAUTOATNP;
  6105. if ((ahd->flags & AHD_INITIATORROLE) != 0)
  6106. scsiseq_template |= ENRSELI;
  6107. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
  6108. /* There are no busy SCBs yet. */
  6109. for (target = 0; target < AHD_NUM_TARGETS; target++) {
  6110. int lun;
  6111. for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
  6112. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
  6113. }
  6114. /*
  6115. * Initialize the group code to command length table.
  6116. * Vendor Unique codes are set to 0 so we only capture
  6117. * the first byte of the cdb. These can be overridden
  6118. * when target mode is enabled.
  6119. */
  6120. ahd_outb(ahd, CMDSIZE_TABLE, 5);
  6121. ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
  6122. ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
  6123. ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
  6124. ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
  6125. ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
  6126. ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
  6127. ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
  6128. /* Tell the sequencer of our initial queue positions */
  6129. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6130. ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
  6131. ahd->qinfifonext = 0;
  6132. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6133. ahd_set_hescb_qoff(ahd, 0);
  6134. ahd_set_snscb_qoff(ahd, 0);
  6135. ahd_set_sescb_qoff(ahd, 0);
  6136. ahd_set_sdscb_qoff(ahd, 0);
  6137. /*
  6138. * Tell the sequencer which SCB will be the next one it receives.
  6139. */
  6140. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6141. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6142. /*
  6143. * Default to coalescing disabled.
  6144. */
  6145. ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
  6146. ahd_outw(ahd, CMDS_PENDING, 0);
  6147. ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
  6148. ahd->int_coalescing_maxcmds,
  6149. ahd->int_coalescing_mincmds);
  6150. ahd_enable_coalescing(ahd, FALSE);
  6151. ahd_loadseq(ahd);
  6152. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6153. if (ahd->features & AHD_AIC79XXB_SLOWCRC) {
  6154. u_int negodat3 = ahd_inb(ahd, NEGCONOPTS);
  6155. negodat3 |= ENSLOWCRC;
  6156. ahd_outb(ahd, NEGCONOPTS, negodat3);
  6157. negodat3 = ahd_inb(ahd, NEGCONOPTS);
  6158. if (!(negodat3 & ENSLOWCRC))
  6159. printf("aic79xx: failed to set the SLOWCRC bit\n");
  6160. else
  6161. printf("aic79xx: SLOWCRC bit set\n");
  6162. }
  6163. }
  6164. /*
  6165. * Setup default device and controller settings.
  6166. * This should only be called if our probe has
  6167. * determined that no configuration data is available.
  6168. */
  6169. int
  6170. ahd_default_config(struct ahd_softc *ahd)
  6171. {
  6172. int targ;
  6173. ahd->our_id = 7;
  6174. /*
  6175. * Allocate a tstate to house information for our
  6176. * initiator presence on the bus as well as the user
  6177. * data for any target mode initiator.
  6178. */
  6179. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6180. printf("%s: unable to allocate ahd_tmode_tstate. "
  6181. "Failing attach\n", ahd_name(ahd));
  6182. return (ENOMEM);
  6183. }
  6184. for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
  6185. struct ahd_devinfo devinfo;
  6186. struct ahd_initiator_tinfo *tinfo;
  6187. struct ahd_tmode_tstate *tstate;
  6188. uint16_t target_mask;
  6189. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6190. targ, &tstate);
  6191. /*
  6192. * We support SPC2 and SPI4.
  6193. */
  6194. tinfo->user.protocol_version = 4;
  6195. tinfo->user.transport_version = 4;
  6196. target_mask = 0x01 << targ;
  6197. ahd->user_discenable |= target_mask;
  6198. tstate->discenable |= target_mask;
  6199. ahd->user_tagenable |= target_mask;
  6200. #ifdef AHD_FORCE_160
  6201. tinfo->user.period = AHD_SYNCRATE_DT;
  6202. #else
  6203. tinfo->user.period = AHD_SYNCRATE_160;
  6204. #endif
  6205. tinfo->user.offset = MAX_OFFSET;
  6206. tinfo->user.ppr_options = MSG_EXT_PPR_RD_STRM
  6207. | MSG_EXT_PPR_WR_FLOW
  6208. | MSG_EXT_PPR_HOLD_MCS
  6209. | MSG_EXT_PPR_IU_REQ
  6210. | MSG_EXT_PPR_QAS_REQ
  6211. | MSG_EXT_PPR_DT_REQ;
  6212. if ((ahd->features & AHD_RTI) != 0)
  6213. tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
  6214. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  6215. /*
  6216. * Start out Async/Narrow/Untagged and with
  6217. * conservative protocol support.
  6218. */
  6219. tinfo->goal.protocol_version = 2;
  6220. tinfo->goal.transport_version = 2;
  6221. tinfo->curr.protocol_version = 2;
  6222. tinfo->curr.transport_version = 2;
  6223. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6224. targ, CAM_LUN_WILDCARD,
  6225. 'A', ROLE_INITIATOR);
  6226. tstate->tagenable &= ~target_mask;
  6227. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6228. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6229. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6230. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6231. /*paused*/TRUE);
  6232. }
  6233. return (0);
  6234. }
  6235. /*
  6236. * Parse device configuration information.
  6237. */
  6238. int
  6239. ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
  6240. {
  6241. int targ;
  6242. int max_targ;
  6243. max_targ = sc->max_targets & CFMAXTARG;
  6244. ahd->our_id = sc->brtime_id & CFSCSIID;
  6245. /*
  6246. * Allocate a tstate to house information for our
  6247. * initiator presence on the bus as well as the user
  6248. * data for any target mode initiator.
  6249. */
  6250. if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
  6251. printf("%s: unable to allocate ahd_tmode_tstate. "
  6252. "Failing attach\n", ahd_name(ahd));
  6253. return (ENOMEM);
  6254. }
  6255. for (targ = 0; targ < max_targ; targ++) {
  6256. struct ahd_devinfo devinfo;
  6257. struct ahd_initiator_tinfo *tinfo;
  6258. struct ahd_transinfo *user_tinfo;
  6259. struct ahd_tmode_tstate *tstate;
  6260. uint16_t target_mask;
  6261. tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
  6262. targ, &tstate);
  6263. user_tinfo = &tinfo->user;
  6264. /*
  6265. * We support SPC2 and SPI4.
  6266. */
  6267. tinfo->user.protocol_version = 4;
  6268. tinfo->user.transport_version = 4;
  6269. target_mask = 0x01 << targ;
  6270. ahd->user_discenable &= ~target_mask;
  6271. tstate->discenable &= ~target_mask;
  6272. ahd->user_tagenable &= ~target_mask;
  6273. if (sc->device_flags[targ] & CFDISC) {
  6274. tstate->discenable |= target_mask;
  6275. ahd->user_discenable |= target_mask;
  6276. ahd->user_tagenable |= target_mask;
  6277. } else {
  6278. /*
  6279. * Cannot be packetized without disconnection.
  6280. */
  6281. sc->device_flags[targ] &= ~CFPACKETIZED;
  6282. }
  6283. user_tinfo->ppr_options = 0;
  6284. user_tinfo->period = (sc->device_flags[targ] & CFXFER);
  6285. if (user_tinfo->period < CFXFER_ASYNC) {
  6286. if (user_tinfo->period <= AHD_PERIOD_10MHz)
  6287. user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
  6288. user_tinfo->offset = MAX_OFFSET;
  6289. } else {
  6290. user_tinfo->offset = 0;
  6291. user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
  6292. }
  6293. #ifdef AHD_FORCE_160
  6294. if (user_tinfo->period <= AHD_SYNCRATE_160)
  6295. user_tinfo->period = AHD_SYNCRATE_DT;
  6296. #endif
  6297. if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
  6298. user_tinfo->ppr_options |= MSG_EXT_PPR_RD_STRM
  6299. | MSG_EXT_PPR_WR_FLOW
  6300. | MSG_EXT_PPR_HOLD_MCS
  6301. | MSG_EXT_PPR_IU_REQ;
  6302. if ((ahd->features & AHD_RTI) != 0)
  6303. user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
  6304. }
  6305. if ((sc->device_flags[targ] & CFQAS) != 0)
  6306. user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
  6307. if ((sc->device_flags[targ] & CFWIDEB) != 0)
  6308. user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
  6309. else
  6310. user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
  6311. #ifdef AHD_DEBUG
  6312. if ((ahd_debug & AHD_SHOW_MISC) != 0)
  6313. printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
  6314. user_tinfo->period, user_tinfo->offset,
  6315. user_tinfo->ppr_options);
  6316. #endif
  6317. /*
  6318. * Start out Async/Narrow/Untagged and with
  6319. * conservative protocol support.
  6320. */
  6321. tstate->tagenable &= ~target_mask;
  6322. tinfo->goal.protocol_version = 2;
  6323. tinfo->goal.transport_version = 2;
  6324. tinfo->curr.protocol_version = 2;
  6325. tinfo->curr.transport_version = 2;
  6326. ahd_compile_devinfo(&devinfo, ahd->our_id,
  6327. targ, CAM_LUN_WILDCARD,
  6328. 'A', ROLE_INITIATOR);
  6329. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  6330. AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
  6331. ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
  6332. /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
  6333. /*paused*/TRUE);
  6334. }
  6335. ahd->flags &= ~AHD_SPCHK_ENB_A;
  6336. if (sc->bios_control & CFSPARITY)
  6337. ahd->flags |= AHD_SPCHK_ENB_A;
  6338. ahd->flags &= ~AHD_RESET_BUS_A;
  6339. if (sc->bios_control & CFRESETB)
  6340. ahd->flags |= AHD_RESET_BUS_A;
  6341. ahd->flags &= ~AHD_EXTENDED_TRANS_A;
  6342. if (sc->bios_control & CFEXTEND)
  6343. ahd->flags |= AHD_EXTENDED_TRANS_A;
  6344. ahd->flags &= ~AHD_BIOS_ENABLED;
  6345. if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
  6346. ahd->flags |= AHD_BIOS_ENABLED;
  6347. ahd->flags &= ~AHD_STPWLEVEL_A;
  6348. if ((sc->adapter_control & CFSTPWLEVEL) != 0)
  6349. ahd->flags |= AHD_STPWLEVEL_A;
  6350. return (0);
  6351. }
  6352. /*
  6353. * Parse device configuration information.
  6354. */
  6355. int
  6356. ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
  6357. {
  6358. int error;
  6359. error = ahd_verify_vpd_cksum(vpd);
  6360. if (error == 0)
  6361. return (EINVAL);
  6362. if ((vpd->bios_flags & VPDBOOTHOST) != 0)
  6363. ahd->flags |= AHD_BOOT_CHANNEL;
  6364. return (0);
  6365. }
  6366. void
  6367. ahd_intr_enable(struct ahd_softc *ahd, int enable)
  6368. {
  6369. u_int hcntrl;
  6370. hcntrl = ahd_inb(ahd, HCNTRL);
  6371. hcntrl &= ~INTEN;
  6372. ahd->pause &= ~INTEN;
  6373. ahd->unpause &= ~INTEN;
  6374. if (enable) {
  6375. hcntrl |= INTEN;
  6376. ahd->pause |= INTEN;
  6377. ahd->unpause |= INTEN;
  6378. }
  6379. ahd_outb(ahd, HCNTRL, hcntrl);
  6380. }
  6381. void
  6382. ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
  6383. u_int mincmds)
  6384. {
  6385. if (timer > AHD_TIMER_MAX_US)
  6386. timer = AHD_TIMER_MAX_US;
  6387. ahd->int_coalescing_timer = timer;
  6388. if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
  6389. maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
  6390. if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
  6391. mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
  6392. ahd->int_coalescing_maxcmds = maxcmds;
  6393. ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
  6394. ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
  6395. ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
  6396. }
  6397. void
  6398. ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
  6399. {
  6400. ahd->hs_mailbox &= ~ENINT_COALESCE;
  6401. if (enable)
  6402. ahd->hs_mailbox |= ENINT_COALESCE;
  6403. ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
  6404. ahd_flush_device_writes(ahd);
  6405. ahd_run_qoutfifo(ahd);
  6406. }
  6407. /*
  6408. * Ensure that the card is paused in a location
  6409. * outside of all critical sections and that all
  6410. * pending work is completed prior to returning.
  6411. * This routine should only be called from outside
  6412. * an interrupt context.
  6413. */
  6414. void
  6415. ahd_pause_and_flushwork(struct ahd_softc *ahd)
  6416. {
  6417. u_int intstat;
  6418. u_int maxloops;
  6419. maxloops = 1000;
  6420. ahd->flags |= AHD_ALL_INTERRUPTS;
  6421. ahd_pause(ahd);
  6422. /*
  6423. * Freeze the outgoing selections. We do this only
  6424. * until we are safely paused without further selections
  6425. * pending.
  6426. */
  6427. ahd->qfreeze_cnt--;
  6428. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  6429. ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
  6430. do {
  6431. ahd_unpause(ahd);
  6432. /*
  6433. * Give the sequencer some time to service
  6434. * any active selections.
  6435. */
  6436. ahd_delay(500);
  6437. ahd_intr(ahd);
  6438. ahd_pause(ahd);
  6439. intstat = ahd_inb(ahd, INTSTAT);
  6440. if ((intstat & INT_PEND) == 0) {
  6441. ahd_clear_critical_section(ahd);
  6442. intstat = ahd_inb(ahd, INTSTAT);
  6443. }
  6444. } while (--maxloops
  6445. && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
  6446. && ((intstat & INT_PEND) != 0
  6447. || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
  6448. || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
  6449. if (maxloops == 0) {
  6450. printf("Infinite interrupt loop, INTSTAT = %x",
  6451. ahd_inb(ahd, INTSTAT));
  6452. }
  6453. ahd->qfreeze_cnt++;
  6454. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  6455. ahd_flush_qoutfifo(ahd);
  6456. ahd_platform_flushwork(ahd);
  6457. ahd->flags &= ~AHD_ALL_INTERRUPTS;
  6458. }
  6459. int
  6460. ahd_suspend(struct ahd_softc *ahd)
  6461. {
  6462. ahd_pause_and_flushwork(ahd);
  6463. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  6464. ahd_unpause(ahd);
  6465. return (EBUSY);
  6466. }
  6467. ahd_shutdown(ahd);
  6468. return (0);
  6469. }
  6470. int
  6471. ahd_resume(struct ahd_softc *ahd)
  6472. {
  6473. ahd_reset(ahd, /*reinit*/TRUE);
  6474. ahd_intr_enable(ahd, TRUE);
  6475. ahd_restart(ahd);
  6476. return (0);
  6477. }
  6478. /************************** Busy Target Table *********************************/
  6479. /*
  6480. * Set SCBPTR to the SCB that contains the busy
  6481. * table entry for TCL. Return the offset into
  6482. * the SCB that contains the entry for TCL.
  6483. * saved_scbid is dereferenced and set to the
  6484. * scbid that should be restored once manipualtion
  6485. * of the TCL entry is complete.
  6486. */
  6487. static __inline u_int
  6488. ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
  6489. {
  6490. /*
  6491. * Index to the SCB that contains the busy entry.
  6492. */
  6493. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6494. *saved_scbid = ahd_get_scbptr(ahd);
  6495. ahd_set_scbptr(ahd, TCL_LUN(tcl)
  6496. | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
  6497. /*
  6498. * And now calculate the SCB offset to the entry.
  6499. * Each entry is 2 bytes wide, hence the
  6500. * multiplication by 2.
  6501. */
  6502. return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
  6503. }
  6504. /*
  6505. * Return the untagged transaction id for a given target/channel lun.
  6506. */
  6507. u_int
  6508. ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
  6509. {
  6510. u_int scbid;
  6511. u_int scb_offset;
  6512. u_int saved_scbptr;
  6513. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6514. scbid = ahd_inw_scbram(ahd, scb_offset);
  6515. ahd_set_scbptr(ahd, saved_scbptr);
  6516. return (scbid);
  6517. }
  6518. void
  6519. ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
  6520. {
  6521. u_int scb_offset;
  6522. u_int saved_scbptr;
  6523. scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
  6524. ahd_outw(ahd, scb_offset, scbid);
  6525. ahd_set_scbptr(ahd, saved_scbptr);
  6526. }
  6527. /************************** SCB and SCB queue management **********************/
  6528. int
  6529. ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
  6530. char channel, int lun, u_int tag, role_t role)
  6531. {
  6532. int targ = SCB_GET_TARGET(ahd, scb);
  6533. char chan = SCB_GET_CHANNEL(ahd, scb);
  6534. int slun = SCB_GET_LUN(scb);
  6535. int match;
  6536. match = ((chan == channel) || (channel == ALL_CHANNELS));
  6537. if (match != 0)
  6538. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  6539. if (match != 0)
  6540. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  6541. if (match != 0) {
  6542. #ifdef AHD_TARGET_MODE
  6543. int group;
  6544. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  6545. if (role == ROLE_INITIATOR) {
  6546. match = (group != XPT_FC_GROUP_TMODE)
  6547. && ((tag == SCB_GET_TAG(scb))
  6548. || (tag == SCB_LIST_NULL));
  6549. } else if (role == ROLE_TARGET) {
  6550. match = (group == XPT_FC_GROUP_TMODE)
  6551. && ((tag == scb->io_ctx->csio.tag_id)
  6552. || (tag == SCB_LIST_NULL));
  6553. }
  6554. #else /* !AHD_TARGET_MODE */
  6555. match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
  6556. #endif /* AHD_TARGET_MODE */
  6557. }
  6558. return match;
  6559. }
  6560. void
  6561. ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
  6562. {
  6563. int target;
  6564. char channel;
  6565. int lun;
  6566. target = SCB_GET_TARGET(ahd, scb);
  6567. lun = SCB_GET_LUN(scb);
  6568. channel = SCB_GET_CHANNEL(ahd, scb);
  6569. ahd_search_qinfifo(ahd, target, channel, lun,
  6570. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  6571. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  6572. ahd_platform_freeze_devq(ahd, scb);
  6573. }
  6574. void
  6575. ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
  6576. {
  6577. struct scb *prev_scb;
  6578. ahd_mode_state saved_modes;
  6579. saved_modes = ahd_save_modes(ahd);
  6580. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6581. prev_scb = NULL;
  6582. if (ahd_qinfifo_count(ahd) != 0) {
  6583. u_int prev_tag;
  6584. u_int prev_pos;
  6585. prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
  6586. prev_tag = ahd->qinfifo[prev_pos];
  6587. prev_scb = ahd_lookup_scb(ahd, prev_tag);
  6588. }
  6589. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6590. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6591. ahd_restore_modes(ahd, saved_modes);
  6592. }
  6593. static void
  6594. ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
  6595. struct scb *scb)
  6596. {
  6597. if (prev_scb == NULL) {
  6598. uint32_t busaddr;
  6599. busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
  6600. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6601. } else {
  6602. prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
  6603. ahd_sync_scb(ahd, prev_scb,
  6604. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6605. }
  6606. ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
  6607. ahd->qinfifonext++;
  6608. scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
  6609. ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  6610. }
  6611. static int
  6612. ahd_qinfifo_count(struct ahd_softc *ahd)
  6613. {
  6614. u_int qinpos;
  6615. u_int wrap_qinpos;
  6616. u_int wrap_qinfifonext;
  6617. AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
  6618. qinpos = ahd_get_snscb_qoff(ahd);
  6619. wrap_qinpos = AHD_QIN_WRAP(qinpos);
  6620. wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
  6621. if (wrap_qinfifonext >= wrap_qinpos)
  6622. return (wrap_qinfifonext - wrap_qinpos);
  6623. else
  6624. return (wrap_qinfifonext
  6625. + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
  6626. }
  6627. void
  6628. ahd_reset_cmds_pending(struct ahd_softc *ahd)
  6629. {
  6630. struct scb *scb;
  6631. ahd_mode_state saved_modes;
  6632. u_int pending_cmds;
  6633. saved_modes = ahd_save_modes(ahd);
  6634. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6635. /*
  6636. * Don't count any commands as outstanding that the
  6637. * sequencer has already marked for completion.
  6638. */
  6639. ahd_flush_qoutfifo(ahd);
  6640. pending_cmds = 0;
  6641. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  6642. pending_cmds++;
  6643. }
  6644. ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
  6645. ahd_restore_modes(ahd, saved_modes);
  6646. ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
  6647. }
  6648. void
  6649. ahd_done_with_status(struct ahd_softc *ahd, struct scb *scb, uint32_t status)
  6650. {
  6651. cam_status ostat;
  6652. cam_status cstat;
  6653. ostat = ahd_get_transaction_status(scb);
  6654. if (ostat == CAM_REQ_INPROG)
  6655. ahd_set_transaction_status(scb, status);
  6656. cstat = ahd_get_transaction_status(scb);
  6657. if (cstat != CAM_REQ_CMP)
  6658. ahd_freeze_scb(scb);
  6659. ahd_done(ahd, scb);
  6660. }
  6661. int
  6662. ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
  6663. int lun, u_int tag, role_t role, uint32_t status,
  6664. ahd_search_action action)
  6665. {
  6666. struct scb *scb;
  6667. struct scb *mk_msg_scb;
  6668. struct scb *prev_scb;
  6669. ahd_mode_state saved_modes;
  6670. u_int qinstart;
  6671. u_int qinpos;
  6672. u_int qintail;
  6673. u_int tid_next;
  6674. u_int tid_prev;
  6675. u_int scbid;
  6676. u_int seq_flags2;
  6677. u_int savedscbptr;
  6678. uint32_t busaddr;
  6679. int found;
  6680. int targets;
  6681. /* Must be in CCHAN mode */
  6682. saved_modes = ahd_save_modes(ahd);
  6683. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  6684. /*
  6685. * Halt any pending SCB DMA. The sequencer will reinitiate
  6686. * this dma if the qinfifo is not empty once we unpause.
  6687. */
  6688. if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
  6689. == (CCARREN|CCSCBEN|CCSCBDIR)) {
  6690. ahd_outb(ahd, CCSCBCTL,
  6691. ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
  6692. while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
  6693. ;
  6694. }
  6695. /* Determine sequencer's position in the qinfifo. */
  6696. qintail = AHD_QIN_WRAP(ahd->qinfifonext);
  6697. qinstart = ahd_get_snscb_qoff(ahd);
  6698. qinpos = AHD_QIN_WRAP(qinstart);
  6699. found = 0;
  6700. prev_scb = NULL;
  6701. if (action == SEARCH_PRINT) {
  6702. printf("qinstart = %d qinfifonext = %d\nQINFIFO:",
  6703. qinstart, ahd->qinfifonext);
  6704. }
  6705. /*
  6706. * Start with an empty queue. Entries that are not chosen
  6707. * for removal will be re-added to the queue as we go.
  6708. */
  6709. ahd->qinfifonext = qinstart;
  6710. busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
  6711. ahd_outl(ahd, NEXT_QUEUED_SCB_ADDR, busaddr);
  6712. while (qinpos != qintail) {
  6713. scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
  6714. if (scb == NULL) {
  6715. printf("qinpos = %d, SCB index = %d\n",
  6716. qinpos, ahd->qinfifo[qinpos]);
  6717. panic("Loop 1\n");
  6718. }
  6719. if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
  6720. /*
  6721. * We found an scb that needs to be acted on.
  6722. */
  6723. found++;
  6724. switch (action) {
  6725. case SEARCH_COMPLETE:
  6726. if ((scb->flags & SCB_ACTIVE) == 0)
  6727. printf("Inactive SCB in qinfifo\n");
  6728. ahd_done_with_status(ahd, scb, status);
  6729. /* FALLTHROUGH */
  6730. case SEARCH_REMOVE:
  6731. break;
  6732. case SEARCH_PRINT:
  6733. printf(" 0x%x", ahd->qinfifo[qinpos]);
  6734. /* FALLTHROUGH */
  6735. case SEARCH_COUNT:
  6736. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6737. prev_scb = scb;
  6738. break;
  6739. }
  6740. } else {
  6741. ahd_qinfifo_requeue(ahd, prev_scb, scb);
  6742. prev_scb = scb;
  6743. }
  6744. qinpos = AHD_QIN_WRAP(qinpos+1);
  6745. }
  6746. ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
  6747. if (action == SEARCH_PRINT)
  6748. printf("\nWAITING_TID_QUEUES:\n");
  6749. /*
  6750. * Search waiting for selection lists. We traverse the
  6751. * list of "their ids" waiting for selection and, if
  6752. * appropriate, traverse the SCBs of each "their id"
  6753. * looking for matches.
  6754. */
  6755. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  6756. seq_flags2 = ahd_inb(ahd, SEQ_FLAGS2);
  6757. if ((seq_flags2 & PENDING_MK_MESSAGE) != 0) {
  6758. scbid = ahd_inw(ahd, MK_MESSAGE_SCB);
  6759. mk_msg_scb = ahd_lookup_scb(ahd, scbid);
  6760. } else
  6761. mk_msg_scb = NULL;
  6762. savedscbptr = ahd_get_scbptr(ahd);
  6763. tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
  6764. tid_prev = SCB_LIST_NULL;
  6765. targets = 0;
  6766. for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
  6767. u_int tid_head;
  6768. u_int tid_tail;
  6769. targets++;
  6770. if (targets > AHD_NUM_TARGETS)
  6771. panic("TID LIST LOOP");
  6772. if (scbid >= ahd->scb_data.numscbs) {
  6773. printf("%s: Waiting TID List inconsistency. "
  6774. "SCB index == 0x%x, yet numscbs == 0x%x.",
  6775. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6776. ahd_dump_card_state(ahd);
  6777. panic("for safety");
  6778. }
  6779. scb = ahd_lookup_scb(ahd, scbid);
  6780. if (scb == NULL) {
  6781. printf("%s: SCB = 0x%x Not Active!\n",
  6782. ahd_name(ahd), scbid);
  6783. panic("Waiting TID List traversal\n");
  6784. }
  6785. ahd_set_scbptr(ahd, scbid);
  6786. tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
  6787. if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
  6788. SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
  6789. tid_prev = scbid;
  6790. continue;
  6791. }
  6792. /*
  6793. * We found a list of scbs that needs to be searched.
  6794. */
  6795. if (action == SEARCH_PRINT)
  6796. printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
  6797. tid_head = scbid;
  6798. found += ahd_search_scb_list(ahd, target, channel,
  6799. lun, tag, role, status,
  6800. action, &tid_head, &tid_tail,
  6801. SCB_GET_TARGET(ahd, scb));
  6802. /*
  6803. * Check any MK_MESSAGE SCB that is still waiting to
  6804. * enter this target's waiting for selection queue.
  6805. */
  6806. if (mk_msg_scb != NULL
  6807. && ahd_match_scb(ahd, mk_msg_scb, target, channel,
  6808. lun, tag, role)) {
  6809. /*
  6810. * We found an scb that needs to be acted on.
  6811. */
  6812. found++;
  6813. switch (action) {
  6814. case SEARCH_COMPLETE:
  6815. if ((mk_msg_scb->flags & SCB_ACTIVE) == 0)
  6816. printf("Inactive SCB pending MK_MSG\n");
  6817. ahd_done_with_status(ahd, mk_msg_scb, status);
  6818. /* FALLTHROUGH */
  6819. case SEARCH_REMOVE:
  6820. {
  6821. u_int tail_offset;
  6822. printf("Removing MK_MSG scb\n");
  6823. /*
  6824. * Reset our tail to the tail of the
  6825. * main per-target list.
  6826. */
  6827. tail_offset = WAITING_SCB_TAILS
  6828. + (2 * SCB_GET_TARGET(ahd, mk_msg_scb));
  6829. ahd_outw(ahd, tail_offset, tid_tail);
  6830. seq_flags2 &= ~PENDING_MK_MESSAGE;
  6831. ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
  6832. ahd_outw(ahd, CMDS_PENDING,
  6833. ahd_inw(ahd, CMDS_PENDING)-1);
  6834. mk_msg_scb = NULL;
  6835. break;
  6836. }
  6837. case SEARCH_PRINT:
  6838. printf(" 0x%x", SCB_GET_TAG(scb));
  6839. /* FALLTHROUGH */
  6840. case SEARCH_COUNT:
  6841. break;
  6842. }
  6843. }
  6844. if (mk_msg_scb != NULL
  6845. && SCBID_IS_NULL(tid_head)
  6846. && ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
  6847. SCB_LIST_NULL, ROLE_UNKNOWN)) {
  6848. /*
  6849. * When removing the last SCB for a target
  6850. * queue with a pending MK_MESSAGE scb, we
  6851. * must queue the MK_MESSAGE scb.
  6852. */
  6853. printf("Queueing mk_msg_scb\n");
  6854. tid_head = ahd_inw(ahd, MK_MESSAGE_SCB);
  6855. seq_flags2 &= ~PENDING_MK_MESSAGE;
  6856. ahd_outb(ahd, SEQ_FLAGS2, seq_flags2);
  6857. mk_msg_scb = NULL;
  6858. }
  6859. if (tid_head != scbid)
  6860. ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
  6861. if (!SCBID_IS_NULL(tid_head))
  6862. tid_prev = tid_head;
  6863. if (action == SEARCH_PRINT)
  6864. printf(")\n");
  6865. }
  6866. /* Restore saved state. */
  6867. ahd_set_scbptr(ahd, savedscbptr);
  6868. ahd_restore_modes(ahd, saved_modes);
  6869. return (found);
  6870. }
  6871. static int
  6872. ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
  6873. int lun, u_int tag, role_t role, uint32_t status,
  6874. ahd_search_action action, u_int *list_head,
  6875. u_int *list_tail, u_int tid)
  6876. {
  6877. struct scb *scb;
  6878. u_int scbid;
  6879. u_int next;
  6880. u_int prev;
  6881. int found;
  6882. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6883. found = 0;
  6884. prev = SCB_LIST_NULL;
  6885. next = *list_head;
  6886. *list_tail = SCB_LIST_NULL;
  6887. for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
  6888. if (scbid >= ahd->scb_data.numscbs) {
  6889. printf("%s:SCB List inconsistency. "
  6890. "SCB == 0x%x, yet numscbs == 0x%x.",
  6891. ahd_name(ahd), scbid, ahd->scb_data.numscbs);
  6892. ahd_dump_card_state(ahd);
  6893. panic("for safety");
  6894. }
  6895. scb = ahd_lookup_scb(ahd, scbid);
  6896. if (scb == NULL) {
  6897. printf("%s: SCB = %d Not Active!\n",
  6898. ahd_name(ahd), scbid);
  6899. panic("Waiting List traversal\n");
  6900. }
  6901. ahd_set_scbptr(ahd, scbid);
  6902. *list_tail = scbid;
  6903. next = ahd_inw_scbram(ahd, SCB_NEXT);
  6904. if (ahd_match_scb(ahd, scb, target, channel,
  6905. lun, SCB_LIST_NULL, role) == 0) {
  6906. prev = scbid;
  6907. continue;
  6908. }
  6909. found++;
  6910. switch (action) {
  6911. case SEARCH_COMPLETE:
  6912. if ((scb->flags & SCB_ACTIVE) == 0)
  6913. printf("Inactive SCB in Waiting List\n");
  6914. ahd_done_with_status(ahd, scb, status);
  6915. /* FALLTHROUGH */
  6916. case SEARCH_REMOVE:
  6917. ahd_rem_wscb(ahd, scbid, prev, next, tid);
  6918. *list_tail = prev;
  6919. if (SCBID_IS_NULL(prev))
  6920. *list_head = next;
  6921. break;
  6922. case SEARCH_PRINT:
  6923. printf("0x%x ", scbid);
  6924. case SEARCH_COUNT:
  6925. prev = scbid;
  6926. break;
  6927. }
  6928. if (found > AHD_SCB_MAX)
  6929. panic("SCB LIST LOOP");
  6930. }
  6931. if (action == SEARCH_COMPLETE
  6932. || action == SEARCH_REMOVE)
  6933. ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
  6934. return (found);
  6935. }
  6936. static void
  6937. ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
  6938. u_int tid_cur, u_int tid_next)
  6939. {
  6940. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6941. if (SCBID_IS_NULL(tid_cur)) {
  6942. /* Bypass current TID list */
  6943. if (SCBID_IS_NULL(tid_prev)) {
  6944. ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
  6945. } else {
  6946. ahd_set_scbptr(ahd, tid_prev);
  6947. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6948. }
  6949. if (SCBID_IS_NULL(tid_next))
  6950. ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
  6951. } else {
  6952. /* Stitch through tid_cur */
  6953. if (SCBID_IS_NULL(tid_prev)) {
  6954. ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
  6955. } else {
  6956. ahd_set_scbptr(ahd, tid_prev);
  6957. ahd_outw(ahd, SCB_NEXT2, tid_cur);
  6958. }
  6959. ahd_set_scbptr(ahd, tid_cur);
  6960. ahd_outw(ahd, SCB_NEXT2, tid_next);
  6961. if (SCBID_IS_NULL(tid_next))
  6962. ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
  6963. }
  6964. }
  6965. /*
  6966. * Manipulate the waiting for selection list and return the
  6967. * scb that follows the one that we remove.
  6968. */
  6969. static u_int
  6970. ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
  6971. u_int prev, u_int next, u_int tid)
  6972. {
  6973. u_int tail_offset;
  6974. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  6975. if (!SCBID_IS_NULL(prev)) {
  6976. ahd_set_scbptr(ahd, prev);
  6977. ahd_outw(ahd, SCB_NEXT, next);
  6978. }
  6979. /*
  6980. * SCBs that have MK_MESSAGE set in them may
  6981. * cause the tail pointer to be updated without
  6982. * setting the next pointer of the previous tail.
  6983. * Only clear the tail if the removed SCB was
  6984. * the tail.
  6985. */
  6986. tail_offset = WAITING_SCB_TAILS + (2 * tid);
  6987. if (SCBID_IS_NULL(next)
  6988. && ahd_inw(ahd, tail_offset) == scbid)
  6989. ahd_outw(ahd, tail_offset, prev);
  6990. ahd_add_scb_to_free_list(ahd, scbid);
  6991. return (next);
  6992. }
  6993. /*
  6994. * Add the SCB as selected by SCBPTR onto the on chip list of
  6995. * free hardware SCBs. This list is empty/unused if we are not
  6996. * performing SCB paging.
  6997. */
  6998. static void
  6999. ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
  7000. {
  7001. /* XXX Need some other mechanism to designate "free". */
  7002. /*
  7003. * Invalidate the tag so that our abort
  7004. * routines don't think it's active.
  7005. ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
  7006. */
  7007. }
  7008. /******************************** Error Handling ******************************/
  7009. /*
  7010. * Abort all SCBs that match the given description (target/channel/lun/tag),
  7011. * setting their status to the passed in status if the status has not already
  7012. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  7013. * is paused before it is called.
  7014. */
  7015. int
  7016. ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
  7017. int lun, u_int tag, role_t role, uint32_t status)
  7018. {
  7019. struct scb *scbp;
  7020. struct scb *scbp_next;
  7021. u_int i, j;
  7022. u_int maxtarget;
  7023. u_int minlun;
  7024. u_int maxlun;
  7025. int found;
  7026. ahd_mode_state saved_modes;
  7027. /* restore this when we're done */
  7028. saved_modes = ahd_save_modes(ahd);
  7029. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7030. found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
  7031. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  7032. /*
  7033. * Clean out the busy target table for any untagged commands.
  7034. */
  7035. i = 0;
  7036. maxtarget = 16;
  7037. if (target != CAM_TARGET_WILDCARD) {
  7038. i = target;
  7039. if (channel == 'B')
  7040. i += 8;
  7041. maxtarget = i + 1;
  7042. }
  7043. if (lun == CAM_LUN_WILDCARD) {
  7044. minlun = 0;
  7045. maxlun = AHD_NUM_LUNS_NONPKT;
  7046. } else if (lun >= AHD_NUM_LUNS_NONPKT) {
  7047. minlun = maxlun = 0;
  7048. } else {
  7049. minlun = lun;
  7050. maxlun = lun + 1;
  7051. }
  7052. if (role != ROLE_TARGET) {
  7053. for (;i < maxtarget; i++) {
  7054. for (j = minlun;j < maxlun; j++) {
  7055. u_int scbid;
  7056. u_int tcl;
  7057. tcl = BUILD_TCL_RAW(i, 'A', j);
  7058. scbid = ahd_find_busy_tcl(ahd, tcl);
  7059. scbp = ahd_lookup_scb(ahd, scbid);
  7060. if (scbp == NULL
  7061. || ahd_match_scb(ahd, scbp, target, channel,
  7062. lun, tag, role) == 0)
  7063. continue;
  7064. ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
  7065. }
  7066. }
  7067. }
  7068. /*
  7069. * Don't abort commands that have already completed,
  7070. * but haven't quite made it up to the host yet.
  7071. */
  7072. ahd_flush_qoutfifo(ahd);
  7073. /*
  7074. * Go through the pending CCB list and look for
  7075. * commands for this target that are still active.
  7076. * These are other tagged commands that were
  7077. * disconnected when the reset occurred.
  7078. */
  7079. scbp_next = LIST_FIRST(&ahd->pending_scbs);
  7080. while (scbp_next != NULL) {
  7081. scbp = scbp_next;
  7082. scbp_next = LIST_NEXT(scbp, pending_links);
  7083. if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
  7084. cam_status ostat;
  7085. ostat = ahd_get_transaction_status(scbp);
  7086. if (ostat == CAM_REQ_INPROG)
  7087. ahd_set_transaction_status(scbp, status);
  7088. if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
  7089. ahd_freeze_scb(scbp);
  7090. if ((scbp->flags & SCB_ACTIVE) == 0)
  7091. printf("Inactive SCB on pending list\n");
  7092. ahd_done(ahd, scbp);
  7093. found++;
  7094. }
  7095. }
  7096. ahd_restore_modes(ahd, saved_modes);
  7097. ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
  7098. ahd->flags |= AHD_UPDATE_PEND_CMDS;
  7099. return found;
  7100. }
  7101. static void
  7102. ahd_reset_current_bus(struct ahd_softc *ahd)
  7103. {
  7104. uint8_t scsiseq;
  7105. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  7106. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
  7107. scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
  7108. ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
  7109. ahd_flush_device_writes(ahd);
  7110. ahd_delay(AHD_BUSRESET_DELAY);
  7111. /* Turn off the bus reset */
  7112. ahd_outb(ahd, SCSISEQ0, scsiseq);
  7113. ahd_flush_device_writes(ahd);
  7114. ahd_delay(AHD_BUSRESET_DELAY);
  7115. if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
  7116. /*
  7117. * 2A Razor #474
  7118. * Certain chip state is not cleared for
  7119. * SCSI bus resets that we initiate, so
  7120. * we must reset the chip.
  7121. */
  7122. ahd_reset(ahd, /*reinit*/TRUE);
  7123. ahd_intr_enable(ahd, /*enable*/TRUE);
  7124. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  7125. }
  7126. ahd_clear_intstat(ahd);
  7127. }
  7128. int
  7129. ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
  7130. {
  7131. struct ahd_devinfo devinfo;
  7132. u_int initiator;
  7133. u_int target;
  7134. u_int max_scsiid;
  7135. int found;
  7136. u_int fifo;
  7137. u_int next_fifo;
  7138. ahd->pending_device = NULL;
  7139. ahd_compile_devinfo(&devinfo,
  7140. CAM_TARGET_WILDCARD,
  7141. CAM_TARGET_WILDCARD,
  7142. CAM_LUN_WILDCARD,
  7143. channel, ROLE_UNKNOWN);
  7144. ahd_pause(ahd);
  7145. /* Make sure the sequencer is in a safe location. */
  7146. ahd_clear_critical_section(ahd);
  7147. #ifdef AHD_TARGET_MODE
  7148. if ((ahd->flags & AHD_TARGETROLE) != 0) {
  7149. ahd_run_tqinfifo(ahd, /*paused*/TRUE);
  7150. }
  7151. #endif
  7152. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7153. /*
  7154. * Disable selections so no automatic hardware
  7155. * functions will modify chip state.
  7156. */
  7157. ahd_outb(ahd, SCSISEQ0, 0);
  7158. ahd_outb(ahd, SCSISEQ1, 0);
  7159. /*
  7160. * Safely shut down our DMA engines. Always start with
  7161. * the FIFO that is not currently active (if any are
  7162. * actively connected).
  7163. */
  7164. next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
  7165. if (next_fifo > CURRFIFO_1)
  7166. /* If disconneced, arbitrarily start with FIFO1. */
  7167. next_fifo = fifo = 0;
  7168. do {
  7169. next_fifo ^= CURRFIFO_1;
  7170. ahd_set_modes(ahd, next_fifo, next_fifo);
  7171. ahd_outb(ahd, DFCNTRL,
  7172. ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
  7173. while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
  7174. ahd_delay(10);
  7175. /*
  7176. * Set CURRFIFO to the now inactive channel.
  7177. */
  7178. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7179. ahd_outb(ahd, DFFSTAT, next_fifo);
  7180. } while (next_fifo != fifo);
  7181. /*
  7182. * Reset the bus if we are initiating this reset
  7183. */
  7184. ahd_clear_msg_state(ahd);
  7185. ahd_outb(ahd, SIMODE1,
  7186. ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST));
  7187. if (initiate_reset)
  7188. ahd_reset_current_bus(ahd);
  7189. ahd_clear_intstat(ahd);
  7190. /*
  7191. * Clean up all the state information for the
  7192. * pending transactions on this bus.
  7193. */
  7194. found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
  7195. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  7196. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  7197. /*
  7198. * Cleanup anything left in the FIFOs.
  7199. */
  7200. ahd_clear_fifo(ahd, 0);
  7201. ahd_clear_fifo(ahd, 1);
  7202. /*
  7203. * Revert to async/narrow transfers until we renegotiate.
  7204. */
  7205. max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
  7206. for (target = 0; target <= max_scsiid; target++) {
  7207. if (ahd->enabled_targets[target] == NULL)
  7208. continue;
  7209. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  7210. struct ahd_devinfo devinfo;
  7211. ahd_compile_devinfo(&devinfo, target, initiator,
  7212. CAM_LUN_WILDCARD,
  7213. 'A', ROLE_UNKNOWN);
  7214. ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  7215. AHD_TRANS_CUR, /*paused*/TRUE);
  7216. ahd_set_syncrate(ahd, &devinfo, /*period*/0,
  7217. /*offset*/0, /*ppr_options*/0,
  7218. AHD_TRANS_CUR, /*paused*/TRUE);
  7219. }
  7220. }
  7221. #ifdef AHD_TARGET_MODE
  7222. max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
  7223. /*
  7224. * Send an immediate notify ccb to all target more peripheral
  7225. * drivers affected by this action.
  7226. */
  7227. for (target = 0; target <= max_scsiid; target++) {
  7228. struct ahd_tmode_tstate* tstate;
  7229. u_int lun;
  7230. tstate = ahd->enabled_targets[target];
  7231. if (tstate == NULL)
  7232. continue;
  7233. for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
  7234. struct ahd_tmode_lstate* lstate;
  7235. lstate = tstate->enabled_luns[lun];
  7236. if (lstate == NULL)
  7237. continue;
  7238. ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
  7239. EVENT_TYPE_BUS_RESET, /*arg*/0);
  7240. ahd_send_lstate_events(ahd, lstate);
  7241. }
  7242. }
  7243. #endif
  7244. /* Notify the XPT that a bus reset occurred */
  7245. ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
  7246. CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
  7247. ahd_restart(ahd);
  7248. /*
  7249. * Freeze the SIMQ until our poller can determine that
  7250. * the bus reset has really gone away. We set the initial
  7251. * timer to 0 to have the check performed as soon as possible
  7252. * from the timer context.
  7253. */
  7254. if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
  7255. ahd->flags |= AHD_RESET_POLL_ACTIVE;
  7256. ahd_freeze_simq(ahd);
  7257. ahd_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
  7258. }
  7259. return (found);
  7260. }
  7261. #define AHD_RESET_POLL_US 1000
  7262. static void
  7263. ahd_reset_poll(void *arg)
  7264. {
  7265. struct ahd_softc *ahd = arg;
  7266. u_int scsiseq1;
  7267. u_long s;
  7268. ahd_lock(ahd, &s);
  7269. ahd_pause(ahd);
  7270. ahd_update_modes(ahd);
  7271. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  7272. ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
  7273. if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
  7274. ahd_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
  7275. ahd_reset_poll, ahd);
  7276. ahd_unpause(ahd);
  7277. ahd_unlock(ahd, &s);
  7278. return;
  7279. }
  7280. /* Reset is now low. Complete chip reinitialization. */
  7281. ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
  7282. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  7283. ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
  7284. ahd_unpause(ahd);
  7285. ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
  7286. ahd_unlock(ahd, &s);
  7287. ahd_release_simq(ahd);
  7288. }
  7289. /**************************** Statistics Processing ***************************/
  7290. static void
  7291. ahd_stat_timer(void *arg)
  7292. {
  7293. struct ahd_softc *ahd = arg;
  7294. u_long s;
  7295. int enint_coal;
  7296. ahd_lock(ahd, &s);
  7297. enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
  7298. if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
  7299. enint_coal |= ENINT_COALESCE;
  7300. else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
  7301. enint_coal &= ~ENINT_COALESCE;
  7302. if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
  7303. ahd_enable_coalescing(ahd, enint_coal);
  7304. #ifdef AHD_DEBUG
  7305. if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
  7306. printf("%s: Interrupt coalescing "
  7307. "now %sabled. Cmds %d\n",
  7308. ahd_name(ahd),
  7309. (enint_coal & ENINT_COALESCE) ? "en" : "dis",
  7310. ahd->cmdcmplt_total);
  7311. #endif
  7312. }
  7313. ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
  7314. ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
  7315. ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
  7316. ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
  7317. ahd_stat_timer, ahd);
  7318. ahd_unlock(ahd, &s);
  7319. }
  7320. /****************************** Status Processing *****************************/
  7321. void
  7322. ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
  7323. {
  7324. if (scb->hscb->shared_data.istatus.scsi_status != 0) {
  7325. ahd_handle_scsi_status(ahd, scb);
  7326. } else {
  7327. ahd_calc_residual(ahd, scb);
  7328. ahd_done(ahd, scb);
  7329. }
  7330. }
  7331. void
  7332. ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
  7333. {
  7334. struct hardware_scb *hscb;
  7335. int paused;
  7336. /*
  7337. * The sequencer freezes its select-out queue
  7338. * anytime a SCSI status error occurs. We must
  7339. * handle the error and increment our qfreeze count
  7340. * to allow the sequencer to continue. We don't
  7341. * bother clearing critical sections here since all
  7342. * operations are on data structures that the sequencer
  7343. * is not touching once the queue is frozen.
  7344. */
  7345. hscb = scb->hscb;
  7346. if (ahd_is_paused(ahd)) {
  7347. paused = 1;
  7348. } else {
  7349. paused = 0;
  7350. ahd_pause(ahd);
  7351. }
  7352. /* Freeze the queue until the client sees the error. */
  7353. ahd_freeze_devq(ahd, scb);
  7354. ahd_freeze_scb(scb);
  7355. ahd->qfreeze_cnt++;
  7356. ahd_outw(ahd, KERNEL_QFREEZE_COUNT, ahd->qfreeze_cnt);
  7357. if (paused == 0)
  7358. ahd_unpause(ahd);
  7359. /* Don't want to clobber the original sense code */
  7360. if ((scb->flags & SCB_SENSE) != 0) {
  7361. /*
  7362. * Clear the SCB_SENSE Flag and perform
  7363. * a normal command completion.
  7364. */
  7365. scb->flags &= ~SCB_SENSE;
  7366. ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  7367. ahd_done(ahd, scb);
  7368. return;
  7369. }
  7370. ahd_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  7371. ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
  7372. switch (hscb->shared_data.istatus.scsi_status) {
  7373. case STATUS_PKT_SENSE:
  7374. {
  7375. struct scsi_status_iu_header *siu;
  7376. ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
  7377. siu = (struct scsi_status_iu_header *)scb->sense_data;
  7378. ahd_set_scsi_status(scb, siu->status);
  7379. #ifdef AHD_DEBUG
  7380. if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
  7381. ahd_print_path(ahd, scb);
  7382. printf("SCB 0x%x Received PKT Status of 0x%x\n",
  7383. SCB_GET_TAG(scb), siu->status);
  7384. printf("\tflags = 0x%x, sense len = 0x%x, "
  7385. "pktfail = 0x%x\n",
  7386. siu->flags, scsi_4btoul(siu->sense_length),
  7387. scsi_4btoul(siu->pkt_failures_length));
  7388. }
  7389. #endif
  7390. if ((siu->flags & SIU_RSPVALID) != 0) {
  7391. ahd_print_path(ahd, scb);
  7392. if (scsi_4btoul(siu->pkt_failures_length) < 4) {
  7393. printf("Unable to parse pkt_failures\n");
  7394. } else {
  7395. switch (SIU_PKTFAIL_CODE(siu)) {
  7396. case SIU_PFC_NONE:
  7397. printf("No packet failure found\n");
  7398. break;
  7399. case SIU_PFC_CIU_FIELDS_INVALID:
  7400. printf("Invalid Command IU Field\n");
  7401. break;
  7402. case SIU_PFC_TMF_NOT_SUPPORTED:
  7403. printf("TMF not supportd\n");
  7404. break;
  7405. case SIU_PFC_TMF_FAILED:
  7406. printf("TMF failed\n");
  7407. break;
  7408. case SIU_PFC_INVALID_TYPE_CODE:
  7409. printf("Invalid L_Q Type code\n");
  7410. break;
  7411. case SIU_PFC_ILLEGAL_REQUEST:
  7412. printf("Illegal request\n");
  7413. default:
  7414. break;
  7415. }
  7416. }
  7417. if (siu->status == SCSI_STATUS_OK)
  7418. ahd_set_transaction_status(scb,
  7419. CAM_REQ_CMP_ERR);
  7420. }
  7421. if ((siu->flags & SIU_SNSVALID) != 0) {
  7422. scb->flags |= SCB_PKT_SENSE;
  7423. #ifdef AHD_DEBUG
  7424. if ((ahd_debug & AHD_SHOW_SENSE) != 0)
  7425. printf("Sense data available\n");
  7426. #endif
  7427. }
  7428. ahd_done(ahd, scb);
  7429. break;
  7430. }
  7431. case SCSI_STATUS_CMD_TERMINATED:
  7432. case SCSI_STATUS_CHECK_COND:
  7433. {
  7434. struct ahd_devinfo devinfo;
  7435. struct ahd_dma_seg *sg;
  7436. struct scsi_sense *sc;
  7437. struct ahd_initiator_tinfo *targ_info;
  7438. struct ahd_tmode_tstate *tstate;
  7439. struct ahd_transinfo *tinfo;
  7440. #ifdef AHD_DEBUG
  7441. if (ahd_debug & AHD_SHOW_SENSE) {
  7442. ahd_print_path(ahd, scb);
  7443. printf("SCB %d: requests Check Status\n",
  7444. SCB_GET_TAG(scb));
  7445. }
  7446. #endif
  7447. if (ahd_perform_autosense(scb) == 0)
  7448. break;
  7449. ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
  7450. SCB_GET_TARGET(ahd, scb),
  7451. SCB_GET_LUN(scb),
  7452. SCB_GET_CHANNEL(ahd, scb),
  7453. ROLE_INITIATOR);
  7454. targ_info = ahd_fetch_transinfo(ahd,
  7455. devinfo.channel,
  7456. devinfo.our_scsiid,
  7457. devinfo.target,
  7458. &tstate);
  7459. tinfo = &targ_info->curr;
  7460. sg = scb->sg_list;
  7461. sc = (struct scsi_sense *)hscb->shared_data.idata.cdb;
  7462. /*
  7463. * Save off the residual if there is one.
  7464. */
  7465. ahd_update_residual(ahd, scb);
  7466. #ifdef AHD_DEBUG
  7467. if (ahd_debug & AHD_SHOW_SENSE) {
  7468. ahd_print_path(ahd, scb);
  7469. printf("Sending Sense\n");
  7470. }
  7471. #endif
  7472. scb->sg_count = 0;
  7473. sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
  7474. ahd_get_sense_bufsize(ahd, scb),
  7475. /*last*/TRUE);
  7476. sc->opcode = REQUEST_SENSE;
  7477. sc->byte2 = 0;
  7478. if (tinfo->protocol_version <= SCSI_REV_2
  7479. && SCB_GET_LUN(scb) < 8)
  7480. sc->byte2 = SCB_GET_LUN(scb) << 5;
  7481. sc->unused[0] = 0;
  7482. sc->unused[1] = 0;
  7483. sc->length = ahd_get_sense_bufsize(ahd, scb);
  7484. sc->control = 0;
  7485. /*
  7486. * We can't allow the target to disconnect.
  7487. * This will be an untagged transaction and
  7488. * having the target disconnect will make this
  7489. * transaction indestinguishable from outstanding
  7490. * tagged transactions.
  7491. */
  7492. hscb->control = 0;
  7493. /*
  7494. * This request sense could be because the
  7495. * the device lost power or in some other
  7496. * way has lost our transfer negotiations.
  7497. * Renegotiate if appropriate. Unit attention
  7498. * errors will be reported before any data
  7499. * phases occur.
  7500. */
  7501. if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
  7502. ahd_update_neg_request(ahd, &devinfo,
  7503. tstate, targ_info,
  7504. AHD_NEG_IF_NON_ASYNC);
  7505. }
  7506. if (tstate->auto_negotiate & devinfo.target_mask) {
  7507. hscb->control |= MK_MESSAGE;
  7508. scb->flags &=
  7509. ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
  7510. scb->flags |= SCB_AUTO_NEGOTIATE;
  7511. }
  7512. hscb->cdb_len = sizeof(*sc);
  7513. ahd_setup_data_scb(ahd, scb);
  7514. scb->flags |= SCB_SENSE;
  7515. ahd_queue_scb(ahd, scb);
  7516. break;
  7517. }
  7518. case SCSI_STATUS_OK:
  7519. printf("%s: Interrupted for staus of 0???\n",
  7520. ahd_name(ahd));
  7521. /* FALLTHROUGH */
  7522. default:
  7523. ahd_done(ahd, scb);
  7524. break;
  7525. }
  7526. }
  7527. /*
  7528. * Calculate the residual for a just completed SCB.
  7529. */
  7530. void
  7531. ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
  7532. {
  7533. struct hardware_scb *hscb;
  7534. struct initiator_status *spkt;
  7535. uint32_t sgptr;
  7536. uint32_t resid_sgptr;
  7537. uint32_t resid;
  7538. /*
  7539. * 5 cases.
  7540. * 1) No residual.
  7541. * SG_STATUS_VALID clear in sgptr.
  7542. * 2) Transferless command
  7543. * 3) Never performed any transfers.
  7544. * sgptr has SG_FULL_RESID set.
  7545. * 4) No residual but target did not
  7546. * save data pointers after the
  7547. * last transfer, so sgptr was
  7548. * never updated.
  7549. * 5) We have a partial residual.
  7550. * Use residual_sgptr to determine
  7551. * where we are.
  7552. */
  7553. hscb = scb->hscb;
  7554. sgptr = ahd_le32toh(hscb->sgptr);
  7555. if ((sgptr & SG_STATUS_VALID) == 0)
  7556. /* Case 1 */
  7557. return;
  7558. sgptr &= ~SG_STATUS_VALID;
  7559. if ((sgptr & SG_LIST_NULL) != 0)
  7560. /* Case 2 */
  7561. return;
  7562. /*
  7563. * Residual fields are the same in both
  7564. * target and initiator status packets,
  7565. * so we can always use the initiator fields
  7566. * regardless of the role for this SCB.
  7567. */
  7568. spkt = &hscb->shared_data.istatus;
  7569. resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
  7570. if ((sgptr & SG_FULL_RESID) != 0) {
  7571. /* Case 3 */
  7572. resid = ahd_get_transfer_length(scb);
  7573. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  7574. /* Case 4 */
  7575. return;
  7576. } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
  7577. ahd_print_path(ahd, scb);
  7578. printf("data overrun detected Tag == 0x%x.\n",
  7579. SCB_GET_TAG(scb));
  7580. ahd_freeze_devq(ahd, scb);
  7581. ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  7582. ahd_freeze_scb(scb);
  7583. return;
  7584. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  7585. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  7586. /* NOTREACHED */
  7587. } else {
  7588. struct ahd_dma_seg *sg;
  7589. /*
  7590. * Remainder of the SG where the transfer
  7591. * stopped.
  7592. */
  7593. resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
  7594. sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
  7595. /* The residual sg_ptr always points to the next sg */
  7596. sg--;
  7597. /*
  7598. * Add up the contents of all residual
  7599. * SG segments that are after the SG where
  7600. * the transfer stopped.
  7601. */
  7602. while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
  7603. sg++;
  7604. resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
  7605. }
  7606. }
  7607. if ((scb->flags & SCB_SENSE) == 0)
  7608. ahd_set_residual(scb, resid);
  7609. else
  7610. ahd_set_sense_residual(scb, resid);
  7611. #ifdef AHD_DEBUG
  7612. if ((ahd_debug & AHD_SHOW_MISC) != 0) {
  7613. ahd_print_path(ahd, scb);
  7614. printf("Handled %sResidual of %d bytes\n",
  7615. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  7616. }
  7617. #endif
  7618. }
  7619. /******************************* Target Mode **********************************/
  7620. #ifdef AHD_TARGET_MODE
  7621. /*
  7622. * Add a target mode event to this lun's queue
  7623. */
  7624. static void
  7625. ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
  7626. u_int initiator_id, u_int event_type, u_int event_arg)
  7627. {
  7628. struct ahd_tmode_event *event;
  7629. int pending;
  7630. xpt_freeze_devq(lstate->path, /*count*/1);
  7631. if (lstate->event_w_idx >= lstate->event_r_idx)
  7632. pending = lstate->event_w_idx - lstate->event_r_idx;
  7633. else
  7634. pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
  7635. - (lstate->event_r_idx - lstate->event_w_idx);
  7636. if (event_type == EVENT_TYPE_BUS_RESET
  7637. || event_type == MSG_BUS_DEV_RESET) {
  7638. /*
  7639. * Any earlier events are irrelevant, so reset our buffer.
  7640. * This has the effect of allowing us to deal with reset
  7641. * floods (an external device holding down the reset line)
  7642. * without losing the event that is really interesting.
  7643. */
  7644. lstate->event_r_idx = 0;
  7645. lstate->event_w_idx = 0;
  7646. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  7647. }
  7648. if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
  7649. xpt_print_path(lstate->path);
  7650. printf("immediate event %x:%x lost\n",
  7651. lstate->event_buffer[lstate->event_r_idx].event_type,
  7652. lstate->event_buffer[lstate->event_r_idx].event_arg);
  7653. lstate->event_r_idx++;
  7654. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7655. lstate->event_r_idx = 0;
  7656. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  7657. }
  7658. event = &lstate->event_buffer[lstate->event_w_idx];
  7659. event->initiator_id = initiator_id;
  7660. event->event_type = event_type;
  7661. event->event_arg = event_arg;
  7662. lstate->event_w_idx++;
  7663. if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7664. lstate->event_w_idx = 0;
  7665. }
  7666. /*
  7667. * Send any target mode events queued up waiting
  7668. * for immediate notify resources.
  7669. */
  7670. void
  7671. ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
  7672. {
  7673. struct ccb_hdr *ccbh;
  7674. struct ccb_immed_notify *inot;
  7675. while (lstate->event_r_idx != lstate->event_w_idx
  7676. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  7677. struct ahd_tmode_event *event;
  7678. event = &lstate->event_buffer[lstate->event_r_idx];
  7679. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  7680. inot = (struct ccb_immed_notify *)ccbh;
  7681. switch (event->event_type) {
  7682. case EVENT_TYPE_BUS_RESET:
  7683. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  7684. break;
  7685. default:
  7686. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  7687. inot->message_args[0] = event->event_type;
  7688. inot->message_args[1] = event->event_arg;
  7689. break;
  7690. }
  7691. inot->initiator_id = event->initiator_id;
  7692. inot->sense_len = 0;
  7693. xpt_done((union ccb *)inot);
  7694. lstate->event_r_idx++;
  7695. if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
  7696. lstate->event_r_idx = 0;
  7697. }
  7698. }
  7699. #endif
  7700. /******************** Sequencer Program Patching/Download *********************/
  7701. #ifdef AHD_DUMP_SEQ
  7702. void
  7703. ahd_dumpseq(struct ahd_softc* ahd)
  7704. {
  7705. int i;
  7706. int max_prog;
  7707. max_prog = 2048;
  7708. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7709. ahd_outw(ahd, PRGMCNT, 0);
  7710. for (i = 0; i < max_prog; i++) {
  7711. uint8_t ins_bytes[4];
  7712. ahd_insb(ahd, SEQRAM, ins_bytes, 4);
  7713. printf("0x%08x\n", ins_bytes[0] << 24
  7714. | ins_bytes[1] << 16
  7715. | ins_bytes[2] << 8
  7716. | ins_bytes[3]);
  7717. }
  7718. }
  7719. #endif
  7720. static void
  7721. ahd_loadseq(struct ahd_softc *ahd)
  7722. {
  7723. struct cs cs_table[num_critical_sections];
  7724. u_int begin_set[num_critical_sections];
  7725. u_int end_set[num_critical_sections];
  7726. struct patch *cur_patch;
  7727. u_int cs_count;
  7728. u_int cur_cs;
  7729. u_int i;
  7730. int downloaded;
  7731. u_int skip_addr;
  7732. u_int sg_prefetch_cnt;
  7733. u_int sg_prefetch_cnt_limit;
  7734. u_int sg_prefetch_align;
  7735. u_int sg_size;
  7736. u_int cacheline_mask;
  7737. uint8_t download_consts[DOWNLOAD_CONST_COUNT];
  7738. if (bootverbose)
  7739. printf("%s: Downloading Sequencer Program...",
  7740. ahd_name(ahd));
  7741. #if DOWNLOAD_CONST_COUNT != 8
  7742. #error "Download Const Mismatch"
  7743. #endif
  7744. /*
  7745. * Start out with 0 critical sections
  7746. * that apply to this firmware load.
  7747. */
  7748. cs_count = 0;
  7749. cur_cs = 0;
  7750. memset(begin_set, 0, sizeof(begin_set));
  7751. memset(end_set, 0, sizeof(end_set));
  7752. /*
  7753. * Setup downloadable constant table.
  7754. *
  7755. * The computation for the S/G prefetch variables is
  7756. * a bit complicated. We would like to always fetch
  7757. * in terms of cachelined sized increments. However,
  7758. * if the cacheline is not an even multiple of the
  7759. * SG element size or is larger than our SG RAM, using
  7760. * just the cache size might leave us with only a portion
  7761. * of an SG element at the tail of a prefetch. If the
  7762. * cacheline is larger than our S/G prefetch buffer less
  7763. * the size of an SG element, we may round down to a cacheline
  7764. * that doesn't contain any or all of the S/G of interest
  7765. * within the bounds of our S/G ram. Provide variables to
  7766. * the sequencer that will allow it to handle these edge
  7767. * cases.
  7768. */
  7769. /* Start by aligning to the nearest cacheline. */
  7770. sg_prefetch_align = ahd->pci_cachesize;
  7771. if (sg_prefetch_align == 0)
  7772. sg_prefetch_align = 8;
  7773. /* Round down to the nearest power of 2. */
  7774. while (powerof2(sg_prefetch_align) == 0)
  7775. sg_prefetch_align--;
  7776. cacheline_mask = sg_prefetch_align - 1;
  7777. /*
  7778. * If the cacheline boundary is greater than half our prefetch RAM
  7779. * we risk not being able to fetch even a single complete S/G
  7780. * segment if we align to that boundary.
  7781. */
  7782. if (sg_prefetch_align > CCSGADDR_MAX/2)
  7783. sg_prefetch_align = CCSGADDR_MAX/2;
  7784. /* Start by fetching a single cacheline. */
  7785. sg_prefetch_cnt = sg_prefetch_align;
  7786. /*
  7787. * Increment the prefetch count by cachelines until
  7788. * at least one S/G element will fit.
  7789. */
  7790. sg_size = sizeof(struct ahd_dma_seg);
  7791. if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
  7792. sg_size = sizeof(struct ahd_dma64_seg);
  7793. while (sg_prefetch_cnt < sg_size)
  7794. sg_prefetch_cnt += sg_prefetch_align;
  7795. /*
  7796. * If the cacheline is not an even multiple of
  7797. * the S/G size, we may only get a partial S/G when
  7798. * we align. Add a cacheline if this is the case.
  7799. */
  7800. if ((sg_prefetch_align % sg_size) != 0
  7801. && (sg_prefetch_cnt < CCSGADDR_MAX))
  7802. sg_prefetch_cnt += sg_prefetch_align;
  7803. /*
  7804. * Lastly, compute a value that the sequencer can use
  7805. * to determine if the remainder of the CCSGRAM buffer
  7806. * has a full S/G element in it.
  7807. */
  7808. sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
  7809. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  7810. download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
  7811. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
  7812. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
  7813. download_consts[SG_SIZEOF] = sg_size;
  7814. download_consts[PKT_OVERRUN_BUFOFFSET] =
  7815. (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
  7816. download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
  7817. download_consts[CACHELINE_MASK] = cacheline_mask;
  7818. cur_patch = patches;
  7819. downloaded = 0;
  7820. skip_addr = 0;
  7821. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  7822. ahd_outw(ahd, PRGMCNT, 0);
  7823. for (i = 0; i < sizeof(seqprog)/4; i++) {
  7824. if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
  7825. /*
  7826. * Don't download this instruction as it
  7827. * is in a patch that was removed.
  7828. */
  7829. continue;
  7830. }
  7831. /*
  7832. * Move through the CS table until we find a CS
  7833. * that might apply to this instruction.
  7834. */
  7835. for (; cur_cs < num_critical_sections; cur_cs++) {
  7836. if (critical_sections[cur_cs].end <= i) {
  7837. if (begin_set[cs_count] == TRUE
  7838. && end_set[cs_count] == FALSE) {
  7839. cs_table[cs_count].end = downloaded;
  7840. end_set[cs_count] = TRUE;
  7841. cs_count++;
  7842. }
  7843. continue;
  7844. }
  7845. if (critical_sections[cur_cs].begin <= i
  7846. && begin_set[cs_count] == FALSE) {
  7847. cs_table[cs_count].begin = downloaded;
  7848. begin_set[cs_count] = TRUE;
  7849. }
  7850. break;
  7851. }
  7852. ahd_download_instr(ahd, i, download_consts);
  7853. downloaded++;
  7854. }
  7855. ahd->num_critical_sections = cs_count;
  7856. if (cs_count != 0) {
  7857. cs_count *= sizeof(struct cs);
  7858. ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  7859. if (ahd->critical_sections == NULL)
  7860. panic("ahd_loadseq: Could not malloc");
  7861. memcpy(ahd->critical_sections, cs_table, cs_count);
  7862. }
  7863. ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
  7864. if (bootverbose) {
  7865. printf(" %d instructions downloaded\n", downloaded);
  7866. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  7867. ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
  7868. }
  7869. }
  7870. static int
  7871. ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
  7872. u_int start_instr, u_int *skip_addr)
  7873. {
  7874. struct patch *cur_patch;
  7875. struct patch *last_patch;
  7876. u_int num_patches;
  7877. num_patches = sizeof(patches)/sizeof(struct patch);
  7878. last_patch = &patches[num_patches];
  7879. cur_patch = *start_patch;
  7880. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  7881. if (cur_patch->patch_func(ahd) == 0) {
  7882. /* Start rejecting code */
  7883. *skip_addr = start_instr + cur_patch->skip_instr;
  7884. cur_patch += cur_patch->skip_patch;
  7885. } else {
  7886. /* Accepted this patch. Advance to the next
  7887. * one and wait for our intruction pointer to
  7888. * hit this point.
  7889. */
  7890. cur_patch++;
  7891. }
  7892. }
  7893. *start_patch = cur_patch;
  7894. if (start_instr < *skip_addr)
  7895. /* Still skipping */
  7896. return (0);
  7897. return (1);
  7898. }
  7899. static u_int
  7900. ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
  7901. {
  7902. struct patch *cur_patch;
  7903. int address_offset;
  7904. u_int skip_addr;
  7905. u_int i;
  7906. address_offset = 0;
  7907. cur_patch = patches;
  7908. skip_addr = 0;
  7909. for (i = 0; i < address;) {
  7910. ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
  7911. if (skip_addr > i) {
  7912. int end_addr;
  7913. end_addr = MIN(address, skip_addr);
  7914. address_offset += end_addr - i;
  7915. i = skip_addr;
  7916. } else {
  7917. i++;
  7918. }
  7919. }
  7920. return (address - address_offset);
  7921. }
  7922. static void
  7923. ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
  7924. {
  7925. union ins_formats instr;
  7926. struct ins_format1 *fmt1_ins;
  7927. struct ins_format3 *fmt3_ins;
  7928. u_int opcode;
  7929. /*
  7930. * The firmware is always compiled into a little endian format.
  7931. */
  7932. instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  7933. fmt1_ins = &instr.format1;
  7934. fmt3_ins = NULL;
  7935. /* Pull the opcode */
  7936. opcode = instr.format1.opcode;
  7937. switch (opcode) {
  7938. case AIC_OP_JMP:
  7939. case AIC_OP_JC:
  7940. case AIC_OP_JNC:
  7941. case AIC_OP_CALL:
  7942. case AIC_OP_JNE:
  7943. case AIC_OP_JNZ:
  7944. case AIC_OP_JE:
  7945. case AIC_OP_JZ:
  7946. {
  7947. fmt3_ins = &instr.format3;
  7948. fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
  7949. /* FALLTHROUGH */
  7950. }
  7951. case AIC_OP_OR:
  7952. case AIC_OP_AND:
  7953. case AIC_OP_XOR:
  7954. case AIC_OP_ADD:
  7955. case AIC_OP_ADC:
  7956. case AIC_OP_BMOV:
  7957. if (fmt1_ins->parity != 0) {
  7958. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  7959. }
  7960. fmt1_ins->parity = 0;
  7961. /* FALLTHROUGH */
  7962. case AIC_OP_ROL:
  7963. {
  7964. int i, count;
  7965. /* Calculate odd parity for the instruction */
  7966. for (i = 0, count = 0; i < 31; i++) {
  7967. uint32_t mask;
  7968. mask = 0x01 << i;
  7969. if ((instr.integer & mask) != 0)
  7970. count++;
  7971. }
  7972. if ((count & 0x01) == 0)
  7973. instr.format1.parity = 1;
  7974. /* The sequencer is a little endian cpu */
  7975. instr.integer = ahd_htole32(instr.integer);
  7976. ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
  7977. break;
  7978. }
  7979. default:
  7980. panic("Unknown opcode encountered in seq program");
  7981. break;
  7982. }
  7983. }
  7984. static int
  7985. ahd_probe_stack_size(struct ahd_softc *ahd)
  7986. {
  7987. int last_probe;
  7988. last_probe = 0;
  7989. while (1) {
  7990. int i;
  7991. /*
  7992. * We avoid using 0 as a pattern to avoid
  7993. * confusion if the stack implementation
  7994. * "back-fills" with zeros when "poping'
  7995. * entries.
  7996. */
  7997. for (i = 1; i <= last_probe+1; i++) {
  7998. ahd_outb(ahd, STACK, i & 0xFF);
  7999. ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
  8000. }
  8001. /* Verify */
  8002. for (i = last_probe+1; i > 0; i--) {
  8003. u_int stack_entry;
  8004. stack_entry = ahd_inb(ahd, STACK)
  8005. |(ahd_inb(ahd, STACK) << 8);
  8006. if (stack_entry != i)
  8007. goto sized;
  8008. }
  8009. last_probe++;
  8010. }
  8011. sized:
  8012. return (last_probe);
  8013. }
  8014. int
  8015. ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
  8016. const char *name, u_int address, u_int value,
  8017. u_int *cur_column, u_int wrap_point)
  8018. {
  8019. int printed;
  8020. u_int printed_mask;
  8021. if (cur_column != NULL && *cur_column >= wrap_point) {
  8022. printf("\n");
  8023. *cur_column = 0;
  8024. }
  8025. printed = printf("%s[0x%x]", name, value);
  8026. if (table == NULL) {
  8027. printed += printf(" ");
  8028. *cur_column += printed;
  8029. return (printed);
  8030. }
  8031. printed_mask = 0;
  8032. while (printed_mask != 0xFF) {
  8033. int entry;
  8034. for (entry = 0; entry < num_entries; entry++) {
  8035. if (((value & table[entry].mask)
  8036. != table[entry].value)
  8037. || ((printed_mask & table[entry].mask)
  8038. == table[entry].mask))
  8039. continue;
  8040. printed += printf("%s%s",
  8041. printed_mask == 0 ? ":(" : "|",
  8042. table[entry].name);
  8043. printed_mask |= table[entry].mask;
  8044. break;
  8045. }
  8046. if (entry >= num_entries)
  8047. break;
  8048. }
  8049. if (printed_mask != 0)
  8050. printed += printf(") ");
  8051. else
  8052. printed += printf(" ");
  8053. if (cur_column != NULL)
  8054. *cur_column += printed;
  8055. return (printed);
  8056. }
  8057. void
  8058. ahd_dump_card_state(struct ahd_softc *ahd)
  8059. {
  8060. struct scb *scb;
  8061. ahd_mode_state saved_modes;
  8062. u_int dffstat;
  8063. int paused;
  8064. u_int scb_index;
  8065. u_int saved_scb_index;
  8066. u_int cur_col;
  8067. int i;
  8068. if (ahd_is_paused(ahd)) {
  8069. paused = 1;
  8070. } else {
  8071. paused = 0;
  8072. ahd_pause(ahd);
  8073. }
  8074. saved_modes = ahd_save_modes(ahd);
  8075. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  8076. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  8077. "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
  8078. ahd_name(ahd),
  8079. ahd_inw(ahd, CURADDR),
  8080. ahd_build_mode_state(ahd, ahd->saved_src_mode,
  8081. ahd->saved_dst_mode));
  8082. if (paused)
  8083. printf("Card was paused\n");
  8084. if (ahd_check_cmdcmpltqueues(ahd))
  8085. printf("Completions are pending\n");
  8086. /*
  8087. * Mode independent registers.
  8088. */
  8089. cur_col = 0;
  8090. ahd_intstat_print(ahd_inb(ahd, INTSTAT), &cur_col, 50);
  8091. ahd_seloid_print(ahd_inb(ahd, SELOID), &cur_col, 50);
  8092. ahd_selid_print(ahd_inb(ahd, SELID), &cur_col, 50);
  8093. ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
  8094. ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
  8095. ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
  8096. ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
  8097. ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
  8098. ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
  8099. ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
  8100. ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
  8101. ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
  8102. ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
  8103. ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
  8104. ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
  8105. ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
  8106. ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
  8107. ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
  8108. ahd_qfreeze_count_print(ahd_inw(ahd, QFREEZE_COUNT), &cur_col, 50);
  8109. ahd_kernel_qfreeze_count_print(ahd_inw(ahd, KERNEL_QFREEZE_COUNT),
  8110. &cur_col, 50);
  8111. ahd_mk_message_scb_print(ahd_inw(ahd, MK_MESSAGE_SCB), &cur_col, 50);
  8112. ahd_mk_message_scsiid_print(ahd_inb(ahd, MK_MESSAGE_SCSIID),
  8113. &cur_col, 50);
  8114. ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
  8115. ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
  8116. ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
  8117. ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
  8118. ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
  8119. ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
  8120. ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
  8121. ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
  8122. ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
  8123. ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
  8124. ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
  8125. ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
  8126. printf("\n");
  8127. printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
  8128. "CURRSCB 0x%x NEXTSCB 0x%x\n",
  8129. ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
  8130. ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
  8131. ahd_inw(ahd, NEXTSCB));
  8132. cur_col = 0;
  8133. /* QINFIFO */
  8134. ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  8135. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  8136. ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
  8137. saved_scb_index = ahd_get_scbptr(ahd);
  8138. printf("Pending list:");
  8139. i = 0;
  8140. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  8141. if (i++ > AHD_SCB_MAX)
  8142. break;
  8143. cur_col = printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
  8144. ahd_inb_scbram(ahd, SCB_FIFO_USE_COUNT));
  8145. ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
  8146. ahd_scb_control_print(ahd_inb_scbram(ahd, SCB_CONTROL),
  8147. &cur_col, 60);
  8148. ahd_scb_scsiid_print(ahd_inb_scbram(ahd, SCB_SCSIID),
  8149. &cur_col, 60);
  8150. }
  8151. printf("\nTotal %d\n", i);
  8152. printf("Kernel Free SCB list: ");
  8153. i = 0;
  8154. TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
  8155. struct scb *list_scb;
  8156. list_scb = scb;
  8157. do {
  8158. printf("%d ", SCB_GET_TAG(list_scb));
  8159. list_scb = LIST_NEXT(list_scb, collision_links);
  8160. } while (list_scb && i++ < AHD_SCB_MAX);
  8161. }
  8162. LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
  8163. if (i++ > AHD_SCB_MAX)
  8164. break;
  8165. printf("%d ", SCB_GET_TAG(scb));
  8166. }
  8167. printf("\n");
  8168. printf("Sequencer Complete DMA-inprog list: ");
  8169. scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
  8170. i = 0;
  8171. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8172. ahd_set_scbptr(ahd, scb_index);
  8173. printf("%d ", scb_index);
  8174. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8175. }
  8176. printf("\n");
  8177. printf("Sequencer Complete list: ");
  8178. scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
  8179. i = 0;
  8180. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8181. ahd_set_scbptr(ahd, scb_index);
  8182. printf("%d ", scb_index);
  8183. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8184. }
  8185. printf("\n");
  8186. printf("Sequencer DMA-Up and Complete list: ");
  8187. scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
  8188. i = 0;
  8189. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8190. ahd_set_scbptr(ahd, scb_index);
  8191. printf("%d ", scb_index);
  8192. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8193. }
  8194. printf("\n");
  8195. printf("Sequencer On QFreeze and Complete list: ");
  8196. scb_index = ahd_inw(ahd, COMPLETE_ON_QFREEZE_HEAD);
  8197. i = 0;
  8198. while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
  8199. ahd_set_scbptr(ahd, scb_index);
  8200. printf("%d ", scb_index);
  8201. scb_index = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
  8202. }
  8203. printf("\n");
  8204. ahd_set_scbptr(ahd, saved_scb_index);
  8205. dffstat = ahd_inb(ahd, DFFSTAT);
  8206. for (i = 0; i < 2; i++) {
  8207. #ifdef AHD_DEBUG
  8208. struct scb *fifo_scb;
  8209. #endif
  8210. u_int fifo_scbptr;
  8211. ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
  8212. fifo_scbptr = ahd_get_scbptr(ahd);
  8213. printf("\n\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
  8214. ahd_name(ahd), i,
  8215. (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
  8216. ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
  8217. cur_col = 0;
  8218. ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
  8219. ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
  8220. ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
  8221. ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
  8222. ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
  8223. &cur_col, 50);
  8224. ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
  8225. ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
  8226. ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
  8227. ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
  8228. if (cur_col > 50) {
  8229. printf("\n");
  8230. cur_col = 0;
  8231. }
  8232. cur_col += printf("SHADDR = 0x%x%x, SHCNT = 0x%x ",
  8233. ahd_inl(ahd, SHADDR+4),
  8234. ahd_inl(ahd, SHADDR),
  8235. (ahd_inb(ahd, SHCNT)
  8236. | (ahd_inb(ahd, SHCNT + 1) << 8)
  8237. | (ahd_inb(ahd, SHCNT + 2) << 16)));
  8238. if (cur_col > 50) {
  8239. printf("\n");
  8240. cur_col = 0;
  8241. }
  8242. cur_col += printf("HADDR = 0x%x%x, HCNT = 0x%x ",
  8243. ahd_inl(ahd, HADDR+4),
  8244. ahd_inl(ahd, HADDR),
  8245. (ahd_inb(ahd, HCNT)
  8246. | (ahd_inb(ahd, HCNT + 1) << 8)
  8247. | (ahd_inb(ahd, HCNT + 2) << 16)));
  8248. ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
  8249. #ifdef AHD_DEBUG
  8250. if ((ahd_debug & AHD_SHOW_SG) != 0) {
  8251. fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
  8252. if (fifo_scb != NULL)
  8253. ahd_dump_sglist(fifo_scb);
  8254. }
  8255. #endif
  8256. }
  8257. printf("\nLQIN: ");
  8258. for (i = 0; i < 20; i++)
  8259. printf("0x%x ", ahd_inb(ahd, LQIN + i));
  8260. printf("\n");
  8261. ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
  8262. printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
  8263. ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
  8264. ahd_inb(ahd, OPTIONMODE));
  8265. printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
  8266. ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
  8267. ahd_inb(ahd, MAXCMDCNT));
  8268. printf("%s: SAVED_SCSIID = 0x%x SAVED_LUN = 0x%x\n",
  8269. ahd_name(ahd), ahd_inb(ahd, SAVED_SCSIID),
  8270. ahd_inb(ahd, SAVED_LUN));
  8271. ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
  8272. printf("\n");
  8273. ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
  8274. cur_col = 0;
  8275. ahd_ccscbctl_print(ahd_inb(ahd, CCSCBCTL), &cur_col, 50);
  8276. printf("\n");
  8277. ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
  8278. printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
  8279. ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
  8280. ahd_inw(ahd, DINDEX));
  8281. printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
  8282. ahd_name(ahd), ahd_get_scbptr(ahd),
  8283. ahd_inw_scbram(ahd, SCB_NEXT),
  8284. ahd_inw_scbram(ahd, SCB_NEXT2));
  8285. printf("CDB %x %x %x %x %x %x\n",
  8286. ahd_inb_scbram(ahd, SCB_CDB_STORE),
  8287. ahd_inb_scbram(ahd, SCB_CDB_STORE+1),
  8288. ahd_inb_scbram(ahd, SCB_CDB_STORE+2),
  8289. ahd_inb_scbram(ahd, SCB_CDB_STORE+3),
  8290. ahd_inb_scbram(ahd, SCB_CDB_STORE+4),
  8291. ahd_inb_scbram(ahd, SCB_CDB_STORE+5));
  8292. printf("STACK:");
  8293. for (i = 0; i < ahd->stack_size; i++) {
  8294. ahd->saved_stack[i] =
  8295. ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
  8296. printf(" 0x%x", ahd->saved_stack[i]);
  8297. }
  8298. for (i = ahd->stack_size-1; i >= 0; i--) {
  8299. ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
  8300. ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
  8301. }
  8302. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  8303. ahd_restore_modes(ahd, saved_modes);
  8304. if (paused == 0)
  8305. ahd_unpause(ahd);
  8306. }
  8307. void
  8308. ahd_dump_scbs(struct ahd_softc *ahd)
  8309. {
  8310. ahd_mode_state saved_modes;
  8311. u_int saved_scb_index;
  8312. int i;
  8313. saved_modes = ahd_save_modes(ahd);
  8314. ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
  8315. saved_scb_index = ahd_get_scbptr(ahd);
  8316. for (i = 0; i < AHD_SCB_MAX; i++) {
  8317. ahd_set_scbptr(ahd, i);
  8318. printf("%3d", i);
  8319. printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
  8320. ahd_inb_scbram(ahd, SCB_CONTROL),
  8321. ahd_inb_scbram(ahd, SCB_SCSIID),
  8322. ahd_inw_scbram(ahd, SCB_NEXT),
  8323. ahd_inw_scbram(ahd, SCB_NEXT2),
  8324. ahd_inl_scbram(ahd, SCB_SGPTR),
  8325. ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR));
  8326. }
  8327. printf("\n");
  8328. ahd_set_scbptr(ahd, saved_scb_index);
  8329. ahd_restore_modes(ahd, saved_modes);
  8330. }
  8331. /**************************** Flexport Logic **********************************/
  8332. /*
  8333. * Read count 16bit words from 16bit word address start_addr from the
  8334. * SEEPROM attached to the controller, into buf, using the controller's
  8335. * SEEPROM reading state machine. Optionally treat the data as a byte
  8336. * stream in terms of byte order.
  8337. */
  8338. int
  8339. ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8340. u_int start_addr, u_int count, int bytestream)
  8341. {
  8342. u_int cur_addr;
  8343. u_int end_addr;
  8344. int error;
  8345. /*
  8346. * If we never make it through the loop even once,
  8347. * we were passed invalid arguments.
  8348. */
  8349. error = EINVAL;
  8350. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8351. end_addr = start_addr + count;
  8352. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8353. ahd_outb(ahd, SEEADR, cur_addr);
  8354. ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
  8355. error = ahd_wait_seeprom(ahd);
  8356. if (error)
  8357. break;
  8358. if (bytestream != 0) {
  8359. uint8_t *bytestream_ptr;
  8360. bytestream_ptr = (uint8_t *)buf;
  8361. *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
  8362. *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
  8363. } else {
  8364. /*
  8365. * ahd_inw() already handles machine byte order.
  8366. */
  8367. *buf = ahd_inw(ahd, SEEDAT);
  8368. }
  8369. buf++;
  8370. }
  8371. return (error);
  8372. }
  8373. /*
  8374. * Write count 16bit words from buf, into SEEPROM attache to the
  8375. * controller starting at 16bit word address start_addr, using the
  8376. * controller's SEEPROM writing state machine.
  8377. */
  8378. int
  8379. ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
  8380. u_int start_addr, u_int count)
  8381. {
  8382. u_int cur_addr;
  8383. u_int end_addr;
  8384. int error;
  8385. int retval;
  8386. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8387. error = ENOENT;
  8388. /* Place the chip into write-enable mode */
  8389. ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
  8390. ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
  8391. error = ahd_wait_seeprom(ahd);
  8392. if (error)
  8393. return (error);
  8394. /*
  8395. * Write the data. If we don't get throught the loop at
  8396. * least once, the arguments were invalid.
  8397. */
  8398. retval = EINVAL;
  8399. end_addr = start_addr + count;
  8400. for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
  8401. ahd_outw(ahd, SEEDAT, *buf++);
  8402. ahd_outb(ahd, SEEADR, cur_addr);
  8403. ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
  8404. retval = ahd_wait_seeprom(ahd);
  8405. if (retval)
  8406. break;
  8407. }
  8408. /*
  8409. * Disable writes.
  8410. */
  8411. ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
  8412. ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
  8413. error = ahd_wait_seeprom(ahd);
  8414. if (error)
  8415. return (error);
  8416. return (retval);
  8417. }
  8418. /*
  8419. * Wait ~100us for the serial eeprom to satisfy our request.
  8420. */
  8421. int
  8422. ahd_wait_seeprom(struct ahd_softc *ahd)
  8423. {
  8424. int cnt;
  8425. cnt = 5000;
  8426. while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
  8427. ahd_delay(5);
  8428. if (cnt == 0)
  8429. return (ETIMEDOUT);
  8430. return (0);
  8431. }
  8432. /*
  8433. * Validate the two checksums in the per_channel
  8434. * vital product data struct.
  8435. */
  8436. int
  8437. ahd_verify_vpd_cksum(struct vpd_config *vpd)
  8438. {
  8439. int i;
  8440. int maxaddr;
  8441. uint32_t checksum;
  8442. uint8_t *vpdarray;
  8443. vpdarray = (uint8_t *)vpd;
  8444. maxaddr = offsetof(struct vpd_config, vpd_checksum);
  8445. checksum = 0;
  8446. for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
  8447. checksum = checksum + vpdarray[i];
  8448. if (checksum == 0
  8449. || (-checksum & 0xFF) != vpd->vpd_checksum)
  8450. return (0);
  8451. checksum = 0;
  8452. maxaddr = offsetof(struct vpd_config, checksum);
  8453. for (i = offsetof(struct vpd_config, default_target_flags);
  8454. i < maxaddr; i++)
  8455. checksum = checksum + vpdarray[i];
  8456. if (checksum == 0
  8457. || (-checksum & 0xFF) != vpd->checksum)
  8458. return (0);
  8459. return (1);
  8460. }
  8461. int
  8462. ahd_verify_cksum(struct seeprom_config *sc)
  8463. {
  8464. int i;
  8465. int maxaddr;
  8466. uint32_t checksum;
  8467. uint16_t *scarray;
  8468. maxaddr = (sizeof(*sc)/2) - 1;
  8469. checksum = 0;
  8470. scarray = (uint16_t *)sc;
  8471. for (i = 0; i < maxaddr; i++)
  8472. checksum = checksum + scarray[i];
  8473. if (checksum == 0
  8474. || (checksum & 0xFFFF) != sc->checksum) {
  8475. return (0);
  8476. } else {
  8477. return (1);
  8478. }
  8479. }
  8480. int
  8481. ahd_acquire_seeprom(struct ahd_softc *ahd)
  8482. {
  8483. /*
  8484. * We should be able to determine the SEEPROM type
  8485. * from the flexport logic, but unfortunately not
  8486. * all implementations have this logic and there is
  8487. * no programatic method for determining if the logic
  8488. * is present.
  8489. */
  8490. return (1);
  8491. #if 0
  8492. uint8_t seetype;
  8493. int error;
  8494. error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
  8495. if (error != 0
  8496. || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
  8497. return (0);
  8498. return (1);
  8499. #endif
  8500. }
  8501. void
  8502. ahd_release_seeprom(struct ahd_softc *ahd)
  8503. {
  8504. /* Currently a no-op */
  8505. }
  8506. int
  8507. ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
  8508. {
  8509. int error;
  8510. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8511. if (addr > 7)
  8512. panic("ahd_write_flexport: address out of range");
  8513. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8514. error = ahd_wait_flexport(ahd);
  8515. if (error != 0)
  8516. return (error);
  8517. ahd_outb(ahd, BRDDAT, value);
  8518. ahd_flush_device_writes(ahd);
  8519. ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
  8520. ahd_flush_device_writes(ahd);
  8521. ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
  8522. ahd_flush_device_writes(ahd);
  8523. ahd_outb(ahd, BRDCTL, 0);
  8524. ahd_flush_device_writes(ahd);
  8525. return (0);
  8526. }
  8527. int
  8528. ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
  8529. {
  8530. int error;
  8531. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8532. if (addr > 7)
  8533. panic("ahd_read_flexport: address out of range");
  8534. ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
  8535. error = ahd_wait_flexport(ahd);
  8536. if (error != 0)
  8537. return (error);
  8538. *value = ahd_inb(ahd, BRDDAT);
  8539. ahd_outb(ahd, BRDCTL, 0);
  8540. ahd_flush_device_writes(ahd);
  8541. return (0);
  8542. }
  8543. /*
  8544. * Wait at most 2 seconds for flexport arbitration to succeed.
  8545. */
  8546. int
  8547. ahd_wait_flexport(struct ahd_softc *ahd)
  8548. {
  8549. int cnt;
  8550. AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
  8551. cnt = 1000000 * 2 / 5;
  8552. while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
  8553. ahd_delay(5);
  8554. if (cnt == 0)
  8555. return (ETIMEDOUT);
  8556. return (0);
  8557. }
  8558. /************************* Target Mode ****************************************/
  8559. #ifdef AHD_TARGET_MODE
  8560. cam_status
  8561. ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
  8562. struct ahd_tmode_tstate **tstate,
  8563. struct ahd_tmode_lstate **lstate,
  8564. int notfound_failure)
  8565. {
  8566. if ((ahd->features & AHD_TARGETMODE) == 0)
  8567. return (CAM_REQ_INVALID);
  8568. /*
  8569. * Handle the 'black hole' device that sucks up
  8570. * requests to unattached luns on enabled targets.
  8571. */
  8572. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  8573. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  8574. *tstate = NULL;
  8575. *lstate = ahd->black_hole;
  8576. } else {
  8577. u_int max_id;
  8578. max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
  8579. if (ccb->ccb_h.target_id > max_id)
  8580. return (CAM_TID_INVALID);
  8581. if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
  8582. return (CAM_LUN_INVALID);
  8583. *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
  8584. *lstate = NULL;
  8585. if (*tstate != NULL)
  8586. *lstate =
  8587. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  8588. }
  8589. if (notfound_failure != 0 && *lstate == NULL)
  8590. return (CAM_PATH_INVALID);
  8591. return (CAM_REQ_CMP);
  8592. }
  8593. void
  8594. ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
  8595. {
  8596. #if NOT_YET
  8597. struct ahd_tmode_tstate *tstate;
  8598. struct ahd_tmode_lstate *lstate;
  8599. struct ccb_en_lun *cel;
  8600. cam_status status;
  8601. u_int target;
  8602. u_int lun;
  8603. u_int target_mask;
  8604. u_long s;
  8605. char channel;
  8606. status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
  8607. /*notfound_failure*/FALSE);
  8608. if (status != CAM_REQ_CMP) {
  8609. ccb->ccb_h.status = status;
  8610. return;
  8611. }
  8612. if ((ahd->features & AHD_MULTIROLE) != 0) {
  8613. u_int our_id;
  8614. our_id = ahd->our_id;
  8615. if (ccb->ccb_h.target_id != our_id) {
  8616. if ((ahd->features & AHD_MULTI_TID) != 0
  8617. && (ahd->flags & AHD_INITIATORROLE) != 0) {
  8618. /*
  8619. * Only allow additional targets if
  8620. * the initiator role is disabled.
  8621. * The hardware cannot handle a re-select-in
  8622. * on the initiator id during a re-select-out
  8623. * on a different target id.
  8624. */
  8625. status = CAM_TID_INVALID;
  8626. } else if ((ahd->flags & AHD_INITIATORROLE) != 0
  8627. || ahd->enabled_luns > 0) {
  8628. /*
  8629. * Only allow our target id to change
  8630. * if the initiator role is not configured
  8631. * and there are no enabled luns which
  8632. * are attached to the currently registered
  8633. * scsi id.
  8634. */
  8635. status = CAM_TID_INVALID;
  8636. }
  8637. }
  8638. }
  8639. if (status != CAM_REQ_CMP) {
  8640. ccb->ccb_h.status = status;
  8641. return;
  8642. }
  8643. /*
  8644. * We now have an id that is valid.
  8645. * If we aren't in target mode, switch modes.
  8646. */
  8647. if ((ahd->flags & AHD_TARGETROLE) == 0
  8648. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  8649. u_long s;
  8650. printf("Configuring Target Mode\n");
  8651. ahd_lock(ahd, &s);
  8652. if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
  8653. ccb->ccb_h.status = CAM_BUSY;
  8654. ahd_unlock(ahd, &s);
  8655. return;
  8656. }
  8657. ahd->flags |= AHD_TARGETROLE;
  8658. if ((ahd->features & AHD_MULTIROLE) == 0)
  8659. ahd->flags &= ~AHD_INITIATORROLE;
  8660. ahd_pause(ahd);
  8661. ahd_loadseq(ahd);
  8662. ahd_restart(ahd);
  8663. ahd_unlock(ahd, &s);
  8664. }
  8665. cel = &ccb->cel;
  8666. target = ccb->ccb_h.target_id;
  8667. lun = ccb->ccb_h.target_lun;
  8668. channel = SIM_CHANNEL(ahd, sim);
  8669. target_mask = 0x01 << target;
  8670. if (channel == 'B')
  8671. target_mask <<= 8;
  8672. if (cel->enable != 0) {
  8673. u_int scsiseq1;
  8674. /* Are we already enabled?? */
  8675. if (lstate != NULL) {
  8676. xpt_print_path(ccb->ccb_h.path);
  8677. printf("Lun already enabled\n");
  8678. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  8679. return;
  8680. }
  8681. if (cel->grp6_len != 0
  8682. || cel->grp7_len != 0) {
  8683. /*
  8684. * Don't (yet?) support vendor
  8685. * specific commands.
  8686. */
  8687. ccb->ccb_h.status = CAM_REQ_INVALID;
  8688. printf("Non-zero Group Codes\n");
  8689. return;
  8690. }
  8691. /*
  8692. * Seems to be okay.
  8693. * Setup our data structures.
  8694. */
  8695. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  8696. tstate = ahd_alloc_tstate(ahd, target, channel);
  8697. if (tstate == NULL) {
  8698. xpt_print_path(ccb->ccb_h.path);
  8699. printf("Couldn't allocate tstate\n");
  8700. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8701. return;
  8702. }
  8703. }
  8704. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  8705. if (lstate == NULL) {
  8706. xpt_print_path(ccb->ccb_h.path);
  8707. printf("Couldn't allocate lstate\n");
  8708. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8709. return;
  8710. }
  8711. memset(lstate, 0, sizeof(*lstate));
  8712. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  8713. xpt_path_path_id(ccb->ccb_h.path),
  8714. xpt_path_target_id(ccb->ccb_h.path),
  8715. xpt_path_lun_id(ccb->ccb_h.path));
  8716. if (status != CAM_REQ_CMP) {
  8717. free(lstate, M_DEVBUF);
  8718. xpt_print_path(ccb->ccb_h.path);
  8719. printf("Couldn't allocate path\n");
  8720. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  8721. return;
  8722. }
  8723. SLIST_INIT(&lstate->accept_tios);
  8724. SLIST_INIT(&lstate->immed_notifies);
  8725. ahd_lock(ahd, &s);
  8726. ahd_pause(ahd);
  8727. if (target != CAM_TARGET_WILDCARD) {
  8728. tstate->enabled_luns[lun] = lstate;
  8729. ahd->enabled_luns++;
  8730. if ((ahd->features & AHD_MULTI_TID) != 0) {
  8731. u_int targid_mask;
  8732. targid_mask = ahd_inw(ahd, TARGID);
  8733. targid_mask |= target_mask;
  8734. ahd_outw(ahd, TARGID, targid_mask);
  8735. ahd_update_scsiid(ahd, targid_mask);
  8736. } else {
  8737. u_int our_id;
  8738. char channel;
  8739. channel = SIM_CHANNEL(ahd, sim);
  8740. our_id = SIM_SCSI_ID(ahd, sim);
  8741. /*
  8742. * This can only happen if selections
  8743. * are not enabled
  8744. */
  8745. if (target != our_id) {
  8746. u_int sblkctl;
  8747. char cur_channel;
  8748. int swap;
  8749. sblkctl = ahd_inb(ahd, SBLKCTL);
  8750. cur_channel = (sblkctl & SELBUSB)
  8751. ? 'B' : 'A';
  8752. if ((ahd->features & AHD_TWIN) == 0)
  8753. cur_channel = 'A';
  8754. swap = cur_channel != channel;
  8755. ahd->our_id = target;
  8756. if (swap)
  8757. ahd_outb(ahd, SBLKCTL,
  8758. sblkctl ^ SELBUSB);
  8759. ahd_outb(ahd, SCSIID, target);
  8760. if (swap)
  8761. ahd_outb(ahd, SBLKCTL, sblkctl);
  8762. }
  8763. }
  8764. } else
  8765. ahd->black_hole = lstate;
  8766. /* Allow select-in operations */
  8767. if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
  8768. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8769. scsiseq1 |= ENSELI;
  8770. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8771. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8772. scsiseq1 |= ENSELI;
  8773. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8774. }
  8775. ahd_unpause(ahd);
  8776. ahd_unlock(ahd, &s);
  8777. ccb->ccb_h.status = CAM_REQ_CMP;
  8778. xpt_print_path(ccb->ccb_h.path);
  8779. printf("Lun now enabled for target mode\n");
  8780. } else {
  8781. struct scb *scb;
  8782. int i, empty;
  8783. if (lstate == NULL) {
  8784. ccb->ccb_h.status = CAM_LUN_INVALID;
  8785. return;
  8786. }
  8787. ahd_lock(ahd, &s);
  8788. ccb->ccb_h.status = CAM_REQ_CMP;
  8789. LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
  8790. struct ccb_hdr *ccbh;
  8791. ccbh = &scb->io_ctx->ccb_h;
  8792. if (ccbh->func_code == XPT_CONT_TARGET_IO
  8793. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  8794. printf("CTIO pending\n");
  8795. ccb->ccb_h.status = CAM_REQ_INVALID;
  8796. ahd_unlock(ahd, &s);
  8797. return;
  8798. }
  8799. }
  8800. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  8801. printf("ATIOs pending\n");
  8802. ccb->ccb_h.status = CAM_REQ_INVALID;
  8803. }
  8804. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  8805. printf("INOTs pending\n");
  8806. ccb->ccb_h.status = CAM_REQ_INVALID;
  8807. }
  8808. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  8809. ahd_unlock(ahd, &s);
  8810. return;
  8811. }
  8812. xpt_print_path(ccb->ccb_h.path);
  8813. printf("Target mode disabled\n");
  8814. xpt_free_path(lstate->path);
  8815. free(lstate, M_DEVBUF);
  8816. ahd_pause(ahd);
  8817. /* Can we clean up the target too? */
  8818. if (target != CAM_TARGET_WILDCARD) {
  8819. tstate->enabled_luns[lun] = NULL;
  8820. ahd->enabled_luns--;
  8821. for (empty = 1, i = 0; i < 8; i++)
  8822. if (tstate->enabled_luns[i] != NULL) {
  8823. empty = 0;
  8824. break;
  8825. }
  8826. if (empty) {
  8827. ahd_free_tstate(ahd, target, channel,
  8828. /*force*/FALSE);
  8829. if (ahd->features & AHD_MULTI_TID) {
  8830. u_int targid_mask;
  8831. targid_mask = ahd_inw(ahd, TARGID);
  8832. targid_mask &= ~target_mask;
  8833. ahd_outw(ahd, TARGID, targid_mask);
  8834. ahd_update_scsiid(ahd, targid_mask);
  8835. }
  8836. }
  8837. } else {
  8838. ahd->black_hole = NULL;
  8839. /*
  8840. * We can't allow selections without
  8841. * our black hole device.
  8842. */
  8843. empty = TRUE;
  8844. }
  8845. if (ahd->enabled_luns == 0) {
  8846. /* Disallow select-in */
  8847. u_int scsiseq1;
  8848. scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
  8849. scsiseq1 &= ~ENSELI;
  8850. ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
  8851. scsiseq1 = ahd_inb(ahd, SCSISEQ1);
  8852. scsiseq1 &= ~ENSELI;
  8853. ahd_outb(ahd, SCSISEQ1, scsiseq1);
  8854. if ((ahd->features & AHD_MULTIROLE) == 0) {
  8855. printf("Configuring Initiator Mode\n");
  8856. ahd->flags &= ~AHD_TARGETROLE;
  8857. ahd->flags |= AHD_INITIATORROLE;
  8858. ahd_pause(ahd);
  8859. ahd_loadseq(ahd);
  8860. ahd_restart(ahd);
  8861. /*
  8862. * Unpaused. The extra unpause
  8863. * that follows is harmless.
  8864. */
  8865. }
  8866. }
  8867. ahd_unpause(ahd);
  8868. ahd_unlock(ahd, &s);
  8869. }
  8870. #endif
  8871. }
  8872. static void
  8873. ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
  8874. {
  8875. #if NOT_YET
  8876. u_int scsiid_mask;
  8877. u_int scsiid;
  8878. if ((ahd->features & AHD_MULTI_TID) == 0)
  8879. panic("ahd_update_scsiid called on non-multitid unit\n");
  8880. /*
  8881. * Since we will rely on the TARGID mask
  8882. * for selection enables, ensure that OID
  8883. * in SCSIID is not set to some other ID
  8884. * that we don't want to allow selections on.
  8885. */
  8886. if ((ahd->features & AHD_ULTRA2) != 0)
  8887. scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
  8888. else
  8889. scsiid = ahd_inb(ahd, SCSIID);
  8890. scsiid_mask = 0x1 << (scsiid & OID);
  8891. if ((targid_mask & scsiid_mask) == 0) {
  8892. u_int our_id;
  8893. /* ffs counts from 1 */
  8894. our_id = ffs(targid_mask);
  8895. if (our_id == 0)
  8896. our_id = ahd->our_id;
  8897. else
  8898. our_id--;
  8899. scsiid &= TID;
  8900. scsiid |= our_id;
  8901. }
  8902. if ((ahd->features & AHD_ULTRA2) != 0)
  8903. ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
  8904. else
  8905. ahd_outb(ahd, SCSIID, scsiid);
  8906. #endif
  8907. }
  8908. void
  8909. ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
  8910. {
  8911. struct target_cmd *cmd;
  8912. ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
  8913. while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
  8914. /*
  8915. * Only advance through the queue if we
  8916. * have the resources to process the command.
  8917. */
  8918. if (ahd_handle_target_cmd(ahd, cmd) != 0)
  8919. break;
  8920. cmd->cmd_valid = 0;
  8921. ahd_dmamap_sync(ahd, ahd->shared_data_dmat,
  8922. ahd->shared_data_map.dmamap,
  8923. ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
  8924. sizeof(struct target_cmd),
  8925. BUS_DMASYNC_PREREAD);
  8926. ahd->tqinfifonext++;
  8927. /*
  8928. * Lazily update our position in the target mode incoming
  8929. * command queue as seen by the sequencer.
  8930. */
  8931. if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  8932. u_int hs_mailbox;
  8933. hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
  8934. hs_mailbox &= ~HOST_TQINPOS;
  8935. hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
  8936. ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
  8937. }
  8938. }
  8939. }
  8940. static int
  8941. ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
  8942. {
  8943. struct ahd_tmode_tstate *tstate;
  8944. struct ahd_tmode_lstate *lstate;
  8945. struct ccb_accept_tio *atio;
  8946. uint8_t *byte;
  8947. int initiator;
  8948. int target;
  8949. int lun;
  8950. initiator = SCSIID_TARGET(ahd, cmd->scsiid);
  8951. target = SCSIID_OUR_ID(cmd->scsiid);
  8952. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  8953. byte = cmd->bytes;
  8954. tstate = ahd->enabled_targets[target];
  8955. lstate = NULL;
  8956. if (tstate != NULL)
  8957. lstate = tstate->enabled_luns[lun];
  8958. /*
  8959. * Commands for disabled luns go to the black hole driver.
  8960. */
  8961. if (lstate == NULL)
  8962. lstate = ahd->black_hole;
  8963. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  8964. if (atio == NULL) {
  8965. ahd->flags |= AHD_TQINFIFO_BLOCKED;
  8966. /*
  8967. * Wait for more ATIOs from the peripheral driver for this lun.
  8968. */
  8969. return (1);
  8970. } else
  8971. ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
  8972. #ifdef AHD_DEBUG
  8973. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  8974. printf("Incoming command from %d for %d:%d%s\n",
  8975. initiator, target, lun,
  8976. lstate == ahd->black_hole ? "(Black Holed)" : "");
  8977. #endif
  8978. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  8979. if (lstate == ahd->black_hole) {
  8980. /* Fill in the wildcards */
  8981. atio->ccb_h.target_id = target;
  8982. atio->ccb_h.target_lun = lun;
  8983. }
  8984. /*
  8985. * Package it up and send it off to
  8986. * whomever has this lun enabled.
  8987. */
  8988. atio->sense_len = 0;
  8989. atio->init_id = initiator;
  8990. if (byte[0] != 0xFF) {
  8991. /* Tag was included */
  8992. atio->tag_action = *byte++;
  8993. atio->tag_id = *byte++;
  8994. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  8995. } else {
  8996. atio->ccb_h.flags = 0;
  8997. }
  8998. byte++;
  8999. /* Okay. Now determine the cdb size based on the command code */
  9000. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  9001. case 0:
  9002. atio->cdb_len = 6;
  9003. break;
  9004. case 1:
  9005. case 2:
  9006. atio->cdb_len = 10;
  9007. break;
  9008. case 4:
  9009. atio->cdb_len = 16;
  9010. break;
  9011. case 5:
  9012. atio->cdb_len = 12;
  9013. break;
  9014. case 3:
  9015. default:
  9016. /* Only copy the opcode. */
  9017. atio->cdb_len = 1;
  9018. printf("Reserved or VU command code type encountered\n");
  9019. break;
  9020. }
  9021. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  9022. atio->ccb_h.status |= CAM_CDB_RECVD;
  9023. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  9024. /*
  9025. * We weren't allowed to disconnect.
  9026. * We're hanging on the bus until a
  9027. * continue target I/O comes in response
  9028. * to this accept tio.
  9029. */
  9030. #ifdef AHD_DEBUG
  9031. if ((ahd_debug & AHD_SHOW_TQIN) != 0)
  9032. printf("Received Immediate Command %d:%d:%d - %p\n",
  9033. initiator, target, lun, ahd->pending_device);
  9034. #endif
  9035. ahd->pending_device = lstate;
  9036. ahd_freeze_ccb((union ccb *)atio);
  9037. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  9038. }
  9039. xpt_done((union ccb*)atio);
  9040. return (0);
  9041. }
  9042. #endif