aic7xxx_core.c 195 KB

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