aic79xx_core.c 264 KB

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