aic79xx_core.c 293 KB

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