aic79xx_core.c 272 KB

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