aic79xx_core.c 290 KB

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