aic79xx_core.c 271 KB

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