aic7xxx_core.c 211 KB

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