aic79xx_core.c 272 KB

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