aic7xxx_core.c 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972
  1. /*
  2. * Core routines and tables shareable across OS platforms.
  3. *
  4. * Copyright (c) 1994-2002 Justin T. Gibbs.
  5. * Copyright (c) 2000-2002 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/aic7xxx.c#155 $
  41. */
  42. #ifdef __linux__
  43. #include "aic7xxx_osm.h"
  44. #include "aic7xxx_inline.h"
  45. #include "aicasm/aicasm_insformat.h"
  46. #else
  47. #include <dev/aic7xxx/aic7xxx_osm.h>
  48. #include <dev/aic7xxx/aic7xxx_inline.h>
  49. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  50. #endif
  51. /***************************** Lookup Tables **********************************/
  52. static const char *const ahc_chip_names[] = {
  53. "NONE",
  54. "aic7770",
  55. "aic7850",
  56. "aic7855",
  57. "aic7859",
  58. "aic7860",
  59. "aic7870",
  60. "aic7880",
  61. "aic7895",
  62. "aic7895C",
  63. "aic7890/91",
  64. "aic7896/97",
  65. "aic7892",
  66. "aic7899"
  67. };
  68. static const u_int num_chip_names = ARRAY_SIZE(ahc_chip_names);
  69. /*
  70. * Hardware error codes.
  71. */
  72. struct ahc_hard_error_entry {
  73. uint8_t errno;
  74. const char *errmesg;
  75. };
  76. static const struct ahc_hard_error_entry ahc_hard_errors[] = {
  77. { ILLHADDR, "Illegal Host Access" },
  78. { ILLSADDR, "Illegal Sequencer Address referrenced" },
  79. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  80. { SQPARERR, "Sequencer Parity Error" },
  81. { DPARERR, "Data-path Parity Error" },
  82. { MPARERR, "Scratch or SCB Memory Parity Error" },
  83. { PCIERRSTAT, "PCI Error detected" },
  84. { CIOPARERR, "CIOBUS Parity Error" },
  85. };
  86. static const u_int num_errors = ARRAY_SIZE(ahc_hard_errors);
  87. static const struct ahc_phase_table_entry ahc_phase_table[] =
  88. {
  89. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  90. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  91. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  92. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  93. { P_COMMAND, MSG_NOOP, "in Command phase" },
  94. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  95. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  96. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  97. { P_BUSFREE, MSG_NOOP, "while idle" },
  98. { 0, MSG_NOOP, "in unknown phase" }
  99. };
  100. /*
  101. * In most cases we only wish to itterate over real phases, so
  102. * exclude the last element from the count.
  103. */
  104. static const u_int num_phases = ARRAY_SIZE(ahc_phase_table) - 1;
  105. /*
  106. * Valid SCSIRATE values. (p. 3-17)
  107. * Provides a mapping of tranfer periods in ns to the proper value to
  108. * stick in the scsixfer reg.
  109. */
  110. static const struct ahc_syncrate ahc_syncrates[] =
  111. {
  112. /* ultra2 fast/ultra period rate */
  113. { 0x42, 0x000, 9, "80.0" },
  114. { 0x03, 0x000, 10, "40.0" },
  115. { 0x04, 0x000, 11, "33.0" },
  116. { 0x05, 0x100, 12, "20.0" },
  117. { 0x06, 0x110, 15, "16.0" },
  118. { 0x07, 0x120, 18, "13.4" },
  119. { 0x08, 0x000, 25, "10.0" },
  120. { 0x19, 0x010, 31, "8.0" },
  121. { 0x1a, 0x020, 37, "6.67" },
  122. { 0x1b, 0x030, 43, "5.7" },
  123. { 0x1c, 0x040, 50, "5.0" },
  124. { 0x00, 0x050, 56, "4.4" },
  125. { 0x00, 0x060, 62, "4.0" },
  126. { 0x00, 0x070, 68, "3.6" },
  127. { 0x00, 0x000, 0, NULL }
  128. };
  129. /* Our Sequencer Program */
  130. #include "aic7xxx_seq.h"
  131. /**************************** Function Declarations ***************************/
  132. static void ahc_force_renegotiation(struct ahc_softc *ahc,
  133. struct ahc_devinfo *devinfo);
  134. static struct ahc_tmode_tstate*
  135. ahc_alloc_tstate(struct ahc_softc *ahc,
  136. u_int scsi_id, char channel);
  137. #ifdef AHC_TARGET_MODE
  138. static void ahc_free_tstate(struct ahc_softc *ahc,
  139. u_int scsi_id, char channel, int force);
  140. #endif
  141. static const struct ahc_syncrate*
  142. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  143. struct ahc_initiator_tinfo *,
  144. u_int *period,
  145. u_int *ppr_options,
  146. role_t role);
  147. static void ahc_update_pending_scbs(struct ahc_softc *ahc);
  148. static void ahc_fetch_devinfo(struct ahc_softc *ahc,
  149. struct ahc_devinfo *devinfo);
  150. static void ahc_scb_devinfo(struct ahc_softc *ahc,
  151. struct ahc_devinfo *devinfo,
  152. struct scb *scb);
  153. static void ahc_assert_atn(struct ahc_softc *ahc);
  154. static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
  155. struct ahc_devinfo *devinfo,
  156. struct scb *scb);
  157. static void ahc_build_transfer_msg(struct ahc_softc *ahc,
  158. struct ahc_devinfo *devinfo);
  159. static void ahc_construct_sdtr(struct ahc_softc *ahc,
  160. struct ahc_devinfo *devinfo,
  161. u_int period, u_int offset);
  162. static void ahc_construct_wdtr(struct ahc_softc *ahc,
  163. struct ahc_devinfo *devinfo,
  164. u_int bus_width);
  165. static void ahc_construct_ppr(struct ahc_softc *ahc,
  166. struct ahc_devinfo *devinfo,
  167. u_int period, u_int offset,
  168. u_int bus_width, u_int ppr_options);
  169. static void ahc_clear_msg_state(struct ahc_softc *ahc);
  170. static void ahc_handle_proto_violation(struct ahc_softc *ahc);
  171. static void ahc_handle_message_phase(struct ahc_softc *ahc);
  172. typedef enum {
  173. AHCMSG_1B,
  174. AHCMSG_2B,
  175. AHCMSG_EXT
  176. } ahc_msgtype;
  177. static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
  178. u_int msgval, int full);
  179. static int ahc_parse_msg(struct ahc_softc *ahc,
  180. struct ahc_devinfo *devinfo);
  181. static int ahc_handle_msg_reject(struct ahc_softc *ahc,
  182. struct ahc_devinfo *devinfo);
  183. static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
  184. struct ahc_devinfo *devinfo);
  185. static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
  186. static void ahc_handle_devreset(struct ahc_softc *ahc,
  187. struct ahc_devinfo *devinfo,
  188. cam_status status, char *message,
  189. int verbose_level);
  190. #ifdef AHC_TARGET_MODE
  191. static void ahc_setup_target_msgin(struct ahc_softc *ahc,
  192. struct ahc_devinfo *devinfo,
  193. struct scb *scb);
  194. #endif
  195. static bus_dmamap_callback_t ahc_dmamap_cb;
  196. static void ahc_build_free_scb_list(struct ahc_softc *ahc);
  197. static int ahc_init_scbdata(struct ahc_softc *ahc);
  198. static void ahc_fini_scbdata(struct ahc_softc *ahc);
  199. static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
  200. struct scb *prev_scb,
  201. struct scb *scb);
  202. static int ahc_qinfifo_count(struct ahc_softc *ahc);
  203. static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
  204. u_int prev, u_int scbptr);
  205. static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
  206. static u_int ahc_rem_wscb(struct ahc_softc *ahc,
  207. u_int scbpos, u_int prev);
  208. static void ahc_reset_current_bus(struct ahc_softc *ahc);
  209. #ifdef AHC_DUMP_SEQ
  210. static void ahc_dumpseq(struct ahc_softc *ahc);
  211. #endif
  212. static int ahc_loadseq(struct ahc_softc *ahc);
  213. static int ahc_check_patch(struct ahc_softc *ahc,
  214. const struct patch **start_patch,
  215. u_int start_instr, u_int *skip_addr);
  216. static void ahc_download_instr(struct ahc_softc *ahc,
  217. u_int instrptr, uint8_t *dconsts);
  218. #ifdef AHC_TARGET_MODE
  219. static void ahc_queue_lstate_event(struct ahc_softc *ahc,
  220. struct ahc_tmode_lstate *lstate,
  221. u_int initiator_id,
  222. u_int event_type,
  223. u_int event_arg);
  224. static void ahc_update_scsiid(struct ahc_softc *ahc,
  225. u_int targid_mask);
  226. static int ahc_handle_target_cmd(struct ahc_softc *ahc,
  227. struct target_cmd *cmd);
  228. #endif
  229. static u_int ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl);
  230. static void ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl);
  231. static void ahc_busy_tcl(struct ahc_softc *ahc,
  232. u_int tcl, u_int busyid);
  233. /************************** SCB and SCB queue management **********************/
  234. static void ahc_run_untagged_queues(struct ahc_softc *ahc);
  235. static void ahc_run_untagged_queue(struct ahc_softc *ahc,
  236. struct scb_tailq *queue);
  237. /****************************** Initialization ********************************/
  238. static void ahc_alloc_scbs(struct ahc_softc *ahc);
  239. static void ahc_shutdown(void *arg);
  240. /*************************** Interrupt Services *******************************/
  241. static void ahc_clear_intstat(struct ahc_softc *ahc);
  242. static void ahc_run_qoutfifo(struct ahc_softc *ahc);
  243. #ifdef AHC_TARGET_MODE
  244. static void ahc_run_tqinfifo(struct ahc_softc *ahc, int paused);
  245. #endif
  246. static void ahc_handle_brkadrint(struct ahc_softc *ahc);
  247. static void ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat);
  248. static void ahc_handle_scsiint(struct ahc_softc *ahc,
  249. u_int intstat);
  250. static void ahc_clear_critical_section(struct ahc_softc *ahc);
  251. /***************************** Error Recovery *********************************/
  252. static void ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
  253. static int ahc_abort_scbs(struct ahc_softc *ahc, int target,
  254. char channel, int lun, u_int tag,
  255. role_t role, uint32_t status);
  256. static void ahc_calc_residual(struct ahc_softc *ahc,
  257. struct scb *scb);
  258. /*********************** Untagged Transaction Routines ************************/
  259. static inline void ahc_freeze_untagged_queues(struct ahc_softc *ahc);
  260. static inline void ahc_release_untagged_queues(struct ahc_softc *ahc);
  261. /*
  262. * Block our completion routine from starting the next untagged
  263. * transaction for this target or target lun.
  264. */
  265. static inline void
  266. ahc_freeze_untagged_queues(struct ahc_softc *ahc)
  267. {
  268. if ((ahc->flags & AHC_SCB_BTT) == 0)
  269. ahc->untagged_queue_lock++;
  270. }
  271. /*
  272. * Allow the next untagged transaction for this target or target lun
  273. * to be executed. We use a counting semaphore to allow the lock
  274. * to be acquired recursively. Once the count drops to zero, the
  275. * transaction queues will be run.
  276. */
  277. static inline void
  278. ahc_release_untagged_queues(struct ahc_softc *ahc)
  279. {
  280. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  281. ahc->untagged_queue_lock--;
  282. if (ahc->untagged_queue_lock == 0)
  283. ahc_run_untagged_queues(ahc);
  284. }
  285. }
  286. /************************* Sequencer Execution Control ************************/
  287. /*
  288. * Work around any chip bugs related to halting sequencer execution.
  289. * On Ultra2 controllers, we must clear the CIOBUS stretch signal by
  290. * reading a register that will set this signal and deassert it.
  291. * Without this workaround, if the chip is paused, by an interrupt or
  292. * manual pause while accessing scb ram, accesses to certain registers
  293. * will hang the system (infinite pci retries).
  294. */
  295. static void
  296. ahc_pause_bug_fix(struct ahc_softc *ahc)
  297. {
  298. if ((ahc->features & AHC_ULTRA2) != 0)
  299. (void)ahc_inb(ahc, CCSCBCTL);
  300. }
  301. /*
  302. * Determine whether the sequencer has halted code execution.
  303. * Returns non-zero status if the sequencer is stopped.
  304. */
  305. int
  306. ahc_is_paused(struct ahc_softc *ahc)
  307. {
  308. return ((ahc_inb(ahc, HCNTRL) & PAUSE) != 0);
  309. }
  310. /*
  311. * Request that the sequencer stop and wait, indefinitely, for it
  312. * to stop. The sequencer will only acknowledge that it is paused
  313. * once it has reached an instruction boundary and PAUSEDIS is
  314. * cleared in the SEQCTL register. The sequencer may use PAUSEDIS
  315. * for critical sections.
  316. */
  317. void
  318. ahc_pause(struct ahc_softc *ahc)
  319. {
  320. ahc_outb(ahc, HCNTRL, ahc->pause);
  321. /*
  322. * Since the sequencer can disable pausing in a critical section, we
  323. * must loop until it actually stops.
  324. */
  325. while (ahc_is_paused(ahc) == 0)
  326. ;
  327. ahc_pause_bug_fix(ahc);
  328. }
  329. /*
  330. * Allow the sequencer to continue program execution.
  331. * We check here to ensure that no additional interrupt
  332. * sources that would cause the sequencer to halt have been
  333. * asserted. If, for example, a SCSI bus reset is detected
  334. * while we are fielding a different, pausing, interrupt type,
  335. * we don't want to release the sequencer before going back
  336. * into our interrupt handler and dealing with this new
  337. * condition.
  338. */
  339. void
  340. ahc_unpause(struct ahc_softc *ahc)
  341. {
  342. if ((ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)
  343. ahc_outb(ahc, HCNTRL, ahc->unpause);
  344. }
  345. /************************** Memory mapping routines ***************************/
  346. static struct ahc_dma_seg *
  347. ahc_sg_bus_to_virt(struct scb *scb, uint32_t sg_busaddr)
  348. {
  349. int sg_index;
  350. sg_index = (sg_busaddr - scb->sg_list_phys)/sizeof(struct ahc_dma_seg);
  351. /* sg_list_phys points to entry 1, not 0 */
  352. sg_index++;
  353. return (&scb->sg_list[sg_index]);
  354. }
  355. static uint32_t
  356. ahc_sg_virt_to_bus(struct scb *scb, struct ahc_dma_seg *sg)
  357. {
  358. int sg_index;
  359. /* sg_list_phys points to entry 1, not 0 */
  360. sg_index = sg - &scb->sg_list[1];
  361. return (scb->sg_list_phys + (sg_index * sizeof(*scb->sg_list)));
  362. }
  363. static uint32_t
  364. ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index)
  365. {
  366. return (ahc->scb_data->hscb_busaddr
  367. + (sizeof(struct hardware_scb) * index));
  368. }
  369. static void
  370. ahc_sync_scb(struct ahc_softc *ahc, struct scb *scb, int op)
  371. {
  372. ahc_dmamap_sync(ahc, ahc->scb_data->hscb_dmat,
  373. ahc->scb_data->hscb_dmamap,
  374. /*offset*/(scb->hscb - ahc->hscbs) * sizeof(*scb->hscb),
  375. /*len*/sizeof(*scb->hscb), op);
  376. }
  377. void
  378. ahc_sync_sglist(struct ahc_softc *ahc, struct scb *scb, int op)
  379. {
  380. if (scb->sg_count == 0)
  381. return;
  382. ahc_dmamap_sync(ahc, ahc->scb_data->sg_dmat, scb->sg_map->sg_dmamap,
  383. /*offset*/(scb->sg_list - scb->sg_map->sg_vaddr)
  384. * sizeof(struct ahc_dma_seg),
  385. /*len*/sizeof(struct ahc_dma_seg) * scb->sg_count, op);
  386. }
  387. #ifdef AHC_TARGET_MODE
  388. static uint32_t
  389. ahc_targetcmd_offset(struct ahc_softc *ahc, u_int index)
  390. {
  391. return (((uint8_t *)&ahc->targetcmds[index]) - ahc->qoutfifo);
  392. }
  393. #endif
  394. /*********************** Miscelaneous Support Functions ***********************/
  395. /*
  396. * Determine whether the sequencer reported a residual
  397. * for this SCB/transaction.
  398. */
  399. static void
  400. ahc_update_residual(struct ahc_softc *ahc, struct scb *scb)
  401. {
  402. uint32_t sgptr;
  403. sgptr = ahc_le32toh(scb->hscb->sgptr);
  404. if ((sgptr & SG_RESID_VALID) != 0)
  405. ahc_calc_residual(ahc, scb);
  406. }
  407. /*
  408. * Return pointers to the transfer negotiation information
  409. * for the specified our_id/remote_id pair.
  410. */
  411. struct ahc_initiator_tinfo *
  412. ahc_fetch_transinfo(struct ahc_softc *ahc, char channel, u_int our_id,
  413. u_int remote_id, struct ahc_tmode_tstate **tstate)
  414. {
  415. /*
  416. * Transfer data structures are stored from the perspective
  417. * of the target role. Since the parameters for a connection
  418. * in the initiator role to a given target are the same as
  419. * when the roles are reversed, we pretend we are the target.
  420. */
  421. if (channel == 'B')
  422. our_id += 8;
  423. *tstate = ahc->enabled_targets[our_id];
  424. return (&(*tstate)->transinfo[remote_id]);
  425. }
  426. uint16_t
  427. ahc_inw(struct ahc_softc *ahc, u_int port)
  428. {
  429. uint16_t r = ahc_inb(ahc, port+1) << 8;
  430. return r | ahc_inb(ahc, port);
  431. }
  432. void
  433. ahc_outw(struct ahc_softc *ahc, u_int port, u_int value)
  434. {
  435. ahc_outb(ahc, port, value & 0xFF);
  436. ahc_outb(ahc, port+1, (value >> 8) & 0xFF);
  437. }
  438. uint32_t
  439. ahc_inl(struct ahc_softc *ahc, u_int port)
  440. {
  441. return ((ahc_inb(ahc, port))
  442. | (ahc_inb(ahc, port+1) << 8)
  443. | (ahc_inb(ahc, port+2) << 16)
  444. | (ahc_inb(ahc, port+3) << 24));
  445. }
  446. void
  447. ahc_outl(struct ahc_softc *ahc, u_int port, uint32_t value)
  448. {
  449. ahc_outb(ahc, port, (value) & 0xFF);
  450. ahc_outb(ahc, port+1, ((value) >> 8) & 0xFF);
  451. ahc_outb(ahc, port+2, ((value) >> 16) & 0xFF);
  452. ahc_outb(ahc, port+3, ((value) >> 24) & 0xFF);
  453. }
  454. uint64_t
  455. ahc_inq(struct ahc_softc *ahc, u_int port)
  456. {
  457. return ((ahc_inb(ahc, port))
  458. | (ahc_inb(ahc, port+1) << 8)
  459. | (ahc_inb(ahc, port+2) << 16)
  460. | (ahc_inb(ahc, port+3) << 24)
  461. | (((uint64_t)ahc_inb(ahc, port+4)) << 32)
  462. | (((uint64_t)ahc_inb(ahc, port+5)) << 40)
  463. | (((uint64_t)ahc_inb(ahc, port+6)) << 48)
  464. | (((uint64_t)ahc_inb(ahc, port+7)) << 56));
  465. }
  466. void
  467. ahc_outq(struct ahc_softc *ahc, u_int port, uint64_t value)
  468. {
  469. ahc_outb(ahc, port, value & 0xFF);
  470. ahc_outb(ahc, port+1, (value >> 8) & 0xFF);
  471. ahc_outb(ahc, port+2, (value >> 16) & 0xFF);
  472. ahc_outb(ahc, port+3, (value >> 24) & 0xFF);
  473. ahc_outb(ahc, port+4, (value >> 32) & 0xFF);
  474. ahc_outb(ahc, port+5, (value >> 40) & 0xFF);
  475. ahc_outb(ahc, port+6, (value >> 48) & 0xFF);
  476. ahc_outb(ahc, port+7, (value >> 56) & 0xFF);
  477. }
  478. /*
  479. * Get a free scb. If there are none, see if we can allocate a new SCB.
  480. */
  481. struct scb *
  482. ahc_get_scb(struct ahc_softc *ahc)
  483. {
  484. struct scb *scb;
  485. if ((scb = SLIST_FIRST(&ahc->scb_data->free_scbs)) == NULL) {
  486. ahc_alloc_scbs(ahc);
  487. scb = SLIST_FIRST(&ahc->scb_data->free_scbs);
  488. if (scb == NULL)
  489. return (NULL);
  490. }
  491. SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links.sle);
  492. return (scb);
  493. }
  494. /*
  495. * Return an SCB resource to the free list.
  496. */
  497. void
  498. ahc_free_scb(struct ahc_softc *ahc, struct scb *scb)
  499. {
  500. struct hardware_scb *hscb;
  501. hscb = scb->hscb;
  502. /* Clean up for the next user */
  503. ahc->scb_data->scbindex[hscb->tag] = NULL;
  504. scb->flags = SCB_FREE;
  505. hscb->control = 0;
  506. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links.sle);
  507. /* Notify the OSM that a resource is now available. */
  508. ahc_platform_scb_free(ahc, scb);
  509. }
  510. struct scb *
  511. ahc_lookup_scb(struct ahc_softc *ahc, u_int tag)
  512. {
  513. struct scb* scb;
  514. scb = ahc->scb_data->scbindex[tag];
  515. if (scb != NULL)
  516. ahc_sync_scb(ahc, scb,
  517. BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
  518. return (scb);
  519. }
  520. static void
  521. ahc_swap_with_next_hscb(struct ahc_softc *ahc, struct scb *scb)
  522. {
  523. struct hardware_scb *q_hscb;
  524. u_int saved_tag;
  525. /*
  526. * Our queuing method is a bit tricky. The card
  527. * knows in advance which HSCB to download, and we
  528. * can't disappoint it. To achieve this, the next
  529. * SCB to download is saved off in ahc->next_queued_scb.
  530. * When we are called to queue "an arbitrary scb",
  531. * we copy the contents of the incoming HSCB to the one
  532. * the sequencer knows about, swap HSCB pointers and
  533. * finally assign the SCB to the tag indexed location
  534. * in the scb_array. This makes sure that we can still
  535. * locate the correct SCB by SCB_TAG.
  536. */
  537. q_hscb = ahc->next_queued_scb->hscb;
  538. saved_tag = q_hscb->tag;
  539. memcpy(q_hscb, scb->hscb, sizeof(*scb->hscb));
  540. if ((scb->flags & SCB_CDB32_PTR) != 0) {
  541. q_hscb->shared_data.cdb_ptr =
  542. ahc_htole32(ahc_hscb_busaddr(ahc, q_hscb->tag)
  543. + offsetof(struct hardware_scb, cdb32));
  544. }
  545. q_hscb->tag = saved_tag;
  546. q_hscb->next = scb->hscb->tag;
  547. /* Now swap HSCB pointers. */
  548. ahc->next_queued_scb->hscb = scb->hscb;
  549. scb->hscb = q_hscb;
  550. /* Now define the mapping from tag to SCB in the scbindex */
  551. ahc->scb_data->scbindex[scb->hscb->tag] = scb;
  552. }
  553. /*
  554. * Tell the sequencer about a new transaction to execute.
  555. */
  556. void
  557. ahc_queue_scb(struct ahc_softc *ahc, struct scb *scb)
  558. {
  559. ahc_swap_with_next_hscb(ahc, scb);
  560. if (scb->hscb->tag == SCB_LIST_NULL
  561. || scb->hscb->next == SCB_LIST_NULL)
  562. panic("Attempt to queue invalid SCB tag %x:%x\n",
  563. scb->hscb->tag, scb->hscb->next);
  564. /*
  565. * Setup data "oddness".
  566. */
  567. scb->hscb->lun &= LID;
  568. if (ahc_get_transfer_length(scb) & 0x1)
  569. scb->hscb->lun |= SCB_XFERLEN_ODD;
  570. /*
  571. * Keep a history of SCBs we've downloaded in the qinfifo.
  572. */
  573. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  574. /*
  575. * Make sure our data is consistent from the
  576. * perspective of the adapter.
  577. */
  578. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  579. /* Tell the adapter about the newly queued SCB */
  580. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  581. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  582. } else {
  583. if ((ahc->features & AHC_AUTOPAUSE) == 0)
  584. ahc_pause(ahc);
  585. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  586. if ((ahc->features & AHC_AUTOPAUSE) == 0)
  587. ahc_unpause(ahc);
  588. }
  589. }
  590. struct scsi_sense_data *
  591. ahc_get_sense_buf(struct ahc_softc *ahc, struct scb *scb)
  592. {
  593. int offset;
  594. offset = scb - ahc->scb_data->scbarray;
  595. return (&ahc->scb_data->sense[offset]);
  596. }
  597. static uint32_t
  598. ahc_get_sense_bufaddr(struct ahc_softc *ahc, struct scb *scb)
  599. {
  600. int offset;
  601. offset = scb - ahc->scb_data->scbarray;
  602. return (ahc->scb_data->sense_busaddr
  603. + (offset * sizeof(struct scsi_sense_data)));
  604. }
  605. /************************** Interrupt Processing ******************************/
  606. static void
  607. ahc_sync_qoutfifo(struct ahc_softc *ahc, int op)
  608. {
  609. ahc_dmamap_sync(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  610. /*offset*/0, /*len*/256, op);
  611. }
  612. static void
  613. ahc_sync_tqinfifo(struct ahc_softc *ahc, int op)
  614. {
  615. #ifdef AHC_TARGET_MODE
  616. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  617. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  618. ahc->shared_data_dmamap,
  619. ahc_targetcmd_offset(ahc, 0),
  620. sizeof(struct target_cmd) * AHC_TMODE_CMDS,
  621. op);
  622. }
  623. #endif
  624. }
  625. /*
  626. * See if the firmware has posted any completed commands
  627. * into our in-core command complete fifos.
  628. */
  629. #define AHC_RUN_QOUTFIFO 0x1
  630. #define AHC_RUN_TQINFIFO 0x2
  631. static u_int
  632. ahc_check_cmdcmpltqueues(struct ahc_softc *ahc)
  633. {
  634. u_int retval;
  635. retval = 0;
  636. ahc_dmamap_sync(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  637. /*offset*/ahc->qoutfifonext, /*len*/1,
  638. BUS_DMASYNC_POSTREAD);
  639. if (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL)
  640. retval |= AHC_RUN_QOUTFIFO;
  641. #ifdef AHC_TARGET_MODE
  642. if ((ahc->flags & AHC_TARGETROLE) != 0
  643. && (ahc->flags & AHC_TQINFIFO_BLOCKED) == 0) {
  644. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  645. ahc->shared_data_dmamap,
  646. ahc_targetcmd_offset(ahc, ahc->tqinfifofnext),
  647. /*len*/sizeof(struct target_cmd),
  648. BUS_DMASYNC_POSTREAD);
  649. if (ahc->targetcmds[ahc->tqinfifonext].cmd_valid != 0)
  650. retval |= AHC_RUN_TQINFIFO;
  651. }
  652. #endif
  653. return (retval);
  654. }
  655. /*
  656. * Catch an interrupt from the adapter
  657. */
  658. int
  659. ahc_intr(struct ahc_softc *ahc)
  660. {
  661. u_int intstat;
  662. if ((ahc->pause & INTEN) == 0) {
  663. /*
  664. * Our interrupt is not enabled on the chip
  665. * and may be disabled for re-entrancy reasons,
  666. * so just return. This is likely just a shared
  667. * interrupt.
  668. */
  669. return (0);
  670. }
  671. /*
  672. * Instead of directly reading the interrupt status register,
  673. * infer the cause of the interrupt by checking our in-core
  674. * completion queues. This avoids a costly PCI bus read in
  675. * most cases.
  676. */
  677. if ((ahc->flags & (AHC_ALL_INTERRUPTS|AHC_EDGE_INTERRUPT)) == 0
  678. && (ahc_check_cmdcmpltqueues(ahc) != 0))
  679. intstat = CMDCMPLT;
  680. else {
  681. intstat = ahc_inb(ahc, INTSTAT);
  682. }
  683. if ((intstat & INT_PEND) == 0) {
  684. #if AHC_PCI_CONFIG > 0
  685. if (ahc->unsolicited_ints > 500) {
  686. ahc->unsolicited_ints = 0;
  687. if ((ahc->chip & AHC_PCI) != 0
  688. && (ahc_inb(ahc, ERROR) & PCIERRSTAT) != 0)
  689. ahc->bus_intr(ahc);
  690. }
  691. #endif
  692. ahc->unsolicited_ints++;
  693. return (0);
  694. }
  695. ahc->unsolicited_ints = 0;
  696. if (intstat & CMDCMPLT) {
  697. ahc_outb(ahc, CLRINT, CLRCMDINT);
  698. /*
  699. * Ensure that the chip sees that we've cleared
  700. * this interrupt before we walk the output fifo.
  701. * Otherwise, we may, due to posted bus writes,
  702. * clear the interrupt after we finish the scan,
  703. * and after the sequencer has added new entries
  704. * and asserted the interrupt again.
  705. */
  706. ahc_flush_device_writes(ahc);
  707. ahc_run_qoutfifo(ahc);
  708. #ifdef AHC_TARGET_MODE
  709. if ((ahc->flags & AHC_TARGETROLE) != 0)
  710. ahc_run_tqinfifo(ahc, /*paused*/FALSE);
  711. #endif
  712. }
  713. /*
  714. * Handle statuses that may invalidate our cached
  715. * copy of INTSTAT separately.
  716. */
  717. if (intstat == 0xFF && (ahc->features & AHC_REMOVABLE) != 0) {
  718. /* Hot eject. Do nothing */
  719. } else if (intstat & BRKADRINT) {
  720. ahc_handle_brkadrint(ahc);
  721. } else if ((intstat & (SEQINT|SCSIINT)) != 0) {
  722. ahc_pause_bug_fix(ahc);
  723. if ((intstat & SEQINT) != 0)
  724. ahc_handle_seqint(ahc, intstat);
  725. if ((intstat & SCSIINT) != 0)
  726. ahc_handle_scsiint(ahc, intstat);
  727. }
  728. return (1);
  729. }
  730. /************************* Sequencer Execution Control ************************/
  731. /*
  732. * Restart the sequencer program from address zero
  733. */
  734. static void
  735. ahc_restart(struct ahc_softc *ahc)
  736. {
  737. ahc_pause(ahc);
  738. /* No more pending messages. */
  739. ahc_clear_msg_state(ahc);
  740. ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
  741. ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
  742. ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
  743. ahc_outb(ahc, LASTPHASE, P_BUSFREE);
  744. ahc_outb(ahc, SAVED_SCSIID, 0xFF);
  745. ahc_outb(ahc, SAVED_LUN, 0xFF);
  746. /*
  747. * Ensure that the sequencer's idea of TQINPOS
  748. * matches our own. The sequencer increments TQINPOS
  749. * only after it sees a DMA complete and a reset could
  750. * occur before the increment leaving the kernel to believe
  751. * the command arrived but the sequencer to not.
  752. */
  753. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  754. /* Always allow reselection */
  755. ahc_outb(ahc, SCSISEQ,
  756. ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  757. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  758. /* Ensure that no DMA operations are in progress */
  759. ahc_outb(ahc, CCSCBCNT, 0);
  760. ahc_outb(ahc, CCSGCTL, 0);
  761. ahc_outb(ahc, CCSCBCTL, 0);
  762. }
  763. /*
  764. * If we were in the process of DMA'ing SCB data into
  765. * an SCB, replace that SCB on the free list. This prevents
  766. * an SCB leak.
  767. */
  768. if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
  769. ahc_add_curscb_to_free_list(ahc);
  770. ahc_outb(ahc, SEQ_FLAGS2,
  771. ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
  772. }
  773. /*
  774. * Clear any pending sequencer interrupt. It is no
  775. * longer relevant since we're resetting the Program
  776. * Counter.
  777. */
  778. ahc_outb(ahc, CLRINT, CLRSEQINT);
  779. ahc_outb(ahc, MWI_RESIDUAL, 0);
  780. ahc_outb(ahc, SEQCTL, ahc->seqctl);
  781. ahc_outb(ahc, SEQADDR0, 0);
  782. ahc_outb(ahc, SEQADDR1, 0);
  783. ahc_unpause(ahc);
  784. }
  785. /************************* Input/Output Queues ********************************/
  786. static void
  787. ahc_run_qoutfifo(struct ahc_softc *ahc)
  788. {
  789. struct scb *scb;
  790. u_int scb_index;
  791. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  792. while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
  793. scb_index = ahc->qoutfifo[ahc->qoutfifonext];
  794. if ((ahc->qoutfifonext & 0x03) == 0x03) {
  795. u_int modnext;
  796. /*
  797. * Clear 32bits of QOUTFIFO at a time
  798. * so that we don't clobber an incoming
  799. * byte DMA to the array on architectures
  800. * that only support 32bit load and store
  801. * operations.
  802. */
  803. modnext = ahc->qoutfifonext & ~0x3;
  804. *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
  805. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  806. ahc->shared_data_dmamap,
  807. /*offset*/modnext, /*len*/4,
  808. BUS_DMASYNC_PREREAD);
  809. }
  810. ahc->qoutfifonext++;
  811. scb = ahc_lookup_scb(ahc, scb_index);
  812. if (scb == NULL) {
  813. printf("%s: WARNING no command for scb %d "
  814. "(cmdcmplt)\nQOUTPOS = %d\n",
  815. ahc_name(ahc), scb_index,
  816. (ahc->qoutfifonext - 1) & 0xFF);
  817. continue;
  818. }
  819. /*
  820. * Save off the residual
  821. * if there is one.
  822. */
  823. ahc_update_residual(ahc, scb);
  824. ahc_done(ahc, scb);
  825. }
  826. }
  827. static void
  828. ahc_run_untagged_queues(struct ahc_softc *ahc)
  829. {
  830. int i;
  831. for (i = 0; i < 16; i++)
  832. ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
  833. }
  834. static void
  835. ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
  836. {
  837. struct scb *scb;
  838. if (ahc->untagged_queue_lock != 0)
  839. return;
  840. if ((scb = TAILQ_FIRST(queue)) != NULL
  841. && (scb->flags & SCB_ACTIVE) == 0) {
  842. scb->flags |= SCB_ACTIVE;
  843. ahc_queue_scb(ahc, scb);
  844. }
  845. }
  846. /************************* Interrupt Handling *********************************/
  847. static void
  848. ahc_handle_brkadrint(struct ahc_softc *ahc)
  849. {
  850. /*
  851. * We upset the sequencer :-(
  852. * Lookup the error message
  853. */
  854. int i;
  855. int error;
  856. error = ahc_inb(ahc, ERROR);
  857. for (i = 0; error != 1 && i < num_errors; i++)
  858. error >>= 1;
  859. printf("%s: brkadrint, %s at seqaddr = 0x%x\n",
  860. ahc_name(ahc), ahc_hard_errors[i].errmesg,
  861. ahc_inb(ahc, SEQADDR0) |
  862. (ahc_inb(ahc, SEQADDR1) << 8));
  863. ahc_dump_card_state(ahc);
  864. /* Tell everyone that this HBA is no longer available */
  865. ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  866. CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  867. CAM_NO_HBA);
  868. /* Disable all interrupt sources by resetting the controller */
  869. ahc_shutdown(ahc);
  870. }
  871. static void
  872. ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
  873. {
  874. struct scb *scb;
  875. struct ahc_devinfo devinfo;
  876. ahc_fetch_devinfo(ahc, &devinfo);
  877. /*
  878. * Clear the upper byte that holds SEQINT status
  879. * codes and clear the SEQINT bit. We will unpause
  880. * the sequencer, if appropriate, after servicing
  881. * the request.
  882. */
  883. ahc_outb(ahc, CLRINT, CLRSEQINT);
  884. switch (intstat & SEQINT_MASK) {
  885. case BAD_STATUS:
  886. {
  887. u_int scb_index;
  888. struct hardware_scb *hscb;
  889. /*
  890. * Set the default return value to 0 (don't
  891. * send sense). The sense code will change
  892. * this if needed.
  893. */
  894. ahc_outb(ahc, RETURN_1, 0);
  895. /*
  896. * The sequencer will notify us when a command
  897. * has an error that would be of interest to
  898. * the kernel. This allows us to leave the sequencer
  899. * running in the common case of command completes
  900. * without error. The sequencer will already have
  901. * dma'd the SCB back up to us, so we can reference
  902. * the in kernel copy directly.
  903. */
  904. scb_index = ahc_inb(ahc, SCB_TAG);
  905. scb = ahc_lookup_scb(ahc, scb_index);
  906. if (scb == NULL) {
  907. ahc_print_devinfo(ahc, &devinfo);
  908. printf("ahc_intr - referenced scb "
  909. "not valid during seqint 0x%x scb(%d)\n",
  910. intstat, scb_index);
  911. ahc_dump_card_state(ahc);
  912. panic("for safety");
  913. goto unpause;
  914. }
  915. hscb = scb->hscb;
  916. /* Don't want to clobber the original sense code */
  917. if ((scb->flags & SCB_SENSE) != 0) {
  918. /*
  919. * Clear the SCB_SENSE Flag and have
  920. * the sequencer do a normal command
  921. * complete.
  922. */
  923. scb->flags &= ~SCB_SENSE;
  924. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  925. break;
  926. }
  927. ahc_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  928. /* Freeze the queue until the client sees the error. */
  929. ahc_freeze_devq(ahc, scb);
  930. ahc_freeze_scb(scb);
  931. ahc_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
  932. switch (hscb->shared_data.status.scsi_status) {
  933. case SCSI_STATUS_OK:
  934. printf("%s: Interrupted for staus of 0???\n",
  935. ahc_name(ahc));
  936. break;
  937. case SCSI_STATUS_CMD_TERMINATED:
  938. case SCSI_STATUS_CHECK_COND:
  939. {
  940. struct ahc_dma_seg *sg;
  941. struct scsi_sense *sc;
  942. struct ahc_initiator_tinfo *targ_info;
  943. struct ahc_tmode_tstate *tstate;
  944. struct ahc_transinfo *tinfo;
  945. #ifdef AHC_DEBUG
  946. if (ahc_debug & AHC_SHOW_SENSE) {
  947. ahc_print_path(ahc, scb);
  948. printf("SCB %d: requests Check Status\n",
  949. scb->hscb->tag);
  950. }
  951. #endif
  952. if (ahc_perform_autosense(scb) == 0)
  953. break;
  954. targ_info = ahc_fetch_transinfo(ahc,
  955. devinfo.channel,
  956. devinfo.our_scsiid,
  957. devinfo.target,
  958. &tstate);
  959. tinfo = &targ_info->curr;
  960. sg = scb->sg_list;
  961. sc = (struct scsi_sense *)(&hscb->shared_data.cdb);
  962. /*
  963. * Save off the residual if there is one.
  964. */
  965. ahc_update_residual(ahc, scb);
  966. #ifdef AHC_DEBUG
  967. if (ahc_debug & AHC_SHOW_SENSE) {
  968. ahc_print_path(ahc, scb);
  969. printf("Sending Sense\n");
  970. }
  971. #endif
  972. sg->addr = ahc_get_sense_bufaddr(ahc, scb);
  973. sg->len = ahc_get_sense_bufsize(ahc, scb);
  974. sg->len |= AHC_DMA_LAST_SEG;
  975. /* Fixup byte order */
  976. sg->addr = ahc_htole32(sg->addr);
  977. sg->len = ahc_htole32(sg->len);
  978. sc->opcode = REQUEST_SENSE;
  979. sc->byte2 = 0;
  980. if (tinfo->protocol_version <= SCSI_REV_2
  981. && SCB_GET_LUN(scb) < 8)
  982. sc->byte2 = SCB_GET_LUN(scb) << 5;
  983. sc->unused[0] = 0;
  984. sc->unused[1] = 0;
  985. sc->length = sg->len;
  986. sc->control = 0;
  987. /*
  988. * We can't allow the target to disconnect.
  989. * This will be an untagged transaction and
  990. * having the target disconnect will make this
  991. * transaction indestinguishable from outstanding
  992. * tagged transactions.
  993. */
  994. hscb->control = 0;
  995. /*
  996. * This request sense could be because the
  997. * the device lost power or in some other
  998. * way has lost our transfer negotiations.
  999. * Renegotiate if appropriate. Unit attention
  1000. * errors will be reported before any data
  1001. * phases occur.
  1002. */
  1003. if (ahc_get_residual(scb)
  1004. == ahc_get_transfer_length(scb)) {
  1005. ahc_update_neg_request(ahc, &devinfo,
  1006. tstate, targ_info,
  1007. AHC_NEG_IF_NON_ASYNC);
  1008. }
  1009. if (tstate->auto_negotiate & devinfo.target_mask) {
  1010. hscb->control |= MK_MESSAGE;
  1011. scb->flags &= ~SCB_NEGOTIATE;
  1012. scb->flags |= SCB_AUTO_NEGOTIATE;
  1013. }
  1014. hscb->cdb_len = sizeof(*sc);
  1015. hscb->dataptr = sg->addr;
  1016. hscb->datacnt = sg->len;
  1017. hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
  1018. hscb->sgptr = ahc_htole32(hscb->sgptr);
  1019. scb->sg_count = 1;
  1020. scb->flags |= SCB_SENSE;
  1021. ahc_qinfifo_requeue_tail(ahc, scb);
  1022. ahc_outb(ahc, RETURN_1, SEND_SENSE);
  1023. /*
  1024. * Ensure we have enough time to actually
  1025. * retrieve the sense.
  1026. */
  1027. ahc_scb_timer_reset(scb, 5 * 1000000);
  1028. break;
  1029. }
  1030. default:
  1031. break;
  1032. }
  1033. break;
  1034. }
  1035. case NO_MATCH:
  1036. {
  1037. /* Ensure we don't leave the selection hardware on */
  1038. ahc_outb(ahc, SCSISEQ,
  1039. ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  1040. printf("%s:%c:%d: no active SCB for reconnecting "
  1041. "target - issuing BUS DEVICE RESET\n",
  1042. ahc_name(ahc), devinfo.channel, devinfo.target);
  1043. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  1044. "ARG_1 == 0x%x ACCUM = 0x%x\n",
  1045. ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  1046. ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  1047. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  1048. "SINDEX == 0x%x\n",
  1049. ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  1050. ahc_index_busy_tcl(ahc,
  1051. BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  1052. ahc_inb(ahc, SAVED_LUN))),
  1053. ahc_inb(ahc, SINDEX));
  1054. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  1055. "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
  1056. ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  1057. ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  1058. ahc_inb(ahc, SCB_CONTROL));
  1059. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
  1060. ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  1061. printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0));
  1062. printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL));
  1063. ahc_dump_card_state(ahc);
  1064. ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
  1065. ahc->msgout_len = 1;
  1066. ahc->msgout_index = 0;
  1067. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  1068. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  1069. ahc_assert_atn(ahc);
  1070. break;
  1071. }
  1072. case SEND_REJECT:
  1073. {
  1074. u_int rejbyte = ahc_inb(ahc, ACCUM);
  1075. printf("%s:%c:%d: Warning - unknown message received from "
  1076. "target (0x%x). Rejecting\n",
  1077. ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
  1078. break;
  1079. }
  1080. case PROTO_VIOLATION:
  1081. {
  1082. ahc_handle_proto_violation(ahc);
  1083. break;
  1084. }
  1085. case IGN_WIDE_RES:
  1086. ahc_handle_ign_wide_residue(ahc, &devinfo);
  1087. break;
  1088. case PDATA_REINIT:
  1089. ahc_reinitialize_dataptrs(ahc);
  1090. break;
  1091. case BAD_PHASE:
  1092. {
  1093. u_int lastphase;
  1094. lastphase = ahc_inb(ahc, LASTPHASE);
  1095. printf("%s:%c:%d: unknown scsi bus phase %x, "
  1096. "lastphase = 0x%x. Attempting to continue\n",
  1097. ahc_name(ahc), devinfo.channel, devinfo.target,
  1098. lastphase, ahc_inb(ahc, SCSISIGI));
  1099. break;
  1100. }
  1101. case MISSED_BUSFREE:
  1102. {
  1103. u_int lastphase;
  1104. lastphase = ahc_inb(ahc, LASTPHASE);
  1105. printf("%s:%c:%d: Missed busfree. "
  1106. "Lastphase = 0x%x, Curphase = 0x%x\n",
  1107. ahc_name(ahc), devinfo.channel, devinfo.target,
  1108. lastphase, ahc_inb(ahc, SCSISIGI));
  1109. ahc_restart(ahc);
  1110. return;
  1111. }
  1112. case HOST_MSG_LOOP:
  1113. {
  1114. /*
  1115. * The sequencer has encountered a message phase
  1116. * that requires host assistance for completion.
  1117. * While handling the message phase(s), we will be
  1118. * notified by the sequencer after each byte is
  1119. * transfered so we can track bus phase changes.
  1120. *
  1121. * If this is the first time we've seen a HOST_MSG_LOOP
  1122. * interrupt, initialize the state of the host message
  1123. * loop.
  1124. */
  1125. if (ahc->msg_type == MSG_TYPE_NONE) {
  1126. struct scb *scb;
  1127. u_int scb_index;
  1128. u_int bus_phase;
  1129. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  1130. if (bus_phase != P_MESGIN
  1131. && bus_phase != P_MESGOUT) {
  1132. printf("ahc_intr: HOST_MSG_LOOP bad "
  1133. "phase 0x%x\n",
  1134. bus_phase);
  1135. /*
  1136. * Probably transitioned to bus free before
  1137. * we got here. Just punt the message.
  1138. */
  1139. ahc_clear_intstat(ahc);
  1140. ahc_restart(ahc);
  1141. return;
  1142. }
  1143. scb_index = ahc_inb(ahc, SCB_TAG);
  1144. scb = ahc_lookup_scb(ahc, scb_index);
  1145. if (devinfo.role == ROLE_INITIATOR) {
  1146. if (bus_phase == P_MESGOUT) {
  1147. if (scb == NULL)
  1148. panic("HOST_MSG_LOOP with "
  1149. "invalid SCB %x\n",
  1150. scb_index);
  1151. ahc_setup_initiator_msgout(ahc,
  1152. &devinfo,
  1153. scb);
  1154. } else {
  1155. ahc->msg_type =
  1156. MSG_TYPE_INITIATOR_MSGIN;
  1157. ahc->msgin_index = 0;
  1158. }
  1159. }
  1160. #ifdef AHC_TARGET_MODE
  1161. else {
  1162. if (bus_phase == P_MESGOUT) {
  1163. ahc->msg_type =
  1164. MSG_TYPE_TARGET_MSGOUT;
  1165. ahc->msgin_index = 0;
  1166. }
  1167. else
  1168. ahc_setup_target_msgin(ahc,
  1169. &devinfo,
  1170. scb);
  1171. }
  1172. #endif
  1173. }
  1174. ahc_handle_message_phase(ahc);
  1175. break;
  1176. }
  1177. case PERR_DETECTED:
  1178. {
  1179. /*
  1180. * If we've cleared the parity error interrupt
  1181. * but the sequencer still believes that SCSIPERR
  1182. * is true, it must be that the parity error is
  1183. * for the currently presented byte on the bus,
  1184. * and we are not in a phase (data-in) where we will
  1185. * eventually ack this byte. Ack the byte and
  1186. * throw it away in the hope that the target will
  1187. * take us to message out to deliver the appropriate
  1188. * error message.
  1189. */
  1190. if ((intstat & SCSIINT) == 0
  1191. && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
  1192. if ((ahc->features & AHC_DT) == 0) {
  1193. u_int curphase;
  1194. /*
  1195. * The hardware will only let you ack bytes
  1196. * if the expected phase in SCSISIGO matches
  1197. * the current phase. Make sure this is
  1198. * currently the case.
  1199. */
  1200. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  1201. ahc_outb(ahc, LASTPHASE, curphase);
  1202. ahc_outb(ahc, SCSISIGO, curphase);
  1203. }
  1204. if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) {
  1205. int wait;
  1206. /*
  1207. * In a data phase. Faster to bitbucket
  1208. * the data than to individually ack each
  1209. * byte. This is also the only strategy
  1210. * that will work with AUTOACK enabled.
  1211. */
  1212. ahc_outb(ahc, SXFRCTL1,
  1213. ahc_inb(ahc, SXFRCTL1) | BITBUCKET);
  1214. wait = 5000;
  1215. while (--wait != 0) {
  1216. if ((ahc_inb(ahc, SCSISIGI)
  1217. & (CDI|MSGI)) != 0)
  1218. break;
  1219. ahc_delay(100);
  1220. }
  1221. ahc_outb(ahc, SXFRCTL1,
  1222. ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
  1223. if (wait == 0) {
  1224. struct scb *scb;
  1225. u_int scb_index;
  1226. ahc_print_devinfo(ahc, &devinfo);
  1227. printf("Unable to clear parity error. "
  1228. "Resetting bus.\n");
  1229. scb_index = ahc_inb(ahc, SCB_TAG);
  1230. scb = ahc_lookup_scb(ahc, scb_index);
  1231. if (scb != NULL)
  1232. ahc_set_transaction_status(scb,
  1233. CAM_UNCOR_PARITY);
  1234. ahc_reset_channel(ahc, devinfo.channel,
  1235. /*init reset*/TRUE);
  1236. }
  1237. } else {
  1238. ahc_inb(ahc, SCSIDATL);
  1239. }
  1240. }
  1241. break;
  1242. }
  1243. case DATA_OVERRUN:
  1244. {
  1245. /*
  1246. * When the sequencer detects an overrun, it
  1247. * places the controller in "BITBUCKET" mode
  1248. * and allows the target to complete its transfer.
  1249. * Unfortunately, none of the counters get updated
  1250. * when the controller is in this mode, so we have
  1251. * no way of knowing how large the overrun was.
  1252. */
  1253. u_int scbindex = ahc_inb(ahc, SCB_TAG);
  1254. u_int lastphase = ahc_inb(ahc, LASTPHASE);
  1255. u_int i;
  1256. scb = ahc_lookup_scb(ahc, scbindex);
  1257. for (i = 0; i < num_phases; i++) {
  1258. if (lastphase == ahc_phase_table[i].phase)
  1259. break;
  1260. }
  1261. ahc_print_path(ahc, scb);
  1262. printf("data overrun detected %s."
  1263. " Tag == 0x%x.\n",
  1264. ahc_phase_table[i].phasemsg,
  1265. scb->hscb->tag);
  1266. ahc_print_path(ahc, scb);
  1267. printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
  1268. ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
  1269. ahc_get_transfer_length(scb), scb->sg_count);
  1270. if (scb->sg_count > 0) {
  1271. for (i = 0; i < scb->sg_count; i++) {
  1272. printf("sg[%d] - Addr 0x%x%x : Length %d\n",
  1273. i,
  1274. (ahc_le32toh(scb->sg_list[i].len) >> 24
  1275. & SG_HIGH_ADDR_BITS),
  1276. ahc_le32toh(scb->sg_list[i].addr),
  1277. ahc_le32toh(scb->sg_list[i].len)
  1278. & AHC_SG_LEN_MASK);
  1279. }
  1280. }
  1281. /*
  1282. * Set this and it will take effect when the
  1283. * target does a command complete.
  1284. */
  1285. ahc_freeze_devq(ahc, scb);
  1286. if ((scb->flags & SCB_SENSE) == 0) {
  1287. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  1288. } else {
  1289. scb->flags &= ~SCB_SENSE;
  1290. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  1291. }
  1292. ahc_freeze_scb(scb);
  1293. if ((ahc->features & AHC_ULTRA2) != 0) {
  1294. /*
  1295. * Clear the channel in case we return
  1296. * to data phase later.
  1297. */
  1298. ahc_outb(ahc, SXFRCTL0,
  1299. ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  1300. ahc_outb(ahc, SXFRCTL0,
  1301. ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  1302. }
  1303. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  1304. u_int dscommand1;
  1305. /* Ensure HHADDR is 0 for future DMA operations. */
  1306. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  1307. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  1308. ahc_outb(ahc, HADDR, 0);
  1309. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  1310. }
  1311. break;
  1312. }
  1313. case MKMSG_FAILED:
  1314. {
  1315. u_int scbindex;
  1316. printf("%s:%c:%d:%d: Attempt to issue message failed\n",
  1317. ahc_name(ahc), devinfo.channel, devinfo.target,
  1318. devinfo.lun);
  1319. scbindex = ahc_inb(ahc, SCB_TAG);
  1320. scb = ahc_lookup_scb(ahc, scbindex);
  1321. if (scb != NULL
  1322. && (scb->flags & SCB_RECOVERY_SCB) != 0)
  1323. /*
  1324. * Ensure that we didn't put a second instance of this
  1325. * SCB into the QINFIFO.
  1326. */
  1327. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  1328. SCB_GET_CHANNEL(ahc, scb),
  1329. SCB_GET_LUN(scb), scb->hscb->tag,
  1330. ROLE_INITIATOR, /*status*/0,
  1331. SEARCH_REMOVE);
  1332. break;
  1333. }
  1334. case NO_FREE_SCB:
  1335. {
  1336. printf("%s: No free or disconnected SCBs\n", ahc_name(ahc));
  1337. ahc_dump_card_state(ahc);
  1338. panic("for safety");
  1339. break;
  1340. }
  1341. case SCB_MISMATCH:
  1342. {
  1343. u_int scbptr;
  1344. scbptr = ahc_inb(ahc, SCBPTR);
  1345. printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
  1346. scbptr, ahc_inb(ahc, ARG_1),
  1347. ahc->scb_data->hscbs[scbptr].tag);
  1348. ahc_dump_card_state(ahc);
  1349. panic("for saftey");
  1350. break;
  1351. }
  1352. case OUT_OF_RANGE:
  1353. {
  1354. printf("%s: BTT calculation out of range\n", ahc_name(ahc));
  1355. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  1356. "ARG_1 == 0x%x ACCUM = 0x%x\n",
  1357. ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  1358. ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  1359. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  1360. "SINDEX == 0x%x\n, A == 0x%x\n",
  1361. ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  1362. ahc_index_busy_tcl(ahc,
  1363. BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  1364. ahc_inb(ahc, SAVED_LUN))),
  1365. ahc_inb(ahc, SINDEX),
  1366. ahc_inb(ahc, ACCUM));
  1367. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  1368. "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
  1369. ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  1370. ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  1371. ahc_inb(ahc, SCB_CONTROL));
  1372. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
  1373. ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  1374. ahc_dump_card_state(ahc);
  1375. panic("for safety");
  1376. break;
  1377. }
  1378. default:
  1379. printf("ahc_intr: seqint, "
  1380. "intstat == 0x%x, scsisigi = 0x%x\n",
  1381. intstat, ahc_inb(ahc, SCSISIGI));
  1382. break;
  1383. }
  1384. unpause:
  1385. /*
  1386. * The sequencer is paused immediately on
  1387. * a SEQINT, so we should restart it when
  1388. * we're done.
  1389. */
  1390. ahc_unpause(ahc);
  1391. }
  1392. static void
  1393. ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
  1394. {
  1395. u_int scb_index;
  1396. u_int status0;
  1397. u_int status;
  1398. struct scb *scb;
  1399. char cur_channel;
  1400. char intr_channel;
  1401. if ((ahc->features & AHC_TWIN) != 0
  1402. && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
  1403. cur_channel = 'B';
  1404. else
  1405. cur_channel = 'A';
  1406. intr_channel = cur_channel;
  1407. if ((ahc->features & AHC_ULTRA2) != 0)
  1408. status0 = ahc_inb(ahc, SSTAT0) & IOERR;
  1409. else
  1410. status0 = 0;
  1411. status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  1412. if (status == 0 && status0 == 0) {
  1413. if ((ahc->features & AHC_TWIN) != 0) {
  1414. /* Try the other channel */
  1415. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  1416. status = ahc_inb(ahc, SSTAT1)
  1417. & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  1418. intr_channel = (cur_channel == 'A') ? 'B' : 'A';
  1419. }
  1420. if (status == 0) {
  1421. printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
  1422. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1423. ahc_unpause(ahc);
  1424. return;
  1425. }
  1426. }
  1427. /* Make sure the sequencer is in a safe location. */
  1428. ahc_clear_critical_section(ahc);
  1429. scb_index = ahc_inb(ahc, SCB_TAG);
  1430. scb = ahc_lookup_scb(ahc, scb_index);
  1431. if (scb != NULL
  1432. && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
  1433. scb = NULL;
  1434. if ((ahc->features & AHC_ULTRA2) != 0
  1435. && (status0 & IOERR) != 0) {
  1436. int now_lvd;
  1437. now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
  1438. printf("%s: Transceiver State Has Changed to %s mode\n",
  1439. ahc_name(ahc), now_lvd ? "LVD" : "SE");
  1440. ahc_outb(ahc, CLRSINT0, CLRIOERR);
  1441. /*
  1442. * When transitioning to SE mode, the reset line
  1443. * glitches, triggering an arbitration bug in some
  1444. * Ultra2 controllers. This bug is cleared when we
  1445. * assert the reset line. Since a reset glitch has
  1446. * already occurred with this transition and a
  1447. * transceiver state change is handled just like
  1448. * a bus reset anyway, asserting the reset line
  1449. * ourselves is safe.
  1450. */
  1451. ahc_reset_channel(ahc, intr_channel,
  1452. /*Initiate Reset*/now_lvd == 0);
  1453. } else if ((status & SCSIRSTI) != 0) {
  1454. printf("%s: Someone reset channel %c\n",
  1455. ahc_name(ahc), intr_channel);
  1456. if (intr_channel != cur_channel)
  1457. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  1458. ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
  1459. } else if ((status & SCSIPERR) != 0) {
  1460. /*
  1461. * Determine the bus phase and queue an appropriate message.
  1462. * SCSIPERR is latched true as soon as a parity error
  1463. * occurs. If the sequencer acked the transfer that
  1464. * caused the parity error and the currently presented
  1465. * transfer on the bus has correct parity, SCSIPERR will
  1466. * be cleared by CLRSCSIPERR. Use this to determine if
  1467. * we should look at the last phase the sequencer recorded,
  1468. * or the current phase presented on the bus.
  1469. */
  1470. struct ahc_devinfo devinfo;
  1471. u_int mesg_out;
  1472. u_int curphase;
  1473. u_int errorphase;
  1474. u_int lastphase;
  1475. u_int scsirate;
  1476. u_int i;
  1477. u_int sstat2;
  1478. int silent;
  1479. lastphase = ahc_inb(ahc, LASTPHASE);
  1480. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  1481. sstat2 = ahc_inb(ahc, SSTAT2);
  1482. ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
  1483. /*
  1484. * For all phases save DATA, the sequencer won't
  1485. * automatically ack a byte that has a parity error
  1486. * in it. So the only way that the current phase
  1487. * could be 'data-in' is if the parity error is for
  1488. * an already acked byte in the data phase. During
  1489. * synchronous data-in transfers, we may actually
  1490. * ack bytes before latching the current phase in
  1491. * LASTPHASE, leading to the discrepancy between
  1492. * curphase and lastphase.
  1493. */
  1494. if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
  1495. || curphase == P_DATAIN || curphase == P_DATAIN_DT)
  1496. errorphase = curphase;
  1497. else
  1498. errorphase = lastphase;
  1499. for (i = 0; i < num_phases; i++) {
  1500. if (errorphase == ahc_phase_table[i].phase)
  1501. break;
  1502. }
  1503. mesg_out = ahc_phase_table[i].mesg_out;
  1504. silent = FALSE;
  1505. if (scb != NULL) {
  1506. if (SCB_IS_SILENT(scb))
  1507. silent = TRUE;
  1508. else
  1509. ahc_print_path(ahc, scb);
  1510. scb->flags |= SCB_TRANSMISSION_ERROR;
  1511. } else
  1512. printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
  1513. SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
  1514. scsirate = ahc_inb(ahc, SCSIRATE);
  1515. if (silent == FALSE) {
  1516. printf("parity error detected %s. "
  1517. "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
  1518. ahc_phase_table[i].phasemsg,
  1519. ahc_inw(ahc, SEQADDR0),
  1520. scsirate);
  1521. if ((ahc->features & AHC_DT) != 0) {
  1522. if ((sstat2 & CRCVALERR) != 0)
  1523. printf("\tCRC Value Mismatch\n");
  1524. if ((sstat2 & CRCENDERR) != 0)
  1525. printf("\tNo terminal CRC packet "
  1526. "recevied\n");
  1527. if ((sstat2 & CRCREQERR) != 0)
  1528. printf("\tIllegal CRC packet "
  1529. "request\n");
  1530. if ((sstat2 & DUAL_EDGE_ERR) != 0)
  1531. printf("\tUnexpected %sDT Data Phase\n",
  1532. (scsirate & SINGLE_EDGE)
  1533. ? "" : "non-");
  1534. }
  1535. }
  1536. if ((ahc->features & AHC_DT) != 0
  1537. && (sstat2 & DUAL_EDGE_ERR) != 0) {
  1538. /*
  1539. * This error applies regardless of
  1540. * data direction, so ignore the value
  1541. * in the phase table.
  1542. */
  1543. mesg_out = MSG_INITIATOR_DET_ERR;
  1544. }
  1545. /*
  1546. * We've set the hardware to assert ATN if we
  1547. * get a parity error on "in" phases, so all we
  1548. * need to do is stuff the message buffer with
  1549. * the appropriate message. "In" phases have set
  1550. * mesg_out to something other than MSG_NOP.
  1551. */
  1552. if (mesg_out != MSG_NOOP) {
  1553. if (ahc->msg_type != MSG_TYPE_NONE)
  1554. ahc->send_msg_perror = TRUE;
  1555. else
  1556. ahc_outb(ahc, MSG_OUT, mesg_out);
  1557. }
  1558. /*
  1559. * Force a renegotiation with this target just in
  1560. * case we are out of sync for some external reason
  1561. * unknown (or unreported) by the target.
  1562. */
  1563. ahc_fetch_devinfo(ahc, &devinfo);
  1564. ahc_force_renegotiation(ahc, &devinfo);
  1565. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1566. ahc_unpause(ahc);
  1567. } else if ((status & SELTO) != 0) {
  1568. u_int scbptr;
  1569. /* Stop the selection */
  1570. ahc_outb(ahc, SCSISEQ, 0);
  1571. /* No more pending messages */
  1572. ahc_clear_msg_state(ahc);
  1573. /* Clear interrupt state */
  1574. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1575. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1576. /*
  1577. * Although the driver does not care about the
  1578. * 'Selection in Progress' status bit, the busy
  1579. * LED does. SELINGO is only cleared by a sucessfull
  1580. * selection, so we must manually clear it to insure
  1581. * the LED turns off just incase no future successful
  1582. * selections occur (e.g. no devices on the bus).
  1583. */
  1584. ahc_outb(ahc, CLRSINT0, CLRSELINGO);
  1585. scbptr = ahc_inb(ahc, WAITING_SCBH);
  1586. ahc_outb(ahc, SCBPTR, scbptr);
  1587. scb_index = ahc_inb(ahc, SCB_TAG);
  1588. scb = ahc_lookup_scb(ahc, scb_index);
  1589. if (scb == NULL) {
  1590. printf("%s: ahc_intr - referenced scb not "
  1591. "valid during SELTO scb(%d, %d)\n",
  1592. ahc_name(ahc), scbptr, scb_index);
  1593. ahc_dump_card_state(ahc);
  1594. } else {
  1595. struct ahc_devinfo devinfo;
  1596. #ifdef AHC_DEBUG
  1597. if ((ahc_debug & AHC_SHOW_SELTO) != 0) {
  1598. ahc_print_path(ahc, scb);
  1599. printf("Saw Selection Timeout for SCB 0x%x\n",
  1600. scb_index);
  1601. }
  1602. #endif
  1603. ahc_scb_devinfo(ahc, &devinfo, scb);
  1604. ahc_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1605. ahc_freeze_devq(ahc, scb);
  1606. /*
  1607. * Cancel any pending transactions on the device
  1608. * now that it seems to be missing. This will
  1609. * also revert us to async/narrow transfers until
  1610. * we can renegotiate with the device.
  1611. */
  1612. ahc_handle_devreset(ahc, &devinfo,
  1613. CAM_SEL_TIMEOUT,
  1614. "Selection Timeout",
  1615. /*verbose_level*/1);
  1616. }
  1617. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1618. ahc_restart(ahc);
  1619. } else if ((status & BUSFREE) != 0
  1620. && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
  1621. struct ahc_devinfo devinfo;
  1622. u_int lastphase;
  1623. u_int saved_scsiid;
  1624. u_int saved_lun;
  1625. u_int target;
  1626. u_int initiator_role_id;
  1627. char channel;
  1628. int printerror;
  1629. /*
  1630. * Clear our selection hardware as soon as possible.
  1631. * We may have an entry in the waiting Q for this target,
  1632. * that is affected by this busfree and we don't want to
  1633. * go about selecting the target while we handle the event.
  1634. */
  1635. ahc_outb(ahc, SCSISEQ,
  1636. ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  1637. /*
  1638. * Disable busfree interrupts and clear the busfree
  1639. * interrupt status. We do this here so that several
  1640. * bus transactions occur prior to clearing the SCSIINT
  1641. * latch. It can take a bit for the clearing to take effect.
  1642. */
  1643. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1644. ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
  1645. /*
  1646. * Look at what phase we were last in.
  1647. * If its message out, chances are pretty good
  1648. * that the busfree was in response to one of
  1649. * our abort requests.
  1650. */
  1651. lastphase = ahc_inb(ahc, LASTPHASE);
  1652. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  1653. saved_lun = ahc_inb(ahc, SAVED_LUN);
  1654. target = SCSIID_TARGET(ahc, saved_scsiid);
  1655. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  1656. channel = SCSIID_CHANNEL(ahc, saved_scsiid);
  1657. ahc_compile_devinfo(&devinfo, initiator_role_id,
  1658. target, saved_lun, channel, ROLE_INITIATOR);
  1659. printerror = 1;
  1660. if (lastphase == P_MESGOUT) {
  1661. u_int tag;
  1662. tag = SCB_LIST_NULL;
  1663. if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
  1664. || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
  1665. if (ahc->msgout_buf[ahc->msgout_index - 1]
  1666. == MSG_ABORT_TAG)
  1667. tag = scb->hscb->tag;
  1668. ahc_print_path(ahc, scb);
  1669. printf("SCB %d - Abort%s Completed.\n",
  1670. scb->hscb->tag, tag == SCB_LIST_NULL ?
  1671. "" : " Tag");
  1672. ahc_abort_scbs(ahc, target, channel,
  1673. saved_lun, tag,
  1674. ROLE_INITIATOR,
  1675. CAM_REQ_ABORTED);
  1676. printerror = 0;
  1677. } else if (ahc_sent_msg(ahc, AHCMSG_1B,
  1678. MSG_BUS_DEV_RESET, TRUE)) {
  1679. #ifdef __FreeBSD__
  1680. /*
  1681. * Don't mark the user's request for this BDR
  1682. * as completing with CAM_BDR_SENT. CAM3
  1683. * specifies CAM_REQ_CMP.
  1684. */
  1685. if (scb != NULL
  1686. && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  1687. && ahc_match_scb(ahc, scb, target, channel,
  1688. CAM_LUN_WILDCARD,
  1689. SCB_LIST_NULL,
  1690. ROLE_INITIATOR)) {
  1691. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1692. }
  1693. #endif
  1694. ahc_compile_devinfo(&devinfo,
  1695. initiator_role_id,
  1696. target,
  1697. CAM_LUN_WILDCARD,
  1698. channel,
  1699. ROLE_INITIATOR);
  1700. ahc_handle_devreset(ahc, &devinfo,
  1701. CAM_BDR_SENT,
  1702. "Bus Device Reset",
  1703. /*verbose_level*/0);
  1704. printerror = 0;
  1705. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1706. MSG_EXT_PPR, FALSE)) {
  1707. struct ahc_initiator_tinfo *tinfo;
  1708. struct ahc_tmode_tstate *tstate;
  1709. /*
  1710. * PPR Rejected. Try non-ppr negotiation
  1711. * and retry command.
  1712. */
  1713. tinfo = ahc_fetch_transinfo(ahc,
  1714. devinfo.channel,
  1715. devinfo.our_scsiid,
  1716. devinfo.target,
  1717. &tstate);
  1718. tinfo->curr.transport_version = 2;
  1719. tinfo->goal.transport_version = 2;
  1720. tinfo->goal.ppr_options = 0;
  1721. ahc_qinfifo_requeue_tail(ahc, scb);
  1722. printerror = 0;
  1723. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1724. MSG_EXT_WDTR, FALSE)) {
  1725. /*
  1726. * Negotiation Rejected. Go-narrow and
  1727. * retry command.
  1728. */
  1729. ahc_set_width(ahc, &devinfo,
  1730. MSG_EXT_WDTR_BUS_8_BIT,
  1731. AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1732. /*paused*/TRUE);
  1733. ahc_qinfifo_requeue_tail(ahc, scb);
  1734. printerror = 0;
  1735. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1736. MSG_EXT_SDTR, FALSE)) {
  1737. /*
  1738. * Negotiation Rejected. Go-async and
  1739. * retry command.
  1740. */
  1741. ahc_set_syncrate(ahc, &devinfo,
  1742. /*syncrate*/NULL,
  1743. /*period*/0, /*offset*/0,
  1744. /*ppr_options*/0,
  1745. AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1746. /*paused*/TRUE);
  1747. ahc_qinfifo_requeue_tail(ahc, scb);
  1748. printerror = 0;
  1749. }
  1750. }
  1751. if (printerror != 0) {
  1752. u_int i;
  1753. if (scb != NULL) {
  1754. u_int tag;
  1755. if ((scb->hscb->control & TAG_ENB) != 0)
  1756. tag = scb->hscb->tag;
  1757. else
  1758. tag = SCB_LIST_NULL;
  1759. ahc_print_path(ahc, scb);
  1760. ahc_abort_scbs(ahc, target, channel,
  1761. SCB_GET_LUN(scb), tag,
  1762. ROLE_INITIATOR,
  1763. CAM_UNEXP_BUSFREE);
  1764. } else {
  1765. /*
  1766. * We had not fully identified this connection,
  1767. * so we cannot abort anything.
  1768. */
  1769. printf("%s: ", ahc_name(ahc));
  1770. }
  1771. for (i = 0; i < num_phases; i++) {
  1772. if (lastphase == ahc_phase_table[i].phase)
  1773. break;
  1774. }
  1775. if (lastphase != P_BUSFREE) {
  1776. /*
  1777. * Renegotiate with this device at the
  1778. * next oportunity just in case this busfree
  1779. * is due to a negotiation mismatch with the
  1780. * device.
  1781. */
  1782. ahc_force_renegotiation(ahc, &devinfo);
  1783. }
  1784. printf("Unexpected busfree %s\n"
  1785. "SEQADDR == 0x%x\n",
  1786. ahc_phase_table[i].phasemsg,
  1787. ahc_inb(ahc, SEQADDR0)
  1788. | (ahc_inb(ahc, SEQADDR1) << 8));
  1789. }
  1790. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1791. ahc_restart(ahc);
  1792. } else {
  1793. printf("%s: Missing case in ahc_handle_scsiint. status = %x\n",
  1794. ahc_name(ahc), status);
  1795. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1796. }
  1797. }
  1798. /*
  1799. * Force renegotiation to occur the next time we initiate
  1800. * a command to the current device.
  1801. */
  1802. static void
  1803. ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1804. {
  1805. struct ahc_initiator_tinfo *targ_info;
  1806. struct ahc_tmode_tstate *tstate;
  1807. targ_info = ahc_fetch_transinfo(ahc,
  1808. devinfo->channel,
  1809. devinfo->our_scsiid,
  1810. devinfo->target,
  1811. &tstate);
  1812. ahc_update_neg_request(ahc, devinfo, tstate,
  1813. targ_info, AHC_NEG_IF_NON_ASYNC);
  1814. }
  1815. #define AHC_MAX_STEPS 2000
  1816. static void
  1817. ahc_clear_critical_section(struct ahc_softc *ahc)
  1818. {
  1819. int stepping;
  1820. int steps;
  1821. u_int simode0;
  1822. u_int simode1;
  1823. if (ahc->num_critical_sections == 0)
  1824. return;
  1825. stepping = FALSE;
  1826. steps = 0;
  1827. simode0 = 0;
  1828. simode1 = 0;
  1829. for (;;) {
  1830. struct cs *cs;
  1831. u_int seqaddr;
  1832. u_int i;
  1833. seqaddr = ahc_inb(ahc, SEQADDR0)
  1834. | (ahc_inb(ahc, SEQADDR1) << 8);
  1835. /*
  1836. * Seqaddr represents the next instruction to execute,
  1837. * so we are really executing the instruction just
  1838. * before it.
  1839. */
  1840. if (seqaddr != 0)
  1841. seqaddr -= 1;
  1842. cs = ahc->critical_sections;
  1843. for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
  1844. if (cs->begin < seqaddr && cs->end >= seqaddr)
  1845. break;
  1846. }
  1847. if (i == ahc->num_critical_sections)
  1848. break;
  1849. if (steps > AHC_MAX_STEPS) {
  1850. printf("%s: Infinite loop in critical section\n",
  1851. ahc_name(ahc));
  1852. ahc_dump_card_state(ahc);
  1853. panic("critical section loop");
  1854. }
  1855. steps++;
  1856. if (stepping == FALSE) {
  1857. /*
  1858. * Disable all interrupt sources so that the
  1859. * sequencer will not be stuck by a pausing
  1860. * interrupt condition while we attempt to
  1861. * leave a critical section.
  1862. */
  1863. simode0 = ahc_inb(ahc, SIMODE0);
  1864. ahc_outb(ahc, SIMODE0, 0);
  1865. simode1 = ahc_inb(ahc, SIMODE1);
  1866. if ((ahc->features & AHC_DT) != 0)
  1867. /*
  1868. * On DT class controllers, we
  1869. * use the enhanced busfree logic.
  1870. * Unfortunately we cannot re-enable
  1871. * busfree detection within the
  1872. * current connection, so we must
  1873. * leave it on while single stepping.
  1874. */
  1875. ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE);
  1876. else
  1877. ahc_outb(ahc, SIMODE1, 0);
  1878. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1879. ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP);
  1880. stepping = TRUE;
  1881. }
  1882. if ((ahc->features & AHC_DT) != 0) {
  1883. ahc_outb(ahc, CLRSINT1, CLRBUSFREE);
  1884. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1885. }
  1886. ahc_outb(ahc, HCNTRL, ahc->unpause);
  1887. while (!ahc_is_paused(ahc))
  1888. ahc_delay(200);
  1889. }
  1890. if (stepping) {
  1891. ahc_outb(ahc, SIMODE0, simode0);
  1892. ahc_outb(ahc, SIMODE1, simode1);
  1893. ahc_outb(ahc, SEQCTL, ahc->seqctl);
  1894. }
  1895. }
  1896. /*
  1897. * Clear any pending interrupt status.
  1898. */
  1899. static void
  1900. ahc_clear_intstat(struct ahc_softc *ahc)
  1901. {
  1902. /* Clear any interrupt conditions this may have caused */
  1903. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  1904. |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
  1905. CLRREQINIT);
  1906. ahc_flush_device_writes(ahc);
  1907. ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
  1908. ahc_flush_device_writes(ahc);
  1909. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1910. ahc_flush_device_writes(ahc);
  1911. }
  1912. /**************************** Debugging Routines ******************************/
  1913. #ifdef AHC_DEBUG
  1914. uint32_t ahc_debug = AHC_DEBUG_OPTS;
  1915. #endif
  1916. #if 0 /* unused */
  1917. static void
  1918. ahc_print_scb(struct scb *scb)
  1919. {
  1920. int i;
  1921. struct hardware_scb *hscb = scb->hscb;
  1922. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
  1923. (void *)scb,
  1924. hscb->control,
  1925. hscb->scsiid,
  1926. hscb->lun,
  1927. hscb->cdb_len);
  1928. printf("Shared Data: ");
  1929. for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
  1930. printf("%#02x", hscb->shared_data.cdb[i]);
  1931. printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
  1932. ahc_le32toh(hscb->dataptr),
  1933. ahc_le32toh(hscb->datacnt),
  1934. ahc_le32toh(hscb->sgptr),
  1935. hscb->tag);
  1936. if (scb->sg_count > 0) {
  1937. for (i = 0; i < scb->sg_count; i++) {
  1938. printf("sg[%d] - Addr 0x%x%x : Length %d\n",
  1939. i,
  1940. (ahc_le32toh(scb->sg_list[i].len) >> 24
  1941. & SG_HIGH_ADDR_BITS),
  1942. ahc_le32toh(scb->sg_list[i].addr),
  1943. ahc_le32toh(scb->sg_list[i].len));
  1944. }
  1945. }
  1946. }
  1947. #endif
  1948. /************************* Transfer Negotiation *******************************/
  1949. /*
  1950. * Allocate per target mode instance (ID we respond to as a target)
  1951. * transfer negotiation data structures.
  1952. */
  1953. static struct ahc_tmode_tstate *
  1954. ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
  1955. {
  1956. struct ahc_tmode_tstate *master_tstate;
  1957. struct ahc_tmode_tstate *tstate;
  1958. int i;
  1959. master_tstate = ahc->enabled_targets[ahc->our_id];
  1960. if (channel == 'B') {
  1961. scsi_id += 8;
  1962. master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
  1963. }
  1964. if (ahc->enabled_targets[scsi_id] != NULL
  1965. && ahc->enabled_targets[scsi_id] != master_tstate)
  1966. panic("%s: ahc_alloc_tstate - Target already allocated",
  1967. ahc_name(ahc));
  1968. tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate),
  1969. M_DEVBUF, M_NOWAIT);
  1970. if (tstate == NULL)
  1971. return (NULL);
  1972. /*
  1973. * If we have allocated a master tstate, copy user settings from
  1974. * the master tstate (taken from SRAM or the EEPROM) for this
  1975. * channel, but reset our current and goal settings to async/narrow
  1976. * until an initiator talks to us.
  1977. */
  1978. if (master_tstate != NULL) {
  1979. memcpy(tstate, master_tstate, sizeof(*tstate));
  1980. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  1981. tstate->ultraenb = 0;
  1982. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1983. memset(&tstate->transinfo[i].curr, 0,
  1984. sizeof(tstate->transinfo[i].curr));
  1985. memset(&tstate->transinfo[i].goal, 0,
  1986. sizeof(tstate->transinfo[i].goal));
  1987. }
  1988. } else
  1989. memset(tstate, 0, sizeof(*tstate));
  1990. ahc->enabled_targets[scsi_id] = tstate;
  1991. return (tstate);
  1992. }
  1993. #ifdef AHC_TARGET_MODE
  1994. /*
  1995. * Free per target mode instance (ID we respond to as a target)
  1996. * transfer negotiation data structures.
  1997. */
  1998. static void
  1999. ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
  2000. {
  2001. struct ahc_tmode_tstate *tstate;
  2002. /*
  2003. * Don't clean up our "master" tstate.
  2004. * It has our default user settings.
  2005. */
  2006. if (((channel == 'B' && scsi_id == ahc->our_id_b)
  2007. || (channel == 'A' && scsi_id == ahc->our_id))
  2008. && force == FALSE)
  2009. return;
  2010. if (channel == 'B')
  2011. scsi_id += 8;
  2012. tstate = ahc->enabled_targets[scsi_id];
  2013. if (tstate != NULL)
  2014. free(tstate, M_DEVBUF);
  2015. ahc->enabled_targets[scsi_id] = NULL;
  2016. }
  2017. #endif
  2018. /*
  2019. * Called when we have an active connection to a target on the bus,
  2020. * this function finds the nearest syncrate to the input period limited
  2021. * by the capabilities of the bus connectivity of and sync settings for
  2022. * the target.
  2023. */
  2024. const struct ahc_syncrate *
  2025. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  2026. struct ahc_initiator_tinfo *tinfo,
  2027. u_int *period, u_int *ppr_options, role_t role)
  2028. {
  2029. struct ahc_transinfo *transinfo;
  2030. u_int maxsync;
  2031. if ((ahc->features & AHC_ULTRA2) != 0) {
  2032. if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
  2033. && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
  2034. maxsync = AHC_SYNCRATE_DT;
  2035. } else {
  2036. maxsync = AHC_SYNCRATE_ULTRA;
  2037. /* Can't do DT on an SE bus */
  2038. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2039. }
  2040. } else if ((ahc->features & AHC_ULTRA) != 0) {
  2041. maxsync = AHC_SYNCRATE_ULTRA;
  2042. } else {
  2043. maxsync = AHC_SYNCRATE_FAST;
  2044. }
  2045. /*
  2046. * Never allow a value higher than our current goal
  2047. * period otherwise we may allow a target initiated
  2048. * negotiation to go above the limit as set by the
  2049. * user. In the case of an initiator initiated
  2050. * sync negotiation, we limit based on the user
  2051. * setting. This allows the system to still accept
  2052. * incoming negotiations even if target initiated
  2053. * negotiation is not performed.
  2054. */
  2055. if (role == ROLE_TARGET)
  2056. transinfo = &tinfo->user;
  2057. else
  2058. transinfo = &tinfo->goal;
  2059. *ppr_options &= transinfo->ppr_options;
  2060. if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
  2061. maxsync = max(maxsync, (u_int)AHC_SYNCRATE_ULTRA2);
  2062. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2063. }
  2064. if (transinfo->period == 0) {
  2065. *period = 0;
  2066. *ppr_options = 0;
  2067. return (NULL);
  2068. }
  2069. *period = max(*period, (u_int)transinfo->period);
  2070. return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
  2071. }
  2072. /*
  2073. * Look up the valid period to SCSIRATE conversion in our table.
  2074. * Return the period and offset that should be sent to the target
  2075. * if this was the beginning of an SDTR.
  2076. */
  2077. const struct ahc_syncrate *
  2078. ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
  2079. u_int *ppr_options, u_int maxsync)
  2080. {
  2081. const struct ahc_syncrate *syncrate;
  2082. if ((ahc->features & AHC_DT) == 0)
  2083. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2084. /* Skip all DT only entries if DT is not available */
  2085. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  2086. && maxsync < AHC_SYNCRATE_ULTRA2)
  2087. maxsync = AHC_SYNCRATE_ULTRA2;
  2088. /* Now set the maxsync based on the card capabilities
  2089. * DT is already done above */
  2090. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  2091. && maxsync < AHC_SYNCRATE_ULTRA)
  2092. maxsync = AHC_SYNCRATE_ULTRA;
  2093. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  2094. && maxsync < AHC_SYNCRATE_FAST)
  2095. maxsync = AHC_SYNCRATE_FAST;
  2096. for (syncrate = &ahc_syncrates[maxsync];
  2097. syncrate->rate != NULL;
  2098. syncrate++) {
  2099. /*
  2100. * The Ultra2 table doesn't go as low
  2101. * as for the Fast/Ultra cards.
  2102. */
  2103. if ((ahc->features & AHC_ULTRA2) != 0
  2104. && (syncrate->sxfr_u2 == 0))
  2105. break;
  2106. if (*period <= syncrate->period) {
  2107. /*
  2108. * When responding to a target that requests
  2109. * sync, the requested rate may fall between
  2110. * two rates that we can output, but still be
  2111. * a rate that we can receive. Because of this,
  2112. * we want to respond to the target with
  2113. * the same rate that it sent to us even
  2114. * if the period we use to send data to it
  2115. * is lower. Only lower the response period
  2116. * if we must.
  2117. */
  2118. if (syncrate == &ahc_syncrates[maxsync])
  2119. *period = syncrate->period;
  2120. /*
  2121. * At some speeds, we only support
  2122. * ST transfers.
  2123. */
  2124. if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
  2125. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2126. break;
  2127. }
  2128. }
  2129. if ((*period == 0)
  2130. || (syncrate->rate == NULL)
  2131. || ((ahc->features & AHC_ULTRA2) != 0
  2132. && (syncrate->sxfr_u2 == 0))) {
  2133. /* Use asynchronous transfers. */
  2134. *period = 0;
  2135. syncrate = NULL;
  2136. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  2137. }
  2138. return (syncrate);
  2139. }
  2140. /*
  2141. * Convert from an entry in our syncrate table to the SCSI equivalent
  2142. * sync "period" factor.
  2143. */
  2144. u_int
  2145. ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
  2146. {
  2147. const struct ahc_syncrate *syncrate;
  2148. if ((ahc->features & AHC_ULTRA2) != 0)
  2149. scsirate &= SXFR_ULTRA2;
  2150. else
  2151. scsirate &= SXFR;
  2152. /* now set maxsync based on card capabilities */
  2153. if ((ahc->features & AHC_DT) == 0 && maxsync < AHC_SYNCRATE_ULTRA2)
  2154. maxsync = AHC_SYNCRATE_ULTRA2;
  2155. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  2156. && maxsync < AHC_SYNCRATE_ULTRA)
  2157. maxsync = AHC_SYNCRATE_ULTRA;
  2158. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  2159. && maxsync < AHC_SYNCRATE_FAST)
  2160. maxsync = AHC_SYNCRATE_FAST;
  2161. syncrate = &ahc_syncrates[maxsync];
  2162. while (syncrate->rate != NULL) {
  2163. if ((ahc->features & AHC_ULTRA2) != 0) {
  2164. if (syncrate->sxfr_u2 == 0)
  2165. break;
  2166. else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
  2167. return (syncrate->period);
  2168. } else if (scsirate == (syncrate->sxfr & SXFR)) {
  2169. return (syncrate->period);
  2170. }
  2171. syncrate++;
  2172. }
  2173. return (0); /* async */
  2174. }
  2175. /*
  2176. * Truncate the given synchronous offset to a value the
  2177. * current adapter type and syncrate are capable of.
  2178. */
  2179. static void
  2180. ahc_validate_offset(struct ahc_softc *ahc,
  2181. struct ahc_initiator_tinfo *tinfo,
  2182. const struct ahc_syncrate *syncrate,
  2183. u_int *offset, int wide, role_t role)
  2184. {
  2185. u_int maxoffset;
  2186. /* Limit offset to what we can do */
  2187. if (syncrate == NULL) {
  2188. maxoffset = 0;
  2189. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  2190. maxoffset = MAX_OFFSET_ULTRA2;
  2191. } else {
  2192. if (wide)
  2193. maxoffset = MAX_OFFSET_16BIT;
  2194. else
  2195. maxoffset = MAX_OFFSET_8BIT;
  2196. }
  2197. *offset = min(*offset, maxoffset);
  2198. if (tinfo != NULL) {
  2199. if (role == ROLE_TARGET)
  2200. *offset = min(*offset, (u_int)tinfo->user.offset);
  2201. else
  2202. *offset = min(*offset, (u_int)tinfo->goal.offset);
  2203. }
  2204. }
  2205. /*
  2206. * Truncate the given transfer width parameter to a value the
  2207. * current adapter type is capable of.
  2208. */
  2209. static void
  2210. ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
  2211. u_int *bus_width, role_t role)
  2212. {
  2213. switch (*bus_width) {
  2214. default:
  2215. if (ahc->features & AHC_WIDE) {
  2216. /* Respond Wide */
  2217. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  2218. break;
  2219. }
  2220. /* FALLTHROUGH */
  2221. case MSG_EXT_WDTR_BUS_8_BIT:
  2222. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  2223. break;
  2224. }
  2225. if (tinfo != NULL) {
  2226. if (role == ROLE_TARGET)
  2227. *bus_width = min((u_int)tinfo->user.width, *bus_width);
  2228. else
  2229. *bus_width = min((u_int)tinfo->goal.width, *bus_width);
  2230. }
  2231. }
  2232. /*
  2233. * Update the bitmask of targets for which the controller should
  2234. * negotiate with at the next convenient oportunity. This currently
  2235. * means the next time we send the initial identify messages for
  2236. * a new transaction.
  2237. */
  2238. int
  2239. ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2240. struct ahc_tmode_tstate *tstate,
  2241. struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
  2242. {
  2243. u_int auto_negotiate_orig;
  2244. auto_negotiate_orig = tstate->auto_negotiate;
  2245. if (neg_type == AHC_NEG_ALWAYS) {
  2246. /*
  2247. * Force our "current" settings to be
  2248. * unknown so that unless a bus reset
  2249. * occurs the need to renegotiate is
  2250. * recorded persistently.
  2251. */
  2252. if ((ahc->features & AHC_WIDE) != 0)
  2253. tinfo->curr.width = AHC_WIDTH_UNKNOWN;
  2254. tinfo->curr.period = AHC_PERIOD_UNKNOWN;
  2255. tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
  2256. }
  2257. if (tinfo->curr.period != tinfo->goal.period
  2258. || tinfo->curr.width != tinfo->goal.width
  2259. || tinfo->curr.offset != tinfo->goal.offset
  2260. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  2261. || (neg_type == AHC_NEG_IF_NON_ASYNC
  2262. && (tinfo->goal.offset != 0
  2263. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  2264. || tinfo->goal.ppr_options != 0)))
  2265. tstate->auto_negotiate |= devinfo->target_mask;
  2266. else
  2267. tstate->auto_negotiate &= ~devinfo->target_mask;
  2268. return (auto_negotiate_orig != tstate->auto_negotiate);
  2269. }
  2270. /*
  2271. * Update the user/goal/curr tables of synchronous negotiation
  2272. * parameters as well as, in the case of a current or active update,
  2273. * any data structures on the host controller. In the case of an
  2274. * active update, the specified target is currently talking to us on
  2275. * the bus, so the transfer parameter update must take effect
  2276. * immediately.
  2277. */
  2278. void
  2279. ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2280. const struct ahc_syncrate *syncrate, u_int period,
  2281. u_int offset, u_int ppr_options, u_int type, int paused)
  2282. {
  2283. struct ahc_initiator_tinfo *tinfo;
  2284. struct ahc_tmode_tstate *tstate;
  2285. u_int old_period;
  2286. u_int old_offset;
  2287. u_int old_ppr;
  2288. int active;
  2289. int update_needed;
  2290. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  2291. update_needed = 0;
  2292. if (syncrate == NULL) {
  2293. period = 0;
  2294. offset = 0;
  2295. }
  2296. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2297. devinfo->target, &tstate);
  2298. if ((type & AHC_TRANS_USER) != 0) {
  2299. tinfo->user.period = period;
  2300. tinfo->user.offset = offset;
  2301. tinfo->user.ppr_options = ppr_options;
  2302. }
  2303. if ((type & AHC_TRANS_GOAL) != 0) {
  2304. tinfo->goal.period = period;
  2305. tinfo->goal.offset = offset;
  2306. tinfo->goal.ppr_options = ppr_options;
  2307. }
  2308. old_period = tinfo->curr.period;
  2309. old_offset = tinfo->curr.offset;
  2310. old_ppr = tinfo->curr.ppr_options;
  2311. if ((type & AHC_TRANS_CUR) != 0
  2312. && (old_period != period
  2313. || old_offset != offset
  2314. || old_ppr != ppr_options)) {
  2315. u_int scsirate;
  2316. update_needed++;
  2317. scsirate = tinfo->scsirate;
  2318. if ((ahc->features & AHC_ULTRA2) != 0) {
  2319. scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
  2320. if (syncrate != NULL) {
  2321. scsirate |= syncrate->sxfr_u2;
  2322. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
  2323. scsirate |= ENABLE_CRC;
  2324. else
  2325. scsirate |= SINGLE_EDGE;
  2326. }
  2327. } else {
  2328. scsirate &= ~(SXFR|SOFS);
  2329. /*
  2330. * Ensure Ultra mode is set properly for
  2331. * this target.
  2332. */
  2333. tstate->ultraenb &= ~devinfo->target_mask;
  2334. if (syncrate != NULL) {
  2335. if (syncrate->sxfr & ULTRA_SXFR) {
  2336. tstate->ultraenb |=
  2337. devinfo->target_mask;
  2338. }
  2339. scsirate |= syncrate->sxfr & SXFR;
  2340. scsirate |= offset & SOFS;
  2341. }
  2342. if (active) {
  2343. u_int sxfrctl0;
  2344. sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
  2345. sxfrctl0 &= ~FAST20;
  2346. if (tstate->ultraenb & devinfo->target_mask)
  2347. sxfrctl0 |= FAST20;
  2348. ahc_outb(ahc, SXFRCTL0, sxfrctl0);
  2349. }
  2350. }
  2351. if (active) {
  2352. ahc_outb(ahc, SCSIRATE, scsirate);
  2353. if ((ahc->features & AHC_ULTRA2) != 0)
  2354. ahc_outb(ahc, SCSIOFFSET, offset);
  2355. }
  2356. tinfo->scsirate = scsirate;
  2357. tinfo->curr.period = period;
  2358. tinfo->curr.offset = offset;
  2359. tinfo->curr.ppr_options = ppr_options;
  2360. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  2361. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  2362. if (bootverbose) {
  2363. if (offset != 0) {
  2364. printf("%s: target %d synchronous at %sMHz%s, "
  2365. "offset = 0x%x\n", ahc_name(ahc),
  2366. devinfo->target, syncrate->rate,
  2367. (ppr_options & MSG_EXT_PPR_DT_REQ)
  2368. ? " DT" : "", offset);
  2369. } else {
  2370. printf("%s: target %d using "
  2371. "asynchronous transfers\n",
  2372. ahc_name(ahc), devinfo->target);
  2373. }
  2374. }
  2375. }
  2376. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  2377. tinfo, AHC_NEG_TO_GOAL);
  2378. if (update_needed)
  2379. ahc_update_pending_scbs(ahc);
  2380. }
  2381. /*
  2382. * Update the user/goal/curr tables of wide negotiation
  2383. * parameters as well as, in the case of a current or active update,
  2384. * any data structures on the host controller. In the case of an
  2385. * active update, the specified target is currently talking to us on
  2386. * the bus, so the transfer parameter update must take effect
  2387. * immediately.
  2388. */
  2389. void
  2390. ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2391. u_int width, u_int type, int paused)
  2392. {
  2393. struct ahc_initiator_tinfo *tinfo;
  2394. struct ahc_tmode_tstate *tstate;
  2395. u_int oldwidth;
  2396. int active;
  2397. int update_needed;
  2398. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  2399. update_needed = 0;
  2400. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2401. devinfo->target, &tstate);
  2402. if ((type & AHC_TRANS_USER) != 0)
  2403. tinfo->user.width = width;
  2404. if ((type & AHC_TRANS_GOAL) != 0)
  2405. tinfo->goal.width = width;
  2406. oldwidth = tinfo->curr.width;
  2407. if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
  2408. u_int scsirate;
  2409. update_needed++;
  2410. scsirate = tinfo->scsirate;
  2411. scsirate &= ~WIDEXFER;
  2412. if (width == MSG_EXT_WDTR_BUS_16_BIT)
  2413. scsirate |= WIDEXFER;
  2414. tinfo->scsirate = scsirate;
  2415. if (active)
  2416. ahc_outb(ahc, SCSIRATE, scsirate);
  2417. tinfo->curr.width = width;
  2418. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  2419. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  2420. if (bootverbose) {
  2421. printf("%s: target %d using %dbit transfers\n",
  2422. ahc_name(ahc), devinfo->target,
  2423. 8 * (0x01 << width));
  2424. }
  2425. }
  2426. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  2427. tinfo, AHC_NEG_TO_GOAL);
  2428. if (update_needed)
  2429. ahc_update_pending_scbs(ahc);
  2430. }
  2431. /*
  2432. * Update the current state of tagged queuing for a given target.
  2433. */
  2434. static void
  2435. ahc_set_tags(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
  2436. struct ahc_devinfo *devinfo, ahc_queue_alg alg)
  2437. {
  2438. struct scsi_device *sdev = cmd->device;
  2439. ahc_platform_set_tags(ahc, sdev, devinfo, alg);
  2440. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  2441. devinfo->lun, AC_TRANSFER_NEG);
  2442. }
  2443. /*
  2444. * When the transfer settings for a connection change, update any
  2445. * in-transit SCBs to contain the new data so the hardware will
  2446. * be set correctly during future (re)selections.
  2447. */
  2448. static void
  2449. ahc_update_pending_scbs(struct ahc_softc *ahc)
  2450. {
  2451. struct scb *pending_scb;
  2452. int pending_scb_count;
  2453. int i;
  2454. int paused;
  2455. u_int saved_scbptr;
  2456. /*
  2457. * Traverse the pending SCB list and ensure that all of the
  2458. * SCBs there have the proper settings.
  2459. */
  2460. pending_scb_count = 0;
  2461. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  2462. struct ahc_devinfo devinfo;
  2463. struct hardware_scb *pending_hscb;
  2464. struct ahc_initiator_tinfo *tinfo;
  2465. struct ahc_tmode_tstate *tstate;
  2466. ahc_scb_devinfo(ahc, &devinfo, pending_scb);
  2467. tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  2468. devinfo.our_scsiid,
  2469. devinfo.target, &tstate);
  2470. pending_hscb = pending_scb->hscb;
  2471. pending_hscb->control &= ~ULTRAENB;
  2472. if ((tstate->ultraenb & devinfo.target_mask) != 0)
  2473. pending_hscb->control |= ULTRAENB;
  2474. pending_hscb->scsirate = tinfo->scsirate;
  2475. pending_hscb->scsioffset = tinfo->curr.offset;
  2476. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  2477. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  2478. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  2479. pending_hscb->control &= ~MK_MESSAGE;
  2480. }
  2481. ahc_sync_scb(ahc, pending_scb,
  2482. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  2483. pending_scb_count++;
  2484. }
  2485. if (pending_scb_count == 0)
  2486. return;
  2487. if (ahc_is_paused(ahc)) {
  2488. paused = 1;
  2489. } else {
  2490. paused = 0;
  2491. ahc_pause(ahc);
  2492. }
  2493. saved_scbptr = ahc_inb(ahc, SCBPTR);
  2494. /* Ensure that the hscbs down on the card match the new information */
  2495. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  2496. struct hardware_scb *pending_hscb;
  2497. u_int control;
  2498. u_int scb_tag;
  2499. ahc_outb(ahc, SCBPTR, i);
  2500. scb_tag = ahc_inb(ahc, SCB_TAG);
  2501. pending_scb = ahc_lookup_scb(ahc, scb_tag);
  2502. if (pending_scb == NULL)
  2503. continue;
  2504. pending_hscb = pending_scb->hscb;
  2505. control = ahc_inb(ahc, SCB_CONTROL);
  2506. control &= ~(ULTRAENB|MK_MESSAGE);
  2507. control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
  2508. ahc_outb(ahc, SCB_CONTROL, control);
  2509. ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
  2510. ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
  2511. }
  2512. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2513. if (paused == 0)
  2514. ahc_unpause(ahc);
  2515. }
  2516. /**************************** Pathing Information *****************************/
  2517. static void
  2518. ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2519. {
  2520. u_int saved_scsiid;
  2521. role_t role;
  2522. int our_id;
  2523. if (ahc_inb(ahc, SSTAT0) & TARGET)
  2524. role = ROLE_TARGET;
  2525. else
  2526. role = ROLE_INITIATOR;
  2527. if (role == ROLE_TARGET
  2528. && (ahc->features & AHC_MULTI_TID) != 0
  2529. && (ahc_inb(ahc, SEQ_FLAGS)
  2530. & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
  2531. /* We were selected, so pull our id from TARGIDIN */
  2532. our_id = ahc_inb(ahc, TARGIDIN) & OID;
  2533. } else if ((ahc->features & AHC_ULTRA2) != 0)
  2534. our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
  2535. else
  2536. our_id = ahc_inb(ahc, SCSIID) & OID;
  2537. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2538. ahc_compile_devinfo(devinfo,
  2539. our_id,
  2540. SCSIID_TARGET(ahc, saved_scsiid),
  2541. ahc_inb(ahc, SAVED_LUN),
  2542. SCSIID_CHANNEL(ahc, saved_scsiid),
  2543. role);
  2544. }
  2545. static const struct ahc_phase_table_entry*
  2546. ahc_lookup_phase_entry(int phase)
  2547. {
  2548. const struct ahc_phase_table_entry *entry;
  2549. const struct ahc_phase_table_entry *last_entry;
  2550. /*
  2551. * num_phases doesn't include the default entry which
  2552. * will be returned if the phase doesn't match.
  2553. */
  2554. last_entry = &ahc_phase_table[num_phases];
  2555. for (entry = ahc_phase_table; entry < last_entry; entry++) {
  2556. if (phase == entry->phase)
  2557. break;
  2558. }
  2559. return (entry);
  2560. }
  2561. void
  2562. ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
  2563. u_int lun, char channel, role_t role)
  2564. {
  2565. devinfo->our_scsiid = our_id;
  2566. devinfo->target = target;
  2567. devinfo->lun = lun;
  2568. devinfo->target_offset = target;
  2569. devinfo->channel = channel;
  2570. devinfo->role = role;
  2571. if (channel == 'B')
  2572. devinfo->target_offset += 8;
  2573. devinfo->target_mask = (0x01 << devinfo->target_offset);
  2574. }
  2575. void
  2576. ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2577. {
  2578. printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
  2579. devinfo->target, devinfo->lun);
  2580. }
  2581. static void
  2582. ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2583. struct scb *scb)
  2584. {
  2585. role_t role;
  2586. int our_id;
  2587. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  2588. role = ROLE_INITIATOR;
  2589. if ((scb->flags & SCB_TARGET_SCB) != 0)
  2590. role = ROLE_TARGET;
  2591. ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
  2592. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
  2593. }
  2594. /************************ Message Phase Processing ****************************/
  2595. static void
  2596. ahc_assert_atn(struct ahc_softc *ahc)
  2597. {
  2598. u_int scsisigo;
  2599. scsisigo = ATNO;
  2600. if ((ahc->features & AHC_DT) == 0)
  2601. scsisigo |= ahc_inb(ahc, SCSISIGI);
  2602. ahc_outb(ahc, SCSISIGO, scsisigo);
  2603. }
  2604. /*
  2605. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  2606. * or enters the initial message out phase, we are interrupted. Fill our
  2607. * outgoing message buffer with the appropriate message and beging handing
  2608. * the message phase(s) manually.
  2609. */
  2610. static void
  2611. ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2612. struct scb *scb)
  2613. {
  2614. /*
  2615. * To facilitate adding multiple messages together,
  2616. * each routine should increment the index and len
  2617. * variables instead of setting them explicitly.
  2618. */
  2619. ahc->msgout_index = 0;
  2620. ahc->msgout_len = 0;
  2621. if ((scb->flags & SCB_DEVICE_RESET) == 0
  2622. && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
  2623. u_int identify_msg;
  2624. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  2625. if ((scb->hscb->control & DISCENB) != 0)
  2626. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  2627. ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
  2628. ahc->msgout_len++;
  2629. if ((scb->hscb->control & TAG_ENB) != 0) {
  2630. ahc->msgout_buf[ahc->msgout_index++] =
  2631. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  2632. ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
  2633. ahc->msgout_len += 2;
  2634. }
  2635. }
  2636. if (scb->flags & SCB_DEVICE_RESET) {
  2637. ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
  2638. ahc->msgout_len++;
  2639. ahc_print_path(ahc, scb);
  2640. printf("Bus Device Reset Message Sent\n");
  2641. /*
  2642. * Clear our selection hardware in advance of
  2643. * the busfree. We may have an entry in the waiting
  2644. * Q for this target, and we don't want to go about
  2645. * selecting while we handle the busfree and blow it
  2646. * away.
  2647. */
  2648. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2649. } else if ((scb->flags & SCB_ABORT) != 0) {
  2650. if ((scb->hscb->control & TAG_ENB) != 0)
  2651. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
  2652. else
  2653. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
  2654. ahc->msgout_len++;
  2655. ahc_print_path(ahc, scb);
  2656. printf("Abort%s Message Sent\n",
  2657. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  2658. /*
  2659. * Clear our selection hardware in advance of
  2660. * the busfree. We may have an entry in the waiting
  2661. * Q for this target, and we don't want to go about
  2662. * selecting while we handle the busfree and blow it
  2663. * away.
  2664. */
  2665. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2666. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  2667. ahc_build_transfer_msg(ahc, devinfo);
  2668. } else {
  2669. printf("ahc_intr: AWAITING_MSG for an SCB that "
  2670. "does not have a waiting message\n");
  2671. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  2672. devinfo->target_mask);
  2673. panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
  2674. "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
  2675. ahc_inb(ahc, MSG_OUT), scb->flags);
  2676. }
  2677. /*
  2678. * Clear the MK_MESSAGE flag from the SCB so we aren't
  2679. * asked to send this message again.
  2680. */
  2681. ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
  2682. scb->hscb->control &= ~MK_MESSAGE;
  2683. ahc->msgout_index = 0;
  2684. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2685. }
  2686. /*
  2687. * Build an appropriate transfer negotiation message for the
  2688. * currently active target.
  2689. */
  2690. static void
  2691. ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2692. {
  2693. /*
  2694. * We need to initiate transfer negotiations.
  2695. * If our current and goal settings are identical,
  2696. * we want to renegotiate due to a check condition.
  2697. */
  2698. struct ahc_initiator_tinfo *tinfo;
  2699. struct ahc_tmode_tstate *tstate;
  2700. const struct ahc_syncrate *rate;
  2701. int dowide;
  2702. int dosync;
  2703. int doppr;
  2704. u_int period;
  2705. u_int ppr_options;
  2706. u_int offset;
  2707. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2708. devinfo->target, &tstate);
  2709. /*
  2710. * Filter our period based on the current connection.
  2711. * If we can't perform DT transfers on this segment (not in LVD
  2712. * mode for instance), then our decision to issue a PPR message
  2713. * may change.
  2714. */
  2715. period = tinfo->goal.period;
  2716. offset = tinfo->goal.offset;
  2717. ppr_options = tinfo->goal.ppr_options;
  2718. /* Target initiated PPR is not allowed in the SCSI spec */
  2719. if (devinfo->role == ROLE_TARGET)
  2720. ppr_options = 0;
  2721. rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2722. &ppr_options, devinfo->role);
  2723. dowide = tinfo->curr.width != tinfo->goal.width;
  2724. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  2725. /*
  2726. * Only use PPR if we have options that need it, even if the device
  2727. * claims to support it. There might be an expander in the way
  2728. * that doesn't.
  2729. */
  2730. doppr = ppr_options != 0;
  2731. if (!dowide && !dosync && !doppr) {
  2732. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  2733. dosync = tinfo->goal.offset != 0;
  2734. }
  2735. if (!dowide && !dosync && !doppr) {
  2736. /*
  2737. * Force async with a WDTR message if we have a wide bus,
  2738. * or just issue an SDTR with a 0 offset.
  2739. */
  2740. if ((ahc->features & AHC_WIDE) != 0)
  2741. dowide = 1;
  2742. else
  2743. dosync = 1;
  2744. if (bootverbose) {
  2745. ahc_print_devinfo(ahc, devinfo);
  2746. printf("Ensuring async\n");
  2747. }
  2748. }
  2749. /* Target initiated PPR is not allowed in the SCSI spec */
  2750. if (devinfo->role == ROLE_TARGET)
  2751. doppr = 0;
  2752. /*
  2753. * Both the PPR message and SDTR message require the
  2754. * goal syncrate to be limited to what the target device
  2755. * is capable of handling (based on whether an LVD->SE
  2756. * expander is on the bus), so combine these two cases.
  2757. * Regardless, guarantee that if we are using WDTR and SDTR
  2758. * messages that WDTR comes first.
  2759. */
  2760. if (doppr || (dosync && !dowide)) {
  2761. offset = tinfo->goal.offset;
  2762. ahc_validate_offset(ahc, tinfo, rate, &offset,
  2763. doppr ? tinfo->goal.width
  2764. : tinfo->curr.width,
  2765. devinfo->role);
  2766. if (doppr) {
  2767. ahc_construct_ppr(ahc, devinfo, period, offset,
  2768. tinfo->goal.width, ppr_options);
  2769. } else {
  2770. ahc_construct_sdtr(ahc, devinfo, period, offset);
  2771. }
  2772. } else {
  2773. ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
  2774. }
  2775. }
  2776. /*
  2777. * Build a synchronous negotiation message in our message
  2778. * buffer based on the input parameters.
  2779. */
  2780. static void
  2781. ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2782. u_int period, u_int offset)
  2783. {
  2784. if (offset == 0)
  2785. period = AHC_ASYNC_XFER_PERIOD;
  2786. ahc->msgout_index += spi_populate_sync_msg(
  2787. ahc->msgout_buf + ahc->msgout_index, period, offset);
  2788. ahc->msgout_len += 5;
  2789. if (bootverbose) {
  2790. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  2791. ahc_name(ahc), devinfo->channel, devinfo->target,
  2792. devinfo->lun, period, offset);
  2793. }
  2794. }
  2795. /*
  2796. * Build a wide negotiation message in our message
  2797. * buffer based on the input parameters.
  2798. */
  2799. static void
  2800. ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2801. u_int bus_width)
  2802. {
  2803. ahc->msgout_index += spi_populate_width_msg(
  2804. ahc->msgout_buf + ahc->msgout_index, bus_width);
  2805. ahc->msgout_len += 4;
  2806. if (bootverbose) {
  2807. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  2808. ahc_name(ahc), devinfo->channel, devinfo->target,
  2809. devinfo->lun, bus_width);
  2810. }
  2811. }
  2812. /*
  2813. * Build a parallel protocol request message in our message
  2814. * buffer based on the input parameters.
  2815. */
  2816. static void
  2817. ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2818. u_int period, u_int offset, u_int bus_width,
  2819. u_int ppr_options)
  2820. {
  2821. if (offset == 0)
  2822. period = AHC_ASYNC_XFER_PERIOD;
  2823. ahc->msgout_index += spi_populate_ppr_msg(
  2824. ahc->msgout_buf + ahc->msgout_index, period, offset,
  2825. bus_width, ppr_options);
  2826. ahc->msgout_len += 8;
  2827. if (bootverbose) {
  2828. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  2829. "offset %x, ppr_options %x\n", ahc_name(ahc),
  2830. devinfo->channel, devinfo->target, devinfo->lun,
  2831. bus_width, period, offset, ppr_options);
  2832. }
  2833. }
  2834. /*
  2835. * Clear any active message state.
  2836. */
  2837. static void
  2838. ahc_clear_msg_state(struct ahc_softc *ahc)
  2839. {
  2840. ahc->msgout_len = 0;
  2841. ahc->msgin_index = 0;
  2842. ahc->msg_type = MSG_TYPE_NONE;
  2843. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
  2844. /*
  2845. * The target didn't care to respond to our
  2846. * message request, so clear ATN.
  2847. */
  2848. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2849. }
  2850. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  2851. ahc_outb(ahc, SEQ_FLAGS2,
  2852. ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  2853. }
  2854. static void
  2855. ahc_handle_proto_violation(struct ahc_softc *ahc)
  2856. {
  2857. struct ahc_devinfo devinfo;
  2858. struct scb *scb;
  2859. u_int scbid;
  2860. u_int seq_flags;
  2861. u_int curphase;
  2862. u_int lastphase;
  2863. int found;
  2864. ahc_fetch_devinfo(ahc, &devinfo);
  2865. scbid = ahc_inb(ahc, SCB_TAG);
  2866. scb = ahc_lookup_scb(ahc, scbid);
  2867. seq_flags = ahc_inb(ahc, SEQ_FLAGS);
  2868. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2869. lastphase = ahc_inb(ahc, LASTPHASE);
  2870. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2871. /*
  2872. * The reconnecting target either did not send an
  2873. * identify message, or did, but we didn't find an SCB
  2874. * to match.
  2875. */
  2876. ahc_print_devinfo(ahc, &devinfo);
  2877. printf("Target did not send an IDENTIFY message. "
  2878. "LASTPHASE = 0x%x.\n", lastphase);
  2879. scb = NULL;
  2880. } else if (scb == NULL) {
  2881. /*
  2882. * We don't seem to have an SCB active for this
  2883. * transaction. Print an error and reset the bus.
  2884. */
  2885. ahc_print_devinfo(ahc, &devinfo);
  2886. printf("No SCB found during protocol violation\n");
  2887. goto proto_violation_reset;
  2888. } else {
  2889. ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2890. if ((seq_flags & NO_CDB_SENT) != 0) {
  2891. ahc_print_path(ahc, scb);
  2892. printf("No or incomplete CDB sent to device.\n");
  2893. } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
  2894. /*
  2895. * The target never bothered to provide status to
  2896. * us prior to completing the command. Since we don't
  2897. * know the disposition of this command, we must attempt
  2898. * to abort it. Assert ATN and prepare to send an abort
  2899. * message.
  2900. */
  2901. ahc_print_path(ahc, scb);
  2902. printf("Completed command without status.\n");
  2903. } else {
  2904. ahc_print_path(ahc, scb);
  2905. printf("Unknown protocol violation.\n");
  2906. ahc_dump_card_state(ahc);
  2907. }
  2908. }
  2909. if ((lastphase & ~P_DATAIN_DT) == 0
  2910. || lastphase == P_COMMAND) {
  2911. proto_violation_reset:
  2912. /*
  2913. * Target either went directly to data/command
  2914. * phase or didn't respond to our ATN.
  2915. * The only safe thing to do is to blow
  2916. * it away with a bus reset.
  2917. */
  2918. found = ahc_reset_channel(ahc, 'A', TRUE);
  2919. printf("%s: Issued Channel %c Bus Reset. "
  2920. "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
  2921. } else {
  2922. /*
  2923. * Leave the selection hardware off in case
  2924. * this abort attempt will affect yet to
  2925. * be sent commands.
  2926. */
  2927. ahc_outb(ahc, SCSISEQ,
  2928. ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  2929. ahc_assert_atn(ahc);
  2930. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2931. if (scb == NULL) {
  2932. ahc_print_devinfo(ahc, &devinfo);
  2933. ahc->msgout_buf[0] = MSG_ABORT_TASK;
  2934. ahc->msgout_len = 1;
  2935. ahc->msgout_index = 0;
  2936. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2937. } else {
  2938. ahc_print_path(ahc, scb);
  2939. scb->flags |= SCB_ABORT;
  2940. }
  2941. printf("Protocol violation %s. Attempting to abort.\n",
  2942. ahc_lookup_phase_entry(curphase)->phasemsg);
  2943. }
  2944. }
  2945. /*
  2946. * Manual message loop handler.
  2947. */
  2948. static void
  2949. ahc_handle_message_phase(struct ahc_softc *ahc)
  2950. {
  2951. struct ahc_devinfo devinfo;
  2952. u_int bus_phase;
  2953. int end_session;
  2954. ahc_fetch_devinfo(ahc, &devinfo);
  2955. end_session = FALSE;
  2956. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2957. reswitch:
  2958. switch (ahc->msg_type) {
  2959. case MSG_TYPE_INITIATOR_MSGOUT:
  2960. {
  2961. int lastbyte;
  2962. int phasemis;
  2963. int msgdone;
  2964. if (ahc->msgout_len == 0)
  2965. panic("HOST_MSG_LOOP interrupt with no active message");
  2966. #ifdef AHC_DEBUG
  2967. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2968. ahc_print_devinfo(ahc, &devinfo);
  2969. printf("INITIATOR_MSG_OUT");
  2970. }
  2971. #endif
  2972. phasemis = bus_phase != P_MESGOUT;
  2973. if (phasemis) {
  2974. #ifdef AHC_DEBUG
  2975. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2976. printf(" PHASEMIS %s\n",
  2977. ahc_lookup_phase_entry(bus_phase)
  2978. ->phasemsg);
  2979. }
  2980. #endif
  2981. if (bus_phase == P_MESGIN) {
  2982. /*
  2983. * Change gears and see if
  2984. * this messages is of interest to
  2985. * us or should be passed back to
  2986. * the sequencer.
  2987. */
  2988. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2989. ahc->send_msg_perror = FALSE;
  2990. ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  2991. ahc->msgin_index = 0;
  2992. goto reswitch;
  2993. }
  2994. end_session = TRUE;
  2995. break;
  2996. }
  2997. if (ahc->send_msg_perror) {
  2998. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2999. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  3000. #ifdef AHC_DEBUG
  3001. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  3002. printf(" byte 0x%x\n", ahc->send_msg_perror);
  3003. #endif
  3004. ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
  3005. break;
  3006. }
  3007. msgdone = ahc->msgout_index == ahc->msgout_len;
  3008. if (msgdone) {
  3009. /*
  3010. * The target has requested a retry.
  3011. * Re-assert ATN, reset our message index to
  3012. * 0, and try again.
  3013. */
  3014. ahc->msgout_index = 0;
  3015. ahc_assert_atn(ahc);
  3016. }
  3017. lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
  3018. if (lastbyte) {
  3019. /* Last byte is signified by dropping ATN */
  3020. ahc_outb(ahc, CLRSINT1, CLRATNO);
  3021. }
  3022. /*
  3023. * Clear our interrupt status and present
  3024. * the next byte on the bus.
  3025. */
  3026. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  3027. #ifdef AHC_DEBUG
  3028. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  3029. printf(" byte 0x%x\n",
  3030. ahc->msgout_buf[ahc->msgout_index]);
  3031. #endif
  3032. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  3033. break;
  3034. }
  3035. case MSG_TYPE_INITIATOR_MSGIN:
  3036. {
  3037. int phasemis;
  3038. int message_done;
  3039. #ifdef AHC_DEBUG
  3040. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  3041. ahc_print_devinfo(ahc, &devinfo);
  3042. printf("INITIATOR_MSG_IN");
  3043. }
  3044. #endif
  3045. phasemis = bus_phase != P_MESGIN;
  3046. if (phasemis) {
  3047. #ifdef AHC_DEBUG
  3048. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  3049. printf(" PHASEMIS %s\n",
  3050. ahc_lookup_phase_entry(bus_phase)
  3051. ->phasemsg);
  3052. }
  3053. #endif
  3054. ahc->msgin_index = 0;
  3055. if (bus_phase == P_MESGOUT
  3056. && (ahc->send_msg_perror == TRUE
  3057. || (ahc->msgout_len != 0
  3058. && ahc->msgout_index == 0))) {
  3059. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  3060. goto reswitch;
  3061. }
  3062. end_session = TRUE;
  3063. break;
  3064. }
  3065. /* Pull the byte in without acking it */
  3066. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
  3067. #ifdef AHC_DEBUG
  3068. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  3069. printf(" byte 0x%x\n",
  3070. ahc->msgin_buf[ahc->msgin_index]);
  3071. #endif
  3072. message_done = ahc_parse_msg(ahc, &devinfo);
  3073. if (message_done) {
  3074. /*
  3075. * Clear our incoming message buffer in case there
  3076. * is another message following this one.
  3077. */
  3078. ahc->msgin_index = 0;
  3079. /*
  3080. * If this message illicited a response,
  3081. * assert ATN so the target takes us to the
  3082. * message out phase.
  3083. */
  3084. if (ahc->msgout_len != 0) {
  3085. #ifdef AHC_DEBUG
  3086. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  3087. ahc_print_devinfo(ahc, &devinfo);
  3088. printf("Asserting ATN for response\n");
  3089. }
  3090. #endif
  3091. ahc_assert_atn(ahc);
  3092. }
  3093. } else
  3094. ahc->msgin_index++;
  3095. if (message_done == MSGLOOP_TERMINATED) {
  3096. end_session = TRUE;
  3097. } else {
  3098. /* Ack the byte */
  3099. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  3100. ahc_inb(ahc, SCSIDATL);
  3101. }
  3102. break;
  3103. }
  3104. case MSG_TYPE_TARGET_MSGIN:
  3105. {
  3106. int msgdone;
  3107. int msgout_request;
  3108. if (ahc->msgout_len == 0)
  3109. panic("Target MSGIN with no active message");
  3110. /*
  3111. * If we interrupted a mesgout session, the initiator
  3112. * will not know this until our first REQ. So, we
  3113. * only honor mesgout requests after we've sent our
  3114. * first byte.
  3115. */
  3116. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
  3117. && ahc->msgout_index > 0)
  3118. msgout_request = TRUE;
  3119. else
  3120. msgout_request = FALSE;
  3121. if (msgout_request) {
  3122. /*
  3123. * Change gears and see if
  3124. * this messages is of interest to
  3125. * us or should be passed back to
  3126. * the sequencer.
  3127. */
  3128. ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
  3129. ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
  3130. ahc->msgin_index = 0;
  3131. /* Dummy read to REQ for first byte */
  3132. ahc_inb(ahc, SCSIDATL);
  3133. ahc_outb(ahc, SXFRCTL0,
  3134. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  3135. break;
  3136. }
  3137. msgdone = ahc->msgout_index == ahc->msgout_len;
  3138. if (msgdone) {
  3139. ahc_outb(ahc, SXFRCTL0,
  3140. ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  3141. end_session = TRUE;
  3142. break;
  3143. }
  3144. /*
  3145. * Present the next byte on the bus.
  3146. */
  3147. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  3148. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  3149. break;
  3150. }
  3151. case MSG_TYPE_TARGET_MSGOUT:
  3152. {
  3153. int lastbyte;
  3154. int msgdone;
  3155. /*
  3156. * The initiator signals that this is
  3157. * the last byte by dropping ATN.
  3158. */
  3159. lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
  3160. /*
  3161. * Read the latched byte, but turn off SPIOEN first
  3162. * so that we don't inadvertently cause a REQ for the
  3163. * next byte.
  3164. */
  3165. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  3166. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
  3167. msgdone = ahc_parse_msg(ahc, &devinfo);
  3168. if (msgdone == MSGLOOP_TERMINATED) {
  3169. /*
  3170. * The message is *really* done in that it caused
  3171. * us to go to bus free. The sequencer has already
  3172. * been reset at this point, so pull the ejection
  3173. * handle.
  3174. */
  3175. return;
  3176. }
  3177. ahc->msgin_index++;
  3178. /*
  3179. * XXX Read spec about initiator dropping ATN too soon
  3180. * and use msgdone to detect it.
  3181. */
  3182. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  3183. ahc->msgin_index = 0;
  3184. /*
  3185. * If this message illicited a response, transition
  3186. * to the Message in phase and send it.
  3187. */
  3188. if (ahc->msgout_len != 0) {
  3189. ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
  3190. ahc_outb(ahc, SXFRCTL0,
  3191. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  3192. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  3193. ahc->msgin_index = 0;
  3194. break;
  3195. }
  3196. }
  3197. if (lastbyte)
  3198. end_session = TRUE;
  3199. else {
  3200. /* Ask for the next byte. */
  3201. ahc_outb(ahc, SXFRCTL0,
  3202. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  3203. }
  3204. break;
  3205. }
  3206. default:
  3207. panic("Unknown REQINIT message type");
  3208. }
  3209. if (end_session) {
  3210. ahc_clear_msg_state(ahc);
  3211. ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
  3212. } else
  3213. ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
  3214. }
  3215. /*
  3216. * See if we sent a particular extended message to the target.
  3217. * If "full" is true, return true only if the target saw the full
  3218. * message. If "full" is false, return true if the target saw at
  3219. * least the first byte of the message.
  3220. */
  3221. static int
  3222. ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
  3223. {
  3224. int found;
  3225. u_int index;
  3226. found = FALSE;
  3227. index = 0;
  3228. while (index < ahc->msgout_len) {
  3229. if (ahc->msgout_buf[index] == MSG_EXTENDED) {
  3230. u_int end_index;
  3231. end_index = index + 1 + ahc->msgout_buf[index + 1];
  3232. if (ahc->msgout_buf[index+2] == msgval
  3233. && type == AHCMSG_EXT) {
  3234. if (full) {
  3235. if (ahc->msgout_index > end_index)
  3236. found = TRUE;
  3237. } else if (ahc->msgout_index > index)
  3238. found = TRUE;
  3239. }
  3240. index = end_index;
  3241. } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
  3242. && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  3243. /* Skip tag type and tag id or residue param*/
  3244. index += 2;
  3245. } else {
  3246. /* Single byte message */
  3247. if (type == AHCMSG_1B
  3248. && ahc->msgout_buf[index] == msgval
  3249. && ahc->msgout_index > index)
  3250. found = TRUE;
  3251. index++;
  3252. }
  3253. if (found)
  3254. break;
  3255. }
  3256. return (found);
  3257. }
  3258. /*
  3259. * Wait for a complete incoming message, parse it, and respond accordingly.
  3260. */
  3261. static int
  3262. ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3263. {
  3264. struct ahc_initiator_tinfo *tinfo;
  3265. struct ahc_tmode_tstate *tstate;
  3266. int reject;
  3267. int done;
  3268. int response;
  3269. u_int targ_scsirate;
  3270. done = MSGLOOP_IN_PROG;
  3271. response = FALSE;
  3272. reject = FALSE;
  3273. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  3274. devinfo->target, &tstate);
  3275. targ_scsirate = tinfo->scsirate;
  3276. /*
  3277. * Parse as much of the message as is available,
  3278. * rejecting it if we don't support it. When
  3279. * the entire message is available and has been
  3280. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  3281. * that we have parsed an entire message.
  3282. *
  3283. * In the case of extended messages, we accept the length
  3284. * byte outright and perform more checking once we know the
  3285. * extended message type.
  3286. */
  3287. switch (ahc->msgin_buf[0]) {
  3288. case MSG_DISCONNECT:
  3289. case MSG_SAVEDATAPOINTER:
  3290. case MSG_CMDCOMPLETE:
  3291. case MSG_RESTOREPOINTERS:
  3292. case MSG_IGN_WIDE_RESIDUE:
  3293. /*
  3294. * End our message loop as these are messages
  3295. * the sequencer handles on its own.
  3296. */
  3297. done = MSGLOOP_TERMINATED;
  3298. break;
  3299. case MSG_MESSAGE_REJECT:
  3300. response = ahc_handle_msg_reject(ahc, devinfo);
  3301. /* FALLTHROUGH */
  3302. case MSG_NOOP:
  3303. done = MSGLOOP_MSGCOMPLETE;
  3304. break;
  3305. case MSG_EXTENDED:
  3306. {
  3307. /* Wait for enough of the message to begin validation */
  3308. if (ahc->msgin_index < 2)
  3309. break;
  3310. switch (ahc->msgin_buf[2]) {
  3311. case MSG_EXT_SDTR:
  3312. {
  3313. const struct ahc_syncrate *syncrate;
  3314. u_int period;
  3315. u_int ppr_options;
  3316. u_int offset;
  3317. u_int saved_offset;
  3318. if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  3319. reject = TRUE;
  3320. break;
  3321. }
  3322. /*
  3323. * Wait until we have both args before validating
  3324. * and acting on this message.
  3325. *
  3326. * Add one to MSG_EXT_SDTR_LEN to account for
  3327. * the extended message preamble.
  3328. */
  3329. if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  3330. break;
  3331. period = ahc->msgin_buf[3];
  3332. ppr_options = 0;
  3333. saved_offset = offset = ahc->msgin_buf[4];
  3334. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  3335. &ppr_options,
  3336. devinfo->role);
  3337. ahc_validate_offset(ahc, tinfo, syncrate, &offset,
  3338. targ_scsirate & WIDEXFER,
  3339. devinfo->role);
  3340. if (bootverbose) {
  3341. printf("(%s:%c:%d:%d): Received "
  3342. "SDTR period %x, offset %x\n\t"
  3343. "Filtered to period %x, offset %x\n",
  3344. ahc_name(ahc), devinfo->channel,
  3345. devinfo->target, devinfo->lun,
  3346. ahc->msgin_buf[3], saved_offset,
  3347. period, offset);
  3348. }
  3349. ahc_set_syncrate(ahc, devinfo,
  3350. syncrate, period,
  3351. offset, ppr_options,
  3352. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3353. /*paused*/TRUE);
  3354. /*
  3355. * See if we initiated Sync Negotiation
  3356. * and didn't have to fall down to async
  3357. * transfers.
  3358. */
  3359. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  3360. /* We started it */
  3361. if (saved_offset != offset) {
  3362. /* Went too low - force async */
  3363. reject = TRUE;
  3364. }
  3365. } else {
  3366. /*
  3367. * Send our own SDTR in reply
  3368. */
  3369. if (bootverbose
  3370. && devinfo->role == ROLE_INITIATOR) {
  3371. printf("(%s:%c:%d:%d): Target "
  3372. "Initiated SDTR\n",
  3373. ahc_name(ahc), devinfo->channel,
  3374. devinfo->target, devinfo->lun);
  3375. }
  3376. ahc->msgout_index = 0;
  3377. ahc->msgout_len = 0;
  3378. ahc_construct_sdtr(ahc, devinfo,
  3379. period, offset);
  3380. ahc->msgout_index = 0;
  3381. response = TRUE;
  3382. }
  3383. done = MSGLOOP_MSGCOMPLETE;
  3384. break;
  3385. }
  3386. case MSG_EXT_WDTR:
  3387. {
  3388. u_int bus_width;
  3389. u_int saved_width;
  3390. u_int sending_reply;
  3391. sending_reply = FALSE;
  3392. if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  3393. reject = TRUE;
  3394. break;
  3395. }
  3396. /*
  3397. * Wait until we have our arg before validating
  3398. * and acting on this message.
  3399. *
  3400. * Add one to MSG_EXT_WDTR_LEN to account for
  3401. * the extended message preamble.
  3402. */
  3403. if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  3404. break;
  3405. bus_width = ahc->msgin_buf[3];
  3406. saved_width = bus_width;
  3407. ahc_validate_width(ahc, tinfo, &bus_width,
  3408. devinfo->role);
  3409. if (bootverbose) {
  3410. printf("(%s:%c:%d:%d): Received WDTR "
  3411. "%x filtered to %x\n",
  3412. ahc_name(ahc), devinfo->channel,
  3413. devinfo->target, devinfo->lun,
  3414. saved_width, bus_width);
  3415. }
  3416. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  3417. /*
  3418. * Don't send a WDTR back to the
  3419. * target, since we asked first.
  3420. * If the width went higher than our
  3421. * request, reject it.
  3422. */
  3423. if (saved_width > bus_width) {
  3424. reject = TRUE;
  3425. printf("(%s:%c:%d:%d): requested %dBit "
  3426. "transfers. Rejecting...\n",
  3427. ahc_name(ahc), devinfo->channel,
  3428. devinfo->target, devinfo->lun,
  3429. 8 * (0x01 << bus_width));
  3430. bus_width = 0;
  3431. }
  3432. } else {
  3433. /*
  3434. * Send our own WDTR in reply
  3435. */
  3436. if (bootverbose
  3437. && devinfo->role == ROLE_INITIATOR) {
  3438. printf("(%s:%c:%d:%d): Target "
  3439. "Initiated WDTR\n",
  3440. ahc_name(ahc), devinfo->channel,
  3441. devinfo->target, devinfo->lun);
  3442. }
  3443. ahc->msgout_index = 0;
  3444. ahc->msgout_len = 0;
  3445. ahc_construct_wdtr(ahc, devinfo, bus_width);
  3446. ahc->msgout_index = 0;
  3447. response = TRUE;
  3448. sending_reply = TRUE;
  3449. }
  3450. /*
  3451. * After a wide message, we are async, but
  3452. * some devices don't seem to honor this portion
  3453. * of the spec. Force a renegotiation of the
  3454. * sync component of our transfer agreement even
  3455. * if our goal is async. By updating our width
  3456. * after forcing the negotiation, we avoid
  3457. * renegotiating for width.
  3458. */
  3459. ahc_update_neg_request(ahc, devinfo, tstate,
  3460. tinfo, AHC_NEG_ALWAYS);
  3461. ahc_set_width(ahc, devinfo, bus_width,
  3462. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3463. /*paused*/TRUE);
  3464. if (sending_reply == FALSE && reject == FALSE) {
  3465. /*
  3466. * We will always have an SDTR to send.
  3467. */
  3468. ahc->msgout_index = 0;
  3469. ahc->msgout_len = 0;
  3470. ahc_build_transfer_msg(ahc, devinfo);
  3471. ahc->msgout_index = 0;
  3472. response = TRUE;
  3473. }
  3474. done = MSGLOOP_MSGCOMPLETE;
  3475. break;
  3476. }
  3477. case MSG_EXT_PPR:
  3478. {
  3479. const struct ahc_syncrate *syncrate;
  3480. u_int period;
  3481. u_int offset;
  3482. u_int bus_width;
  3483. u_int ppr_options;
  3484. u_int saved_width;
  3485. u_int saved_offset;
  3486. u_int saved_ppr_options;
  3487. if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  3488. reject = TRUE;
  3489. break;
  3490. }
  3491. /*
  3492. * Wait until we have all args before validating
  3493. * and acting on this message.
  3494. *
  3495. * Add one to MSG_EXT_PPR_LEN to account for
  3496. * the extended message preamble.
  3497. */
  3498. if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
  3499. break;
  3500. period = ahc->msgin_buf[3];
  3501. offset = ahc->msgin_buf[5];
  3502. bus_width = ahc->msgin_buf[6];
  3503. saved_width = bus_width;
  3504. ppr_options = ahc->msgin_buf[7];
  3505. /*
  3506. * According to the spec, a DT only
  3507. * period factor with no DT option
  3508. * set implies async.
  3509. */
  3510. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  3511. && period == 9)
  3512. offset = 0;
  3513. saved_ppr_options = ppr_options;
  3514. saved_offset = offset;
  3515. /*
  3516. * Mask out any options we don't support
  3517. * on any controller. Transfer options are
  3518. * only available if we are negotiating wide.
  3519. */
  3520. ppr_options &= MSG_EXT_PPR_DT_REQ;
  3521. if (bus_width == 0)
  3522. ppr_options = 0;
  3523. ahc_validate_width(ahc, tinfo, &bus_width,
  3524. devinfo->role);
  3525. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  3526. &ppr_options,
  3527. devinfo->role);
  3528. ahc_validate_offset(ahc, tinfo, syncrate,
  3529. &offset, bus_width,
  3530. devinfo->role);
  3531. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
  3532. /*
  3533. * If we are unable to do any of the
  3534. * requested options (we went too low),
  3535. * then we'll have to reject the message.
  3536. */
  3537. if (saved_width > bus_width
  3538. || saved_offset != offset
  3539. || saved_ppr_options != ppr_options) {
  3540. reject = TRUE;
  3541. period = 0;
  3542. offset = 0;
  3543. bus_width = 0;
  3544. ppr_options = 0;
  3545. syncrate = NULL;
  3546. }
  3547. } else {
  3548. if (devinfo->role != ROLE_TARGET)
  3549. printf("(%s:%c:%d:%d): Target "
  3550. "Initiated PPR\n",
  3551. ahc_name(ahc), devinfo->channel,
  3552. devinfo->target, devinfo->lun);
  3553. else
  3554. printf("(%s:%c:%d:%d): Initiator "
  3555. "Initiated PPR\n",
  3556. ahc_name(ahc), devinfo->channel,
  3557. devinfo->target, devinfo->lun);
  3558. ahc->msgout_index = 0;
  3559. ahc->msgout_len = 0;
  3560. ahc_construct_ppr(ahc, devinfo, period, offset,
  3561. bus_width, ppr_options);
  3562. ahc->msgout_index = 0;
  3563. response = TRUE;
  3564. }
  3565. if (bootverbose) {
  3566. printf("(%s:%c:%d:%d): Received PPR width %x, "
  3567. "period %x, offset %x,options %x\n"
  3568. "\tFiltered to width %x, period %x, "
  3569. "offset %x, options %x\n",
  3570. ahc_name(ahc), devinfo->channel,
  3571. devinfo->target, devinfo->lun,
  3572. saved_width, ahc->msgin_buf[3],
  3573. saved_offset, saved_ppr_options,
  3574. bus_width, period, offset, ppr_options);
  3575. }
  3576. ahc_set_width(ahc, devinfo, bus_width,
  3577. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3578. /*paused*/TRUE);
  3579. ahc_set_syncrate(ahc, devinfo,
  3580. syncrate, period,
  3581. offset, ppr_options,
  3582. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3583. /*paused*/TRUE);
  3584. done = MSGLOOP_MSGCOMPLETE;
  3585. break;
  3586. }
  3587. default:
  3588. /* Unknown extended message. Reject it. */
  3589. reject = TRUE;
  3590. break;
  3591. }
  3592. break;
  3593. }
  3594. #ifdef AHC_TARGET_MODE
  3595. case MSG_BUS_DEV_RESET:
  3596. ahc_handle_devreset(ahc, devinfo,
  3597. CAM_BDR_SENT,
  3598. "Bus Device Reset Received",
  3599. /*verbose_level*/0);
  3600. ahc_restart(ahc);
  3601. done = MSGLOOP_TERMINATED;
  3602. break;
  3603. case MSG_ABORT_TAG:
  3604. case MSG_ABORT:
  3605. case MSG_CLEAR_QUEUE:
  3606. {
  3607. int tag;
  3608. /* Target mode messages */
  3609. if (devinfo->role != ROLE_TARGET) {
  3610. reject = TRUE;
  3611. break;
  3612. }
  3613. tag = SCB_LIST_NULL;
  3614. if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
  3615. tag = ahc_inb(ahc, INITIATOR_TAG);
  3616. ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3617. devinfo->lun, tag, ROLE_TARGET,
  3618. CAM_REQ_ABORTED);
  3619. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3620. if (tstate != NULL) {
  3621. struct ahc_tmode_lstate* lstate;
  3622. lstate = tstate->enabled_luns[devinfo->lun];
  3623. if (lstate != NULL) {
  3624. ahc_queue_lstate_event(ahc, lstate,
  3625. devinfo->our_scsiid,
  3626. ahc->msgin_buf[0],
  3627. /*arg*/tag);
  3628. ahc_send_lstate_events(ahc, lstate);
  3629. }
  3630. }
  3631. ahc_restart(ahc);
  3632. done = MSGLOOP_TERMINATED;
  3633. break;
  3634. }
  3635. #endif
  3636. case MSG_TERM_IO_PROC:
  3637. default:
  3638. reject = TRUE;
  3639. break;
  3640. }
  3641. if (reject) {
  3642. /*
  3643. * Setup to reject the message.
  3644. */
  3645. ahc->msgout_index = 0;
  3646. ahc->msgout_len = 1;
  3647. ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
  3648. done = MSGLOOP_MSGCOMPLETE;
  3649. response = TRUE;
  3650. }
  3651. if (done != MSGLOOP_IN_PROG && !response)
  3652. /* Clear the outgoing message buffer */
  3653. ahc->msgout_len = 0;
  3654. return (done);
  3655. }
  3656. /*
  3657. * Process a message reject message.
  3658. */
  3659. static int
  3660. ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3661. {
  3662. /*
  3663. * What we care about here is if we had an
  3664. * outstanding SDTR or WDTR message for this
  3665. * target. If we did, this is a signal that
  3666. * the target is refusing negotiation.
  3667. */
  3668. struct scb *scb;
  3669. struct ahc_initiator_tinfo *tinfo;
  3670. struct ahc_tmode_tstate *tstate;
  3671. u_int scb_index;
  3672. u_int last_msg;
  3673. int response = 0;
  3674. scb_index = ahc_inb(ahc, SCB_TAG);
  3675. scb = ahc_lookup_scb(ahc, scb_index);
  3676. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
  3677. devinfo->our_scsiid,
  3678. devinfo->target, &tstate);
  3679. /* Might be necessary */
  3680. last_msg = ahc_inb(ahc, LAST_MSG);
  3681. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  3682. /*
  3683. * Target does not support the PPR message.
  3684. * Attempt to negotiate SPI-2 style.
  3685. */
  3686. if (bootverbose) {
  3687. printf("(%s:%c:%d:%d): PPR Rejected. "
  3688. "Trying WDTR/SDTR\n",
  3689. ahc_name(ahc), devinfo->channel,
  3690. devinfo->target, devinfo->lun);
  3691. }
  3692. tinfo->goal.ppr_options = 0;
  3693. tinfo->curr.transport_version = 2;
  3694. tinfo->goal.transport_version = 2;
  3695. ahc->msgout_index = 0;
  3696. ahc->msgout_len = 0;
  3697. ahc_build_transfer_msg(ahc, devinfo);
  3698. ahc->msgout_index = 0;
  3699. response = 1;
  3700. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  3701. /* note 8bit xfers */
  3702. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  3703. "8bit transfers\n", ahc_name(ahc),
  3704. devinfo->channel, devinfo->target, devinfo->lun);
  3705. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3706. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3707. /*paused*/TRUE);
  3708. /*
  3709. * No need to clear the sync rate. If the target
  3710. * did not accept the command, our syncrate is
  3711. * unaffected. If the target started the negotiation,
  3712. * but rejected our response, we already cleared the
  3713. * sync rate before sending our WDTR.
  3714. */
  3715. if (tinfo->goal.offset != tinfo->curr.offset) {
  3716. /* Start the sync negotiation */
  3717. ahc->msgout_index = 0;
  3718. ahc->msgout_len = 0;
  3719. ahc_build_transfer_msg(ahc, devinfo);
  3720. ahc->msgout_index = 0;
  3721. response = 1;
  3722. }
  3723. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  3724. /* note asynch xfers and clear flag */
  3725. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
  3726. /*offset*/0, /*ppr_options*/0,
  3727. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3728. /*paused*/TRUE);
  3729. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  3730. "Using asynchronous transfers\n",
  3731. ahc_name(ahc), devinfo->channel,
  3732. devinfo->target, devinfo->lun);
  3733. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  3734. int tag_type;
  3735. int mask;
  3736. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  3737. if (tag_type == MSG_SIMPLE_TASK) {
  3738. printf("(%s:%c:%d:%d): refuses tagged commands. "
  3739. "Performing non-tagged I/O\n", ahc_name(ahc),
  3740. devinfo->channel, devinfo->target, devinfo->lun);
  3741. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_NONE);
  3742. mask = ~0x23;
  3743. } else {
  3744. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  3745. "Performing simple queue tagged I/O only\n",
  3746. ahc_name(ahc), devinfo->channel, devinfo->target,
  3747. devinfo->lun, tag_type == MSG_ORDERED_TASK
  3748. ? "ordered" : "head of queue");
  3749. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_BASIC);
  3750. mask = ~0x03;
  3751. }
  3752. /*
  3753. * Resend the identify for this CCB as the target
  3754. * may believe that the selection is invalid otherwise.
  3755. */
  3756. ahc_outb(ahc, SCB_CONTROL,
  3757. ahc_inb(ahc, SCB_CONTROL) & mask);
  3758. scb->hscb->control &= mask;
  3759. ahc_set_transaction_tag(scb, /*enabled*/FALSE,
  3760. /*type*/MSG_SIMPLE_TASK);
  3761. ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
  3762. ahc_assert_atn(ahc);
  3763. /*
  3764. * This transaction is now at the head of
  3765. * the untagged queue for this target.
  3766. */
  3767. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  3768. struct scb_tailq *untagged_q;
  3769. untagged_q =
  3770. &(ahc->untagged_queues[devinfo->target_offset]);
  3771. TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
  3772. scb->flags |= SCB_UNTAGGEDQ;
  3773. }
  3774. ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  3775. scb->hscb->tag);
  3776. /*
  3777. * Requeue all tagged commands for this target
  3778. * currently in our posession so they can be
  3779. * converted to untagged commands.
  3780. */
  3781. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  3782. SCB_GET_CHANNEL(ahc, scb),
  3783. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  3784. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  3785. SEARCH_COMPLETE);
  3786. } else {
  3787. /*
  3788. * Otherwise, we ignore it.
  3789. */
  3790. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  3791. ahc_name(ahc), devinfo->channel, devinfo->target,
  3792. last_msg);
  3793. }
  3794. return (response);
  3795. }
  3796. /*
  3797. * Process an ingnore wide residue message.
  3798. */
  3799. static void
  3800. ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3801. {
  3802. u_int scb_index;
  3803. struct scb *scb;
  3804. scb_index = ahc_inb(ahc, SCB_TAG);
  3805. scb = ahc_lookup_scb(ahc, scb_index);
  3806. /*
  3807. * XXX Actually check data direction in the sequencer?
  3808. * Perhaps add datadir to some spare bits in the hscb?
  3809. */
  3810. if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
  3811. || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
  3812. /*
  3813. * Ignore the message if we haven't
  3814. * seen an appropriate data phase yet.
  3815. */
  3816. } else {
  3817. /*
  3818. * If the residual occurred on the last
  3819. * transfer and the transfer request was
  3820. * expected to end on an odd count, do
  3821. * nothing. Otherwise, subtract a byte
  3822. * and update the residual count accordingly.
  3823. */
  3824. uint32_t sgptr;
  3825. sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3826. if ((sgptr & SG_LIST_NULL) != 0
  3827. && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
  3828. /*
  3829. * If the residual occurred on the last
  3830. * transfer and the transfer request was
  3831. * expected to end on an odd count, do
  3832. * nothing.
  3833. */
  3834. } else {
  3835. struct ahc_dma_seg *sg;
  3836. uint32_t data_cnt;
  3837. uint32_t data_addr;
  3838. uint32_t sglen;
  3839. /* Pull in all of the sgptr */
  3840. sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
  3841. data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
  3842. if ((sgptr & SG_LIST_NULL) != 0) {
  3843. /*
  3844. * The residual data count is not updated
  3845. * for the command run to completion case.
  3846. * Explicitly zero the count.
  3847. */
  3848. data_cnt &= ~AHC_SG_LEN_MASK;
  3849. }
  3850. data_addr = ahc_inl(ahc, SHADDR);
  3851. data_cnt += 1;
  3852. data_addr -= 1;
  3853. sgptr &= SG_PTR_MASK;
  3854. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3855. /*
  3856. * The residual sg ptr points to the next S/G
  3857. * to load so we must go back one.
  3858. */
  3859. sg--;
  3860. sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  3861. if (sg != scb->sg_list
  3862. && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
  3863. sg--;
  3864. sglen = ahc_le32toh(sg->len);
  3865. /*
  3866. * Preserve High Address and SG_LIST bits
  3867. * while setting the count to 1.
  3868. */
  3869. data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
  3870. data_addr = ahc_le32toh(sg->addr)
  3871. + (sglen & AHC_SG_LEN_MASK) - 1;
  3872. /*
  3873. * Increment sg so it points to the
  3874. * "next" sg.
  3875. */
  3876. sg++;
  3877. sgptr = ahc_sg_virt_to_bus(scb, sg);
  3878. }
  3879. ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
  3880. ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
  3881. /*
  3882. * Toggle the "oddness" of the transfer length
  3883. * to handle this mid-transfer ignore wide
  3884. * residue. This ensures that the oddness is
  3885. * correct for subsequent data transfers.
  3886. */
  3887. ahc_outb(ahc, SCB_LUN,
  3888. ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
  3889. }
  3890. }
  3891. }
  3892. /*
  3893. * Reinitialize the data pointers for the active transfer
  3894. * based on its current residual.
  3895. */
  3896. static void
  3897. ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
  3898. {
  3899. struct scb *scb;
  3900. struct ahc_dma_seg *sg;
  3901. u_int scb_index;
  3902. uint32_t sgptr;
  3903. uint32_t resid;
  3904. uint32_t dataptr;
  3905. scb_index = ahc_inb(ahc, SCB_TAG);
  3906. scb = ahc_lookup_scb(ahc, scb_index);
  3907. sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3908. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3909. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
  3910. | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3911. sgptr &= SG_PTR_MASK;
  3912. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3913. /* The residual sg_ptr always points to the next sg */
  3914. sg--;
  3915. resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
  3916. | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
  3917. | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
  3918. dataptr = ahc_le32toh(sg->addr)
  3919. + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
  3920. - resid;
  3921. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  3922. u_int dscommand1;
  3923. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  3924. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  3925. ahc_outb(ahc, HADDR,
  3926. (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
  3927. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  3928. }
  3929. ahc_outb(ahc, HADDR + 3, dataptr >> 24);
  3930. ahc_outb(ahc, HADDR + 2, dataptr >> 16);
  3931. ahc_outb(ahc, HADDR + 1, dataptr >> 8);
  3932. ahc_outb(ahc, HADDR, dataptr);
  3933. ahc_outb(ahc, HCNT + 2, resid >> 16);
  3934. ahc_outb(ahc, HCNT + 1, resid >> 8);
  3935. ahc_outb(ahc, HCNT, resid);
  3936. if ((ahc->features & AHC_ULTRA2) == 0) {
  3937. ahc_outb(ahc, STCNT + 2, resid >> 16);
  3938. ahc_outb(ahc, STCNT + 1, resid >> 8);
  3939. ahc_outb(ahc, STCNT, resid);
  3940. }
  3941. }
  3942. /*
  3943. * Handle the effects of issuing a bus device reset message.
  3944. */
  3945. static void
  3946. ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3947. cam_status status, char *message, int verbose_level)
  3948. {
  3949. #ifdef AHC_TARGET_MODE
  3950. struct ahc_tmode_tstate* tstate;
  3951. u_int lun;
  3952. #endif
  3953. int found;
  3954. found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3955. CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
  3956. status);
  3957. #ifdef AHC_TARGET_MODE
  3958. /*
  3959. * Send an immediate notify ccb to all target mord peripheral
  3960. * drivers affected by this action.
  3961. */
  3962. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3963. if (tstate != NULL) {
  3964. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  3965. struct ahc_tmode_lstate* lstate;
  3966. lstate = tstate->enabled_luns[lun];
  3967. if (lstate == NULL)
  3968. continue;
  3969. ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
  3970. MSG_BUS_DEV_RESET, /*arg*/0);
  3971. ahc_send_lstate_events(ahc, lstate);
  3972. }
  3973. }
  3974. #endif
  3975. /*
  3976. * Go back to async/narrow transfers and renegotiate.
  3977. */
  3978. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3979. AHC_TRANS_CUR, /*paused*/TRUE);
  3980. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
  3981. /*period*/0, /*offset*/0, /*ppr_options*/0,
  3982. AHC_TRANS_CUR, /*paused*/TRUE);
  3983. if (status != CAM_SEL_TIMEOUT)
  3984. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  3985. CAM_LUN_WILDCARD, AC_SENT_BDR);
  3986. if (message != NULL
  3987. && (verbose_level <= bootverbose))
  3988. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
  3989. message, devinfo->channel, devinfo->target, found);
  3990. }
  3991. #ifdef AHC_TARGET_MODE
  3992. static void
  3993. ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3994. struct scb *scb)
  3995. {
  3996. /*
  3997. * To facilitate adding multiple messages together,
  3998. * each routine should increment the index and len
  3999. * variables instead of setting them explicitly.
  4000. */
  4001. ahc->msgout_index = 0;
  4002. ahc->msgout_len = 0;
  4003. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  4004. ahc_build_transfer_msg(ahc, devinfo);
  4005. else
  4006. panic("ahc_intr: AWAITING target message with no message");
  4007. ahc->msgout_index = 0;
  4008. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  4009. }
  4010. #endif
  4011. /**************************** Initialization **********************************/
  4012. /*
  4013. * Allocate a controller structure for a new device
  4014. * and perform initial initializion.
  4015. */
  4016. struct ahc_softc *
  4017. ahc_alloc(void *platform_arg, char *name)
  4018. {
  4019. struct ahc_softc *ahc;
  4020. int i;
  4021. #ifndef __FreeBSD__
  4022. ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
  4023. if (!ahc) {
  4024. printf("aic7xxx: cannot malloc softc!\n");
  4025. free(name, M_DEVBUF);
  4026. return NULL;
  4027. }
  4028. #else
  4029. ahc = device_get_softc((device_t)platform_arg);
  4030. #endif
  4031. memset(ahc, 0, sizeof(*ahc));
  4032. ahc->seep_config = malloc(sizeof(*ahc->seep_config),
  4033. M_DEVBUF, M_NOWAIT);
  4034. if (ahc->seep_config == NULL) {
  4035. #ifndef __FreeBSD__
  4036. free(ahc, M_DEVBUF);
  4037. #endif
  4038. free(name, M_DEVBUF);
  4039. return (NULL);
  4040. }
  4041. LIST_INIT(&ahc->pending_scbs);
  4042. /* We don't know our unit number until the OSM sets it */
  4043. ahc->name = name;
  4044. ahc->unit = -1;
  4045. ahc->description = NULL;
  4046. ahc->channel = 'A';
  4047. ahc->channel_b = 'B';
  4048. ahc->chip = AHC_NONE;
  4049. ahc->features = AHC_FENONE;
  4050. ahc->bugs = AHC_BUGNONE;
  4051. ahc->flags = AHC_FNONE;
  4052. /*
  4053. * Default to all error reporting enabled with the
  4054. * sequencer operating at its fastest speed.
  4055. * The bus attach code may modify this.
  4056. */
  4057. ahc->seqctl = FASTMODE;
  4058. for (i = 0; i < AHC_NUM_TARGETS; i++)
  4059. TAILQ_INIT(&ahc->untagged_queues[i]);
  4060. if (ahc_platform_alloc(ahc, platform_arg) != 0) {
  4061. ahc_free(ahc);
  4062. ahc = NULL;
  4063. }
  4064. return (ahc);
  4065. }
  4066. int
  4067. ahc_softc_init(struct ahc_softc *ahc)
  4068. {
  4069. /* The IRQMS bit is only valid on VL and EISA chips */
  4070. if ((ahc->chip & AHC_PCI) == 0)
  4071. ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
  4072. else
  4073. ahc->unpause = 0;
  4074. ahc->pause = ahc->unpause | PAUSE;
  4075. /* XXX The shared scb data stuff should be deprecated */
  4076. if (ahc->scb_data == NULL) {
  4077. ahc->scb_data = malloc(sizeof(*ahc->scb_data),
  4078. M_DEVBUF, M_NOWAIT);
  4079. if (ahc->scb_data == NULL)
  4080. return (ENOMEM);
  4081. memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
  4082. }
  4083. return (0);
  4084. }
  4085. void
  4086. ahc_set_unit(struct ahc_softc *ahc, int unit)
  4087. {
  4088. ahc->unit = unit;
  4089. }
  4090. void
  4091. ahc_set_name(struct ahc_softc *ahc, char *name)
  4092. {
  4093. if (ahc->name != NULL)
  4094. free(ahc->name, M_DEVBUF);
  4095. ahc->name = name;
  4096. }
  4097. void
  4098. ahc_free(struct ahc_softc *ahc)
  4099. {
  4100. int i;
  4101. switch (ahc->init_level) {
  4102. default:
  4103. case 5:
  4104. ahc_shutdown(ahc);
  4105. /* FALLTHROUGH */
  4106. case 4:
  4107. ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
  4108. ahc->shared_data_dmamap);
  4109. /* FALLTHROUGH */
  4110. case 3:
  4111. ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
  4112. ahc->shared_data_dmamap);
  4113. ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
  4114. ahc->shared_data_dmamap);
  4115. /* FALLTHROUGH */
  4116. case 2:
  4117. ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
  4118. case 1:
  4119. #ifndef __linux__
  4120. ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
  4121. #endif
  4122. break;
  4123. case 0:
  4124. break;
  4125. }
  4126. #ifndef __linux__
  4127. ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
  4128. #endif
  4129. ahc_platform_free(ahc);
  4130. ahc_fini_scbdata(ahc);
  4131. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  4132. struct ahc_tmode_tstate *tstate;
  4133. tstate = ahc->enabled_targets[i];
  4134. if (tstate != NULL) {
  4135. #ifdef AHC_TARGET_MODE
  4136. int j;
  4137. for (j = 0; j < AHC_NUM_LUNS; j++) {
  4138. struct ahc_tmode_lstate *lstate;
  4139. lstate = tstate->enabled_luns[j];
  4140. if (lstate != NULL) {
  4141. xpt_free_path(lstate->path);
  4142. free(lstate, M_DEVBUF);
  4143. }
  4144. }
  4145. #endif
  4146. free(tstate, M_DEVBUF);
  4147. }
  4148. }
  4149. #ifdef AHC_TARGET_MODE
  4150. if (ahc->black_hole != NULL) {
  4151. xpt_free_path(ahc->black_hole->path);
  4152. free(ahc->black_hole, M_DEVBUF);
  4153. }
  4154. #endif
  4155. if (ahc->name != NULL)
  4156. free(ahc->name, M_DEVBUF);
  4157. if (ahc->seep_config != NULL)
  4158. free(ahc->seep_config, M_DEVBUF);
  4159. #ifndef __FreeBSD__
  4160. free(ahc, M_DEVBUF);
  4161. #endif
  4162. return;
  4163. }
  4164. static void
  4165. ahc_shutdown(void *arg)
  4166. {
  4167. struct ahc_softc *ahc;
  4168. int i;
  4169. ahc = (struct ahc_softc *)arg;
  4170. /* This will reset most registers to 0, but not all */
  4171. ahc_reset(ahc, /*reinit*/FALSE);
  4172. ahc_outb(ahc, SCSISEQ, 0);
  4173. ahc_outb(ahc, SXFRCTL0, 0);
  4174. ahc_outb(ahc, DSPCISTATUS, 0);
  4175. for (i = TARG_SCSIRATE; i < SCSICONF; i++)
  4176. ahc_outb(ahc, i, 0);
  4177. }
  4178. /*
  4179. * Reset the controller and record some information about it
  4180. * that is only available just after a reset. If "reinit" is
  4181. * non-zero, this reset occured after initial configuration
  4182. * and the caller requests that the chip be fully reinitialized
  4183. * to a runable state. Chip interrupts are *not* enabled after
  4184. * a reinitialization. The caller must enable interrupts via
  4185. * ahc_intr_enable().
  4186. */
  4187. int
  4188. ahc_reset(struct ahc_softc *ahc, int reinit)
  4189. {
  4190. u_int sblkctl;
  4191. u_int sxfrctl1_a, sxfrctl1_b;
  4192. int error;
  4193. int wait;
  4194. /*
  4195. * Preserve the value of the SXFRCTL1 register for all channels.
  4196. * It contains settings that affect termination and we don't want
  4197. * to disturb the integrity of the bus.
  4198. */
  4199. ahc_pause(ahc);
  4200. sxfrctl1_b = 0;
  4201. if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
  4202. u_int sblkctl;
  4203. /*
  4204. * Save channel B's settings in case this chip
  4205. * is setup for TWIN channel operation.
  4206. */
  4207. sblkctl = ahc_inb(ahc, SBLKCTL);
  4208. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  4209. sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
  4210. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  4211. }
  4212. sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
  4213. ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
  4214. /*
  4215. * Ensure that the reset has finished. We delay 1000us
  4216. * prior to reading the register to make sure the chip
  4217. * has sufficiently completed its reset to handle register
  4218. * accesses.
  4219. */
  4220. wait = 1000;
  4221. do {
  4222. ahc_delay(1000);
  4223. } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
  4224. if (wait == 0) {
  4225. printf("%s: WARNING - Failed chip reset! "
  4226. "Trying to initialize anyway.\n", ahc_name(ahc));
  4227. }
  4228. ahc_outb(ahc, HCNTRL, ahc->pause);
  4229. /* Determine channel configuration */
  4230. sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
  4231. /* No Twin Channel PCI cards */
  4232. if ((ahc->chip & AHC_PCI) != 0)
  4233. sblkctl &= ~SELBUSB;
  4234. switch (sblkctl) {
  4235. case 0:
  4236. /* Single Narrow Channel */
  4237. break;
  4238. case 2:
  4239. /* Wide Channel */
  4240. ahc->features |= AHC_WIDE;
  4241. break;
  4242. case 8:
  4243. /* Twin Channel */
  4244. ahc->features |= AHC_TWIN;
  4245. break;
  4246. default:
  4247. printf(" Unsupported adapter type. Ignoring\n");
  4248. return(-1);
  4249. }
  4250. /*
  4251. * Reload sxfrctl1.
  4252. *
  4253. * We must always initialize STPWEN to 1 before we
  4254. * restore the saved values. STPWEN is initialized
  4255. * to a tri-state condition which can only be cleared
  4256. * by turning it on.
  4257. */
  4258. if ((ahc->features & AHC_TWIN) != 0) {
  4259. u_int sblkctl;
  4260. sblkctl = ahc_inb(ahc, SBLKCTL);
  4261. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  4262. ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
  4263. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  4264. }
  4265. ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
  4266. error = 0;
  4267. if (reinit != 0)
  4268. /*
  4269. * If a recovery action has forced a chip reset,
  4270. * re-initialize the chip to our liking.
  4271. */
  4272. error = ahc->bus_chip_init(ahc);
  4273. #ifdef AHC_DUMP_SEQ
  4274. else
  4275. ahc_dumpseq(ahc);
  4276. #endif
  4277. return (error);
  4278. }
  4279. /*
  4280. * Determine the number of SCBs available on the controller
  4281. */
  4282. int
  4283. ahc_probe_scbs(struct ahc_softc *ahc) {
  4284. int i;
  4285. for (i = 0; i < AHC_SCB_MAX; i++) {
  4286. ahc_outb(ahc, SCBPTR, i);
  4287. ahc_outb(ahc, SCB_BASE, i);
  4288. if (ahc_inb(ahc, SCB_BASE) != i)
  4289. break;
  4290. ahc_outb(ahc, SCBPTR, 0);
  4291. if (ahc_inb(ahc, SCB_BASE) != 0)
  4292. break;
  4293. }
  4294. return (i);
  4295. }
  4296. static void
  4297. ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  4298. {
  4299. dma_addr_t *baddr;
  4300. baddr = (dma_addr_t *)arg;
  4301. *baddr = segs->ds_addr;
  4302. }
  4303. static void
  4304. ahc_build_free_scb_list(struct ahc_softc *ahc)
  4305. {
  4306. int scbsize;
  4307. int i;
  4308. scbsize = 32;
  4309. if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
  4310. scbsize = 64;
  4311. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  4312. int j;
  4313. ahc_outb(ahc, SCBPTR, i);
  4314. /*
  4315. * Touch all SCB bytes to avoid parity errors
  4316. * should one of our debugging routines read
  4317. * an otherwise uninitiatlized byte.
  4318. */
  4319. for (j = 0; j < scbsize; j++)
  4320. ahc_outb(ahc, SCB_BASE+j, 0xFF);
  4321. /* Clear the control byte. */
  4322. ahc_outb(ahc, SCB_CONTROL, 0);
  4323. /* Set the next pointer */
  4324. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4325. ahc_outb(ahc, SCB_NEXT, i+1);
  4326. else
  4327. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  4328. /* Make the tag number, SCSIID, and lun invalid */
  4329. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  4330. ahc_outb(ahc, SCB_SCSIID, 0xFF);
  4331. ahc_outb(ahc, SCB_LUN, 0xFF);
  4332. }
  4333. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  4334. /* SCB 0 heads the free list. */
  4335. ahc_outb(ahc, FREE_SCBH, 0);
  4336. } else {
  4337. /* No free list. */
  4338. ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
  4339. }
  4340. /* Make sure that the last SCB terminates the free list */
  4341. ahc_outb(ahc, SCBPTR, i-1);
  4342. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  4343. }
  4344. static int
  4345. ahc_init_scbdata(struct ahc_softc *ahc)
  4346. {
  4347. struct scb_data *scb_data;
  4348. scb_data = ahc->scb_data;
  4349. SLIST_INIT(&scb_data->free_scbs);
  4350. SLIST_INIT(&scb_data->sg_maps);
  4351. /* Allocate SCB resources */
  4352. scb_data->scbarray =
  4353. (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
  4354. M_DEVBUF, M_NOWAIT);
  4355. if (scb_data->scbarray == NULL)
  4356. return (ENOMEM);
  4357. memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
  4358. /* Determine the number of hardware SCBs and initialize them */
  4359. scb_data->maxhscbs = ahc_probe_scbs(ahc);
  4360. if (ahc->scb_data->maxhscbs == 0) {
  4361. printf("%s: No SCB space found\n", ahc_name(ahc));
  4362. return (ENXIO);
  4363. }
  4364. /*
  4365. * Create our DMA tags. These tags define the kinds of device
  4366. * accessible memory allocations and memory mappings we will
  4367. * need to perform during normal operation.
  4368. *
  4369. * Unless we need to further restrict the allocation, we rely
  4370. * on the restrictions of the parent dmat, hence the common
  4371. * use of MAXADDR and MAXSIZE.
  4372. */
  4373. /* DMA tag for our hardware scb structures */
  4374. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4375. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4376. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4377. /*highaddr*/BUS_SPACE_MAXADDR,
  4378. /*filter*/NULL, /*filterarg*/NULL,
  4379. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  4380. /*nsegments*/1,
  4381. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4382. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  4383. goto error_exit;
  4384. }
  4385. scb_data->init_level++;
  4386. /* Allocation for our hscbs */
  4387. if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
  4388. (void **)&scb_data->hscbs,
  4389. BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
  4390. goto error_exit;
  4391. }
  4392. scb_data->init_level++;
  4393. /* And permanently map them */
  4394. ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
  4395. scb_data->hscbs,
  4396. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  4397. ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
  4398. scb_data->init_level++;
  4399. /* DMA tag for our sense buffers */
  4400. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4401. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4402. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4403. /*highaddr*/BUS_SPACE_MAXADDR,
  4404. /*filter*/NULL, /*filterarg*/NULL,
  4405. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  4406. /*nsegments*/1,
  4407. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4408. /*flags*/0, &scb_data->sense_dmat) != 0) {
  4409. goto error_exit;
  4410. }
  4411. scb_data->init_level++;
  4412. /* Allocate them */
  4413. if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
  4414. (void **)&scb_data->sense,
  4415. BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
  4416. goto error_exit;
  4417. }
  4418. scb_data->init_level++;
  4419. /* And permanently map them */
  4420. ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
  4421. scb_data->sense,
  4422. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  4423. ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
  4424. scb_data->init_level++;
  4425. /* DMA tag for our S/G structures. We allocate in page sized chunks */
  4426. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
  4427. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4428. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4429. /*highaddr*/BUS_SPACE_MAXADDR,
  4430. /*filter*/NULL, /*filterarg*/NULL,
  4431. PAGE_SIZE, /*nsegments*/1,
  4432. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4433. /*flags*/0, &scb_data->sg_dmat) != 0) {
  4434. goto error_exit;
  4435. }
  4436. scb_data->init_level++;
  4437. /* Perform initial CCB allocation */
  4438. memset(scb_data->hscbs, 0,
  4439. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
  4440. ahc_alloc_scbs(ahc);
  4441. if (scb_data->numscbs == 0) {
  4442. printf("%s: ahc_init_scbdata - "
  4443. "Unable to allocate initial scbs\n",
  4444. ahc_name(ahc));
  4445. goto error_exit;
  4446. }
  4447. /*
  4448. * Reserve the next queued SCB.
  4449. */
  4450. ahc->next_queued_scb = ahc_get_scb(ahc);
  4451. /*
  4452. * Note that we were successfull
  4453. */
  4454. return (0);
  4455. error_exit:
  4456. return (ENOMEM);
  4457. }
  4458. static void
  4459. ahc_fini_scbdata(struct ahc_softc *ahc)
  4460. {
  4461. struct scb_data *scb_data;
  4462. scb_data = ahc->scb_data;
  4463. if (scb_data == NULL)
  4464. return;
  4465. switch (scb_data->init_level) {
  4466. default:
  4467. case 7:
  4468. {
  4469. struct sg_map_node *sg_map;
  4470. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
  4471. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  4472. ahc_dmamap_unload(ahc, scb_data->sg_dmat,
  4473. sg_map->sg_dmamap);
  4474. ahc_dmamem_free(ahc, scb_data->sg_dmat,
  4475. sg_map->sg_vaddr,
  4476. sg_map->sg_dmamap);
  4477. free(sg_map, M_DEVBUF);
  4478. }
  4479. ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
  4480. }
  4481. case 6:
  4482. ahc_dmamap_unload(ahc, scb_data->sense_dmat,
  4483. scb_data->sense_dmamap);
  4484. case 5:
  4485. ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
  4486. scb_data->sense_dmamap);
  4487. ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
  4488. scb_data->sense_dmamap);
  4489. case 4:
  4490. ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
  4491. case 3:
  4492. ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
  4493. scb_data->hscb_dmamap);
  4494. case 2:
  4495. ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
  4496. scb_data->hscb_dmamap);
  4497. ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
  4498. scb_data->hscb_dmamap);
  4499. case 1:
  4500. ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
  4501. break;
  4502. case 0:
  4503. break;
  4504. }
  4505. if (scb_data->scbarray != NULL)
  4506. free(scb_data->scbarray, M_DEVBUF);
  4507. }
  4508. static void
  4509. ahc_alloc_scbs(struct ahc_softc *ahc)
  4510. {
  4511. struct scb_data *scb_data;
  4512. struct scb *next_scb;
  4513. struct sg_map_node *sg_map;
  4514. dma_addr_t physaddr;
  4515. struct ahc_dma_seg *segs;
  4516. int newcount;
  4517. int i;
  4518. scb_data = ahc->scb_data;
  4519. if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
  4520. /* Can't allocate any more */
  4521. return;
  4522. next_scb = &scb_data->scbarray[scb_data->numscbs];
  4523. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  4524. if (sg_map == NULL)
  4525. return;
  4526. /* Allocate S/G space for the next batch of SCBS */
  4527. if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
  4528. (void **)&sg_map->sg_vaddr,
  4529. BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  4530. free(sg_map, M_DEVBUF);
  4531. return;
  4532. }
  4533. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  4534. ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
  4535. sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
  4536. &sg_map->sg_physaddr, /*flags*/0);
  4537. segs = sg_map->sg_vaddr;
  4538. physaddr = sg_map->sg_physaddr;
  4539. newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
  4540. newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
  4541. for (i = 0; i < newcount; i++) {
  4542. struct scb_platform_data *pdata;
  4543. #ifndef __linux__
  4544. int error;
  4545. #endif
  4546. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  4547. M_DEVBUF, M_NOWAIT);
  4548. if (pdata == NULL)
  4549. break;
  4550. next_scb->platform_data = pdata;
  4551. next_scb->sg_map = sg_map;
  4552. next_scb->sg_list = segs;
  4553. /*
  4554. * The sequencer always starts with the second entry.
  4555. * The first entry is embedded in the scb.
  4556. */
  4557. next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
  4558. next_scb->ahc_softc = ahc;
  4559. next_scb->flags = SCB_FREE;
  4560. #ifndef __linux__
  4561. error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
  4562. &next_scb->dmamap);
  4563. if (error != 0)
  4564. break;
  4565. #endif
  4566. next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
  4567. next_scb->hscb->tag = ahc->scb_data->numscbs;
  4568. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
  4569. next_scb, links.sle);
  4570. segs += AHC_NSEG;
  4571. physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
  4572. next_scb++;
  4573. ahc->scb_data->numscbs++;
  4574. }
  4575. }
  4576. void
  4577. ahc_controller_info(struct ahc_softc *ahc, char *buf)
  4578. {
  4579. int len;
  4580. len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
  4581. buf += len;
  4582. if ((ahc->features & AHC_TWIN) != 0)
  4583. len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
  4584. "B SCSI Id=%d, primary %c, ",
  4585. ahc->our_id, ahc->our_id_b,
  4586. (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
  4587. else {
  4588. const char *speed;
  4589. const char *type;
  4590. speed = "";
  4591. if ((ahc->features & AHC_ULTRA) != 0) {
  4592. speed = "Ultra ";
  4593. } else if ((ahc->features & AHC_DT) != 0) {
  4594. speed = "Ultra160 ";
  4595. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  4596. speed = "Ultra2 ";
  4597. }
  4598. if ((ahc->features & AHC_WIDE) != 0) {
  4599. type = "Wide";
  4600. } else {
  4601. type = "Single";
  4602. }
  4603. len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
  4604. speed, type, ahc->channel, ahc->our_id);
  4605. }
  4606. buf += len;
  4607. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4608. sprintf(buf, "%d/%d SCBs",
  4609. ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
  4610. else
  4611. sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
  4612. }
  4613. int
  4614. ahc_chip_init(struct ahc_softc *ahc)
  4615. {
  4616. int term;
  4617. int error;
  4618. u_int i;
  4619. u_int scsi_conf;
  4620. u_int scsiseq_template;
  4621. uint32_t physaddr;
  4622. ahc_outb(ahc, SEQ_FLAGS, 0);
  4623. ahc_outb(ahc, SEQ_FLAGS2, 0);
  4624. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
  4625. if (ahc->features & AHC_TWIN) {
  4626. /*
  4627. * Setup Channel B first.
  4628. */
  4629. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
  4630. term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
  4631. ahc_outb(ahc, SCSIID, ahc->our_id_b);
  4632. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4633. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4634. |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
  4635. if ((ahc->features & AHC_ULTRA2) != 0)
  4636. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4637. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4638. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4639. /* Select Channel A */
  4640. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
  4641. }
  4642. term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
  4643. if ((ahc->features & AHC_ULTRA2) != 0)
  4644. ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
  4645. else
  4646. ahc_outb(ahc, SCSIID, ahc->our_id);
  4647. scsi_conf = ahc_inb(ahc, SCSICONF);
  4648. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4649. |term|ahc->seltime
  4650. |ENSTIMER|ACTNEGEN);
  4651. if ((ahc->features & AHC_ULTRA2) != 0)
  4652. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4653. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4654. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4655. /* There are no untagged SCBs active yet. */
  4656. for (i = 0; i < 16; i++) {
  4657. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
  4658. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4659. int lun;
  4660. /*
  4661. * The SCB based BTT allows an entry per
  4662. * target and lun pair.
  4663. */
  4664. for (lun = 1; lun < AHC_NUM_LUNS; lun++)
  4665. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
  4666. }
  4667. }
  4668. /* All of our queues are empty */
  4669. for (i = 0; i < 256; i++)
  4670. ahc->qoutfifo[i] = SCB_LIST_NULL;
  4671. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
  4672. for (i = 0; i < 256; i++)
  4673. ahc->qinfifo[i] = SCB_LIST_NULL;
  4674. if ((ahc->features & AHC_MULTI_TID) != 0) {
  4675. ahc_outb(ahc, TARGID, 0);
  4676. ahc_outb(ahc, TARGID + 1, 0);
  4677. }
  4678. /*
  4679. * Tell the sequencer where it can find our arrays in memory.
  4680. */
  4681. physaddr = ahc->scb_data->hscb_busaddr;
  4682. ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
  4683. ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
  4684. ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
  4685. ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
  4686. physaddr = ahc->shared_data_busaddr;
  4687. ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
  4688. ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
  4689. ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
  4690. ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
  4691. /*
  4692. * Initialize the group code to command length table.
  4693. * This overrides the values in TARG_SCSIRATE, so only
  4694. * setup the table after we have processed that information.
  4695. */
  4696. ahc_outb(ahc, CMDSIZE_TABLE, 5);
  4697. ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
  4698. ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
  4699. ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
  4700. ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
  4701. ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
  4702. ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
  4703. ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
  4704. if ((ahc->features & AHC_HS_MAILBOX) != 0)
  4705. ahc_outb(ahc, HS_MAILBOX, 0);
  4706. /* Tell the sequencer of our initial queue positions */
  4707. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4708. ahc->tqinfifonext = 1;
  4709. ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
  4710. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  4711. }
  4712. ahc->qinfifonext = 0;
  4713. ahc->qoutfifonext = 0;
  4714. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4715. ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
  4716. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4717. ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
  4718. ahc_outb(ahc, SDSCB_QOFF, 0);
  4719. } else {
  4720. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4721. ahc_outb(ahc, QINPOS, ahc->qinfifonext);
  4722. ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
  4723. }
  4724. /* We don't have any waiting selections */
  4725. ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
  4726. /* Our disconnection list is empty too */
  4727. ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
  4728. /* Message out buffer starts empty */
  4729. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  4730. /*
  4731. * Setup the allowed SCSI Sequences based on operational mode.
  4732. * If we are a target, we'll enalbe select in operations once
  4733. * we've had a lun enabled.
  4734. */
  4735. scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
  4736. if ((ahc->flags & AHC_INITIATORROLE) != 0)
  4737. scsiseq_template |= ENRSELI;
  4738. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
  4739. /* Initialize our list of free SCBs. */
  4740. ahc_build_free_scb_list(ahc);
  4741. /*
  4742. * Tell the sequencer which SCB will be the next one it receives.
  4743. */
  4744. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4745. /*
  4746. * Load the Sequencer program and Enable the adapter
  4747. * in "fast" mode.
  4748. */
  4749. if (bootverbose)
  4750. printf("%s: Downloading Sequencer Program...",
  4751. ahc_name(ahc));
  4752. error = ahc_loadseq(ahc);
  4753. if (error != 0)
  4754. return (error);
  4755. if ((ahc->features & AHC_ULTRA2) != 0) {
  4756. int wait;
  4757. /*
  4758. * Wait for up to 500ms for our transceivers
  4759. * to settle. If the adapter does not have
  4760. * a cable attached, the transceivers may
  4761. * never settle, so don't complain if we
  4762. * fail here.
  4763. */
  4764. for (wait = 5000;
  4765. (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  4766. wait--)
  4767. ahc_delay(100);
  4768. }
  4769. ahc_restart(ahc);
  4770. return (0);
  4771. }
  4772. /*
  4773. * Start the board, ready for normal operation
  4774. */
  4775. int
  4776. ahc_init(struct ahc_softc *ahc)
  4777. {
  4778. int max_targ;
  4779. u_int i;
  4780. u_int scsi_conf;
  4781. u_int ultraenb;
  4782. u_int discenable;
  4783. u_int tagenable;
  4784. size_t driver_data_size;
  4785. #ifdef AHC_DEBUG
  4786. if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
  4787. ahc->flags |= AHC_SEQUENCER_DEBUG;
  4788. #endif
  4789. #ifdef AHC_PRINT_SRAM
  4790. printf("Scratch Ram:");
  4791. for (i = 0x20; i < 0x5f; i++) {
  4792. if (((i % 8) == 0) && (i != 0)) {
  4793. printf ("\n ");
  4794. }
  4795. printf (" 0x%x", ahc_inb(ahc, i));
  4796. }
  4797. if ((ahc->features & AHC_MORE_SRAM) != 0) {
  4798. for (i = 0x70; i < 0x7f; i++) {
  4799. if (((i % 8) == 0) && (i != 0)) {
  4800. printf ("\n ");
  4801. }
  4802. printf (" 0x%x", ahc_inb(ahc, i));
  4803. }
  4804. }
  4805. printf ("\n");
  4806. /*
  4807. * Reading uninitialized scratch ram may
  4808. * generate parity errors.
  4809. */
  4810. ahc_outb(ahc, CLRINT, CLRPARERR);
  4811. ahc_outb(ahc, CLRINT, CLRBRKADRINT);
  4812. #endif
  4813. max_targ = 15;
  4814. /*
  4815. * Assume we have a board at this stage and it has been reset.
  4816. */
  4817. if ((ahc->flags & AHC_USEDEFAULTS) != 0)
  4818. ahc->our_id = ahc->our_id_b = 7;
  4819. /*
  4820. * Default to allowing initiator operations.
  4821. */
  4822. ahc->flags |= AHC_INITIATORROLE;
  4823. /*
  4824. * Only allow target mode features if this unit has them enabled.
  4825. */
  4826. if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
  4827. ahc->features &= ~AHC_TARGETMODE;
  4828. #ifndef __linux__
  4829. /* DMA tag for mapping buffers into device visible space. */
  4830. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4831. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4832. /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
  4833. ? (dma_addr_t)0x7FFFFFFFFFULL
  4834. : BUS_SPACE_MAXADDR_32BIT,
  4835. /*highaddr*/BUS_SPACE_MAXADDR,
  4836. /*filter*/NULL, /*filterarg*/NULL,
  4837. /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
  4838. /*nsegments*/AHC_NSEG,
  4839. /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
  4840. /*flags*/BUS_DMA_ALLOCNOW,
  4841. &ahc->buffer_dmat) != 0) {
  4842. return (ENOMEM);
  4843. }
  4844. #endif
  4845. ahc->init_level++;
  4846. /*
  4847. * DMA tag for our command fifos and other data in system memory
  4848. * the card's sequencer must be able to access. For initiator
  4849. * roles, we need to allocate space for the qinfifo and qoutfifo.
  4850. * The qinfifo and qoutfifo are composed of 256 1 byte elements.
  4851. * When providing for the target mode role, we must additionally
  4852. * provide space for the incoming target command fifo and an extra
  4853. * byte to deal with a dma bug in some chip versions.
  4854. */
  4855. driver_data_size = 2 * 256 * sizeof(uint8_t);
  4856. if ((ahc->features & AHC_TARGETMODE) != 0)
  4857. driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
  4858. + /*DMA WideOdd Bug Buffer*/1;
  4859. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4860. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4861. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4862. /*highaddr*/BUS_SPACE_MAXADDR,
  4863. /*filter*/NULL, /*filterarg*/NULL,
  4864. driver_data_size,
  4865. /*nsegments*/1,
  4866. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4867. /*flags*/0, &ahc->shared_data_dmat) != 0) {
  4868. return (ENOMEM);
  4869. }
  4870. ahc->init_level++;
  4871. /* Allocation of driver data */
  4872. if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
  4873. (void **)&ahc->qoutfifo,
  4874. BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
  4875. return (ENOMEM);
  4876. }
  4877. ahc->init_level++;
  4878. /* And permanently map it in */
  4879. ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  4880. ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
  4881. &ahc->shared_data_busaddr, /*flags*/0);
  4882. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4883. ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
  4884. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
  4885. ahc->dma_bug_buf = ahc->shared_data_busaddr
  4886. + driver_data_size - 1;
  4887. /* All target command blocks start out invalid. */
  4888. for (i = 0; i < AHC_TMODE_CMDS; i++)
  4889. ahc->targetcmds[i].cmd_valid = 0;
  4890. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
  4891. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
  4892. }
  4893. ahc->qinfifo = &ahc->qoutfifo[256];
  4894. ahc->init_level++;
  4895. /* Allocate SCB data now that buffer_dmat is initialized */
  4896. if (ahc->scb_data->maxhscbs == 0)
  4897. if (ahc_init_scbdata(ahc) != 0)
  4898. return (ENOMEM);
  4899. /*
  4900. * Allocate a tstate to house information for our
  4901. * initiator presence on the bus as well as the user
  4902. * data for any target mode initiator.
  4903. */
  4904. if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
  4905. printf("%s: unable to allocate ahc_tmode_tstate. "
  4906. "Failing attach\n", ahc_name(ahc));
  4907. return (ENOMEM);
  4908. }
  4909. if ((ahc->features & AHC_TWIN) != 0) {
  4910. if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
  4911. printf("%s: unable to allocate ahc_tmode_tstate. "
  4912. "Failing attach\n", ahc_name(ahc));
  4913. return (ENOMEM);
  4914. }
  4915. }
  4916. if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
  4917. ahc->flags |= AHC_PAGESCBS;
  4918. } else {
  4919. ahc->flags &= ~AHC_PAGESCBS;
  4920. }
  4921. #ifdef AHC_DEBUG
  4922. if (ahc_debug & AHC_SHOW_MISC) {
  4923. printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
  4924. "ahc_dma %u bytes\n",
  4925. ahc_name(ahc),
  4926. (u_int)sizeof(struct hardware_scb),
  4927. (u_int)sizeof(struct scb),
  4928. (u_int)sizeof(struct ahc_dma_seg));
  4929. }
  4930. #endif /* AHC_DEBUG */
  4931. /*
  4932. * Look at the information that board initialization or
  4933. * the board bios has left us.
  4934. */
  4935. if (ahc->features & AHC_TWIN) {
  4936. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4937. if ((scsi_conf & RESET_SCSI) != 0
  4938. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4939. ahc->flags |= AHC_RESET_BUS_B;
  4940. }
  4941. scsi_conf = ahc_inb(ahc, SCSICONF);
  4942. if ((scsi_conf & RESET_SCSI) != 0
  4943. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4944. ahc->flags |= AHC_RESET_BUS_A;
  4945. ultraenb = 0;
  4946. tagenable = ALL_TARGETS_MASK;
  4947. /* Grab the disconnection disable table and invert it for our needs */
  4948. if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
  4949. printf("%s: Host Adapter Bios disabled. Using default SCSI "
  4950. "device parameters\n", ahc_name(ahc));
  4951. ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
  4952. AHC_TERM_ENB_A|AHC_TERM_ENB_B;
  4953. discenable = ALL_TARGETS_MASK;
  4954. if ((ahc->features & AHC_ULTRA) != 0)
  4955. ultraenb = ALL_TARGETS_MASK;
  4956. } else {
  4957. discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
  4958. | ahc_inb(ahc, DISC_DSB));
  4959. if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
  4960. ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
  4961. | ahc_inb(ahc, ULTRA_ENB);
  4962. }
  4963. if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
  4964. max_targ = 7;
  4965. for (i = 0; i <= max_targ; i++) {
  4966. struct ahc_initiator_tinfo *tinfo;
  4967. struct ahc_tmode_tstate *tstate;
  4968. u_int our_id;
  4969. u_int target_id;
  4970. char channel;
  4971. channel = 'A';
  4972. our_id = ahc->our_id;
  4973. target_id = i;
  4974. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  4975. channel = 'B';
  4976. our_id = ahc->our_id_b;
  4977. target_id = i % 8;
  4978. }
  4979. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  4980. target_id, &tstate);
  4981. /* Default to async narrow across the board */
  4982. memset(tinfo, 0, sizeof(*tinfo));
  4983. if (ahc->flags & AHC_USEDEFAULTS) {
  4984. if ((ahc->features & AHC_WIDE) != 0)
  4985. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4986. /*
  4987. * These will be truncated when we determine the
  4988. * connection type we have with the target.
  4989. */
  4990. tinfo->user.period = ahc_syncrates->period;
  4991. tinfo->user.offset = MAX_OFFSET;
  4992. } else {
  4993. u_int scsirate;
  4994. uint16_t mask;
  4995. /* Take the settings leftover in scratch RAM. */
  4996. scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
  4997. mask = (0x01 << i);
  4998. if ((ahc->features & AHC_ULTRA2) != 0) {
  4999. u_int offset;
  5000. u_int maxsync;
  5001. if ((scsirate & SOFS) == 0x0F) {
  5002. /*
  5003. * Haven't negotiated yet,
  5004. * so the format is different.
  5005. */
  5006. scsirate = (scsirate & SXFR) >> 4
  5007. | (ultraenb & mask)
  5008. ? 0x08 : 0x0
  5009. | (scsirate & WIDEXFER);
  5010. offset = MAX_OFFSET_ULTRA2;
  5011. } else
  5012. offset = ahc_inb(ahc, TARG_OFFSET + i);
  5013. if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
  5014. /* Set to the lowest sync rate, 5MHz */
  5015. scsirate |= 0x1c;
  5016. maxsync = AHC_SYNCRATE_ULTRA2;
  5017. if ((ahc->features & AHC_DT) != 0)
  5018. maxsync = AHC_SYNCRATE_DT;
  5019. tinfo->user.period =
  5020. ahc_find_period(ahc, scsirate, maxsync);
  5021. if (offset == 0)
  5022. tinfo->user.period = 0;
  5023. else
  5024. tinfo->user.offset = MAX_OFFSET;
  5025. if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
  5026. && (ahc->features & AHC_DT) != 0)
  5027. tinfo->user.ppr_options =
  5028. MSG_EXT_PPR_DT_REQ;
  5029. } else if ((scsirate & SOFS) != 0) {
  5030. if ((scsirate & SXFR) == 0x40
  5031. && (ultraenb & mask) != 0) {
  5032. /* Treat 10MHz as a non-ultra speed */
  5033. scsirate &= ~SXFR;
  5034. ultraenb &= ~mask;
  5035. }
  5036. tinfo->user.period =
  5037. ahc_find_period(ahc, scsirate,
  5038. (ultraenb & mask)
  5039. ? AHC_SYNCRATE_ULTRA
  5040. : AHC_SYNCRATE_FAST);
  5041. if (tinfo->user.period != 0)
  5042. tinfo->user.offset = MAX_OFFSET;
  5043. }
  5044. if (tinfo->user.period == 0)
  5045. tinfo->user.offset = 0;
  5046. if ((scsirate & WIDEXFER) != 0
  5047. && (ahc->features & AHC_WIDE) != 0)
  5048. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  5049. tinfo->user.protocol_version = 4;
  5050. if ((ahc->features & AHC_DT) != 0)
  5051. tinfo->user.transport_version = 3;
  5052. else
  5053. tinfo->user.transport_version = 2;
  5054. tinfo->goal.protocol_version = 2;
  5055. tinfo->goal.transport_version = 2;
  5056. tinfo->curr.protocol_version = 2;
  5057. tinfo->curr.transport_version = 2;
  5058. }
  5059. tstate->ultraenb = 0;
  5060. }
  5061. ahc->user_discenable = discenable;
  5062. ahc->user_tagenable = tagenable;
  5063. return (ahc->bus_chip_init(ahc));
  5064. }
  5065. void
  5066. ahc_intr_enable(struct ahc_softc *ahc, int enable)
  5067. {
  5068. u_int hcntrl;
  5069. hcntrl = ahc_inb(ahc, HCNTRL);
  5070. hcntrl &= ~INTEN;
  5071. ahc->pause &= ~INTEN;
  5072. ahc->unpause &= ~INTEN;
  5073. if (enable) {
  5074. hcntrl |= INTEN;
  5075. ahc->pause |= INTEN;
  5076. ahc->unpause |= INTEN;
  5077. }
  5078. ahc_outb(ahc, HCNTRL, hcntrl);
  5079. }
  5080. /*
  5081. * Ensure that the card is paused in a location
  5082. * outside of all critical sections and that all
  5083. * pending work is completed prior to returning.
  5084. * This routine should only be called from outside
  5085. * an interrupt context.
  5086. */
  5087. void
  5088. ahc_pause_and_flushwork(struct ahc_softc *ahc)
  5089. {
  5090. int intstat;
  5091. int maxloops;
  5092. int paused;
  5093. maxloops = 1000;
  5094. ahc->flags |= AHC_ALL_INTERRUPTS;
  5095. paused = FALSE;
  5096. do {
  5097. if (paused) {
  5098. ahc_unpause(ahc);
  5099. /*
  5100. * Give the sequencer some time to service
  5101. * any active selections.
  5102. */
  5103. ahc_delay(500);
  5104. }
  5105. ahc_intr(ahc);
  5106. ahc_pause(ahc);
  5107. paused = TRUE;
  5108. ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  5109. intstat = ahc_inb(ahc, INTSTAT);
  5110. if ((intstat & INT_PEND) == 0) {
  5111. ahc_clear_critical_section(ahc);
  5112. intstat = ahc_inb(ahc, INTSTAT);
  5113. }
  5114. } while (--maxloops
  5115. && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
  5116. && ((intstat & INT_PEND) != 0
  5117. || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
  5118. if (maxloops == 0) {
  5119. printf("Infinite interrupt loop, INTSTAT = %x",
  5120. ahc_inb(ahc, INTSTAT));
  5121. }
  5122. ahc_platform_flushwork(ahc);
  5123. ahc->flags &= ~AHC_ALL_INTERRUPTS;
  5124. }
  5125. #ifdef CONFIG_PM
  5126. int
  5127. ahc_suspend(struct ahc_softc *ahc)
  5128. {
  5129. ahc_pause_and_flushwork(ahc);
  5130. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  5131. ahc_unpause(ahc);
  5132. return (EBUSY);
  5133. }
  5134. #ifdef AHC_TARGET_MODE
  5135. /*
  5136. * XXX What about ATIOs that have not yet been serviced?
  5137. * Perhaps we should just refuse to be suspended if we
  5138. * are acting in a target role.
  5139. */
  5140. if (ahc->pending_device != NULL) {
  5141. ahc_unpause(ahc);
  5142. return (EBUSY);
  5143. }
  5144. #endif
  5145. ahc_shutdown(ahc);
  5146. return (0);
  5147. }
  5148. int
  5149. ahc_resume(struct ahc_softc *ahc)
  5150. {
  5151. ahc_reset(ahc, /*reinit*/TRUE);
  5152. ahc_intr_enable(ahc, TRUE);
  5153. ahc_restart(ahc);
  5154. return (0);
  5155. }
  5156. #endif
  5157. /************************** Busy Target Table *********************************/
  5158. /*
  5159. * Return the untagged transaction id for a given target/channel lun.
  5160. * Optionally, clear the entry.
  5161. */
  5162. static u_int
  5163. ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
  5164. {
  5165. u_int scbid;
  5166. u_int target_offset;
  5167. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5168. u_int saved_scbptr;
  5169. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5170. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5171. scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
  5172. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5173. } else {
  5174. target_offset = TCL_TARGET_OFFSET(tcl);
  5175. scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
  5176. }
  5177. return (scbid);
  5178. }
  5179. static void
  5180. ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
  5181. {
  5182. u_int target_offset;
  5183. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5184. u_int saved_scbptr;
  5185. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5186. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5187. ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
  5188. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5189. } else {
  5190. target_offset = TCL_TARGET_OFFSET(tcl);
  5191. ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
  5192. }
  5193. }
  5194. static void
  5195. ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
  5196. {
  5197. u_int target_offset;
  5198. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  5199. u_int saved_scbptr;
  5200. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5201. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  5202. ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
  5203. ahc_outb(ahc, SCBPTR, saved_scbptr);
  5204. } else {
  5205. target_offset = TCL_TARGET_OFFSET(tcl);
  5206. ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
  5207. }
  5208. }
  5209. /************************** SCB and SCB queue management **********************/
  5210. int
  5211. ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
  5212. char channel, int lun, u_int tag, role_t role)
  5213. {
  5214. int targ = SCB_GET_TARGET(ahc, scb);
  5215. char chan = SCB_GET_CHANNEL(ahc, scb);
  5216. int slun = SCB_GET_LUN(scb);
  5217. int match;
  5218. match = ((chan == channel) || (channel == ALL_CHANNELS));
  5219. if (match != 0)
  5220. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  5221. if (match != 0)
  5222. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  5223. if (match != 0) {
  5224. #ifdef AHC_TARGET_MODE
  5225. int group;
  5226. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  5227. if (role == ROLE_INITIATOR) {
  5228. match = (group != XPT_FC_GROUP_TMODE)
  5229. && ((tag == scb->hscb->tag)
  5230. || (tag == SCB_LIST_NULL));
  5231. } else if (role == ROLE_TARGET) {
  5232. match = (group == XPT_FC_GROUP_TMODE)
  5233. && ((tag == scb->io_ctx->csio.tag_id)
  5234. || (tag == SCB_LIST_NULL));
  5235. }
  5236. #else /* !AHC_TARGET_MODE */
  5237. match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
  5238. #endif /* AHC_TARGET_MODE */
  5239. }
  5240. return match;
  5241. }
  5242. static void
  5243. ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  5244. {
  5245. int target;
  5246. char channel;
  5247. int lun;
  5248. target = SCB_GET_TARGET(ahc, scb);
  5249. lun = SCB_GET_LUN(scb);
  5250. channel = SCB_GET_CHANNEL(ahc, scb);
  5251. ahc_search_qinfifo(ahc, target, channel, lun,
  5252. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  5253. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5254. ahc_platform_freeze_devq(ahc, scb);
  5255. }
  5256. void
  5257. ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
  5258. {
  5259. struct scb *prev_scb;
  5260. prev_scb = NULL;
  5261. if (ahc_qinfifo_count(ahc) != 0) {
  5262. u_int prev_tag;
  5263. uint8_t prev_pos;
  5264. prev_pos = ahc->qinfifonext - 1;
  5265. prev_tag = ahc->qinfifo[prev_pos];
  5266. prev_scb = ahc_lookup_scb(ahc, prev_tag);
  5267. }
  5268. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5269. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5270. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  5271. } else {
  5272. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  5273. }
  5274. }
  5275. static void
  5276. ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
  5277. struct scb *scb)
  5278. {
  5279. if (prev_scb == NULL) {
  5280. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  5281. } else {
  5282. prev_scb->hscb->next = scb->hscb->tag;
  5283. ahc_sync_scb(ahc, prev_scb,
  5284. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  5285. }
  5286. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  5287. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  5288. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  5289. }
  5290. static int
  5291. ahc_qinfifo_count(struct ahc_softc *ahc)
  5292. {
  5293. uint8_t qinpos;
  5294. uint8_t diff;
  5295. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5296. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  5297. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  5298. } else
  5299. qinpos = ahc_inb(ahc, QINPOS);
  5300. diff = ahc->qinfifonext - qinpos;
  5301. return (diff);
  5302. }
  5303. int
  5304. ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
  5305. int lun, u_int tag, role_t role, uint32_t status,
  5306. ahc_search_action action)
  5307. {
  5308. struct scb *scb;
  5309. struct scb *prev_scb;
  5310. uint8_t qinstart;
  5311. uint8_t qinpos;
  5312. uint8_t qintail;
  5313. uint8_t next;
  5314. uint8_t prev;
  5315. uint8_t curscbptr;
  5316. int found;
  5317. int have_qregs;
  5318. qintail = ahc->qinfifonext;
  5319. have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
  5320. if (have_qregs) {
  5321. qinstart = ahc_inb(ahc, SNSCB_QOFF);
  5322. ahc_outb(ahc, SNSCB_QOFF, qinstart);
  5323. } else
  5324. qinstart = ahc_inb(ahc, QINPOS);
  5325. qinpos = qinstart;
  5326. found = 0;
  5327. prev_scb = NULL;
  5328. if (action == SEARCH_COMPLETE) {
  5329. /*
  5330. * Don't attempt to run any queued untagged transactions
  5331. * until we are done with the abort process.
  5332. */
  5333. ahc_freeze_untagged_queues(ahc);
  5334. }
  5335. /*
  5336. * Start with an empty queue. Entries that are not chosen
  5337. * for removal will be re-added to the queue as we go.
  5338. */
  5339. ahc->qinfifonext = qinpos;
  5340. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  5341. while (qinpos != qintail) {
  5342. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
  5343. if (scb == NULL) {
  5344. printf("qinpos = %d, SCB index = %d\n",
  5345. qinpos, ahc->qinfifo[qinpos]);
  5346. panic("Loop 1\n");
  5347. }
  5348. if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
  5349. /*
  5350. * We found an scb that needs to be acted on.
  5351. */
  5352. found++;
  5353. switch (action) {
  5354. case SEARCH_COMPLETE:
  5355. {
  5356. cam_status ostat;
  5357. cam_status cstat;
  5358. ostat = ahc_get_transaction_status(scb);
  5359. if (ostat == CAM_REQ_INPROG)
  5360. ahc_set_transaction_status(scb, status);
  5361. cstat = ahc_get_transaction_status(scb);
  5362. if (cstat != CAM_REQ_CMP)
  5363. ahc_freeze_scb(scb);
  5364. if ((scb->flags & SCB_ACTIVE) == 0)
  5365. printf("Inactive SCB in qinfifo\n");
  5366. ahc_done(ahc, scb);
  5367. /* FALLTHROUGH */
  5368. }
  5369. case SEARCH_REMOVE:
  5370. break;
  5371. case SEARCH_COUNT:
  5372. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5373. prev_scb = scb;
  5374. break;
  5375. }
  5376. } else {
  5377. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  5378. prev_scb = scb;
  5379. }
  5380. qinpos++;
  5381. }
  5382. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  5383. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  5384. } else {
  5385. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  5386. }
  5387. if (action != SEARCH_COUNT
  5388. && (found != 0)
  5389. && (qinstart != ahc->qinfifonext)) {
  5390. /*
  5391. * The sequencer may be in the process of dmaing
  5392. * down the SCB at the beginning of the queue.
  5393. * This could be problematic if either the first,
  5394. * or the second SCB is removed from the queue
  5395. * (the first SCB includes a pointer to the "next"
  5396. * SCB to dma). If we have removed any entries, swap
  5397. * the first element in the queue with the next HSCB
  5398. * so the sequencer will notice that NEXT_QUEUED_SCB
  5399. * has changed during its dma attempt and will retry
  5400. * the DMA.
  5401. */
  5402. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
  5403. if (scb == NULL) {
  5404. printf("found = %d, qinstart = %d, qinfifionext = %d\n",
  5405. found, qinstart, ahc->qinfifonext);
  5406. panic("First/Second Qinfifo fixup\n");
  5407. }
  5408. /*
  5409. * ahc_swap_with_next_hscb forces our next pointer to
  5410. * point to the reserved SCB for future commands. Save
  5411. * and restore our original next pointer to maintain
  5412. * queue integrity.
  5413. */
  5414. next = scb->hscb->next;
  5415. ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
  5416. ahc_swap_with_next_hscb(ahc, scb);
  5417. scb->hscb->next = next;
  5418. ahc->qinfifo[qinstart] = scb->hscb->tag;
  5419. /* Tell the card about the new head of the qinfifo. */
  5420. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  5421. /* Fixup the tail "next" pointer. */
  5422. qintail = ahc->qinfifonext - 1;
  5423. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
  5424. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  5425. }
  5426. /*
  5427. * Search waiting for selection list.
  5428. */
  5429. curscbptr = ahc_inb(ahc, SCBPTR);
  5430. next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
  5431. prev = SCB_LIST_NULL;
  5432. while (next != SCB_LIST_NULL) {
  5433. uint8_t scb_index;
  5434. ahc_outb(ahc, SCBPTR, next);
  5435. scb_index = ahc_inb(ahc, SCB_TAG);
  5436. if (scb_index >= ahc->scb_data->numscbs) {
  5437. printf("Waiting List inconsistency. "
  5438. "SCB index == %d, yet numscbs == %d.",
  5439. scb_index, ahc->scb_data->numscbs);
  5440. ahc_dump_card_state(ahc);
  5441. panic("for safety");
  5442. }
  5443. scb = ahc_lookup_scb(ahc, scb_index);
  5444. if (scb == NULL) {
  5445. printf("scb_index = %d, next = %d\n",
  5446. scb_index, next);
  5447. panic("Waiting List traversal\n");
  5448. }
  5449. if (ahc_match_scb(ahc, scb, target, channel,
  5450. lun, SCB_LIST_NULL, role)) {
  5451. /*
  5452. * We found an scb that needs to be acted on.
  5453. */
  5454. found++;
  5455. switch (action) {
  5456. case SEARCH_COMPLETE:
  5457. {
  5458. cam_status ostat;
  5459. cam_status cstat;
  5460. ostat = ahc_get_transaction_status(scb);
  5461. if (ostat == CAM_REQ_INPROG)
  5462. ahc_set_transaction_status(scb,
  5463. status);
  5464. cstat = ahc_get_transaction_status(scb);
  5465. if (cstat != CAM_REQ_CMP)
  5466. ahc_freeze_scb(scb);
  5467. if ((scb->flags & SCB_ACTIVE) == 0)
  5468. printf("Inactive SCB in Waiting List\n");
  5469. ahc_done(ahc, scb);
  5470. /* FALLTHROUGH */
  5471. }
  5472. case SEARCH_REMOVE:
  5473. next = ahc_rem_wscb(ahc, next, prev);
  5474. break;
  5475. case SEARCH_COUNT:
  5476. prev = next;
  5477. next = ahc_inb(ahc, SCB_NEXT);
  5478. break;
  5479. }
  5480. } else {
  5481. prev = next;
  5482. next = ahc_inb(ahc, SCB_NEXT);
  5483. }
  5484. }
  5485. ahc_outb(ahc, SCBPTR, curscbptr);
  5486. found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
  5487. channel, lun, status, action);
  5488. if (action == SEARCH_COMPLETE)
  5489. ahc_release_untagged_queues(ahc);
  5490. return (found);
  5491. }
  5492. int
  5493. ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
  5494. int target, char channel, int lun, uint32_t status,
  5495. ahc_search_action action)
  5496. {
  5497. struct scb *scb;
  5498. int maxtarget;
  5499. int found;
  5500. int i;
  5501. if (action == SEARCH_COMPLETE) {
  5502. /*
  5503. * Don't attempt to run any queued untagged transactions
  5504. * until we are done with the abort process.
  5505. */
  5506. ahc_freeze_untagged_queues(ahc);
  5507. }
  5508. found = 0;
  5509. i = 0;
  5510. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  5511. maxtarget = 16;
  5512. if (target != CAM_TARGET_WILDCARD) {
  5513. i = target;
  5514. if (channel == 'B')
  5515. i += 8;
  5516. maxtarget = i + 1;
  5517. }
  5518. } else {
  5519. maxtarget = 0;
  5520. }
  5521. for (; i < maxtarget; i++) {
  5522. struct scb_tailq *untagged_q;
  5523. struct scb *next_scb;
  5524. untagged_q = &(ahc->untagged_queues[i]);
  5525. next_scb = TAILQ_FIRST(untagged_q);
  5526. while (next_scb != NULL) {
  5527. scb = next_scb;
  5528. next_scb = TAILQ_NEXT(scb, links.tqe);
  5529. /*
  5530. * The head of the list may be the currently
  5531. * active untagged command for a device.
  5532. * We're only searching for commands that
  5533. * have not been started. A transaction
  5534. * marked active but still in the qinfifo
  5535. * is removed by the qinfifo scanning code
  5536. * above.
  5537. */
  5538. if ((scb->flags & SCB_ACTIVE) != 0)
  5539. continue;
  5540. if (ahc_match_scb(ahc, scb, target, channel, lun,
  5541. SCB_LIST_NULL, ROLE_INITIATOR) == 0
  5542. || (ctx != NULL && ctx != scb->io_ctx))
  5543. continue;
  5544. /*
  5545. * We found an scb that needs to be acted on.
  5546. */
  5547. found++;
  5548. switch (action) {
  5549. case SEARCH_COMPLETE:
  5550. {
  5551. cam_status ostat;
  5552. cam_status cstat;
  5553. ostat = ahc_get_transaction_status(scb);
  5554. if (ostat == CAM_REQ_INPROG)
  5555. ahc_set_transaction_status(scb, status);
  5556. cstat = ahc_get_transaction_status(scb);
  5557. if (cstat != CAM_REQ_CMP)
  5558. ahc_freeze_scb(scb);
  5559. if ((scb->flags & SCB_ACTIVE) == 0)
  5560. printf("Inactive SCB in untaggedQ\n");
  5561. ahc_done(ahc, scb);
  5562. break;
  5563. }
  5564. case SEARCH_REMOVE:
  5565. scb->flags &= ~SCB_UNTAGGEDQ;
  5566. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  5567. break;
  5568. case SEARCH_COUNT:
  5569. break;
  5570. }
  5571. }
  5572. }
  5573. if (action == SEARCH_COMPLETE)
  5574. ahc_release_untagged_queues(ahc);
  5575. return (found);
  5576. }
  5577. int
  5578. ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
  5579. int lun, u_int tag, int stop_on_first, int remove,
  5580. int save_state)
  5581. {
  5582. struct scb *scbp;
  5583. u_int next;
  5584. u_int prev;
  5585. u_int count;
  5586. u_int active_scb;
  5587. count = 0;
  5588. next = ahc_inb(ahc, DISCONNECTED_SCBH);
  5589. prev = SCB_LIST_NULL;
  5590. if (save_state) {
  5591. /* restore this when we're done */
  5592. active_scb = ahc_inb(ahc, SCBPTR);
  5593. } else
  5594. /* Silence compiler */
  5595. active_scb = SCB_LIST_NULL;
  5596. while (next != SCB_LIST_NULL) {
  5597. u_int scb_index;
  5598. ahc_outb(ahc, SCBPTR, next);
  5599. scb_index = ahc_inb(ahc, SCB_TAG);
  5600. if (scb_index >= ahc->scb_data->numscbs) {
  5601. printf("Disconnected List inconsistency. "
  5602. "SCB index == %d, yet numscbs == %d.",
  5603. scb_index, ahc->scb_data->numscbs);
  5604. ahc_dump_card_state(ahc);
  5605. panic("for safety");
  5606. }
  5607. if (next == prev) {
  5608. panic("Disconnected List Loop. "
  5609. "cur SCBPTR == %x, prev SCBPTR == %x.",
  5610. next, prev);
  5611. }
  5612. scbp = ahc_lookup_scb(ahc, scb_index);
  5613. if (ahc_match_scb(ahc, scbp, target, channel, lun,
  5614. tag, ROLE_INITIATOR)) {
  5615. count++;
  5616. if (remove) {
  5617. next =
  5618. ahc_rem_scb_from_disc_list(ahc, prev, next);
  5619. } else {
  5620. prev = next;
  5621. next = ahc_inb(ahc, SCB_NEXT);
  5622. }
  5623. if (stop_on_first)
  5624. break;
  5625. } else {
  5626. prev = next;
  5627. next = ahc_inb(ahc, SCB_NEXT);
  5628. }
  5629. }
  5630. if (save_state)
  5631. ahc_outb(ahc, SCBPTR, active_scb);
  5632. return (count);
  5633. }
  5634. /*
  5635. * Remove an SCB from the on chip list of disconnected transactions.
  5636. * This is empty/unused if we are not performing SCB paging.
  5637. */
  5638. static u_int
  5639. ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
  5640. {
  5641. u_int next;
  5642. ahc_outb(ahc, SCBPTR, scbptr);
  5643. next = ahc_inb(ahc, SCB_NEXT);
  5644. ahc_outb(ahc, SCB_CONTROL, 0);
  5645. ahc_add_curscb_to_free_list(ahc);
  5646. if (prev != SCB_LIST_NULL) {
  5647. ahc_outb(ahc, SCBPTR, prev);
  5648. ahc_outb(ahc, SCB_NEXT, next);
  5649. } else
  5650. ahc_outb(ahc, DISCONNECTED_SCBH, next);
  5651. return (next);
  5652. }
  5653. /*
  5654. * Add the SCB as selected by SCBPTR onto the on chip list of
  5655. * free hardware SCBs. This list is empty/unused if we are not
  5656. * performing SCB paging.
  5657. */
  5658. static void
  5659. ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
  5660. {
  5661. /*
  5662. * Invalidate the tag so that our abort
  5663. * routines don't think it's active.
  5664. */
  5665. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  5666. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  5667. ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
  5668. ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
  5669. }
  5670. }
  5671. /*
  5672. * Manipulate the waiting for selection list and return the
  5673. * scb that follows the one that we remove.
  5674. */
  5675. static u_int
  5676. ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
  5677. {
  5678. u_int curscb, next;
  5679. /*
  5680. * Select the SCB we want to abort and
  5681. * pull the next pointer out of it.
  5682. */
  5683. curscb = ahc_inb(ahc, SCBPTR);
  5684. ahc_outb(ahc, SCBPTR, scbpos);
  5685. next = ahc_inb(ahc, SCB_NEXT);
  5686. /* Clear the necessary fields */
  5687. ahc_outb(ahc, SCB_CONTROL, 0);
  5688. ahc_add_curscb_to_free_list(ahc);
  5689. /* update the waiting list */
  5690. if (prev == SCB_LIST_NULL) {
  5691. /* First in the list */
  5692. ahc_outb(ahc, WAITING_SCBH, next);
  5693. /*
  5694. * Ensure we aren't attempting to perform
  5695. * selection for this entry.
  5696. */
  5697. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  5698. } else {
  5699. /*
  5700. * Select the scb that pointed to us
  5701. * and update its next pointer.
  5702. */
  5703. ahc_outb(ahc, SCBPTR, prev);
  5704. ahc_outb(ahc, SCB_NEXT, next);
  5705. }
  5706. /*
  5707. * Point us back at the original scb position.
  5708. */
  5709. ahc_outb(ahc, SCBPTR, curscb);
  5710. return next;
  5711. }
  5712. /******************************** Error Handling ******************************/
  5713. /*
  5714. * Abort all SCBs that match the given description (target/channel/lun/tag),
  5715. * setting their status to the passed in status if the status has not already
  5716. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  5717. * is paused before it is called.
  5718. */
  5719. static int
  5720. ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  5721. int lun, u_int tag, role_t role, uint32_t status)
  5722. {
  5723. struct scb *scbp;
  5724. struct scb *scbp_next;
  5725. u_int active_scb;
  5726. int i, j;
  5727. int maxtarget;
  5728. int minlun;
  5729. int maxlun;
  5730. int found;
  5731. /*
  5732. * Don't attempt to run any queued untagged transactions
  5733. * until we are done with the abort process.
  5734. */
  5735. ahc_freeze_untagged_queues(ahc);
  5736. /* restore this when we're done */
  5737. active_scb = ahc_inb(ahc, SCBPTR);
  5738. found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
  5739. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5740. /*
  5741. * Clean out the busy target table for any untagged commands.
  5742. */
  5743. i = 0;
  5744. maxtarget = 16;
  5745. if (target != CAM_TARGET_WILDCARD) {
  5746. i = target;
  5747. if (channel == 'B')
  5748. i += 8;
  5749. maxtarget = i + 1;
  5750. }
  5751. if (lun == CAM_LUN_WILDCARD) {
  5752. /*
  5753. * Unless we are using an SCB based
  5754. * busy targets table, there is only
  5755. * one table entry for all luns of
  5756. * a target.
  5757. */
  5758. minlun = 0;
  5759. maxlun = 1;
  5760. if ((ahc->flags & AHC_SCB_BTT) != 0)
  5761. maxlun = AHC_NUM_LUNS;
  5762. } else {
  5763. minlun = lun;
  5764. maxlun = lun + 1;
  5765. }
  5766. if (role != ROLE_TARGET) {
  5767. for (;i < maxtarget; i++) {
  5768. for (j = minlun;j < maxlun; j++) {
  5769. u_int scbid;
  5770. u_int tcl;
  5771. tcl = BUILD_TCL(i << 4, j);
  5772. scbid = ahc_index_busy_tcl(ahc, tcl);
  5773. scbp = ahc_lookup_scb(ahc, scbid);
  5774. if (scbp == NULL
  5775. || ahc_match_scb(ahc, scbp, target, channel,
  5776. lun, tag, role) == 0)
  5777. continue;
  5778. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
  5779. }
  5780. }
  5781. /*
  5782. * Go through the disconnected list and remove any entries we
  5783. * have queued for completion, 0'ing their control byte too.
  5784. * We save the active SCB and restore it ourselves, so there
  5785. * is no reason for this search to restore it too.
  5786. */
  5787. ahc_search_disc_list(ahc, target, channel, lun, tag,
  5788. /*stop_on_first*/FALSE, /*remove*/TRUE,
  5789. /*save_state*/FALSE);
  5790. }
  5791. /*
  5792. * Go through the hardware SCB array looking for commands that
  5793. * were active but not on any list. In some cases, these remnants
  5794. * might not still have mappings in the scbindex array (e.g. unexpected
  5795. * bus free with the same scb queued for an abort). Don't hold this
  5796. * against them.
  5797. */
  5798. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  5799. u_int scbid;
  5800. ahc_outb(ahc, SCBPTR, i);
  5801. scbid = ahc_inb(ahc, SCB_TAG);
  5802. scbp = ahc_lookup_scb(ahc, scbid);
  5803. if ((scbp == NULL && scbid != SCB_LIST_NULL)
  5804. || (scbp != NULL
  5805. && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
  5806. ahc_add_curscb_to_free_list(ahc);
  5807. }
  5808. /*
  5809. * Go through the pending CCB list and look for
  5810. * commands for this target that are still active.
  5811. * These are other tagged commands that were
  5812. * disconnected when the reset occurred.
  5813. */
  5814. scbp_next = LIST_FIRST(&ahc->pending_scbs);
  5815. while (scbp_next != NULL) {
  5816. scbp = scbp_next;
  5817. scbp_next = LIST_NEXT(scbp, pending_links);
  5818. if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
  5819. cam_status ostat;
  5820. ostat = ahc_get_transaction_status(scbp);
  5821. if (ostat == CAM_REQ_INPROG)
  5822. ahc_set_transaction_status(scbp, status);
  5823. if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
  5824. ahc_freeze_scb(scbp);
  5825. if ((scbp->flags & SCB_ACTIVE) == 0)
  5826. printf("Inactive SCB on pending list\n");
  5827. ahc_done(ahc, scbp);
  5828. found++;
  5829. }
  5830. }
  5831. ahc_outb(ahc, SCBPTR, active_scb);
  5832. ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
  5833. ahc_release_untagged_queues(ahc);
  5834. return found;
  5835. }
  5836. static void
  5837. ahc_reset_current_bus(struct ahc_softc *ahc)
  5838. {
  5839. uint8_t scsiseq;
  5840. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
  5841. scsiseq = ahc_inb(ahc, SCSISEQ);
  5842. ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
  5843. ahc_flush_device_writes(ahc);
  5844. ahc_delay(AHC_BUSRESET_DELAY);
  5845. /* Turn off the bus reset */
  5846. ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
  5847. ahc_clear_intstat(ahc);
  5848. /* Re-enable reset interrupts */
  5849. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
  5850. }
  5851. int
  5852. ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
  5853. {
  5854. struct ahc_devinfo devinfo;
  5855. u_int initiator, target, max_scsiid;
  5856. u_int sblkctl;
  5857. u_int scsiseq;
  5858. u_int simode1;
  5859. int found;
  5860. int restart_needed;
  5861. char cur_channel;
  5862. ahc->pending_device = NULL;
  5863. ahc_compile_devinfo(&devinfo,
  5864. CAM_TARGET_WILDCARD,
  5865. CAM_TARGET_WILDCARD,
  5866. CAM_LUN_WILDCARD,
  5867. channel, ROLE_UNKNOWN);
  5868. ahc_pause(ahc);
  5869. /* Make sure the sequencer is in a safe location. */
  5870. ahc_clear_critical_section(ahc);
  5871. /*
  5872. * Run our command complete fifos to ensure that we perform
  5873. * completion processing on any commands that 'completed'
  5874. * before the reset occurred.
  5875. */
  5876. ahc_run_qoutfifo(ahc);
  5877. #ifdef AHC_TARGET_MODE
  5878. /*
  5879. * XXX - In Twin mode, the tqinfifo may have commands
  5880. * for an unaffected channel in it. However, if
  5881. * we have run out of ATIO resources to drain that
  5882. * queue, we may not get them all out here. Further,
  5883. * the blocked transactions for the reset channel
  5884. * should just be killed off, irrespecitve of whether
  5885. * we are blocked on ATIO resources. Write a routine
  5886. * to compact the tqinfifo appropriately.
  5887. */
  5888. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  5889. ahc_run_tqinfifo(ahc, /*paused*/TRUE);
  5890. }
  5891. #endif
  5892. /*
  5893. * Reset the bus if we are initiating this reset
  5894. */
  5895. sblkctl = ahc_inb(ahc, SBLKCTL);
  5896. cur_channel = 'A';
  5897. if ((ahc->features & AHC_TWIN) != 0
  5898. && ((sblkctl & SELBUSB) != 0))
  5899. cur_channel = 'B';
  5900. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  5901. if (cur_channel != channel) {
  5902. /* Case 1: Command for another bus is active
  5903. * Stealthily reset the other bus without
  5904. * upsetting the current bus.
  5905. */
  5906. ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
  5907. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5908. #ifdef AHC_TARGET_MODE
  5909. /*
  5910. * Bus resets clear ENSELI, so we cannot
  5911. * defer re-enabling bus reset interrupts
  5912. * if we are in target mode.
  5913. */
  5914. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5915. simode1 |= ENSCSIRST;
  5916. #endif
  5917. ahc_outb(ahc, SIMODE1, simode1);
  5918. if (initiate_reset)
  5919. ahc_reset_current_bus(ahc);
  5920. ahc_clear_intstat(ahc);
  5921. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5922. ahc_outb(ahc, SBLKCTL, sblkctl);
  5923. restart_needed = FALSE;
  5924. } else {
  5925. /* Case 2: A command from this bus is active or we're idle */
  5926. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5927. #ifdef AHC_TARGET_MODE
  5928. /*
  5929. * Bus resets clear ENSELI, so we cannot
  5930. * defer re-enabling bus reset interrupts
  5931. * if we are in target mode.
  5932. */
  5933. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5934. simode1 |= ENSCSIRST;
  5935. #endif
  5936. ahc_outb(ahc, SIMODE1, simode1);
  5937. if (initiate_reset)
  5938. ahc_reset_current_bus(ahc);
  5939. ahc_clear_intstat(ahc);
  5940. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5941. restart_needed = TRUE;
  5942. }
  5943. /*
  5944. * Clean up all the state information for the
  5945. * pending transactions on this bus.
  5946. */
  5947. found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
  5948. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  5949. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  5950. max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
  5951. #ifdef AHC_TARGET_MODE
  5952. /*
  5953. * Send an immediate notify ccb to all target more peripheral
  5954. * drivers affected by this action.
  5955. */
  5956. for (target = 0; target <= max_scsiid; target++) {
  5957. struct ahc_tmode_tstate* tstate;
  5958. u_int lun;
  5959. tstate = ahc->enabled_targets[target];
  5960. if (tstate == NULL)
  5961. continue;
  5962. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  5963. struct ahc_tmode_lstate* lstate;
  5964. lstate = tstate->enabled_luns[lun];
  5965. if (lstate == NULL)
  5966. continue;
  5967. ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
  5968. EVENT_TYPE_BUS_RESET, /*arg*/0);
  5969. ahc_send_lstate_events(ahc, lstate);
  5970. }
  5971. }
  5972. #endif
  5973. /* Notify the XPT that a bus reset occurred */
  5974. ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
  5975. CAM_LUN_WILDCARD, AC_BUS_RESET);
  5976. /*
  5977. * Revert to async/narrow transfers until we renegotiate.
  5978. */
  5979. for (target = 0; target <= max_scsiid; target++) {
  5980. if (ahc->enabled_targets[target] == NULL)
  5981. continue;
  5982. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  5983. struct ahc_devinfo devinfo;
  5984. ahc_compile_devinfo(&devinfo, target, initiator,
  5985. CAM_LUN_WILDCARD,
  5986. channel, ROLE_UNKNOWN);
  5987. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  5988. AHC_TRANS_CUR, /*paused*/TRUE);
  5989. ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  5990. /*period*/0, /*offset*/0,
  5991. /*ppr_options*/0, AHC_TRANS_CUR,
  5992. /*paused*/TRUE);
  5993. }
  5994. }
  5995. if (restart_needed)
  5996. ahc_restart(ahc);
  5997. else
  5998. ahc_unpause(ahc);
  5999. return found;
  6000. }
  6001. /***************************** Residual Processing ****************************/
  6002. /*
  6003. * Calculate the residual for a just completed SCB.
  6004. */
  6005. static void
  6006. ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
  6007. {
  6008. struct hardware_scb *hscb;
  6009. struct status_pkt *spkt;
  6010. uint32_t sgptr;
  6011. uint32_t resid_sgptr;
  6012. uint32_t resid;
  6013. /*
  6014. * 5 cases.
  6015. * 1) No residual.
  6016. * SG_RESID_VALID clear in sgptr.
  6017. * 2) Transferless command
  6018. * 3) Never performed any transfers.
  6019. * sgptr has SG_FULL_RESID set.
  6020. * 4) No residual but target did not
  6021. * save data pointers after the
  6022. * last transfer, so sgptr was
  6023. * never updated.
  6024. * 5) We have a partial residual.
  6025. * Use residual_sgptr to determine
  6026. * where we are.
  6027. */
  6028. hscb = scb->hscb;
  6029. sgptr = ahc_le32toh(hscb->sgptr);
  6030. if ((sgptr & SG_RESID_VALID) == 0)
  6031. /* Case 1 */
  6032. return;
  6033. sgptr &= ~SG_RESID_VALID;
  6034. if ((sgptr & SG_LIST_NULL) != 0)
  6035. /* Case 2 */
  6036. return;
  6037. spkt = &hscb->shared_data.status;
  6038. resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
  6039. if ((sgptr & SG_FULL_RESID) != 0) {
  6040. /* Case 3 */
  6041. resid = ahc_get_transfer_length(scb);
  6042. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  6043. /* Case 4 */
  6044. return;
  6045. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  6046. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  6047. } else {
  6048. struct ahc_dma_seg *sg;
  6049. /*
  6050. * Remainder of the SG where the transfer
  6051. * stopped.
  6052. */
  6053. resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
  6054. sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
  6055. /* The residual sg_ptr always points to the next sg */
  6056. sg--;
  6057. /*
  6058. * Add up the contents of all residual
  6059. * SG segments that are after the SG where
  6060. * the transfer stopped.
  6061. */
  6062. while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
  6063. sg++;
  6064. resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  6065. }
  6066. }
  6067. if ((scb->flags & SCB_SENSE) == 0)
  6068. ahc_set_residual(scb, resid);
  6069. else
  6070. ahc_set_sense_residual(scb, resid);
  6071. #ifdef AHC_DEBUG
  6072. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  6073. ahc_print_path(ahc, scb);
  6074. printf("Handled %sResidual of %d bytes\n",
  6075. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  6076. }
  6077. #endif
  6078. }
  6079. /******************************* Target Mode **********************************/
  6080. #ifdef AHC_TARGET_MODE
  6081. /*
  6082. * Add a target mode event to this lun's queue
  6083. */
  6084. static void
  6085. ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
  6086. u_int initiator_id, u_int event_type, u_int event_arg)
  6087. {
  6088. struct ahc_tmode_event *event;
  6089. int pending;
  6090. xpt_freeze_devq(lstate->path, /*count*/1);
  6091. if (lstate->event_w_idx >= lstate->event_r_idx)
  6092. pending = lstate->event_w_idx - lstate->event_r_idx;
  6093. else
  6094. pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
  6095. - (lstate->event_r_idx - lstate->event_w_idx);
  6096. if (event_type == EVENT_TYPE_BUS_RESET
  6097. || event_type == MSG_BUS_DEV_RESET) {
  6098. /*
  6099. * Any earlier events are irrelevant, so reset our buffer.
  6100. * This has the effect of allowing us to deal with reset
  6101. * floods (an external device holding down the reset line)
  6102. * without losing the event that is really interesting.
  6103. */
  6104. lstate->event_r_idx = 0;
  6105. lstate->event_w_idx = 0;
  6106. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  6107. }
  6108. if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
  6109. xpt_print_path(lstate->path);
  6110. printf("immediate event %x:%x lost\n",
  6111. lstate->event_buffer[lstate->event_r_idx].event_type,
  6112. lstate->event_buffer[lstate->event_r_idx].event_arg);
  6113. lstate->event_r_idx++;
  6114. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6115. lstate->event_r_idx = 0;
  6116. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  6117. }
  6118. event = &lstate->event_buffer[lstate->event_w_idx];
  6119. event->initiator_id = initiator_id;
  6120. event->event_type = event_type;
  6121. event->event_arg = event_arg;
  6122. lstate->event_w_idx++;
  6123. if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6124. lstate->event_w_idx = 0;
  6125. }
  6126. /*
  6127. * Send any target mode events queued up waiting
  6128. * for immediate notify resources.
  6129. */
  6130. void
  6131. ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
  6132. {
  6133. struct ccb_hdr *ccbh;
  6134. struct ccb_immed_notify *inot;
  6135. while (lstate->event_r_idx != lstate->event_w_idx
  6136. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  6137. struct ahc_tmode_event *event;
  6138. event = &lstate->event_buffer[lstate->event_r_idx];
  6139. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  6140. inot = (struct ccb_immed_notify *)ccbh;
  6141. switch (event->event_type) {
  6142. case EVENT_TYPE_BUS_RESET:
  6143. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  6144. break;
  6145. default:
  6146. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  6147. inot->message_args[0] = event->event_type;
  6148. inot->message_args[1] = event->event_arg;
  6149. break;
  6150. }
  6151. inot->initiator_id = event->initiator_id;
  6152. inot->sense_len = 0;
  6153. xpt_done((union ccb *)inot);
  6154. lstate->event_r_idx++;
  6155. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  6156. lstate->event_r_idx = 0;
  6157. }
  6158. }
  6159. #endif
  6160. /******************** Sequencer Program Patching/Download *********************/
  6161. #ifdef AHC_DUMP_SEQ
  6162. void
  6163. ahc_dumpseq(struct ahc_softc* ahc)
  6164. {
  6165. int i;
  6166. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  6167. ahc_outb(ahc, SEQADDR0, 0);
  6168. ahc_outb(ahc, SEQADDR1, 0);
  6169. for (i = 0; i < ahc->instruction_ram_size; i++) {
  6170. uint8_t ins_bytes[4];
  6171. ahc_insb(ahc, SEQRAM, ins_bytes, 4);
  6172. printf("0x%08x\n", ins_bytes[0] << 24
  6173. | ins_bytes[1] << 16
  6174. | ins_bytes[2] << 8
  6175. | ins_bytes[3]);
  6176. }
  6177. }
  6178. #endif
  6179. static int
  6180. ahc_loadseq(struct ahc_softc *ahc)
  6181. {
  6182. struct cs cs_table[num_critical_sections];
  6183. u_int begin_set[num_critical_sections];
  6184. u_int end_set[num_critical_sections];
  6185. const struct patch *cur_patch;
  6186. u_int cs_count;
  6187. u_int cur_cs;
  6188. u_int i;
  6189. u_int skip_addr;
  6190. u_int sg_prefetch_cnt;
  6191. int downloaded;
  6192. uint8_t download_consts[7];
  6193. /*
  6194. * Start out with 0 critical sections
  6195. * that apply to this firmware load.
  6196. */
  6197. cs_count = 0;
  6198. cur_cs = 0;
  6199. memset(begin_set, 0, sizeof(begin_set));
  6200. memset(end_set, 0, sizeof(end_set));
  6201. /* Setup downloadable constant table */
  6202. download_consts[QOUTFIFO_OFFSET] = 0;
  6203. if (ahc->targetcmds != NULL)
  6204. download_consts[QOUTFIFO_OFFSET] += 32;
  6205. download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
  6206. download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
  6207. download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
  6208. sg_prefetch_cnt = ahc->pci_cachesize;
  6209. if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
  6210. sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
  6211. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  6212. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
  6213. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
  6214. cur_patch = patches;
  6215. downloaded = 0;
  6216. skip_addr = 0;
  6217. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  6218. ahc_outb(ahc, SEQADDR0, 0);
  6219. ahc_outb(ahc, SEQADDR1, 0);
  6220. for (i = 0; i < sizeof(seqprog)/4; i++) {
  6221. if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
  6222. /*
  6223. * Don't download this instruction as it
  6224. * is in a patch that was removed.
  6225. */
  6226. continue;
  6227. }
  6228. if (downloaded == ahc->instruction_ram_size) {
  6229. /*
  6230. * We're about to exceed the instruction
  6231. * storage capacity for this chip. Fail
  6232. * the load.
  6233. */
  6234. printf("\n%s: Program too large for instruction memory "
  6235. "size of %d!\n", ahc_name(ahc),
  6236. ahc->instruction_ram_size);
  6237. return (ENOMEM);
  6238. }
  6239. /*
  6240. * Move through the CS table until we find a CS
  6241. * that might apply to this instruction.
  6242. */
  6243. for (; cur_cs < num_critical_sections; cur_cs++) {
  6244. if (critical_sections[cur_cs].end <= i) {
  6245. if (begin_set[cs_count] == TRUE
  6246. && end_set[cs_count] == FALSE) {
  6247. cs_table[cs_count].end = downloaded;
  6248. end_set[cs_count] = TRUE;
  6249. cs_count++;
  6250. }
  6251. continue;
  6252. }
  6253. if (critical_sections[cur_cs].begin <= i
  6254. && begin_set[cs_count] == FALSE) {
  6255. cs_table[cs_count].begin = downloaded;
  6256. begin_set[cs_count] = TRUE;
  6257. }
  6258. break;
  6259. }
  6260. ahc_download_instr(ahc, i, download_consts);
  6261. downloaded++;
  6262. }
  6263. ahc->num_critical_sections = cs_count;
  6264. if (cs_count != 0) {
  6265. cs_count *= sizeof(struct cs);
  6266. ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  6267. if (ahc->critical_sections == NULL)
  6268. panic("ahc_loadseq: Could not malloc");
  6269. memcpy(ahc->critical_sections, cs_table, cs_count);
  6270. }
  6271. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
  6272. if (bootverbose) {
  6273. printf(" %d instructions downloaded\n", downloaded);
  6274. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  6275. ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
  6276. }
  6277. return (0);
  6278. }
  6279. static int
  6280. ahc_check_patch(struct ahc_softc *ahc, const struct patch **start_patch,
  6281. u_int start_instr, u_int *skip_addr)
  6282. {
  6283. const struct patch *cur_patch;
  6284. const struct patch *last_patch;
  6285. u_int num_patches;
  6286. num_patches = ARRAY_SIZE(patches);
  6287. last_patch = &patches[num_patches];
  6288. cur_patch = *start_patch;
  6289. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  6290. if (cur_patch->patch_func(ahc) == 0) {
  6291. /* Start rejecting code */
  6292. *skip_addr = start_instr + cur_patch->skip_instr;
  6293. cur_patch += cur_patch->skip_patch;
  6294. } else {
  6295. /* Accepted this patch. Advance to the next
  6296. * one and wait for our intruction pointer to
  6297. * hit this point.
  6298. */
  6299. cur_patch++;
  6300. }
  6301. }
  6302. *start_patch = cur_patch;
  6303. if (start_instr < *skip_addr)
  6304. /* Still skipping */
  6305. return (0);
  6306. return (1);
  6307. }
  6308. static void
  6309. ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
  6310. {
  6311. union ins_formats instr;
  6312. struct ins_format1 *fmt1_ins;
  6313. struct ins_format3 *fmt3_ins;
  6314. u_int opcode;
  6315. /*
  6316. * The firmware is always compiled into a little endian format.
  6317. */
  6318. instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  6319. fmt1_ins = &instr.format1;
  6320. fmt3_ins = NULL;
  6321. /* Pull the opcode */
  6322. opcode = instr.format1.opcode;
  6323. switch (opcode) {
  6324. case AIC_OP_JMP:
  6325. case AIC_OP_JC:
  6326. case AIC_OP_JNC:
  6327. case AIC_OP_CALL:
  6328. case AIC_OP_JNE:
  6329. case AIC_OP_JNZ:
  6330. case AIC_OP_JE:
  6331. case AIC_OP_JZ:
  6332. {
  6333. const struct patch *cur_patch;
  6334. int address_offset;
  6335. u_int address;
  6336. u_int skip_addr;
  6337. u_int i;
  6338. fmt3_ins = &instr.format3;
  6339. address_offset = 0;
  6340. address = fmt3_ins->address;
  6341. cur_patch = patches;
  6342. skip_addr = 0;
  6343. for (i = 0; i < address;) {
  6344. ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
  6345. if (skip_addr > i) {
  6346. int end_addr;
  6347. end_addr = min(address, skip_addr);
  6348. address_offset += end_addr - i;
  6349. i = skip_addr;
  6350. } else {
  6351. i++;
  6352. }
  6353. }
  6354. address -= address_offset;
  6355. fmt3_ins->address = address;
  6356. /* FALLTHROUGH */
  6357. }
  6358. case AIC_OP_OR:
  6359. case AIC_OP_AND:
  6360. case AIC_OP_XOR:
  6361. case AIC_OP_ADD:
  6362. case AIC_OP_ADC:
  6363. case AIC_OP_BMOV:
  6364. if (fmt1_ins->parity != 0) {
  6365. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  6366. }
  6367. fmt1_ins->parity = 0;
  6368. if ((ahc->features & AHC_CMD_CHAN) == 0
  6369. && opcode == AIC_OP_BMOV) {
  6370. /*
  6371. * Block move was added at the same time
  6372. * as the command channel. Verify that
  6373. * this is only a move of a single element
  6374. * and convert the BMOV to a MOV
  6375. * (AND with an immediate of FF).
  6376. */
  6377. if (fmt1_ins->immediate != 1)
  6378. panic("%s: BMOV not supported\n",
  6379. ahc_name(ahc));
  6380. fmt1_ins->opcode = AIC_OP_AND;
  6381. fmt1_ins->immediate = 0xff;
  6382. }
  6383. /* FALLTHROUGH */
  6384. case AIC_OP_ROL:
  6385. if ((ahc->features & AHC_ULTRA2) != 0) {
  6386. int i, count;
  6387. /* Calculate odd parity for the instruction */
  6388. for (i = 0, count = 0; i < 31; i++) {
  6389. uint32_t mask;
  6390. mask = 0x01 << i;
  6391. if ((instr.integer & mask) != 0)
  6392. count++;
  6393. }
  6394. if ((count & 0x01) == 0)
  6395. instr.format1.parity = 1;
  6396. } else {
  6397. /* Compress the instruction for older sequencers */
  6398. if (fmt3_ins != NULL) {
  6399. instr.integer =
  6400. fmt3_ins->immediate
  6401. | (fmt3_ins->source << 8)
  6402. | (fmt3_ins->address << 16)
  6403. | (fmt3_ins->opcode << 25);
  6404. } else {
  6405. instr.integer =
  6406. fmt1_ins->immediate
  6407. | (fmt1_ins->source << 8)
  6408. | (fmt1_ins->destination << 16)
  6409. | (fmt1_ins->ret << 24)
  6410. | (fmt1_ins->opcode << 25);
  6411. }
  6412. }
  6413. /* The sequencer is a little endian cpu */
  6414. instr.integer = ahc_htole32(instr.integer);
  6415. ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
  6416. break;
  6417. default:
  6418. panic("Unknown opcode encountered in seq program");
  6419. break;
  6420. }
  6421. }
  6422. int
  6423. ahc_print_register(const ahc_reg_parse_entry_t *table, u_int num_entries,
  6424. const char *name, u_int address, u_int value,
  6425. u_int *cur_column, u_int wrap_point)
  6426. {
  6427. int printed;
  6428. u_int printed_mask;
  6429. if (cur_column != NULL && *cur_column >= wrap_point) {
  6430. printf("\n");
  6431. *cur_column = 0;
  6432. }
  6433. printed = printf("%s[0x%x]", name, value);
  6434. if (table == NULL) {
  6435. printed += printf(" ");
  6436. *cur_column += printed;
  6437. return (printed);
  6438. }
  6439. printed_mask = 0;
  6440. while (printed_mask != 0xFF) {
  6441. int entry;
  6442. for (entry = 0; entry < num_entries; entry++) {
  6443. if (((value & table[entry].mask)
  6444. != table[entry].value)
  6445. || ((printed_mask & table[entry].mask)
  6446. == table[entry].mask))
  6447. continue;
  6448. printed += printf("%s%s",
  6449. printed_mask == 0 ? ":(" : "|",
  6450. table[entry].name);
  6451. printed_mask |= table[entry].mask;
  6452. break;
  6453. }
  6454. if (entry >= num_entries)
  6455. break;
  6456. }
  6457. if (printed_mask != 0)
  6458. printed += printf(") ");
  6459. else
  6460. printed += printf(" ");
  6461. if (cur_column != NULL)
  6462. *cur_column += printed;
  6463. return (printed);
  6464. }
  6465. void
  6466. ahc_dump_card_state(struct ahc_softc *ahc)
  6467. {
  6468. struct scb *scb;
  6469. struct scb_tailq *untagged_q;
  6470. u_int cur_col;
  6471. int paused;
  6472. int target;
  6473. int maxtarget;
  6474. int i;
  6475. uint8_t last_phase;
  6476. uint8_t qinpos;
  6477. uint8_t qintail;
  6478. uint8_t qoutpos;
  6479. uint8_t scb_index;
  6480. uint8_t saved_scbptr;
  6481. if (ahc_is_paused(ahc)) {
  6482. paused = 1;
  6483. } else {
  6484. paused = 0;
  6485. ahc_pause(ahc);
  6486. }
  6487. saved_scbptr = ahc_inb(ahc, SCBPTR);
  6488. last_phase = ahc_inb(ahc, LASTPHASE);
  6489. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  6490. "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
  6491. ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
  6492. ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
  6493. if (paused)
  6494. printf("Card was paused\n");
  6495. printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
  6496. ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
  6497. ahc_inb(ahc, ARG_2));
  6498. printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
  6499. ahc_inb(ahc, SCBPTR));
  6500. cur_col = 0;
  6501. if ((ahc->features & AHC_DT) != 0)
  6502. ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
  6503. ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
  6504. ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
  6505. ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
  6506. ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
  6507. ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
  6508. ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
  6509. ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
  6510. ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
  6511. ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
  6512. ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
  6513. ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
  6514. ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
  6515. ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
  6516. ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
  6517. ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
  6518. ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
  6519. ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
  6520. ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
  6521. if (cur_col != 0)
  6522. printf("\n");
  6523. printf("STACK:");
  6524. for (i = 0; i < STACK_SIZE; i++)
  6525. printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
  6526. printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
  6527. printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
  6528. printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
  6529. /* QINFIFO */
  6530. printf("QINFIFO entries: ");
  6531. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  6532. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  6533. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  6534. } else
  6535. qinpos = ahc_inb(ahc, QINPOS);
  6536. qintail = ahc->qinfifonext;
  6537. while (qinpos != qintail) {
  6538. printf("%d ", ahc->qinfifo[qinpos]);
  6539. qinpos++;
  6540. }
  6541. printf("\n");
  6542. printf("Waiting Queue entries: ");
  6543. scb_index = ahc_inb(ahc, WAITING_SCBH);
  6544. i = 0;
  6545. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6546. ahc_outb(ahc, SCBPTR, scb_index);
  6547. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6548. scb_index = ahc_inb(ahc, SCB_NEXT);
  6549. }
  6550. printf("\n");
  6551. printf("Disconnected Queue entries: ");
  6552. scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
  6553. i = 0;
  6554. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6555. ahc_outb(ahc, SCBPTR, scb_index);
  6556. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6557. scb_index = ahc_inb(ahc, SCB_NEXT);
  6558. }
  6559. printf("\n");
  6560. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  6561. printf("QOUTFIFO entries: ");
  6562. qoutpos = ahc->qoutfifonext;
  6563. i = 0;
  6564. while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
  6565. printf("%d ", ahc->qoutfifo[qoutpos]);
  6566. qoutpos++;
  6567. }
  6568. printf("\n");
  6569. printf("Sequencer Free SCB List: ");
  6570. scb_index = ahc_inb(ahc, FREE_SCBH);
  6571. i = 0;
  6572. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6573. ahc_outb(ahc, SCBPTR, scb_index);
  6574. printf("%d ", scb_index);
  6575. scb_index = ahc_inb(ahc, SCB_NEXT);
  6576. }
  6577. printf("\n");
  6578. printf("Sequencer SCB Info: ");
  6579. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  6580. ahc_outb(ahc, SCBPTR, i);
  6581. cur_col = printf("\n%3d ", i);
  6582. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
  6583. ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
  6584. ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
  6585. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6586. }
  6587. printf("\n");
  6588. printf("Pending list: ");
  6589. i = 0;
  6590. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6591. if (i++ > 256)
  6592. break;
  6593. cur_col = printf("\n%3d ", scb->hscb->tag);
  6594. ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
  6595. ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
  6596. ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
  6597. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  6598. ahc_outb(ahc, SCBPTR, scb->hscb->tag);
  6599. printf("(");
  6600. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
  6601. &cur_col, 60);
  6602. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6603. printf(")");
  6604. }
  6605. }
  6606. printf("\n");
  6607. printf("Kernel Free SCB list: ");
  6608. i = 0;
  6609. SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
  6610. if (i++ > 256)
  6611. break;
  6612. printf("%d ", scb->hscb->tag);
  6613. }
  6614. printf("\n");
  6615. maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
  6616. for (target = 0; target <= maxtarget; target++) {
  6617. untagged_q = &ahc->untagged_queues[target];
  6618. if (TAILQ_FIRST(untagged_q) == NULL)
  6619. continue;
  6620. printf("Untagged Q(%d): ", target);
  6621. i = 0;
  6622. TAILQ_FOREACH(scb, untagged_q, links.tqe) {
  6623. if (i++ > 256)
  6624. break;
  6625. printf("%d ", scb->hscb->tag);
  6626. }
  6627. printf("\n");
  6628. }
  6629. ahc_platform_dump_card_state(ahc);
  6630. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  6631. ahc_outb(ahc, SCBPTR, saved_scbptr);
  6632. if (paused == 0)
  6633. ahc_unpause(ahc);
  6634. }
  6635. /************************* Target Mode ****************************************/
  6636. #ifdef AHC_TARGET_MODE
  6637. cam_status
  6638. ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
  6639. struct ahc_tmode_tstate **tstate,
  6640. struct ahc_tmode_lstate **lstate,
  6641. int notfound_failure)
  6642. {
  6643. if ((ahc->features & AHC_TARGETMODE) == 0)
  6644. return (CAM_REQ_INVALID);
  6645. /*
  6646. * Handle the 'black hole' device that sucks up
  6647. * requests to unattached luns on enabled targets.
  6648. */
  6649. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  6650. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  6651. *tstate = NULL;
  6652. *lstate = ahc->black_hole;
  6653. } else {
  6654. u_int max_id;
  6655. max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  6656. if (ccb->ccb_h.target_id >= max_id)
  6657. return (CAM_TID_INVALID);
  6658. if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
  6659. return (CAM_LUN_INVALID);
  6660. *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
  6661. *lstate = NULL;
  6662. if (*tstate != NULL)
  6663. *lstate =
  6664. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  6665. }
  6666. if (notfound_failure != 0 && *lstate == NULL)
  6667. return (CAM_PATH_INVALID);
  6668. return (CAM_REQ_CMP);
  6669. }
  6670. void
  6671. ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
  6672. {
  6673. struct ahc_tmode_tstate *tstate;
  6674. struct ahc_tmode_lstate *lstate;
  6675. struct ccb_en_lun *cel;
  6676. cam_status status;
  6677. u_long s;
  6678. u_int target;
  6679. u_int lun;
  6680. u_int target_mask;
  6681. u_int our_id;
  6682. int error;
  6683. char channel;
  6684. status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
  6685. /*notfound_failure*/FALSE);
  6686. if (status != CAM_REQ_CMP) {
  6687. ccb->ccb_h.status = status;
  6688. return;
  6689. }
  6690. if (cam_sim_bus(sim) == 0)
  6691. our_id = ahc->our_id;
  6692. else
  6693. our_id = ahc->our_id_b;
  6694. if (ccb->ccb_h.target_id != our_id) {
  6695. /*
  6696. * our_id represents our initiator ID, or
  6697. * the ID of the first target to have an
  6698. * enabled lun in target mode. There are
  6699. * two cases that may preclude enabling a
  6700. * target id other than our_id.
  6701. *
  6702. * o our_id is for an active initiator role.
  6703. * Since the hardware does not support
  6704. * reselections to the initiator role at
  6705. * anything other than our_id, and our_id
  6706. * is used by the hardware to indicate the
  6707. * ID to use for both select-out and
  6708. * reselect-out operations, the only target
  6709. * ID we can support in this mode is our_id.
  6710. *
  6711. * o The MULTARGID feature is not available and
  6712. * a previous target mode ID has been enabled.
  6713. */
  6714. if ((ahc->features & AHC_MULTIROLE) != 0) {
  6715. if ((ahc->features & AHC_MULTI_TID) != 0
  6716. && (ahc->flags & AHC_INITIATORROLE) != 0) {
  6717. /*
  6718. * Only allow additional targets if
  6719. * the initiator role is disabled.
  6720. * The hardware cannot handle a re-select-in
  6721. * on the initiator id during a re-select-out
  6722. * on a different target id.
  6723. */
  6724. status = CAM_TID_INVALID;
  6725. } else if ((ahc->flags & AHC_INITIATORROLE) != 0
  6726. || ahc->enabled_luns > 0) {
  6727. /*
  6728. * Only allow our target id to change
  6729. * if the initiator role is not configured
  6730. * and there are no enabled luns which
  6731. * are attached to the currently registered
  6732. * scsi id.
  6733. */
  6734. status = CAM_TID_INVALID;
  6735. }
  6736. } else if ((ahc->features & AHC_MULTI_TID) == 0
  6737. && ahc->enabled_luns > 0) {
  6738. status = CAM_TID_INVALID;
  6739. }
  6740. }
  6741. if (status != CAM_REQ_CMP) {
  6742. ccb->ccb_h.status = status;
  6743. return;
  6744. }
  6745. /*
  6746. * We now have an id that is valid.
  6747. * If we aren't in target mode, switch modes.
  6748. */
  6749. if ((ahc->flags & AHC_TARGETROLE) == 0
  6750. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  6751. u_long s;
  6752. ahc_flag saved_flags;
  6753. printf("Configuring Target Mode\n");
  6754. ahc_lock(ahc, &s);
  6755. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  6756. ccb->ccb_h.status = CAM_BUSY;
  6757. ahc_unlock(ahc, &s);
  6758. return;
  6759. }
  6760. saved_flags = ahc->flags;
  6761. ahc->flags |= AHC_TARGETROLE;
  6762. if ((ahc->features & AHC_MULTIROLE) == 0)
  6763. ahc->flags &= ~AHC_INITIATORROLE;
  6764. ahc_pause(ahc);
  6765. error = ahc_loadseq(ahc);
  6766. if (error != 0) {
  6767. /*
  6768. * Restore original configuration and notify
  6769. * the caller that we cannot support target mode.
  6770. * Since the adapter started out in this
  6771. * configuration, the firmware load will succeed,
  6772. * so there is no point in checking ahc_loadseq's
  6773. * return value.
  6774. */
  6775. ahc->flags = saved_flags;
  6776. (void)ahc_loadseq(ahc);
  6777. ahc_restart(ahc);
  6778. ahc_unlock(ahc, &s);
  6779. ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  6780. return;
  6781. }
  6782. ahc_restart(ahc);
  6783. ahc_unlock(ahc, &s);
  6784. }
  6785. cel = &ccb->cel;
  6786. target = ccb->ccb_h.target_id;
  6787. lun = ccb->ccb_h.target_lun;
  6788. channel = SIM_CHANNEL(ahc, sim);
  6789. target_mask = 0x01 << target;
  6790. if (channel == 'B')
  6791. target_mask <<= 8;
  6792. if (cel->enable != 0) {
  6793. u_int scsiseq;
  6794. /* Are we already enabled?? */
  6795. if (lstate != NULL) {
  6796. xpt_print_path(ccb->ccb_h.path);
  6797. printf("Lun already enabled\n");
  6798. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  6799. return;
  6800. }
  6801. if (cel->grp6_len != 0
  6802. || cel->grp7_len != 0) {
  6803. /*
  6804. * Don't (yet?) support vendor
  6805. * specific commands.
  6806. */
  6807. ccb->ccb_h.status = CAM_REQ_INVALID;
  6808. printf("Non-zero Group Codes\n");
  6809. return;
  6810. }
  6811. /*
  6812. * Seems to be okay.
  6813. * Setup our data structures.
  6814. */
  6815. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  6816. tstate = ahc_alloc_tstate(ahc, target, channel);
  6817. if (tstate == NULL) {
  6818. xpt_print_path(ccb->ccb_h.path);
  6819. printf("Couldn't allocate tstate\n");
  6820. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6821. return;
  6822. }
  6823. }
  6824. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  6825. if (lstate == NULL) {
  6826. xpt_print_path(ccb->ccb_h.path);
  6827. printf("Couldn't allocate lstate\n");
  6828. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6829. return;
  6830. }
  6831. memset(lstate, 0, sizeof(*lstate));
  6832. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  6833. xpt_path_path_id(ccb->ccb_h.path),
  6834. xpt_path_target_id(ccb->ccb_h.path),
  6835. xpt_path_lun_id(ccb->ccb_h.path));
  6836. if (status != CAM_REQ_CMP) {
  6837. free(lstate, M_DEVBUF);
  6838. xpt_print_path(ccb->ccb_h.path);
  6839. printf("Couldn't allocate path\n");
  6840. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6841. return;
  6842. }
  6843. SLIST_INIT(&lstate->accept_tios);
  6844. SLIST_INIT(&lstate->immed_notifies);
  6845. ahc_lock(ahc, &s);
  6846. ahc_pause(ahc);
  6847. if (target != CAM_TARGET_WILDCARD) {
  6848. tstate->enabled_luns[lun] = lstate;
  6849. ahc->enabled_luns++;
  6850. if ((ahc->features & AHC_MULTI_TID) != 0) {
  6851. u_int targid_mask;
  6852. targid_mask = ahc_inb(ahc, TARGID)
  6853. | (ahc_inb(ahc, TARGID + 1) << 8);
  6854. targid_mask |= target_mask;
  6855. ahc_outb(ahc, TARGID, targid_mask);
  6856. ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
  6857. ahc_update_scsiid(ahc, targid_mask);
  6858. } else {
  6859. u_int our_id;
  6860. char channel;
  6861. channel = SIM_CHANNEL(ahc, sim);
  6862. our_id = SIM_SCSI_ID(ahc, sim);
  6863. /*
  6864. * This can only happen if selections
  6865. * are not enabled
  6866. */
  6867. if (target != our_id) {
  6868. u_int sblkctl;
  6869. char cur_channel;
  6870. int swap;
  6871. sblkctl = ahc_inb(ahc, SBLKCTL);
  6872. cur_channel = (sblkctl & SELBUSB)
  6873. ? 'B' : 'A';
  6874. if ((ahc->features & AHC_TWIN) == 0)
  6875. cur_channel = 'A';
  6876. swap = cur_channel != channel;
  6877. if (channel == 'A')
  6878. ahc->our_id = target;
  6879. else
  6880. ahc->our_id_b = target;
  6881. if (swap)
  6882. ahc_outb(ahc, SBLKCTL,
  6883. sblkctl ^ SELBUSB);
  6884. ahc_outb(ahc, SCSIID, target);
  6885. if (swap)
  6886. ahc_outb(ahc, SBLKCTL, sblkctl);
  6887. }
  6888. }
  6889. } else
  6890. ahc->black_hole = lstate;
  6891. /* Allow select-in operations */
  6892. if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
  6893. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6894. scsiseq |= ENSELI;
  6895. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6896. scsiseq = ahc_inb(ahc, SCSISEQ);
  6897. scsiseq |= ENSELI;
  6898. ahc_outb(ahc, SCSISEQ, scsiseq);
  6899. }
  6900. ahc_unpause(ahc);
  6901. ahc_unlock(ahc, &s);
  6902. ccb->ccb_h.status = CAM_REQ_CMP;
  6903. xpt_print_path(ccb->ccb_h.path);
  6904. printf("Lun now enabled for target mode\n");
  6905. } else {
  6906. struct scb *scb;
  6907. int i, empty;
  6908. if (lstate == NULL) {
  6909. ccb->ccb_h.status = CAM_LUN_INVALID;
  6910. return;
  6911. }
  6912. ahc_lock(ahc, &s);
  6913. ccb->ccb_h.status = CAM_REQ_CMP;
  6914. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6915. struct ccb_hdr *ccbh;
  6916. ccbh = &scb->io_ctx->ccb_h;
  6917. if (ccbh->func_code == XPT_CONT_TARGET_IO
  6918. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  6919. printf("CTIO pending\n");
  6920. ccb->ccb_h.status = CAM_REQ_INVALID;
  6921. ahc_unlock(ahc, &s);
  6922. return;
  6923. }
  6924. }
  6925. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  6926. printf("ATIOs pending\n");
  6927. ccb->ccb_h.status = CAM_REQ_INVALID;
  6928. }
  6929. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  6930. printf("INOTs pending\n");
  6931. ccb->ccb_h.status = CAM_REQ_INVALID;
  6932. }
  6933. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  6934. ahc_unlock(ahc, &s);
  6935. return;
  6936. }
  6937. xpt_print_path(ccb->ccb_h.path);
  6938. printf("Target mode disabled\n");
  6939. xpt_free_path(lstate->path);
  6940. free(lstate, M_DEVBUF);
  6941. ahc_pause(ahc);
  6942. /* Can we clean up the target too? */
  6943. if (target != CAM_TARGET_WILDCARD) {
  6944. tstate->enabled_luns[lun] = NULL;
  6945. ahc->enabled_luns--;
  6946. for (empty = 1, i = 0; i < 8; i++)
  6947. if (tstate->enabled_luns[i] != NULL) {
  6948. empty = 0;
  6949. break;
  6950. }
  6951. if (empty) {
  6952. ahc_free_tstate(ahc, target, channel,
  6953. /*force*/FALSE);
  6954. if (ahc->features & AHC_MULTI_TID) {
  6955. u_int targid_mask;
  6956. targid_mask = ahc_inb(ahc, TARGID)
  6957. | (ahc_inb(ahc, TARGID + 1)
  6958. << 8);
  6959. targid_mask &= ~target_mask;
  6960. ahc_outb(ahc, TARGID, targid_mask);
  6961. ahc_outb(ahc, TARGID+1,
  6962. (targid_mask >> 8));
  6963. ahc_update_scsiid(ahc, targid_mask);
  6964. }
  6965. }
  6966. } else {
  6967. ahc->black_hole = NULL;
  6968. /*
  6969. * We can't allow selections without
  6970. * our black hole device.
  6971. */
  6972. empty = TRUE;
  6973. }
  6974. if (ahc->enabled_luns == 0) {
  6975. /* Disallow select-in */
  6976. u_int scsiseq;
  6977. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6978. scsiseq &= ~ENSELI;
  6979. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6980. scsiseq = ahc_inb(ahc, SCSISEQ);
  6981. scsiseq &= ~ENSELI;
  6982. ahc_outb(ahc, SCSISEQ, scsiseq);
  6983. if ((ahc->features & AHC_MULTIROLE) == 0) {
  6984. printf("Configuring Initiator Mode\n");
  6985. ahc->flags &= ~AHC_TARGETROLE;
  6986. ahc->flags |= AHC_INITIATORROLE;
  6987. /*
  6988. * Returning to a configuration that
  6989. * fit previously will always succeed.
  6990. */
  6991. (void)ahc_loadseq(ahc);
  6992. ahc_restart(ahc);
  6993. /*
  6994. * Unpaused. The extra unpause
  6995. * that follows is harmless.
  6996. */
  6997. }
  6998. }
  6999. ahc_unpause(ahc);
  7000. ahc_unlock(ahc, &s);
  7001. }
  7002. }
  7003. static void
  7004. ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
  7005. {
  7006. u_int scsiid_mask;
  7007. u_int scsiid;
  7008. if ((ahc->features & AHC_MULTI_TID) == 0)
  7009. panic("ahc_update_scsiid called on non-multitid unit\n");
  7010. /*
  7011. * Since we will rely on the TARGID mask
  7012. * for selection enables, ensure that OID
  7013. * in SCSIID is not set to some other ID
  7014. * that we don't want to allow selections on.
  7015. */
  7016. if ((ahc->features & AHC_ULTRA2) != 0)
  7017. scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
  7018. else
  7019. scsiid = ahc_inb(ahc, SCSIID);
  7020. scsiid_mask = 0x1 << (scsiid & OID);
  7021. if ((targid_mask & scsiid_mask) == 0) {
  7022. u_int our_id;
  7023. /* ffs counts from 1 */
  7024. our_id = ffs(targid_mask);
  7025. if (our_id == 0)
  7026. our_id = ahc->our_id;
  7027. else
  7028. our_id--;
  7029. scsiid &= TID;
  7030. scsiid |= our_id;
  7031. }
  7032. if ((ahc->features & AHC_ULTRA2) != 0)
  7033. ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
  7034. else
  7035. ahc_outb(ahc, SCSIID, scsiid);
  7036. }
  7037. static void
  7038. ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
  7039. {
  7040. struct target_cmd *cmd;
  7041. /*
  7042. * If the card supports auto-access pause,
  7043. * we can access the card directly regardless
  7044. * of whether it is paused or not.
  7045. */
  7046. if ((ahc->features & AHC_AUTOPAUSE) != 0)
  7047. paused = TRUE;
  7048. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
  7049. while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
  7050. /*
  7051. * Only advance through the queue if we
  7052. * have the resources to process the command.
  7053. */
  7054. if (ahc_handle_target_cmd(ahc, cmd) != 0)
  7055. break;
  7056. cmd->cmd_valid = 0;
  7057. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  7058. ahc->shared_data_dmamap,
  7059. ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
  7060. sizeof(struct target_cmd),
  7061. BUS_DMASYNC_PREREAD);
  7062. ahc->tqinfifonext++;
  7063. /*
  7064. * Lazily update our position in the target mode incoming
  7065. * command queue as seen by the sequencer.
  7066. */
  7067. if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  7068. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  7069. u_int hs_mailbox;
  7070. hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
  7071. hs_mailbox &= ~HOST_TQINPOS;
  7072. hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
  7073. ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
  7074. } else {
  7075. if (!paused)
  7076. ahc_pause(ahc);
  7077. ahc_outb(ahc, KERNEL_TQINPOS,
  7078. ahc->tqinfifonext & HOST_TQINPOS);
  7079. if (!paused)
  7080. ahc_unpause(ahc);
  7081. }
  7082. }
  7083. }
  7084. }
  7085. static int
  7086. ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
  7087. {
  7088. struct ahc_tmode_tstate *tstate;
  7089. struct ahc_tmode_lstate *lstate;
  7090. struct ccb_accept_tio *atio;
  7091. uint8_t *byte;
  7092. int initiator;
  7093. int target;
  7094. int lun;
  7095. initiator = SCSIID_TARGET(ahc, cmd->scsiid);
  7096. target = SCSIID_OUR_ID(cmd->scsiid);
  7097. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  7098. byte = cmd->bytes;
  7099. tstate = ahc->enabled_targets[target];
  7100. lstate = NULL;
  7101. if (tstate != NULL)
  7102. lstate = tstate->enabled_luns[lun];
  7103. /*
  7104. * Commands for disabled luns go to the black hole driver.
  7105. */
  7106. if (lstate == NULL)
  7107. lstate = ahc->black_hole;
  7108. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  7109. if (atio == NULL) {
  7110. ahc->flags |= AHC_TQINFIFO_BLOCKED;
  7111. /*
  7112. * Wait for more ATIOs from the peripheral driver for this lun.
  7113. */
  7114. if (bootverbose)
  7115. printf("%s: ATIOs exhausted\n", ahc_name(ahc));
  7116. return (1);
  7117. } else
  7118. ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
  7119. #if 0
  7120. printf("Incoming command from %d for %d:%d%s\n",
  7121. initiator, target, lun,
  7122. lstate == ahc->black_hole ? "(Black Holed)" : "");
  7123. #endif
  7124. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  7125. if (lstate == ahc->black_hole) {
  7126. /* Fill in the wildcards */
  7127. atio->ccb_h.target_id = target;
  7128. atio->ccb_h.target_lun = lun;
  7129. }
  7130. /*
  7131. * Package it up and send it off to
  7132. * whomever has this lun enabled.
  7133. */
  7134. atio->sense_len = 0;
  7135. atio->init_id = initiator;
  7136. if (byte[0] != 0xFF) {
  7137. /* Tag was included */
  7138. atio->tag_action = *byte++;
  7139. atio->tag_id = *byte++;
  7140. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  7141. } else {
  7142. atio->ccb_h.flags = 0;
  7143. }
  7144. byte++;
  7145. /* Okay. Now determine the cdb size based on the command code */
  7146. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  7147. case 0:
  7148. atio->cdb_len = 6;
  7149. break;
  7150. case 1:
  7151. case 2:
  7152. atio->cdb_len = 10;
  7153. break;
  7154. case 4:
  7155. atio->cdb_len = 16;
  7156. break;
  7157. case 5:
  7158. atio->cdb_len = 12;
  7159. break;
  7160. case 3:
  7161. default:
  7162. /* Only copy the opcode. */
  7163. atio->cdb_len = 1;
  7164. printf("Reserved or VU command code type encountered\n");
  7165. break;
  7166. }
  7167. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  7168. atio->ccb_h.status |= CAM_CDB_RECVD;
  7169. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  7170. /*
  7171. * We weren't allowed to disconnect.
  7172. * We're hanging on the bus until a
  7173. * continue target I/O comes in response
  7174. * to this accept tio.
  7175. */
  7176. #if 0
  7177. printf("Received Immediate Command %d:%d:%d - %p\n",
  7178. initiator, target, lun, ahc->pending_device);
  7179. #endif
  7180. ahc->pending_device = lstate;
  7181. ahc_freeze_ccb((union ccb *)atio);
  7182. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  7183. }
  7184. xpt_done((union ccb*)atio);
  7185. return (0);
  7186. }
  7187. #endif