aic7xxx_core.c 197 KB

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