aic7xxx_core.c 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399
  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. /* Now set the maxsync based on the card capabilities
  1586. * DT is already done above */
  1587. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  1588. && maxsync < AHC_SYNCRATE_ULTRA)
  1589. maxsync = AHC_SYNCRATE_ULTRA;
  1590. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  1591. && maxsync < AHC_SYNCRATE_FAST)
  1592. maxsync = AHC_SYNCRATE_FAST;
  1593. for (syncrate = &ahc_syncrates[maxsync];
  1594. syncrate->rate != NULL;
  1595. syncrate++) {
  1596. /*
  1597. * The Ultra2 table doesn't go as low
  1598. * as for the Fast/Ultra cards.
  1599. */
  1600. if ((ahc->features & AHC_ULTRA2) != 0
  1601. && (syncrate->sxfr_u2 == 0))
  1602. break;
  1603. if (*period <= syncrate->period) {
  1604. /*
  1605. * When responding to a target that requests
  1606. * sync, the requested rate may fall between
  1607. * two rates that we can output, but still be
  1608. * a rate that we can receive. Because of this,
  1609. * we want to respond to the target with
  1610. * the same rate that it sent to us even
  1611. * if the period we use to send data to it
  1612. * is lower. Only lower the response period
  1613. * if we must.
  1614. */
  1615. if (syncrate == &ahc_syncrates[maxsync])
  1616. *period = syncrate->period;
  1617. /*
  1618. * At some speeds, we only support
  1619. * ST transfers.
  1620. */
  1621. if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
  1622. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1623. break;
  1624. }
  1625. }
  1626. if ((*period == 0)
  1627. || (syncrate->rate == NULL)
  1628. || ((ahc->features & AHC_ULTRA2) != 0
  1629. && (syncrate->sxfr_u2 == 0))) {
  1630. /* Use asynchronous transfers. */
  1631. *period = 0;
  1632. syncrate = NULL;
  1633. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1634. }
  1635. return (syncrate);
  1636. }
  1637. /*
  1638. * Convert from an entry in our syncrate table to the SCSI equivalent
  1639. * sync "period" factor.
  1640. */
  1641. u_int
  1642. ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
  1643. {
  1644. struct ahc_syncrate *syncrate;
  1645. if ((ahc->features & AHC_ULTRA2) != 0)
  1646. scsirate &= SXFR_ULTRA2;
  1647. else
  1648. scsirate &= SXFR;
  1649. /* now set maxsync based on card capabilities */
  1650. if ((ahc->features & AHC_DT) == 0 && maxsync < AHC_SYNCRATE_ULTRA2)
  1651. maxsync = AHC_SYNCRATE_ULTRA2;
  1652. if ((ahc->features & (AHC_DT | AHC_ULTRA2)) == 0
  1653. && maxsync < AHC_SYNCRATE_ULTRA)
  1654. maxsync = AHC_SYNCRATE_ULTRA;
  1655. if ((ahc->features & (AHC_DT | AHC_ULTRA2 | AHC_ULTRA)) == 0
  1656. && maxsync < AHC_SYNCRATE_FAST)
  1657. maxsync = AHC_SYNCRATE_FAST;
  1658. syncrate = &ahc_syncrates[maxsync];
  1659. while (syncrate->rate != NULL) {
  1660. if ((ahc->features & AHC_ULTRA2) != 0) {
  1661. if (syncrate->sxfr_u2 == 0)
  1662. break;
  1663. else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
  1664. return (syncrate->period);
  1665. } else if (scsirate == (syncrate->sxfr & SXFR)) {
  1666. return (syncrate->period);
  1667. }
  1668. syncrate++;
  1669. }
  1670. return (0); /* async */
  1671. }
  1672. /*
  1673. * Truncate the given synchronous offset to a value the
  1674. * current adapter type and syncrate are capable of.
  1675. */
  1676. void
  1677. ahc_validate_offset(struct ahc_softc *ahc,
  1678. struct ahc_initiator_tinfo *tinfo,
  1679. struct ahc_syncrate *syncrate,
  1680. u_int *offset, int wide, role_t role)
  1681. {
  1682. u_int maxoffset;
  1683. /* Limit offset to what we can do */
  1684. if (syncrate == NULL) {
  1685. maxoffset = 0;
  1686. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  1687. maxoffset = MAX_OFFSET_ULTRA2;
  1688. } else {
  1689. if (wide)
  1690. maxoffset = MAX_OFFSET_16BIT;
  1691. else
  1692. maxoffset = MAX_OFFSET_8BIT;
  1693. }
  1694. *offset = min(*offset, maxoffset);
  1695. if (tinfo != NULL) {
  1696. if (role == ROLE_TARGET)
  1697. *offset = min(*offset, (u_int)tinfo->user.offset);
  1698. else
  1699. *offset = min(*offset, (u_int)tinfo->goal.offset);
  1700. }
  1701. }
  1702. /*
  1703. * Truncate the given transfer width parameter to a value the
  1704. * current adapter type is capable of.
  1705. */
  1706. void
  1707. ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
  1708. u_int *bus_width, role_t role)
  1709. {
  1710. switch (*bus_width) {
  1711. default:
  1712. if (ahc->features & AHC_WIDE) {
  1713. /* Respond Wide */
  1714. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  1715. break;
  1716. }
  1717. /* FALLTHROUGH */
  1718. case MSG_EXT_WDTR_BUS_8_BIT:
  1719. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  1720. break;
  1721. }
  1722. if (tinfo != NULL) {
  1723. if (role == ROLE_TARGET)
  1724. *bus_width = min((u_int)tinfo->user.width, *bus_width);
  1725. else
  1726. *bus_width = min((u_int)tinfo->goal.width, *bus_width);
  1727. }
  1728. }
  1729. /*
  1730. * Update the bitmask of targets for which the controller should
  1731. * negotiate with at the next convenient oportunity. This currently
  1732. * means the next time we send the initial identify messages for
  1733. * a new transaction.
  1734. */
  1735. int
  1736. ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1737. struct ahc_tmode_tstate *tstate,
  1738. struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
  1739. {
  1740. u_int auto_negotiate_orig;
  1741. auto_negotiate_orig = tstate->auto_negotiate;
  1742. if (neg_type == AHC_NEG_ALWAYS) {
  1743. /*
  1744. * Force our "current" settings to be
  1745. * unknown so that unless a bus reset
  1746. * occurs the need to renegotiate is
  1747. * recorded persistently.
  1748. */
  1749. if ((ahc->features & AHC_WIDE) != 0)
  1750. tinfo->curr.width = AHC_WIDTH_UNKNOWN;
  1751. tinfo->curr.period = AHC_PERIOD_UNKNOWN;
  1752. tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
  1753. }
  1754. if (tinfo->curr.period != tinfo->goal.period
  1755. || tinfo->curr.width != tinfo->goal.width
  1756. || tinfo->curr.offset != tinfo->goal.offset
  1757. || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  1758. || (neg_type == AHC_NEG_IF_NON_ASYNC
  1759. && (tinfo->goal.offset != 0
  1760. || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  1761. || tinfo->goal.ppr_options != 0)))
  1762. tstate->auto_negotiate |= devinfo->target_mask;
  1763. else
  1764. tstate->auto_negotiate &= ~devinfo->target_mask;
  1765. return (auto_negotiate_orig != tstate->auto_negotiate);
  1766. }
  1767. /*
  1768. * Update the user/goal/curr tables of synchronous negotiation
  1769. * parameters as well as, in the case of a current or active update,
  1770. * any data structures on the host controller. In the case of an
  1771. * active update, the specified target is currently talking to us on
  1772. * the bus, so the transfer parameter update must take effect
  1773. * immediately.
  1774. */
  1775. void
  1776. ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1777. struct ahc_syncrate *syncrate, u_int period,
  1778. u_int offset, u_int ppr_options, u_int type, int paused)
  1779. {
  1780. struct ahc_initiator_tinfo *tinfo;
  1781. struct ahc_tmode_tstate *tstate;
  1782. u_int old_period;
  1783. u_int old_offset;
  1784. u_int old_ppr;
  1785. int active;
  1786. int update_needed;
  1787. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1788. update_needed = 0;
  1789. if (syncrate == NULL) {
  1790. period = 0;
  1791. offset = 0;
  1792. }
  1793. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1794. devinfo->target, &tstate);
  1795. if ((type & AHC_TRANS_USER) != 0) {
  1796. tinfo->user.period = period;
  1797. tinfo->user.offset = offset;
  1798. tinfo->user.ppr_options = ppr_options;
  1799. }
  1800. if ((type & AHC_TRANS_GOAL) != 0) {
  1801. tinfo->goal.period = period;
  1802. tinfo->goal.offset = offset;
  1803. tinfo->goal.ppr_options = ppr_options;
  1804. }
  1805. old_period = tinfo->curr.period;
  1806. old_offset = tinfo->curr.offset;
  1807. old_ppr = tinfo->curr.ppr_options;
  1808. if ((type & AHC_TRANS_CUR) != 0
  1809. && (old_period != period
  1810. || old_offset != offset
  1811. || old_ppr != ppr_options)) {
  1812. u_int scsirate;
  1813. update_needed++;
  1814. scsirate = tinfo->scsirate;
  1815. if ((ahc->features & AHC_ULTRA2) != 0) {
  1816. scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
  1817. if (syncrate != NULL) {
  1818. scsirate |= syncrate->sxfr_u2;
  1819. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
  1820. scsirate |= ENABLE_CRC;
  1821. else
  1822. scsirate |= SINGLE_EDGE;
  1823. }
  1824. } else {
  1825. scsirate &= ~(SXFR|SOFS);
  1826. /*
  1827. * Ensure Ultra mode is set properly for
  1828. * this target.
  1829. */
  1830. tstate->ultraenb &= ~devinfo->target_mask;
  1831. if (syncrate != NULL) {
  1832. if (syncrate->sxfr & ULTRA_SXFR) {
  1833. tstate->ultraenb |=
  1834. devinfo->target_mask;
  1835. }
  1836. scsirate |= syncrate->sxfr & SXFR;
  1837. scsirate |= offset & SOFS;
  1838. }
  1839. if (active) {
  1840. u_int sxfrctl0;
  1841. sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
  1842. sxfrctl0 &= ~FAST20;
  1843. if (tstate->ultraenb & devinfo->target_mask)
  1844. sxfrctl0 |= FAST20;
  1845. ahc_outb(ahc, SXFRCTL0, sxfrctl0);
  1846. }
  1847. }
  1848. if (active) {
  1849. ahc_outb(ahc, SCSIRATE, scsirate);
  1850. if ((ahc->features & AHC_ULTRA2) != 0)
  1851. ahc_outb(ahc, SCSIOFFSET, offset);
  1852. }
  1853. tinfo->scsirate = scsirate;
  1854. tinfo->curr.period = period;
  1855. tinfo->curr.offset = offset;
  1856. tinfo->curr.ppr_options = ppr_options;
  1857. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1858. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  1859. if (bootverbose) {
  1860. if (offset != 0) {
  1861. printf("%s: target %d synchronous at %sMHz%s, "
  1862. "offset = 0x%x\n", ahc_name(ahc),
  1863. devinfo->target, syncrate->rate,
  1864. (ppr_options & MSG_EXT_PPR_DT_REQ)
  1865. ? " DT" : "", offset);
  1866. } else {
  1867. printf("%s: target %d using "
  1868. "asynchronous transfers\n",
  1869. ahc_name(ahc), devinfo->target);
  1870. }
  1871. }
  1872. }
  1873. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1874. tinfo, AHC_NEG_TO_GOAL);
  1875. if (update_needed)
  1876. ahc_update_pending_scbs(ahc);
  1877. }
  1878. /*
  1879. * Update the user/goal/curr tables of wide negotiation
  1880. * parameters as well as, in the case of a current or active update,
  1881. * any data structures on the host controller. In the case of an
  1882. * active update, the specified target is currently talking to us on
  1883. * the bus, so the transfer parameter update must take effect
  1884. * immediately.
  1885. */
  1886. void
  1887. ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1888. u_int width, u_int type, int paused)
  1889. {
  1890. struct ahc_initiator_tinfo *tinfo;
  1891. struct ahc_tmode_tstate *tstate;
  1892. u_int oldwidth;
  1893. int active;
  1894. int update_needed;
  1895. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1896. update_needed = 0;
  1897. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1898. devinfo->target, &tstate);
  1899. if ((type & AHC_TRANS_USER) != 0)
  1900. tinfo->user.width = width;
  1901. if ((type & AHC_TRANS_GOAL) != 0)
  1902. tinfo->goal.width = width;
  1903. oldwidth = tinfo->curr.width;
  1904. if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
  1905. u_int scsirate;
  1906. update_needed++;
  1907. scsirate = tinfo->scsirate;
  1908. scsirate &= ~WIDEXFER;
  1909. if (width == MSG_EXT_WDTR_BUS_16_BIT)
  1910. scsirate |= WIDEXFER;
  1911. tinfo->scsirate = scsirate;
  1912. if (active)
  1913. ahc_outb(ahc, SCSIRATE, scsirate);
  1914. tinfo->curr.width = width;
  1915. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1916. CAM_LUN_WILDCARD, AC_TRANSFER_NEG);
  1917. if (bootverbose) {
  1918. printf("%s: target %d using %dbit transfers\n",
  1919. ahc_name(ahc), devinfo->target,
  1920. 8 * (0x01 << width));
  1921. }
  1922. }
  1923. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1924. tinfo, AHC_NEG_TO_GOAL);
  1925. if (update_needed)
  1926. ahc_update_pending_scbs(ahc);
  1927. }
  1928. /*
  1929. * Update the current state of tagged queuing for a given target.
  1930. */
  1931. static void
  1932. ahc_set_tags(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
  1933. struct ahc_devinfo *devinfo, ahc_queue_alg alg)
  1934. {
  1935. struct scsi_device *sdev = cmd->device;
  1936. ahc_platform_set_tags(ahc, sdev, devinfo, alg);
  1937. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1938. devinfo->lun, AC_TRANSFER_NEG);
  1939. }
  1940. /*
  1941. * When the transfer settings for a connection change, update any
  1942. * in-transit SCBs to contain the new data so the hardware will
  1943. * be set correctly during future (re)selections.
  1944. */
  1945. static void
  1946. ahc_update_pending_scbs(struct ahc_softc *ahc)
  1947. {
  1948. struct scb *pending_scb;
  1949. int pending_scb_count;
  1950. int i;
  1951. int paused;
  1952. u_int saved_scbptr;
  1953. /*
  1954. * Traverse the pending SCB list and ensure that all of the
  1955. * SCBs there have the proper settings.
  1956. */
  1957. pending_scb_count = 0;
  1958. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1959. struct ahc_devinfo devinfo;
  1960. struct hardware_scb *pending_hscb;
  1961. struct ahc_initiator_tinfo *tinfo;
  1962. struct ahc_tmode_tstate *tstate;
  1963. ahc_scb_devinfo(ahc, &devinfo, pending_scb);
  1964. tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  1965. devinfo.our_scsiid,
  1966. devinfo.target, &tstate);
  1967. pending_hscb = pending_scb->hscb;
  1968. pending_hscb->control &= ~ULTRAENB;
  1969. if ((tstate->ultraenb & devinfo.target_mask) != 0)
  1970. pending_hscb->control |= ULTRAENB;
  1971. pending_hscb->scsirate = tinfo->scsirate;
  1972. pending_hscb->scsioffset = tinfo->curr.offset;
  1973. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  1974. && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  1975. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  1976. pending_hscb->control &= ~MK_MESSAGE;
  1977. }
  1978. ahc_sync_scb(ahc, pending_scb,
  1979. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  1980. pending_scb_count++;
  1981. }
  1982. if (pending_scb_count == 0)
  1983. return;
  1984. if (ahc_is_paused(ahc)) {
  1985. paused = 1;
  1986. } else {
  1987. paused = 0;
  1988. ahc_pause(ahc);
  1989. }
  1990. saved_scbptr = ahc_inb(ahc, SCBPTR);
  1991. /* Ensure that the hscbs down on the card match the new information */
  1992. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  1993. struct hardware_scb *pending_hscb;
  1994. u_int control;
  1995. u_int scb_tag;
  1996. ahc_outb(ahc, SCBPTR, i);
  1997. scb_tag = ahc_inb(ahc, SCB_TAG);
  1998. pending_scb = ahc_lookup_scb(ahc, scb_tag);
  1999. if (pending_scb == NULL)
  2000. continue;
  2001. pending_hscb = pending_scb->hscb;
  2002. control = ahc_inb(ahc, SCB_CONTROL);
  2003. control &= ~(ULTRAENB|MK_MESSAGE);
  2004. control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
  2005. ahc_outb(ahc, SCB_CONTROL, control);
  2006. ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
  2007. ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
  2008. }
  2009. ahc_outb(ahc, SCBPTR, saved_scbptr);
  2010. if (paused == 0)
  2011. ahc_unpause(ahc);
  2012. }
  2013. /**************************** Pathing Information *****************************/
  2014. static void
  2015. ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2016. {
  2017. u_int saved_scsiid;
  2018. role_t role;
  2019. int our_id;
  2020. if (ahc_inb(ahc, SSTAT0) & TARGET)
  2021. role = ROLE_TARGET;
  2022. else
  2023. role = ROLE_INITIATOR;
  2024. if (role == ROLE_TARGET
  2025. && (ahc->features & AHC_MULTI_TID) != 0
  2026. && (ahc_inb(ahc, SEQ_FLAGS)
  2027. & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
  2028. /* We were selected, so pull our id from TARGIDIN */
  2029. our_id = ahc_inb(ahc, TARGIDIN) & OID;
  2030. } else if ((ahc->features & AHC_ULTRA2) != 0)
  2031. our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
  2032. else
  2033. our_id = ahc_inb(ahc, SCSIID) & OID;
  2034. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  2035. ahc_compile_devinfo(devinfo,
  2036. our_id,
  2037. SCSIID_TARGET(ahc, saved_scsiid),
  2038. ahc_inb(ahc, SAVED_LUN),
  2039. SCSIID_CHANNEL(ahc, saved_scsiid),
  2040. role);
  2041. }
  2042. struct ahc_phase_table_entry*
  2043. ahc_lookup_phase_entry(int phase)
  2044. {
  2045. struct ahc_phase_table_entry *entry;
  2046. struct ahc_phase_table_entry *last_entry;
  2047. /*
  2048. * num_phases doesn't include the default entry which
  2049. * will be returned if the phase doesn't match.
  2050. */
  2051. last_entry = &ahc_phase_table[num_phases];
  2052. for (entry = ahc_phase_table; entry < last_entry; entry++) {
  2053. if (phase == entry->phase)
  2054. break;
  2055. }
  2056. return (entry);
  2057. }
  2058. void
  2059. ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
  2060. u_int lun, char channel, role_t role)
  2061. {
  2062. devinfo->our_scsiid = our_id;
  2063. devinfo->target = target;
  2064. devinfo->lun = lun;
  2065. devinfo->target_offset = target;
  2066. devinfo->channel = channel;
  2067. devinfo->role = role;
  2068. if (channel == 'B')
  2069. devinfo->target_offset += 8;
  2070. devinfo->target_mask = (0x01 << devinfo->target_offset);
  2071. }
  2072. void
  2073. ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2074. {
  2075. printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
  2076. devinfo->target, devinfo->lun);
  2077. }
  2078. static void
  2079. ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2080. struct scb *scb)
  2081. {
  2082. role_t role;
  2083. int our_id;
  2084. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  2085. role = ROLE_INITIATOR;
  2086. if ((scb->flags & SCB_TARGET_SCB) != 0)
  2087. role = ROLE_TARGET;
  2088. ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
  2089. SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
  2090. }
  2091. /************************ Message Phase Processing ****************************/
  2092. static void
  2093. ahc_assert_atn(struct ahc_softc *ahc)
  2094. {
  2095. u_int scsisigo;
  2096. scsisigo = ATNO;
  2097. if ((ahc->features & AHC_DT) == 0)
  2098. scsisigo |= ahc_inb(ahc, SCSISIGI);
  2099. ahc_outb(ahc, SCSISIGO, scsisigo);
  2100. }
  2101. /*
  2102. * When an initiator transaction with the MK_MESSAGE flag either reconnects
  2103. * or enters the initial message out phase, we are interrupted. Fill our
  2104. * outgoing message buffer with the appropriate message and beging handing
  2105. * the message phase(s) manually.
  2106. */
  2107. static void
  2108. ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2109. struct scb *scb)
  2110. {
  2111. /*
  2112. * To facilitate adding multiple messages together,
  2113. * each routine should increment the index and len
  2114. * variables instead of setting them explicitly.
  2115. */
  2116. ahc->msgout_index = 0;
  2117. ahc->msgout_len = 0;
  2118. if ((scb->flags & SCB_DEVICE_RESET) == 0
  2119. && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
  2120. u_int identify_msg;
  2121. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  2122. if ((scb->hscb->control & DISCENB) != 0)
  2123. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  2124. ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
  2125. ahc->msgout_len++;
  2126. if ((scb->hscb->control & TAG_ENB) != 0) {
  2127. ahc->msgout_buf[ahc->msgout_index++] =
  2128. scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  2129. ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
  2130. ahc->msgout_len += 2;
  2131. }
  2132. }
  2133. if (scb->flags & SCB_DEVICE_RESET) {
  2134. ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
  2135. ahc->msgout_len++;
  2136. ahc_print_path(ahc, scb);
  2137. printf("Bus Device Reset Message Sent\n");
  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_ABORT) != 0) {
  2147. if ((scb->hscb->control & TAG_ENB) != 0)
  2148. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
  2149. else
  2150. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
  2151. ahc->msgout_len++;
  2152. ahc_print_path(ahc, scb);
  2153. printf("Abort%s Message Sent\n",
  2154. (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  2155. /*
  2156. * Clear our selection hardware in advance of
  2157. * the busfree. We may have an entry in the waiting
  2158. * Q for this target, and we don't want to go about
  2159. * selecting while we handle the busfree and blow it
  2160. * away.
  2161. */
  2162. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2163. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  2164. ahc_build_transfer_msg(ahc, devinfo);
  2165. } else {
  2166. printf("ahc_intr: AWAITING_MSG for an SCB that "
  2167. "does not have a waiting message\n");
  2168. printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
  2169. devinfo->target_mask);
  2170. panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
  2171. "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
  2172. ahc_inb(ahc, MSG_OUT), scb->flags);
  2173. }
  2174. /*
  2175. * Clear the MK_MESSAGE flag from the SCB so we aren't
  2176. * asked to send this message again.
  2177. */
  2178. ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
  2179. scb->hscb->control &= ~MK_MESSAGE;
  2180. ahc->msgout_index = 0;
  2181. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2182. }
  2183. /*
  2184. * Build an appropriate transfer negotiation message for the
  2185. * currently active target.
  2186. */
  2187. static void
  2188. ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2189. {
  2190. /*
  2191. * We need to initiate transfer negotiations.
  2192. * If our current and goal settings are identical,
  2193. * we want to renegotiate due to a check condition.
  2194. */
  2195. struct ahc_initiator_tinfo *tinfo;
  2196. struct ahc_tmode_tstate *tstate;
  2197. struct ahc_syncrate *rate;
  2198. int dowide;
  2199. int dosync;
  2200. int doppr;
  2201. u_int period;
  2202. u_int ppr_options;
  2203. u_int offset;
  2204. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2205. devinfo->target, &tstate);
  2206. /*
  2207. * Filter our period based on the current connection.
  2208. * If we can't perform DT transfers on this segment (not in LVD
  2209. * mode for instance), then our decision to issue a PPR message
  2210. * may change.
  2211. */
  2212. period = tinfo->goal.period;
  2213. offset = tinfo->goal.offset;
  2214. ppr_options = tinfo->goal.ppr_options;
  2215. /* Target initiated PPR is not allowed in the SCSI spec */
  2216. if (devinfo->role == ROLE_TARGET)
  2217. ppr_options = 0;
  2218. rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2219. &ppr_options, devinfo->role);
  2220. dowide = tinfo->curr.width != tinfo->goal.width;
  2221. dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
  2222. /*
  2223. * Only use PPR if we have options that need it, even if the device
  2224. * claims to support it. There might be an expander in the way
  2225. * that doesn't.
  2226. */
  2227. doppr = ppr_options != 0;
  2228. if (!dowide && !dosync && !doppr) {
  2229. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  2230. dosync = tinfo->goal.offset != 0;
  2231. }
  2232. if (!dowide && !dosync && !doppr) {
  2233. /*
  2234. * Force async with a WDTR message if we have a wide bus,
  2235. * or just issue an SDTR with a 0 offset.
  2236. */
  2237. if ((ahc->features & AHC_WIDE) != 0)
  2238. dowide = 1;
  2239. else
  2240. dosync = 1;
  2241. if (bootverbose) {
  2242. ahc_print_devinfo(ahc, devinfo);
  2243. printf("Ensuring async\n");
  2244. }
  2245. }
  2246. /* Target initiated PPR is not allowed in the SCSI spec */
  2247. if (devinfo->role == ROLE_TARGET)
  2248. doppr = 0;
  2249. /*
  2250. * Both the PPR message and SDTR message require the
  2251. * goal syncrate to be limited to what the target device
  2252. * is capable of handling (based on whether an LVD->SE
  2253. * expander is on the bus), so combine these two cases.
  2254. * Regardless, guarantee that if we are using WDTR and SDTR
  2255. * messages that WDTR comes first.
  2256. */
  2257. if (doppr || (dosync && !dowide)) {
  2258. offset = tinfo->goal.offset;
  2259. ahc_validate_offset(ahc, tinfo, rate, &offset,
  2260. doppr ? tinfo->goal.width
  2261. : tinfo->curr.width,
  2262. devinfo->role);
  2263. if (doppr) {
  2264. ahc_construct_ppr(ahc, devinfo, period, offset,
  2265. tinfo->goal.width, ppr_options);
  2266. } else {
  2267. ahc_construct_sdtr(ahc, devinfo, period, offset);
  2268. }
  2269. } else {
  2270. ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
  2271. }
  2272. }
  2273. /*
  2274. * Build a synchronous negotiation message in our message
  2275. * buffer based on the input parameters.
  2276. */
  2277. static void
  2278. ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2279. u_int period, u_int offset)
  2280. {
  2281. if (offset == 0)
  2282. period = AHC_ASYNC_XFER_PERIOD;
  2283. ahc->msgout_index += spi_populate_sync_msg(
  2284. ahc->msgout_buf + ahc->msgout_index, period, offset);
  2285. ahc->msgout_len += 5;
  2286. if (bootverbose) {
  2287. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
  2288. ahc_name(ahc), devinfo->channel, devinfo->target,
  2289. devinfo->lun, period, offset);
  2290. }
  2291. }
  2292. /*
  2293. * Build a wide negotiation message in our message
  2294. * buffer based on the input parameters.
  2295. */
  2296. static void
  2297. ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2298. u_int bus_width)
  2299. {
  2300. ahc->msgout_index += spi_populate_width_msg(
  2301. ahc->msgout_buf + ahc->msgout_index, bus_width);
  2302. ahc->msgout_len += 4;
  2303. if (bootverbose) {
  2304. printf("(%s:%c:%d:%d): Sending WDTR %x\n",
  2305. ahc_name(ahc), devinfo->channel, devinfo->target,
  2306. devinfo->lun, bus_width);
  2307. }
  2308. }
  2309. /*
  2310. * Build a parallel protocol request message in our message
  2311. * buffer based on the input parameters.
  2312. */
  2313. static void
  2314. ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2315. u_int period, u_int offset, u_int bus_width,
  2316. u_int ppr_options)
  2317. {
  2318. if (offset == 0)
  2319. period = AHC_ASYNC_XFER_PERIOD;
  2320. ahc->msgout_index += spi_populate_ppr_msg(
  2321. ahc->msgout_buf + ahc->msgout_index, period, offset,
  2322. bus_width, ppr_options);
  2323. ahc->msgout_len += 8;
  2324. if (bootverbose) {
  2325. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  2326. "offset %x, ppr_options %x\n", ahc_name(ahc),
  2327. devinfo->channel, devinfo->target, devinfo->lun,
  2328. bus_width, period, offset, ppr_options);
  2329. }
  2330. }
  2331. /*
  2332. * Clear any active message state.
  2333. */
  2334. static void
  2335. ahc_clear_msg_state(struct ahc_softc *ahc)
  2336. {
  2337. ahc->msgout_len = 0;
  2338. ahc->msgin_index = 0;
  2339. ahc->msg_type = MSG_TYPE_NONE;
  2340. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
  2341. /*
  2342. * The target didn't care to respond to our
  2343. * message request, so clear ATN.
  2344. */
  2345. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2346. }
  2347. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  2348. ahc_outb(ahc, SEQ_FLAGS2,
  2349. ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  2350. }
  2351. static void
  2352. ahc_handle_proto_violation(struct ahc_softc *ahc)
  2353. {
  2354. struct ahc_devinfo devinfo;
  2355. struct scb *scb;
  2356. u_int scbid;
  2357. u_int seq_flags;
  2358. u_int curphase;
  2359. u_int lastphase;
  2360. int found;
  2361. ahc_fetch_devinfo(ahc, &devinfo);
  2362. scbid = ahc_inb(ahc, SCB_TAG);
  2363. scb = ahc_lookup_scb(ahc, scbid);
  2364. seq_flags = ahc_inb(ahc, SEQ_FLAGS);
  2365. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2366. lastphase = ahc_inb(ahc, LASTPHASE);
  2367. if ((seq_flags & NOT_IDENTIFIED) != 0) {
  2368. /*
  2369. * The reconnecting target either did not send an
  2370. * identify message, or did, but we didn't find an SCB
  2371. * to match.
  2372. */
  2373. ahc_print_devinfo(ahc, &devinfo);
  2374. printf("Target did not send an IDENTIFY message. "
  2375. "LASTPHASE = 0x%x.\n", lastphase);
  2376. scb = NULL;
  2377. } else if (scb == NULL) {
  2378. /*
  2379. * We don't seem to have an SCB active for this
  2380. * transaction. Print an error and reset the bus.
  2381. */
  2382. ahc_print_devinfo(ahc, &devinfo);
  2383. printf("No SCB found during protocol violation\n");
  2384. goto proto_violation_reset;
  2385. } else {
  2386. ahc_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
  2387. if ((seq_flags & NO_CDB_SENT) != 0) {
  2388. ahc_print_path(ahc, scb);
  2389. printf("No or incomplete CDB sent to device.\n");
  2390. } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
  2391. /*
  2392. * The target never bothered to provide status to
  2393. * us prior to completing the command. Since we don't
  2394. * know the disposition of this command, we must attempt
  2395. * to abort it. Assert ATN and prepare to send an abort
  2396. * message.
  2397. */
  2398. ahc_print_path(ahc, scb);
  2399. printf("Completed command without status.\n");
  2400. } else {
  2401. ahc_print_path(ahc, scb);
  2402. printf("Unknown protocol violation.\n");
  2403. ahc_dump_card_state(ahc);
  2404. }
  2405. }
  2406. if ((lastphase & ~P_DATAIN_DT) == 0
  2407. || lastphase == P_COMMAND) {
  2408. proto_violation_reset:
  2409. /*
  2410. * Target either went directly to data/command
  2411. * phase or didn't respond to our ATN.
  2412. * The only safe thing to do is to blow
  2413. * it away with a bus reset.
  2414. */
  2415. found = ahc_reset_channel(ahc, 'A', TRUE);
  2416. printf("%s: Issued Channel %c Bus Reset. "
  2417. "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
  2418. } else {
  2419. /*
  2420. * Leave the selection hardware off in case
  2421. * this abort attempt will affect yet to
  2422. * be sent commands.
  2423. */
  2424. ahc_outb(ahc, SCSISEQ,
  2425. ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  2426. ahc_assert_atn(ahc);
  2427. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  2428. if (scb == NULL) {
  2429. ahc_print_devinfo(ahc, &devinfo);
  2430. ahc->msgout_buf[0] = MSG_ABORT_TASK;
  2431. ahc->msgout_len = 1;
  2432. ahc->msgout_index = 0;
  2433. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2434. } else {
  2435. ahc_print_path(ahc, scb);
  2436. scb->flags |= SCB_ABORT;
  2437. }
  2438. printf("Protocol violation %s. Attempting to abort.\n",
  2439. ahc_lookup_phase_entry(curphase)->phasemsg);
  2440. }
  2441. }
  2442. /*
  2443. * Manual message loop handler.
  2444. */
  2445. static void
  2446. ahc_handle_message_phase(struct ahc_softc *ahc)
  2447. {
  2448. struct ahc_devinfo devinfo;
  2449. u_int bus_phase;
  2450. int end_session;
  2451. ahc_fetch_devinfo(ahc, &devinfo);
  2452. end_session = FALSE;
  2453. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2454. reswitch:
  2455. switch (ahc->msg_type) {
  2456. case MSG_TYPE_INITIATOR_MSGOUT:
  2457. {
  2458. int lastbyte;
  2459. int phasemis;
  2460. int msgdone;
  2461. if (ahc->msgout_len == 0)
  2462. panic("HOST_MSG_LOOP interrupt with no active message");
  2463. #ifdef AHC_DEBUG
  2464. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2465. ahc_print_devinfo(ahc, &devinfo);
  2466. printf("INITIATOR_MSG_OUT");
  2467. }
  2468. #endif
  2469. phasemis = bus_phase != P_MESGOUT;
  2470. if (phasemis) {
  2471. #ifdef AHC_DEBUG
  2472. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2473. printf(" PHASEMIS %s\n",
  2474. ahc_lookup_phase_entry(bus_phase)
  2475. ->phasemsg);
  2476. }
  2477. #endif
  2478. if (bus_phase == P_MESGIN) {
  2479. /*
  2480. * Change gears and see if
  2481. * this messages is of interest to
  2482. * us or should be passed back to
  2483. * the sequencer.
  2484. */
  2485. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2486. ahc->send_msg_perror = FALSE;
  2487. ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  2488. ahc->msgin_index = 0;
  2489. goto reswitch;
  2490. }
  2491. end_session = TRUE;
  2492. break;
  2493. }
  2494. if (ahc->send_msg_perror) {
  2495. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2496. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2497. #ifdef AHC_DEBUG
  2498. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2499. printf(" byte 0x%x\n", ahc->send_msg_perror);
  2500. #endif
  2501. ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
  2502. break;
  2503. }
  2504. msgdone = ahc->msgout_index == ahc->msgout_len;
  2505. if (msgdone) {
  2506. /*
  2507. * The target has requested a retry.
  2508. * Re-assert ATN, reset our message index to
  2509. * 0, and try again.
  2510. */
  2511. ahc->msgout_index = 0;
  2512. ahc_assert_atn(ahc);
  2513. }
  2514. lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
  2515. if (lastbyte) {
  2516. /* Last byte is signified by dropping ATN */
  2517. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2518. }
  2519. /*
  2520. * Clear our interrupt status and present
  2521. * the next byte on the bus.
  2522. */
  2523. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2524. #ifdef AHC_DEBUG
  2525. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2526. printf(" byte 0x%x\n",
  2527. ahc->msgout_buf[ahc->msgout_index]);
  2528. #endif
  2529. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2530. break;
  2531. }
  2532. case MSG_TYPE_INITIATOR_MSGIN:
  2533. {
  2534. int phasemis;
  2535. int message_done;
  2536. #ifdef AHC_DEBUG
  2537. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2538. ahc_print_devinfo(ahc, &devinfo);
  2539. printf("INITIATOR_MSG_IN");
  2540. }
  2541. #endif
  2542. phasemis = bus_phase != P_MESGIN;
  2543. if (phasemis) {
  2544. #ifdef AHC_DEBUG
  2545. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2546. printf(" PHASEMIS %s\n",
  2547. ahc_lookup_phase_entry(bus_phase)
  2548. ->phasemsg);
  2549. }
  2550. #endif
  2551. ahc->msgin_index = 0;
  2552. if (bus_phase == P_MESGOUT
  2553. && (ahc->send_msg_perror == TRUE
  2554. || (ahc->msgout_len != 0
  2555. && ahc->msgout_index == 0))) {
  2556. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2557. goto reswitch;
  2558. }
  2559. end_session = TRUE;
  2560. break;
  2561. }
  2562. /* Pull the byte in without acking it */
  2563. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
  2564. #ifdef AHC_DEBUG
  2565. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
  2566. printf(" byte 0x%x\n",
  2567. ahc->msgin_buf[ahc->msgin_index]);
  2568. #endif
  2569. message_done = ahc_parse_msg(ahc, &devinfo);
  2570. if (message_done) {
  2571. /*
  2572. * Clear our incoming message buffer in case there
  2573. * is another message following this one.
  2574. */
  2575. ahc->msgin_index = 0;
  2576. /*
  2577. * If this message illicited a response,
  2578. * assert ATN so the target takes us to the
  2579. * message out phase.
  2580. */
  2581. if (ahc->msgout_len != 0) {
  2582. #ifdef AHC_DEBUG
  2583. if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
  2584. ahc_print_devinfo(ahc, &devinfo);
  2585. printf("Asserting ATN for response\n");
  2586. }
  2587. #endif
  2588. ahc_assert_atn(ahc);
  2589. }
  2590. } else
  2591. ahc->msgin_index++;
  2592. if (message_done == MSGLOOP_TERMINATED) {
  2593. end_session = TRUE;
  2594. } else {
  2595. /* Ack the byte */
  2596. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2597. ahc_inb(ahc, SCSIDATL);
  2598. }
  2599. break;
  2600. }
  2601. case MSG_TYPE_TARGET_MSGIN:
  2602. {
  2603. int msgdone;
  2604. int msgout_request;
  2605. if (ahc->msgout_len == 0)
  2606. panic("Target MSGIN with no active message");
  2607. /*
  2608. * If we interrupted a mesgout session, the initiator
  2609. * will not know this until our first REQ. So, we
  2610. * only honor mesgout requests after we've sent our
  2611. * first byte.
  2612. */
  2613. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
  2614. && ahc->msgout_index > 0)
  2615. msgout_request = TRUE;
  2616. else
  2617. msgout_request = FALSE;
  2618. if (msgout_request) {
  2619. /*
  2620. * Change gears and see if
  2621. * this messages is of interest to
  2622. * us or should be passed back to
  2623. * the sequencer.
  2624. */
  2625. ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
  2626. ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
  2627. ahc->msgin_index = 0;
  2628. /* Dummy read to REQ for first byte */
  2629. ahc_inb(ahc, SCSIDATL);
  2630. ahc_outb(ahc, SXFRCTL0,
  2631. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2632. break;
  2633. }
  2634. msgdone = ahc->msgout_index == ahc->msgout_len;
  2635. if (msgdone) {
  2636. ahc_outb(ahc, SXFRCTL0,
  2637. ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2638. end_session = TRUE;
  2639. break;
  2640. }
  2641. /*
  2642. * Present the next byte on the bus.
  2643. */
  2644. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2645. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2646. break;
  2647. }
  2648. case MSG_TYPE_TARGET_MSGOUT:
  2649. {
  2650. int lastbyte;
  2651. int msgdone;
  2652. /*
  2653. * The initiator signals that this is
  2654. * the last byte by dropping ATN.
  2655. */
  2656. lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
  2657. /*
  2658. * Read the latched byte, but turn off SPIOEN first
  2659. * so that we don't inadvertently cause a REQ for the
  2660. * next byte.
  2661. */
  2662. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2663. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
  2664. msgdone = ahc_parse_msg(ahc, &devinfo);
  2665. if (msgdone == MSGLOOP_TERMINATED) {
  2666. /*
  2667. * The message is *really* done in that it caused
  2668. * us to go to bus free. The sequencer has already
  2669. * been reset at this point, so pull the ejection
  2670. * handle.
  2671. */
  2672. return;
  2673. }
  2674. ahc->msgin_index++;
  2675. /*
  2676. * XXX Read spec about initiator dropping ATN too soon
  2677. * and use msgdone to detect it.
  2678. */
  2679. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  2680. ahc->msgin_index = 0;
  2681. /*
  2682. * If this message illicited a response, transition
  2683. * to the Message in phase and send it.
  2684. */
  2685. if (ahc->msgout_len != 0) {
  2686. ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
  2687. ahc_outb(ahc, SXFRCTL0,
  2688. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2689. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  2690. ahc->msgin_index = 0;
  2691. break;
  2692. }
  2693. }
  2694. if (lastbyte)
  2695. end_session = TRUE;
  2696. else {
  2697. /* Ask for the next byte. */
  2698. ahc_outb(ahc, SXFRCTL0,
  2699. ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2700. }
  2701. break;
  2702. }
  2703. default:
  2704. panic("Unknown REQINIT message type");
  2705. }
  2706. if (end_session) {
  2707. ahc_clear_msg_state(ahc);
  2708. ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
  2709. } else
  2710. ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
  2711. }
  2712. /*
  2713. * See if we sent a particular extended message to the target.
  2714. * If "full" is true, return true only if the target saw the full
  2715. * message. If "full" is false, return true if the target saw at
  2716. * least the first byte of the message.
  2717. */
  2718. static int
  2719. ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
  2720. {
  2721. int found;
  2722. u_int index;
  2723. found = FALSE;
  2724. index = 0;
  2725. while (index < ahc->msgout_len) {
  2726. if (ahc->msgout_buf[index] == MSG_EXTENDED) {
  2727. u_int end_index;
  2728. end_index = index + 1 + ahc->msgout_buf[index + 1];
  2729. if (ahc->msgout_buf[index+2] == msgval
  2730. && type == AHCMSG_EXT) {
  2731. if (full) {
  2732. if (ahc->msgout_index > end_index)
  2733. found = TRUE;
  2734. } else if (ahc->msgout_index > index)
  2735. found = TRUE;
  2736. }
  2737. index = end_index;
  2738. } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
  2739. && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  2740. /* Skip tag type and tag id or residue param*/
  2741. index += 2;
  2742. } else {
  2743. /* Single byte message */
  2744. if (type == AHCMSG_1B
  2745. && ahc->msgout_buf[index] == msgval
  2746. && ahc->msgout_index > index)
  2747. found = TRUE;
  2748. index++;
  2749. }
  2750. if (found)
  2751. break;
  2752. }
  2753. return (found);
  2754. }
  2755. /*
  2756. * Wait for a complete incoming message, parse it, and respond accordingly.
  2757. */
  2758. static int
  2759. ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2760. {
  2761. struct ahc_initiator_tinfo *tinfo;
  2762. struct ahc_tmode_tstate *tstate;
  2763. int reject;
  2764. int done;
  2765. int response;
  2766. u_int targ_scsirate;
  2767. done = MSGLOOP_IN_PROG;
  2768. response = FALSE;
  2769. reject = FALSE;
  2770. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2771. devinfo->target, &tstate);
  2772. targ_scsirate = tinfo->scsirate;
  2773. /*
  2774. * Parse as much of the message as is available,
  2775. * rejecting it if we don't support it. When
  2776. * the entire message is available and has been
  2777. * handled, return MSGLOOP_MSGCOMPLETE, indicating
  2778. * that we have parsed an entire message.
  2779. *
  2780. * In the case of extended messages, we accept the length
  2781. * byte outright and perform more checking once we know the
  2782. * extended message type.
  2783. */
  2784. switch (ahc->msgin_buf[0]) {
  2785. case MSG_DISCONNECT:
  2786. case MSG_SAVEDATAPOINTER:
  2787. case MSG_CMDCOMPLETE:
  2788. case MSG_RESTOREPOINTERS:
  2789. case MSG_IGN_WIDE_RESIDUE:
  2790. /*
  2791. * End our message loop as these are messages
  2792. * the sequencer handles on its own.
  2793. */
  2794. done = MSGLOOP_TERMINATED;
  2795. break;
  2796. case MSG_MESSAGE_REJECT:
  2797. response = ahc_handle_msg_reject(ahc, devinfo);
  2798. /* FALLTHROUGH */
  2799. case MSG_NOOP:
  2800. done = MSGLOOP_MSGCOMPLETE;
  2801. break;
  2802. case MSG_EXTENDED:
  2803. {
  2804. /* Wait for enough of the message to begin validation */
  2805. if (ahc->msgin_index < 2)
  2806. break;
  2807. switch (ahc->msgin_buf[2]) {
  2808. case MSG_EXT_SDTR:
  2809. {
  2810. struct ahc_syncrate *syncrate;
  2811. u_int period;
  2812. u_int ppr_options;
  2813. u_int offset;
  2814. u_int saved_offset;
  2815. if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  2816. reject = TRUE;
  2817. break;
  2818. }
  2819. /*
  2820. * Wait until we have both args before validating
  2821. * and acting on this message.
  2822. *
  2823. * Add one to MSG_EXT_SDTR_LEN to account for
  2824. * the extended message preamble.
  2825. */
  2826. if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  2827. break;
  2828. period = ahc->msgin_buf[3];
  2829. ppr_options = 0;
  2830. saved_offset = offset = ahc->msgin_buf[4];
  2831. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2832. &ppr_options,
  2833. devinfo->role);
  2834. ahc_validate_offset(ahc, tinfo, syncrate, &offset,
  2835. targ_scsirate & WIDEXFER,
  2836. devinfo->role);
  2837. if (bootverbose) {
  2838. printf("(%s:%c:%d:%d): Received "
  2839. "SDTR period %x, offset %x\n\t"
  2840. "Filtered to period %x, offset %x\n",
  2841. ahc_name(ahc), devinfo->channel,
  2842. devinfo->target, devinfo->lun,
  2843. ahc->msgin_buf[3], saved_offset,
  2844. period, offset);
  2845. }
  2846. ahc_set_syncrate(ahc, devinfo,
  2847. syncrate, period,
  2848. offset, ppr_options,
  2849. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2850. /*paused*/TRUE);
  2851. /*
  2852. * See if we initiated Sync Negotiation
  2853. * and didn't have to fall down to async
  2854. * transfers.
  2855. */
  2856. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  2857. /* We started it */
  2858. if (saved_offset != offset) {
  2859. /* Went too low - force async */
  2860. reject = TRUE;
  2861. }
  2862. } else {
  2863. /*
  2864. * Send our own SDTR in reply
  2865. */
  2866. if (bootverbose
  2867. && devinfo->role == ROLE_INITIATOR) {
  2868. printf("(%s:%c:%d:%d): Target "
  2869. "Initiated SDTR\n",
  2870. ahc_name(ahc), devinfo->channel,
  2871. devinfo->target, devinfo->lun);
  2872. }
  2873. ahc->msgout_index = 0;
  2874. ahc->msgout_len = 0;
  2875. ahc_construct_sdtr(ahc, devinfo,
  2876. period, offset);
  2877. ahc->msgout_index = 0;
  2878. response = TRUE;
  2879. }
  2880. done = MSGLOOP_MSGCOMPLETE;
  2881. break;
  2882. }
  2883. case MSG_EXT_WDTR:
  2884. {
  2885. u_int bus_width;
  2886. u_int saved_width;
  2887. u_int sending_reply;
  2888. sending_reply = FALSE;
  2889. if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  2890. reject = TRUE;
  2891. break;
  2892. }
  2893. /*
  2894. * Wait until we have our arg before validating
  2895. * and acting on this message.
  2896. *
  2897. * Add one to MSG_EXT_WDTR_LEN to account for
  2898. * the extended message preamble.
  2899. */
  2900. if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  2901. break;
  2902. bus_width = ahc->msgin_buf[3];
  2903. saved_width = bus_width;
  2904. ahc_validate_width(ahc, tinfo, &bus_width,
  2905. devinfo->role);
  2906. if (bootverbose) {
  2907. printf("(%s:%c:%d:%d): Received WDTR "
  2908. "%x filtered to %x\n",
  2909. ahc_name(ahc), devinfo->channel,
  2910. devinfo->target, devinfo->lun,
  2911. saved_width, bus_width);
  2912. }
  2913. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  2914. /*
  2915. * Don't send a WDTR back to the
  2916. * target, since we asked first.
  2917. * If the width went higher than our
  2918. * request, reject it.
  2919. */
  2920. if (saved_width > bus_width) {
  2921. reject = TRUE;
  2922. printf("(%s:%c:%d:%d): requested %dBit "
  2923. "transfers. Rejecting...\n",
  2924. ahc_name(ahc), devinfo->channel,
  2925. devinfo->target, devinfo->lun,
  2926. 8 * (0x01 << bus_width));
  2927. bus_width = 0;
  2928. }
  2929. } else {
  2930. /*
  2931. * Send our own WDTR in reply
  2932. */
  2933. if (bootverbose
  2934. && devinfo->role == ROLE_INITIATOR) {
  2935. printf("(%s:%c:%d:%d): Target "
  2936. "Initiated WDTR\n",
  2937. ahc_name(ahc), devinfo->channel,
  2938. devinfo->target, devinfo->lun);
  2939. }
  2940. ahc->msgout_index = 0;
  2941. ahc->msgout_len = 0;
  2942. ahc_construct_wdtr(ahc, devinfo, bus_width);
  2943. ahc->msgout_index = 0;
  2944. response = TRUE;
  2945. sending_reply = TRUE;
  2946. }
  2947. /*
  2948. * After a wide message, we are async, but
  2949. * some devices don't seem to honor this portion
  2950. * of the spec. Force a renegotiation of the
  2951. * sync component of our transfer agreement even
  2952. * if our goal is async. By updating our width
  2953. * after forcing the negotiation, we avoid
  2954. * renegotiating for width.
  2955. */
  2956. ahc_update_neg_request(ahc, devinfo, tstate,
  2957. tinfo, AHC_NEG_ALWAYS);
  2958. ahc_set_width(ahc, devinfo, bus_width,
  2959. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2960. /*paused*/TRUE);
  2961. if (sending_reply == FALSE && reject == FALSE) {
  2962. /*
  2963. * We will always have an SDTR to send.
  2964. */
  2965. ahc->msgout_index = 0;
  2966. ahc->msgout_len = 0;
  2967. ahc_build_transfer_msg(ahc, devinfo);
  2968. ahc->msgout_index = 0;
  2969. response = TRUE;
  2970. }
  2971. done = MSGLOOP_MSGCOMPLETE;
  2972. break;
  2973. }
  2974. case MSG_EXT_PPR:
  2975. {
  2976. struct ahc_syncrate *syncrate;
  2977. u_int period;
  2978. u_int offset;
  2979. u_int bus_width;
  2980. u_int ppr_options;
  2981. u_int saved_width;
  2982. u_int saved_offset;
  2983. u_int saved_ppr_options;
  2984. if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  2985. reject = TRUE;
  2986. break;
  2987. }
  2988. /*
  2989. * Wait until we have all args before validating
  2990. * and acting on this message.
  2991. *
  2992. * Add one to MSG_EXT_PPR_LEN to account for
  2993. * the extended message preamble.
  2994. */
  2995. if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
  2996. break;
  2997. period = ahc->msgin_buf[3];
  2998. offset = ahc->msgin_buf[5];
  2999. bus_width = ahc->msgin_buf[6];
  3000. saved_width = bus_width;
  3001. ppr_options = ahc->msgin_buf[7];
  3002. /*
  3003. * According to the spec, a DT only
  3004. * period factor with no DT option
  3005. * set implies async.
  3006. */
  3007. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  3008. && period == 9)
  3009. offset = 0;
  3010. saved_ppr_options = ppr_options;
  3011. saved_offset = offset;
  3012. /*
  3013. * Mask out any options we don't support
  3014. * on any controller. Transfer options are
  3015. * only available if we are negotiating wide.
  3016. */
  3017. ppr_options &= MSG_EXT_PPR_DT_REQ;
  3018. if (bus_width == 0)
  3019. ppr_options = 0;
  3020. ahc_validate_width(ahc, tinfo, &bus_width,
  3021. devinfo->role);
  3022. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  3023. &ppr_options,
  3024. devinfo->role);
  3025. ahc_validate_offset(ahc, tinfo, syncrate,
  3026. &offset, bus_width,
  3027. devinfo->role);
  3028. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
  3029. /*
  3030. * If we are unable to do any of the
  3031. * requested options (we went too low),
  3032. * then we'll have to reject the message.
  3033. */
  3034. if (saved_width > bus_width
  3035. || saved_offset != offset
  3036. || saved_ppr_options != ppr_options) {
  3037. reject = TRUE;
  3038. period = 0;
  3039. offset = 0;
  3040. bus_width = 0;
  3041. ppr_options = 0;
  3042. syncrate = NULL;
  3043. }
  3044. } else {
  3045. if (devinfo->role != ROLE_TARGET)
  3046. printf("(%s:%c:%d:%d): Target "
  3047. "Initiated PPR\n",
  3048. ahc_name(ahc), devinfo->channel,
  3049. devinfo->target, devinfo->lun);
  3050. else
  3051. printf("(%s:%c:%d:%d): Initiator "
  3052. "Initiated PPR\n",
  3053. ahc_name(ahc), devinfo->channel,
  3054. devinfo->target, devinfo->lun);
  3055. ahc->msgout_index = 0;
  3056. ahc->msgout_len = 0;
  3057. ahc_construct_ppr(ahc, devinfo, period, offset,
  3058. bus_width, ppr_options);
  3059. ahc->msgout_index = 0;
  3060. response = TRUE;
  3061. }
  3062. if (bootverbose) {
  3063. printf("(%s:%c:%d:%d): Received PPR width %x, "
  3064. "period %x, offset %x,options %x\n"
  3065. "\tFiltered to width %x, period %x, "
  3066. "offset %x, options %x\n",
  3067. ahc_name(ahc), devinfo->channel,
  3068. devinfo->target, devinfo->lun,
  3069. saved_width, ahc->msgin_buf[3],
  3070. saved_offset, saved_ppr_options,
  3071. bus_width, period, offset, ppr_options);
  3072. }
  3073. ahc_set_width(ahc, devinfo, bus_width,
  3074. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3075. /*paused*/TRUE);
  3076. ahc_set_syncrate(ahc, devinfo,
  3077. syncrate, period,
  3078. offset, ppr_options,
  3079. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3080. /*paused*/TRUE);
  3081. done = MSGLOOP_MSGCOMPLETE;
  3082. break;
  3083. }
  3084. default:
  3085. /* Unknown extended message. Reject it. */
  3086. reject = TRUE;
  3087. break;
  3088. }
  3089. break;
  3090. }
  3091. #ifdef AHC_TARGET_MODE
  3092. case MSG_BUS_DEV_RESET:
  3093. ahc_handle_devreset(ahc, devinfo,
  3094. CAM_BDR_SENT,
  3095. "Bus Device Reset Received",
  3096. /*verbose_level*/0);
  3097. ahc_restart(ahc);
  3098. done = MSGLOOP_TERMINATED;
  3099. break;
  3100. case MSG_ABORT_TAG:
  3101. case MSG_ABORT:
  3102. case MSG_CLEAR_QUEUE:
  3103. {
  3104. int tag;
  3105. /* Target mode messages */
  3106. if (devinfo->role != ROLE_TARGET) {
  3107. reject = TRUE;
  3108. break;
  3109. }
  3110. tag = SCB_LIST_NULL;
  3111. if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
  3112. tag = ahc_inb(ahc, INITIATOR_TAG);
  3113. ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3114. devinfo->lun, tag, ROLE_TARGET,
  3115. CAM_REQ_ABORTED);
  3116. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3117. if (tstate != NULL) {
  3118. struct ahc_tmode_lstate* lstate;
  3119. lstate = tstate->enabled_luns[devinfo->lun];
  3120. if (lstate != NULL) {
  3121. ahc_queue_lstate_event(ahc, lstate,
  3122. devinfo->our_scsiid,
  3123. ahc->msgin_buf[0],
  3124. /*arg*/tag);
  3125. ahc_send_lstate_events(ahc, lstate);
  3126. }
  3127. }
  3128. ahc_restart(ahc);
  3129. done = MSGLOOP_TERMINATED;
  3130. break;
  3131. }
  3132. #endif
  3133. case MSG_TERM_IO_PROC:
  3134. default:
  3135. reject = TRUE;
  3136. break;
  3137. }
  3138. if (reject) {
  3139. /*
  3140. * Setup to reject the message.
  3141. */
  3142. ahc->msgout_index = 0;
  3143. ahc->msgout_len = 1;
  3144. ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
  3145. done = MSGLOOP_MSGCOMPLETE;
  3146. response = TRUE;
  3147. }
  3148. if (done != MSGLOOP_IN_PROG && !response)
  3149. /* Clear the outgoing message buffer */
  3150. ahc->msgout_len = 0;
  3151. return (done);
  3152. }
  3153. /*
  3154. * Process a message reject message.
  3155. */
  3156. static int
  3157. ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3158. {
  3159. /*
  3160. * What we care about here is if we had an
  3161. * outstanding SDTR or WDTR message for this
  3162. * target. If we did, this is a signal that
  3163. * the target is refusing negotiation.
  3164. */
  3165. struct scb *scb;
  3166. struct ahc_initiator_tinfo *tinfo;
  3167. struct ahc_tmode_tstate *tstate;
  3168. u_int scb_index;
  3169. u_int last_msg;
  3170. int response = 0;
  3171. scb_index = ahc_inb(ahc, SCB_TAG);
  3172. scb = ahc_lookup_scb(ahc, scb_index);
  3173. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
  3174. devinfo->our_scsiid,
  3175. devinfo->target, &tstate);
  3176. /* Might be necessary */
  3177. last_msg = ahc_inb(ahc, LAST_MSG);
  3178. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  3179. /*
  3180. * Target does not support the PPR message.
  3181. * Attempt to negotiate SPI-2 style.
  3182. */
  3183. if (bootverbose) {
  3184. printf("(%s:%c:%d:%d): PPR Rejected. "
  3185. "Trying WDTR/SDTR\n",
  3186. ahc_name(ahc), devinfo->channel,
  3187. devinfo->target, devinfo->lun);
  3188. }
  3189. tinfo->goal.ppr_options = 0;
  3190. tinfo->curr.transport_version = 2;
  3191. tinfo->goal.transport_version = 2;
  3192. ahc->msgout_index = 0;
  3193. ahc->msgout_len = 0;
  3194. ahc_build_transfer_msg(ahc, devinfo);
  3195. ahc->msgout_index = 0;
  3196. response = 1;
  3197. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  3198. /* note 8bit xfers */
  3199. printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
  3200. "8bit transfers\n", ahc_name(ahc),
  3201. devinfo->channel, devinfo->target, devinfo->lun);
  3202. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3203. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3204. /*paused*/TRUE);
  3205. /*
  3206. * No need to clear the sync rate. If the target
  3207. * did not accept the command, our syncrate is
  3208. * unaffected. If the target started the negotiation,
  3209. * but rejected our response, we already cleared the
  3210. * sync rate before sending our WDTR.
  3211. */
  3212. if (tinfo->goal.offset != tinfo->curr.offset) {
  3213. /* Start the sync negotiation */
  3214. ahc->msgout_index = 0;
  3215. ahc->msgout_len = 0;
  3216. ahc_build_transfer_msg(ahc, devinfo);
  3217. ahc->msgout_index = 0;
  3218. response = 1;
  3219. }
  3220. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  3221. /* note asynch xfers and clear flag */
  3222. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
  3223. /*offset*/0, /*ppr_options*/0,
  3224. AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  3225. /*paused*/TRUE);
  3226. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  3227. "Using asynchronous transfers\n",
  3228. ahc_name(ahc), devinfo->channel,
  3229. devinfo->target, devinfo->lun);
  3230. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  3231. int tag_type;
  3232. int mask;
  3233. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  3234. if (tag_type == MSG_SIMPLE_TASK) {
  3235. printf("(%s:%c:%d:%d): refuses tagged commands. "
  3236. "Performing non-tagged I/O\n", ahc_name(ahc),
  3237. devinfo->channel, devinfo->target, devinfo->lun);
  3238. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_NONE);
  3239. mask = ~0x23;
  3240. } else {
  3241. printf("(%s:%c:%d:%d): refuses %s tagged commands. "
  3242. "Performing simple queue tagged I/O only\n",
  3243. ahc_name(ahc), devinfo->channel, devinfo->target,
  3244. devinfo->lun, tag_type == MSG_ORDERED_TASK
  3245. ? "ordered" : "head of queue");
  3246. ahc_set_tags(ahc, scb->io_ctx, devinfo, AHC_QUEUE_BASIC);
  3247. mask = ~0x03;
  3248. }
  3249. /*
  3250. * Resend the identify for this CCB as the target
  3251. * may believe that the selection is invalid otherwise.
  3252. */
  3253. ahc_outb(ahc, SCB_CONTROL,
  3254. ahc_inb(ahc, SCB_CONTROL) & mask);
  3255. scb->hscb->control &= mask;
  3256. ahc_set_transaction_tag(scb, /*enabled*/FALSE,
  3257. /*type*/MSG_SIMPLE_TASK);
  3258. ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
  3259. ahc_assert_atn(ahc);
  3260. /*
  3261. * This transaction is now at the head of
  3262. * the untagged queue for this target.
  3263. */
  3264. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  3265. struct scb_tailq *untagged_q;
  3266. untagged_q =
  3267. &(ahc->untagged_queues[devinfo->target_offset]);
  3268. TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
  3269. scb->flags |= SCB_UNTAGGEDQ;
  3270. }
  3271. ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  3272. scb->hscb->tag);
  3273. /*
  3274. * Requeue all tagged commands for this target
  3275. * currently in our posession so they can be
  3276. * converted to untagged commands.
  3277. */
  3278. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  3279. SCB_GET_CHANNEL(ahc, scb),
  3280. SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  3281. ROLE_INITIATOR, CAM_REQUEUE_REQ,
  3282. SEARCH_COMPLETE);
  3283. } else {
  3284. /*
  3285. * Otherwise, we ignore it.
  3286. */
  3287. printf("%s:%c:%d: Message reject for %x -- ignored\n",
  3288. ahc_name(ahc), devinfo->channel, devinfo->target,
  3289. last_msg);
  3290. }
  3291. return (response);
  3292. }
  3293. /*
  3294. * Process an ingnore wide residue message.
  3295. */
  3296. static void
  3297. ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3298. {
  3299. u_int scb_index;
  3300. struct scb *scb;
  3301. scb_index = ahc_inb(ahc, SCB_TAG);
  3302. scb = ahc_lookup_scb(ahc, scb_index);
  3303. /*
  3304. * XXX Actually check data direction in the sequencer?
  3305. * Perhaps add datadir to some spare bits in the hscb?
  3306. */
  3307. if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
  3308. || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
  3309. /*
  3310. * Ignore the message if we haven't
  3311. * seen an appropriate data phase yet.
  3312. */
  3313. } else {
  3314. /*
  3315. * If the residual occurred on the last
  3316. * transfer and the transfer request was
  3317. * expected to end on an odd count, do
  3318. * nothing. Otherwise, subtract a byte
  3319. * and update the residual count accordingly.
  3320. */
  3321. uint32_t sgptr;
  3322. sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3323. if ((sgptr & SG_LIST_NULL) != 0
  3324. && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
  3325. /*
  3326. * If the residual occurred on the last
  3327. * transfer and the transfer request was
  3328. * expected to end on an odd count, do
  3329. * nothing.
  3330. */
  3331. } else {
  3332. struct ahc_dma_seg *sg;
  3333. uint32_t data_cnt;
  3334. uint32_t data_addr;
  3335. uint32_t sglen;
  3336. /* Pull in all of the sgptr */
  3337. sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
  3338. data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
  3339. if ((sgptr & SG_LIST_NULL) != 0) {
  3340. /*
  3341. * The residual data count is not updated
  3342. * for the command run to completion case.
  3343. * Explicitly zero the count.
  3344. */
  3345. data_cnt &= ~AHC_SG_LEN_MASK;
  3346. }
  3347. data_addr = ahc_inl(ahc, SHADDR);
  3348. data_cnt += 1;
  3349. data_addr -= 1;
  3350. sgptr &= SG_PTR_MASK;
  3351. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3352. /*
  3353. * The residual sg ptr points to the next S/G
  3354. * to load so we must go back one.
  3355. */
  3356. sg--;
  3357. sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  3358. if (sg != scb->sg_list
  3359. && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
  3360. sg--;
  3361. sglen = ahc_le32toh(sg->len);
  3362. /*
  3363. * Preserve High Address and SG_LIST bits
  3364. * while setting the count to 1.
  3365. */
  3366. data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
  3367. data_addr = ahc_le32toh(sg->addr)
  3368. + (sglen & AHC_SG_LEN_MASK) - 1;
  3369. /*
  3370. * Increment sg so it points to the
  3371. * "next" sg.
  3372. */
  3373. sg++;
  3374. sgptr = ahc_sg_virt_to_bus(scb, sg);
  3375. }
  3376. ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
  3377. ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
  3378. /*
  3379. * Toggle the "oddness" of the transfer length
  3380. * to handle this mid-transfer ignore wide
  3381. * residue. This ensures that the oddness is
  3382. * correct for subsequent data transfers.
  3383. */
  3384. ahc_outb(ahc, SCB_LUN,
  3385. ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
  3386. }
  3387. }
  3388. }
  3389. /*
  3390. * Reinitialize the data pointers for the active transfer
  3391. * based on its current residual.
  3392. */
  3393. static void
  3394. ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
  3395. {
  3396. struct scb *scb;
  3397. struct ahc_dma_seg *sg;
  3398. u_int scb_index;
  3399. uint32_t sgptr;
  3400. uint32_t resid;
  3401. uint32_t dataptr;
  3402. scb_index = ahc_inb(ahc, SCB_TAG);
  3403. scb = ahc_lookup_scb(ahc, scb_index);
  3404. sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3405. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3406. | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
  3407. | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3408. sgptr &= SG_PTR_MASK;
  3409. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3410. /* The residual sg_ptr always points to the next sg */
  3411. sg--;
  3412. resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
  3413. | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
  3414. | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
  3415. dataptr = ahc_le32toh(sg->addr)
  3416. + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
  3417. - resid;
  3418. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  3419. u_int dscommand1;
  3420. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  3421. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  3422. ahc_outb(ahc, HADDR,
  3423. (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
  3424. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  3425. }
  3426. ahc_outb(ahc, HADDR + 3, dataptr >> 24);
  3427. ahc_outb(ahc, HADDR + 2, dataptr >> 16);
  3428. ahc_outb(ahc, HADDR + 1, dataptr >> 8);
  3429. ahc_outb(ahc, HADDR, dataptr);
  3430. ahc_outb(ahc, HCNT + 2, resid >> 16);
  3431. ahc_outb(ahc, HCNT + 1, resid >> 8);
  3432. ahc_outb(ahc, HCNT, resid);
  3433. if ((ahc->features & AHC_ULTRA2) == 0) {
  3434. ahc_outb(ahc, STCNT + 2, resid >> 16);
  3435. ahc_outb(ahc, STCNT + 1, resid >> 8);
  3436. ahc_outb(ahc, STCNT, resid);
  3437. }
  3438. }
  3439. /*
  3440. * Handle the effects of issuing a bus device reset message.
  3441. */
  3442. static void
  3443. ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3444. cam_status status, char *message, int verbose_level)
  3445. {
  3446. #ifdef AHC_TARGET_MODE
  3447. struct ahc_tmode_tstate* tstate;
  3448. u_int lun;
  3449. #endif
  3450. int found;
  3451. found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3452. CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
  3453. status);
  3454. #ifdef AHC_TARGET_MODE
  3455. /*
  3456. * Send an immediate notify ccb to all target mord peripheral
  3457. * drivers affected by this action.
  3458. */
  3459. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3460. if (tstate != NULL) {
  3461. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  3462. struct ahc_tmode_lstate* lstate;
  3463. lstate = tstate->enabled_luns[lun];
  3464. if (lstate == NULL)
  3465. continue;
  3466. ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
  3467. MSG_BUS_DEV_RESET, /*arg*/0);
  3468. ahc_send_lstate_events(ahc, lstate);
  3469. }
  3470. }
  3471. #endif
  3472. /*
  3473. * Go back to async/narrow transfers and renegotiate.
  3474. */
  3475. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3476. AHC_TRANS_CUR, /*paused*/TRUE);
  3477. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
  3478. /*period*/0, /*offset*/0, /*ppr_options*/0,
  3479. AHC_TRANS_CUR, /*paused*/TRUE);
  3480. if (status != CAM_SEL_TIMEOUT)
  3481. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  3482. CAM_LUN_WILDCARD, AC_SENT_BDR);
  3483. if (message != NULL
  3484. && (verbose_level <= bootverbose))
  3485. printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
  3486. message, devinfo->channel, devinfo->target, found);
  3487. }
  3488. #ifdef AHC_TARGET_MODE
  3489. static void
  3490. ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3491. struct scb *scb)
  3492. {
  3493. /*
  3494. * To facilitate adding multiple messages together,
  3495. * each routine should increment the index and len
  3496. * variables instead of setting them explicitly.
  3497. */
  3498. ahc->msgout_index = 0;
  3499. ahc->msgout_len = 0;
  3500. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  3501. ahc_build_transfer_msg(ahc, devinfo);
  3502. else
  3503. panic("ahc_intr: AWAITING target message with no message");
  3504. ahc->msgout_index = 0;
  3505. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  3506. }
  3507. #endif
  3508. /**************************** Initialization **********************************/
  3509. /*
  3510. * Allocate a controller structure for a new device
  3511. * and perform initial initializion.
  3512. */
  3513. struct ahc_softc *
  3514. ahc_alloc(void *platform_arg, char *name)
  3515. {
  3516. struct ahc_softc *ahc;
  3517. int i;
  3518. #ifndef __FreeBSD__
  3519. ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
  3520. if (!ahc) {
  3521. printf("aic7xxx: cannot malloc softc!\n");
  3522. free(name, M_DEVBUF);
  3523. return NULL;
  3524. }
  3525. #else
  3526. ahc = device_get_softc((device_t)platform_arg);
  3527. #endif
  3528. memset(ahc, 0, sizeof(*ahc));
  3529. ahc->seep_config = malloc(sizeof(*ahc->seep_config),
  3530. M_DEVBUF, M_NOWAIT);
  3531. if (ahc->seep_config == NULL) {
  3532. #ifndef __FreeBSD__
  3533. free(ahc, M_DEVBUF);
  3534. #endif
  3535. free(name, M_DEVBUF);
  3536. return (NULL);
  3537. }
  3538. LIST_INIT(&ahc->pending_scbs);
  3539. /* We don't know our unit number until the OSM sets it */
  3540. ahc->name = name;
  3541. ahc->unit = -1;
  3542. ahc->description = NULL;
  3543. ahc->channel = 'A';
  3544. ahc->channel_b = 'B';
  3545. ahc->chip = AHC_NONE;
  3546. ahc->features = AHC_FENONE;
  3547. ahc->bugs = AHC_BUGNONE;
  3548. ahc->flags = AHC_FNONE;
  3549. /*
  3550. * Default to all error reporting enabled with the
  3551. * sequencer operating at its fastest speed.
  3552. * The bus attach code may modify this.
  3553. */
  3554. ahc->seqctl = FASTMODE;
  3555. for (i = 0; i < AHC_NUM_TARGETS; i++)
  3556. TAILQ_INIT(&ahc->untagged_queues[i]);
  3557. if (ahc_platform_alloc(ahc, platform_arg) != 0) {
  3558. ahc_free(ahc);
  3559. ahc = NULL;
  3560. }
  3561. return (ahc);
  3562. }
  3563. int
  3564. ahc_softc_init(struct ahc_softc *ahc)
  3565. {
  3566. /* The IRQMS bit is only valid on VL and EISA chips */
  3567. if ((ahc->chip & AHC_PCI) == 0)
  3568. ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
  3569. else
  3570. ahc->unpause = 0;
  3571. ahc->pause = ahc->unpause | PAUSE;
  3572. /* XXX The shared scb data stuff should be deprecated */
  3573. if (ahc->scb_data == NULL) {
  3574. ahc->scb_data = malloc(sizeof(*ahc->scb_data),
  3575. M_DEVBUF, M_NOWAIT);
  3576. if (ahc->scb_data == NULL)
  3577. return (ENOMEM);
  3578. memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
  3579. }
  3580. return (0);
  3581. }
  3582. void
  3583. ahc_set_unit(struct ahc_softc *ahc, int unit)
  3584. {
  3585. ahc->unit = unit;
  3586. }
  3587. void
  3588. ahc_set_name(struct ahc_softc *ahc, char *name)
  3589. {
  3590. if (ahc->name != NULL)
  3591. free(ahc->name, M_DEVBUF);
  3592. ahc->name = name;
  3593. }
  3594. void
  3595. ahc_free(struct ahc_softc *ahc)
  3596. {
  3597. int i;
  3598. switch (ahc->init_level) {
  3599. default:
  3600. case 5:
  3601. ahc_shutdown(ahc);
  3602. /* FALLTHROUGH */
  3603. case 4:
  3604. ahc_dmamap_unload(ahc, ahc->shared_data_dmat,
  3605. ahc->shared_data_dmamap);
  3606. /* FALLTHROUGH */
  3607. case 3:
  3608. ahc_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
  3609. ahc->shared_data_dmamap);
  3610. ahc_dmamap_destroy(ahc, ahc->shared_data_dmat,
  3611. ahc->shared_data_dmamap);
  3612. /* FALLTHROUGH */
  3613. case 2:
  3614. ahc_dma_tag_destroy(ahc, ahc->shared_data_dmat);
  3615. case 1:
  3616. #ifndef __linux__
  3617. ahc_dma_tag_destroy(ahc, ahc->buffer_dmat);
  3618. #endif
  3619. break;
  3620. case 0:
  3621. break;
  3622. }
  3623. #ifndef __linux__
  3624. ahc_dma_tag_destroy(ahc, ahc->parent_dmat);
  3625. #endif
  3626. ahc_platform_free(ahc);
  3627. ahc_fini_scbdata(ahc);
  3628. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  3629. struct ahc_tmode_tstate *tstate;
  3630. tstate = ahc->enabled_targets[i];
  3631. if (tstate != NULL) {
  3632. #ifdef AHC_TARGET_MODE
  3633. int j;
  3634. for (j = 0; j < AHC_NUM_LUNS; j++) {
  3635. struct ahc_tmode_lstate *lstate;
  3636. lstate = tstate->enabled_luns[j];
  3637. if (lstate != NULL) {
  3638. xpt_free_path(lstate->path);
  3639. free(lstate, M_DEVBUF);
  3640. }
  3641. }
  3642. #endif
  3643. free(tstate, M_DEVBUF);
  3644. }
  3645. }
  3646. #ifdef AHC_TARGET_MODE
  3647. if (ahc->black_hole != NULL) {
  3648. xpt_free_path(ahc->black_hole->path);
  3649. free(ahc->black_hole, M_DEVBUF);
  3650. }
  3651. #endif
  3652. if (ahc->name != NULL)
  3653. free(ahc->name, M_DEVBUF);
  3654. if (ahc->seep_config != NULL)
  3655. free(ahc->seep_config, M_DEVBUF);
  3656. #ifndef __FreeBSD__
  3657. free(ahc, M_DEVBUF);
  3658. #endif
  3659. return;
  3660. }
  3661. void
  3662. ahc_shutdown(void *arg)
  3663. {
  3664. struct ahc_softc *ahc;
  3665. int i;
  3666. ahc = (struct ahc_softc *)arg;
  3667. /* This will reset most registers to 0, but not all */
  3668. ahc_reset(ahc, /*reinit*/FALSE);
  3669. ahc_outb(ahc, SCSISEQ, 0);
  3670. ahc_outb(ahc, SXFRCTL0, 0);
  3671. ahc_outb(ahc, DSPCISTATUS, 0);
  3672. for (i = TARG_SCSIRATE; i < SCSICONF; i++)
  3673. ahc_outb(ahc, i, 0);
  3674. }
  3675. /*
  3676. * Reset the controller and record some information about it
  3677. * that is only available just after a reset. If "reinit" is
  3678. * non-zero, this reset occured after initial configuration
  3679. * and the caller requests that the chip be fully reinitialized
  3680. * to a runable state. Chip interrupts are *not* enabled after
  3681. * a reinitialization. The caller must enable interrupts via
  3682. * ahc_intr_enable().
  3683. */
  3684. int
  3685. ahc_reset(struct ahc_softc *ahc, int reinit)
  3686. {
  3687. u_int sblkctl;
  3688. u_int sxfrctl1_a, sxfrctl1_b;
  3689. int error;
  3690. int wait;
  3691. /*
  3692. * Preserve the value of the SXFRCTL1 register for all channels.
  3693. * It contains settings that affect termination and we don't want
  3694. * to disturb the integrity of the bus.
  3695. */
  3696. ahc_pause(ahc);
  3697. sxfrctl1_b = 0;
  3698. if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
  3699. u_int sblkctl;
  3700. /*
  3701. * Save channel B's settings in case this chip
  3702. * is setup for TWIN channel operation.
  3703. */
  3704. sblkctl = ahc_inb(ahc, SBLKCTL);
  3705. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3706. sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
  3707. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3708. }
  3709. sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
  3710. ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
  3711. /*
  3712. * Ensure that the reset has finished. We delay 1000us
  3713. * prior to reading the register to make sure the chip
  3714. * has sufficiently completed its reset to handle register
  3715. * accesses.
  3716. */
  3717. wait = 1000;
  3718. do {
  3719. ahc_delay(1000);
  3720. } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
  3721. if (wait == 0) {
  3722. printf("%s: WARNING - Failed chip reset! "
  3723. "Trying to initialize anyway.\n", ahc_name(ahc));
  3724. }
  3725. ahc_outb(ahc, HCNTRL, ahc->pause);
  3726. /* Determine channel configuration */
  3727. sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
  3728. /* No Twin Channel PCI cards */
  3729. if ((ahc->chip & AHC_PCI) != 0)
  3730. sblkctl &= ~SELBUSB;
  3731. switch (sblkctl) {
  3732. case 0:
  3733. /* Single Narrow Channel */
  3734. break;
  3735. case 2:
  3736. /* Wide Channel */
  3737. ahc->features |= AHC_WIDE;
  3738. break;
  3739. case 8:
  3740. /* Twin Channel */
  3741. ahc->features |= AHC_TWIN;
  3742. break;
  3743. default:
  3744. printf(" Unsupported adapter type. Ignoring\n");
  3745. return(-1);
  3746. }
  3747. /*
  3748. * Reload sxfrctl1.
  3749. *
  3750. * We must always initialize STPWEN to 1 before we
  3751. * restore the saved values. STPWEN is initialized
  3752. * to a tri-state condition which can only be cleared
  3753. * by turning it on.
  3754. */
  3755. if ((ahc->features & AHC_TWIN) != 0) {
  3756. u_int sblkctl;
  3757. sblkctl = ahc_inb(ahc, SBLKCTL);
  3758. ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
  3759. ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
  3760. ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
  3761. }
  3762. ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
  3763. error = 0;
  3764. if (reinit != 0)
  3765. /*
  3766. * If a recovery action has forced a chip reset,
  3767. * re-initialize the chip to our liking.
  3768. */
  3769. error = ahc->bus_chip_init(ahc);
  3770. #ifdef AHC_DUMP_SEQ
  3771. else
  3772. ahc_dumpseq(ahc);
  3773. #endif
  3774. return (error);
  3775. }
  3776. /*
  3777. * Determine the number of SCBs available on the controller
  3778. */
  3779. int
  3780. ahc_probe_scbs(struct ahc_softc *ahc) {
  3781. int i;
  3782. for (i = 0; i < AHC_SCB_MAX; i++) {
  3783. ahc_outb(ahc, SCBPTR, i);
  3784. ahc_outb(ahc, SCB_BASE, i);
  3785. if (ahc_inb(ahc, SCB_BASE) != i)
  3786. break;
  3787. ahc_outb(ahc, SCBPTR, 0);
  3788. if (ahc_inb(ahc, SCB_BASE) != 0)
  3789. break;
  3790. }
  3791. return (i);
  3792. }
  3793. static void
  3794. ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  3795. {
  3796. dma_addr_t *baddr;
  3797. baddr = (dma_addr_t *)arg;
  3798. *baddr = segs->ds_addr;
  3799. }
  3800. static void
  3801. ahc_build_free_scb_list(struct ahc_softc *ahc)
  3802. {
  3803. int scbsize;
  3804. int i;
  3805. scbsize = 32;
  3806. if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
  3807. scbsize = 64;
  3808. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  3809. int j;
  3810. ahc_outb(ahc, SCBPTR, i);
  3811. /*
  3812. * Touch all SCB bytes to avoid parity errors
  3813. * should one of our debugging routines read
  3814. * an otherwise uninitiatlized byte.
  3815. */
  3816. for (j = 0; j < scbsize; j++)
  3817. ahc_outb(ahc, SCB_BASE+j, 0xFF);
  3818. /* Clear the control byte. */
  3819. ahc_outb(ahc, SCB_CONTROL, 0);
  3820. /* Set the next pointer */
  3821. if ((ahc->flags & AHC_PAGESCBS) != 0)
  3822. ahc_outb(ahc, SCB_NEXT, i+1);
  3823. else
  3824. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3825. /* Make the tag number, SCSIID, and lun invalid */
  3826. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  3827. ahc_outb(ahc, SCB_SCSIID, 0xFF);
  3828. ahc_outb(ahc, SCB_LUN, 0xFF);
  3829. }
  3830. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  3831. /* SCB 0 heads the free list. */
  3832. ahc_outb(ahc, FREE_SCBH, 0);
  3833. } else {
  3834. /* No free list. */
  3835. ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
  3836. }
  3837. /* Make sure that the last SCB terminates the free list */
  3838. ahc_outb(ahc, SCBPTR, i-1);
  3839. ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
  3840. }
  3841. static int
  3842. ahc_init_scbdata(struct ahc_softc *ahc)
  3843. {
  3844. struct scb_data *scb_data;
  3845. scb_data = ahc->scb_data;
  3846. SLIST_INIT(&scb_data->free_scbs);
  3847. SLIST_INIT(&scb_data->sg_maps);
  3848. /* Allocate SCB resources */
  3849. scb_data->scbarray =
  3850. (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
  3851. M_DEVBUF, M_NOWAIT);
  3852. if (scb_data->scbarray == NULL)
  3853. return (ENOMEM);
  3854. memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
  3855. /* Determine the number of hardware SCBs and initialize them */
  3856. scb_data->maxhscbs = ahc_probe_scbs(ahc);
  3857. if (ahc->scb_data->maxhscbs == 0) {
  3858. printf("%s: No SCB space found\n", ahc_name(ahc));
  3859. return (ENXIO);
  3860. }
  3861. /*
  3862. * Create our DMA tags. These tags define the kinds of device
  3863. * accessible memory allocations and memory mappings we will
  3864. * need to perform during normal operation.
  3865. *
  3866. * Unless we need to further restrict the allocation, we rely
  3867. * on the restrictions of the parent dmat, hence the common
  3868. * use of MAXADDR and MAXSIZE.
  3869. */
  3870. /* DMA tag for our hardware scb structures */
  3871. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3872. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3873. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3874. /*highaddr*/BUS_SPACE_MAXADDR,
  3875. /*filter*/NULL, /*filterarg*/NULL,
  3876. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3877. /*nsegments*/1,
  3878. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3879. /*flags*/0, &scb_data->hscb_dmat) != 0) {
  3880. goto error_exit;
  3881. }
  3882. scb_data->init_level++;
  3883. /* Allocation for our hscbs */
  3884. if (ahc_dmamem_alloc(ahc, scb_data->hscb_dmat,
  3885. (void **)&scb_data->hscbs,
  3886. BUS_DMA_NOWAIT, &scb_data->hscb_dmamap) != 0) {
  3887. goto error_exit;
  3888. }
  3889. scb_data->init_level++;
  3890. /* And permanently map them */
  3891. ahc_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
  3892. scb_data->hscbs,
  3893. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
  3894. ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
  3895. scb_data->init_level++;
  3896. /* DMA tag for our sense buffers */
  3897. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  3898. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3899. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3900. /*highaddr*/BUS_SPACE_MAXADDR,
  3901. /*filter*/NULL, /*filterarg*/NULL,
  3902. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3903. /*nsegments*/1,
  3904. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3905. /*flags*/0, &scb_data->sense_dmat) != 0) {
  3906. goto error_exit;
  3907. }
  3908. scb_data->init_level++;
  3909. /* Allocate them */
  3910. if (ahc_dmamem_alloc(ahc, scb_data->sense_dmat,
  3911. (void **)&scb_data->sense,
  3912. BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
  3913. goto error_exit;
  3914. }
  3915. scb_data->init_level++;
  3916. /* And permanently map them */
  3917. ahc_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
  3918. scb_data->sense,
  3919. AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
  3920. ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
  3921. scb_data->init_level++;
  3922. /* DMA tag for our S/G structures. We allocate in page sized chunks */
  3923. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
  3924. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  3925. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  3926. /*highaddr*/BUS_SPACE_MAXADDR,
  3927. /*filter*/NULL, /*filterarg*/NULL,
  3928. PAGE_SIZE, /*nsegments*/1,
  3929. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  3930. /*flags*/0, &scb_data->sg_dmat) != 0) {
  3931. goto error_exit;
  3932. }
  3933. scb_data->init_level++;
  3934. /* Perform initial CCB allocation */
  3935. memset(scb_data->hscbs, 0,
  3936. AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
  3937. ahc_alloc_scbs(ahc);
  3938. if (scb_data->numscbs == 0) {
  3939. printf("%s: ahc_init_scbdata - "
  3940. "Unable to allocate initial scbs\n",
  3941. ahc_name(ahc));
  3942. goto error_exit;
  3943. }
  3944. /*
  3945. * Reserve the next queued SCB.
  3946. */
  3947. ahc->next_queued_scb = ahc_get_scb(ahc);
  3948. /*
  3949. * Note that we were successfull
  3950. */
  3951. return (0);
  3952. error_exit:
  3953. return (ENOMEM);
  3954. }
  3955. static void
  3956. ahc_fini_scbdata(struct ahc_softc *ahc)
  3957. {
  3958. struct scb_data *scb_data;
  3959. scb_data = ahc->scb_data;
  3960. if (scb_data == NULL)
  3961. return;
  3962. switch (scb_data->init_level) {
  3963. default:
  3964. case 7:
  3965. {
  3966. struct sg_map_node *sg_map;
  3967. while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
  3968. SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
  3969. ahc_dmamap_unload(ahc, scb_data->sg_dmat,
  3970. sg_map->sg_dmamap);
  3971. ahc_dmamem_free(ahc, scb_data->sg_dmat,
  3972. sg_map->sg_vaddr,
  3973. sg_map->sg_dmamap);
  3974. free(sg_map, M_DEVBUF);
  3975. }
  3976. ahc_dma_tag_destroy(ahc, scb_data->sg_dmat);
  3977. }
  3978. case 6:
  3979. ahc_dmamap_unload(ahc, scb_data->sense_dmat,
  3980. scb_data->sense_dmamap);
  3981. case 5:
  3982. ahc_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
  3983. scb_data->sense_dmamap);
  3984. ahc_dmamap_destroy(ahc, scb_data->sense_dmat,
  3985. scb_data->sense_dmamap);
  3986. case 4:
  3987. ahc_dma_tag_destroy(ahc, scb_data->sense_dmat);
  3988. case 3:
  3989. ahc_dmamap_unload(ahc, scb_data->hscb_dmat,
  3990. scb_data->hscb_dmamap);
  3991. case 2:
  3992. ahc_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
  3993. scb_data->hscb_dmamap);
  3994. ahc_dmamap_destroy(ahc, scb_data->hscb_dmat,
  3995. scb_data->hscb_dmamap);
  3996. case 1:
  3997. ahc_dma_tag_destroy(ahc, scb_data->hscb_dmat);
  3998. break;
  3999. case 0:
  4000. break;
  4001. }
  4002. if (scb_data->scbarray != NULL)
  4003. free(scb_data->scbarray, M_DEVBUF);
  4004. }
  4005. void
  4006. ahc_alloc_scbs(struct ahc_softc *ahc)
  4007. {
  4008. struct scb_data *scb_data;
  4009. struct scb *next_scb;
  4010. struct sg_map_node *sg_map;
  4011. dma_addr_t physaddr;
  4012. struct ahc_dma_seg *segs;
  4013. int newcount;
  4014. int i;
  4015. scb_data = ahc->scb_data;
  4016. if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
  4017. /* Can't allocate any more */
  4018. return;
  4019. next_scb = &scb_data->scbarray[scb_data->numscbs];
  4020. sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
  4021. if (sg_map == NULL)
  4022. return;
  4023. /* Allocate S/G space for the next batch of SCBS */
  4024. if (ahc_dmamem_alloc(ahc, scb_data->sg_dmat,
  4025. (void **)&sg_map->sg_vaddr,
  4026. BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
  4027. free(sg_map, M_DEVBUF);
  4028. return;
  4029. }
  4030. SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
  4031. ahc_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
  4032. sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
  4033. &sg_map->sg_physaddr, /*flags*/0);
  4034. segs = sg_map->sg_vaddr;
  4035. physaddr = sg_map->sg_physaddr;
  4036. newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
  4037. newcount = min(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
  4038. for (i = 0; i < newcount; i++) {
  4039. struct scb_platform_data *pdata;
  4040. #ifndef __linux__
  4041. int error;
  4042. #endif
  4043. pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
  4044. M_DEVBUF, M_NOWAIT);
  4045. if (pdata == NULL)
  4046. break;
  4047. next_scb->platform_data = pdata;
  4048. next_scb->sg_map = sg_map;
  4049. next_scb->sg_list = segs;
  4050. /*
  4051. * The sequencer always starts with the second entry.
  4052. * The first entry is embedded in the scb.
  4053. */
  4054. next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
  4055. next_scb->ahc_softc = ahc;
  4056. next_scb->flags = SCB_FREE;
  4057. #ifndef __linux__
  4058. error = ahc_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
  4059. &next_scb->dmamap);
  4060. if (error != 0)
  4061. break;
  4062. #endif
  4063. next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
  4064. next_scb->hscb->tag = ahc->scb_data->numscbs;
  4065. SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
  4066. next_scb, links.sle);
  4067. segs += AHC_NSEG;
  4068. physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
  4069. next_scb++;
  4070. ahc->scb_data->numscbs++;
  4071. }
  4072. }
  4073. void
  4074. ahc_controller_info(struct ahc_softc *ahc, char *buf)
  4075. {
  4076. int len;
  4077. len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
  4078. buf += len;
  4079. if ((ahc->features & AHC_TWIN) != 0)
  4080. len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
  4081. "B SCSI Id=%d, primary %c, ",
  4082. ahc->our_id, ahc->our_id_b,
  4083. (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
  4084. else {
  4085. const char *speed;
  4086. const char *type;
  4087. speed = "";
  4088. if ((ahc->features & AHC_ULTRA) != 0) {
  4089. speed = "Ultra ";
  4090. } else if ((ahc->features & AHC_DT) != 0) {
  4091. speed = "Ultra160 ";
  4092. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  4093. speed = "Ultra2 ";
  4094. }
  4095. if ((ahc->features & AHC_WIDE) != 0) {
  4096. type = "Wide";
  4097. } else {
  4098. type = "Single";
  4099. }
  4100. len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
  4101. speed, type, ahc->channel, ahc->our_id);
  4102. }
  4103. buf += len;
  4104. if ((ahc->flags & AHC_PAGESCBS) != 0)
  4105. sprintf(buf, "%d/%d SCBs",
  4106. ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
  4107. else
  4108. sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
  4109. }
  4110. int
  4111. ahc_chip_init(struct ahc_softc *ahc)
  4112. {
  4113. int term;
  4114. int error;
  4115. u_int i;
  4116. u_int scsi_conf;
  4117. u_int scsiseq_template;
  4118. uint32_t physaddr;
  4119. ahc_outb(ahc, SEQ_FLAGS, 0);
  4120. ahc_outb(ahc, SEQ_FLAGS2, 0);
  4121. /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
  4122. if (ahc->features & AHC_TWIN) {
  4123. /*
  4124. * Setup Channel B first.
  4125. */
  4126. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
  4127. term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
  4128. ahc_outb(ahc, SCSIID, ahc->our_id_b);
  4129. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4130. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4131. |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
  4132. if ((ahc->features & AHC_ULTRA2) != 0)
  4133. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4134. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4135. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4136. /* Select Channel A */
  4137. ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
  4138. }
  4139. term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
  4140. if ((ahc->features & AHC_ULTRA2) != 0)
  4141. ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
  4142. else
  4143. ahc_outb(ahc, SCSIID, ahc->our_id);
  4144. scsi_conf = ahc_inb(ahc, SCSICONF);
  4145. ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
  4146. |term|ahc->seltime
  4147. |ENSTIMER|ACTNEGEN);
  4148. if ((ahc->features & AHC_ULTRA2) != 0)
  4149. ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
  4150. ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
  4151. ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
  4152. /* There are no untagged SCBs active yet. */
  4153. for (i = 0; i < 16; i++) {
  4154. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
  4155. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4156. int lun;
  4157. /*
  4158. * The SCB based BTT allows an entry per
  4159. * target and lun pair.
  4160. */
  4161. for (lun = 1; lun < AHC_NUM_LUNS; lun++)
  4162. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
  4163. }
  4164. }
  4165. /* All of our queues are empty */
  4166. for (i = 0; i < 256; i++)
  4167. ahc->qoutfifo[i] = SCB_LIST_NULL;
  4168. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
  4169. for (i = 0; i < 256; i++)
  4170. ahc->qinfifo[i] = SCB_LIST_NULL;
  4171. if ((ahc->features & AHC_MULTI_TID) != 0) {
  4172. ahc_outb(ahc, TARGID, 0);
  4173. ahc_outb(ahc, TARGID + 1, 0);
  4174. }
  4175. /*
  4176. * Tell the sequencer where it can find our arrays in memory.
  4177. */
  4178. physaddr = ahc->scb_data->hscb_busaddr;
  4179. ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
  4180. ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
  4181. ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
  4182. ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
  4183. physaddr = ahc->shared_data_busaddr;
  4184. ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
  4185. ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
  4186. ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
  4187. ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
  4188. /*
  4189. * Initialize the group code to command length table.
  4190. * This overrides the values in TARG_SCSIRATE, so only
  4191. * setup the table after we have processed that information.
  4192. */
  4193. ahc_outb(ahc, CMDSIZE_TABLE, 5);
  4194. ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
  4195. ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
  4196. ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
  4197. ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
  4198. ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
  4199. ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
  4200. ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
  4201. if ((ahc->features & AHC_HS_MAILBOX) != 0)
  4202. ahc_outb(ahc, HS_MAILBOX, 0);
  4203. /* Tell the sequencer of our initial queue positions */
  4204. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4205. ahc->tqinfifonext = 1;
  4206. ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
  4207. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  4208. }
  4209. ahc->qinfifonext = 0;
  4210. ahc->qoutfifonext = 0;
  4211. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4212. ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
  4213. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4214. ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
  4215. ahc_outb(ahc, SDSCB_QOFF, 0);
  4216. } else {
  4217. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4218. ahc_outb(ahc, QINPOS, ahc->qinfifonext);
  4219. ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
  4220. }
  4221. /* We don't have any waiting selections */
  4222. ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
  4223. /* Our disconnection list is empty too */
  4224. ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
  4225. /* Message out buffer starts empty */
  4226. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  4227. /*
  4228. * Setup the allowed SCSI Sequences based on operational mode.
  4229. * If we are a target, we'll enalbe select in operations once
  4230. * we've had a lun enabled.
  4231. */
  4232. scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
  4233. if ((ahc->flags & AHC_INITIATORROLE) != 0)
  4234. scsiseq_template |= ENRSELI;
  4235. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
  4236. /* Initialize our list of free SCBs. */
  4237. ahc_build_free_scb_list(ahc);
  4238. /*
  4239. * Tell the sequencer which SCB will be the next one it receives.
  4240. */
  4241. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4242. /*
  4243. * Load the Sequencer program and Enable the adapter
  4244. * in "fast" mode.
  4245. */
  4246. if (bootverbose)
  4247. printf("%s: Downloading Sequencer Program...",
  4248. ahc_name(ahc));
  4249. error = ahc_loadseq(ahc);
  4250. if (error != 0)
  4251. return (error);
  4252. if ((ahc->features & AHC_ULTRA2) != 0) {
  4253. int wait;
  4254. /*
  4255. * Wait for up to 500ms for our transceivers
  4256. * to settle. If the adapter does not have
  4257. * a cable attached, the transceivers may
  4258. * never settle, so don't complain if we
  4259. * fail here.
  4260. */
  4261. for (wait = 5000;
  4262. (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
  4263. wait--)
  4264. ahc_delay(100);
  4265. }
  4266. ahc_restart(ahc);
  4267. return (0);
  4268. }
  4269. /*
  4270. * Start the board, ready for normal operation
  4271. */
  4272. int
  4273. ahc_init(struct ahc_softc *ahc)
  4274. {
  4275. int max_targ;
  4276. u_int i;
  4277. u_int scsi_conf;
  4278. u_int ultraenb;
  4279. u_int discenable;
  4280. u_int tagenable;
  4281. size_t driver_data_size;
  4282. #ifdef AHC_DEBUG
  4283. if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
  4284. ahc->flags |= AHC_SEQUENCER_DEBUG;
  4285. #endif
  4286. #ifdef AHC_PRINT_SRAM
  4287. printf("Scratch Ram:");
  4288. for (i = 0x20; i < 0x5f; i++) {
  4289. if (((i % 8) == 0) && (i != 0)) {
  4290. printf ("\n ");
  4291. }
  4292. printf (" 0x%x", ahc_inb(ahc, i));
  4293. }
  4294. if ((ahc->features & AHC_MORE_SRAM) != 0) {
  4295. for (i = 0x70; i < 0x7f; i++) {
  4296. if (((i % 8) == 0) && (i != 0)) {
  4297. printf ("\n ");
  4298. }
  4299. printf (" 0x%x", ahc_inb(ahc, i));
  4300. }
  4301. }
  4302. printf ("\n");
  4303. /*
  4304. * Reading uninitialized scratch ram may
  4305. * generate parity errors.
  4306. */
  4307. ahc_outb(ahc, CLRINT, CLRPARERR);
  4308. ahc_outb(ahc, CLRINT, CLRBRKADRINT);
  4309. #endif
  4310. max_targ = 15;
  4311. /*
  4312. * Assume we have a board at this stage and it has been reset.
  4313. */
  4314. if ((ahc->flags & AHC_USEDEFAULTS) != 0)
  4315. ahc->our_id = ahc->our_id_b = 7;
  4316. /*
  4317. * Default to allowing initiator operations.
  4318. */
  4319. ahc->flags |= AHC_INITIATORROLE;
  4320. /*
  4321. * Only allow target mode features if this unit has them enabled.
  4322. */
  4323. if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
  4324. ahc->features &= ~AHC_TARGETMODE;
  4325. #ifndef __linux__
  4326. /* DMA tag for mapping buffers into device visible space. */
  4327. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4328. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4329. /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
  4330. ? (dma_addr_t)0x7FFFFFFFFFULL
  4331. : BUS_SPACE_MAXADDR_32BIT,
  4332. /*highaddr*/BUS_SPACE_MAXADDR,
  4333. /*filter*/NULL, /*filterarg*/NULL,
  4334. /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
  4335. /*nsegments*/AHC_NSEG,
  4336. /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
  4337. /*flags*/BUS_DMA_ALLOCNOW,
  4338. &ahc->buffer_dmat) != 0) {
  4339. return (ENOMEM);
  4340. }
  4341. #endif
  4342. ahc->init_level++;
  4343. /*
  4344. * DMA tag for our command fifos and other data in system memory
  4345. * the card's sequencer must be able to access. For initiator
  4346. * roles, we need to allocate space for the qinfifo and qoutfifo.
  4347. * The qinfifo and qoutfifo are composed of 256 1 byte elements.
  4348. * When providing for the target mode role, we must additionally
  4349. * provide space for the incoming target command fifo and an extra
  4350. * byte to deal with a dma bug in some chip versions.
  4351. */
  4352. driver_data_size = 2 * 256 * sizeof(uint8_t);
  4353. if ((ahc->features & AHC_TARGETMODE) != 0)
  4354. driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
  4355. + /*DMA WideOdd Bug Buffer*/1;
  4356. if (ahc_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
  4357. /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
  4358. /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
  4359. /*highaddr*/BUS_SPACE_MAXADDR,
  4360. /*filter*/NULL, /*filterarg*/NULL,
  4361. driver_data_size,
  4362. /*nsegments*/1,
  4363. /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
  4364. /*flags*/0, &ahc->shared_data_dmat) != 0) {
  4365. return (ENOMEM);
  4366. }
  4367. ahc->init_level++;
  4368. /* Allocation of driver data */
  4369. if (ahc_dmamem_alloc(ahc, ahc->shared_data_dmat,
  4370. (void **)&ahc->qoutfifo,
  4371. BUS_DMA_NOWAIT, &ahc->shared_data_dmamap) != 0) {
  4372. return (ENOMEM);
  4373. }
  4374. ahc->init_level++;
  4375. /* And permanently map it in */
  4376. ahc_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
  4377. ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
  4378. &ahc->shared_data_busaddr, /*flags*/0);
  4379. if ((ahc->features & AHC_TARGETMODE) != 0) {
  4380. ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
  4381. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
  4382. ahc->dma_bug_buf = ahc->shared_data_busaddr
  4383. + driver_data_size - 1;
  4384. /* All target command blocks start out invalid. */
  4385. for (i = 0; i < AHC_TMODE_CMDS; i++)
  4386. ahc->targetcmds[i].cmd_valid = 0;
  4387. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
  4388. ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[256];
  4389. }
  4390. ahc->qinfifo = &ahc->qoutfifo[256];
  4391. ahc->init_level++;
  4392. /* Allocate SCB data now that buffer_dmat is initialized */
  4393. if (ahc->scb_data->maxhscbs == 0)
  4394. if (ahc_init_scbdata(ahc) != 0)
  4395. return (ENOMEM);
  4396. /*
  4397. * Allocate a tstate to house information for our
  4398. * initiator presence on the bus as well as the user
  4399. * data for any target mode initiator.
  4400. */
  4401. if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
  4402. printf("%s: unable to allocate ahc_tmode_tstate. "
  4403. "Failing attach\n", ahc_name(ahc));
  4404. return (ENOMEM);
  4405. }
  4406. if ((ahc->features & AHC_TWIN) != 0) {
  4407. if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
  4408. printf("%s: unable to allocate ahc_tmode_tstate. "
  4409. "Failing attach\n", ahc_name(ahc));
  4410. return (ENOMEM);
  4411. }
  4412. }
  4413. if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
  4414. ahc->flags |= AHC_PAGESCBS;
  4415. } else {
  4416. ahc->flags &= ~AHC_PAGESCBS;
  4417. }
  4418. #ifdef AHC_DEBUG
  4419. if (ahc_debug & AHC_SHOW_MISC) {
  4420. printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
  4421. "ahc_dma %u bytes\n",
  4422. ahc_name(ahc),
  4423. (u_int)sizeof(struct hardware_scb),
  4424. (u_int)sizeof(struct scb),
  4425. (u_int)sizeof(struct ahc_dma_seg));
  4426. }
  4427. #endif /* AHC_DEBUG */
  4428. /*
  4429. * Look at the information that board initialization or
  4430. * the board bios has left us.
  4431. */
  4432. if (ahc->features & AHC_TWIN) {
  4433. scsi_conf = ahc_inb(ahc, SCSICONF + 1);
  4434. if ((scsi_conf & RESET_SCSI) != 0
  4435. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4436. ahc->flags |= AHC_RESET_BUS_B;
  4437. }
  4438. scsi_conf = ahc_inb(ahc, SCSICONF);
  4439. if ((scsi_conf & RESET_SCSI) != 0
  4440. && (ahc->flags & AHC_INITIATORROLE) != 0)
  4441. ahc->flags |= AHC_RESET_BUS_A;
  4442. ultraenb = 0;
  4443. tagenable = ALL_TARGETS_MASK;
  4444. /* Grab the disconnection disable table and invert it for our needs */
  4445. if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
  4446. printf("%s: Host Adapter Bios disabled. Using default SCSI "
  4447. "device parameters\n", ahc_name(ahc));
  4448. ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
  4449. AHC_TERM_ENB_A|AHC_TERM_ENB_B;
  4450. discenable = ALL_TARGETS_MASK;
  4451. if ((ahc->features & AHC_ULTRA) != 0)
  4452. ultraenb = ALL_TARGETS_MASK;
  4453. } else {
  4454. discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
  4455. | ahc_inb(ahc, DISC_DSB));
  4456. if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
  4457. ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
  4458. | ahc_inb(ahc, ULTRA_ENB);
  4459. }
  4460. if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
  4461. max_targ = 7;
  4462. for (i = 0; i <= max_targ; i++) {
  4463. struct ahc_initiator_tinfo *tinfo;
  4464. struct ahc_tmode_tstate *tstate;
  4465. u_int our_id;
  4466. u_int target_id;
  4467. char channel;
  4468. channel = 'A';
  4469. our_id = ahc->our_id;
  4470. target_id = i;
  4471. if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
  4472. channel = 'B';
  4473. our_id = ahc->our_id_b;
  4474. target_id = i % 8;
  4475. }
  4476. tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
  4477. target_id, &tstate);
  4478. /* Default to async narrow across the board */
  4479. memset(tinfo, 0, sizeof(*tinfo));
  4480. if (ahc->flags & AHC_USEDEFAULTS) {
  4481. if ((ahc->features & AHC_WIDE) != 0)
  4482. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4483. /*
  4484. * These will be truncated when we determine the
  4485. * connection type we have with the target.
  4486. */
  4487. tinfo->user.period = ahc_syncrates->period;
  4488. tinfo->user.offset = MAX_OFFSET;
  4489. } else {
  4490. u_int scsirate;
  4491. uint16_t mask;
  4492. /* Take the settings leftover in scratch RAM. */
  4493. scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
  4494. mask = (0x01 << i);
  4495. if ((ahc->features & AHC_ULTRA2) != 0) {
  4496. u_int offset;
  4497. u_int maxsync;
  4498. if ((scsirate & SOFS) == 0x0F) {
  4499. /*
  4500. * Haven't negotiated yet,
  4501. * so the format is different.
  4502. */
  4503. scsirate = (scsirate & SXFR) >> 4
  4504. | (ultraenb & mask)
  4505. ? 0x08 : 0x0
  4506. | (scsirate & WIDEXFER);
  4507. offset = MAX_OFFSET_ULTRA2;
  4508. } else
  4509. offset = ahc_inb(ahc, TARG_OFFSET + i);
  4510. if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
  4511. /* Set to the lowest sync rate, 5MHz */
  4512. scsirate |= 0x1c;
  4513. maxsync = AHC_SYNCRATE_ULTRA2;
  4514. if ((ahc->features & AHC_DT) != 0)
  4515. maxsync = AHC_SYNCRATE_DT;
  4516. tinfo->user.period =
  4517. ahc_find_period(ahc, scsirate, maxsync);
  4518. if (offset == 0)
  4519. tinfo->user.period = 0;
  4520. else
  4521. tinfo->user.offset = MAX_OFFSET;
  4522. if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
  4523. && (ahc->features & AHC_DT) != 0)
  4524. tinfo->user.ppr_options =
  4525. MSG_EXT_PPR_DT_REQ;
  4526. } else if ((scsirate & SOFS) != 0) {
  4527. if ((scsirate & SXFR) == 0x40
  4528. && (ultraenb & mask) != 0) {
  4529. /* Treat 10MHz as a non-ultra speed */
  4530. scsirate &= ~SXFR;
  4531. ultraenb &= ~mask;
  4532. }
  4533. tinfo->user.period =
  4534. ahc_find_period(ahc, scsirate,
  4535. (ultraenb & mask)
  4536. ? AHC_SYNCRATE_ULTRA
  4537. : AHC_SYNCRATE_FAST);
  4538. if (tinfo->user.period != 0)
  4539. tinfo->user.offset = MAX_OFFSET;
  4540. }
  4541. if (tinfo->user.period == 0)
  4542. tinfo->user.offset = 0;
  4543. if ((scsirate & WIDEXFER) != 0
  4544. && (ahc->features & AHC_WIDE) != 0)
  4545. tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
  4546. tinfo->user.protocol_version = 4;
  4547. if ((ahc->features & AHC_DT) != 0)
  4548. tinfo->user.transport_version = 3;
  4549. else
  4550. tinfo->user.transport_version = 2;
  4551. tinfo->goal.protocol_version = 2;
  4552. tinfo->goal.transport_version = 2;
  4553. tinfo->curr.protocol_version = 2;
  4554. tinfo->curr.transport_version = 2;
  4555. }
  4556. tstate->ultraenb = 0;
  4557. }
  4558. ahc->user_discenable = discenable;
  4559. ahc->user_tagenable = tagenable;
  4560. return (ahc->bus_chip_init(ahc));
  4561. }
  4562. void
  4563. ahc_intr_enable(struct ahc_softc *ahc, int enable)
  4564. {
  4565. u_int hcntrl;
  4566. hcntrl = ahc_inb(ahc, HCNTRL);
  4567. hcntrl &= ~INTEN;
  4568. ahc->pause &= ~INTEN;
  4569. ahc->unpause &= ~INTEN;
  4570. if (enable) {
  4571. hcntrl |= INTEN;
  4572. ahc->pause |= INTEN;
  4573. ahc->unpause |= INTEN;
  4574. }
  4575. ahc_outb(ahc, HCNTRL, hcntrl);
  4576. }
  4577. /*
  4578. * Ensure that the card is paused in a location
  4579. * outside of all critical sections and that all
  4580. * pending work is completed prior to returning.
  4581. * This routine should only be called from outside
  4582. * an interrupt context.
  4583. */
  4584. void
  4585. ahc_pause_and_flushwork(struct ahc_softc *ahc)
  4586. {
  4587. int intstat;
  4588. int maxloops;
  4589. int paused;
  4590. maxloops = 1000;
  4591. ahc->flags |= AHC_ALL_INTERRUPTS;
  4592. paused = FALSE;
  4593. do {
  4594. if (paused) {
  4595. ahc_unpause(ahc);
  4596. /*
  4597. * Give the sequencer some time to service
  4598. * any active selections.
  4599. */
  4600. ahc_delay(500);
  4601. }
  4602. ahc_intr(ahc);
  4603. ahc_pause(ahc);
  4604. paused = TRUE;
  4605. ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
  4606. intstat = ahc_inb(ahc, INTSTAT);
  4607. if ((intstat & INT_PEND) == 0) {
  4608. ahc_clear_critical_section(ahc);
  4609. intstat = ahc_inb(ahc, INTSTAT);
  4610. }
  4611. } while (--maxloops
  4612. && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
  4613. && ((intstat & INT_PEND) != 0
  4614. || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
  4615. if (maxloops == 0) {
  4616. printf("Infinite interrupt loop, INTSTAT = %x",
  4617. ahc_inb(ahc, INTSTAT));
  4618. }
  4619. ahc_platform_flushwork(ahc);
  4620. ahc->flags &= ~AHC_ALL_INTERRUPTS;
  4621. }
  4622. #ifdef CONFIG_PM
  4623. int
  4624. ahc_suspend(struct ahc_softc *ahc)
  4625. {
  4626. ahc_pause_and_flushwork(ahc);
  4627. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  4628. ahc_unpause(ahc);
  4629. return (EBUSY);
  4630. }
  4631. #ifdef AHC_TARGET_MODE
  4632. /*
  4633. * XXX What about ATIOs that have not yet been serviced?
  4634. * Perhaps we should just refuse to be suspended if we
  4635. * are acting in a target role.
  4636. */
  4637. if (ahc->pending_device != NULL) {
  4638. ahc_unpause(ahc);
  4639. return (EBUSY);
  4640. }
  4641. #endif
  4642. ahc_shutdown(ahc);
  4643. return (0);
  4644. }
  4645. int
  4646. ahc_resume(struct ahc_softc *ahc)
  4647. {
  4648. ahc_reset(ahc, /*reinit*/TRUE);
  4649. ahc_intr_enable(ahc, TRUE);
  4650. ahc_restart(ahc);
  4651. return (0);
  4652. }
  4653. #endif
  4654. /************************** Busy Target Table *********************************/
  4655. /*
  4656. * Return the untagged transaction id for a given target/channel lun.
  4657. * Optionally, clear the entry.
  4658. */
  4659. u_int
  4660. ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
  4661. {
  4662. u_int scbid;
  4663. u_int target_offset;
  4664. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4665. u_int saved_scbptr;
  4666. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4667. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4668. scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
  4669. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4670. } else {
  4671. target_offset = TCL_TARGET_OFFSET(tcl);
  4672. scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
  4673. }
  4674. return (scbid);
  4675. }
  4676. void
  4677. ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
  4678. {
  4679. u_int target_offset;
  4680. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4681. u_int saved_scbptr;
  4682. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4683. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4684. ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
  4685. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4686. } else {
  4687. target_offset = TCL_TARGET_OFFSET(tcl);
  4688. ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
  4689. }
  4690. }
  4691. void
  4692. ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
  4693. {
  4694. u_int target_offset;
  4695. if ((ahc->flags & AHC_SCB_BTT) != 0) {
  4696. u_int saved_scbptr;
  4697. saved_scbptr = ahc_inb(ahc, SCBPTR);
  4698. ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
  4699. ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
  4700. ahc_outb(ahc, SCBPTR, saved_scbptr);
  4701. } else {
  4702. target_offset = TCL_TARGET_OFFSET(tcl);
  4703. ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
  4704. }
  4705. }
  4706. /************************** SCB and SCB queue management **********************/
  4707. int
  4708. ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
  4709. char channel, int lun, u_int tag, role_t role)
  4710. {
  4711. int targ = SCB_GET_TARGET(ahc, scb);
  4712. char chan = SCB_GET_CHANNEL(ahc, scb);
  4713. int slun = SCB_GET_LUN(scb);
  4714. int match;
  4715. match = ((chan == channel) || (channel == ALL_CHANNELS));
  4716. if (match != 0)
  4717. match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
  4718. if (match != 0)
  4719. match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
  4720. if (match != 0) {
  4721. #ifdef AHC_TARGET_MODE
  4722. int group;
  4723. group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
  4724. if (role == ROLE_INITIATOR) {
  4725. match = (group != XPT_FC_GROUP_TMODE)
  4726. && ((tag == scb->hscb->tag)
  4727. || (tag == SCB_LIST_NULL));
  4728. } else if (role == ROLE_TARGET) {
  4729. match = (group == XPT_FC_GROUP_TMODE)
  4730. && ((tag == scb->io_ctx->csio.tag_id)
  4731. || (tag == SCB_LIST_NULL));
  4732. }
  4733. #else /* !AHC_TARGET_MODE */
  4734. match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
  4735. #endif /* AHC_TARGET_MODE */
  4736. }
  4737. return match;
  4738. }
  4739. void
  4740. ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
  4741. {
  4742. int target;
  4743. char channel;
  4744. int lun;
  4745. target = SCB_GET_TARGET(ahc, scb);
  4746. lun = SCB_GET_LUN(scb);
  4747. channel = SCB_GET_CHANNEL(ahc, scb);
  4748. ahc_search_qinfifo(ahc, target, channel, lun,
  4749. /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
  4750. CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  4751. ahc_platform_freeze_devq(ahc, scb);
  4752. }
  4753. void
  4754. ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
  4755. {
  4756. struct scb *prev_scb;
  4757. prev_scb = NULL;
  4758. if (ahc_qinfifo_count(ahc) != 0) {
  4759. u_int prev_tag;
  4760. uint8_t prev_pos;
  4761. prev_pos = ahc->qinfifonext - 1;
  4762. prev_tag = ahc->qinfifo[prev_pos];
  4763. prev_scb = ahc_lookup_scb(ahc, prev_tag);
  4764. }
  4765. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4766. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4767. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4768. } else {
  4769. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4770. }
  4771. }
  4772. static void
  4773. ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
  4774. struct scb *scb)
  4775. {
  4776. if (prev_scb == NULL) {
  4777. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4778. } else {
  4779. prev_scb->hscb->next = scb->hscb->tag;
  4780. ahc_sync_scb(ahc, prev_scb,
  4781. BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4782. }
  4783. ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
  4784. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4785. ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  4786. }
  4787. static int
  4788. ahc_qinfifo_count(struct ahc_softc *ahc)
  4789. {
  4790. uint8_t qinpos;
  4791. uint8_t diff;
  4792. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4793. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  4794. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  4795. } else
  4796. qinpos = ahc_inb(ahc, QINPOS);
  4797. diff = ahc->qinfifonext - qinpos;
  4798. return (diff);
  4799. }
  4800. int
  4801. ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
  4802. int lun, u_int tag, role_t role, uint32_t status,
  4803. ahc_search_action action)
  4804. {
  4805. struct scb *scb;
  4806. struct scb *prev_scb;
  4807. uint8_t qinstart;
  4808. uint8_t qinpos;
  4809. uint8_t qintail;
  4810. uint8_t next;
  4811. uint8_t prev;
  4812. uint8_t curscbptr;
  4813. int found;
  4814. int have_qregs;
  4815. qintail = ahc->qinfifonext;
  4816. have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
  4817. if (have_qregs) {
  4818. qinstart = ahc_inb(ahc, SNSCB_QOFF);
  4819. ahc_outb(ahc, SNSCB_QOFF, qinstart);
  4820. } else
  4821. qinstart = ahc_inb(ahc, QINPOS);
  4822. qinpos = qinstart;
  4823. found = 0;
  4824. prev_scb = NULL;
  4825. if (action == SEARCH_COMPLETE) {
  4826. /*
  4827. * Don't attempt to run any queued untagged transactions
  4828. * until we are done with the abort process.
  4829. */
  4830. ahc_freeze_untagged_queues(ahc);
  4831. }
  4832. /*
  4833. * Start with an empty queue. Entries that are not chosen
  4834. * for removal will be re-added to the queue as we go.
  4835. */
  4836. ahc->qinfifonext = qinpos;
  4837. ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
  4838. while (qinpos != qintail) {
  4839. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
  4840. if (scb == NULL) {
  4841. printf("qinpos = %d, SCB index = %d\n",
  4842. qinpos, ahc->qinfifo[qinpos]);
  4843. panic("Loop 1\n");
  4844. }
  4845. if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
  4846. /*
  4847. * We found an scb that needs to be acted on.
  4848. */
  4849. found++;
  4850. switch (action) {
  4851. case SEARCH_COMPLETE:
  4852. {
  4853. cam_status ostat;
  4854. cam_status cstat;
  4855. ostat = ahc_get_transaction_status(scb);
  4856. if (ostat == CAM_REQ_INPROG)
  4857. ahc_set_transaction_status(scb, status);
  4858. cstat = ahc_get_transaction_status(scb);
  4859. if (cstat != CAM_REQ_CMP)
  4860. ahc_freeze_scb(scb);
  4861. if ((scb->flags & SCB_ACTIVE) == 0)
  4862. printf("Inactive SCB in qinfifo\n");
  4863. ahc_done(ahc, scb);
  4864. /* FALLTHROUGH */
  4865. }
  4866. case SEARCH_REMOVE:
  4867. break;
  4868. case SEARCH_COUNT:
  4869. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4870. prev_scb = scb;
  4871. break;
  4872. }
  4873. } else {
  4874. ahc_qinfifo_requeue(ahc, prev_scb, scb);
  4875. prev_scb = scb;
  4876. }
  4877. qinpos++;
  4878. }
  4879. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  4880. ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
  4881. } else {
  4882. ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
  4883. }
  4884. if (action != SEARCH_COUNT
  4885. && (found != 0)
  4886. && (qinstart != ahc->qinfifonext)) {
  4887. /*
  4888. * The sequencer may be in the process of dmaing
  4889. * down the SCB at the beginning of the queue.
  4890. * This could be problematic if either the first,
  4891. * or the second SCB is removed from the queue
  4892. * (the first SCB includes a pointer to the "next"
  4893. * SCB to dma). If we have removed any entries, swap
  4894. * the first element in the queue with the next HSCB
  4895. * so the sequencer will notice that NEXT_QUEUED_SCB
  4896. * has changed during its dma attempt and will retry
  4897. * the DMA.
  4898. */
  4899. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
  4900. if (scb == NULL) {
  4901. printf("found = %d, qinstart = %d, qinfifionext = %d\n",
  4902. found, qinstart, ahc->qinfifonext);
  4903. panic("First/Second Qinfifo fixup\n");
  4904. }
  4905. /*
  4906. * ahc_swap_with_next_hscb forces our next pointer to
  4907. * point to the reserved SCB for future commands. Save
  4908. * and restore our original next pointer to maintain
  4909. * queue integrity.
  4910. */
  4911. next = scb->hscb->next;
  4912. ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
  4913. ahc_swap_with_next_hscb(ahc, scb);
  4914. scb->hscb->next = next;
  4915. ahc->qinfifo[qinstart] = scb->hscb->tag;
  4916. /* Tell the card about the new head of the qinfifo. */
  4917. ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
  4918. /* Fixup the tail "next" pointer. */
  4919. qintail = ahc->qinfifonext - 1;
  4920. scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
  4921. scb->hscb->next = ahc->next_queued_scb->hscb->tag;
  4922. }
  4923. /*
  4924. * Search waiting for selection list.
  4925. */
  4926. curscbptr = ahc_inb(ahc, SCBPTR);
  4927. next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
  4928. prev = SCB_LIST_NULL;
  4929. while (next != SCB_LIST_NULL) {
  4930. uint8_t scb_index;
  4931. ahc_outb(ahc, SCBPTR, next);
  4932. scb_index = ahc_inb(ahc, SCB_TAG);
  4933. if (scb_index >= ahc->scb_data->numscbs) {
  4934. printf("Waiting List inconsistency. "
  4935. "SCB index == %d, yet numscbs == %d.",
  4936. scb_index, ahc->scb_data->numscbs);
  4937. ahc_dump_card_state(ahc);
  4938. panic("for safety");
  4939. }
  4940. scb = ahc_lookup_scb(ahc, scb_index);
  4941. if (scb == NULL) {
  4942. printf("scb_index = %d, next = %d\n",
  4943. scb_index, next);
  4944. panic("Waiting List traversal\n");
  4945. }
  4946. if (ahc_match_scb(ahc, scb, target, channel,
  4947. lun, SCB_LIST_NULL, role)) {
  4948. /*
  4949. * We found an scb that needs to be acted on.
  4950. */
  4951. found++;
  4952. switch (action) {
  4953. case SEARCH_COMPLETE:
  4954. {
  4955. cam_status ostat;
  4956. cam_status cstat;
  4957. ostat = ahc_get_transaction_status(scb);
  4958. if (ostat == CAM_REQ_INPROG)
  4959. ahc_set_transaction_status(scb,
  4960. status);
  4961. cstat = ahc_get_transaction_status(scb);
  4962. if (cstat != CAM_REQ_CMP)
  4963. ahc_freeze_scb(scb);
  4964. if ((scb->flags & SCB_ACTIVE) == 0)
  4965. printf("Inactive SCB in Waiting List\n");
  4966. ahc_done(ahc, scb);
  4967. /* FALLTHROUGH */
  4968. }
  4969. case SEARCH_REMOVE:
  4970. next = ahc_rem_wscb(ahc, next, prev);
  4971. break;
  4972. case SEARCH_COUNT:
  4973. prev = next;
  4974. next = ahc_inb(ahc, SCB_NEXT);
  4975. break;
  4976. }
  4977. } else {
  4978. prev = next;
  4979. next = ahc_inb(ahc, SCB_NEXT);
  4980. }
  4981. }
  4982. ahc_outb(ahc, SCBPTR, curscbptr);
  4983. found += ahc_search_untagged_queues(ahc, /*ahc_io_ctx_t*/NULL, target,
  4984. channel, lun, status, action);
  4985. if (action == SEARCH_COMPLETE)
  4986. ahc_release_untagged_queues(ahc);
  4987. return (found);
  4988. }
  4989. int
  4990. ahc_search_untagged_queues(struct ahc_softc *ahc, ahc_io_ctx_t ctx,
  4991. int target, char channel, int lun, uint32_t status,
  4992. ahc_search_action action)
  4993. {
  4994. struct scb *scb;
  4995. int maxtarget;
  4996. int found;
  4997. int i;
  4998. if (action == SEARCH_COMPLETE) {
  4999. /*
  5000. * Don't attempt to run any queued untagged transactions
  5001. * until we are done with the abort process.
  5002. */
  5003. ahc_freeze_untagged_queues(ahc);
  5004. }
  5005. found = 0;
  5006. i = 0;
  5007. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  5008. maxtarget = 16;
  5009. if (target != CAM_TARGET_WILDCARD) {
  5010. i = target;
  5011. if (channel == 'B')
  5012. i += 8;
  5013. maxtarget = i + 1;
  5014. }
  5015. } else {
  5016. maxtarget = 0;
  5017. }
  5018. for (; i < maxtarget; i++) {
  5019. struct scb_tailq *untagged_q;
  5020. struct scb *next_scb;
  5021. untagged_q = &(ahc->untagged_queues[i]);
  5022. next_scb = TAILQ_FIRST(untagged_q);
  5023. while (next_scb != NULL) {
  5024. scb = next_scb;
  5025. next_scb = TAILQ_NEXT(scb, links.tqe);
  5026. /*
  5027. * The head of the list may be the currently
  5028. * active untagged command for a device.
  5029. * We're only searching for commands that
  5030. * have not been started. A transaction
  5031. * marked active but still in the qinfifo
  5032. * is removed by the qinfifo scanning code
  5033. * above.
  5034. */
  5035. if ((scb->flags & SCB_ACTIVE) != 0)
  5036. continue;
  5037. if (ahc_match_scb(ahc, scb, target, channel, lun,
  5038. SCB_LIST_NULL, ROLE_INITIATOR) == 0
  5039. || (ctx != NULL && ctx != scb->io_ctx))
  5040. continue;
  5041. /*
  5042. * We found an scb that needs to be acted on.
  5043. */
  5044. found++;
  5045. switch (action) {
  5046. case SEARCH_COMPLETE:
  5047. {
  5048. cam_status ostat;
  5049. cam_status cstat;
  5050. ostat = ahc_get_transaction_status(scb);
  5051. if (ostat == CAM_REQ_INPROG)
  5052. ahc_set_transaction_status(scb, status);
  5053. cstat = ahc_get_transaction_status(scb);
  5054. if (cstat != CAM_REQ_CMP)
  5055. ahc_freeze_scb(scb);
  5056. if ((scb->flags & SCB_ACTIVE) == 0)
  5057. printf("Inactive SCB in untaggedQ\n");
  5058. ahc_done(ahc, scb);
  5059. break;
  5060. }
  5061. case SEARCH_REMOVE:
  5062. scb->flags &= ~SCB_UNTAGGEDQ;
  5063. TAILQ_REMOVE(untagged_q, scb, links.tqe);
  5064. break;
  5065. case SEARCH_COUNT:
  5066. break;
  5067. }
  5068. }
  5069. }
  5070. if (action == SEARCH_COMPLETE)
  5071. ahc_release_untagged_queues(ahc);
  5072. return (found);
  5073. }
  5074. int
  5075. ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
  5076. int lun, u_int tag, int stop_on_first, int remove,
  5077. int save_state)
  5078. {
  5079. struct scb *scbp;
  5080. u_int next;
  5081. u_int prev;
  5082. u_int count;
  5083. u_int active_scb;
  5084. count = 0;
  5085. next = ahc_inb(ahc, DISCONNECTED_SCBH);
  5086. prev = SCB_LIST_NULL;
  5087. if (save_state) {
  5088. /* restore this when we're done */
  5089. active_scb = ahc_inb(ahc, SCBPTR);
  5090. } else
  5091. /* Silence compiler */
  5092. active_scb = SCB_LIST_NULL;
  5093. while (next != SCB_LIST_NULL) {
  5094. u_int scb_index;
  5095. ahc_outb(ahc, SCBPTR, next);
  5096. scb_index = ahc_inb(ahc, SCB_TAG);
  5097. if (scb_index >= ahc->scb_data->numscbs) {
  5098. printf("Disconnected List inconsistency. "
  5099. "SCB index == %d, yet numscbs == %d.",
  5100. scb_index, ahc->scb_data->numscbs);
  5101. ahc_dump_card_state(ahc);
  5102. panic("for safety");
  5103. }
  5104. if (next == prev) {
  5105. panic("Disconnected List Loop. "
  5106. "cur SCBPTR == %x, prev SCBPTR == %x.",
  5107. next, prev);
  5108. }
  5109. scbp = ahc_lookup_scb(ahc, scb_index);
  5110. if (ahc_match_scb(ahc, scbp, target, channel, lun,
  5111. tag, ROLE_INITIATOR)) {
  5112. count++;
  5113. if (remove) {
  5114. next =
  5115. ahc_rem_scb_from_disc_list(ahc, prev, next);
  5116. } else {
  5117. prev = next;
  5118. next = ahc_inb(ahc, SCB_NEXT);
  5119. }
  5120. if (stop_on_first)
  5121. break;
  5122. } else {
  5123. prev = next;
  5124. next = ahc_inb(ahc, SCB_NEXT);
  5125. }
  5126. }
  5127. if (save_state)
  5128. ahc_outb(ahc, SCBPTR, active_scb);
  5129. return (count);
  5130. }
  5131. /*
  5132. * Remove an SCB from the on chip list of disconnected transactions.
  5133. * This is empty/unused if we are not performing SCB paging.
  5134. */
  5135. static u_int
  5136. ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
  5137. {
  5138. u_int next;
  5139. ahc_outb(ahc, SCBPTR, scbptr);
  5140. next = ahc_inb(ahc, SCB_NEXT);
  5141. ahc_outb(ahc, SCB_CONTROL, 0);
  5142. ahc_add_curscb_to_free_list(ahc);
  5143. if (prev != SCB_LIST_NULL) {
  5144. ahc_outb(ahc, SCBPTR, prev);
  5145. ahc_outb(ahc, SCB_NEXT, next);
  5146. } else
  5147. ahc_outb(ahc, DISCONNECTED_SCBH, next);
  5148. return (next);
  5149. }
  5150. /*
  5151. * Add the SCB as selected by SCBPTR onto the on chip list of
  5152. * free hardware SCBs. This list is empty/unused if we are not
  5153. * performing SCB paging.
  5154. */
  5155. static void
  5156. ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
  5157. {
  5158. /*
  5159. * Invalidate the tag so that our abort
  5160. * routines don't think it's active.
  5161. */
  5162. ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
  5163. if ((ahc->flags & AHC_PAGESCBS) != 0) {
  5164. ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
  5165. ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
  5166. }
  5167. }
  5168. /*
  5169. * Manipulate the waiting for selection list and return the
  5170. * scb that follows the one that we remove.
  5171. */
  5172. static u_int
  5173. ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
  5174. {
  5175. u_int curscb, next;
  5176. /*
  5177. * Select the SCB we want to abort and
  5178. * pull the next pointer out of it.
  5179. */
  5180. curscb = ahc_inb(ahc, SCBPTR);
  5181. ahc_outb(ahc, SCBPTR, scbpos);
  5182. next = ahc_inb(ahc, SCB_NEXT);
  5183. /* Clear the necessary fields */
  5184. ahc_outb(ahc, SCB_CONTROL, 0);
  5185. ahc_add_curscb_to_free_list(ahc);
  5186. /* update the waiting list */
  5187. if (prev == SCB_LIST_NULL) {
  5188. /* First in the list */
  5189. ahc_outb(ahc, WAITING_SCBH, next);
  5190. /*
  5191. * Ensure we aren't attempting to perform
  5192. * selection for this entry.
  5193. */
  5194. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  5195. } else {
  5196. /*
  5197. * Select the scb that pointed to us
  5198. * and update its next pointer.
  5199. */
  5200. ahc_outb(ahc, SCBPTR, prev);
  5201. ahc_outb(ahc, SCB_NEXT, next);
  5202. }
  5203. /*
  5204. * Point us back at the original scb position.
  5205. */
  5206. ahc_outb(ahc, SCBPTR, curscb);
  5207. return next;
  5208. }
  5209. /******************************** Error Handling ******************************/
  5210. /*
  5211. * Abort all SCBs that match the given description (target/channel/lun/tag),
  5212. * setting their status to the passed in status if the status has not already
  5213. * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
  5214. * is paused before it is called.
  5215. */
  5216. int
  5217. ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
  5218. int lun, u_int tag, role_t role, uint32_t status)
  5219. {
  5220. struct scb *scbp;
  5221. struct scb *scbp_next;
  5222. u_int active_scb;
  5223. int i, j;
  5224. int maxtarget;
  5225. int minlun;
  5226. int maxlun;
  5227. int found;
  5228. /*
  5229. * Don't attempt to run any queued untagged transactions
  5230. * until we are done with the abort process.
  5231. */
  5232. ahc_freeze_untagged_queues(ahc);
  5233. /* restore this when we're done */
  5234. active_scb = ahc_inb(ahc, SCBPTR);
  5235. found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
  5236. role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
  5237. /*
  5238. * Clean out the busy target table for any untagged commands.
  5239. */
  5240. i = 0;
  5241. maxtarget = 16;
  5242. if (target != CAM_TARGET_WILDCARD) {
  5243. i = target;
  5244. if (channel == 'B')
  5245. i += 8;
  5246. maxtarget = i + 1;
  5247. }
  5248. if (lun == CAM_LUN_WILDCARD) {
  5249. /*
  5250. * Unless we are using an SCB based
  5251. * busy targets table, there is only
  5252. * one table entry for all luns of
  5253. * a target.
  5254. */
  5255. minlun = 0;
  5256. maxlun = 1;
  5257. if ((ahc->flags & AHC_SCB_BTT) != 0)
  5258. maxlun = AHC_NUM_LUNS;
  5259. } else {
  5260. minlun = lun;
  5261. maxlun = lun + 1;
  5262. }
  5263. if (role != ROLE_TARGET) {
  5264. for (;i < maxtarget; i++) {
  5265. for (j = minlun;j < maxlun; j++) {
  5266. u_int scbid;
  5267. u_int tcl;
  5268. tcl = BUILD_TCL(i << 4, j);
  5269. scbid = ahc_index_busy_tcl(ahc, tcl);
  5270. scbp = ahc_lookup_scb(ahc, scbid);
  5271. if (scbp == NULL
  5272. || ahc_match_scb(ahc, scbp, target, channel,
  5273. lun, tag, role) == 0)
  5274. continue;
  5275. ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
  5276. }
  5277. }
  5278. /*
  5279. * Go through the disconnected list and remove any entries we
  5280. * have queued for completion, 0'ing their control byte too.
  5281. * We save the active SCB and restore it ourselves, so there
  5282. * is no reason for this search to restore it too.
  5283. */
  5284. ahc_search_disc_list(ahc, target, channel, lun, tag,
  5285. /*stop_on_first*/FALSE, /*remove*/TRUE,
  5286. /*save_state*/FALSE);
  5287. }
  5288. /*
  5289. * Go through the hardware SCB array looking for commands that
  5290. * were active but not on any list. In some cases, these remnants
  5291. * might not still have mappings in the scbindex array (e.g. unexpected
  5292. * bus free with the same scb queued for an abort). Don't hold this
  5293. * against them.
  5294. */
  5295. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  5296. u_int scbid;
  5297. ahc_outb(ahc, SCBPTR, i);
  5298. scbid = ahc_inb(ahc, SCB_TAG);
  5299. scbp = ahc_lookup_scb(ahc, scbid);
  5300. if ((scbp == NULL && scbid != SCB_LIST_NULL)
  5301. || (scbp != NULL
  5302. && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
  5303. ahc_add_curscb_to_free_list(ahc);
  5304. }
  5305. /*
  5306. * Go through the pending CCB list and look for
  5307. * commands for this target that are still active.
  5308. * These are other tagged commands that were
  5309. * disconnected when the reset occurred.
  5310. */
  5311. scbp_next = LIST_FIRST(&ahc->pending_scbs);
  5312. while (scbp_next != NULL) {
  5313. scbp = scbp_next;
  5314. scbp_next = LIST_NEXT(scbp, pending_links);
  5315. if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
  5316. cam_status ostat;
  5317. ostat = ahc_get_transaction_status(scbp);
  5318. if (ostat == CAM_REQ_INPROG)
  5319. ahc_set_transaction_status(scbp, status);
  5320. if (ahc_get_transaction_status(scbp) != CAM_REQ_CMP)
  5321. ahc_freeze_scb(scbp);
  5322. if ((scbp->flags & SCB_ACTIVE) == 0)
  5323. printf("Inactive SCB on pending list\n");
  5324. ahc_done(ahc, scbp);
  5325. found++;
  5326. }
  5327. }
  5328. ahc_outb(ahc, SCBPTR, active_scb);
  5329. ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
  5330. ahc_release_untagged_queues(ahc);
  5331. return found;
  5332. }
  5333. static void
  5334. ahc_reset_current_bus(struct ahc_softc *ahc)
  5335. {
  5336. uint8_t scsiseq;
  5337. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
  5338. scsiseq = ahc_inb(ahc, SCSISEQ);
  5339. ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
  5340. ahc_flush_device_writes(ahc);
  5341. ahc_delay(AHC_BUSRESET_DELAY);
  5342. /* Turn off the bus reset */
  5343. ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
  5344. ahc_clear_intstat(ahc);
  5345. /* Re-enable reset interrupts */
  5346. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
  5347. }
  5348. int
  5349. ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
  5350. {
  5351. struct ahc_devinfo devinfo;
  5352. u_int initiator, target, max_scsiid;
  5353. u_int sblkctl;
  5354. u_int scsiseq;
  5355. u_int simode1;
  5356. int found;
  5357. int restart_needed;
  5358. char cur_channel;
  5359. ahc->pending_device = NULL;
  5360. ahc_compile_devinfo(&devinfo,
  5361. CAM_TARGET_WILDCARD,
  5362. CAM_TARGET_WILDCARD,
  5363. CAM_LUN_WILDCARD,
  5364. channel, ROLE_UNKNOWN);
  5365. ahc_pause(ahc);
  5366. /* Make sure the sequencer is in a safe location. */
  5367. ahc_clear_critical_section(ahc);
  5368. /*
  5369. * Run our command complete fifos to ensure that we perform
  5370. * completion processing on any commands that 'completed'
  5371. * before the reset occurred.
  5372. */
  5373. ahc_run_qoutfifo(ahc);
  5374. #ifdef AHC_TARGET_MODE
  5375. /*
  5376. * XXX - In Twin mode, the tqinfifo may have commands
  5377. * for an unaffected channel in it. However, if
  5378. * we have run out of ATIO resources to drain that
  5379. * queue, we may not get them all out here. Further,
  5380. * the blocked transactions for the reset channel
  5381. * should just be killed off, irrespecitve of whether
  5382. * we are blocked on ATIO resources. Write a routine
  5383. * to compact the tqinfifo appropriately.
  5384. */
  5385. if ((ahc->flags & AHC_TARGETROLE) != 0) {
  5386. ahc_run_tqinfifo(ahc, /*paused*/TRUE);
  5387. }
  5388. #endif
  5389. /*
  5390. * Reset the bus if we are initiating this reset
  5391. */
  5392. sblkctl = ahc_inb(ahc, SBLKCTL);
  5393. cur_channel = 'A';
  5394. if ((ahc->features & AHC_TWIN) != 0
  5395. && ((sblkctl & SELBUSB) != 0))
  5396. cur_channel = 'B';
  5397. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  5398. if (cur_channel != channel) {
  5399. /* Case 1: Command for another bus is active
  5400. * Stealthily reset the other bus without
  5401. * upsetting the current bus.
  5402. */
  5403. ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
  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. ahc_outb(ahc, SBLKCTL, sblkctl);
  5420. restart_needed = FALSE;
  5421. } else {
  5422. /* Case 2: A command from this bus is active or we're idle */
  5423. simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
  5424. #ifdef AHC_TARGET_MODE
  5425. /*
  5426. * Bus resets clear ENSELI, so we cannot
  5427. * defer re-enabling bus reset interrupts
  5428. * if we are in target mode.
  5429. */
  5430. if ((ahc->flags & AHC_TARGETROLE) != 0)
  5431. simode1 |= ENSCSIRST;
  5432. #endif
  5433. ahc_outb(ahc, SIMODE1, simode1);
  5434. if (initiate_reset)
  5435. ahc_reset_current_bus(ahc);
  5436. ahc_clear_intstat(ahc);
  5437. ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
  5438. restart_needed = TRUE;
  5439. }
  5440. /*
  5441. * Clean up all the state information for the
  5442. * pending transactions on this bus.
  5443. */
  5444. found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
  5445. CAM_LUN_WILDCARD, SCB_LIST_NULL,
  5446. ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
  5447. max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
  5448. #ifdef AHC_TARGET_MODE
  5449. /*
  5450. * Send an immediate notify ccb to all target more peripheral
  5451. * drivers affected by this action.
  5452. */
  5453. for (target = 0; target <= max_scsiid; target++) {
  5454. struct ahc_tmode_tstate* tstate;
  5455. u_int lun;
  5456. tstate = ahc->enabled_targets[target];
  5457. if (tstate == NULL)
  5458. continue;
  5459. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  5460. struct ahc_tmode_lstate* lstate;
  5461. lstate = tstate->enabled_luns[lun];
  5462. if (lstate == NULL)
  5463. continue;
  5464. ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
  5465. EVENT_TYPE_BUS_RESET, /*arg*/0);
  5466. ahc_send_lstate_events(ahc, lstate);
  5467. }
  5468. }
  5469. #endif
  5470. /* Notify the XPT that a bus reset occurred */
  5471. ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
  5472. CAM_LUN_WILDCARD, AC_BUS_RESET);
  5473. /*
  5474. * Revert to async/narrow transfers until we renegotiate.
  5475. */
  5476. for (target = 0; target <= max_scsiid; target++) {
  5477. if (ahc->enabled_targets[target] == NULL)
  5478. continue;
  5479. for (initiator = 0; initiator <= max_scsiid; initiator++) {
  5480. struct ahc_devinfo devinfo;
  5481. ahc_compile_devinfo(&devinfo, target, initiator,
  5482. CAM_LUN_WILDCARD,
  5483. channel, ROLE_UNKNOWN);
  5484. ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  5485. AHC_TRANS_CUR, /*paused*/TRUE);
  5486. ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
  5487. /*period*/0, /*offset*/0,
  5488. /*ppr_options*/0, AHC_TRANS_CUR,
  5489. /*paused*/TRUE);
  5490. }
  5491. }
  5492. if (restart_needed)
  5493. ahc_restart(ahc);
  5494. else
  5495. ahc_unpause(ahc);
  5496. return found;
  5497. }
  5498. /***************************** Residual Processing ****************************/
  5499. /*
  5500. * Calculate the residual for a just completed SCB.
  5501. */
  5502. void
  5503. ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
  5504. {
  5505. struct hardware_scb *hscb;
  5506. struct status_pkt *spkt;
  5507. uint32_t sgptr;
  5508. uint32_t resid_sgptr;
  5509. uint32_t resid;
  5510. /*
  5511. * 5 cases.
  5512. * 1) No residual.
  5513. * SG_RESID_VALID clear in sgptr.
  5514. * 2) Transferless command
  5515. * 3) Never performed any transfers.
  5516. * sgptr has SG_FULL_RESID set.
  5517. * 4) No residual but target did not
  5518. * save data pointers after the
  5519. * last transfer, so sgptr was
  5520. * never updated.
  5521. * 5) We have a partial residual.
  5522. * Use residual_sgptr to determine
  5523. * where we are.
  5524. */
  5525. hscb = scb->hscb;
  5526. sgptr = ahc_le32toh(hscb->sgptr);
  5527. if ((sgptr & SG_RESID_VALID) == 0)
  5528. /* Case 1 */
  5529. return;
  5530. sgptr &= ~SG_RESID_VALID;
  5531. if ((sgptr & SG_LIST_NULL) != 0)
  5532. /* Case 2 */
  5533. return;
  5534. spkt = &hscb->shared_data.status;
  5535. resid_sgptr = ahc_le32toh(spkt->residual_sg_ptr);
  5536. if ((sgptr & SG_FULL_RESID) != 0) {
  5537. /* Case 3 */
  5538. resid = ahc_get_transfer_length(scb);
  5539. } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
  5540. /* Case 4 */
  5541. return;
  5542. } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
  5543. panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
  5544. } else {
  5545. struct ahc_dma_seg *sg;
  5546. /*
  5547. * Remainder of the SG where the transfer
  5548. * stopped.
  5549. */
  5550. resid = ahc_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
  5551. sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
  5552. /* The residual sg_ptr always points to the next sg */
  5553. sg--;
  5554. /*
  5555. * Add up the contents of all residual
  5556. * SG segments that are after the SG where
  5557. * the transfer stopped.
  5558. */
  5559. while ((ahc_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
  5560. sg++;
  5561. resid += ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  5562. }
  5563. }
  5564. if ((scb->flags & SCB_SENSE) == 0)
  5565. ahc_set_residual(scb, resid);
  5566. else
  5567. ahc_set_sense_residual(scb, resid);
  5568. #ifdef AHC_DEBUG
  5569. if ((ahc_debug & AHC_SHOW_MISC) != 0) {
  5570. ahc_print_path(ahc, scb);
  5571. printf("Handled %sResidual of %d bytes\n",
  5572. (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
  5573. }
  5574. #endif
  5575. }
  5576. /******************************* Target Mode **********************************/
  5577. #ifdef AHC_TARGET_MODE
  5578. /*
  5579. * Add a target mode event to this lun's queue
  5580. */
  5581. static void
  5582. ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
  5583. u_int initiator_id, u_int event_type, u_int event_arg)
  5584. {
  5585. struct ahc_tmode_event *event;
  5586. int pending;
  5587. xpt_freeze_devq(lstate->path, /*count*/1);
  5588. if (lstate->event_w_idx >= lstate->event_r_idx)
  5589. pending = lstate->event_w_idx - lstate->event_r_idx;
  5590. else
  5591. pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
  5592. - (lstate->event_r_idx - lstate->event_w_idx);
  5593. if (event_type == EVENT_TYPE_BUS_RESET
  5594. || event_type == MSG_BUS_DEV_RESET) {
  5595. /*
  5596. * Any earlier events are irrelevant, so reset our buffer.
  5597. * This has the effect of allowing us to deal with reset
  5598. * floods (an external device holding down the reset line)
  5599. * without losing the event that is really interesting.
  5600. */
  5601. lstate->event_r_idx = 0;
  5602. lstate->event_w_idx = 0;
  5603. xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
  5604. }
  5605. if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
  5606. xpt_print_path(lstate->path);
  5607. printf("immediate event %x:%x lost\n",
  5608. lstate->event_buffer[lstate->event_r_idx].event_type,
  5609. lstate->event_buffer[lstate->event_r_idx].event_arg);
  5610. lstate->event_r_idx++;
  5611. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5612. lstate->event_r_idx = 0;
  5613. xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
  5614. }
  5615. event = &lstate->event_buffer[lstate->event_w_idx];
  5616. event->initiator_id = initiator_id;
  5617. event->event_type = event_type;
  5618. event->event_arg = event_arg;
  5619. lstate->event_w_idx++;
  5620. if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5621. lstate->event_w_idx = 0;
  5622. }
  5623. /*
  5624. * Send any target mode events queued up waiting
  5625. * for immediate notify resources.
  5626. */
  5627. void
  5628. ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
  5629. {
  5630. struct ccb_hdr *ccbh;
  5631. struct ccb_immed_notify *inot;
  5632. while (lstate->event_r_idx != lstate->event_w_idx
  5633. && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
  5634. struct ahc_tmode_event *event;
  5635. event = &lstate->event_buffer[lstate->event_r_idx];
  5636. SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
  5637. inot = (struct ccb_immed_notify *)ccbh;
  5638. switch (event->event_type) {
  5639. case EVENT_TYPE_BUS_RESET:
  5640. ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
  5641. break;
  5642. default:
  5643. ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
  5644. inot->message_args[0] = event->event_type;
  5645. inot->message_args[1] = event->event_arg;
  5646. break;
  5647. }
  5648. inot->initiator_id = event->initiator_id;
  5649. inot->sense_len = 0;
  5650. xpt_done((union ccb *)inot);
  5651. lstate->event_r_idx++;
  5652. if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
  5653. lstate->event_r_idx = 0;
  5654. }
  5655. }
  5656. #endif
  5657. /******************** Sequencer Program Patching/Download *********************/
  5658. #ifdef AHC_DUMP_SEQ
  5659. void
  5660. ahc_dumpseq(struct ahc_softc* ahc)
  5661. {
  5662. int i;
  5663. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5664. ahc_outb(ahc, SEQADDR0, 0);
  5665. ahc_outb(ahc, SEQADDR1, 0);
  5666. for (i = 0; i < ahc->instruction_ram_size; i++) {
  5667. uint8_t ins_bytes[4];
  5668. ahc_insb(ahc, SEQRAM, ins_bytes, 4);
  5669. printf("0x%08x\n", ins_bytes[0] << 24
  5670. | ins_bytes[1] << 16
  5671. | ins_bytes[2] << 8
  5672. | ins_bytes[3]);
  5673. }
  5674. }
  5675. #endif
  5676. static int
  5677. ahc_loadseq(struct ahc_softc *ahc)
  5678. {
  5679. struct cs cs_table[num_critical_sections];
  5680. u_int begin_set[num_critical_sections];
  5681. u_int end_set[num_critical_sections];
  5682. struct patch *cur_patch;
  5683. u_int cs_count;
  5684. u_int cur_cs;
  5685. u_int i;
  5686. u_int skip_addr;
  5687. u_int sg_prefetch_cnt;
  5688. int downloaded;
  5689. uint8_t download_consts[7];
  5690. /*
  5691. * Start out with 0 critical sections
  5692. * that apply to this firmware load.
  5693. */
  5694. cs_count = 0;
  5695. cur_cs = 0;
  5696. memset(begin_set, 0, sizeof(begin_set));
  5697. memset(end_set, 0, sizeof(end_set));
  5698. /* Setup downloadable constant table */
  5699. download_consts[QOUTFIFO_OFFSET] = 0;
  5700. if (ahc->targetcmds != NULL)
  5701. download_consts[QOUTFIFO_OFFSET] += 32;
  5702. download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
  5703. download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
  5704. download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
  5705. sg_prefetch_cnt = ahc->pci_cachesize;
  5706. if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
  5707. sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
  5708. download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
  5709. download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
  5710. download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
  5711. cur_patch = patches;
  5712. downloaded = 0;
  5713. skip_addr = 0;
  5714. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
  5715. ahc_outb(ahc, SEQADDR0, 0);
  5716. ahc_outb(ahc, SEQADDR1, 0);
  5717. for (i = 0; i < sizeof(seqprog)/4; i++) {
  5718. if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
  5719. /*
  5720. * Don't download this instruction as it
  5721. * is in a patch that was removed.
  5722. */
  5723. continue;
  5724. }
  5725. if (downloaded == ahc->instruction_ram_size) {
  5726. /*
  5727. * We're about to exceed the instruction
  5728. * storage capacity for this chip. Fail
  5729. * the load.
  5730. */
  5731. printf("\n%s: Program too large for instruction memory "
  5732. "size of %d!\n", ahc_name(ahc),
  5733. ahc->instruction_ram_size);
  5734. return (ENOMEM);
  5735. }
  5736. /*
  5737. * Move through the CS table until we find a CS
  5738. * that might apply to this instruction.
  5739. */
  5740. for (; cur_cs < num_critical_sections; cur_cs++) {
  5741. if (critical_sections[cur_cs].end <= i) {
  5742. if (begin_set[cs_count] == TRUE
  5743. && end_set[cs_count] == FALSE) {
  5744. cs_table[cs_count].end = downloaded;
  5745. end_set[cs_count] = TRUE;
  5746. cs_count++;
  5747. }
  5748. continue;
  5749. }
  5750. if (critical_sections[cur_cs].begin <= i
  5751. && begin_set[cs_count] == FALSE) {
  5752. cs_table[cs_count].begin = downloaded;
  5753. begin_set[cs_count] = TRUE;
  5754. }
  5755. break;
  5756. }
  5757. ahc_download_instr(ahc, i, download_consts);
  5758. downloaded++;
  5759. }
  5760. ahc->num_critical_sections = cs_count;
  5761. if (cs_count != 0) {
  5762. cs_count *= sizeof(struct cs);
  5763. ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
  5764. if (ahc->critical_sections == NULL)
  5765. panic("ahc_loadseq: Could not malloc");
  5766. memcpy(ahc->critical_sections, cs_table, cs_count);
  5767. }
  5768. ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
  5769. if (bootverbose) {
  5770. printf(" %d instructions downloaded\n", downloaded);
  5771. printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
  5772. ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
  5773. }
  5774. return (0);
  5775. }
  5776. static int
  5777. ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
  5778. u_int start_instr, u_int *skip_addr)
  5779. {
  5780. struct patch *cur_patch;
  5781. struct patch *last_patch;
  5782. u_int num_patches;
  5783. num_patches = ARRAY_SIZE(patches);
  5784. last_patch = &patches[num_patches];
  5785. cur_patch = *start_patch;
  5786. while (cur_patch < last_patch && start_instr == cur_patch->begin) {
  5787. if (cur_patch->patch_func(ahc) == 0) {
  5788. /* Start rejecting code */
  5789. *skip_addr = start_instr + cur_patch->skip_instr;
  5790. cur_patch += cur_patch->skip_patch;
  5791. } else {
  5792. /* Accepted this patch. Advance to the next
  5793. * one and wait for our intruction pointer to
  5794. * hit this point.
  5795. */
  5796. cur_patch++;
  5797. }
  5798. }
  5799. *start_patch = cur_patch;
  5800. if (start_instr < *skip_addr)
  5801. /* Still skipping */
  5802. return (0);
  5803. return (1);
  5804. }
  5805. static void
  5806. ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
  5807. {
  5808. union ins_formats instr;
  5809. struct ins_format1 *fmt1_ins;
  5810. struct ins_format3 *fmt3_ins;
  5811. u_int opcode;
  5812. /*
  5813. * The firmware is always compiled into a little endian format.
  5814. */
  5815. instr.integer = ahc_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
  5816. fmt1_ins = &instr.format1;
  5817. fmt3_ins = NULL;
  5818. /* Pull the opcode */
  5819. opcode = instr.format1.opcode;
  5820. switch (opcode) {
  5821. case AIC_OP_JMP:
  5822. case AIC_OP_JC:
  5823. case AIC_OP_JNC:
  5824. case AIC_OP_CALL:
  5825. case AIC_OP_JNE:
  5826. case AIC_OP_JNZ:
  5827. case AIC_OP_JE:
  5828. case AIC_OP_JZ:
  5829. {
  5830. struct patch *cur_patch;
  5831. int address_offset;
  5832. u_int address;
  5833. u_int skip_addr;
  5834. u_int i;
  5835. fmt3_ins = &instr.format3;
  5836. address_offset = 0;
  5837. address = fmt3_ins->address;
  5838. cur_patch = patches;
  5839. skip_addr = 0;
  5840. for (i = 0; i < address;) {
  5841. ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
  5842. if (skip_addr > i) {
  5843. int end_addr;
  5844. end_addr = min(address, skip_addr);
  5845. address_offset += end_addr - i;
  5846. i = skip_addr;
  5847. } else {
  5848. i++;
  5849. }
  5850. }
  5851. address -= address_offset;
  5852. fmt3_ins->address = address;
  5853. /* FALLTHROUGH */
  5854. }
  5855. case AIC_OP_OR:
  5856. case AIC_OP_AND:
  5857. case AIC_OP_XOR:
  5858. case AIC_OP_ADD:
  5859. case AIC_OP_ADC:
  5860. case AIC_OP_BMOV:
  5861. if (fmt1_ins->parity != 0) {
  5862. fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
  5863. }
  5864. fmt1_ins->parity = 0;
  5865. if ((ahc->features & AHC_CMD_CHAN) == 0
  5866. && opcode == AIC_OP_BMOV) {
  5867. /*
  5868. * Block move was added at the same time
  5869. * as the command channel. Verify that
  5870. * this is only a move of a single element
  5871. * and convert the BMOV to a MOV
  5872. * (AND with an immediate of FF).
  5873. */
  5874. if (fmt1_ins->immediate != 1)
  5875. panic("%s: BMOV not supported\n",
  5876. ahc_name(ahc));
  5877. fmt1_ins->opcode = AIC_OP_AND;
  5878. fmt1_ins->immediate = 0xff;
  5879. }
  5880. /* FALLTHROUGH */
  5881. case AIC_OP_ROL:
  5882. if ((ahc->features & AHC_ULTRA2) != 0) {
  5883. int i, count;
  5884. /* Calculate odd parity for the instruction */
  5885. for (i = 0, count = 0; i < 31; i++) {
  5886. uint32_t mask;
  5887. mask = 0x01 << i;
  5888. if ((instr.integer & mask) != 0)
  5889. count++;
  5890. }
  5891. if ((count & 0x01) == 0)
  5892. instr.format1.parity = 1;
  5893. } else {
  5894. /* Compress the instruction for older sequencers */
  5895. if (fmt3_ins != NULL) {
  5896. instr.integer =
  5897. fmt3_ins->immediate
  5898. | (fmt3_ins->source << 8)
  5899. | (fmt3_ins->address << 16)
  5900. | (fmt3_ins->opcode << 25);
  5901. } else {
  5902. instr.integer =
  5903. fmt1_ins->immediate
  5904. | (fmt1_ins->source << 8)
  5905. | (fmt1_ins->destination << 16)
  5906. | (fmt1_ins->ret << 24)
  5907. | (fmt1_ins->opcode << 25);
  5908. }
  5909. }
  5910. /* The sequencer is a little endian cpu */
  5911. instr.integer = ahc_htole32(instr.integer);
  5912. ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
  5913. break;
  5914. default:
  5915. panic("Unknown opcode encountered in seq program");
  5916. break;
  5917. }
  5918. }
  5919. int
  5920. ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries,
  5921. const char *name, u_int address, u_int value,
  5922. u_int *cur_column, u_int wrap_point)
  5923. {
  5924. int printed;
  5925. u_int printed_mask;
  5926. if (cur_column != NULL && *cur_column >= wrap_point) {
  5927. printf("\n");
  5928. *cur_column = 0;
  5929. }
  5930. printed = printf("%s[0x%x]", name, value);
  5931. if (table == NULL) {
  5932. printed += printf(" ");
  5933. *cur_column += printed;
  5934. return (printed);
  5935. }
  5936. printed_mask = 0;
  5937. while (printed_mask != 0xFF) {
  5938. int entry;
  5939. for (entry = 0; entry < num_entries; entry++) {
  5940. if (((value & table[entry].mask)
  5941. != table[entry].value)
  5942. || ((printed_mask & table[entry].mask)
  5943. == table[entry].mask))
  5944. continue;
  5945. printed += printf("%s%s",
  5946. printed_mask == 0 ? ":(" : "|",
  5947. table[entry].name);
  5948. printed_mask |= table[entry].mask;
  5949. break;
  5950. }
  5951. if (entry >= num_entries)
  5952. break;
  5953. }
  5954. if (printed_mask != 0)
  5955. printed += printf(") ");
  5956. else
  5957. printed += printf(" ");
  5958. if (cur_column != NULL)
  5959. *cur_column += printed;
  5960. return (printed);
  5961. }
  5962. void
  5963. ahc_dump_card_state(struct ahc_softc *ahc)
  5964. {
  5965. struct scb *scb;
  5966. struct scb_tailq *untagged_q;
  5967. u_int cur_col;
  5968. int paused;
  5969. int target;
  5970. int maxtarget;
  5971. int i;
  5972. uint8_t last_phase;
  5973. uint8_t qinpos;
  5974. uint8_t qintail;
  5975. uint8_t qoutpos;
  5976. uint8_t scb_index;
  5977. uint8_t saved_scbptr;
  5978. if (ahc_is_paused(ahc)) {
  5979. paused = 1;
  5980. } else {
  5981. paused = 0;
  5982. ahc_pause(ahc);
  5983. }
  5984. saved_scbptr = ahc_inb(ahc, SCBPTR);
  5985. last_phase = ahc_inb(ahc, LASTPHASE);
  5986. printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
  5987. "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
  5988. ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
  5989. ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
  5990. if (paused)
  5991. printf("Card was paused\n");
  5992. printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
  5993. ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
  5994. ahc_inb(ahc, ARG_2));
  5995. printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
  5996. ahc_inb(ahc, SCBPTR));
  5997. cur_col = 0;
  5998. if ((ahc->features & AHC_DT) != 0)
  5999. ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
  6000. ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
  6001. ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
  6002. ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
  6003. ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
  6004. ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
  6005. ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
  6006. ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
  6007. ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
  6008. ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
  6009. ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
  6010. ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
  6011. ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
  6012. ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
  6013. ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
  6014. ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
  6015. ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
  6016. ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
  6017. ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
  6018. if (cur_col != 0)
  6019. printf("\n");
  6020. printf("STACK:");
  6021. for (i = 0; i < STACK_SIZE; i++)
  6022. printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
  6023. printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
  6024. printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
  6025. printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
  6026. /* QINFIFO */
  6027. printf("QINFIFO entries: ");
  6028. if ((ahc->features & AHC_QUEUE_REGS) != 0) {
  6029. qinpos = ahc_inb(ahc, SNSCB_QOFF);
  6030. ahc_outb(ahc, SNSCB_QOFF, qinpos);
  6031. } else
  6032. qinpos = ahc_inb(ahc, QINPOS);
  6033. qintail = ahc->qinfifonext;
  6034. while (qinpos != qintail) {
  6035. printf("%d ", ahc->qinfifo[qinpos]);
  6036. qinpos++;
  6037. }
  6038. printf("\n");
  6039. printf("Waiting Queue entries: ");
  6040. scb_index = ahc_inb(ahc, WAITING_SCBH);
  6041. i = 0;
  6042. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6043. ahc_outb(ahc, SCBPTR, scb_index);
  6044. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6045. scb_index = ahc_inb(ahc, SCB_NEXT);
  6046. }
  6047. printf("\n");
  6048. printf("Disconnected Queue entries: ");
  6049. scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
  6050. i = 0;
  6051. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6052. ahc_outb(ahc, SCBPTR, scb_index);
  6053. printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
  6054. scb_index = ahc_inb(ahc, SCB_NEXT);
  6055. }
  6056. printf("\n");
  6057. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  6058. printf("QOUTFIFO entries: ");
  6059. qoutpos = ahc->qoutfifonext;
  6060. i = 0;
  6061. while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
  6062. printf("%d ", ahc->qoutfifo[qoutpos]);
  6063. qoutpos++;
  6064. }
  6065. printf("\n");
  6066. printf("Sequencer Free SCB List: ");
  6067. scb_index = ahc_inb(ahc, FREE_SCBH);
  6068. i = 0;
  6069. while (scb_index != SCB_LIST_NULL && i++ < 256) {
  6070. ahc_outb(ahc, SCBPTR, scb_index);
  6071. printf("%d ", scb_index);
  6072. scb_index = ahc_inb(ahc, SCB_NEXT);
  6073. }
  6074. printf("\n");
  6075. printf("Sequencer SCB Info: ");
  6076. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  6077. ahc_outb(ahc, SCBPTR, i);
  6078. cur_col = printf("\n%3d ", i);
  6079. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
  6080. ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
  6081. ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
  6082. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6083. }
  6084. printf("\n");
  6085. printf("Pending list: ");
  6086. i = 0;
  6087. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6088. if (i++ > 256)
  6089. break;
  6090. cur_col = printf("\n%3d ", scb->hscb->tag);
  6091. ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
  6092. ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
  6093. ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
  6094. if ((ahc->flags & AHC_PAGESCBS) == 0) {
  6095. ahc_outb(ahc, SCBPTR, scb->hscb->tag);
  6096. printf("(");
  6097. ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
  6098. &cur_col, 60);
  6099. ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
  6100. printf(")");
  6101. }
  6102. }
  6103. printf("\n");
  6104. printf("Kernel Free SCB list: ");
  6105. i = 0;
  6106. SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
  6107. if (i++ > 256)
  6108. break;
  6109. printf("%d ", scb->hscb->tag);
  6110. }
  6111. printf("\n");
  6112. maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
  6113. for (target = 0; target <= maxtarget; target++) {
  6114. untagged_q = &ahc->untagged_queues[target];
  6115. if (TAILQ_FIRST(untagged_q) == NULL)
  6116. continue;
  6117. printf("Untagged Q(%d): ", target);
  6118. i = 0;
  6119. TAILQ_FOREACH(scb, untagged_q, links.tqe) {
  6120. if (i++ > 256)
  6121. break;
  6122. printf("%d ", scb->hscb->tag);
  6123. }
  6124. printf("\n");
  6125. }
  6126. ahc_platform_dump_card_state(ahc);
  6127. printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
  6128. ahc_outb(ahc, SCBPTR, saved_scbptr);
  6129. if (paused == 0)
  6130. ahc_unpause(ahc);
  6131. }
  6132. /************************* Target Mode ****************************************/
  6133. #ifdef AHC_TARGET_MODE
  6134. cam_status
  6135. ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
  6136. struct ahc_tmode_tstate **tstate,
  6137. struct ahc_tmode_lstate **lstate,
  6138. int notfound_failure)
  6139. {
  6140. if ((ahc->features & AHC_TARGETMODE) == 0)
  6141. return (CAM_REQ_INVALID);
  6142. /*
  6143. * Handle the 'black hole' device that sucks up
  6144. * requests to unattached luns on enabled targets.
  6145. */
  6146. if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
  6147. && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
  6148. *tstate = NULL;
  6149. *lstate = ahc->black_hole;
  6150. } else {
  6151. u_int max_id;
  6152. max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
  6153. if (ccb->ccb_h.target_id >= max_id)
  6154. return (CAM_TID_INVALID);
  6155. if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
  6156. return (CAM_LUN_INVALID);
  6157. *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
  6158. *lstate = NULL;
  6159. if (*tstate != NULL)
  6160. *lstate =
  6161. (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
  6162. }
  6163. if (notfound_failure != 0 && *lstate == NULL)
  6164. return (CAM_PATH_INVALID);
  6165. return (CAM_REQ_CMP);
  6166. }
  6167. void
  6168. ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
  6169. {
  6170. struct ahc_tmode_tstate *tstate;
  6171. struct ahc_tmode_lstate *lstate;
  6172. struct ccb_en_lun *cel;
  6173. cam_status status;
  6174. u_long s;
  6175. u_int target;
  6176. u_int lun;
  6177. u_int target_mask;
  6178. u_int our_id;
  6179. int error;
  6180. char channel;
  6181. status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
  6182. /*notfound_failure*/FALSE);
  6183. if (status != CAM_REQ_CMP) {
  6184. ccb->ccb_h.status = status;
  6185. return;
  6186. }
  6187. if (cam_sim_bus(sim) == 0)
  6188. our_id = ahc->our_id;
  6189. else
  6190. our_id = ahc->our_id_b;
  6191. if (ccb->ccb_h.target_id != our_id) {
  6192. /*
  6193. * our_id represents our initiator ID, or
  6194. * the ID of the first target to have an
  6195. * enabled lun in target mode. There are
  6196. * two cases that may preclude enabling a
  6197. * target id other than our_id.
  6198. *
  6199. * o our_id is for an active initiator role.
  6200. * Since the hardware does not support
  6201. * reselections to the initiator role at
  6202. * anything other than our_id, and our_id
  6203. * is used by the hardware to indicate the
  6204. * ID to use for both select-out and
  6205. * reselect-out operations, the only target
  6206. * ID we can support in this mode is our_id.
  6207. *
  6208. * o The MULTARGID feature is not available and
  6209. * a previous target mode ID has been enabled.
  6210. */
  6211. if ((ahc->features & AHC_MULTIROLE) != 0) {
  6212. if ((ahc->features & AHC_MULTI_TID) != 0
  6213. && (ahc->flags & AHC_INITIATORROLE) != 0) {
  6214. /*
  6215. * Only allow additional targets if
  6216. * the initiator role is disabled.
  6217. * The hardware cannot handle a re-select-in
  6218. * on the initiator id during a re-select-out
  6219. * on a different target id.
  6220. */
  6221. status = CAM_TID_INVALID;
  6222. } else if ((ahc->flags & AHC_INITIATORROLE) != 0
  6223. || ahc->enabled_luns > 0) {
  6224. /*
  6225. * Only allow our target id to change
  6226. * if the initiator role is not configured
  6227. * and there are no enabled luns which
  6228. * are attached to the currently registered
  6229. * scsi id.
  6230. */
  6231. status = CAM_TID_INVALID;
  6232. }
  6233. } else if ((ahc->features & AHC_MULTI_TID) == 0
  6234. && ahc->enabled_luns > 0) {
  6235. status = CAM_TID_INVALID;
  6236. }
  6237. }
  6238. if (status != CAM_REQ_CMP) {
  6239. ccb->ccb_h.status = status;
  6240. return;
  6241. }
  6242. /*
  6243. * We now have an id that is valid.
  6244. * If we aren't in target mode, switch modes.
  6245. */
  6246. if ((ahc->flags & AHC_TARGETROLE) == 0
  6247. && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
  6248. u_long s;
  6249. ahc_flag saved_flags;
  6250. printf("Configuring Target Mode\n");
  6251. ahc_lock(ahc, &s);
  6252. if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
  6253. ccb->ccb_h.status = CAM_BUSY;
  6254. ahc_unlock(ahc, &s);
  6255. return;
  6256. }
  6257. saved_flags = ahc->flags;
  6258. ahc->flags |= AHC_TARGETROLE;
  6259. if ((ahc->features & AHC_MULTIROLE) == 0)
  6260. ahc->flags &= ~AHC_INITIATORROLE;
  6261. ahc_pause(ahc);
  6262. error = ahc_loadseq(ahc);
  6263. if (error != 0) {
  6264. /*
  6265. * Restore original configuration and notify
  6266. * the caller that we cannot support target mode.
  6267. * Since the adapter started out in this
  6268. * configuration, the firmware load will succeed,
  6269. * so there is no point in checking ahc_loadseq's
  6270. * return value.
  6271. */
  6272. ahc->flags = saved_flags;
  6273. (void)ahc_loadseq(ahc);
  6274. ahc_restart(ahc);
  6275. ahc_unlock(ahc, &s);
  6276. ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
  6277. return;
  6278. }
  6279. ahc_restart(ahc);
  6280. ahc_unlock(ahc, &s);
  6281. }
  6282. cel = &ccb->cel;
  6283. target = ccb->ccb_h.target_id;
  6284. lun = ccb->ccb_h.target_lun;
  6285. channel = SIM_CHANNEL(ahc, sim);
  6286. target_mask = 0x01 << target;
  6287. if (channel == 'B')
  6288. target_mask <<= 8;
  6289. if (cel->enable != 0) {
  6290. u_int scsiseq;
  6291. /* Are we already enabled?? */
  6292. if (lstate != NULL) {
  6293. xpt_print_path(ccb->ccb_h.path);
  6294. printf("Lun already enabled\n");
  6295. ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
  6296. return;
  6297. }
  6298. if (cel->grp6_len != 0
  6299. || cel->grp7_len != 0) {
  6300. /*
  6301. * Don't (yet?) support vendor
  6302. * specific commands.
  6303. */
  6304. ccb->ccb_h.status = CAM_REQ_INVALID;
  6305. printf("Non-zero Group Codes\n");
  6306. return;
  6307. }
  6308. /*
  6309. * Seems to be okay.
  6310. * Setup our data structures.
  6311. */
  6312. if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
  6313. tstate = ahc_alloc_tstate(ahc, target, channel);
  6314. if (tstate == NULL) {
  6315. xpt_print_path(ccb->ccb_h.path);
  6316. printf("Couldn't allocate tstate\n");
  6317. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6318. return;
  6319. }
  6320. }
  6321. lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
  6322. if (lstate == NULL) {
  6323. xpt_print_path(ccb->ccb_h.path);
  6324. printf("Couldn't allocate lstate\n");
  6325. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6326. return;
  6327. }
  6328. memset(lstate, 0, sizeof(*lstate));
  6329. status = xpt_create_path(&lstate->path, /*periph*/NULL,
  6330. xpt_path_path_id(ccb->ccb_h.path),
  6331. xpt_path_target_id(ccb->ccb_h.path),
  6332. xpt_path_lun_id(ccb->ccb_h.path));
  6333. if (status != CAM_REQ_CMP) {
  6334. free(lstate, M_DEVBUF);
  6335. xpt_print_path(ccb->ccb_h.path);
  6336. printf("Couldn't allocate path\n");
  6337. ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
  6338. return;
  6339. }
  6340. SLIST_INIT(&lstate->accept_tios);
  6341. SLIST_INIT(&lstate->immed_notifies);
  6342. ahc_lock(ahc, &s);
  6343. ahc_pause(ahc);
  6344. if (target != CAM_TARGET_WILDCARD) {
  6345. tstate->enabled_luns[lun] = lstate;
  6346. ahc->enabled_luns++;
  6347. if ((ahc->features & AHC_MULTI_TID) != 0) {
  6348. u_int targid_mask;
  6349. targid_mask = ahc_inb(ahc, TARGID)
  6350. | (ahc_inb(ahc, TARGID + 1) << 8);
  6351. targid_mask |= target_mask;
  6352. ahc_outb(ahc, TARGID, targid_mask);
  6353. ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
  6354. ahc_update_scsiid(ahc, targid_mask);
  6355. } else {
  6356. u_int our_id;
  6357. char channel;
  6358. channel = SIM_CHANNEL(ahc, sim);
  6359. our_id = SIM_SCSI_ID(ahc, sim);
  6360. /*
  6361. * This can only happen if selections
  6362. * are not enabled
  6363. */
  6364. if (target != our_id) {
  6365. u_int sblkctl;
  6366. char cur_channel;
  6367. int swap;
  6368. sblkctl = ahc_inb(ahc, SBLKCTL);
  6369. cur_channel = (sblkctl & SELBUSB)
  6370. ? 'B' : 'A';
  6371. if ((ahc->features & AHC_TWIN) == 0)
  6372. cur_channel = 'A';
  6373. swap = cur_channel != channel;
  6374. if (channel == 'A')
  6375. ahc->our_id = target;
  6376. else
  6377. ahc->our_id_b = target;
  6378. if (swap)
  6379. ahc_outb(ahc, SBLKCTL,
  6380. sblkctl ^ SELBUSB);
  6381. ahc_outb(ahc, SCSIID, target);
  6382. if (swap)
  6383. ahc_outb(ahc, SBLKCTL, sblkctl);
  6384. }
  6385. }
  6386. } else
  6387. ahc->black_hole = lstate;
  6388. /* Allow select-in operations */
  6389. if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
  6390. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6391. scsiseq |= ENSELI;
  6392. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6393. scsiseq = ahc_inb(ahc, SCSISEQ);
  6394. scsiseq |= ENSELI;
  6395. ahc_outb(ahc, SCSISEQ, scsiseq);
  6396. }
  6397. ahc_unpause(ahc);
  6398. ahc_unlock(ahc, &s);
  6399. ccb->ccb_h.status = CAM_REQ_CMP;
  6400. xpt_print_path(ccb->ccb_h.path);
  6401. printf("Lun now enabled for target mode\n");
  6402. } else {
  6403. struct scb *scb;
  6404. int i, empty;
  6405. if (lstate == NULL) {
  6406. ccb->ccb_h.status = CAM_LUN_INVALID;
  6407. return;
  6408. }
  6409. ahc_lock(ahc, &s);
  6410. ccb->ccb_h.status = CAM_REQ_CMP;
  6411. LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
  6412. struct ccb_hdr *ccbh;
  6413. ccbh = &scb->io_ctx->ccb_h;
  6414. if (ccbh->func_code == XPT_CONT_TARGET_IO
  6415. && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
  6416. printf("CTIO pending\n");
  6417. ccb->ccb_h.status = CAM_REQ_INVALID;
  6418. ahc_unlock(ahc, &s);
  6419. return;
  6420. }
  6421. }
  6422. if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
  6423. printf("ATIOs pending\n");
  6424. ccb->ccb_h.status = CAM_REQ_INVALID;
  6425. }
  6426. if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
  6427. printf("INOTs pending\n");
  6428. ccb->ccb_h.status = CAM_REQ_INVALID;
  6429. }
  6430. if (ccb->ccb_h.status != CAM_REQ_CMP) {
  6431. ahc_unlock(ahc, &s);
  6432. return;
  6433. }
  6434. xpt_print_path(ccb->ccb_h.path);
  6435. printf("Target mode disabled\n");
  6436. xpt_free_path(lstate->path);
  6437. free(lstate, M_DEVBUF);
  6438. ahc_pause(ahc);
  6439. /* Can we clean up the target too? */
  6440. if (target != CAM_TARGET_WILDCARD) {
  6441. tstate->enabled_luns[lun] = NULL;
  6442. ahc->enabled_luns--;
  6443. for (empty = 1, i = 0; i < 8; i++)
  6444. if (tstate->enabled_luns[i] != NULL) {
  6445. empty = 0;
  6446. break;
  6447. }
  6448. if (empty) {
  6449. ahc_free_tstate(ahc, target, channel,
  6450. /*force*/FALSE);
  6451. if (ahc->features & AHC_MULTI_TID) {
  6452. u_int targid_mask;
  6453. targid_mask = ahc_inb(ahc, TARGID)
  6454. | (ahc_inb(ahc, TARGID + 1)
  6455. << 8);
  6456. targid_mask &= ~target_mask;
  6457. ahc_outb(ahc, TARGID, targid_mask);
  6458. ahc_outb(ahc, TARGID+1,
  6459. (targid_mask >> 8));
  6460. ahc_update_scsiid(ahc, targid_mask);
  6461. }
  6462. }
  6463. } else {
  6464. ahc->black_hole = NULL;
  6465. /*
  6466. * We can't allow selections without
  6467. * our black hole device.
  6468. */
  6469. empty = TRUE;
  6470. }
  6471. if (ahc->enabled_luns == 0) {
  6472. /* Disallow select-in */
  6473. u_int scsiseq;
  6474. scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
  6475. scsiseq &= ~ENSELI;
  6476. ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
  6477. scsiseq = ahc_inb(ahc, SCSISEQ);
  6478. scsiseq &= ~ENSELI;
  6479. ahc_outb(ahc, SCSISEQ, scsiseq);
  6480. if ((ahc->features & AHC_MULTIROLE) == 0) {
  6481. printf("Configuring Initiator Mode\n");
  6482. ahc->flags &= ~AHC_TARGETROLE;
  6483. ahc->flags |= AHC_INITIATORROLE;
  6484. /*
  6485. * Returning to a configuration that
  6486. * fit previously will always succeed.
  6487. */
  6488. (void)ahc_loadseq(ahc);
  6489. ahc_restart(ahc);
  6490. /*
  6491. * Unpaused. The extra unpause
  6492. * that follows is harmless.
  6493. */
  6494. }
  6495. }
  6496. ahc_unpause(ahc);
  6497. ahc_unlock(ahc, &s);
  6498. }
  6499. }
  6500. static void
  6501. ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
  6502. {
  6503. u_int scsiid_mask;
  6504. u_int scsiid;
  6505. if ((ahc->features & AHC_MULTI_TID) == 0)
  6506. panic("ahc_update_scsiid called on non-multitid unit\n");
  6507. /*
  6508. * Since we will rely on the TARGID mask
  6509. * for selection enables, ensure that OID
  6510. * in SCSIID is not set to some other ID
  6511. * that we don't want to allow selections on.
  6512. */
  6513. if ((ahc->features & AHC_ULTRA2) != 0)
  6514. scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
  6515. else
  6516. scsiid = ahc_inb(ahc, SCSIID);
  6517. scsiid_mask = 0x1 << (scsiid & OID);
  6518. if ((targid_mask & scsiid_mask) == 0) {
  6519. u_int our_id;
  6520. /* ffs counts from 1 */
  6521. our_id = ffs(targid_mask);
  6522. if (our_id == 0)
  6523. our_id = ahc->our_id;
  6524. else
  6525. our_id--;
  6526. scsiid &= TID;
  6527. scsiid |= our_id;
  6528. }
  6529. if ((ahc->features & AHC_ULTRA2) != 0)
  6530. ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
  6531. else
  6532. ahc_outb(ahc, SCSIID, scsiid);
  6533. }
  6534. void
  6535. ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
  6536. {
  6537. struct target_cmd *cmd;
  6538. /*
  6539. * If the card supports auto-access pause,
  6540. * we can access the card directly regardless
  6541. * of whether it is paused or not.
  6542. */
  6543. if ((ahc->features & AHC_AUTOPAUSE) != 0)
  6544. paused = TRUE;
  6545. ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
  6546. while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
  6547. /*
  6548. * Only advance through the queue if we
  6549. * have the resources to process the command.
  6550. */
  6551. if (ahc_handle_target_cmd(ahc, cmd) != 0)
  6552. break;
  6553. cmd->cmd_valid = 0;
  6554. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  6555. ahc->shared_data_dmamap,
  6556. ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
  6557. sizeof(struct target_cmd),
  6558. BUS_DMASYNC_PREREAD);
  6559. ahc->tqinfifonext++;
  6560. /*
  6561. * Lazily update our position in the target mode incoming
  6562. * command queue as seen by the sequencer.
  6563. */
  6564. if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
  6565. if ((ahc->features & AHC_HS_MAILBOX) != 0) {
  6566. u_int hs_mailbox;
  6567. hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
  6568. hs_mailbox &= ~HOST_TQINPOS;
  6569. hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
  6570. ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
  6571. } else {
  6572. if (!paused)
  6573. ahc_pause(ahc);
  6574. ahc_outb(ahc, KERNEL_TQINPOS,
  6575. ahc->tqinfifonext & HOST_TQINPOS);
  6576. if (!paused)
  6577. ahc_unpause(ahc);
  6578. }
  6579. }
  6580. }
  6581. }
  6582. static int
  6583. ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
  6584. {
  6585. struct ahc_tmode_tstate *tstate;
  6586. struct ahc_tmode_lstate *lstate;
  6587. struct ccb_accept_tio *atio;
  6588. uint8_t *byte;
  6589. int initiator;
  6590. int target;
  6591. int lun;
  6592. initiator = SCSIID_TARGET(ahc, cmd->scsiid);
  6593. target = SCSIID_OUR_ID(cmd->scsiid);
  6594. lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
  6595. byte = cmd->bytes;
  6596. tstate = ahc->enabled_targets[target];
  6597. lstate = NULL;
  6598. if (tstate != NULL)
  6599. lstate = tstate->enabled_luns[lun];
  6600. /*
  6601. * Commands for disabled luns go to the black hole driver.
  6602. */
  6603. if (lstate == NULL)
  6604. lstate = ahc->black_hole;
  6605. atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
  6606. if (atio == NULL) {
  6607. ahc->flags |= AHC_TQINFIFO_BLOCKED;
  6608. /*
  6609. * Wait for more ATIOs from the peripheral driver for this lun.
  6610. */
  6611. if (bootverbose)
  6612. printf("%s: ATIOs exhausted\n", ahc_name(ahc));
  6613. return (1);
  6614. } else
  6615. ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
  6616. #if 0
  6617. printf("Incoming command from %d for %d:%d%s\n",
  6618. initiator, target, lun,
  6619. lstate == ahc->black_hole ? "(Black Holed)" : "");
  6620. #endif
  6621. SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
  6622. if (lstate == ahc->black_hole) {
  6623. /* Fill in the wildcards */
  6624. atio->ccb_h.target_id = target;
  6625. atio->ccb_h.target_lun = lun;
  6626. }
  6627. /*
  6628. * Package it up and send it off to
  6629. * whomever has this lun enabled.
  6630. */
  6631. atio->sense_len = 0;
  6632. atio->init_id = initiator;
  6633. if (byte[0] != 0xFF) {
  6634. /* Tag was included */
  6635. atio->tag_action = *byte++;
  6636. atio->tag_id = *byte++;
  6637. atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
  6638. } else {
  6639. atio->ccb_h.flags = 0;
  6640. }
  6641. byte++;
  6642. /* Okay. Now determine the cdb size based on the command code */
  6643. switch (*byte >> CMD_GROUP_CODE_SHIFT) {
  6644. case 0:
  6645. atio->cdb_len = 6;
  6646. break;
  6647. case 1:
  6648. case 2:
  6649. atio->cdb_len = 10;
  6650. break;
  6651. case 4:
  6652. atio->cdb_len = 16;
  6653. break;
  6654. case 5:
  6655. atio->cdb_len = 12;
  6656. break;
  6657. case 3:
  6658. default:
  6659. /* Only copy the opcode. */
  6660. atio->cdb_len = 1;
  6661. printf("Reserved or VU command code type encountered\n");
  6662. break;
  6663. }
  6664. memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
  6665. atio->ccb_h.status |= CAM_CDB_RECVD;
  6666. if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
  6667. /*
  6668. * We weren't allowed to disconnect.
  6669. * We're hanging on the bus until a
  6670. * continue target I/O comes in response
  6671. * to this accept tio.
  6672. */
  6673. #if 0
  6674. printf("Received Immediate Command %d:%d:%d - %p\n",
  6675. initiator, target, lun, ahc->pending_device);
  6676. #endif
  6677. ahc->pending_device = lstate;
  6678. ahc_freeze_ccb((union ccb *)atio);
  6679. atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
  6680. }
  6681. xpt_done((union ccb*)atio);
  6682. return (0);
  6683. }
  6684. #endif