aic79xx_core.c 267 KB

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