ncr53c8xx.c 205 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /******************************************************************************
  3. ** Device driver for the PCI-SCSI NCR538XX controller family.
  4. **
  5. ** Copyright (C) 1994 Wolfgang Stanglmeier
  6. **
  7. **
  8. **-----------------------------------------------------------------------------
  9. **
  10. ** This driver has been ported to Linux from the FreeBSD NCR53C8XX driver
  11. ** and is currently maintained by
  12. **
  13. ** Gerard Roudier <[email protected]>
  14. **
  15. ** Being given that this driver originates from the FreeBSD version, and
  16. ** in order to keep synergy on both, any suggested enhancements and corrections
  17. ** received on Linux are automatically a potential candidate for the FreeBSD
  18. ** version.
  19. **
  20. ** The original driver has been written for 386bsd and FreeBSD by
  21. ** Wolfgang Stanglmeier <[email protected]>
  22. ** Stefan Esser <[email protected]>
  23. **
  24. ** And has been ported to NetBSD by
  25. ** Charles M. Hannum <[email protected]>
  26. **
  27. **-----------------------------------------------------------------------------
  28. **
  29. ** Brief history
  30. **
  31. ** December 10 1995 by Gerard Roudier:
  32. ** Initial port to Linux.
  33. **
  34. ** June 23 1996 by Gerard Roudier:
  35. ** Support for 64 bits architectures (Alpha).
  36. **
  37. ** November 30 1996 by Gerard Roudier:
  38. ** Support for Fast-20 scsi.
  39. ** Support for large DMA fifo and 128 dwords bursting.
  40. **
  41. ** February 27 1997 by Gerard Roudier:
  42. ** Support for Fast-40 scsi.
  43. ** Support for on-Board RAM.
  44. **
  45. ** May 3 1997 by Gerard Roudier:
  46. ** Full support for scsi scripts instructions pre-fetching.
  47. **
  48. ** May 19 1997 by Richard Waltham <[email protected]>:
  49. ** Support for NvRAM detection and reading.
  50. **
  51. ** August 18 1997 by Cort <[email protected]>:
  52. ** Support for Power/PC (Big Endian).
  53. **
  54. ** June 20 1998 by Gerard Roudier
  55. ** Support for up to 64 tags per lun.
  56. ** O(1) everywhere (C and SCRIPTS) for normal cases.
  57. ** Low PCI traffic for command handling when on-chip RAM is present.
  58. ** Aggressive SCSI SCRIPTS optimizations.
  59. **
  60. ** 2005 by Matthew Wilcox and James Bottomley
  61. ** PCI-ectomy. This driver now supports only the 720 chip (see the
  62. ** NCR_Q720 and zalon drivers for the bus probe logic).
  63. **
  64. *******************************************************************************
  65. */
  66. /*
  67. ** Supported SCSI-II features:
  68. ** Synchronous negotiation
  69. ** Wide negotiation (depends on the NCR Chip)
  70. ** Enable disconnection
  71. ** Tagged command queuing
  72. ** Parity checking
  73. ** Etc...
  74. **
  75. ** Supported NCR/SYMBIOS chips:
  76. ** 53C720 (Wide, Fast SCSI-2, intfly problems)
  77. */
  78. /* Name and version of the driver */
  79. #define SCSI_NCR_DRIVER_NAME "ncr53c8xx-3.4.3g"
  80. #define SCSI_NCR_DEBUG_FLAGS (0)
  81. #include <linux/blkdev.h>
  82. #include <linux/delay.h>
  83. #include <linux/dma-mapping.h>
  84. #include <linux/errno.h>
  85. #include <linux/gfp.h>
  86. #include <linux/init.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/ioport.h>
  89. #include <linux/mm.h>
  90. #include <linux/module.h>
  91. #include <linux/sched.h>
  92. #include <linux/signal.h>
  93. #include <linux/spinlock.h>
  94. #include <linux/stat.h>
  95. #include <linux/string.h>
  96. #include <linux/time.h>
  97. #include <linux/timer.h>
  98. #include <linux/types.h>
  99. #include <asm/dma.h>
  100. #include <asm/io.h>
  101. #include <scsi/scsi.h>
  102. #include <scsi/scsi_cmnd.h>
  103. #include <scsi/scsi_dbg.h>
  104. #include <scsi/scsi_device.h>
  105. #include <scsi/scsi_tcq.h>
  106. #include <scsi/scsi_transport.h>
  107. #include <scsi/scsi_transport_spi.h>
  108. #include "ncr53c8xx.h"
  109. #define NAME53C8XX "ncr53c8xx"
  110. /*==========================================================
  111. **
  112. ** Debugging tags
  113. **
  114. **==========================================================
  115. */
  116. #define DEBUG_ALLOC (0x0001)
  117. #define DEBUG_PHASE (0x0002)
  118. #define DEBUG_QUEUE (0x0008)
  119. #define DEBUG_RESULT (0x0010)
  120. #define DEBUG_POINTER (0x0020)
  121. #define DEBUG_SCRIPT (0x0040)
  122. #define DEBUG_TINY (0x0080)
  123. #define DEBUG_TIMING (0x0100)
  124. #define DEBUG_NEGO (0x0200)
  125. #define DEBUG_TAGS (0x0400)
  126. #define DEBUG_SCATTER (0x0800)
  127. #define DEBUG_IC (0x1000)
  128. /*
  129. ** Enable/Disable debug messages.
  130. ** Can be changed at runtime too.
  131. */
  132. #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
  133. static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
  134. #define DEBUG_FLAGS ncr_debug
  135. #else
  136. #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
  137. #endif
  138. /*
  139. * Locally used status flag
  140. */
  141. #define SAM_STAT_ILLEGAL 0xff
  142. static inline struct list_head *ncr_list_pop(struct list_head *head)
  143. {
  144. if (!list_empty(head)) {
  145. struct list_head *elem = head->next;
  146. list_del(elem);
  147. return elem;
  148. }
  149. return NULL;
  150. }
  151. /*==========================================================
  152. **
  153. ** Simple power of two buddy-like allocator.
  154. **
  155. ** This simple code is not intended to be fast, but to
  156. ** provide power of 2 aligned memory allocations.
  157. ** Since the SCRIPTS processor only supplies 8 bit
  158. ** arithmetic, this allocator allows simple and fast
  159. ** address calculations from the SCRIPTS code.
  160. ** In addition, cache line alignment is guaranteed for
  161. ** power of 2 cache line size.
  162. ** Enhanced in linux-2.3.44 to provide a memory pool
  163. ** per pcidev to support dynamic dma mapping. (I would
  164. ** have preferred a real bus abstraction, btw).
  165. **
  166. **==========================================================
  167. */
  168. #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
  169. #if PAGE_SIZE >= 8192
  170. #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
  171. #else
  172. #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
  173. #endif
  174. #define MEMO_FREE_UNUSED /* Free unused pages immediately */
  175. #define MEMO_WARN 1
  176. #define MEMO_GFP_FLAGS GFP_ATOMIC
  177. #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
  178. #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
  179. #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
  180. typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
  181. typedef struct device *m_bush_t; /* Something that addresses DMAable */
  182. typedef struct m_link { /* Link between free memory chunks */
  183. struct m_link *next;
  184. } m_link_s;
  185. typedef struct m_vtob { /* Virtual to Bus address translation */
  186. struct m_vtob *next;
  187. m_addr_t vaddr;
  188. m_addr_t baddr;
  189. } m_vtob_s;
  190. #define VTOB_HASH_SHIFT 5
  191. #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
  192. #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
  193. #define VTOB_HASH_CODE(m) \
  194. ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
  195. typedef struct m_pool { /* Memory pool of a given kind */
  196. m_bush_t bush;
  197. m_addr_t (*getp)(struct m_pool *);
  198. void (*freep)(struct m_pool *, m_addr_t);
  199. int nump;
  200. m_vtob_s *(vtob[VTOB_HASH_SIZE]);
  201. struct m_pool *next;
  202. struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
  203. } m_pool_s;
  204. static void *___m_alloc(m_pool_s *mp, int size)
  205. {
  206. int i = 0;
  207. int s = (1 << MEMO_SHIFT);
  208. int j;
  209. m_addr_t a;
  210. m_link_s *h = mp->h;
  211. if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
  212. return NULL;
  213. while (size > s) {
  214. s <<= 1;
  215. ++i;
  216. }
  217. j = i;
  218. while (!h[j].next) {
  219. if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
  220. h[j].next = (m_link_s *)mp->getp(mp);
  221. if (h[j].next)
  222. h[j].next->next = NULL;
  223. break;
  224. }
  225. ++j;
  226. s <<= 1;
  227. }
  228. a = (m_addr_t) h[j].next;
  229. if (a) {
  230. h[j].next = h[j].next->next;
  231. while (j > i) {
  232. j -= 1;
  233. s >>= 1;
  234. h[j].next = (m_link_s *) (a+s);
  235. h[j].next->next = NULL;
  236. }
  237. }
  238. #ifdef DEBUG
  239. printk("___m_alloc(%d) = %p\n", size, (void *) a);
  240. #endif
  241. return (void *) a;
  242. }
  243. static void ___m_free(m_pool_s *mp, void *ptr, int size)
  244. {
  245. int i = 0;
  246. int s = (1 << MEMO_SHIFT);
  247. m_link_s *q;
  248. m_addr_t a, b;
  249. m_link_s *h = mp->h;
  250. #ifdef DEBUG
  251. printk("___m_free(%p, %d)\n", ptr, size);
  252. #endif
  253. if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
  254. return;
  255. while (size > s) {
  256. s <<= 1;
  257. ++i;
  258. }
  259. a = (m_addr_t) ptr;
  260. while (1) {
  261. #ifdef MEMO_FREE_UNUSED
  262. if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
  263. mp->freep(mp, a);
  264. break;
  265. }
  266. #endif
  267. b = a ^ s;
  268. q = &h[i];
  269. while (q->next && q->next != (m_link_s *) b) {
  270. q = q->next;
  271. }
  272. if (!q->next) {
  273. ((m_link_s *) a)->next = h[i].next;
  274. h[i].next = (m_link_s *) a;
  275. break;
  276. }
  277. q->next = q->next->next;
  278. a = a & b;
  279. s <<= 1;
  280. ++i;
  281. }
  282. }
  283. static DEFINE_SPINLOCK(ncr53c8xx_lock);
  284. static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
  285. {
  286. void *p;
  287. p = ___m_alloc(mp, size);
  288. if (DEBUG_FLAGS & DEBUG_ALLOC)
  289. printk ("new %-10s[%4d] @%p.\n", name, size, p);
  290. if (p)
  291. memset(p, 0, size);
  292. else if (uflags & MEMO_WARN)
  293. printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
  294. return p;
  295. }
  296. #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
  297. static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
  298. {
  299. if (DEBUG_FLAGS & DEBUG_ALLOC)
  300. printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
  301. ___m_free(mp, ptr, size);
  302. }
  303. /*
  304. * With pci bus iommu support, we use a default pool of unmapped memory
  305. * for memory we donnot need to DMA from/to and one pool per pcidev for
  306. * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
  307. */
  308. static m_addr_t ___mp0_getp(m_pool_s *mp)
  309. {
  310. m_addr_t m = __get_free_pages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER);
  311. if (m)
  312. ++mp->nump;
  313. return m;
  314. }
  315. static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
  316. {
  317. free_pages(m, MEMO_PAGE_ORDER);
  318. --mp->nump;
  319. }
  320. static m_pool_s mp0 = {NULL, ___mp0_getp, ___mp0_freep};
  321. /*
  322. * DMAable pools.
  323. */
  324. /*
  325. * With pci bus iommu support, we maintain one pool per pcidev and a
  326. * hashed reverse table for virtual to bus physical address translations.
  327. */
  328. static m_addr_t ___dma_getp(m_pool_s *mp)
  329. {
  330. m_addr_t vp;
  331. m_vtob_s *vbp;
  332. vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
  333. if (vbp) {
  334. dma_addr_t daddr;
  335. vp = (m_addr_t) dma_alloc_coherent(mp->bush,
  336. PAGE_SIZE<<MEMO_PAGE_ORDER,
  337. &daddr, GFP_ATOMIC);
  338. if (vp) {
  339. int hc = VTOB_HASH_CODE(vp);
  340. vbp->vaddr = vp;
  341. vbp->baddr = daddr;
  342. vbp->next = mp->vtob[hc];
  343. mp->vtob[hc] = vbp;
  344. ++mp->nump;
  345. return vp;
  346. }
  347. }
  348. if (vbp)
  349. __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
  350. return 0;
  351. }
  352. static void ___dma_freep(m_pool_s *mp, m_addr_t m)
  353. {
  354. m_vtob_s **vbpp, *vbp;
  355. int hc = VTOB_HASH_CODE(m);
  356. vbpp = &mp->vtob[hc];
  357. while (*vbpp && (*vbpp)->vaddr != m)
  358. vbpp = &(*vbpp)->next;
  359. if (*vbpp) {
  360. vbp = *vbpp;
  361. *vbpp = (*vbpp)->next;
  362. dma_free_coherent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
  363. (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
  364. __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
  365. --mp->nump;
  366. }
  367. }
  368. static inline m_pool_s *___get_dma_pool(m_bush_t bush)
  369. {
  370. m_pool_s *mp;
  371. for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
  372. return mp;
  373. }
  374. static m_pool_s *___cre_dma_pool(m_bush_t bush)
  375. {
  376. m_pool_s *mp;
  377. mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
  378. if (mp) {
  379. memset(mp, 0, sizeof(*mp));
  380. mp->bush = bush;
  381. mp->getp = ___dma_getp;
  382. mp->freep = ___dma_freep;
  383. mp->next = mp0.next;
  384. mp0.next = mp;
  385. }
  386. return mp;
  387. }
  388. static void ___del_dma_pool(m_pool_s *p)
  389. {
  390. struct m_pool **pp = &mp0.next;
  391. while (*pp && *pp != p)
  392. pp = &(*pp)->next;
  393. if (*pp) {
  394. *pp = (*pp)->next;
  395. __m_free(&mp0, p, sizeof(*p), "MPOOL");
  396. }
  397. }
  398. static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
  399. {
  400. u_long flags;
  401. struct m_pool *mp;
  402. void *m = NULL;
  403. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  404. mp = ___get_dma_pool(bush);
  405. if (!mp)
  406. mp = ___cre_dma_pool(bush);
  407. if (mp)
  408. m = __m_calloc(mp, size, name);
  409. if (mp && !mp->nump)
  410. ___del_dma_pool(mp);
  411. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  412. return m;
  413. }
  414. static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
  415. {
  416. u_long flags;
  417. struct m_pool *mp;
  418. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  419. mp = ___get_dma_pool(bush);
  420. if (mp)
  421. __m_free(mp, m, size, name);
  422. if (mp && !mp->nump)
  423. ___del_dma_pool(mp);
  424. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  425. }
  426. static m_addr_t __vtobus(m_bush_t bush, void *m)
  427. {
  428. u_long flags;
  429. m_pool_s *mp;
  430. int hc = VTOB_HASH_CODE(m);
  431. m_vtob_s *vp = NULL;
  432. m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
  433. spin_lock_irqsave(&ncr53c8xx_lock, flags);
  434. mp = ___get_dma_pool(bush);
  435. if (mp) {
  436. vp = mp->vtob[hc];
  437. while (vp && (m_addr_t) vp->vaddr != a)
  438. vp = vp->next;
  439. }
  440. spin_unlock_irqrestore(&ncr53c8xx_lock, flags);
  441. return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
  442. }
  443. #define _m_calloc_dma(np, s, n) __m_calloc_dma(np->dev, s, n)
  444. #define _m_free_dma(np, p, s, n) __m_free_dma(np->dev, p, s, n)
  445. #define m_calloc_dma(s, n) _m_calloc_dma(np, s, n)
  446. #define m_free_dma(p, s, n) _m_free_dma(np, p, s, n)
  447. #define _vtobus(np, p) __vtobus(np->dev, p)
  448. #define vtobus(p) _vtobus(np, p)
  449. /*
  450. * Deal with DMA mapping/unmapping.
  451. */
  452. static void __unmap_scsi_data(struct device *dev, struct scsi_cmnd *cmd)
  453. {
  454. struct ncr_cmd_priv *cmd_priv = scsi_cmd_priv(cmd);
  455. switch(cmd_priv->data_mapped) {
  456. case 2:
  457. scsi_dma_unmap(cmd);
  458. break;
  459. }
  460. cmd_priv->data_mapped = 0;
  461. }
  462. static int __map_scsi_sg_data(struct device *dev, struct scsi_cmnd *cmd)
  463. {
  464. struct ncr_cmd_priv *cmd_priv = scsi_cmd_priv(cmd);
  465. int use_sg;
  466. use_sg = scsi_dma_map(cmd);
  467. if (!use_sg)
  468. return 0;
  469. cmd_priv->data_mapped = 2;
  470. cmd_priv->data_mapping = use_sg;
  471. return use_sg;
  472. }
  473. #define unmap_scsi_data(np, cmd) __unmap_scsi_data(np->dev, cmd)
  474. #define map_scsi_sg_data(np, cmd) __map_scsi_sg_data(np->dev, cmd)
  475. /*==========================================================
  476. **
  477. ** Driver setup.
  478. **
  479. ** This structure is initialized from linux config
  480. ** options. It can be overridden at boot-up by the boot
  481. ** command line.
  482. **
  483. **==========================================================
  484. */
  485. static struct ncr_driver_setup
  486. driver_setup = SCSI_NCR_DRIVER_SETUP;
  487. #ifndef MODULE
  488. #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
  489. static struct ncr_driver_setup
  490. driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
  491. #endif
  492. #endif /* !MODULE */
  493. #define initverbose (driver_setup.verbose)
  494. #define bootverbose (np->verbose)
  495. /*===================================================================
  496. **
  497. ** Driver setup from the boot command line
  498. **
  499. **===================================================================
  500. */
  501. #ifdef MODULE
  502. #define ARG_SEP ' '
  503. #else
  504. #define ARG_SEP ','
  505. #endif
  506. #define OPT_TAGS 1
  507. #define OPT_MASTER_PARITY 2
  508. #define OPT_SCSI_PARITY 3
  509. #define OPT_DISCONNECTION 4
  510. #define OPT_SPECIAL_FEATURES 5
  511. #define OPT_UNUSED_1 6
  512. #define OPT_FORCE_SYNC_NEGO 7
  513. #define OPT_REVERSE_PROBE 8
  514. #define OPT_DEFAULT_SYNC 9
  515. #define OPT_VERBOSE 10
  516. #define OPT_DEBUG 11
  517. #define OPT_BURST_MAX 12
  518. #define OPT_LED_PIN 13
  519. #define OPT_MAX_WIDE 14
  520. #define OPT_SETTLE_DELAY 15
  521. #define OPT_DIFF_SUPPORT 16
  522. #define OPT_IRQM 17
  523. #define OPT_PCI_FIX_UP 18
  524. #define OPT_BUS_CHECK 19
  525. #define OPT_OPTIMIZE 20
  526. #define OPT_RECOVERY 21
  527. #define OPT_SAFE_SETUP 22
  528. #define OPT_USE_NVRAM 23
  529. #define OPT_EXCLUDE 24
  530. #define OPT_HOST_ID 25
  531. #ifdef SCSI_NCR_IARB_SUPPORT
  532. #define OPT_IARB 26
  533. #endif
  534. #ifdef MODULE
  535. #define ARG_SEP ' '
  536. #else
  537. #define ARG_SEP ','
  538. #endif
  539. #ifndef MODULE
  540. static char setup_token[] __initdata =
  541. "tags:" "mpar:"
  542. "spar:" "disc:"
  543. "specf:" "ultra:"
  544. "fsn:" "revprob:"
  545. "sync:" "verb:"
  546. "debug:" "burst:"
  547. "led:" "wide:"
  548. "settle:" "diff:"
  549. "irqm:" "pcifix:"
  550. "buschk:" "optim:"
  551. "recovery:"
  552. "safe:" "nvram:"
  553. "excl:" "hostid:"
  554. #ifdef SCSI_NCR_IARB_SUPPORT
  555. "iarb:"
  556. #endif
  557. ; /* DONNOT REMOVE THIS ';' */
  558. static int __init get_setup_token(char *p)
  559. {
  560. char *cur = setup_token;
  561. char *pc;
  562. int i = 0;
  563. while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
  564. ++pc;
  565. ++i;
  566. if (!strncmp(p, cur, pc - cur))
  567. return i;
  568. cur = pc;
  569. }
  570. return 0;
  571. }
  572. static int __init sym53c8xx__setup(char *str)
  573. {
  574. #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
  575. char *cur = str;
  576. char *pc, *pv;
  577. int i, val, c;
  578. int xi = 0;
  579. while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
  580. char *pe;
  581. val = 0;
  582. pv = pc;
  583. c = *++pv;
  584. if (c == 'n')
  585. val = 0;
  586. else if (c == 'y')
  587. val = 1;
  588. else
  589. val = (int) simple_strtoul(pv, &pe, 0);
  590. switch (get_setup_token(cur)) {
  591. case OPT_TAGS:
  592. driver_setup.default_tags = val;
  593. if (pe && *pe == '/') {
  594. i = 0;
  595. while (*pe && *pe != ARG_SEP &&
  596. i < sizeof(driver_setup.tag_ctrl)-1) {
  597. driver_setup.tag_ctrl[i++] = *pe++;
  598. }
  599. driver_setup.tag_ctrl[i] = '\0';
  600. }
  601. break;
  602. case OPT_MASTER_PARITY:
  603. driver_setup.master_parity = val;
  604. break;
  605. case OPT_SCSI_PARITY:
  606. driver_setup.scsi_parity = val;
  607. break;
  608. case OPT_DISCONNECTION:
  609. driver_setup.disconnection = val;
  610. break;
  611. case OPT_SPECIAL_FEATURES:
  612. driver_setup.special_features = val;
  613. break;
  614. case OPT_FORCE_SYNC_NEGO:
  615. driver_setup.force_sync_nego = val;
  616. break;
  617. case OPT_REVERSE_PROBE:
  618. driver_setup.reverse_probe = val;
  619. break;
  620. case OPT_DEFAULT_SYNC:
  621. driver_setup.default_sync = val;
  622. break;
  623. case OPT_VERBOSE:
  624. driver_setup.verbose = val;
  625. break;
  626. case OPT_DEBUG:
  627. driver_setup.debug = val;
  628. break;
  629. case OPT_BURST_MAX:
  630. driver_setup.burst_max = val;
  631. break;
  632. case OPT_LED_PIN:
  633. driver_setup.led_pin = val;
  634. break;
  635. case OPT_MAX_WIDE:
  636. driver_setup.max_wide = val? 1:0;
  637. break;
  638. case OPT_SETTLE_DELAY:
  639. driver_setup.settle_delay = val;
  640. break;
  641. case OPT_DIFF_SUPPORT:
  642. driver_setup.diff_support = val;
  643. break;
  644. case OPT_IRQM:
  645. driver_setup.irqm = val;
  646. break;
  647. case OPT_PCI_FIX_UP:
  648. driver_setup.pci_fix_up = val;
  649. break;
  650. case OPT_BUS_CHECK:
  651. driver_setup.bus_check = val;
  652. break;
  653. case OPT_OPTIMIZE:
  654. driver_setup.optimize = val;
  655. break;
  656. case OPT_RECOVERY:
  657. driver_setup.recovery = val;
  658. break;
  659. case OPT_USE_NVRAM:
  660. driver_setup.use_nvram = val;
  661. break;
  662. case OPT_SAFE_SETUP:
  663. memcpy(&driver_setup, &driver_safe_setup,
  664. sizeof(driver_setup));
  665. break;
  666. case OPT_EXCLUDE:
  667. if (xi < SCSI_NCR_MAX_EXCLUDES)
  668. driver_setup.excludes[xi++] = val;
  669. break;
  670. case OPT_HOST_ID:
  671. driver_setup.host_id = val;
  672. break;
  673. #ifdef SCSI_NCR_IARB_SUPPORT
  674. case OPT_IARB:
  675. driver_setup.iarb = val;
  676. break;
  677. #endif
  678. default:
  679. printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
  680. break;
  681. }
  682. if ((cur = strchr(cur, ARG_SEP)) != NULL)
  683. ++cur;
  684. }
  685. #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
  686. return 1;
  687. }
  688. #endif /* !MODULE */
  689. /*===================================================================
  690. **
  691. ** Get device queue depth from boot command line.
  692. **
  693. **===================================================================
  694. */
  695. #define DEF_DEPTH (driver_setup.default_tags)
  696. #define ALL_TARGETS -2
  697. #define NO_TARGET -1
  698. #define ALL_LUNS -2
  699. #define NO_LUN -1
  700. static int device_queue_depth(int unit, int target, int lun)
  701. {
  702. int c, h, t, u, v;
  703. char *p = driver_setup.tag_ctrl;
  704. char *ep;
  705. h = -1;
  706. t = NO_TARGET;
  707. u = NO_LUN;
  708. while ((c = *p++) != 0) {
  709. v = simple_strtoul(p, &ep, 0);
  710. switch(c) {
  711. case '/':
  712. ++h;
  713. t = ALL_TARGETS;
  714. u = ALL_LUNS;
  715. break;
  716. case 't':
  717. if (t != target)
  718. t = (target == v) ? v : NO_TARGET;
  719. u = ALL_LUNS;
  720. break;
  721. case 'u':
  722. if (u != lun)
  723. u = (lun == v) ? v : NO_LUN;
  724. break;
  725. case 'q':
  726. if (h == unit &&
  727. (t == ALL_TARGETS || t == target) &&
  728. (u == ALL_LUNS || u == lun))
  729. return v;
  730. break;
  731. case '-':
  732. t = ALL_TARGETS;
  733. u = ALL_LUNS;
  734. break;
  735. default:
  736. break;
  737. }
  738. p = ep;
  739. }
  740. return DEF_DEPTH;
  741. }
  742. /*==========================================================
  743. **
  744. ** The CCB done queue uses an array of CCB virtual
  745. ** addresses. Empty entries are flagged using the bogus
  746. ** virtual address 0xffffffff.
  747. **
  748. ** Since PCI ensures that only aligned DWORDs are accessed
  749. ** atomically, 64 bit little-endian architecture requires
  750. ** to test the high order DWORD of the entry to determine
  751. ** if it is empty or valid.
  752. **
  753. ** BTW, I will make things differently as soon as I will
  754. ** have a better idea, but this is simple and should work.
  755. **
  756. **==========================================================
  757. */
  758. #define SCSI_NCR_CCB_DONE_SUPPORT
  759. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  760. #define MAX_DONE 24
  761. #define CCB_DONE_EMPTY 0xffffffffUL
  762. /* All 32 bit architectures */
  763. #if BITS_PER_LONG == 32
  764. #define CCB_DONE_VALID(cp) (((u_long) cp) != CCB_DONE_EMPTY)
  765. /* All > 32 bit (64 bit) architectures regardless endian-ness */
  766. #else
  767. #define CCB_DONE_VALID(cp) \
  768. ((((u_long) cp) & 0xffffffff00000000ul) && \
  769. (((u_long) cp) & 0xfffffffful) != CCB_DONE_EMPTY)
  770. #endif
  771. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  772. /*==========================================================
  773. **
  774. ** Configuration and Debugging
  775. **
  776. **==========================================================
  777. */
  778. /*
  779. ** SCSI address of this device.
  780. ** The boot routines should have set it.
  781. ** If not, use this.
  782. */
  783. #ifndef SCSI_NCR_MYADDR
  784. #define SCSI_NCR_MYADDR (7)
  785. #endif
  786. /*
  787. ** The maximum number of tags per logic unit.
  788. ** Used only for disk devices that support tags.
  789. */
  790. #ifndef SCSI_NCR_MAX_TAGS
  791. #define SCSI_NCR_MAX_TAGS (8)
  792. #endif
  793. /*
  794. ** TAGS are actually limited to 64 tags/lun.
  795. ** We need to deal with power of 2, for alignment constraints.
  796. */
  797. #if SCSI_NCR_MAX_TAGS > 64
  798. #define MAX_TAGS (64)
  799. #else
  800. #define MAX_TAGS SCSI_NCR_MAX_TAGS
  801. #endif
  802. #define NO_TAG (255)
  803. /*
  804. ** Choose appropriate type for tag bitmap.
  805. */
  806. #if MAX_TAGS > 32
  807. typedef u64 tagmap_t;
  808. #else
  809. typedef u32 tagmap_t;
  810. #endif
  811. /*
  812. ** Number of targets supported by the driver.
  813. ** n permits target numbers 0..n-1.
  814. ** Default is 16, meaning targets #0..#15.
  815. ** #7 .. is myself.
  816. */
  817. #ifdef SCSI_NCR_MAX_TARGET
  818. #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
  819. #else
  820. #define MAX_TARGET (16)
  821. #endif
  822. /*
  823. ** Number of logic units supported by the driver.
  824. ** n enables logic unit numbers 0..n-1.
  825. ** The common SCSI devices require only
  826. ** one lun, so take 1 as the default.
  827. */
  828. #ifdef SCSI_NCR_MAX_LUN
  829. #define MAX_LUN SCSI_NCR_MAX_LUN
  830. #else
  831. #define MAX_LUN (1)
  832. #endif
  833. /*
  834. ** Asynchronous pre-scaler (ns). Shall be 40
  835. */
  836. #ifndef SCSI_NCR_MIN_ASYNC
  837. #define SCSI_NCR_MIN_ASYNC (40)
  838. #endif
  839. /*
  840. ** The maximum number of jobs scheduled for starting.
  841. ** There should be one slot per target, and one slot
  842. ** for each tag of each target in use.
  843. ** The calculation below is actually quite silly ...
  844. */
  845. #ifdef SCSI_NCR_CAN_QUEUE
  846. #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
  847. #else
  848. #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
  849. #endif
  850. /*
  851. ** We limit the max number of pending IO to 250.
  852. ** since we donnot want to allocate more than 1
  853. ** PAGE for 'scripth'.
  854. */
  855. #if MAX_START > 250
  856. #undef MAX_START
  857. #define MAX_START 250
  858. #endif
  859. /*
  860. ** The maximum number of segments a transfer is split into.
  861. ** We support up to 127 segments for both read and write.
  862. ** The data scripts are broken into 2 sub-scripts.
  863. ** 80 (MAX_SCATTERL) segments are moved from a sub-script
  864. ** in on-chip RAM. This makes data transfers shorter than
  865. ** 80k (assuming 1k fs) as fast as possible.
  866. */
  867. #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
  868. #if (MAX_SCATTER > 80)
  869. #define MAX_SCATTERL 80
  870. #define MAX_SCATTERH (MAX_SCATTER - MAX_SCATTERL)
  871. #else
  872. #define MAX_SCATTERL (MAX_SCATTER-1)
  873. #define MAX_SCATTERH 1
  874. #endif
  875. /*
  876. ** other
  877. */
  878. #define NCR_SNOOP_TIMEOUT (1000000)
  879. /*
  880. ** Other definitions
  881. */
  882. #define initverbose (driver_setup.verbose)
  883. #define bootverbose (np->verbose)
  884. /*==========================================================
  885. **
  886. ** Command control block states.
  887. **
  888. **==========================================================
  889. */
  890. #define HS_IDLE (0)
  891. #define HS_BUSY (1)
  892. #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
  893. #define HS_DISCONNECT (3) /* Disconnected by target */
  894. #define HS_DONEMASK (0x80)
  895. #define HS_COMPLETE (4|HS_DONEMASK)
  896. #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
  897. #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
  898. #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
  899. #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
  900. #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
  901. #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
  902. /*
  903. ** Invalid host status values used by the SCRIPTS processor
  904. ** when the nexus is not fully identified.
  905. ** Shall never appear in a CCB.
  906. */
  907. #define HS_INVALMASK (0x40)
  908. #define HS_SELECTING (0|HS_INVALMASK)
  909. #define HS_IN_RESELECT (1|HS_INVALMASK)
  910. #define HS_STARTING (2|HS_INVALMASK)
  911. /*
  912. ** Flags set by the SCRIPT processor for commands
  913. ** that have been skipped.
  914. */
  915. #define HS_SKIPMASK (0x20)
  916. /*==========================================================
  917. **
  918. ** Software Interrupt Codes
  919. **
  920. **==========================================================
  921. */
  922. #define SIR_BAD_STATUS (1)
  923. #define SIR_XXXXXXXXXX (2)
  924. #define SIR_NEGO_SYNC (3)
  925. #define SIR_NEGO_WIDE (4)
  926. #define SIR_NEGO_FAILED (5)
  927. #define SIR_NEGO_PROTO (6)
  928. #define SIR_REJECT_RECEIVED (7)
  929. #define SIR_REJECT_SENT (8)
  930. #define SIR_IGN_RESIDUE (9)
  931. #define SIR_MISSING_SAVE (10)
  932. #define SIR_RESEL_NO_MSG_IN (11)
  933. #define SIR_RESEL_NO_IDENTIFY (12)
  934. #define SIR_RESEL_BAD_LUN (13)
  935. #define SIR_RESEL_BAD_TARGET (14)
  936. #define SIR_RESEL_BAD_I_T_L (15)
  937. #define SIR_RESEL_BAD_I_T_L_Q (16)
  938. #define SIR_DONE_OVERFLOW (17)
  939. #define SIR_INTFLY (18)
  940. #define SIR_MAX (18)
  941. /*==========================================================
  942. **
  943. ** Extended error codes.
  944. ** xerr_status field of struct ccb.
  945. **
  946. **==========================================================
  947. */
  948. #define XE_OK (0)
  949. #define XE_EXTRA_DATA (1) /* unexpected data phase */
  950. #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
  951. /*==========================================================
  952. **
  953. ** Negotiation status.
  954. ** nego_status field of struct ccb.
  955. **
  956. **==========================================================
  957. */
  958. #define NS_NOCHANGE (0)
  959. #define NS_SYNC (1)
  960. #define NS_WIDE (2)
  961. #define NS_PPR (4)
  962. /*==========================================================
  963. **
  964. ** Misc.
  965. **
  966. **==========================================================
  967. */
  968. #define CCB_MAGIC (0xf2691ad2)
  969. /*==========================================================
  970. **
  971. ** Declaration of structs.
  972. **
  973. **==========================================================
  974. */
  975. static struct scsi_transport_template *ncr53c8xx_transport_template = NULL;
  976. struct tcb;
  977. struct lcb;
  978. struct ccb;
  979. struct ncb;
  980. struct script;
  981. struct link {
  982. ncrcmd l_cmd;
  983. ncrcmd l_paddr;
  984. };
  985. struct usrcmd {
  986. u_long target;
  987. u_long lun;
  988. u_long data;
  989. u_long cmd;
  990. };
  991. #define UC_SETSYNC 10
  992. #define UC_SETTAGS 11
  993. #define UC_SETDEBUG 12
  994. #define UC_SETORDER 13
  995. #define UC_SETWIDE 14
  996. #define UC_SETFLAG 15
  997. #define UC_SETVERBOSE 17
  998. #define UF_TRACE (0x01)
  999. #define UF_NODISC (0x02)
  1000. #define UF_NOSCAN (0x04)
  1001. /*========================================================================
  1002. **
  1003. ** Declaration of structs: target control block
  1004. **
  1005. **========================================================================
  1006. */
  1007. struct tcb {
  1008. /*----------------------------------------------------------------
  1009. ** During reselection the ncr jumps to this point with SFBR
  1010. ** set to the encoded target number with bit 7 set.
  1011. ** if it's not this target, jump to the next.
  1012. **
  1013. ** JUMP IF (SFBR != #target#), @(next tcb)
  1014. **----------------------------------------------------------------
  1015. */
  1016. struct link jump_tcb;
  1017. /*----------------------------------------------------------------
  1018. ** Load the actual values for the sxfer and the scntl3
  1019. ** register (sync/wide mode).
  1020. **
  1021. ** SCR_COPY (1), @(sval field of this tcb), @(sxfer register)
  1022. ** SCR_COPY (1), @(wval field of this tcb), @(scntl3 register)
  1023. **----------------------------------------------------------------
  1024. */
  1025. ncrcmd getscr[6];
  1026. /*----------------------------------------------------------------
  1027. ** Get the IDENTIFY message and load the LUN to SFBR.
  1028. **
  1029. ** CALL, <RESEL_LUN>
  1030. **----------------------------------------------------------------
  1031. */
  1032. struct link call_lun;
  1033. /*----------------------------------------------------------------
  1034. ** Now look for the right lun.
  1035. **
  1036. ** For i = 0 to 3
  1037. ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(first lcb mod. i)
  1038. **
  1039. ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
  1040. ** It is kind of hashcoding.
  1041. **----------------------------------------------------------------
  1042. */
  1043. struct link jump_lcb[4]; /* JUMPs for reselection */
  1044. struct lcb * lp[MAX_LUN]; /* The lcb's of this tcb */
  1045. /*----------------------------------------------------------------
  1046. ** Pointer to the ccb used for negotiation.
  1047. ** Prevent from starting a negotiation for all queued commands
  1048. ** when tagged command queuing is enabled.
  1049. **----------------------------------------------------------------
  1050. */
  1051. struct ccb * nego_cp;
  1052. /*----------------------------------------------------------------
  1053. ** statistical data
  1054. **----------------------------------------------------------------
  1055. */
  1056. u_long transfers;
  1057. u_long bytes;
  1058. /*----------------------------------------------------------------
  1059. ** negotiation of wide and synch transfer and device quirks.
  1060. **----------------------------------------------------------------
  1061. */
  1062. #ifdef SCSI_NCR_BIG_ENDIAN
  1063. /*0*/ u16 period;
  1064. /*2*/ u_char sval;
  1065. /*3*/ u_char minsync;
  1066. /*0*/ u_char wval;
  1067. /*1*/ u_char widedone;
  1068. /*2*/ u_char quirks;
  1069. /*3*/ u_char maxoffs;
  1070. #else
  1071. /*0*/ u_char minsync;
  1072. /*1*/ u_char sval;
  1073. /*2*/ u16 period;
  1074. /*0*/ u_char maxoffs;
  1075. /*1*/ u_char quirks;
  1076. /*2*/ u_char widedone;
  1077. /*3*/ u_char wval;
  1078. #endif
  1079. /* User settable limits and options. */
  1080. u_char usrsync;
  1081. u_char usrwide;
  1082. u_char usrtags;
  1083. u_char usrflag;
  1084. struct scsi_target *starget;
  1085. };
  1086. /*========================================================================
  1087. **
  1088. ** Declaration of structs: lun control block
  1089. **
  1090. **========================================================================
  1091. */
  1092. struct lcb {
  1093. /*----------------------------------------------------------------
  1094. ** During reselection the ncr jumps to this point
  1095. ** with SFBR set to the "Identify" message.
  1096. ** if it's not this lun, jump to the next.
  1097. **
  1098. ** JUMP IF (SFBR != #lun#), @(next lcb of this target)
  1099. **
  1100. ** It is this lun. Load TEMP with the nexus jumps table
  1101. ** address and jump to RESEL_TAG (or RESEL_NOTAG).
  1102. **
  1103. ** SCR_COPY (4), p_jump_ccb, TEMP,
  1104. ** SCR_JUMP, <RESEL_TAG>
  1105. **----------------------------------------------------------------
  1106. */
  1107. struct link jump_lcb;
  1108. ncrcmd load_jump_ccb[3];
  1109. struct link jump_tag;
  1110. ncrcmd p_jump_ccb; /* Jump table bus address */
  1111. /*----------------------------------------------------------------
  1112. ** Jump table used by the script processor to directly jump
  1113. ** to the CCB corresponding to the reselected nexus.
  1114. ** Address is allocated on 256 bytes boundary in order to
  1115. ** allow 8 bit calculation of the tag jump entry for up to
  1116. ** 64 possible tags.
  1117. **----------------------------------------------------------------
  1118. */
  1119. u32 jump_ccb_0; /* Default table if no tags */
  1120. u32 *jump_ccb; /* Virtual address */
  1121. /*----------------------------------------------------------------
  1122. ** CCB queue management.
  1123. **----------------------------------------------------------------
  1124. */
  1125. struct list_head free_ccbq; /* Queue of available CCBs */
  1126. struct list_head busy_ccbq; /* Queue of busy CCBs */
  1127. struct list_head wait_ccbq; /* Queue of waiting for IO CCBs */
  1128. struct list_head skip_ccbq; /* Queue of skipped CCBs */
  1129. u_char actccbs; /* Number of allocated CCBs */
  1130. u_char busyccbs; /* CCBs busy for this lun */
  1131. u_char queuedccbs; /* CCBs queued to the controller*/
  1132. u_char queuedepth; /* Queue depth for this lun */
  1133. u_char scdev_depth; /* SCSI device queue depth */
  1134. u_char maxnxs; /* Max possible nexuses */
  1135. /*----------------------------------------------------------------
  1136. ** Control of tagged command queuing.
  1137. ** Tags allocation is performed using a circular buffer.
  1138. ** This avoids using a loop for tag allocation.
  1139. **----------------------------------------------------------------
  1140. */
  1141. u_char ia_tag; /* Allocation index */
  1142. u_char if_tag; /* Freeing index */
  1143. u_char cb_tags[MAX_TAGS]; /* Circular tags buffer */
  1144. u_char usetags; /* Command queuing is active */
  1145. u_char maxtags; /* Max nr of tags asked by user */
  1146. u_char numtags; /* Current number of tags */
  1147. /*----------------------------------------------------------------
  1148. ** QUEUE FULL control and ORDERED tag control.
  1149. **----------------------------------------------------------------
  1150. */
  1151. /*----------------------------------------------------------------
  1152. ** QUEUE FULL and ORDERED tag control.
  1153. **----------------------------------------------------------------
  1154. */
  1155. u16 num_good; /* Nr of GOOD since QUEUE FULL */
  1156. tagmap_t tags_umap; /* Used tags bitmap */
  1157. tagmap_t tags_smap; /* Tags in use at 'tag_stime' */
  1158. u_long tags_stime; /* Last time we set smap=umap */
  1159. struct ccb * held_ccb; /* CCB held for QUEUE FULL */
  1160. };
  1161. /*========================================================================
  1162. **
  1163. ** Declaration of structs: the launch script.
  1164. **
  1165. **========================================================================
  1166. **
  1167. ** It is part of the CCB and is called by the scripts processor to
  1168. ** start or restart the data structure (nexus).
  1169. ** This 6 DWORDs mini script makes use of prefetching.
  1170. **
  1171. **------------------------------------------------------------------------
  1172. */
  1173. struct launch {
  1174. /*----------------------------------------------------------------
  1175. ** SCR_COPY(4), @(p_phys), @(dsa register)
  1176. ** SCR_JUMP, @(scheduler_point)
  1177. **----------------------------------------------------------------
  1178. */
  1179. ncrcmd setup_dsa[3]; /* Copy 'phys' address to dsa */
  1180. struct link schedule; /* Jump to scheduler point */
  1181. ncrcmd p_phys; /* 'phys' header bus address */
  1182. };
  1183. /*========================================================================
  1184. **
  1185. ** Declaration of structs: global HEADER.
  1186. **
  1187. **========================================================================
  1188. **
  1189. ** This substructure is copied from the ccb to a global address after
  1190. ** selection (or reselection) and copied back before disconnect.
  1191. **
  1192. ** These fields are accessible to the script processor.
  1193. **
  1194. **------------------------------------------------------------------------
  1195. */
  1196. struct head {
  1197. /*----------------------------------------------------------------
  1198. ** Saved data pointer.
  1199. ** Points to the position in the script responsible for the
  1200. ** actual transfer transfer of data.
  1201. ** It's written after reception of a SAVE_DATA_POINTER message.
  1202. ** The goalpointer points after the last transfer command.
  1203. **----------------------------------------------------------------
  1204. */
  1205. u32 savep;
  1206. u32 lastp;
  1207. u32 goalp;
  1208. /*----------------------------------------------------------------
  1209. ** Alternate data pointer.
  1210. ** They are copied back to savep/lastp/goalp by the SCRIPTS
  1211. ** when the direction is unknown and the device claims data out.
  1212. **----------------------------------------------------------------
  1213. */
  1214. u32 wlastp;
  1215. u32 wgoalp;
  1216. /*----------------------------------------------------------------
  1217. ** The virtual address of the ccb containing this header.
  1218. **----------------------------------------------------------------
  1219. */
  1220. struct ccb * cp;
  1221. /*----------------------------------------------------------------
  1222. ** Status fields.
  1223. **----------------------------------------------------------------
  1224. */
  1225. u_char scr_st[4]; /* script status */
  1226. u_char status[4]; /* host status. must be the */
  1227. /* last DWORD of the header. */
  1228. };
  1229. /*
  1230. ** The status bytes are used by the host and the script processor.
  1231. **
  1232. ** The byte corresponding to the host_status must be stored in the
  1233. ** last DWORD of the CCB header since it is used for command
  1234. ** completion (ncr_wakeup()). Doing so, we are sure that the header
  1235. ** has been entirely copied back to the CCB when the host_status is
  1236. ** seen complete by the CPU.
  1237. **
  1238. ** The last four bytes (status[4]) are copied to the scratchb register
  1239. ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
  1240. ** and copied back just after disconnecting.
  1241. ** Inside the script the XX_REG are used.
  1242. **
  1243. ** The first four bytes (scr_st[4]) are used inside the script by
  1244. ** "COPY" commands.
  1245. ** Because source and destination must have the same alignment
  1246. ** in a DWORD, the fields HAVE to be at the chosen offsets.
  1247. ** xerr_st 0 (0x34) scratcha
  1248. ** sync_st 1 (0x05) sxfer
  1249. ** wide_st 3 (0x03) scntl3
  1250. */
  1251. /*
  1252. ** Last four bytes (script)
  1253. */
  1254. #define QU_REG scr0
  1255. #define HS_REG scr1
  1256. #define HS_PRT nc_scr1
  1257. #define SS_REG scr2
  1258. #define SS_PRT nc_scr2
  1259. #define PS_REG scr3
  1260. /*
  1261. ** Last four bytes (host)
  1262. */
  1263. #ifdef SCSI_NCR_BIG_ENDIAN
  1264. #define actualquirks phys.header.status[3]
  1265. #define host_status phys.header.status[2]
  1266. #define scsi_status phys.header.status[1]
  1267. #define parity_status phys.header.status[0]
  1268. #else
  1269. #define actualquirks phys.header.status[0]
  1270. #define host_status phys.header.status[1]
  1271. #define scsi_status phys.header.status[2]
  1272. #define parity_status phys.header.status[3]
  1273. #endif
  1274. /*
  1275. ** First four bytes (script)
  1276. */
  1277. #define xerr_st header.scr_st[0]
  1278. #define sync_st header.scr_st[1]
  1279. #define nego_st header.scr_st[2]
  1280. #define wide_st header.scr_st[3]
  1281. /*
  1282. ** First four bytes (host)
  1283. */
  1284. #define xerr_status phys.xerr_st
  1285. #define nego_status phys.nego_st
  1286. /*==========================================================
  1287. **
  1288. ** Declaration of structs: Data structure block
  1289. **
  1290. **==========================================================
  1291. **
  1292. ** During execution of a ccb by the script processor,
  1293. ** the DSA (data structure address) register points
  1294. ** to this substructure of the ccb.
  1295. ** This substructure contains the header with
  1296. ** the script-processor-changeable data and
  1297. ** data blocks for the indirect move commands.
  1298. **
  1299. **----------------------------------------------------------
  1300. */
  1301. struct dsb {
  1302. /*
  1303. ** Header.
  1304. */
  1305. struct head header;
  1306. /*
  1307. ** Table data for Script
  1308. */
  1309. struct scr_tblsel select;
  1310. struct scr_tblmove smsg ;
  1311. struct scr_tblmove cmd ;
  1312. struct scr_tblmove sense ;
  1313. struct scr_tblmove data[MAX_SCATTER];
  1314. };
  1315. /*========================================================================
  1316. **
  1317. ** Declaration of structs: Command control block.
  1318. **
  1319. **========================================================================
  1320. */
  1321. struct ccb {
  1322. /*----------------------------------------------------------------
  1323. ** This is the data structure which is pointed by the DSA
  1324. ** register when it is executed by the script processor.
  1325. ** It must be the first entry because it contains the header
  1326. ** as first entry that must be cache line aligned.
  1327. **----------------------------------------------------------------
  1328. */
  1329. struct dsb phys;
  1330. /*----------------------------------------------------------------
  1331. ** Mini-script used at CCB execution start-up.
  1332. ** Load the DSA with the data structure address (phys) and
  1333. ** jump to SELECT. Jump to CANCEL if CCB is to be canceled.
  1334. **----------------------------------------------------------------
  1335. */
  1336. struct launch start;
  1337. /*----------------------------------------------------------------
  1338. ** Mini-script used at CCB relection to restart the nexus.
  1339. ** Load the DSA with the data structure address (phys) and
  1340. ** jump to RESEL_DSA. Jump to ABORT if CCB is to be aborted.
  1341. **----------------------------------------------------------------
  1342. */
  1343. struct launch restart;
  1344. /*----------------------------------------------------------------
  1345. ** If a data transfer phase is terminated too early
  1346. ** (after reception of a message (i.e. DISCONNECT)),
  1347. ** we have to prepare a mini script to transfer
  1348. ** the rest of the data.
  1349. **----------------------------------------------------------------
  1350. */
  1351. ncrcmd patch[8];
  1352. /*----------------------------------------------------------------
  1353. ** The general SCSI driver provides a
  1354. ** pointer to a control block.
  1355. **----------------------------------------------------------------
  1356. */
  1357. struct scsi_cmnd *cmd; /* SCSI command */
  1358. u_char cdb_buf[16]; /* Copy of CDB */
  1359. u_char sense_buf[64];
  1360. int data_len; /* Total data length */
  1361. /*----------------------------------------------------------------
  1362. ** Message areas.
  1363. ** We prepare a message to be sent after selection.
  1364. ** We may use a second one if the command is rescheduled
  1365. ** due to GETCC or QFULL.
  1366. ** Contents are IDENTIFY and SIMPLE_TAG.
  1367. ** While negotiating sync or wide transfer,
  1368. ** a SDTR or WDTR message is appended.
  1369. **----------------------------------------------------------------
  1370. */
  1371. u_char scsi_smsg [8];
  1372. u_char scsi_smsg2[8];
  1373. /*----------------------------------------------------------------
  1374. ** Other fields.
  1375. **----------------------------------------------------------------
  1376. */
  1377. u_long p_ccb; /* BUS address of this CCB */
  1378. u_char sensecmd[6]; /* Sense command */
  1379. u_char tag; /* Tag for this transfer */
  1380. /* 255 means no tag */
  1381. u_char target;
  1382. u_char lun;
  1383. u_char queued;
  1384. u_char auto_sense;
  1385. struct ccb * link_ccb; /* Host adapter CCB chain */
  1386. struct list_head link_ccbq; /* Link to unit CCB queue */
  1387. u32 startp; /* Initial data pointer */
  1388. u_long magic; /* Free / busy CCB flag */
  1389. };
  1390. #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
  1391. /*========================================================================
  1392. **
  1393. ** Declaration of structs: NCR device descriptor
  1394. **
  1395. **========================================================================
  1396. */
  1397. struct ncb {
  1398. /*----------------------------------------------------------------
  1399. ** The global header.
  1400. ** It is accessible to both the host and the script processor.
  1401. ** Must be cache line size aligned (32 for x86) in order to
  1402. ** allow cache line bursting when it is copied to/from CCB.
  1403. **----------------------------------------------------------------
  1404. */
  1405. struct head header;
  1406. /*----------------------------------------------------------------
  1407. ** CCBs management queues.
  1408. **----------------------------------------------------------------
  1409. */
  1410. struct scsi_cmnd *waiting_list; /* Commands waiting for a CCB */
  1411. /* when lcb is not allocated. */
  1412. struct scsi_cmnd *done_list; /* Commands waiting for done() */
  1413. /* callback to be invoked. */
  1414. spinlock_t smp_lock; /* Lock for SMP threading */
  1415. /*----------------------------------------------------------------
  1416. ** Chip and controller identification.
  1417. **----------------------------------------------------------------
  1418. */
  1419. int unit; /* Unit number */
  1420. char inst_name[16]; /* ncb instance name */
  1421. /*----------------------------------------------------------------
  1422. ** Initial value of some IO register bits.
  1423. ** These values are assumed to have been set by BIOS, and may
  1424. ** be used for probing adapter implementation differences.
  1425. **----------------------------------------------------------------
  1426. */
  1427. u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest0, sv_ctest3,
  1428. sv_ctest4, sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4;
  1429. /*----------------------------------------------------------------
  1430. ** Actual initial value of IO register bits used by the
  1431. ** driver. They are loaded at initialisation according to
  1432. ** features that are to be enabled.
  1433. **----------------------------------------------------------------
  1434. */
  1435. u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest0, rv_ctest3,
  1436. rv_ctest4, rv_ctest5, rv_stest2;
  1437. /*----------------------------------------------------------------
  1438. ** Targets management.
  1439. ** During reselection the ncr jumps to jump_tcb.
  1440. ** The SFBR register is loaded with the encoded target id.
  1441. ** For i = 0 to 3
  1442. ** SCR_JUMP ^ IFTRUE(MASK(i, 3)), @(next tcb mod. i)
  1443. **
  1444. ** Recent chips will prefetch the 4 JUMPS using only 1 burst.
  1445. ** It is kind of hashcoding.
  1446. **----------------------------------------------------------------
  1447. */
  1448. struct link jump_tcb[4]; /* JUMPs for reselection */
  1449. struct tcb target[MAX_TARGET]; /* Target data */
  1450. /*----------------------------------------------------------------
  1451. ** Virtual and physical bus addresses of the chip.
  1452. **----------------------------------------------------------------
  1453. */
  1454. void __iomem *vaddr; /* Virtual and bus address of */
  1455. unsigned long paddr; /* chip's IO registers. */
  1456. unsigned long paddr2; /* On-chip RAM bus address. */
  1457. volatile /* Pointer to volatile for */
  1458. struct ncr_reg __iomem *reg; /* memory mapped IO. */
  1459. /*----------------------------------------------------------------
  1460. ** SCRIPTS virtual and physical bus addresses.
  1461. ** 'script' is loaded in the on-chip RAM if present.
  1462. ** 'scripth' stays in main memory.
  1463. **----------------------------------------------------------------
  1464. */
  1465. struct script *script0; /* Copies of script and scripth */
  1466. struct scripth *scripth0; /* relocated for this ncb. */
  1467. struct scripth *scripth; /* Actual scripth virt. address */
  1468. u_long p_script; /* Actual script and scripth */
  1469. u_long p_scripth; /* bus addresses. */
  1470. /*----------------------------------------------------------------
  1471. ** General controller parameters and configuration.
  1472. **----------------------------------------------------------------
  1473. */
  1474. struct device *dev;
  1475. u_char revision_id; /* PCI device revision id */
  1476. u32 irq; /* IRQ level */
  1477. u32 features; /* Chip features map */
  1478. u_char myaddr; /* SCSI id of the adapter */
  1479. u_char maxburst; /* log base 2 of dwords burst */
  1480. u_char maxwide; /* Maximum transfer width */
  1481. u_char minsync; /* Minimum sync period factor */
  1482. u_char maxsync; /* Maximum sync period factor */
  1483. u_char maxoffs; /* Max scsi offset */
  1484. u_char multiplier; /* Clock multiplier (1,2,4) */
  1485. u_char clock_divn; /* Number of clock divisors */
  1486. u_long clock_khz; /* SCSI clock frequency in KHz */
  1487. /*----------------------------------------------------------------
  1488. ** Start queue management.
  1489. ** It is filled up by the host processor and accessed by the
  1490. ** SCRIPTS processor in order to start SCSI commands.
  1491. **----------------------------------------------------------------
  1492. */
  1493. u16 squeueput; /* Next free slot of the queue */
  1494. u16 actccbs; /* Number of allocated CCBs */
  1495. u16 queuedccbs; /* Number of CCBs in start queue*/
  1496. u16 queuedepth; /* Start queue depth */
  1497. /*----------------------------------------------------------------
  1498. ** Timeout handler.
  1499. **----------------------------------------------------------------
  1500. */
  1501. struct timer_list timer; /* Timer handler link header */
  1502. u_long lasttime;
  1503. u_long settle_time; /* Resetting the SCSI BUS */
  1504. /*----------------------------------------------------------------
  1505. ** Debugging and profiling.
  1506. **----------------------------------------------------------------
  1507. */
  1508. struct ncr_reg regdump; /* Register dump */
  1509. u_long regtime; /* Time it has been done */
  1510. /*----------------------------------------------------------------
  1511. ** Miscellaneous buffers accessed by the scripts-processor.
  1512. ** They shall be DWORD aligned, because they may be read or
  1513. ** written with a SCR_COPY script command.
  1514. **----------------------------------------------------------------
  1515. */
  1516. u_char msgout[8]; /* Buffer for MESSAGE OUT */
  1517. u_char msgin [8]; /* Buffer for MESSAGE IN */
  1518. u32 lastmsg; /* Last SCSI message sent */
  1519. u_char scratch; /* Scratch for SCSI receive */
  1520. /*----------------------------------------------------------------
  1521. ** Miscellaneous configuration and status parameters.
  1522. **----------------------------------------------------------------
  1523. */
  1524. u_char disc; /* Disconnection allowed */
  1525. u_char scsi_mode; /* Current SCSI BUS mode */
  1526. u_char order; /* Tag order to use */
  1527. u_char verbose; /* Verbosity for this controller*/
  1528. int ncr_cache; /* Used for cache test at init. */
  1529. u_long p_ncb; /* BUS address of this NCB */
  1530. /*----------------------------------------------------------------
  1531. ** Command completion handling.
  1532. **----------------------------------------------------------------
  1533. */
  1534. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  1535. struct ccb *(ccb_done[MAX_DONE]);
  1536. int ccb_done_ic;
  1537. #endif
  1538. /*----------------------------------------------------------------
  1539. ** Fields that should be removed or changed.
  1540. **----------------------------------------------------------------
  1541. */
  1542. struct ccb *ccb; /* Global CCB */
  1543. struct usrcmd user; /* Command from user */
  1544. volatile u_char release_stage; /* Synchronisation stage on release */
  1545. };
  1546. #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
  1547. #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
  1548. /*==========================================================
  1549. **
  1550. **
  1551. ** Script for NCR-Processor.
  1552. **
  1553. ** Use ncr_script_fill() to create the variable parts.
  1554. ** Use ncr_script_copy_and_bind() to make a copy and
  1555. ** bind to physical addresses.
  1556. **
  1557. **
  1558. **==========================================================
  1559. **
  1560. ** We have to know the offsets of all labels before
  1561. ** we reach them (for forward jumps).
  1562. ** Therefore we declare a struct here.
  1563. ** If you make changes inside the script,
  1564. ** DONT FORGET TO CHANGE THE LENGTHS HERE!
  1565. **
  1566. **----------------------------------------------------------
  1567. */
  1568. /*
  1569. ** For HP Zalon/53c720 systems, the Zalon interface
  1570. ** between CPU and 53c720 does prefetches, which causes
  1571. ** problems with self modifying scripts. The problem
  1572. ** is overcome by calling a dummy subroutine after each
  1573. ** modification, to force a refetch of the script on
  1574. ** return from the subroutine.
  1575. */
  1576. #ifdef CONFIG_NCR53C8XX_PREFETCH
  1577. #define PREFETCH_FLUSH_CNT 2
  1578. #define PREFETCH_FLUSH SCR_CALL, PADDRH (wait_dma),
  1579. #else
  1580. #define PREFETCH_FLUSH_CNT 0
  1581. #define PREFETCH_FLUSH
  1582. #endif
  1583. /*
  1584. ** Script fragments which are loaded into the on-chip RAM
  1585. ** of 825A, 875 and 895 chips.
  1586. */
  1587. struct script {
  1588. ncrcmd start [ 5];
  1589. ncrcmd startpos [ 1];
  1590. ncrcmd select [ 6];
  1591. ncrcmd select2 [ 9 + PREFETCH_FLUSH_CNT];
  1592. ncrcmd loadpos [ 4];
  1593. ncrcmd send_ident [ 9];
  1594. ncrcmd prepare [ 6];
  1595. ncrcmd prepare2 [ 7];
  1596. ncrcmd command [ 6];
  1597. ncrcmd dispatch [ 32];
  1598. ncrcmd clrack [ 4];
  1599. ncrcmd no_data [ 17];
  1600. ncrcmd status [ 8];
  1601. ncrcmd msg_in [ 2];
  1602. ncrcmd msg_in2 [ 16];
  1603. ncrcmd msg_bad [ 4];
  1604. ncrcmd setmsg [ 7];
  1605. ncrcmd cleanup [ 6];
  1606. ncrcmd complete [ 9];
  1607. ncrcmd cleanup_ok [ 8 + PREFETCH_FLUSH_CNT];
  1608. ncrcmd cleanup0 [ 1];
  1609. #ifndef SCSI_NCR_CCB_DONE_SUPPORT
  1610. ncrcmd signal [ 12];
  1611. #else
  1612. ncrcmd signal [ 9];
  1613. ncrcmd done_pos [ 1];
  1614. ncrcmd done_plug [ 2];
  1615. ncrcmd done_end [ 7];
  1616. #endif
  1617. ncrcmd save_dp [ 7];
  1618. ncrcmd restore_dp [ 5];
  1619. ncrcmd disconnect [ 10];
  1620. ncrcmd msg_out [ 9];
  1621. ncrcmd msg_out_done [ 7];
  1622. ncrcmd idle [ 2];
  1623. ncrcmd reselect [ 8];
  1624. ncrcmd reselected [ 8];
  1625. ncrcmd resel_dsa [ 6 + PREFETCH_FLUSH_CNT];
  1626. ncrcmd loadpos1 [ 4];
  1627. ncrcmd resel_lun [ 6];
  1628. ncrcmd resel_tag [ 6];
  1629. ncrcmd jump_to_nexus [ 4 + PREFETCH_FLUSH_CNT];
  1630. ncrcmd nexus_indirect [ 4];
  1631. ncrcmd resel_notag [ 4];
  1632. ncrcmd data_in [MAX_SCATTERL * 4];
  1633. ncrcmd data_in2 [ 4];
  1634. ncrcmd data_out [MAX_SCATTERL * 4];
  1635. ncrcmd data_out2 [ 4];
  1636. };
  1637. /*
  1638. ** Script fragments which stay in main memory for all chips.
  1639. */
  1640. struct scripth {
  1641. ncrcmd tryloop [MAX_START*2];
  1642. ncrcmd tryloop2 [ 2];
  1643. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  1644. ncrcmd done_queue [MAX_DONE*5];
  1645. ncrcmd done_queue2 [ 2];
  1646. #endif
  1647. ncrcmd select_no_atn [ 8];
  1648. ncrcmd cancel [ 4];
  1649. ncrcmd skip [ 9 + PREFETCH_FLUSH_CNT];
  1650. ncrcmd skip2 [ 19];
  1651. ncrcmd par_err_data_in [ 6];
  1652. ncrcmd par_err_other [ 4];
  1653. ncrcmd msg_reject [ 8];
  1654. ncrcmd msg_ign_residue [ 24];
  1655. ncrcmd msg_extended [ 10];
  1656. ncrcmd msg_ext_2 [ 10];
  1657. ncrcmd msg_wdtr [ 14];
  1658. ncrcmd send_wdtr [ 7];
  1659. ncrcmd msg_ext_3 [ 10];
  1660. ncrcmd msg_sdtr [ 14];
  1661. ncrcmd send_sdtr [ 7];
  1662. ncrcmd nego_bad_phase [ 4];
  1663. ncrcmd msg_out_abort [ 10];
  1664. ncrcmd hdata_in [MAX_SCATTERH * 4];
  1665. ncrcmd hdata_in2 [ 2];
  1666. ncrcmd hdata_out [MAX_SCATTERH * 4];
  1667. ncrcmd hdata_out2 [ 2];
  1668. ncrcmd reset [ 4];
  1669. ncrcmd aborttag [ 4];
  1670. ncrcmd abort [ 2];
  1671. ncrcmd abort_resel [ 20];
  1672. ncrcmd resend_ident [ 4];
  1673. ncrcmd clratn_go_on [ 3];
  1674. ncrcmd nxtdsp_go_on [ 1];
  1675. ncrcmd sdata_in [ 8];
  1676. ncrcmd data_io [ 18];
  1677. ncrcmd bad_identify [ 12];
  1678. ncrcmd bad_i_t_l [ 4];
  1679. ncrcmd bad_i_t_l_q [ 4];
  1680. ncrcmd bad_target [ 8];
  1681. ncrcmd bad_status [ 8];
  1682. ncrcmd start_ram [ 4 + PREFETCH_FLUSH_CNT];
  1683. ncrcmd start_ram0 [ 4];
  1684. ncrcmd sto_restart [ 5];
  1685. ncrcmd wait_dma [ 2];
  1686. ncrcmd snooptest [ 9];
  1687. ncrcmd snoopend [ 2];
  1688. };
  1689. /*==========================================================
  1690. **
  1691. **
  1692. ** Function headers.
  1693. **
  1694. **
  1695. **==========================================================
  1696. */
  1697. static void ncr_alloc_ccb (struct ncb *np, u_char tn, u_char ln);
  1698. static void ncr_complete (struct ncb *np, struct ccb *cp);
  1699. static void ncr_exception (struct ncb *np);
  1700. static void ncr_free_ccb (struct ncb *np, struct ccb *cp);
  1701. static void ncr_init_ccb (struct ncb *np, struct ccb *cp);
  1702. static void ncr_init_tcb (struct ncb *np, u_char tn);
  1703. static struct lcb * ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln);
  1704. static struct lcb * ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev);
  1705. static void ncr_getclock (struct ncb *np, int mult);
  1706. static void ncr_selectclock (struct ncb *np, u_char scntl3);
  1707. static struct ccb *ncr_get_ccb (struct ncb *np, struct scsi_cmnd *cmd);
  1708. static void ncr_chip_reset (struct ncb *np, int delay);
  1709. static void ncr_init (struct ncb *np, int reset, char * msg, u_long code);
  1710. static int ncr_int_sbmc (struct ncb *np);
  1711. static int ncr_int_par (struct ncb *np);
  1712. static void ncr_int_ma (struct ncb *np);
  1713. static void ncr_int_sir (struct ncb *np);
  1714. static void ncr_int_sto (struct ncb *np);
  1715. static void ncr_negotiate (struct ncb* np, struct tcb* tp);
  1716. static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr);
  1717. static void ncr_script_copy_and_bind
  1718. (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len);
  1719. static void ncr_script_fill (struct script * scr, struct scripth * scripth);
  1720. static int ncr_scatter (struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd);
  1721. static void ncr_getsync (struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p);
  1722. static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer);
  1723. static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev);
  1724. static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack);
  1725. static int ncr_snooptest (struct ncb *np);
  1726. static void ncr_timeout (struct ncb *np);
  1727. static void ncr_wakeup (struct ncb *np, u_long code);
  1728. static void ncr_wakeup_done (struct ncb *np);
  1729. static void ncr_start_next_ccb (struct ncb *np, struct lcb * lp, int maxn);
  1730. static void ncr_put_start_queue(struct ncb *np, struct ccb *cp);
  1731. static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd);
  1732. static void process_waiting_list(struct ncb *np, int sts);
  1733. #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
  1734. #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
  1735. static inline char *ncr_name (struct ncb *np)
  1736. {
  1737. return np->inst_name;
  1738. }
  1739. /*==========================================================
  1740. **
  1741. **
  1742. ** Scripts for NCR-Processor.
  1743. **
  1744. ** Use ncr_script_bind for binding to physical addresses.
  1745. **
  1746. **
  1747. **==========================================================
  1748. **
  1749. ** NADDR generates a reference to a field of the controller data.
  1750. ** PADDR generates a reference to another part of the script.
  1751. ** RADDR generates a reference to a script processor register.
  1752. ** FADDR generates a reference to a script processor register
  1753. ** with offset.
  1754. **
  1755. **----------------------------------------------------------
  1756. */
  1757. #define RELOC_SOFTC 0x40000000
  1758. #define RELOC_LABEL 0x50000000
  1759. #define RELOC_REGISTER 0x60000000
  1760. #define RELOC_LABELH 0x80000000
  1761. #define RELOC_MASK 0xf0000000
  1762. #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
  1763. #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
  1764. #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
  1765. #define RADDR(label) (RELOC_REGISTER | REG(label))
  1766. #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
  1767. static struct script script0 __initdata = {
  1768. /*--------------------------< START >-----------------------*/ {
  1769. /*
  1770. ** This NOP will be patched with LED ON
  1771. ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
  1772. */
  1773. SCR_NO_OP,
  1774. 0,
  1775. /*
  1776. ** Clear SIGP.
  1777. */
  1778. SCR_FROM_REG (ctest2),
  1779. 0,
  1780. /*
  1781. ** Then jump to a certain point in tryloop.
  1782. ** Due to the lack of indirect addressing the code
  1783. ** is self modifying here.
  1784. */
  1785. SCR_JUMP,
  1786. }/*-------------------------< STARTPOS >--------------------*/,{
  1787. PADDRH(tryloop),
  1788. }/*-------------------------< SELECT >----------------------*/,{
  1789. /*
  1790. ** DSA contains the address of a scheduled
  1791. ** data structure.
  1792. **
  1793. ** SCRATCHA contains the address of the script,
  1794. ** which starts the next entry.
  1795. **
  1796. ** Set Initiator mode.
  1797. **
  1798. ** (Target mode is left as an exercise for the reader)
  1799. */
  1800. SCR_CLR (SCR_TRG),
  1801. 0,
  1802. SCR_LOAD_REG (HS_REG, HS_SELECTING),
  1803. 0,
  1804. /*
  1805. ** And try to select this target.
  1806. */
  1807. SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
  1808. PADDR (reselect),
  1809. }/*-------------------------< SELECT2 >----------------------*/,{
  1810. /*
  1811. ** Now there are 4 possibilities:
  1812. **
  1813. ** (1) The ncr loses arbitration.
  1814. ** This is ok, because it will try again,
  1815. ** when the bus becomes idle.
  1816. ** (But beware of the timeout function!)
  1817. **
  1818. ** (2) The ncr is reselected.
  1819. ** Then the script processor takes the jump
  1820. ** to the RESELECT label.
  1821. **
  1822. ** (3) The ncr wins arbitration.
  1823. ** Then it will execute SCRIPTS instruction until
  1824. ** the next instruction that checks SCSI phase.
  1825. ** Then will stop and wait for selection to be
  1826. ** complete or selection time-out to occur.
  1827. ** As a result the SCRIPTS instructions until
  1828. ** LOADPOS + 2 should be executed in parallel with
  1829. ** the SCSI core performing selection.
  1830. */
  1831. /*
  1832. ** The MESSAGE_REJECT problem seems to be due to a selection
  1833. ** timing problem.
  1834. ** Wait immediately for the selection to complete.
  1835. ** (2.5x behaves so)
  1836. */
  1837. SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  1838. 0,
  1839. /*
  1840. ** Next time use the next slot.
  1841. */
  1842. SCR_COPY (4),
  1843. RADDR (temp),
  1844. PADDR (startpos),
  1845. /*
  1846. ** The ncr doesn't have an indirect load
  1847. ** or store command. So we have to
  1848. ** copy part of the control block to a
  1849. ** fixed place, where we can access it.
  1850. **
  1851. ** We patch the address part of a
  1852. ** COPY command with the DSA-register.
  1853. */
  1854. SCR_COPY_F (4),
  1855. RADDR (dsa),
  1856. PADDR (loadpos),
  1857. /*
  1858. ** Flush script prefetch if required
  1859. */
  1860. PREFETCH_FLUSH
  1861. /*
  1862. ** then we do the actual copy.
  1863. */
  1864. SCR_COPY (sizeof (struct head)),
  1865. /*
  1866. ** continued after the next label ...
  1867. */
  1868. }/*-------------------------< LOADPOS >---------------------*/,{
  1869. 0,
  1870. NADDR (header),
  1871. /*
  1872. ** Wait for the next phase or the selection
  1873. ** to complete or time-out.
  1874. */
  1875. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  1876. PADDR (prepare),
  1877. }/*-------------------------< SEND_IDENT >----------------------*/,{
  1878. /*
  1879. ** Selection complete.
  1880. ** Send the IDENTIFY and SIMPLE_TAG messages
  1881. ** (and the EXTENDED_SDTR message)
  1882. */
  1883. SCR_MOVE_TBL ^ SCR_MSG_OUT,
  1884. offsetof (struct dsb, smsg),
  1885. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
  1886. PADDRH (resend_ident),
  1887. SCR_LOAD_REG (scratcha, 0x80),
  1888. 0,
  1889. SCR_COPY (1),
  1890. RADDR (scratcha),
  1891. NADDR (lastmsg),
  1892. }/*-------------------------< PREPARE >----------------------*/,{
  1893. /*
  1894. ** load the savep (saved pointer) into
  1895. ** the TEMP register (actual pointer)
  1896. */
  1897. SCR_COPY (4),
  1898. NADDR (header.savep),
  1899. RADDR (temp),
  1900. /*
  1901. ** Initialize the status registers
  1902. */
  1903. SCR_COPY (4),
  1904. NADDR (header.status),
  1905. RADDR (scr0),
  1906. }/*-------------------------< PREPARE2 >---------------------*/,{
  1907. /*
  1908. ** Initialize the msgout buffer with a NOOP message.
  1909. */
  1910. SCR_LOAD_REG (scratcha, NOP),
  1911. 0,
  1912. SCR_COPY (1),
  1913. RADDR (scratcha),
  1914. NADDR (msgout),
  1915. /*
  1916. ** Anticipate the COMMAND phase.
  1917. ** This is the normal case for initial selection.
  1918. */
  1919. SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
  1920. PADDR (dispatch),
  1921. }/*-------------------------< COMMAND >--------------------*/,{
  1922. /*
  1923. ** ... and send the command
  1924. */
  1925. SCR_MOVE_TBL ^ SCR_COMMAND,
  1926. offsetof (struct dsb, cmd),
  1927. /*
  1928. ** If status is still HS_NEGOTIATE, negotiation failed.
  1929. ** We check this here, since we want to do that
  1930. ** only once.
  1931. */
  1932. SCR_FROM_REG (HS_REG),
  1933. 0,
  1934. SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
  1935. SIR_NEGO_FAILED,
  1936. }/*-----------------------< DISPATCH >----------------------*/,{
  1937. /*
  1938. ** MSG_IN is the only phase that shall be
  1939. ** entered at least once for each (re)selection.
  1940. ** So we test it first.
  1941. */
  1942. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
  1943. PADDR (msg_in),
  1944. SCR_RETURN ^ IFTRUE (IF (SCR_DATA_OUT)),
  1945. 0,
  1946. /*
  1947. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 4.
  1948. ** Possible data corruption during Memory Write and Invalidate.
  1949. ** This work-around resets the addressing logic prior to the
  1950. ** start of the first MOVE of a DATA IN phase.
  1951. ** (See Documentation/scsi/ncr53c8xx.rst for more information)
  1952. */
  1953. SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
  1954. 20,
  1955. SCR_COPY (4),
  1956. RADDR (scratcha),
  1957. RADDR (scratcha),
  1958. SCR_RETURN,
  1959. 0,
  1960. SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
  1961. PADDR (status),
  1962. SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
  1963. PADDR (command),
  1964. SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
  1965. PADDR (msg_out),
  1966. /*
  1967. ** Discard one illegal phase byte, if required.
  1968. */
  1969. SCR_LOAD_REG (scratcha, XE_BAD_PHASE),
  1970. 0,
  1971. SCR_COPY (1),
  1972. RADDR (scratcha),
  1973. NADDR (xerr_st),
  1974. SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_OUT)),
  1975. 8,
  1976. SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
  1977. NADDR (scratch),
  1978. SCR_JUMPR ^ IFFALSE (IF (SCR_ILG_IN)),
  1979. 8,
  1980. SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
  1981. NADDR (scratch),
  1982. SCR_JUMP,
  1983. PADDR (dispatch),
  1984. }/*-------------------------< CLRACK >----------------------*/,{
  1985. /*
  1986. ** Terminate possible pending message phase.
  1987. */
  1988. SCR_CLR (SCR_ACK),
  1989. 0,
  1990. SCR_JUMP,
  1991. PADDR (dispatch),
  1992. }/*-------------------------< NO_DATA >--------------------*/,{
  1993. /*
  1994. ** The target wants to tranfer too much data
  1995. ** or in the wrong direction.
  1996. ** Remember that in extended error.
  1997. */
  1998. SCR_LOAD_REG (scratcha, XE_EXTRA_DATA),
  1999. 0,
  2000. SCR_COPY (1),
  2001. RADDR (scratcha),
  2002. NADDR (xerr_st),
  2003. /*
  2004. ** Discard one data byte, if required.
  2005. */
  2006. SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2007. 8,
  2008. SCR_MOVE_ABS (1) ^ SCR_DATA_OUT,
  2009. NADDR (scratch),
  2010. SCR_JUMPR ^ IFFALSE (IF (SCR_DATA_IN)),
  2011. 8,
  2012. SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
  2013. NADDR (scratch),
  2014. /*
  2015. ** .. and repeat as required.
  2016. */
  2017. SCR_CALL,
  2018. PADDR (dispatch),
  2019. SCR_JUMP,
  2020. PADDR (no_data),
  2021. }/*-------------------------< STATUS >--------------------*/,{
  2022. /*
  2023. ** get the status
  2024. */
  2025. SCR_MOVE_ABS (1) ^ SCR_STATUS,
  2026. NADDR (scratch),
  2027. /*
  2028. ** save status to scsi_status.
  2029. ** mark as complete.
  2030. */
  2031. SCR_TO_REG (SS_REG),
  2032. 0,
  2033. SCR_LOAD_REG (HS_REG, HS_COMPLETE),
  2034. 0,
  2035. SCR_JUMP,
  2036. PADDR (dispatch),
  2037. }/*-------------------------< MSG_IN >--------------------*/,{
  2038. /*
  2039. ** Get the first byte of the message
  2040. ** and save it to SCRATCHA.
  2041. **
  2042. ** The script processor doesn't negate the
  2043. ** ACK signal after this transfer.
  2044. */
  2045. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2046. NADDR (msgin[0]),
  2047. }/*-------------------------< MSG_IN2 >--------------------*/,{
  2048. /*
  2049. ** Handle this message.
  2050. */
  2051. SCR_JUMP ^ IFTRUE (DATA (COMMAND_COMPLETE)),
  2052. PADDR (complete),
  2053. SCR_JUMP ^ IFTRUE (DATA (DISCONNECT)),
  2054. PADDR (disconnect),
  2055. SCR_JUMP ^ IFTRUE (DATA (SAVE_POINTERS)),
  2056. PADDR (save_dp),
  2057. SCR_JUMP ^ IFTRUE (DATA (RESTORE_POINTERS)),
  2058. PADDR (restore_dp),
  2059. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_MESSAGE)),
  2060. PADDRH (msg_extended),
  2061. SCR_JUMP ^ IFTRUE (DATA (NOP)),
  2062. PADDR (clrack),
  2063. SCR_JUMP ^ IFTRUE (DATA (MESSAGE_REJECT)),
  2064. PADDRH (msg_reject),
  2065. SCR_JUMP ^ IFTRUE (DATA (IGNORE_WIDE_RESIDUE)),
  2066. PADDRH (msg_ign_residue),
  2067. /*
  2068. ** Rest of the messages left as
  2069. ** an exercise ...
  2070. **
  2071. ** Unimplemented messages:
  2072. ** fall through to MSG_BAD.
  2073. */
  2074. }/*-------------------------< MSG_BAD >------------------*/,{
  2075. /*
  2076. ** unimplemented message - reject it.
  2077. */
  2078. SCR_INT,
  2079. SIR_REJECT_SENT,
  2080. SCR_LOAD_REG (scratcha, MESSAGE_REJECT),
  2081. 0,
  2082. }/*-------------------------< SETMSG >----------------------*/,{
  2083. SCR_COPY (1),
  2084. RADDR (scratcha),
  2085. NADDR (msgout),
  2086. SCR_SET (SCR_ATN),
  2087. 0,
  2088. SCR_JUMP,
  2089. PADDR (clrack),
  2090. }/*-------------------------< CLEANUP >-------------------*/,{
  2091. /*
  2092. ** dsa: Pointer to ccb
  2093. ** or xxxxxxFF (no ccb)
  2094. **
  2095. ** HS_REG: Host-Status (<>0!)
  2096. */
  2097. SCR_FROM_REG (dsa),
  2098. 0,
  2099. SCR_JUMP ^ IFTRUE (DATA (0xff)),
  2100. PADDR (start),
  2101. /*
  2102. ** dsa is valid.
  2103. ** complete the cleanup.
  2104. */
  2105. SCR_JUMP,
  2106. PADDR (cleanup_ok),
  2107. }/*-------------------------< COMPLETE >-----------------*/,{
  2108. /*
  2109. ** Complete message.
  2110. **
  2111. ** Copy TEMP register to LASTP in header.
  2112. */
  2113. SCR_COPY (4),
  2114. RADDR (temp),
  2115. NADDR (header.lastp),
  2116. /*
  2117. ** When we terminate the cycle by clearing ACK,
  2118. ** the target may disconnect immediately.
  2119. **
  2120. ** We don't want to be told of an
  2121. ** "unexpected disconnect",
  2122. ** so we disable this feature.
  2123. */
  2124. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2125. 0,
  2126. /*
  2127. ** Terminate cycle ...
  2128. */
  2129. SCR_CLR (SCR_ACK|SCR_ATN),
  2130. 0,
  2131. /*
  2132. ** ... and wait for the disconnect.
  2133. */
  2134. SCR_WAIT_DISC,
  2135. 0,
  2136. }/*-------------------------< CLEANUP_OK >----------------*/,{
  2137. /*
  2138. ** Save host status to header.
  2139. */
  2140. SCR_COPY (4),
  2141. RADDR (scr0),
  2142. NADDR (header.status),
  2143. /*
  2144. ** and copy back the header to the ccb.
  2145. */
  2146. SCR_COPY_F (4),
  2147. RADDR (dsa),
  2148. PADDR (cleanup0),
  2149. /*
  2150. ** Flush script prefetch if required
  2151. */
  2152. PREFETCH_FLUSH
  2153. SCR_COPY (sizeof (struct head)),
  2154. NADDR (header),
  2155. }/*-------------------------< CLEANUP0 >--------------------*/,{
  2156. 0,
  2157. }/*-------------------------< SIGNAL >----------------------*/,{
  2158. /*
  2159. ** if job not completed ...
  2160. */
  2161. SCR_FROM_REG (HS_REG),
  2162. 0,
  2163. /*
  2164. ** ... start the next command.
  2165. */
  2166. SCR_JUMP ^ IFTRUE (MASK (0, (HS_DONEMASK|HS_SKIPMASK))),
  2167. PADDR(start),
  2168. /*
  2169. ** If command resulted in not GOOD status,
  2170. ** call the C code if needed.
  2171. */
  2172. SCR_FROM_REG (SS_REG),
  2173. 0,
  2174. SCR_CALL ^ IFFALSE (DATA (SAM_STAT_GOOD)),
  2175. PADDRH (bad_status),
  2176. #ifndef SCSI_NCR_CCB_DONE_SUPPORT
  2177. /*
  2178. ** ... signal completion to the host
  2179. */
  2180. SCR_INT,
  2181. SIR_INTFLY,
  2182. /*
  2183. ** Auf zu neuen Schandtaten!
  2184. */
  2185. SCR_JUMP,
  2186. PADDR(start),
  2187. #else /* defined SCSI_NCR_CCB_DONE_SUPPORT */
  2188. /*
  2189. ** ... signal completion to the host
  2190. */
  2191. SCR_JUMP,
  2192. }/*------------------------< DONE_POS >---------------------*/,{
  2193. PADDRH (done_queue),
  2194. }/*------------------------< DONE_PLUG >--------------------*/,{
  2195. SCR_INT,
  2196. SIR_DONE_OVERFLOW,
  2197. }/*------------------------< DONE_END >---------------------*/,{
  2198. SCR_INT,
  2199. SIR_INTFLY,
  2200. SCR_COPY (4),
  2201. RADDR (temp),
  2202. PADDR (done_pos),
  2203. SCR_JUMP,
  2204. PADDR (start),
  2205. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  2206. }/*-------------------------< SAVE_DP >------------------*/,{
  2207. /*
  2208. ** SAVE_DP message:
  2209. ** Copy TEMP register to SAVEP in header.
  2210. */
  2211. SCR_COPY (4),
  2212. RADDR (temp),
  2213. NADDR (header.savep),
  2214. SCR_CLR (SCR_ACK),
  2215. 0,
  2216. SCR_JUMP,
  2217. PADDR (dispatch),
  2218. }/*-------------------------< RESTORE_DP >---------------*/,{
  2219. /*
  2220. ** RESTORE_DP message:
  2221. ** Copy SAVEP in header to TEMP register.
  2222. */
  2223. SCR_COPY (4),
  2224. NADDR (header.savep),
  2225. RADDR (temp),
  2226. SCR_JUMP,
  2227. PADDR (clrack),
  2228. }/*-------------------------< DISCONNECT >---------------*/,{
  2229. /*
  2230. ** DISCONNECTing ...
  2231. **
  2232. ** disable the "unexpected disconnect" feature,
  2233. ** and remove the ACK signal.
  2234. */
  2235. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2236. 0,
  2237. SCR_CLR (SCR_ACK|SCR_ATN),
  2238. 0,
  2239. /*
  2240. ** Wait for the disconnect.
  2241. */
  2242. SCR_WAIT_DISC,
  2243. 0,
  2244. /*
  2245. ** Status is: DISCONNECTED.
  2246. */
  2247. SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
  2248. 0,
  2249. SCR_JUMP,
  2250. PADDR (cleanup_ok),
  2251. }/*-------------------------< MSG_OUT >-------------------*/,{
  2252. /*
  2253. ** The target requests a message.
  2254. */
  2255. SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
  2256. NADDR (msgout),
  2257. SCR_COPY (1),
  2258. NADDR (msgout),
  2259. NADDR (lastmsg),
  2260. /*
  2261. ** If it was no ABORT message ...
  2262. */
  2263. SCR_JUMP ^ IFTRUE (DATA (ABORT_TASK_SET)),
  2264. PADDRH (msg_out_abort),
  2265. /*
  2266. ** ... wait for the next phase
  2267. ** if it's a message out, send it again, ...
  2268. */
  2269. SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
  2270. PADDR (msg_out),
  2271. }/*-------------------------< MSG_OUT_DONE >--------------*/,{
  2272. /*
  2273. ** ... else clear the message ...
  2274. */
  2275. SCR_LOAD_REG (scratcha, NOP),
  2276. 0,
  2277. SCR_COPY (4),
  2278. RADDR (scratcha),
  2279. NADDR (msgout),
  2280. /*
  2281. ** ... and process the next phase
  2282. */
  2283. SCR_JUMP,
  2284. PADDR (dispatch),
  2285. }/*-------------------------< IDLE >------------------------*/,{
  2286. /*
  2287. ** Nothing to do?
  2288. ** Wait for reselect.
  2289. ** This NOP will be patched with LED OFF
  2290. ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
  2291. */
  2292. SCR_NO_OP,
  2293. 0,
  2294. }/*-------------------------< RESELECT >--------------------*/,{
  2295. /*
  2296. ** make the DSA invalid.
  2297. */
  2298. SCR_LOAD_REG (dsa, 0xff),
  2299. 0,
  2300. SCR_CLR (SCR_TRG),
  2301. 0,
  2302. SCR_LOAD_REG (HS_REG, HS_IN_RESELECT),
  2303. 0,
  2304. /*
  2305. ** Sleep waiting for a reselection.
  2306. ** If SIGP is set, special treatment.
  2307. **
  2308. ** Zu allem bereit ..
  2309. */
  2310. SCR_WAIT_RESEL,
  2311. PADDR(start),
  2312. }/*-------------------------< RESELECTED >------------------*/,{
  2313. /*
  2314. ** This NOP will be patched with LED ON
  2315. ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
  2316. */
  2317. SCR_NO_OP,
  2318. 0,
  2319. /*
  2320. ** ... zu nichts zu gebrauchen ?
  2321. **
  2322. ** load the target id into the SFBR
  2323. ** and jump to the control block.
  2324. **
  2325. ** Look at the declarations of
  2326. ** - struct ncb
  2327. ** - struct tcb
  2328. ** - struct lcb
  2329. ** - struct ccb
  2330. ** to understand what's going on.
  2331. */
  2332. SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
  2333. 0,
  2334. SCR_TO_REG (sdid),
  2335. 0,
  2336. SCR_JUMP,
  2337. NADDR (jump_tcb),
  2338. }/*-------------------------< RESEL_DSA >-------------------*/,{
  2339. /*
  2340. ** Ack the IDENTIFY or TAG previously received.
  2341. */
  2342. SCR_CLR (SCR_ACK),
  2343. 0,
  2344. /*
  2345. ** The ncr doesn't have an indirect load
  2346. ** or store command. So we have to
  2347. ** copy part of the control block to a
  2348. ** fixed place, where we can access it.
  2349. **
  2350. ** We patch the address part of a
  2351. ** COPY command with the DSA-register.
  2352. */
  2353. SCR_COPY_F (4),
  2354. RADDR (dsa),
  2355. PADDR (loadpos1),
  2356. /*
  2357. ** Flush script prefetch if required
  2358. */
  2359. PREFETCH_FLUSH
  2360. /*
  2361. ** then we do the actual copy.
  2362. */
  2363. SCR_COPY (sizeof (struct head)),
  2364. /*
  2365. ** continued after the next label ...
  2366. */
  2367. }/*-------------------------< LOADPOS1 >-------------------*/,{
  2368. 0,
  2369. NADDR (header),
  2370. /*
  2371. ** The DSA contains the data structure address.
  2372. */
  2373. SCR_JUMP,
  2374. PADDR (prepare),
  2375. }/*-------------------------< RESEL_LUN >-------------------*/,{
  2376. /*
  2377. ** come back to this point
  2378. ** to get an IDENTIFY message
  2379. ** Wait for a msg_in phase.
  2380. */
  2381. SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2382. SIR_RESEL_NO_MSG_IN,
  2383. /*
  2384. ** message phase.
  2385. ** Read the data directly from the BUS DATA lines.
  2386. ** This helps to support very old SCSI devices that
  2387. ** may reselect without sending an IDENTIFY.
  2388. */
  2389. SCR_FROM_REG (sbdl),
  2390. 0,
  2391. /*
  2392. ** It should be an Identify message.
  2393. */
  2394. SCR_RETURN,
  2395. 0,
  2396. }/*-------------------------< RESEL_TAG >-------------------*/,{
  2397. /*
  2398. ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
  2399. ** Aggressive optimization, is'nt it?
  2400. ** No need to test the SIMPLE TAG message, since the
  2401. ** driver only supports conformant devices for tags. ;-)
  2402. */
  2403. SCR_MOVE_ABS (3) ^ SCR_MSG_IN,
  2404. NADDR (msgin),
  2405. /*
  2406. ** Read the TAG from the SIDL.
  2407. ** Still an aggressive optimization. ;-)
  2408. ** Compute the CCB indirect jump address which
  2409. ** is (#TAG*2 & 0xfc) due to tag numbering using
  2410. ** 1,3,5..MAXTAGS*2+1 actual values.
  2411. */
  2412. SCR_REG_SFBR (sidl, SCR_SHL, 0),
  2413. 0,
  2414. SCR_SFBR_REG (temp, SCR_AND, 0xfc),
  2415. 0,
  2416. }/*-------------------------< JUMP_TO_NEXUS >-------------------*/,{
  2417. SCR_COPY_F (4),
  2418. RADDR (temp),
  2419. PADDR (nexus_indirect),
  2420. /*
  2421. ** Flush script prefetch if required
  2422. */
  2423. PREFETCH_FLUSH
  2424. SCR_COPY (4),
  2425. }/*-------------------------< NEXUS_INDIRECT >-------------------*/,{
  2426. 0,
  2427. RADDR (temp),
  2428. SCR_RETURN,
  2429. 0,
  2430. }/*-------------------------< RESEL_NOTAG >-------------------*/,{
  2431. /*
  2432. ** No tag expected.
  2433. ** Read an throw away the IDENTIFY.
  2434. */
  2435. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2436. NADDR (msgin),
  2437. SCR_JUMP,
  2438. PADDR (jump_to_nexus),
  2439. }/*-------------------------< DATA_IN >--------------------*/,{
  2440. /*
  2441. ** Because the size depends on the
  2442. ** #define MAX_SCATTERL parameter,
  2443. ** it is filled in at runtime.
  2444. **
  2445. ** ##===========< i=0; i<MAX_SCATTERL >=========
  2446. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2447. ** || PADDR (dispatch),
  2448. ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
  2449. ** || offsetof (struct dsb, data[ i]),
  2450. ** ##==========================================
  2451. **
  2452. **---------------------------------------------------------
  2453. */
  2454. 0
  2455. }/*-------------------------< DATA_IN2 >-------------------*/,{
  2456. SCR_CALL,
  2457. PADDR (dispatch),
  2458. SCR_JUMP,
  2459. PADDR (no_data),
  2460. }/*-------------------------< DATA_OUT >--------------------*/,{
  2461. /*
  2462. ** Because the size depends on the
  2463. ** #define MAX_SCATTERL parameter,
  2464. ** it is filled in at runtime.
  2465. **
  2466. ** ##===========< i=0; i<MAX_SCATTERL >=========
  2467. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2468. ** || PADDR (dispatch),
  2469. ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
  2470. ** || offsetof (struct dsb, data[ i]),
  2471. ** ##==========================================
  2472. **
  2473. **---------------------------------------------------------
  2474. */
  2475. 0
  2476. }/*-------------------------< DATA_OUT2 >-------------------*/,{
  2477. SCR_CALL,
  2478. PADDR (dispatch),
  2479. SCR_JUMP,
  2480. PADDR (no_data),
  2481. }/*--------------------------------------------------------*/
  2482. };
  2483. static struct scripth scripth0 __initdata = {
  2484. /*-------------------------< TRYLOOP >---------------------*/{
  2485. /*
  2486. ** Start the next entry.
  2487. ** Called addresses point to the launch script in the CCB.
  2488. ** They are patched by the main processor.
  2489. **
  2490. ** Because the size depends on the
  2491. ** #define MAX_START parameter, it is filled
  2492. ** in at runtime.
  2493. **
  2494. **-----------------------------------------------------------
  2495. **
  2496. ** ##===========< I=0; i<MAX_START >===========
  2497. ** || SCR_CALL,
  2498. ** || PADDR (idle),
  2499. ** ##==========================================
  2500. **
  2501. **-----------------------------------------------------------
  2502. */
  2503. 0
  2504. }/*------------------------< TRYLOOP2 >---------------------*/,{
  2505. SCR_JUMP,
  2506. PADDRH(tryloop),
  2507. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  2508. }/*------------------------< DONE_QUEUE >-------------------*/,{
  2509. /*
  2510. ** Copy the CCB address to the next done entry.
  2511. ** Because the size depends on the
  2512. ** #define MAX_DONE parameter, it is filled
  2513. ** in at runtime.
  2514. **
  2515. **-----------------------------------------------------------
  2516. **
  2517. ** ##===========< I=0; i<MAX_DONE >===========
  2518. ** || SCR_COPY (sizeof(struct ccb *),
  2519. ** || NADDR (header.cp),
  2520. ** || NADDR (ccb_done[i]),
  2521. ** || SCR_CALL,
  2522. ** || PADDR (done_end),
  2523. ** ##==========================================
  2524. **
  2525. **-----------------------------------------------------------
  2526. */
  2527. 0
  2528. }/*------------------------< DONE_QUEUE2 >------------------*/,{
  2529. SCR_JUMP,
  2530. PADDRH (done_queue),
  2531. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  2532. }/*------------------------< SELECT_NO_ATN >-----------------*/,{
  2533. /*
  2534. ** Set Initiator mode.
  2535. ** And try to select this target without ATN.
  2536. */
  2537. SCR_CLR (SCR_TRG),
  2538. 0,
  2539. SCR_LOAD_REG (HS_REG, HS_SELECTING),
  2540. 0,
  2541. SCR_SEL_TBL ^ offsetof (struct dsb, select),
  2542. PADDR (reselect),
  2543. SCR_JUMP,
  2544. PADDR (select2),
  2545. }/*-------------------------< CANCEL >------------------------*/,{
  2546. SCR_LOAD_REG (scratcha, HS_ABORTED),
  2547. 0,
  2548. SCR_JUMPR,
  2549. 8,
  2550. }/*-------------------------< SKIP >------------------------*/,{
  2551. SCR_LOAD_REG (scratcha, 0),
  2552. 0,
  2553. /*
  2554. ** This entry has been canceled.
  2555. ** Next time use the next slot.
  2556. */
  2557. SCR_COPY (4),
  2558. RADDR (temp),
  2559. PADDR (startpos),
  2560. /*
  2561. ** The ncr doesn't have an indirect load
  2562. ** or store command. So we have to
  2563. ** copy part of the control block to a
  2564. ** fixed place, where we can access it.
  2565. **
  2566. ** We patch the address part of a
  2567. ** COPY command with the DSA-register.
  2568. */
  2569. SCR_COPY_F (4),
  2570. RADDR (dsa),
  2571. PADDRH (skip2),
  2572. /*
  2573. ** Flush script prefetch if required
  2574. */
  2575. PREFETCH_FLUSH
  2576. /*
  2577. ** then we do the actual copy.
  2578. */
  2579. SCR_COPY (sizeof (struct head)),
  2580. /*
  2581. ** continued after the next label ...
  2582. */
  2583. }/*-------------------------< SKIP2 >---------------------*/,{
  2584. 0,
  2585. NADDR (header),
  2586. /*
  2587. ** Initialize the status registers
  2588. */
  2589. SCR_COPY (4),
  2590. NADDR (header.status),
  2591. RADDR (scr0),
  2592. /*
  2593. ** Force host status.
  2594. */
  2595. SCR_FROM_REG (scratcha),
  2596. 0,
  2597. SCR_JUMPR ^ IFFALSE (MASK (0, HS_DONEMASK)),
  2598. 16,
  2599. SCR_REG_REG (HS_REG, SCR_OR, HS_SKIPMASK),
  2600. 0,
  2601. SCR_JUMPR,
  2602. 8,
  2603. SCR_TO_REG (HS_REG),
  2604. 0,
  2605. SCR_LOAD_REG (SS_REG, SAM_STAT_GOOD),
  2606. 0,
  2607. SCR_JUMP,
  2608. PADDR (cleanup_ok),
  2609. },/*-------------------------< PAR_ERR_DATA_IN >---------------*/{
  2610. /*
  2611. ** Ignore all data in byte, until next phase
  2612. */
  2613. SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2614. PADDRH (par_err_other),
  2615. SCR_MOVE_ABS (1) ^ SCR_DATA_IN,
  2616. NADDR (scratch),
  2617. SCR_JUMPR,
  2618. -24,
  2619. },/*-------------------------< PAR_ERR_OTHER >------------------*/{
  2620. /*
  2621. ** count it.
  2622. */
  2623. SCR_REG_REG (PS_REG, SCR_ADD, 0x01),
  2624. 0,
  2625. /*
  2626. ** jump to dispatcher.
  2627. */
  2628. SCR_JUMP,
  2629. PADDR (dispatch),
  2630. }/*-------------------------< MSG_REJECT >---------------*/,{
  2631. /*
  2632. ** If a negotiation was in progress,
  2633. ** negotiation failed.
  2634. ** Otherwise, let the C code print
  2635. ** some message.
  2636. */
  2637. SCR_FROM_REG (HS_REG),
  2638. 0,
  2639. SCR_INT ^ IFFALSE (DATA (HS_NEGOTIATE)),
  2640. SIR_REJECT_RECEIVED,
  2641. SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
  2642. SIR_NEGO_FAILED,
  2643. SCR_JUMP,
  2644. PADDR (clrack),
  2645. }/*-------------------------< MSG_IGN_RESIDUE >----------*/,{
  2646. /*
  2647. ** Terminate cycle
  2648. */
  2649. SCR_CLR (SCR_ACK),
  2650. 0,
  2651. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2652. PADDR (dispatch),
  2653. /*
  2654. ** get residue size.
  2655. */
  2656. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2657. NADDR (msgin[1]),
  2658. /*
  2659. ** Size is 0 .. ignore message.
  2660. */
  2661. SCR_JUMP ^ IFTRUE (DATA (0)),
  2662. PADDR (clrack),
  2663. /*
  2664. ** Size is not 1 .. have to interrupt.
  2665. */
  2666. SCR_JUMPR ^ IFFALSE (DATA (1)),
  2667. 40,
  2668. /*
  2669. ** Check for residue byte in swide register
  2670. */
  2671. SCR_FROM_REG (scntl2),
  2672. 0,
  2673. SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
  2674. 16,
  2675. /*
  2676. ** There IS data in the swide register.
  2677. ** Discard it.
  2678. */
  2679. SCR_REG_REG (scntl2, SCR_OR, WSR),
  2680. 0,
  2681. SCR_JUMP,
  2682. PADDR (clrack),
  2683. /*
  2684. ** Load again the size to the sfbr register.
  2685. */
  2686. SCR_FROM_REG (scratcha),
  2687. 0,
  2688. SCR_INT,
  2689. SIR_IGN_RESIDUE,
  2690. SCR_JUMP,
  2691. PADDR (clrack),
  2692. }/*-------------------------< MSG_EXTENDED >-------------*/,{
  2693. /*
  2694. ** Terminate cycle
  2695. */
  2696. SCR_CLR (SCR_ACK),
  2697. 0,
  2698. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2699. PADDR (dispatch),
  2700. /*
  2701. ** get length.
  2702. */
  2703. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2704. NADDR (msgin[1]),
  2705. /*
  2706. */
  2707. SCR_JUMP ^ IFTRUE (DATA (3)),
  2708. PADDRH (msg_ext_3),
  2709. SCR_JUMP ^ IFFALSE (DATA (2)),
  2710. PADDR (msg_bad),
  2711. }/*-------------------------< MSG_EXT_2 >----------------*/,{
  2712. SCR_CLR (SCR_ACK),
  2713. 0,
  2714. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2715. PADDR (dispatch),
  2716. /*
  2717. ** get extended message code.
  2718. */
  2719. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2720. NADDR (msgin[2]),
  2721. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_WDTR)),
  2722. PADDRH (msg_wdtr),
  2723. /*
  2724. ** unknown extended message
  2725. */
  2726. SCR_JUMP,
  2727. PADDR (msg_bad)
  2728. }/*-------------------------< MSG_WDTR >-----------------*/,{
  2729. SCR_CLR (SCR_ACK),
  2730. 0,
  2731. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2732. PADDR (dispatch),
  2733. /*
  2734. ** get data bus width
  2735. */
  2736. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2737. NADDR (msgin[3]),
  2738. /*
  2739. ** let the host do the real work.
  2740. */
  2741. SCR_INT,
  2742. SIR_NEGO_WIDE,
  2743. /*
  2744. ** let the target fetch our answer.
  2745. */
  2746. SCR_SET (SCR_ATN),
  2747. 0,
  2748. SCR_CLR (SCR_ACK),
  2749. 0,
  2750. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  2751. PADDRH (nego_bad_phase),
  2752. }/*-------------------------< SEND_WDTR >----------------*/,{
  2753. /*
  2754. ** Send the EXTENDED_WDTR
  2755. */
  2756. SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
  2757. NADDR (msgout),
  2758. SCR_COPY (1),
  2759. NADDR (msgout),
  2760. NADDR (lastmsg),
  2761. SCR_JUMP,
  2762. PADDR (msg_out_done),
  2763. }/*-------------------------< MSG_EXT_3 >----------------*/,{
  2764. SCR_CLR (SCR_ACK),
  2765. 0,
  2766. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2767. PADDR (dispatch),
  2768. /*
  2769. ** get extended message code.
  2770. */
  2771. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  2772. NADDR (msgin[2]),
  2773. SCR_JUMP ^ IFTRUE (DATA (EXTENDED_SDTR)),
  2774. PADDRH (msg_sdtr),
  2775. /*
  2776. ** unknown extended message
  2777. */
  2778. SCR_JUMP,
  2779. PADDR (msg_bad)
  2780. }/*-------------------------< MSG_SDTR >-----------------*/,{
  2781. SCR_CLR (SCR_ACK),
  2782. 0,
  2783. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
  2784. PADDR (dispatch),
  2785. /*
  2786. ** get period and offset
  2787. */
  2788. SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
  2789. NADDR (msgin[3]),
  2790. /*
  2791. ** let the host do the real work.
  2792. */
  2793. SCR_INT,
  2794. SIR_NEGO_SYNC,
  2795. /*
  2796. ** let the target fetch our answer.
  2797. */
  2798. SCR_SET (SCR_ATN),
  2799. 0,
  2800. SCR_CLR (SCR_ACK),
  2801. 0,
  2802. SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
  2803. PADDRH (nego_bad_phase),
  2804. }/*-------------------------< SEND_SDTR >-------------*/,{
  2805. /*
  2806. ** Send the EXTENDED_SDTR
  2807. */
  2808. SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
  2809. NADDR (msgout),
  2810. SCR_COPY (1),
  2811. NADDR (msgout),
  2812. NADDR (lastmsg),
  2813. SCR_JUMP,
  2814. PADDR (msg_out_done),
  2815. }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
  2816. SCR_INT,
  2817. SIR_NEGO_PROTO,
  2818. SCR_JUMP,
  2819. PADDR (dispatch),
  2820. }/*-------------------------< MSG_OUT_ABORT >-------------*/,{
  2821. /*
  2822. ** After ABORT message,
  2823. **
  2824. ** expect an immediate disconnect, ...
  2825. */
  2826. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2827. 0,
  2828. SCR_CLR (SCR_ACK|SCR_ATN),
  2829. 0,
  2830. SCR_WAIT_DISC,
  2831. 0,
  2832. /*
  2833. ** ... and set the status to "ABORTED"
  2834. */
  2835. SCR_LOAD_REG (HS_REG, HS_ABORTED),
  2836. 0,
  2837. SCR_JUMP,
  2838. PADDR (cleanup),
  2839. }/*-------------------------< HDATA_IN >-------------------*/,{
  2840. /*
  2841. ** Because the size depends on the
  2842. ** #define MAX_SCATTERH parameter,
  2843. ** it is filled in at runtime.
  2844. **
  2845. ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
  2846. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2847. ** || PADDR (dispatch),
  2848. ** || SCR_MOVE_TBL ^ SCR_DATA_IN,
  2849. ** || offsetof (struct dsb, data[ i]),
  2850. ** ##===================================================
  2851. **
  2852. **---------------------------------------------------------
  2853. */
  2854. 0
  2855. }/*-------------------------< HDATA_IN2 >------------------*/,{
  2856. SCR_JUMP,
  2857. PADDR (data_in),
  2858. }/*-------------------------< HDATA_OUT >-------------------*/,{
  2859. /*
  2860. ** Because the size depends on the
  2861. ** #define MAX_SCATTERH parameter,
  2862. ** it is filled in at runtime.
  2863. **
  2864. ** ##==< i=MAX_SCATTERL; i<MAX_SCATTERL+MAX_SCATTERH >==
  2865. ** || SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT)),
  2866. ** || PADDR (dispatch),
  2867. ** || SCR_MOVE_TBL ^ SCR_DATA_OUT,
  2868. ** || offsetof (struct dsb, data[ i]),
  2869. ** ##===================================================
  2870. **
  2871. **---------------------------------------------------------
  2872. */
  2873. 0
  2874. }/*-------------------------< HDATA_OUT2 >------------------*/,{
  2875. SCR_JUMP,
  2876. PADDR (data_out),
  2877. }/*-------------------------< RESET >----------------------*/,{
  2878. /*
  2879. ** Send a TARGET_RESET message if bad IDENTIFY
  2880. ** received on reselection.
  2881. */
  2882. SCR_LOAD_REG (scratcha, ABORT_TASK),
  2883. 0,
  2884. SCR_JUMP,
  2885. PADDRH (abort_resel),
  2886. }/*-------------------------< ABORTTAG >-------------------*/,{
  2887. /*
  2888. ** Abort a wrong tag received on reselection.
  2889. */
  2890. SCR_LOAD_REG (scratcha, ABORT_TASK),
  2891. 0,
  2892. SCR_JUMP,
  2893. PADDRH (abort_resel),
  2894. }/*-------------------------< ABORT >----------------------*/,{
  2895. /*
  2896. ** Abort a reselection when no active CCB.
  2897. */
  2898. SCR_LOAD_REG (scratcha, ABORT_TASK_SET),
  2899. 0,
  2900. }/*-------------------------< ABORT_RESEL >----------------*/,{
  2901. SCR_COPY (1),
  2902. RADDR (scratcha),
  2903. NADDR (msgout),
  2904. SCR_SET (SCR_ATN),
  2905. 0,
  2906. SCR_CLR (SCR_ACK),
  2907. 0,
  2908. /*
  2909. ** and send it.
  2910. ** we expect an immediate disconnect
  2911. */
  2912. SCR_REG_REG (scntl2, SCR_AND, 0x7f),
  2913. 0,
  2914. SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
  2915. NADDR (msgout),
  2916. SCR_COPY (1),
  2917. NADDR (msgout),
  2918. NADDR (lastmsg),
  2919. SCR_CLR (SCR_ACK|SCR_ATN),
  2920. 0,
  2921. SCR_WAIT_DISC,
  2922. 0,
  2923. SCR_JUMP,
  2924. PADDR (start),
  2925. }/*-------------------------< RESEND_IDENT >-------------------*/,{
  2926. /*
  2927. ** The target stays in MSG OUT phase after having acked
  2928. ** Identify [+ Tag [+ Extended message ]]. Targets shall
  2929. ** behave this way on parity error.
  2930. ** We must send it again all the messages.
  2931. */
  2932. SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
  2933. 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
  2934. SCR_JUMP,
  2935. PADDR (send_ident),
  2936. }/*-------------------------< CLRATN_GO_ON >-------------------*/,{
  2937. SCR_CLR (SCR_ATN),
  2938. 0,
  2939. SCR_JUMP,
  2940. }/*-------------------------< NXTDSP_GO_ON >-------------------*/,{
  2941. 0,
  2942. }/*-------------------------< SDATA_IN >-------------------*/,{
  2943. SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN)),
  2944. PADDR (dispatch),
  2945. SCR_MOVE_TBL ^ SCR_DATA_IN,
  2946. offsetof (struct dsb, sense),
  2947. SCR_CALL,
  2948. PADDR (dispatch),
  2949. SCR_JUMP,
  2950. PADDR (no_data),
  2951. }/*-------------------------< DATA_IO >--------------------*/,{
  2952. /*
  2953. ** We jump here if the data direction was unknown at the
  2954. ** time we had to queue the command to the scripts processor.
  2955. ** Pointers had been set as follow in this situation:
  2956. ** savep --> DATA_IO
  2957. ** lastp --> start pointer when DATA_IN
  2958. ** goalp --> goal pointer when DATA_IN
  2959. ** wlastp --> start pointer when DATA_OUT
  2960. ** wgoalp --> goal pointer when DATA_OUT
  2961. ** This script sets savep/lastp/goalp according to the
  2962. ** direction chosen by the target.
  2963. */
  2964. SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_OUT)),
  2965. 32,
  2966. /*
  2967. ** Direction is DATA IN.
  2968. ** Warning: we jump here, even when phase is DATA OUT.
  2969. */
  2970. SCR_COPY (4),
  2971. NADDR (header.lastp),
  2972. NADDR (header.savep),
  2973. /*
  2974. ** Jump to the SCRIPTS according to actual direction.
  2975. */
  2976. SCR_COPY (4),
  2977. NADDR (header.savep),
  2978. RADDR (temp),
  2979. SCR_RETURN,
  2980. 0,
  2981. /*
  2982. ** Direction is DATA OUT.
  2983. */
  2984. SCR_COPY (4),
  2985. NADDR (header.wlastp),
  2986. NADDR (header.lastp),
  2987. SCR_COPY (4),
  2988. NADDR (header.wgoalp),
  2989. NADDR (header.goalp),
  2990. SCR_JUMPR,
  2991. -64,
  2992. }/*-------------------------< BAD_IDENTIFY >---------------*/,{
  2993. /*
  2994. ** If message phase but not an IDENTIFY,
  2995. ** get some help from the C code.
  2996. ** Old SCSI device may behave so.
  2997. */
  2998. SCR_JUMPR ^ IFTRUE (MASK (0x80, 0x80)),
  2999. 16,
  3000. SCR_INT,
  3001. SIR_RESEL_NO_IDENTIFY,
  3002. SCR_JUMP,
  3003. PADDRH (reset),
  3004. /*
  3005. ** Message is an IDENTIFY, but lun is unknown.
  3006. ** Read the message, since we got it directly
  3007. ** from the SCSI BUS data lines.
  3008. ** Signal problem to C code for logging the event.
  3009. ** Send an ABORT_TASK_SET to clear all pending tasks.
  3010. */
  3011. SCR_INT,
  3012. SIR_RESEL_BAD_LUN,
  3013. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  3014. NADDR (msgin),
  3015. SCR_JUMP,
  3016. PADDRH (abort),
  3017. }/*-------------------------< BAD_I_T_L >------------------*/,{
  3018. /*
  3019. ** We donnot have a task for that I_T_L.
  3020. ** Signal problem to C code for logging the event.
  3021. ** Send an ABORT_TASK_SET message.
  3022. */
  3023. SCR_INT,
  3024. SIR_RESEL_BAD_I_T_L,
  3025. SCR_JUMP,
  3026. PADDRH (abort),
  3027. }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
  3028. /*
  3029. ** We donnot have a task that matches the tag.
  3030. ** Signal problem to C code for logging the event.
  3031. ** Send an ABORT_TASK message.
  3032. */
  3033. SCR_INT,
  3034. SIR_RESEL_BAD_I_T_L_Q,
  3035. SCR_JUMP,
  3036. PADDRH (aborttag),
  3037. }/*-------------------------< BAD_TARGET >-----------------*/,{
  3038. /*
  3039. ** We donnot know the target that reselected us.
  3040. ** Grab the first message if any (IDENTIFY).
  3041. ** Signal problem to C code for logging the event.
  3042. ** TARGET_RESET message.
  3043. */
  3044. SCR_INT,
  3045. SIR_RESEL_BAD_TARGET,
  3046. SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_IN)),
  3047. 8,
  3048. SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
  3049. NADDR (msgin),
  3050. SCR_JUMP,
  3051. PADDRH (reset),
  3052. }/*-------------------------< BAD_STATUS >-----------------*/,{
  3053. /*
  3054. ** If command resulted in either TASK_SET FULL,
  3055. ** CHECK CONDITION or COMMAND TERMINATED,
  3056. ** call the C code.
  3057. */
  3058. SCR_INT ^ IFTRUE (DATA (SAM_STAT_TASK_SET_FULL)),
  3059. SIR_BAD_STATUS,
  3060. SCR_INT ^ IFTRUE (DATA (SAM_STAT_CHECK_CONDITION)),
  3061. SIR_BAD_STATUS,
  3062. SCR_INT ^ IFTRUE (DATA (SAM_STAT_COMMAND_TERMINATED)),
  3063. SIR_BAD_STATUS,
  3064. SCR_RETURN,
  3065. 0,
  3066. }/*-------------------------< START_RAM >-------------------*/,{
  3067. /*
  3068. ** Load the script into on-chip RAM,
  3069. ** and jump to start point.
  3070. */
  3071. SCR_COPY_F (4),
  3072. RADDR (scratcha),
  3073. PADDRH (start_ram0),
  3074. /*
  3075. ** Flush script prefetch if required
  3076. */
  3077. PREFETCH_FLUSH
  3078. SCR_COPY (sizeof (struct script)),
  3079. }/*-------------------------< START_RAM0 >--------------------*/,{
  3080. 0,
  3081. PADDR (start),
  3082. SCR_JUMP,
  3083. PADDR (start),
  3084. }/*-------------------------< STO_RESTART >-------------------*/,{
  3085. /*
  3086. **
  3087. ** Repair start queue (e.g. next time use the next slot)
  3088. ** and jump to start point.
  3089. */
  3090. SCR_COPY (4),
  3091. RADDR (temp),
  3092. PADDR (startpos),
  3093. SCR_JUMP,
  3094. PADDR (start),
  3095. }/*-------------------------< WAIT_DMA >-------------------*/,{
  3096. /*
  3097. ** For HP Zalon/53c720 systems, the Zalon interface
  3098. ** between CPU and 53c720 does prefetches, which causes
  3099. ** problems with self modifying scripts. The problem
  3100. ** is overcome by calling a dummy subroutine after each
  3101. ** modification, to force a refetch of the script on
  3102. ** return from the subroutine.
  3103. */
  3104. SCR_RETURN,
  3105. 0,
  3106. }/*-------------------------< SNOOPTEST >-------------------*/,{
  3107. /*
  3108. ** Read the variable.
  3109. */
  3110. SCR_COPY (4),
  3111. NADDR(ncr_cache),
  3112. RADDR (scratcha),
  3113. /*
  3114. ** Write the variable.
  3115. */
  3116. SCR_COPY (4),
  3117. RADDR (temp),
  3118. NADDR(ncr_cache),
  3119. /*
  3120. ** Read back the variable.
  3121. */
  3122. SCR_COPY (4),
  3123. NADDR(ncr_cache),
  3124. RADDR (temp),
  3125. }/*-------------------------< SNOOPEND >-------------------*/,{
  3126. /*
  3127. ** And stop.
  3128. */
  3129. SCR_INT,
  3130. 99,
  3131. }/*--------------------------------------------------------*/
  3132. };
  3133. /*==========================================================
  3134. **
  3135. **
  3136. ** Fill in #define dependent parts of the script
  3137. **
  3138. **
  3139. **==========================================================
  3140. */
  3141. void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
  3142. {
  3143. int i;
  3144. ncrcmd *p;
  3145. p = scrh->tryloop;
  3146. for (i=0; i<MAX_START; i++) {
  3147. *p++ =SCR_CALL;
  3148. *p++ =PADDR (idle);
  3149. }
  3150. BUG_ON((u_long)p != (u_long)&scrh->tryloop + sizeof (scrh->tryloop));
  3151. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  3152. p = scrh->done_queue;
  3153. for (i = 0; i<MAX_DONE; i++) {
  3154. *p++ =SCR_COPY (sizeof(struct ccb *));
  3155. *p++ =NADDR (header.cp);
  3156. *p++ =NADDR (ccb_done[i]);
  3157. *p++ =SCR_CALL;
  3158. *p++ =PADDR (done_end);
  3159. }
  3160. BUG_ON((u_long)p != (u_long)&scrh->done_queue+sizeof(scrh->done_queue));
  3161. #endif /* SCSI_NCR_CCB_DONE_SUPPORT */
  3162. p = scrh->hdata_in;
  3163. for (i=0; i<MAX_SCATTERH; i++) {
  3164. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
  3165. *p++ =PADDR (dispatch);
  3166. *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
  3167. *p++ =offsetof (struct dsb, data[i]);
  3168. }
  3169. BUG_ON((u_long)p != (u_long)&scrh->hdata_in + sizeof (scrh->hdata_in));
  3170. p = scr->data_in;
  3171. for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
  3172. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_IN));
  3173. *p++ =PADDR (dispatch);
  3174. *p++ =SCR_MOVE_TBL ^ SCR_DATA_IN;
  3175. *p++ =offsetof (struct dsb, data[i]);
  3176. }
  3177. BUG_ON((u_long)p != (u_long)&scr->data_in + sizeof (scr->data_in));
  3178. p = scrh->hdata_out;
  3179. for (i=0; i<MAX_SCATTERH; i++) {
  3180. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
  3181. *p++ =PADDR (dispatch);
  3182. *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
  3183. *p++ =offsetof (struct dsb, data[i]);
  3184. }
  3185. BUG_ON((u_long)p != (u_long)&scrh->hdata_out + sizeof (scrh->hdata_out));
  3186. p = scr->data_out;
  3187. for (i=MAX_SCATTERH; i<MAX_SCATTERH+MAX_SCATTERL; i++) {
  3188. *p++ =SCR_CALL ^ IFFALSE (WHEN (SCR_DATA_OUT));
  3189. *p++ =PADDR (dispatch);
  3190. *p++ =SCR_MOVE_TBL ^ SCR_DATA_OUT;
  3191. *p++ =offsetof (struct dsb, data[i]);
  3192. }
  3193. BUG_ON((u_long) p != (u_long)&scr->data_out + sizeof (scr->data_out));
  3194. }
  3195. /*==========================================================
  3196. **
  3197. **
  3198. ** Copy and rebind a script.
  3199. **
  3200. **
  3201. **==========================================================
  3202. */
  3203. static void __init
  3204. ncr_script_copy_and_bind (struct ncb *np, ncrcmd *src, ncrcmd *dst, int len)
  3205. {
  3206. ncrcmd opcode, new, old, tmp1, tmp2;
  3207. ncrcmd *start, *end;
  3208. int relocs;
  3209. int opchanged = 0;
  3210. start = src;
  3211. end = src + len/4;
  3212. while (src < end) {
  3213. opcode = *src++;
  3214. *dst++ = cpu_to_scr(opcode);
  3215. /*
  3216. ** If we forget to change the length
  3217. ** in struct script, a field will be
  3218. ** padded with 0. This is an illegal
  3219. ** command.
  3220. */
  3221. if (opcode == 0) {
  3222. printk (KERN_ERR "%s: ERROR0 IN SCRIPT at %d.\n",
  3223. ncr_name(np), (int) (src-start-1));
  3224. mdelay(1000);
  3225. }
  3226. if (DEBUG_FLAGS & DEBUG_SCRIPT)
  3227. printk (KERN_DEBUG "%p: <%x>\n",
  3228. (src-1), (unsigned)opcode);
  3229. /*
  3230. ** We don't have to decode ALL commands
  3231. */
  3232. switch (opcode >> 28) {
  3233. case 0xc:
  3234. /*
  3235. ** COPY has TWO arguments.
  3236. */
  3237. relocs = 2;
  3238. tmp1 = src[0];
  3239. #ifdef RELOC_KVAR
  3240. if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
  3241. tmp1 = 0;
  3242. #endif
  3243. tmp2 = src[1];
  3244. #ifdef RELOC_KVAR
  3245. if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
  3246. tmp2 = 0;
  3247. #endif
  3248. if ((tmp1 ^ tmp2) & 3) {
  3249. printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
  3250. ncr_name(np), (int) (src-start-1));
  3251. mdelay(1000);
  3252. }
  3253. /*
  3254. ** If PREFETCH feature not enabled, remove
  3255. ** the NO FLUSH bit if present.
  3256. */
  3257. if ((opcode & SCR_NO_FLUSH) && !(np->features & FE_PFEN)) {
  3258. dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
  3259. ++opchanged;
  3260. }
  3261. break;
  3262. case 0x0:
  3263. /*
  3264. ** MOVE (absolute address)
  3265. */
  3266. relocs = 1;
  3267. break;
  3268. case 0x8:
  3269. /*
  3270. ** JUMP / CALL
  3271. ** don't relocate if relative :-)
  3272. */
  3273. if (opcode & 0x00800000)
  3274. relocs = 0;
  3275. else
  3276. relocs = 1;
  3277. break;
  3278. case 0x4:
  3279. case 0x5:
  3280. case 0x6:
  3281. case 0x7:
  3282. relocs = 1;
  3283. break;
  3284. default:
  3285. relocs = 0;
  3286. break;
  3287. }
  3288. if (relocs) {
  3289. while (relocs--) {
  3290. old = *src++;
  3291. switch (old & RELOC_MASK) {
  3292. case RELOC_REGISTER:
  3293. new = (old & ~RELOC_MASK) + np->paddr;
  3294. break;
  3295. case RELOC_LABEL:
  3296. new = (old & ~RELOC_MASK) + np->p_script;
  3297. break;
  3298. case RELOC_LABELH:
  3299. new = (old & ~RELOC_MASK) + np->p_scripth;
  3300. break;
  3301. case RELOC_SOFTC:
  3302. new = (old & ~RELOC_MASK) + np->p_ncb;
  3303. break;
  3304. #ifdef RELOC_KVAR
  3305. case RELOC_KVAR:
  3306. if (((old & ~RELOC_MASK) <
  3307. SCRIPT_KVAR_FIRST) ||
  3308. ((old & ~RELOC_MASK) >
  3309. SCRIPT_KVAR_LAST))
  3310. panic("ncr KVAR out of range");
  3311. new = vtophys(script_kvars[old &
  3312. ~RELOC_MASK]);
  3313. break;
  3314. #endif
  3315. case 0:
  3316. /* Don't relocate a 0 address. */
  3317. if (old == 0) {
  3318. new = old;
  3319. break;
  3320. }
  3321. fallthrough;
  3322. default:
  3323. panic("ncr_script_copy_and_bind: weird relocation %x\n", old);
  3324. break;
  3325. }
  3326. *dst++ = cpu_to_scr(new);
  3327. }
  3328. } else
  3329. *dst++ = cpu_to_scr(*src++);
  3330. }
  3331. }
  3332. /*
  3333. ** Linux host data structure
  3334. */
  3335. struct host_data {
  3336. struct ncb *ncb;
  3337. };
  3338. #define PRINT_ADDR(cmd, arg...) dev_info(&cmd->device->sdev_gendev , ## arg)
  3339. static void ncr_print_msg(struct ccb *cp, char *label, u_char *msg)
  3340. {
  3341. PRINT_ADDR(cp->cmd, "%s: ", label);
  3342. spi_print_msg(msg);
  3343. printk("\n");
  3344. }
  3345. /*==========================================================
  3346. **
  3347. ** NCR chip clock divisor table.
  3348. ** Divisors are multiplied by 10,000,000 in order to make
  3349. ** calculations more simple.
  3350. **
  3351. **==========================================================
  3352. */
  3353. #define _5M 5000000
  3354. static u_long div_10M[] =
  3355. {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
  3356. /*===============================================================
  3357. **
  3358. ** Prepare io register values used by ncr_init() according
  3359. ** to selected and supported features.
  3360. **
  3361. ** NCR chips allow burst lengths of 2, 4, 8, 16, 32, 64, 128
  3362. ** transfers. 32,64,128 are only supported by 875 and 895 chips.
  3363. ** We use log base 2 (burst length) as internal code, with
  3364. ** value 0 meaning "burst disabled".
  3365. **
  3366. **===============================================================
  3367. */
  3368. /*
  3369. * Burst length from burst code.
  3370. */
  3371. #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
  3372. /*
  3373. * Burst code from io register bits. Burst enable is ctest0 for c720
  3374. */
  3375. #define burst_code(dmode, ctest0) \
  3376. (ctest0) & 0x80 ? 0 : (((dmode) & 0xc0) >> 6) + 1
  3377. /*
  3378. * Set initial io register bits from burst code.
  3379. */
  3380. static inline void ncr_init_burst(struct ncb *np, u_char bc)
  3381. {
  3382. u_char *be = &np->rv_ctest0;
  3383. *be &= ~0x80;
  3384. np->rv_dmode &= ~(0x3 << 6);
  3385. np->rv_ctest5 &= ~0x4;
  3386. if (!bc) {
  3387. *be |= 0x80;
  3388. } else {
  3389. --bc;
  3390. np->rv_dmode |= ((bc & 0x3) << 6);
  3391. np->rv_ctest5 |= (bc & 0x4);
  3392. }
  3393. }
  3394. static void __init ncr_prepare_setting(struct ncb *np)
  3395. {
  3396. u_char burst_max;
  3397. u_long period;
  3398. int i;
  3399. /*
  3400. ** Save assumed BIOS setting
  3401. */
  3402. np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
  3403. np->sv_scntl3 = INB(nc_scntl3) & 0x07;
  3404. np->sv_dmode = INB(nc_dmode) & 0xce;
  3405. np->sv_dcntl = INB(nc_dcntl) & 0xa8;
  3406. np->sv_ctest0 = INB(nc_ctest0) & 0x84;
  3407. np->sv_ctest3 = INB(nc_ctest3) & 0x01;
  3408. np->sv_ctest4 = INB(nc_ctest4) & 0x80;
  3409. np->sv_ctest5 = INB(nc_ctest5) & 0x24;
  3410. np->sv_gpcntl = INB(nc_gpcntl);
  3411. np->sv_stest2 = INB(nc_stest2) & 0x20;
  3412. np->sv_stest4 = INB(nc_stest4);
  3413. /*
  3414. ** Wide ?
  3415. */
  3416. np->maxwide = (np->features & FE_WIDE)? 1 : 0;
  3417. /*
  3418. * Guess the frequency of the chip's clock.
  3419. */
  3420. if (np->features & FE_ULTRA)
  3421. np->clock_khz = 80000;
  3422. else
  3423. np->clock_khz = 40000;
  3424. /*
  3425. * Get the clock multiplier factor.
  3426. */
  3427. if (np->features & FE_QUAD)
  3428. np->multiplier = 4;
  3429. else if (np->features & FE_DBLR)
  3430. np->multiplier = 2;
  3431. else
  3432. np->multiplier = 1;
  3433. /*
  3434. * Measure SCSI clock frequency for chips
  3435. * it may vary from assumed one.
  3436. */
  3437. if (np->features & FE_VARCLK)
  3438. ncr_getclock(np, np->multiplier);
  3439. /*
  3440. * Divisor to be used for async (timer pre-scaler).
  3441. */
  3442. i = np->clock_divn - 1;
  3443. while (--i >= 0) {
  3444. if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz > div_10M[i]) {
  3445. ++i;
  3446. break;
  3447. }
  3448. }
  3449. np->rv_scntl3 = i+1;
  3450. /*
  3451. * Minimum synchronous period factor supported by the chip.
  3452. * Btw, 'period' is in tenths of nanoseconds.
  3453. */
  3454. period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
  3455. if (period <= 250) np->minsync = 10;
  3456. else if (period <= 303) np->minsync = 11;
  3457. else if (period <= 500) np->minsync = 12;
  3458. else np->minsync = (period + 40 - 1) / 40;
  3459. /*
  3460. * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
  3461. */
  3462. if (np->minsync < 25 && !(np->features & FE_ULTRA))
  3463. np->minsync = 25;
  3464. /*
  3465. * Maximum synchronous period factor supported by the chip.
  3466. */
  3467. period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
  3468. np->maxsync = period > 2540 ? 254 : period / 10;
  3469. /*
  3470. ** Prepare initial value of other IO registers
  3471. */
  3472. #if defined SCSI_NCR_TRUST_BIOS_SETTING
  3473. np->rv_scntl0 = np->sv_scntl0;
  3474. np->rv_dmode = np->sv_dmode;
  3475. np->rv_dcntl = np->sv_dcntl;
  3476. np->rv_ctest0 = np->sv_ctest0;
  3477. np->rv_ctest3 = np->sv_ctest3;
  3478. np->rv_ctest4 = np->sv_ctest4;
  3479. np->rv_ctest5 = np->sv_ctest5;
  3480. burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
  3481. #else
  3482. /*
  3483. ** Select burst length (dwords)
  3484. */
  3485. burst_max = driver_setup.burst_max;
  3486. if (burst_max == 255)
  3487. burst_max = burst_code(np->sv_dmode, np->sv_ctest0);
  3488. if (burst_max > 7)
  3489. burst_max = 7;
  3490. if (burst_max > np->maxburst)
  3491. burst_max = np->maxburst;
  3492. /*
  3493. ** Select all supported special features
  3494. */
  3495. if (np->features & FE_ERL)
  3496. np->rv_dmode |= ERL; /* Enable Read Line */
  3497. if (np->features & FE_BOF)
  3498. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  3499. if (np->features & FE_ERMP)
  3500. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  3501. if (np->features & FE_PFEN)
  3502. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  3503. if (np->features & FE_CLSE)
  3504. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  3505. if (np->features & FE_WRIE)
  3506. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  3507. if (np->features & FE_DFS)
  3508. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  3509. if (np->features & FE_MUX)
  3510. np->rv_ctest4 |= MUX; /* Host bus multiplex mode */
  3511. if (np->features & FE_EA)
  3512. np->rv_dcntl |= EA; /* Enable ACK */
  3513. if (np->features & FE_EHP)
  3514. np->rv_ctest0 |= EHP; /* Even host parity */
  3515. /*
  3516. ** Select some other
  3517. */
  3518. if (driver_setup.master_parity)
  3519. np->rv_ctest4 |= MPEE; /* Master parity checking */
  3520. if (driver_setup.scsi_parity)
  3521. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  3522. /*
  3523. ** Get SCSI addr of host adapter (set by bios?).
  3524. */
  3525. if (np->myaddr == 255) {
  3526. np->myaddr = INB(nc_scid) & 0x07;
  3527. if (!np->myaddr)
  3528. np->myaddr = SCSI_NCR_MYADDR;
  3529. }
  3530. #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
  3531. /*
  3532. * Prepare initial io register bits for burst length
  3533. */
  3534. ncr_init_burst(np, burst_max);
  3535. /*
  3536. ** Set SCSI BUS mode.
  3537. **
  3538. ** - ULTRA2 chips (895/895A/896) report the current
  3539. ** BUS mode through the STEST4 IO register.
  3540. ** - For previous generation chips (825/825A/875),
  3541. ** user has to tell us how to check against HVD,
  3542. ** since a 100% safe algorithm is not possible.
  3543. */
  3544. np->scsi_mode = SMODE_SE;
  3545. if (np->features & FE_DIFF) {
  3546. switch(driver_setup.diff_support) {
  3547. case 4: /* Trust previous settings if present, then GPIO3 */
  3548. if (np->sv_scntl3) {
  3549. if (np->sv_stest2 & 0x20)
  3550. np->scsi_mode = SMODE_HVD;
  3551. break;
  3552. }
  3553. fallthrough;
  3554. case 3: /* SYMBIOS controllers report HVD through GPIO3 */
  3555. if (INB(nc_gpreg) & 0x08)
  3556. break;
  3557. fallthrough;
  3558. case 2: /* Set HVD unconditionally */
  3559. np->scsi_mode = SMODE_HVD;
  3560. fallthrough;
  3561. case 1: /* Trust previous settings for HVD */
  3562. if (np->sv_stest2 & 0x20)
  3563. np->scsi_mode = SMODE_HVD;
  3564. break;
  3565. default:/* Don't care about HVD */
  3566. break;
  3567. }
  3568. }
  3569. if (np->scsi_mode == SMODE_HVD)
  3570. np->rv_stest2 |= 0x20;
  3571. /*
  3572. ** Set LED support from SCRIPTS.
  3573. ** Ignore this feature for boards known to use a
  3574. ** specific GPIO wiring and for the 895A or 896
  3575. ** that drive the LED directly.
  3576. ** Also probe initial setting of GPIO0 as output.
  3577. */
  3578. if ((driver_setup.led_pin) &&
  3579. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  3580. np->features |= FE_LED0;
  3581. /*
  3582. ** Set irq mode.
  3583. */
  3584. switch(driver_setup.irqm & 3) {
  3585. case 2:
  3586. np->rv_dcntl |= IRQM;
  3587. break;
  3588. case 1:
  3589. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  3590. break;
  3591. default:
  3592. break;
  3593. }
  3594. /*
  3595. ** Configure targets according to driver setup.
  3596. ** Allow to override sync, wide and NOSCAN from
  3597. ** boot command line.
  3598. */
  3599. for (i = 0 ; i < MAX_TARGET ; i++) {
  3600. struct tcb *tp = &np->target[i];
  3601. tp->usrsync = driver_setup.default_sync;
  3602. tp->usrwide = driver_setup.max_wide;
  3603. tp->usrtags = MAX_TAGS;
  3604. tp->period = 0xffff;
  3605. if (!driver_setup.disconnection)
  3606. np->target[i].usrflag = UF_NODISC;
  3607. }
  3608. /*
  3609. ** Announce all that stuff to user.
  3610. */
  3611. printk(KERN_INFO "%s: ID %d, Fast-%d%s%s\n", ncr_name(np),
  3612. np->myaddr,
  3613. np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10),
  3614. (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
  3615. (np->rv_stest2 & 0x20) ? ", Differential" : "");
  3616. if (bootverbose > 1) {
  3617. printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  3618. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  3619. ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  3620. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  3621. printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  3622. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  3623. ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  3624. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  3625. }
  3626. if (bootverbose && np->paddr2)
  3627. printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
  3628. ncr_name(np), np->paddr2);
  3629. }
  3630. /*==========================================================
  3631. **
  3632. **
  3633. ** Done SCSI commands list management.
  3634. **
  3635. ** We donnot enter the scsi_done() callback immediately
  3636. ** after a command has been seen as completed but we
  3637. ** insert it into a list which is flushed outside any kind
  3638. ** of driver critical section.
  3639. ** This allows to do minimal stuff under interrupt and
  3640. ** inside critical sections and to also avoid locking up
  3641. ** on recursive calls to driver entry points under SMP.
  3642. ** In fact, the only kernel point which is entered by the
  3643. ** driver with a driver lock set is kmalloc(GFP_ATOMIC)
  3644. ** that shall not reenter the driver under any circumstances,
  3645. ** AFAIK.
  3646. **
  3647. **==========================================================
  3648. */
  3649. static inline void ncr_queue_done_cmd(struct ncb *np, struct scsi_cmnd *cmd)
  3650. {
  3651. unmap_scsi_data(np, cmd);
  3652. cmd->host_scribble = (char *) np->done_list;
  3653. np->done_list = cmd;
  3654. }
  3655. static inline void ncr_flush_done_cmds(struct scsi_cmnd *lcmd)
  3656. {
  3657. struct scsi_cmnd *cmd;
  3658. while (lcmd) {
  3659. cmd = lcmd;
  3660. lcmd = (struct scsi_cmnd *) cmd->host_scribble;
  3661. scsi_done(cmd);
  3662. }
  3663. }
  3664. /*==========================================================
  3665. **
  3666. **
  3667. ** Prepare the next negotiation message if needed.
  3668. **
  3669. ** Fill in the part of message buffer that contains the
  3670. ** negotiation and the nego_status field of the CCB.
  3671. ** Returns the size of the message in bytes.
  3672. **
  3673. **
  3674. **==========================================================
  3675. */
  3676. static int ncr_prepare_nego(struct ncb *np, struct ccb *cp, u_char *msgptr)
  3677. {
  3678. struct tcb *tp = &np->target[cp->target];
  3679. int msglen = 0;
  3680. int nego = 0;
  3681. struct scsi_target *starget = tp->starget;
  3682. /* negotiate wide transfers ? */
  3683. if (!tp->widedone) {
  3684. if (spi_support_wide(starget)) {
  3685. nego = NS_WIDE;
  3686. } else
  3687. tp->widedone=1;
  3688. }
  3689. /* negotiate synchronous transfers? */
  3690. if (!nego && !tp->period) {
  3691. if (spi_support_sync(starget)) {
  3692. nego = NS_SYNC;
  3693. } else {
  3694. tp->period =0xffff;
  3695. dev_info(&starget->dev, "target did not report SYNC.\n");
  3696. }
  3697. }
  3698. switch (nego) {
  3699. case NS_SYNC:
  3700. msglen += spi_populate_sync_msg(msgptr + msglen,
  3701. tp->maxoffs ? tp->minsync : 0, tp->maxoffs);
  3702. break;
  3703. case NS_WIDE:
  3704. msglen += spi_populate_width_msg(msgptr + msglen, tp->usrwide);
  3705. break;
  3706. }
  3707. cp->nego_status = nego;
  3708. if (nego) {
  3709. tp->nego_cp = cp;
  3710. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3711. ncr_print_msg(cp, nego == NS_WIDE ?
  3712. "wide msgout":"sync_msgout", msgptr);
  3713. }
  3714. }
  3715. return msglen;
  3716. }
  3717. /*==========================================================
  3718. **
  3719. **
  3720. ** Start execution of a SCSI command.
  3721. ** This is called from the generic SCSI driver.
  3722. **
  3723. **
  3724. **==========================================================
  3725. */
  3726. static int ncr_queue_command (struct ncb *np, struct scsi_cmnd *cmd)
  3727. {
  3728. struct scsi_device *sdev = cmd->device;
  3729. struct tcb *tp = &np->target[sdev->id];
  3730. struct lcb *lp = tp->lp[sdev->lun];
  3731. struct ccb *cp;
  3732. int segments;
  3733. u_char idmsg, *msgptr;
  3734. u32 msglen;
  3735. int direction;
  3736. u32 lastp, goalp;
  3737. /*---------------------------------------------
  3738. **
  3739. ** Some shortcuts ...
  3740. **
  3741. **---------------------------------------------
  3742. */
  3743. if ((sdev->id == np->myaddr ) ||
  3744. (sdev->id >= MAX_TARGET) ||
  3745. (sdev->lun >= MAX_LUN )) {
  3746. return(DID_BAD_TARGET);
  3747. }
  3748. /*---------------------------------------------
  3749. **
  3750. ** Complete the 1st TEST UNIT READY command
  3751. ** with error condition if the device is
  3752. ** flagged NOSCAN, in order to speed up
  3753. ** the boot.
  3754. **
  3755. **---------------------------------------------
  3756. */
  3757. if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) &&
  3758. (tp->usrflag & UF_NOSCAN)) {
  3759. tp->usrflag &= ~UF_NOSCAN;
  3760. return DID_BAD_TARGET;
  3761. }
  3762. if (DEBUG_FLAGS & DEBUG_TINY) {
  3763. PRINT_ADDR(cmd, "CMD=%x ", cmd->cmnd[0]);
  3764. }
  3765. /*---------------------------------------------------
  3766. **
  3767. ** Assign a ccb / bind cmd.
  3768. ** If resetting, shorten settle_time if necessary
  3769. ** in order to avoid spurious timeouts.
  3770. ** If resetting or no free ccb,
  3771. ** insert cmd into the waiting list.
  3772. **
  3773. **----------------------------------------------------
  3774. */
  3775. if (np->settle_time && scsi_cmd_to_rq(cmd)->timeout >= HZ) {
  3776. u_long tlimit = jiffies + scsi_cmd_to_rq(cmd)->timeout - HZ;
  3777. if (time_after(np->settle_time, tlimit))
  3778. np->settle_time = tlimit;
  3779. }
  3780. if (np->settle_time || !(cp=ncr_get_ccb (np, cmd))) {
  3781. insert_into_waiting_list(np, cmd);
  3782. return(DID_OK);
  3783. }
  3784. cp->cmd = cmd;
  3785. /*----------------------------------------------------
  3786. **
  3787. ** Build the identify / tag / sdtr message
  3788. **
  3789. **----------------------------------------------------
  3790. */
  3791. idmsg = IDENTIFY(0, sdev->lun);
  3792. if (cp ->tag != NO_TAG ||
  3793. (cp != np->ccb && np->disc && !(tp->usrflag & UF_NODISC)))
  3794. idmsg |= 0x40;
  3795. msgptr = cp->scsi_smsg;
  3796. msglen = 0;
  3797. msgptr[msglen++] = idmsg;
  3798. if (cp->tag != NO_TAG) {
  3799. char order = np->order;
  3800. /*
  3801. ** Force ordered tag if necessary to avoid timeouts
  3802. ** and to preserve interactivity.
  3803. */
  3804. if (lp && time_after(jiffies, lp->tags_stime)) {
  3805. if (lp->tags_smap) {
  3806. order = ORDERED_QUEUE_TAG;
  3807. if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>2){
  3808. PRINT_ADDR(cmd,
  3809. "ordered tag forced.\n");
  3810. }
  3811. }
  3812. lp->tags_stime = jiffies + 3*HZ;
  3813. lp->tags_smap = lp->tags_umap;
  3814. }
  3815. if (order == 0) {
  3816. /*
  3817. ** Ordered write ops, unordered read ops.
  3818. */
  3819. switch (cmd->cmnd[0]) {
  3820. case 0x08: /* READ_SMALL (6) */
  3821. case 0x28: /* READ_BIG (10) */
  3822. case 0xa8: /* READ_HUGE (12) */
  3823. order = SIMPLE_QUEUE_TAG;
  3824. break;
  3825. default:
  3826. order = ORDERED_QUEUE_TAG;
  3827. }
  3828. }
  3829. msgptr[msglen++] = order;
  3830. /*
  3831. ** Actual tags are numbered 1,3,5,..2*MAXTAGS+1,
  3832. ** since we may have to deal with devices that have
  3833. ** problems with #TAG 0 or too great #TAG numbers.
  3834. */
  3835. msgptr[msglen++] = (cp->tag << 1) + 1;
  3836. }
  3837. /*----------------------------------------------------
  3838. **
  3839. ** Build the data descriptors
  3840. **
  3841. **----------------------------------------------------
  3842. */
  3843. direction = cmd->sc_data_direction;
  3844. if (direction != DMA_NONE) {
  3845. segments = ncr_scatter(np, cp, cp->cmd);
  3846. if (segments < 0) {
  3847. ncr_free_ccb(np, cp);
  3848. return(DID_ERROR);
  3849. }
  3850. }
  3851. else {
  3852. cp->data_len = 0;
  3853. segments = 0;
  3854. }
  3855. /*---------------------------------------------------
  3856. **
  3857. ** negotiation required?
  3858. **
  3859. ** (nego_status is filled by ncr_prepare_nego())
  3860. **
  3861. **---------------------------------------------------
  3862. */
  3863. cp->nego_status = 0;
  3864. if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
  3865. msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
  3866. }
  3867. /*----------------------------------------------------
  3868. **
  3869. ** Determine xfer direction.
  3870. **
  3871. **----------------------------------------------------
  3872. */
  3873. if (!cp->data_len)
  3874. direction = DMA_NONE;
  3875. /*
  3876. ** If data direction is BIDIRECTIONAL, speculate FROM_DEVICE
  3877. ** but prepare alternate pointers for TO_DEVICE in case
  3878. ** of our speculation will be just wrong.
  3879. ** SCRIPTS will swap values if needed.
  3880. */
  3881. switch(direction) {
  3882. case DMA_BIDIRECTIONAL:
  3883. case DMA_TO_DEVICE:
  3884. goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
  3885. if (segments <= MAX_SCATTERL)
  3886. lastp = goalp - 8 - (segments * 16);
  3887. else {
  3888. lastp = NCB_SCRIPTH_PHYS (np, hdata_out2);
  3889. lastp -= (segments - MAX_SCATTERL) * 16;
  3890. }
  3891. if (direction != DMA_BIDIRECTIONAL)
  3892. break;
  3893. cp->phys.header.wgoalp = cpu_to_scr(goalp);
  3894. cp->phys.header.wlastp = cpu_to_scr(lastp);
  3895. fallthrough;
  3896. case DMA_FROM_DEVICE:
  3897. goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
  3898. if (segments <= MAX_SCATTERL)
  3899. lastp = goalp - 8 - (segments * 16);
  3900. else {
  3901. lastp = NCB_SCRIPTH_PHYS (np, hdata_in2);
  3902. lastp -= (segments - MAX_SCATTERL) * 16;
  3903. }
  3904. break;
  3905. default:
  3906. case DMA_NONE:
  3907. lastp = goalp = NCB_SCRIPT_PHYS (np, no_data);
  3908. break;
  3909. }
  3910. /*
  3911. ** Set all pointers values needed by SCRIPTS.
  3912. ** If direction is unknown, start at data_io.
  3913. */
  3914. cp->phys.header.lastp = cpu_to_scr(lastp);
  3915. cp->phys.header.goalp = cpu_to_scr(goalp);
  3916. if (direction == DMA_BIDIRECTIONAL)
  3917. cp->phys.header.savep =
  3918. cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
  3919. else
  3920. cp->phys.header.savep= cpu_to_scr(lastp);
  3921. /*
  3922. ** Save the initial data pointer in order to be able
  3923. ** to redo the command.
  3924. */
  3925. cp->startp = cp->phys.header.savep;
  3926. /*----------------------------------------------------
  3927. **
  3928. ** fill in ccb
  3929. **
  3930. **----------------------------------------------------
  3931. **
  3932. **
  3933. ** physical -> virtual backlink
  3934. ** Generic SCSI command
  3935. */
  3936. /*
  3937. ** Startqueue
  3938. */
  3939. cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  3940. cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_dsa));
  3941. /*
  3942. ** select
  3943. */
  3944. cp->phys.select.sel_id = sdev_id(sdev);
  3945. cp->phys.select.sel_scntl3 = tp->wval;
  3946. cp->phys.select.sel_sxfer = tp->sval;
  3947. /*
  3948. ** message
  3949. */
  3950. cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
  3951. cp->phys.smsg.size = cpu_to_scr(msglen);
  3952. /*
  3953. ** command
  3954. */
  3955. memcpy(cp->cdb_buf, cmd->cmnd, min_t(int, cmd->cmd_len, sizeof(cp->cdb_buf)));
  3956. cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
  3957. cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
  3958. /*
  3959. ** status
  3960. */
  3961. cp->actualquirks = 0;
  3962. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  3963. cp->scsi_status = SAM_STAT_ILLEGAL;
  3964. cp->parity_status = 0;
  3965. cp->xerr_status = XE_OK;
  3966. /*----------------------------------------------------
  3967. **
  3968. ** Critical region: start this job.
  3969. **
  3970. **----------------------------------------------------
  3971. */
  3972. /* activate this job. */
  3973. cp->magic = CCB_MAGIC;
  3974. /*
  3975. ** insert next CCBs into start queue.
  3976. ** 2 max at a time is enough to flush the CCB wait queue.
  3977. */
  3978. cp->auto_sense = 0;
  3979. if (lp)
  3980. ncr_start_next_ccb(np, lp, 2);
  3981. else
  3982. ncr_put_start_queue(np, cp);
  3983. /* Command is successfully queued. */
  3984. return DID_OK;
  3985. }
  3986. /*==========================================================
  3987. **
  3988. **
  3989. ** Insert a CCB into the start queue and wake up the
  3990. ** SCRIPTS processor.
  3991. **
  3992. **
  3993. **==========================================================
  3994. */
  3995. static void ncr_start_next_ccb(struct ncb *np, struct lcb *lp, int maxn)
  3996. {
  3997. struct list_head *qp;
  3998. struct ccb *cp;
  3999. if (lp->held_ccb)
  4000. return;
  4001. while (maxn-- && lp->queuedccbs < lp->queuedepth) {
  4002. qp = ncr_list_pop(&lp->wait_ccbq);
  4003. if (!qp)
  4004. break;
  4005. ++lp->queuedccbs;
  4006. cp = list_entry(qp, struct ccb, link_ccbq);
  4007. list_add_tail(qp, &lp->busy_ccbq);
  4008. lp->jump_ccb[cp->tag == NO_TAG ? 0 : cp->tag] =
  4009. cpu_to_scr(CCB_PHYS (cp, restart));
  4010. ncr_put_start_queue(np, cp);
  4011. }
  4012. }
  4013. static void ncr_put_start_queue(struct ncb *np, struct ccb *cp)
  4014. {
  4015. u16 qidx;
  4016. /*
  4017. ** insert into start queue.
  4018. */
  4019. if (!np->squeueput) np->squeueput = 1;
  4020. qidx = np->squeueput + 2;
  4021. if (qidx >= MAX_START + MAX_START) qidx = 1;
  4022. np->scripth->tryloop [qidx] = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  4023. MEMORY_BARRIER();
  4024. np->scripth->tryloop [np->squeueput] = cpu_to_scr(CCB_PHYS (cp, start));
  4025. np->squeueput = qidx;
  4026. ++np->queuedccbs;
  4027. cp->queued = 1;
  4028. if (DEBUG_FLAGS & DEBUG_QUEUE)
  4029. printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
  4030. /*
  4031. ** Script processor may be waiting for reselect.
  4032. ** Wake it up.
  4033. */
  4034. MEMORY_BARRIER();
  4035. OUTB (nc_istat, SIGP);
  4036. }
  4037. static int ncr_reset_scsi_bus(struct ncb *np, int enab_int, int settle_delay)
  4038. {
  4039. u32 term;
  4040. int retv = 0;
  4041. np->settle_time = jiffies + settle_delay * HZ;
  4042. if (bootverbose > 1)
  4043. printk("%s: resetting, "
  4044. "command processing suspended for %d seconds\n",
  4045. ncr_name(np), settle_delay);
  4046. ncr_chip_reset(np, 100);
  4047. udelay(2000); /* The 895 needs time for the bus mode to settle */
  4048. if (enab_int)
  4049. OUTW (nc_sien, RST);
  4050. /*
  4051. ** Enable Tolerant, reset IRQD if present and
  4052. ** properly set IRQ mode, prior to resetting the bus.
  4053. */
  4054. OUTB (nc_stest3, TE);
  4055. OUTB (nc_scntl1, CRST);
  4056. udelay(200);
  4057. if (!driver_setup.bus_check)
  4058. goto out;
  4059. /*
  4060. ** Check for no terminators or SCSI bus shorts to ground.
  4061. ** Read SCSI data bus, data parity bits and control signals.
  4062. ** We are expecting RESET to be TRUE and other signals to be
  4063. ** FALSE.
  4064. */
  4065. term = INB(nc_sstat0);
  4066. term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
  4067. term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
  4068. ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
  4069. ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
  4070. INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
  4071. if (!(np->features & FE_WIDE))
  4072. term &= 0x3ffff;
  4073. if (term != (2<<7)) {
  4074. printk("%s: suspicious SCSI data while resetting the BUS.\n",
  4075. ncr_name(np));
  4076. printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
  4077. "0x%lx, expecting 0x%lx\n",
  4078. ncr_name(np),
  4079. (np->features & FE_WIDE) ? "dp1,d15-8," : "",
  4080. (u_long)term, (u_long)(2<<7));
  4081. if (driver_setup.bus_check == 1)
  4082. retv = 1;
  4083. }
  4084. out:
  4085. OUTB (nc_scntl1, 0);
  4086. return retv;
  4087. }
  4088. /*
  4089. * Start reset process.
  4090. * If reset in progress do nothing.
  4091. * The interrupt handler will reinitialize the chip.
  4092. * The timeout handler will wait for settle_time before
  4093. * clearing it and so resuming command processing.
  4094. */
  4095. static void ncr_start_reset(struct ncb *np)
  4096. {
  4097. if (!np->settle_time) {
  4098. ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
  4099. }
  4100. }
  4101. /*==========================================================
  4102. **
  4103. **
  4104. ** Reset the SCSI BUS.
  4105. ** This is called from the generic SCSI driver.
  4106. **
  4107. **
  4108. **==========================================================
  4109. */
  4110. static int ncr_reset_bus (struct ncb *np)
  4111. {
  4112. /*
  4113. * Return immediately if reset is in progress.
  4114. */
  4115. if (np->settle_time) {
  4116. return FAILED;
  4117. }
  4118. /*
  4119. * Start the reset process.
  4120. * The script processor is then assumed to be stopped.
  4121. * Commands will now be queued in the waiting list until a settle
  4122. * delay of 2 seconds will be completed.
  4123. */
  4124. ncr_start_reset(np);
  4125. /*
  4126. * Wake-up all awaiting commands with DID_RESET.
  4127. */
  4128. reset_waiting_list(np);
  4129. /*
  4130. * Wake-up all pending commands with HS_RESET -> DID_RESET.
  4131. */
  4132. ncr_wakeup(np, HS_RESET);
  4133. return SUCCESS;
  4134. }
  4135. static void ncr_detach(struct ncb *np)
  4136. {
  4137. struct ccb *cp;
  4138. struct tcb *tp;
  4139. struct lcb *lp;
  4140. int target, lun;
  4141. int i;
  4142. char inst_name[16];
  4143. /* Local copy so we don't access np after freeing it! */
  4144. strlcpy(inst_name, ncr_name(np), sizeof(inst_name));
  4145. printk("%s: releasing host resources\n", ncr_name(np));
  4146. /*
  4147. ** Stop the ncr_timeout process
  4148. ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
  4149. */
  4150. #ifdef DEBUG_NCR53C8XX
  4151. printk("%s: stopping the timer\n", ncr_name(np));
  4152. #endif
  4153. np->release_stage = 1;
  4154. for (i = 50 ; i && np->release_stage != 2 ; i--)
  4155. mdelay(100);
  4156. if (np->release_stage != 2)
  4157. printk("%s: the timer seems to be already stopped\n", ncr_name(np));
  4158. else np->release_stage = 2;
  4159. /*
  4160. ** Disable chip interrupts
  4161. */
  4162. #ifdef DEBUG_NCR53C8XX
  4163. printk("%s: disabling chip interrupts\n", ncr_name(np));
  4164. #endif
  4165. OUTW (nc_sien , 0);
  4166. OUTB (nc_dien , 0);
  4167. /*
  4168. ** Reset NCR chip
  4169. ** Restore bios setting for automatic clock detection.
  4170. */
  4171. printk("%s: resetting chip\n", ncr_name(np));
  4172. ncr_chip_reset(np, 100);
  4173. OUTB(nc_dmode, np->sv_dmode);
  4174. OUTB(nc_dcntl, np->sv_dcntl);
  4175. OUTB(nc_ctest0, np->sv_ctest0);
  4176. OUTB(nc_ctest3, np->sv_ctest3);
  4177. OUTB(nc_ctest4, np->sv_ctest4);
  4178. OUTB(nc_ctest5, np->sv_ctest5);
  4179. OUTB(nc_gpcntl, np->sv_gpcntl);
  4180. OUTB(nc_stest2, np->sv_stest2);
  4181. ncr_selectclock(np, np->sv_scntl3);
  4182. /*
  4183. ** Free allocated ccb(s)
  4184. */
  4185. while ((cp=np->ccb->link_ccb) != NULL) {
  4186. np->ccb->link_ccb = cp->link_ccb;
  4187. if (cp->host_status) {
  4188. printk("%s: shall free an active ccb (host_status=%d)\n",
  4189. ncr_name(np), cp->host_status);
  4190. }
  4191. #ifdef DEBUG_NCR53C8XX
  4192. printk("%s: freeing ccb (%lx)\n", ncr_name(np), (u_long) cp);
  4193. #endif
  4194. m_free_dma(cp, sizeof(*cp), "CCB");
  4195. }
  4196. /* Free allocated tp(s) */
  4197. for (target = 0; target < MAX_TARGET ; target++) {
  4198. tp=&np->target[target];
  4199. for (lun = 0 ; lun < MAX_LUN ; lun++) {
  4200. lp = tp->lp[lun];
  4201. if (lp) {
  4202. #ifdef DEBUG_NCR53C8XX
  4203. printk("%s: freeing lp (%lx)\n", ncr_name(np), (u_long) lp);
  4204. #endif
  4205. if (lp->jump_ccb != &lp->jump_ccb_0)
  4206. m_free_dma(lp->jump_ccb,256,"JUMP_CCB");
  4207. m_free_dma(lp, sizeof(*lp), "LCB");
  4208. }
  4209. }
  4210. }
  4211. if (np->scripth0)
  4212. m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
  4213. if (np->script0)
  4214. m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
  4215. if (np->ccb)
  4216. m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
  4217. m_free_dma(np, sizeof(struct ncb), "NCB");
  4218. printk("%s: host resources successfully released\n", inst_name);
  4219. }
  4220. /*==========================================================
  4221. **
  4222. **
  4223. ** Complete execution of a SCSI command.
  4224. ** Signal completion to the generic SCSI driver.
  4225. **
  4226. **
  4227. **==========================================================
  4228. */
  4229. void ncr_complete (struct ncb *np, struct ccb *cp)
  4230. {
  4231. struct scsi_cmnd *cmd;
  4232. struct tcb *tp;
  4233. struct lcb *lp;
  4234. /*
  4235. ** Sanity check
  4236. */
  4237. if (!cp || cp->magic != CCB_MAGIC || !cp->cmd)
  4238. return;
  4239. /*
  4240. ** Print minimal debug information.
  4241. */
  4242. if (DEBUG_FLAGS & DEBUG_TINY)
  4243. printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
  4244. cp->host_status,cp->scsi_status);
  4245. /*
  4246. ** Get command, target and lun pointers.
  4247. */
  4248. cmd = cp->cmd;
  4249. cp->cmd = NULL;
  4250. tp = &np->target[cmd->device->id];
  4251. lp = tp->lp[cmd->device->lun];
  4252. /*
  4253. ** We donnot queue more than 1 ccb per target
  4254. ** with negotiation at any time. If this ccb was
  4255. ** used for negotiation, clear this info in the tcb.
  4256. */
  4257. if (cp == tp->nego_cp)
  4258. tp->nego_cp = NULL;
  4259. /*
  4260. ** If auto-sense performed, change scsi status.
  4261. */
  4262. if (cp->auto_sense) {
  4263. cp->scsi_status = cp->auto_sense;
  4264. }
  4265. /*
  4266. ** If we were recovering from queue full or performing
  4267. ** auto-sense, requeue skipped CCBs to the wait queue.
  4268. */
  4269. if (lp && lp->held_ccb) {
  4270. if (cp == lp->held_ccb) {
  4271. list_splice_init(&lp->skip_ccbq, &lp->wait_ccbq);
  4272. lp->held_ccb = NULL;
  4273. }
  4274. }
  4275. /*
  4276. ** Check for parity errors.
  4277. */
  4278. if (cp->parity_status > 1) {
  4279. PRINT_ADDR(cmd, "%d parity error(s).\n",cp->parity_status);
  4280. }
  4281. /*
  4282. ** Check for extended errors.
  4283. */
  4284. if (cp->xerr_status != XE_OK) {
  4285. switch (cp->xerr_status) {
  4286. case XE_EXTRA_DATA:
  4287. PRINT_ADDR(cmd, "extraneous data discarded.\n");
  4288. break;
  4289. case XE_BAD_PHASE:
  4290. PRINT_ADDR(cmd, "invalid scsi phase (4/5).\n");
  4291. break;
  4292. default:
  4293. PRINT_ADDR(cmd, "extended error %d.\n",
  4294. cp->xerr_status);
  4295. break;
  4296. }
  4297. if (cp->host_status==HS_COMPLETE)
  4298. cp->host_status = HS_FAIL;
  4299. }
  4300. /*
  4301. ** Print out any error for debugging purpose.
  4302. */
  4303. if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
  4304. if (cp->host_status != HS_COMPLETE ||
  4305. cp->scsi_status != SAM_STAT_GOOD) {
  4306. PRINT_ADDR(cmd, "ERROR: cmd=%x host_status=%x "
  4307. "scsi_status=%x\n", cmd->cmnd[0],
  4308. cp->host_status, cp->scsi_status);
  4309. }
  4310. }
  4311. /*
  4312. ** Check the status.
  4313. */
  4314. cmd->result = 0;
  4315. if ( (cp->host_status == HS_COMPLETE)
  4316. && (cp->scsi_status == SAM_STAT_GOOD ||
  4317. cp->scsi_status == SAM_STAT_CONDITION_MET)) {
  4318. /*
  4319. * All went well (GOOD status).
  4320. * CONDITION MET status is returned on
  4321. * `Pre-Fetch' or `Search data' success.
  4322. */
  4323. set_status_byte(cmd, cp->scsi_status);
  4324. /*
  4325. ** @RESID@
  4326. ** Could dig out the correct value for resid,
  4327. ** but it would be quite complicated.
  4328. */
  4329. /* if (cp->phys.header.lastp != cp->phys.header.goalp) */
  4330. /*
  4331. ** Allocate the lcb if not yet.
  4332. */
  4333. if (!lp)
  4334. ncr_alloc_lcb (np, cmd->device->id, cmd->device->lun);
  4335. tp->bytes += cp->data_len;
  4336. tp->transfers ++;
  4337. /*
  4338. ** If tags was reduced due to queue full,
  4339. ** increase tags if 1000 good status received.
  4340. */
  4341. if (lp && lp->usetags && lp->numtags < lp->maxtags) {
  4342. ++lp->num_good;
  4343. if (lp->num_good >= 1000) {
  4344. lp->num_good = 0;
  4345. ++lp->numtags;
  4346. ncr_setup_tags (np, cmd->device);
  4347. }
  4348. }
  4349. } else if ((cp->host_status == HS_COMPLETE)
  4350. && (cp->scsi_status == SAM_STAT_CHECK_CONDITION)) {
  4351. /*
  4352. ** Check condition code
  4353. */
  4354. set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
  4355. /*
  4356. ** Copy back sense data to caller's buffer.
  4357. */
  4358. memcpy(cmd->sense_buffer, cp->sense_buf,
  4359. min_t(size_t, SCSI_SENSE_BUFFERSIZE,
  4360. sizeof(cp->sense_buf)));
  4361. if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
  4362. u_char *p = cmd->sense_buffer;
  4363. int i;
  4364. PRINT_ADDR(cmd, "sense data:");
  4365. for (i=0; i<14; i++) printk (" %x", *p++);
  4366. printk (".\n");
  4367. }
  4368. } else if ((cp->host_status == HS_COMPLETE)
  4369. && (cp->scsi_status == SAM_STAT_RESERVATION_CONFLICT)) {
  4370. /*
  4371. ** Reservation Conflict condition code
  4372. */
  4373. set_status_byte(cmd, SAM_STAT_RESERVATION_CONFLICT);
  4374. } else if ((cp->host_status == HS_COMPLETE)
  4375. && (cp->scsi_status == SAM_STAT_BUSY ||
  4376. cp->scsi_status == SAM_STAT_TASK_SET_FULL)) {
  4377. /*
  4378. ** Target is busy.
  4379. */
  4380. set_status_byte(cmd, cp->scsi_status);
  4381. } else if ((cp->host_status == HS_SEL_TIMEOUT)
  4382. || (cp->host_status == HS_TIMEOUT)) {
  4383. /*
  4384. ** No response
  4385. */
  4386. set_status_byte(cmd, cp->scsi_status);
  4387. set_host_byte(cmd, DID_TIME_OUT);
  4388. } else if (cp->host_status == HS_RESET) {
  4389. /*
  4390. ** SCSI bus reset
  4391. */
  4392. set_status_byte(cmd, cp->scsi_status);
  4393. set_host_byte(cmd, DID_RESET);
  4394. } else if (cp->host_status == HS_ABORTED) {
  4395. /*
  4396. ** Transfer aborted
  4397. */
  4398. set_status_byte(cmd, cp->scsi_status);
  4399. set_host_byte(cmd, DID_ABORT);
  4400. } else {
  4401. /*
  4402. ** Other protocol messes
  4403. */
  4404. PRINT_ADDR(cmd, "COMMAND FAILED (%x %x) @%p.\n",
  4405. cp->host_status, cp->scsi_status, cp);
  4406. set_status_byte(cmd, cp->scsi_status);
  4407. set_host_byte(cmd, DID_ERROR);
  4408. }
  4409. /*
  4410. ** trace output
  4411. */
  4412. if (tp->usrflag & UF_TRACE) {
  4413. u_char * p;
  4414. int i;
  4415. PRINT_ADDR(cmd, " CMD:");
  4416. p = (u_char*) &cmd->cmnd[0];
  4417. for (i=0; i<cmd->cmd_len; i++) printk (" %x", *p++);
  4418. if (cp->host_status==HS_COMPLETE) {
  4419. switch (cp->scsi_status) {
  4420. case SAM_STAT_GOOD:
  4421. printk (" GOOD");
  4422. break;
  4423. case SAM_STAT_CHECK_CONDITION:
  4424. printk (" SENSE:");
  4425. p = (u_char*) &cmd->sense_buffer;
  4426. for (i=0; i<14; i++)
  4427. printk (" %x", *p++);
  4428. break;
  4429. default:
  4430. printk (" STAT: %x\n", cp->scsi_status);
  4431. break;
  4432. }
  4433. } else printk (" HOSTERROR: %x", cp->host_status);
  4434. printk ("\n");
  4435. }
  4436. /*
  4437. ** Free this ccb
  4438. */
  4439. ncr_free_ccb (np, cp);
  4440. /*
  4441. ** requeue awaiting scsi commands for this lun.
  4442. */
  4443. if (lp && lp->queuedccbs < lp->queuedepth &&
  4444. !list_empty(&lp->wait_ccbq))
  4445. ncr_start_next_ccb(np, lp, 2);
  4446. /*
  4447. ** requeue awaiting scsi commands for this controller.
  4448. */
  4449. if (np->waiting_list)
  4450. requeue_waiting_list(np);
  4451. /*
  4452. ** signal completion to generic driver.
  4453. */
  4454. ncr_queue_done_cmd(np, cmd);
  4455. }
  4456. /*==========================================================
  4457. **
  4458. **
  4459. ** Signal all (or one) control block done.
  4460. **
  4461. **
  4462. **==========================================================
  4463. */
  4464. /*
  4465. ** This CCB has been skipped by the NCR.
  4466. ** Queue it in the corresponding unit queue.
  4467. */
  4468. static void ncr_ccb_skipped(struct ncb *np, struct ccb *cp)
  4469. {
  4470. struct tcb *tp = &np->target[cp->target];
  4471. struct lcb *lp = tp->lp[cp->lun];
  4472. if (lp && cp != np->ccb) {
  4473. cp->host_status &= ~HS_SKIPMASK;
  4474. cp->start.schedule.l_paddr =
  4475. cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  4476. list_move_tail(&cp->link_ccbq, &lp->skip_ccbq);
  4477. if (cp->queued) {
  4478. --lp->queuedccbs;
  4479. }
  4480. }
  4481. if (cp->queued) {
  4482. --np->queuedccbs;
  4483. cp->queued = 0;
  4484. }
  4485. }
  4486. /*
  4487. ** The NCR has completed CCBs.
  4488. ** Look at the DONE QUEUE if enabled, otherwise scan all CCBs
  4489. */
  4490. void ncr_wakeup_done (struct ncb *np)
  4491. {
  4492. struct ccb *cp;
  4493. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  4494. int i, j;
  4495. i = np->ccb_done_ic;
  4496. while (1) {
  4497. j = i+1;
  4498. if (j >= MAX_DONE)
  4499. j = 0;
  4500. cp = np->ccb_done[j];
  4501. if (!CCB_DONE_VALID(cp))
  4502. break;
  4503. np->ccb_done[j] = (struct ccb *)CCB_DONE_EMPTY;
  4504. np->scripth->done_queue[5*j + 4] =
  4505. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
  4506. MEMORY_BARRIER();
  4507. np->scripth->done_queue[5*i + 4] =
  4508. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
  4509. if (cp->host_status & HS_DONEMASK)
  4510. ncr_complete (np, cp);
  4511. else if (cp->host_status & HS_SKIPMASK)
  4512. ncr_ccb_skipped (np, cp);
  4513. i = j;
  4514. }
  4515. np->ccb_done_ic = i;
  4516. #else
  4517. cp = np->ccb;
  4518. while (cp) {
  4519. if (cp->host_status & HS_DONEMASK)
  4520. ncr_complete (np, cp);
  4521. else if (cp->host_status & HS_SKIPMASK)
  4522. ncr_ccb_skipped (np, cp);
  4523. cp = cp->link_ccb;
  4524. }
  4525. #endif
  4526. }
  4527. /*
  4528. ** Complete all active CCBs.
  4529. */
  4530. void ncr_wakeup (struct ncb *np, u_long code)
  4531. {
  4532. struct ccb *cp = np->ccb;
  4533. while (cp) {
  4534. if (cp->host_status != HS_IDLE) {
  4535. cp->host_status = code;
  4536. ncr_complete (np, cp);
  4537. }
  4538. cp = cp->link_ccb;
  4539. }
  4540. }
  4541. /*
  4542. ** Reset ncr chip.
  4543. */
  4544. /* Some initialisation must be done immediately following reset, for 53c720,
  4545. * at least. EA (dcntl bit 5) isn't set here as it is set once only in
  4546. * the _detect function.
  4547. */
  4548. static void ncr_chip_reset(struct ncb *np, int delay)
  4549. {
  4550. OUTB (nc_istat, SRST);
  4551. udelay(delay);
  4552. OUTB (nc_istat, 0 );
  4553. if (np->features & FE_EHP)
  4554. OUTB (nc_ctest0, EHP);
  4555. if (np->features & FE_MUX)
  4556. OUTB (nc_ctest4, MUX);
  4557. }
  4558. /*==========================================================
  4559. **
  4560. **
  4561. ** Start NCR chip.
  4562. **
  4563. **
  4564. **==========================================================
  4565. */
  4566. void ncr_init (struct ncb *np, int reset, char * msg, u_long code)
  4567. {
  4568. int i;
  4569. /*
  4570. ** Reset chip if asked, otherwise just clear fifos.
  4571. */
  4572. if (reset) {
  4573. OUTB (nc_istat, SRST);
  4574. udelay(100);
  4575. }
  4576. else {
  4577. OUTB (nc_stest3, TE|CSF);
  4578. OUTONB (nc_ctest3, CLF);
  4579. }
  4580. /*
  4581. ** Message.
  4582. */
  4583. if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
  4584. /*
  4585. ** Clear Start Queue
  4586. */
  4587. np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
  4588. for (i = 1; i < MAX_START + MAX_START; i += 2)
  4589. np->scripth0->tryloop[i] =
  4590. cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  4591. /*
  4592. ** Start at first entry.
  4593. */
  4594. np->squeueput = 0;
  4595. np->script0->startpos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np, tryloop));
  4596. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  4597. /*
  4598. ** Clear Done Queue
  4599. */
  4600. for (i = 0; i < MAX_DONE; i++) {
  4601. np->ccb_done[i] = (struct ccb *)CCB_DONE_EMPTY;
  4602. np->scripth0->done_queue[5*i + 4] =
  4603. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_end));
  4604. }
  4605. #endif
  4606. /*
  4607. ** Start at first entry.
  4608. */
  4609. np->script0->done_pos[0] = cpu_to_scr(NCB_SCRIPTH_PHYS (np,done_queue));
  4610. np->ccb_done_ic = MAX_DONE-1;
  4611. np->scripth0->done_queue[5*(MAX_DONE-1) + 4] =
  4612. cpu_to_scr(NCB_SCRIPT_PHYS (np, done_plug));
  4613. /*
  4614. ** Wakeup all pending jobs.
  4615. */
  4616. ncr_wakeup (np, code);
  4617. /*
  4618. ** Init chip.
  4619. */
  4620. /*
  4621. ** Remove reset; big delay because the 895 needs time for the
  4622. ** bus mode to settle
  4623. */
  4624. ncr_chip_reset(np, 2000);
  4625. OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
  4626. /* full arb., ena parity, par->ATN */
  4627. OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
  4628. ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
  4629. OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
  4630. OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
  4631. OUTB (nc_istat , SIGP ); /* Signal Process */
  4632. OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
  4633. OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
  4634. OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
  4635. OUTB (nc_ctest0, np->rv_ctest0); /* 720: CDIS and EHP */
  4636. OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
  4637. OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
  4638. OUTB (nc_stest2, EXT|np->rv_stest2); /* Extended Sreq/Sack filtering */
  4639. OUTB (nc_stest3, TE); /* TolerANT enable */
  4640. OUTB (nc_stime0, 0x0c ); /* HTH disabled STO 0.25 sec */
  4641. /*
  4642. ** Disable disconnects.
  4643. */
  4644. np->disc = 0;
  4645. /*
  4646. ** Enable GPIO0 pin for writing if LED support.
  4647. */
  4648. if (np->features & FE_LED0) {
  4649. OUTOFFB (nc_gpcntl, 0x01);
  4650. }
  4651. /*
  4652. ** enable ints
  4653. */
  4654. OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
  4655. OUTB (nc_dien , MDPE|BF|ABRT|SSI|SIR|IID);
  4656. /*
  4657. ** Fill in target structure.
  4658. ** Reinitialize usrsync.
  4659. ** Reinitialize usrwide.
  4660. ** Prepare sync negotiation according to actual SCSI bus mode.
  4661. */
  4662. for (i=0;i<MAX_TARGET;i++) {
  4663. struct tcb *tp = &np->target[i];
  4664. tp->sval = 0;
  4665. tp->wval = np->rv_scntl3;
  4666. if (tp->usrsync != 255) {
  4667. if (tp->usrsync <= np->maxsync) {
  4668. if (tp->usrsync < np->minsync) {
  4669. tp->usrsync = np->minsync;
  4670. }
  4671. }
  4672. else
  4673. tp->usrsync = 255;
  4674. }
  4675. if (tp->usrwide > np->maxwide)
  4676. tp->usrwide = np->maxwide;
  4677. }
  4678. /*
  4679. ** Start script processor.
  4680. */
  4681. if (np->paddr2) {
  4682. if (bootverbose)
  4683. printk ("%s: Downloading SCSI SCRIPTS.\n",
  4684. ncr_name(np));
  4685. OUTL (nc_scratcha, vtobus(np->script0));
  4686. OUTL_DSP (NCB_SCRIPTH_PHYS (np, start_ram));
  4687. }
  4688. else
  4689. OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
  4690. }
  4691. /*==========================================================
  4692. **
  4693. ** Prepare the negotiation values for wide and
  4694. ** synchronous transfers.
  4695. **
  4696. **==========================================================
  4697. */
  4698. static void ncr_negotiate (struct ncb* np, struct tcb* tp)
  4699. {
  4700. /*
  4701. ** minsync unit is 4ns !
  4702. */
  4703. u_long minsync = tp->usrsync;
  4704. /*
  4705. ** SCSI bus mode limit
  4706. */
  4707. if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
  4708. if (minsync < 12) minsync = 12;
  4709. }
  4710. /*
  4711. ** our limit ..
  4712. */
  4713. if (minsync < np->minsync)
  4714. minsync = np->minsync;
  4715. /*
  4716. ** divider limit
  4717. */
  4718. if (minsync > np->maxsync)
  4719. minsync = 255;
  4720. if (tp->maxoffs > np->maxoffs)
  4721. tp->maxoffs = np->maxoffs;
  4722. tp->minsync = minsync;
  4723. tp->maxoffs = (minsync<255 ? tp->maxoffs : 0);
  4724. /*
  4725. ** period=0: has to negotiate sync transfer
  4726. */
  4727. tp->period=0;
  4728. /*
  4729. ** widedone=0: has to negotiate wide transfer
  4730. */
  4731. tp->widedone=0;
  4732. }
  4733. /*==========================================================
  4734. **
  4735. ** Get clock factor and sync divisor for a given
  4736. ** synchronous factor period.
  4737. ** Returns the clock factor (in sxfer) and scntl3
  4738. ** synchronous divisor field.
  4739. **
  4740. **==========================================================
  4741. */
  4742. static void ncr_getsync(struct ncb *np, u_char sfac, u_char *fakp, u_char *scntl3p)
  4743. {
  4744. u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
  4745. int div = np->clock_divn; /* Number of divisors supported */
  4746. u_long fak; /* Sync factor in sxfer */
  4747. u_long per; /* Period in tenths of ns */
  4748. u_long kpc; /* (per * clk) */
  4749. /*
  4750. ** Compute the synchronous period in tenths of nano-seconds
  4751. */
  4752. if (sfac <= 10) per = 250;
  4753. else if (sfac == 11) per = 303;
  4754. else if (sfac == 12) per = 500;
  4755. else per = 40 * sfac;
  4756. /*
  4757. ** Look for the greatest clock divisor that allows an
  4758. ** input speed faster than the period.
  4759. */
  4760. kpc = per * clk;
  4761. while (--div > 0)
  4762. if (kpc >= (div_10M[div] << 2)) break;
  4763. /*
  4764. ** Calculate the lowest clock factor that allows an output
  4765. ** speed not faster than the period.
  4766. */
  4767. fak = (kpc - 1) / div_10M[div] + 1;
  4768. if (fak < 4) fak = 4; /* Should never happen, too bad ... */
  4769. /*
  4770. ** Compute and return sync parameters for the ncr
  4771. */
  4772. *fakp = fak - 4;
  4773. *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
  4774. }
  4775. /*==========================================================
  4776. **
  4777. ** Set actual values, sync status and patch all ccbs of
  4778. ** a target according to new sync/wide agreement.
  4779. **
  4780. **==========================================================
  4781. */
  4782. static void ncr_set_sync_wide_status (struct ncb *np, u_char target)
  4783. {
  4784. struct ccb *cp;
  4785. struct tcb *tp = &np->target[target];
  4786. /*
  4787. ** set actual value and sync_status
  4788. */
  4789. OUTB (nc_sxfer, tp->sval);
  4790. np->sync_st = tp->sval;
  4791. OUTB (nc_scntl3, tp->wval);
  4792. np->wide_st = tp->wval;
  4793. /*
  4794. ** patch ALL ccbs of this target.
  4795. */
  4796. for (cp = np->ccb; cp; cp = cp->link_ccb) {
  4797. if (!cp->cmd) continue;
  4798. if (scmd_id(cp->cmd) != target) continue;
  4799. cp->phys.select.sel_scntl3 = tp->wval;
  4800. cp->phys.select.sel_sxfer = tp->sval;
  4801. }
  4802. }
  4803. /*==========================================================
  4804. **
  4805. ** Switch sync mode for current job and it's target
  4806. **
  4807. **==========================================================
  4808. */
  4809. static void ncr_setsync (struct ncb *np, struct ccb *cp, u_char scntl3, u_char sxfer)
  4810. {
  4811. struct scsi_cmnd *cmd = cp->cmd;
  4812. struct tcb *tp;
  4813. u_char target = INB (nc_sdid) & 0x0f;
  4814. u_char idiv;
  4815. BUG_ON(target != (scmd_id(cmd) & 0xf));
  4816. tp = &np->target[target];
  4817. if (!scntl3 || !(sxfer & 0x1f))
  4818. scntl3 = np->rv_scntl3;
  4819. scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) | (np->rv_scntl3 & 0x07);
  4820. /*
  4821. ** Deduce the value of controller sync period from scntl3.
  4822. ** period is in tenths of nano-seconds.
  4823. */
  4824. idiv = ((scntl3 >> 4) & 0x7);
  4825. if ((sxfer & 0x1f) && idiv)
  4826. tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
  4827. else
  4828. tp->period = 0xffff;
  4829. /* Stop there if sync parameters are unchanged */
  4830. if (tp->sval == sxfer && tp->wval == scntl3)
  4831. return;
  4832. tp->sval = sxfer;
  4833. tp->wval = scntl3;
  4834. if (sxfer & 0x01f) {
  4835. /* Disable extended Sreq/Sack filtering */
  4836. if (tp->period <= 2000)
  4837. OUTOFFB(nc_stest2, EXT);
  4838. }
  4839. spi_display_xfer_agreement(tp->starget);
  4840. /*
  4841. ** set actual value and sync_status
  4842. ** patch ALL ccbs of this target.
  4843. */
  4844. ncr_set_sync_wide_status(np, target);
  4845. }
  4846. /*==========================================================
  4847. **
  4848. ** Switch wide mode for current job and it's target
  4849. ** SCSI specs say: a SCSI device that accepts a WDTR
  4850. ** message shall reset the synchronous agreement to
  4851. ** asynchronous mode.
  4852. **
  4853. **==========================================================
  4854. */
  4855. static void ncr_setwide (struct ncb *np, struct ccb *cp, u_char wide, u_char ack)
  4856. {
  4857. struct scsi_cmnd *cmd = cp->cmd;
  4858. u16 target = INB (nc_sdid) & 0x0f;
  4859. struct tcb *tp;
  4860. u_char scntl3;
  4861. u_char sxfer;
  4862. BUG_ON(target != (scmd_id(cmd) & 0xf));
  4863. tp = &np->target[target];
  4864. tp->widedone = wide+1;
  4865. scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
  4866. sxfer = ack ? 0 : tp->sval;
  4867. /*
  4868. ** Stop there if sync/wide parameters are unchanged
  4869. */
  4870. if (tp->sval == sxfer && tp->wval == scntl3) return;
  4871. tp->sval = sxfer;
  4872. tp->wval = scntl3;
  4873. /*
  4874. ** Bells and whistles ;-)
  4875. */
  4876. if (bootverbose >= 2) {
  4877. dev_info(&cmd->device->sdev_target->dev, "WIDE SCSI %sabled.\n",
  4878. (scntl3 & EWS) ? "en" : "dis");
  4879. }
  4880. /*
  4881. ** set actual value and sync_status
  4882. ** patch ALL ccbs of this target.
  4883. */
  4884. ncr_set_sync_wide_status(np, target);
  4885. }
  4886. /*==========================================================
  4887. **
  4888. ** Switch tagged mode for a target.
  4889. **
  4890. **==========================================================
  4891. */
  4892. static void ncr_setup_tags (struct ncb *np, struct scsi_device *sdev)
  4893. {
  4894. unsigned char tn = sdev->id, ln = sdev->lun;
  4895. struct tcb *tp = &np->target[tn];
  4896. struct lcb *lp = tp->lp[ln];
  4897. u_char reqtags, maxdepth;
  4898. /*
  4899. ** Just in case ...
  4900. */
  4901. if ((!tp) || (!lp) || !sdev)
  4902. return;
  4903. /*
  4904. ** If SCSI device queue depth is not yet set, leave here.
  4905. */
  4906. if (!lp->scdev_depth)
  4907. return;
  4908. /*
  4909. ** Donnot allow more tags than the SCSI driver can queue
  4910. ** for this device.
  4911. ** Donnot allow more tags than we can handle.
  4912. */
  4913. maxdepth = lp->scdev_depth;
  4914. if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
  4915. if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
  4916. if (lp->numtags > maxdepth) lp->numtags = maxdepth;
  4917. /*
  4918. ** only devices conformant to ANSI Version >= 2
  4919. ** only devices capable of tagged commands
  4920. ** only if enabled by user ..
  4921. */
  4922. if (sdev->tagged_supported && lp->numtags > 1) {
  4923. reqtags = lp->numtags;
  4924. } else {
  4925. reqtags = 1;
  4926. }
  4927. /*
  4928. ** Update max number of tags
  4929. */
  4930. lp->numtags = reqtags;
  4931. if (lp->numtags > lp->maxtags)
  4932. lp->maxtags = lp->numtags;
  4933. /*
  4934. ** If we want to switch tag mode, we must wait
  4935. ** for no CCB to be active.
  4936. */
  4937. if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
  4938. if (lp->queuedepth == reqtags) /* Already announced */
  4939. return;
  4940. lp->queuedepth = reqtags;
  4941. }
  4942. else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
  4943. lp->queuedepth = reqtags;
  4944. return;
  4945. }
  4946. else { /* Want to switch tag mode */
  4947. if (lp->busyccbs) /* If not yet safe, return */
  4948. return;
  4949. lp->queuedepth = reqtags;
  4950. lp->usetags = reqtags > 1 ? 1 : 0;
  4951. }
  4952. /*
  4953. ** Patch the lun mini-script, according to tag mode.
  4954. */
  4955. lp->jump_tag.l_paddr = lp->usetags?
  4956. cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
  4957. cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
  4958. /*
  4959. ** Announce change to user.
  4960. */
  4961. if (bootverbose) {
  4962. if (lp->usetags) {
  4963. dev_info(&sdev->sdev_gendev,
  4964. "tagged command queue depth set to %d\n",
  4965. reqtags);
  4966. } else {
  4967. dev_info(&sdev->sdev_gendev,
  4968. "tagged command queueing disabled\n");
  4969. }
  4970. }
  4971. }
  4972. /*==========================================================
  4973. **
  4974. **
  4975. ** ncr timeout handler.
  4976. **
  4977. **
  4978. **==========================================================
  4979. **
  4980. ** Misused to keep the driver running when
  4981. ** interrupts are not configured correctly.
  4982. **
  4983. **----------------------------------------------------------
  4984. */
  4985. static void ncr_timeout (struct ncb *np)
  4986. {
  4987. u_long thistime = jiffies;
  4988. /*
  4989. ** If release process in progress, let's go
  4990. ** Set the release stage from 1 to 2 to synchronize
  4991. ** with the release process.
  4992. */
  4993. if (np->release_stage) {
  4994. if (np->release_stage == 1) np->release_stage = 2;
  4995. return;
  4996. }
  4997. np->timer.expires = jiffies + SCSI_NCR_TIMER_INTERVAL;
  4998. add_timer(&np->timer);
  4999. /*
  5000. ** If we are resetting the ncr, wait for settle_time before
  5001. ** clearing it. Then command processing will be resumed.
  5002. */
  5003. if (np->settle_time) {
  5004. if (np->settle_time <= thistime) {
  5005. if (bootverbose > 1)
  5006. printk("%s: command processing resumed\n", ncr_name(np));
  5007. np->settle_time = 0;
  5008. np->disc = 1;
  5009. requeue_waiting_list(np);
  5010. }
  5011. return;
  5012. }
  5013. /*
  5014. ** Since the generic scsi driver only allows us 0.5 second
  5015. ** to perform abort of a command, we must look at ccbs about
  5016. ** every 0.25 second.
  5017. */
  5018. if (np->lasttime + 4*HZ < thistime) {
  5019. /*
  5020. ** block ncr interrupts
  5021. */
  5022. np->lasttime = thistime;
  5023. }
  5024. #ifdef SCSI_NCR_BROKEN_INTR
  5025. if (INB(nc_istat) & (INTF|SIP|DIP)) {
  5026. /*
  5027. ** Process pending interrupts.
  5028. */
  5029. if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
  5030. ncr_exception (np);
  5031. if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
  5032. }
  5033. #endif /* SCSI_NCR_BROKEN_INTR */
  5034. }
  5035. /*==========================================================
  5036. **
  5037. ** log message for real hard errors
  5038. **
  5039. ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
  5040. ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
  5041. **
  5042. ** exception register:
  5043. ** ds: dstat
  5044. ** si: sist
  5045. **
  5046. ** SCSI bus lines:
  5047. ** so: control lines as driver by NCR.
  5048. ** si: control lines as seen by NCR.
  5049. ** sd: scsi data lines as seen by NCR.
  5050. **
  5051. ** wide/fastmode:
  5052. ** sxfer: (see the manual)
  5053. ** scntl3: (see the manual)
  5054. **
  5055. ** current script command:
  5056. ** dsp: script address (relative to start of script).
  5057. ** dbc: first word of script command.
  5058. **
  5059. ** First 16 register of the chip:
  5060. ** r0..rf
  5061. **
  5062. **==========================================================
  5063. */
  5064. static void ncr_log_hard_error(struct ncb *np, u16 sist, u_char dstat)
  5065. {
  5066. u32 dsp;
  5067. int script_ofs;
  5068. int script_size;
  5069. char *script_name;
  5070. u_char *script_base;
  5071. int i;
  5072. dsp = INL (nc_dsp);
  5073. if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
  5074. script_ofs = dsp - np->p_script;
  5075. script_size = sizeof(struct script);
  5076. script_base = (u_char *) np->script0;
  5077. script_name = "script";
  5078. }
  5079. else if (np->p_scripth < dsp &&
  5080. dsp <= np->p_scripth + sizeof(struct scripth)) {
  5081. script_ofs = dsp - np->p_scripth;
  5082. script_size = sizeof(struct scripth);
  5083. script_base = (u_char *) np->scripth0;
  5084. script_name = "scripth";
  5085. } else {
  5086. script_ofs = dsp;
  5087. script_size = 0;
  5088. script_base = NULL;
  5089. script_name = "mem";
  5090. }
  5091. printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
  5092. ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
  5093. (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
  5094. (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
  5095. (unsigned)INL (nc_dbc));
  5096. if (((script_ofs & 3) == 0) &&
  5097. (unsigned)script_ofs < script_size) {
  5098. printk ("%s: script cmd = %08x\n", ncr_name(np),
  5099. scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
  5100. }
  5101. printk ("%s: regdump:", ncr_name(np));
  5102. for (i=0; i<16;i++)
  5103. printk (" %02x", (unsigned)INB_OFF(i));
  5104. printk (".\n");
  5105. }
  5106. /*============================================================
  5107. **
  5108. ** ncr chip exception handler.
  5109. **
  5110. **============================================================
  5111. **
  5112. ** In normal cases, interrupt conditions occur one at a
  5113. ** time. The ncr is able to stack in some extra registers
  5114. ** other interrupts that will occur after the first one.
  5115. ** But, several interrupts may occur at the same time.
  5116. **
  5117. ** We probably should only try to deal with the normal
  5118. ** case, but it seems that multiple interrupts occur in
  5119. ** some cases that are not abnormal at all.
  5120. **
  5121. ** The most frequent interrupt condition is Phase Mismatch.
  5122. ** We should want to service this interrupt quickly.
  5123. ** A SCSI parity error may be delivered at the same time.
  5124. ** The SIR interrupt is not very frequent in this driver,
  5125. ** since the INTFLY is likely used for command completion
  5126. ** signaling.
  5127. ** The Selection Timeout interrupt may be triggered with
  5128. ** IID and/or UDC.
  5129. ** The SBMC interrupt (SCSI Bus Mode Change) may probably
  5130. ** occur at any time.
  5131. **
  5132. ** This handler try to deal as cleverly as possible with all
  5133. ** the above.
  5134. **
  5135. **============================================================
  5136. */
  5137. void ncr_exception (struct ncb *np)
  5138. {
  5139. u_char istat, dstat;
  5140. u16 sist;
  5141. int i;
  5142. /*
  5143. ** interrupt on the fly ?
  5144. ** Since the global header may be copied back to a CCB
  5145. ** using a posted PCI memory write, the last operation on
  5146. ** the istat register is a READ in order to flush posted
  5147. ** PCI write commands.
  5148. */
  5149. istat = INB (nc_istat);
  5150. if (istat & INTF) {
  5151. OUTB (nc_istat, (istat & SIGP) | INTF);
  5152. istat = INB (nc_istat);
  5153. if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
  5154. ncr_wakeup_done (np);
  5155. }
  5156. if (!(istat & (SIP|DIP)))
  5157. return;
  5158. if (istat & CABRT)
  5159. OUTB (nc_istat, CABRT);
  5160. /*
  5161. ** Steinbach's Guideline for Systems Programming:
  5162. ** Never test for an error condition you don't know how to handle.
  5163. */
  5164. sist = (istat & SIP) ? INW (nc_sist) : 0;
  5165. dstat = (istat & DIP) ? INB (nc_dstat) : 0;
  5166. if (DEBUG_FLAGS & DEBUG_TINY)
  5167. printk ("<%d|%x:%x|%x:%x>",
  5168. (int)INB(nc_scr0),
  5169. dstat,sist,
  5170. (unsigned)INL(nc_dsp),
  5171. (unsigned)INL(nc_dbc));
  5172. /*========================================================
  5173. ** First, interrupts we want to service cleanly.
  5174. **
  5175. ** Phase mismatch is the most frequent interrupt, and
  5176. ** so we have to service it as quickly and as cleanly
  5177. ** as possible.
  5178. ** Programmed interrupts are rarely used in this driver,
  5179. ** but we must handle them cleanly anyway.
  5180. ** We try to deal with PAR and SBMC combined with
  5181. ** some other interrupt(s).
  5182. **=========================================================
  5183. */
  5184. if (!(sist & (STO|GEN|HTH|SGE|UDC|RST)) &&
  5185. !(dstat & (MDPE|BF|ABRT|IID))) {
  5186. if ((sist & SBMC) && ncr_int_sbmc (np))
  5187. return;
  5188. if ((sist & PAR) && ncr_int_par (np))
  5189. return;
  5190. if (sist & MA) {
  5191. ncr_int_ma (np);
  5192. return;
  5193. }
  5194. if (dstat & SIR) {
  5195. ncr_int_sir (np);
  5196. return;
  5197. }
  5198. /*
  5199. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 2.
  5200. */
  5201. if (!(sist & (SBMC|PAR)) && !(dstat & SSI)) {
  5202. printk( "%s: unknown interrupt(s) ignored, "
  5203. "ISTAT=%x DSTAT=%x SIST=%x\n",
  5204. ncr_name(np), istat, dstat, sist);
  5205. return;
  5206. }
  5207. OUTONB_STD ();
  5208. return;
  5209. }
  5210. /*========================================================
  5211. ** Now, interrupts that need some fixing up.
  5212. ** Order and multiple interrupts is so less important.
  5213. **
  5214. ** If SRST has been asserted, we just reset the chip.
  5215. **
  5216. ** Selection is intirely handled by the chip. If the
  5217. ** chip says STO, we trust it. Seems some other
  5218. ** interrupts may occur at the same time (UDC, IID), so
  5219. ** we ignore them. In any case we do enough fix-up
  5220. ** in the service routine.
  5221. ** We just exclude some fatal dma errors.
  5222. **=========================================================
  5223. */
  5224. if (sist & RST) {
  5225. ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
  5226. return;
  5227. }
  5228. if ((sist & STO) &&
  5229. !(dstat & (MDPE|BF|ABRT))) {
  5230. /*
  5231. ** DEL 397 - 53C875 Rev 3 - Part Number 609-0392410 - ITEM 1.
  5232. */
  5233. OUTONB (nc_ctest3, CLF);
  5234. ncr_int_sto (np);
  5235. return;
  5236. }
  5237. /*=========================================================
  5238. ** Now, interrupts we are not able to recover cleanly.
  5239. ** (At least for the moment).
  5240. **
  5241. ** Do the register dump.
  5242. ** Log message for real hard errors.
  5243. ** Clear all fifos.
  5244. ** For MDPE, BF, ABORT, IID, SGE and HTH we reset the
  5245. ** BUS and the chip.
  5246. ** We are more soft for UDC.
  5247. **=========================================================
  5248. */
  5249. if (time_after(jiffies, np->regtime)) {
  5250. np->regtime = jiffies + 10*HZ;
  5251. for (i = 0; i<sizeof(np->regdump); i++)
  5252. ((char*)&np->regdump)[i] = INB_OFF(i);
  5253. np->regdump.nc_dstat = dstat;
  5254. np->regdump.nc_sist = sist;
  5255. }
  5256. ncr_log_hard_error(np, sist, dstat);
  5257. printk ("%s: have to clear fifos.\n", ncr_name (np));
  5258. OUTB (nc_stest3, TE|CSF);
  5259. OUTONB (nc_ctest3, CLF);
  5260. if ((sist & (SGE)) ||
  5261. (dstat & (MDPE|BF|ABRT|IID))) {
  5262. ncr_start_reset(np);
  5263. return;
  5264. }
  5265. if (sist & HTH) {
  5266. printk ("%s: handshake timeout\n", ncr_name(np));
  5267. ncr_start_reset(np);
  5268. return;
  5269. }
  5270. if (sist & UDC) {
  5271. printk ("%s: unexpected disconnect\n", ncr_name(np));
  5272. OUTB (HS_PRT, HS_UNEXPECTED);
  5273. OUTL_DSP (NCB_SCRIPT_PHYS (np, cleanup));
  5274. return;
  5275. }
  5276. /*=========================================================
  5277. ** We just miss the cause of the interrupt. :(
  5278. ** Print a message. The timeout will do the real work.
  5279. **=========================================================
  5280. */
  5281. printk ("%s: unknown interrupt\n", ncr_name(np));
  5282. }
  5283. /*==========================================================
  5284. **
  5285. ** ncr chip exception handler for selection timeout
  5286. **
  5287. **==========================================================
  5288. **
  5289. ** There seems to be a bug in the 53c810.
  5290. ** Although a STO-Interrupt is pending,
  5291. ** it continues executing script commands.
  5292. ** But it will fail and interrupt (IID) on
  5293. ** the next instruction where it's looking
  5294. ** for a valid phase.
  5295. **
  5296. **----------------------------------------------------------
  5297. */
  5298. void ncr_int_sto (struct ncb *np)
  5299. {
  5300. u_long dsa;
  5301. struct ccb *cp;
  5302. if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
  5303. /*
  5304. ** look for ccb and set the status.
  5305. */
  5306. dsa = INL (nc_dsa);
  5307. cp = np->ccb;
  5308. while (cp && (CCB_PHYS (cp, phys) != dsa))
  5309. cp = cp->link_ccb;
  5310. if (cp) {
  5311. cp-> host_status = HS_SEL_TIMEOUT;
  5312. ncr_complete (np, cp);
  5313. }
  5314. /*
  5315. ** repair start queue and jump to start point.
  5316. */
  5317. OUTL_DSP (NCB_SCRIPTH_PHYS (np, sto_restart));
  5318. return;
  5319. }
  5320. /*==========================================================
  5321. **
  5322. ** ncr chip exception handler for SCSI bus mode change
  5323. **
  5324. **==========================================================
  5325. **
  5326. ** spi2-r12 11.2.3 says a transceiver mode change must
  5327. ** generate a reset event and a device that detects a reset
  5328. ** event shall initiate a hard reset. It says also that a
  5329. ** device that detects a mode change shall set data transfer
  5330. ** mode to eight bit asynchronous, etc...
  5331. ** So, just resetting should be enough.
  5332. **
  5333. **
  5334. **----------------------------------------------------------
  5335. */
  5336. static int ncr_int_sbmc (struct ncb *np)
  5337. {
  5338. u_char scsi_mode = INB (nc_stest4) & SMODE;
  5339. if (scsi_mode != np->scsi_mode) {
  5340. printk("%s: SCSI bus mode change from %x to %x.\n",
  5341. ncr_name(np), np->scsi_mode, scsi_mode);
  5342. np->scsi_mode = scsi_mode;
  5343. /*
  5344. ** Suspend command processing for 1 second and
  5345. ** reinitialize all except the chip.
  5346. */
  5347. np->settle_time = jiffies + HZ;
  5348. ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
  5349. return 1;
  5350. }
  5351. return 0;
  5352. }
  5353. /*==========================================================
  5354. **
  5355. ** ncr chip exception handler for SCSI parity error.
  5356. **
  5357. **==========================================================
  5358. **
  5359. **
  5360. **----------------------------------------------------------
  5361. */
  5362. static int ncr_int_par (struct ncb *np)
  5363. {
  5364. u_char hsts = INB (HS_PRT);
  5365. u32 dbc = INL (nc_dbc);
  5366. u_char sstat1 = INB (nc_sstat1);
  5367. int phase = -1;
  5368. int msg = -1;
  5369. u32 jmp;
  5370. printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SSTAT1=%x\n",
  5371. ncr_name(np), hsts, dbc, sstat1);
  5372. /*
  5373. * Ignore the interrupt if the NCR is not connected
  5374. * to the SCSI bus, since the right work should have
  5375. * been done on unexpected disconnection handling.
  5376. */
  5377. if (!(INB (nc_scntl1) & ISCON))
  5378. return 0;
  5379. /*
  5380. * If the nexus is not clearly identified, reset the bus.
  5381. * We will try to do better later.
  5382. */
  5383. if (hsts & HS_INVALMASK)
  5384. goto reset_all;
  5385. /*
  5386. * If the SCSI parity error occurs in MSG IN phase, prepare a
  5387. * MSG PARITY message. Otherwise, prepare a INITIATOR DETECTED
  5388. * ERROR message and let the device decide to retry the command
  5389. * or to terminate with check condition. If we were in MSG IN
  5390. * phase waiting for the response of a negotiation, we will
  5391. * get SIR_NEGO_FAILED at dispatch.
  5392. */
  5393. if (!(dbc & 0xc0000000))
  5394. phase = (dbc >> 24) & 7;
  5395. if (phase == 7)
  5396. msg = MSG_PARITY_ERROR;
  5397. else
  5398. msg = INITIATOR_ERROR;
  5399. /*
  5400. * If the NCR stopped on a MOVE ^ DATA_IN, we jump to a
  5401. * script that will ignore all data in bytes until phase
  5402. * change, since we are not sure the chip will wait the phase
  5403. * change prior to delivering the interrupt.
  5404. */
  5405. if (phase == 1)
  5406. jmp = NCB_SCRIPTH_PHYS (np, par_err_data_in);
  5407. else
  5408. jmp = NCB_SCRIPTH_PHYS (np, par_err_other);
  5409. OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
  5410. OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
  5411. np->msgout[0] = msg;
  5412. OUTL_DSP (jmp);
  5413. return 1;
  5414. reset_all:
  5415. ncr_start_reset(np);
  5416. return 1;
  5417. }
  5418. /*==========================================================
  5419. **
  5420. **
  5421. ** ncr chip exception handler for phase errors.
  5422. **
  5423. **
  5424. **==========================================================
  5425. **
  5426. ** We have to construct a new transfer descriptor,
  5427. ** to transfer the rest of the current block.
  5428. **
  5429. **----------------------------------------------------------
  5430. */
  5431. static void ncr_int_ma (struct ncb *np)
  5432. {
  5433. u32 dbc;
  5434. u32 rest;
  5435. u32 dsp;
  5436. u32 dsa;
  5437. u32 nxtdsp;
  5438. u32 newtmp;
  5439. u32 *vdsp;
  5440. u32 oadr, olen;
  5441. u32 *tblp;
  5442. ncrcmd *newcmd;
  5443. u_char cmd, sbcl;
  5444. struct ccb *cp;
  5445. dsp = INL (nc_dsp);
  5446. dbc = INL (nc_dbc);
  5447. sbcl = INB (nc_sbcl);
  5448. cmd = dbc >> 24;
  5449. rest = dbc & 0xffffff;
  5450. /*
  5451. ** Take into account dma fifo and various buffers and latches,
  5452. ** only if the interrupted phase is an OUTPUT phase.
  5453. */
  5454. if ((cmd & 1) == 0) {
  5455. u_char ctest5, ss0, ss2;
  5456. u16 delta;
  5457. ctest5 = (np->rv_ctest5 & DFS) ? INB (nc_ctest5) : 0;
  5458. if (ctest5 & DFS)
  5459. delta=(((ctest5 << 8) | (INB (nc_dfifo) & 0xff)) - rest) & 0x3ff;
  5460. else
  5461. delta=(INB (nc_dfifo) - rest) & 0x7f;
  5462. /*
  5463. ** The data in the dma fifo has not been transferred to
  5464. ** the target -> add the amount to the rest
  5465. ** and clear the data.
  5466. ** Check the sstat2 register in case of wide transfer.
  5467. */
  5468. rest += delta;
  5469. ss0 = INB (nc_sstat0);
  5470. if (ss0 & OLF) rest++;
  5471. if (ss0 & ORF) rest++;
  5472. if (INB(nc_scntl3) & EWS) {
  5473. ss2 = INB (nc_sstat2);
  5474. if (ss2 & OLF1) rest++;
  5475. if (ss2 & ORF1) rest++;
  5476. }
  5477. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  5478. printk ("P%x%x RL=%d D=%d SS0=%x ", cmd&7, sbcl&7,
  5479. (unsigned) rest, (unsigned) delta, ss0);
  5480. } else {
  5481. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  5482. printk ("P%x%x RL=%d ", cmd&7, sbcl&7, rest);
  5483. }
  5484. /*
  5485. ** Clear fifos.
  5486. */
  5487. OUTONB (nc_ctest3, CLF ); /* clear dma fifo */
  5488. OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
  5489. /*
  5490. ** locate matching cp.
  5491. ** if the interrupted phase is DATA IN or DATA OUT,
  5492. ** trust the global header.
  5493. */
  5494. dsa = INL (nc_dsa);
  5495. if (!(cmd & 6)) {
  5496. cp = np->header.cp;
  5497. if (CCB_PHYS(cp, phys) != dsa)
  5498. cp = NULL;
  5499. } else {
  5500. cp = np->ccb;
  5501. while (cp && (CCB_PHYS (cp, phys) != dsa))
  5502. cp = cp->link_ccb;
  5503. }
  5504. /*
  5505. ** try to find the interrupted script command,
  5506. ** and the address at which to continue.
  5507. */
  5508. vdsp = NULL;
  5509. nxtdsp = 0;
  5510. if (dsp > np->p_script &&
  5511. dsp <= np->p_script + sizeof(struct script)) {
  5512. vdsp = (u32 *)((char*)np->script0 + (dsp-np->p_script-8));
  5513. nxtdsp = dsp;
  5514. }
  5515. else if (dsp > np->p_scripth &&
  5516. dsp <= np->p_scripth + sizeof(struct scripth)) {
  5517. vdsp = (u32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
  5518. nxtdsp = dsp;
  5519. }
  5520. else if (cp) {
  5521. if (dsp == CCB_PHYS (cp, patch[2])) {
  5522. vdsp = &cp->patch[0];
  5523. nxtdsp = scr_to_cpu(vdsp[3]);
  5524. }
  5525. else if (dsp == CCB_PHYS (cp, patch[6])) {
  5526. vdsp = &cp->patch[4];
  5527. nxtdsp = scr_to_cpu(vdsp[3]);
  5528. }
  5529. }
  5530. /*
  5531. ** log the information
  5532. */
  5533. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5534. printk ("\nCP=%p CP2=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
  5535. cp, np->header.cp,
  5536. (unsigned)dsp,
  5537. (unsigned)nxtdsp, vdsp, cmd);
  5538. }
  5539. /*
  5540. ** cp=0 means that the DSA does not point to a valid control
  5541. ** block. This should not happen since we donnot use multi-byte
  5542. ** move while we are being reselected ot after command complete.
  5543. ** We are not able to recover from such a phase error.
  5544. */
  5545. if (!cp) {
  5546. printk ("%s: SCSI phase error fixup: "
  5547. "CCB already dequeued (0x%08lx)\n",
  5548. ncr_name (np), (u_long) np->header.cp);
  5549. goto reset_all;
  5550. }
  5551. /*
  5552. ** get old startaddress and old length.
  5553. */
  5554. oadr = scr_to_cpu(vdsp[1]);
  5555. if (cmd & 0x10) { /* Table indirect */
  5556. tblp = (u32 *) ((char*) &cp->phys + oadr);
  5557. olen = scr_to_cpu(tblp[0]);
  5558. oadr = scr_to_cpu(tblp[1]);
  5559. } else {
  5560. tblp = (u32 *) 0;
  5561. olen = scr_to_cpu(vdsp[0]) & 0xffffff;
  5562. }
  5563. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5564. printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
  5565. (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
  5566. tblp,
  5567. (unsigned) olen,
  5568. (unsigned) oadr);
  5569. }
  5570. /*
  5571. ** check cmd against assumed interrupted script command.
  5572. */
  5573. if (cmd != (scr_to_cpu(vdsp[0]) >> 24)) {
  5574. PRINT_ADDR(cp->cmd, "internal error: cmd=%02x != %02x=(vdsp[0] "
  5575. ">> 24)\n", cmd, scr_to_cpu(vdsp[0]) >> 24);
  5576. goto reset_all;
  5577. }
  5578. /*
  5579. ** cp != np->header.cp means that the header of the CCB
  5580. ** currently being processed has not yet been copied to
  5581. ** the global header area. That may happen if the device did
  5582. ** not accept all our messages after having been selected.
  5583. */
  5584. if (cp != np->header.cp) {
  5585. printk ("%s: SCSI phase error fixup: "
  5586. "CCB address mismatch (0x%08lx != 0x%08lx)\n",
  5587. ncr_name (np), (u_long) cp, (u_long) np->header.cp);
  5588. }
  5589. /*
  5590. ** if old phase not dataphase, leave here.
  5591. */
  5592. if (cmd & 0x06) {
  5593. PRINT_ADDR(cp->cmd, "phase change %x-%x %d@%08x resid=%d.\n",
  5594. cmd&7, sbcl&7, (unsigned)olen,
  5595. (unsigned)oadr, (unsigned)rest);
  5596. goto unexpected_phase;
  5597. }
  5598. /*
  5599. ** choose the correct patch area.
  5600. ** if savep points to one, choose the other.
  5601. */
  5602. newcmd = cp->patch;
  5603. newtmp = CCB_PHYS (cp, patch);
  5604. if (newtmp == scr_to_cpu(cp->phys.header.savep)) {
  5605. newcmd = &cp->patch[4];
  5606. newtmp = CCB_PHYS (cp, patch[4]);
  5607. }
  5608. /*
  5609. ** fillin the commands
  5610. */
  5611. newcmd[0] = cpu_to_scr(((cmd & 0x0f) << 24) | rest);
  5612. newcmd[1] = cpu_to_scr(oadr + olen - rest);
  5613. newcmd[2] = cpu_to_scr(SCR_JUMP);
  5614. newcmd[3] = cpu_to_scr(nxtdsp);
  5615. if (DEBUG_FLAGS & DEBUG_PHASE) {
  5616. PRINT_ADDR(cp->cmd, "newcmd[%d] %x %x %x %x.\n",
  5617. (int) (newcmd - cp->patch),
  5618. (unsigned)scr_to_cpu(newcmd[0]),
  5619. (unsigned)scr_to_cpu(newcmd[1]),
  5620. (unsigned)scr_to_cpu(newcmd[2]),
  5621. (unsigned)scr_to_cpu(newcmd[3]));
  5622. }
  5623. /*
  5624. ** fake the return address (to the patch).
  5625. ** and restart script processor at dispatcher.
  5626. */
  5627. OUTL (nc_temp, newtmp);
  5628. OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
  5629. return;
  5630. /*
  5631. ** Unexpected phase changes that occurs when the current phase
  5632. ** is not a DATA IN or DATA OUT phase are due to error conditions.
  5633. ** Such event may only happen when the SCRIPTS is using a
  5634. ** multibyte SCSI MOVE.
  5635. **
  5636. ** Phase change Some possible cause
  5637. **
  5638. ** COMMAND --> MSG IN SCSI parity error detected by target.
  5639. ** COMMAND --> STATUS Bad command or refused by target.
  5640. ** MSG OUT --> MSG IN Message rejected by target.
  5641. ** MSG OUT --> COMMAND Bogus target that discards extended
  5642. ** negotiation messages.
  5643. **
  5644. ** The code below does not care of the new phase and so
  5645. ** trusts the target. Why to annoy it ?
  5646. ** If the interrupted phase is COMMAND phase, we restart at
  5647. ** dispatcher.
  5648. ** If a target does not get all the messages after selection,
  5649. ** the code assumes blindly that the target discards extended
  5650. ** messages and clears the negotiation status.
  5651. ** If the target does not want all our response to negotiation,
  5652. ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
  5653. ** bloat for such a should_not_happen situation).
  5654. ** In all other situation, we reset the BUS.
  5655. ** Are these assumptions reasonable ? (Wait and see ...)
  5656. */
  5657. unexpected_phase:
  5658. dsp -= 8;
  5659. nxtdsp = 0;
  5660. switch (cmd & 7) {
  5661. case 2: /* COMMAND phase */
  5662. nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
  5663. break;
  5664. #if 0
  5665. case 3: /* STATUS phase */
  5666. nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
  5667. break;
  5668. #endif
  5669. case 6: /* MSG OUT phase */
  5670. np->scripth->nxtdsp_go_on[0] = cpu_to_scr(dsp + 8);
  5671. if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
  5672. cp->host_status = HS_BUSY;
  5673. nxtdsp = NCB_SCRIPTH_PHYS (np, clratn_go_on);
  5674. }
  5675. else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
  5676. dsp == NCB_SCRIPTH_PHYS (np, send_sdtr)) {
  5677. nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
  5678. }
  5679. break;
  5680. #if 0
  5681. case 7: /* MSG IN phase */
  5682. nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
  5683. break;
  5684. #endif
  5685. }
  5686. if (nxtdsp) {
  5687. OUTL_DSP (nxtdsp);
  5688. return;
  5689. }
  5690. reset_all:
  5691. ncr_start_reset(np);
  5692. }
  5693. static void ncr_sir_to_redo(struct ncb *np, int num, struct ccb *cp)
  5694. {
  5695. struct scsi_cmnd *cmd = cp->cmd;
  5696. struct tcb *tp = &np->target[cmd->device->id];
  5697. struct lcb *lp = tp->lp[cmd->device->lun];
  5698. struct list_head *qp;
  5699. struct ccb * cp2;
  5700. int disc_cnt = 0;
  5701. int busy_cnt = 0;
  5702. u32 startp;
  5703. u_char s_status = INB (SS_PRT);
  5704. /*
  5705. ** Let the SCRIPTS processor skip all not yet started CCBs,
  5706. ** and count disconnected CCBs. Since the busy queue is in
  5707. ** the same order as the chip start queue, disconnected CCBs
  5708. ** are before cp and busy ones after.
  5709. */
  5710. if (lp) {
  5711. qp = lp->busy_ccbq.prev;
  5712. while (qp != &lp->busy_ccbq) {
  5713. cp2 = list_entry(qp, struct ccb, link_ccbq);
  5714. qp = qp->prev;
  5715. ++busy_cnt;
  5716. if (cp2 == cp)
  5717. break;
  5718. cp2->start.schedule.l_paddr =
  5719. cpu_to_scr(NCB_SCRIPTH_PHYS (np, skip));
  5720. }
  5721. lp->held_ccb = cp; /* Requeue when this one completes */
  5722. disc_cnt = lp->queuedccbs - busy_cnt;
  5723. }
  5724. switch(s_status) {
  5725. default: /* Just for safety, should never happen */
  5726. case SAM_STAT_TASK_SET_FULL:
  5727. /*
  5728. ** Decrease number of tags to the number of
  5729. ** disconnected commands.
  5730. */
  5731. if (!lp)
  5732. goto out;
  5733. if (bootverbose >= 1) {
  5734. PRINT_ADDR(cmd, "QUEUE FULL! %d busy, %d disconnected "
  5735. "CCBs\n", busy_cnt, disc_cnt);
  5736. }
  5737. if (disc_cnt < lp->numtags) {
  5738. lp->numtags = disc_cnt > 2 ? disc_cnt : 2;
  5739. lp->num_good = 0;
  5740. ncr_setup_tags (np, cmd->device);
  5741. }
  5742. /*
  5743. ** Requeue the command to the start queue.
  5744. ** If any disconnected commands,
  5745. ** Clear SIGP.
  5746. ** Jump to reselect.
  5747. */
  5748. cp->phys.header.savep = cp->startp;
  5749. cp->host_status = HS_BUSY;
  5750. cp->scsi_status = SAM_STAT_ILLEGAL;
  5751. ncr_put_start_queue(np, cp);
  5752. if (disc_cnt)
  5753. INB (nc_ctest2); /* Clear SIGP */
  5754. OUTL_DSP (NCB_SCRIPT_PHYS (np, reselect));
  5755. return;
  5756. case SAM_STAT_COMMAND_TERMINATED:
  5757. case SAM_STAT_CHECK_CONDITION:
  5758. /*
  5759. ** If we were requesting sense, give up.
  5760. */
  5761. if (cp->auto_sense)
  5762. goto out;
  5763. /*
  5764. ** Device returned CHECK CONDITION status.
  5765. ** Prepare all needed data strutures for getting
  5766. ** sense data.
  5767. **
  5768. ** identify message
  5769. */
  5770. cp->scsi_smsg2[0] = IDENTIFY(0, cmd->device->lun);
  5771. cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
  5772. cp->phys.smsg.size = cpu_to_scr(1);
  5773. /*
  5774. ** sense command
  5775. */
  5776. cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
  5777. cp->phys.cmd.size = cpu_to_scr(6);
  5778. /*
  5779. ** patch requested size into sense command
  5780. */
  5781. cp->sensecmd[0] = 0x03;
  5782. cp->sensecmd[1] = (cmd->device->lun & 0x7) << 5;
  5783. cp->sensecmd[4] = sizeof(cp->sense_buf);
  5784. /*
  5785. ** sense data
  5786. */
  5787. memset(cp->sense_buf, 0, sizeof(cp->sense_buf));
  5788. cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
  5789. cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
  5790. /*
  5791. ** requeue the command.
  5792. */
  5793. startp = cpu_to_scr(NCB_SCRIPTH_PHYS (np, sdata_in));
  5794. cp->phys.header.savep = startp;
  5795. cp->phys.header.goalp = startp + 24;
  5796. cp->phys.header.lastp = startp;
  5797. cp->phys.header.wgoalp = startp + 24;
  5798. cp->phys.header.wlastp = startp;
  5799. cp->host_status = HS_BUSY;
  5800. cp->scsi_status = SAM_STAT_ILLEGAL;
  5801. cp->auto_sense = s_status;
  5802. cp->start.schedule.l_paddr =
  5803. cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
  5804. /*
  5805. ** Select without ATN for quirky devices.
  5806. */
  5807. if (cmd->device->select_no_atn)
  5808. cp->start.schedule.l_paddr =
  5809. cpu_to_scr(NCB_SCRIPTH_PHYS (np, select_no_atn));
  5810. ncr_put_start_queue(np, cp);
  5811. OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
  5812. return;
  5813. }
  5814. out:
  5815. OUTONB_STD ();
  5816. return;
  5817. }
  5818. /*==========================================================
  5819. **
  5820. **
  5821. ** ncr chip exception handler for programmed interrupts.
  5822. **
  5823. **
  5824. **==========================================================
  5825. */
  5826. void ncr_int_sir (struct ncb *np)
  5827. {
  5828. u_char scntl3;
  5829. u_char chg, ofs, per, fak, wide;
  5830. u_char num = INB (nc_dsps);
  5831. struct ccb *cp=NULL;
  5832. u_long dsa = INL (nc_dsa);
  5833. u_char target = INB (nc_sdid) & 0x0f;
  5834. struct tcb *tp = &np->target[target];
  5835. struct scsi_target *starget = tp->starget;
  5836. if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
  5837. switch (num) {
  5838. case SIR_INTFLY:
  5839. /*
  5840. ** This is used for HP Zalon/53c720 where INTFLY
  5841. ** operation is currently broken.
  5842. */
  5843. ncr_wakeup_done(np);
  5844. #ifdef SCSI_NCR_CCB_DONE_SUPPORT
  5845. OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, done_end) + 8);
  5846. #else
  5847. OUTL(nc_dsp, NCB_SCRIPT_PHYS (np, start));
  5848. #endif
  5849. return;
  5850. case SIR_RESEL_NO_MSG_IN:
  5851. case SIR_RESEL_NO_IDENTIFY:
  5852. /*
  5853. ** If devices reselecting without sending an IDENTIFY
  5854. ** message still exist, this should help.
  5855. ** We just assume lun=0, 1 CCB, no tag.
  5856. */
  5857. if (tp->lp[0]) {
  5858. OUTL_DSP (scr_to_cpu(tp->lp[0]->jump_ccb[0]));
  5859. return;
  5860. }
  5861. fallthrough;
  5862. case SIR_RESEL_BAD_TARGET: /* Will send a TARGET RESET message */
  5863. case SIR_RESEL_BAD_LUN: /* Will send a TARGET RESET message */
  5864. case SIR_RESEL_BAD_I_T_L_Q: /* Will send an ABORT TAG message */
  5865. case SIR_RESEL_BAD_I_T_L: /* Will send an ABORT message */
  5866. printk ("%s:%d: SIR %d, "
  5867. "incorrect nexus identification on reselection\n",
  5868. ncr_name (np), target, num);
  5869. goto out;
  5870. case SIR_DONE_OVERFLOW:
  5871. printk ("%s:%d: SIR %d, "
  5872. "CCB done queue overflow\n",
  5873. ncr_name (np), target, num);
  5874. goto out;
  5875. case SIR_BAD_STATUS:
  5876. cp = np->header.cp;
  5877. if (!cp || CCB_PHYS (cp, phys) != dsa)
  5878. goto out;
  5879. ncr_sir_to_redo(np, num, cp);
  5880. return;
  5881. default:
  5882. /*
  5883. ** lookup the ccb
  5884. */
  5885. cp = np->ccb;
  5886. while (cp && (CCB_PHYS (cp, phys) != dsa))
  5887. cp = cp->link_ccb;
  5888. BUG_ON(!cp);
  5889. BUG_ON(cp != np->header.cp);
  5890. if (!cp || cp != np->header.cp)
  5891. goto out;
  5892. }
  5893. switch (num) {
  5894. /*-----------------------------------------------------------------------------
  5895. **
  5896. ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
  5897. ** ("Everything you've always wanted to know about transfer mode
  5898. ** negotiation")
  5899. **
  5900. ** We try to negotiate sync and wide transfer only after
  5901. ** a successful inquire command. We look at byte 7 of the
  5902. ** inquire data to determine the capabilities of the target.
  5903. **
  5904. ** When we try to negotiate, we append the negotiation message
  5905. ** to the identify and (maybe) simple tag message.
  5906. ** The host status field is set to HS_NEGOTIATE to mark this
  5907. ** situation.
  5908. **
  5909. ** If the target doesn't answer this message immediately
  5910. ** (as required by the standard), the SIR_NEGO_FAIL interrupt
  5911. ** will be raised eventually.
  5912. ** The handler removes the HS_NEGOTIATE status, and sets the
  5913. ** negotiated value to the default (async / nowide).
  5914. **
  5915. ** If we receive a matching answer immediately, we check it
  5916. ** for validity, and set the values.
  5917. **
  5918. ** If we receive a Reject message immediately, we assume the
  5919. ** negotiation has failed, and fall back to standard values.
  5920. **
  5921. ** If we receive a negotiation message while not in HS_NEGOTIATE
  5922. ** state, it's a target initiated negotiation. We prepare a
  5923. ** (hopefully) valid answer, set our parameters, and send back
  5924. ** this answer to the target.
  5925. **
  5926. ** If the target doesn't fetch the answer (no message out phase),
  5927. ** we assume the negotiation has failed, and fall back to default
  5928. ** settings.
  5929. **
  5930. ** When we set the values, we adjust them in all ccbs belonging
  5931. ** to this target, in the controller's register, and in the "phys"
  5932. ** field of the controller's struct ncb.
  5933. **
  5934. ** Possible cases: hs sir msg_in value send goto
  5935. ** We try to negotiate:
  5936. ** -> target doesn't msgin NEG FAIL noop defa. - dispatch
  5937. ** -> target rejected our msg NEG FAIL reject defa. - dispatch
  5938. ** -> target answered (ok) NEG SYNC sdtr set - clrack
  5939. ** -> target answered (!ok) NEG SYNC sdtr defa. REJ--->msg_bad
  5940. ** -> target answered (ok) NEG WIDE wdtr set - clrack
  5941. ** -> target answered (!ok) NEG WIDE wdtr defa. REJ--->msg_bad
  5942. ** -> any other msgin NEG FAIL noop defa. - dispatch
  5943. **
  5944. ** Target tries to negotiate:
  5945. ** -> incoming message --- SYNC sdtr set SDTR -
  5946. ** -> incoming message --- WIDE wdtr set WDTR -
  5947. ** We sent our answer:
  5948. ** -> target doesn't msgout --- PROTO ? defa. - dispatch
  5949. **
  5950. **-----------------------------------------------------------------------------
  5951. */
  5952. case SIR_NEGO_FAILED:
  5953. /*-------------------------------------------------------
  5954. **
  5955. ** Negotiation failed.
  5956. ** Target doesn't send an answer message,
  5957. ** or target rejected our message.
  5958. **
  5959. ** Remove negotiation request.
  5960. **
  5961. **-------------------------------------------------------
  5962. */
  5963. OUTB (HS_PRT, HS_BUSY);
  5964. fallthrough;
  5965. case SIR_NEGO_PROTO:
  5966. /*-------------------------------------------------------
  5967. **
  5968. ** Negotiation failed.
  5969. ** Target doesn't fetch the answer message.
  5970. **
  5971. **-------------------------------------------------------
  5972. */
  5973. if (DEBUG_FLAGS & DEBUG_NEGO) {
  5974. PRINT_ADDR(cp->cmd, "negotiation failed sir=%x "
  5975. "status=%x.\n", num, cp->nego_status);
  5976. }
  5977. /*
  5978. ** any error in negotiation:
  5979. ** fall back to default mode.
  5980. */
  5981. switch (cp->nego_status) {
  5982. case NS_SYNC:
  5983. spi_period(starget) = 0;
  5984. spi_offset(starget) = 0;
  5985. ncr_setsync (np, cp, 0, 0xe0);
  5986. break;
  5987. case NS_WIDE:
  5988. spi_width(starget) = 0;
  5989. ncr_setwide (np, cp, 0, 0);
  5990. break;
  5991. }
  5992. np->msgin [0] = NOP;
  5993. np->msgout[0] = NOP;
  5994. cp->nego_status = 0;
  5995. break;
  5996. case SIR_NEGO_SYNC:
  5997. if (DEBUG_FLAGS & DEBUG_NEGO) {
  5998. ncr_print_msg(cp, "sync msgin", np->msgin);
  5999. }
  6000. chg = 0;
  6001. per = np->msgin[3];
  6002. ofs = np->msgin[4];
  6003. if (ofs==0) per=255;
  6004. /*
  6005. ** if target sends SDTR message,
  6006. ** it CAN transfer synch.
  6007. */
  6008. if (ofs && starget)
  6009. spi_support_sync(starget) = 1;
  6010. /*
  6011. ** check values against driver limits.
  6012. */
  6013. if (per < np->minsync)
  6014. {chg = 1; per = np->minsync;}
  6015. if (per < tp->minsync)
  6016. {chg = 1; per = tp->minsync;}
  6017. if (ofs > tp->maxoffs)
  6018. {chg = 1; ofs = tp->maxoffs;}
  6019. /*
  6020. ** Check against controller limits.
  6021. */
  6022. fak = 7;
  6023. scntl3 = 0;
  6024. if (ofs != 0) {
  6025. ncr_getsync(np, per, &fak, &scntl3);
  6026. if (fak > 7) {
  6027. chg = 1;
  6028. ofs = 0;
  6029. }
  6030. }
  6031. if (ofs == 0) {
  6032. fak = 7;
  6033. per = 0;
  6034. scntl3 = 0;
  6035. tp->minsync = 0;
  6036. }
  6037. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6038. PRINT_ADDR(cp->cmd, "sync: per=%d scntl3=0x%x ofs=%d "
  6039. "fak=%d chg=%d.\n", per, scntl3, ofs, fak, chg);
  6040. }
  6041. if (INB (HS_PRT) == HS_NEGOTIATE) {
  6042. OUTB (HS_PRT, HS_BUSY);
  6043. switch (cp->nego_status) {
  6044. case NS_SYNC:
  6045. /* This was an answer message */
  6046. if (chg) {
  6047. /* Answer wasn't acceptable. */
  6048. spi_period(starget) = 0;
  6049. spi_offset(starget) = 0;
  6050. ncr_setsync(np, cp, 0, 0xe0);
  6051. OUTL_DSP(NCB_SCRIPT_PHYS (np, msg_bad));
  6052. } else {
  6053. /* Answer is ok. */
  6054. spi_period(starget) = per;
  6055. spi_offset(starget) = ofs;
  6056. ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
  6057. OUTL_DSP(NCB_SCRIPT_PHYS (np, clrack));
  6058. }
  6059. return;
  6060. case NS_WIDE:
  6061. spi_width(starget) = 0;
  6062. ncr_setwide(np, cp, 0, 0);
  6063. break;
  6064. }
  6065. }
  6066. /*
  6067. ** It was a request. Set value and
  6068. ** prepare an answer message
  6069. */
  6070. spi_period(starget) = per;
  6071. spi_offset(starget) = ofs;
  6072. ncr_setsync(np, cp, scntl3, (fak<<5)|ofs);
  6073. spi_populate_sync_msg(np->msgout, per, ofs);
  6074. cp->nego_status = NS_SYNC;
  6075. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6076. ncr_print_msg(cp, "sync msgout", np->msgout);
  6077. }
  6078. if (!ofs) {
  6079. OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
  6080. return;
  6081. }
  6082. np->msgin [0] = NOP;
  6083. break;
  6084. case SIR_NEGO_WIDE:
  6085. /*
  6086. ** Wide request message received.
  6087. */
  6088. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6089. ncr_print_msg(cp, "wide msgin", np->msgin);
  6090. }
  6091. /*
  6092. ** get requested values.
  6093. */
  6094. chg = 0;
  6095. wide = np->msgin[3];
  6096. /*
  6097. ** if target sends WDTR message,
  6098. ** it CAN transfer wide.
  6099. */
  6100. if (wide && starget)
  6101. spi_support_wide(starget) = 1;
  6102. /*
  6103. ** check values against driver limits.
  6104. */
  6105. if (wide > tp->usrwide)
  6106. {chg = 1; wide = tp->usrwide;}
  6107. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6108. PRINT_ADDR(cp->cmd, "wide: wide=%d chg=%d.\n", wide,
  6109. chg);
  6110. }
  6111. if (INB (HS_PRT) == HS_NEGOTIATE) {
  6112. OUTB (HS_PRT, HS_BUSY);
  6113. switch (cp->nego_status) {
  6114. case NS_WIDE:
  6115. /*
  6116. ** This was an answer message
  6117. */
  6118. if (chg) {
  6119. /* Answer wasn't acceptable. */
  6120. spi_width(starget) = 0;
  6121. ncr_setwide(np, cp, 0, 1);
  6122. OUTL_DSP (NCB_SCRIPT_PHYS (np, msg_bad));
  6123. } else {
  6124. /* Answer is ok. */
  6125. spi_width(starget) = wide;
  6126. ncr_setwide(np, cp, wide, 1);
  6127. OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
  6128. }
  6129. return;
  6130. case NS_SYNC:
  6131. spi_period(starget) = 0;
  6132. spi_offset(starget) = 0;
  6133. ncr_setsync(np, cp, 0, 0xe0);
  6134. break;
  6135. }
  6136. }
  6137. /*
  6138. ** It was a request, set value and
  6139. ** prepare an answer message
  6140. */
  6141. spi_width(starget) = wide;
  6142. ncr_setwide(np, cp, wide, 1);
  6143. spi_populate_width_msg(np->msgout, wide);
  6144. np->msgin [0] = NOP;
  6145. cp->nego_status = NS_WIDE;
  6146. if (DEBUG_FLAGS & DEBUG_NEGO) {
  6147. ncr_print_msg(cp, "wide msgout", np->msgin);
  6148. }
  6149. break;
  6150. /*--------------------------------------------------------------------
  6151. **
  6152. ** Processing of special messages
  6153. **
  6154. **--------------------------------------------------------------------
  6155. */
  6156. case SIR_REJECT_RECEIVED:
  6157. /*-----------------------------------------------
  6158. **
  6159. ** We received a MESSAGE_REJECT.
  6160. **
  6161. **-----------------------------------------------
  6162. */
  6163. PRINT_ADDR(cp->cmd, "MESSAGE_REJECT received (%x:%x).\n",
  6164. (unsigned)scr_to_cpu(np->lastmsg), np->msgout[0]);
  6165. break;
  6166. case SIR_REJECT_SENT:
  6167. /*-----------------------------------------------
  6168. **
  6169. ** We received an unknown message
  6170. **
  6171. **-----------------------------------------------
  6172. */
  6173. ncr_print_msg(cp, "MESSAGE_REJECT sent for", np->msgin);
  6174. break;
  6175. /*--------------------------------------------------------------------
  6176. **
  6177. ** Processing of special messages
  6178. **
  6179. **--------------------------------------------------------------------
  6180. */
  6181. case SIR_IGN_RESIDUE:
  6182. /*-----------------------------------------------
  6183. **
  6184. ** We received an IGNORE RESIDUE message,
  6185. ** which couldn't be handled by the script.
  6186. **
  6187. **-----------------------------------------------
  6188. */
  6189. PRINT_ADDR(cp->cmd, "IGNORE_WIDE_RESIDUE received, but not yet "
  6190. "implemented.\n");
  6191. break;
  6192. #if 0
  6193. case SIR_MISSING_SAVE:
  6194. /*-----------------------------------------------
  6195. **
  6196. ** We received an DISCONNECT message,
  6197. ** but the datapointer wasn't saved before.
  6198. **
  6199. **-----------------------------------------------
  6200. */
  6201. PRINT_ADDR(cp->cmd, "DISCONNECT received, but datapointer "
  6202. "not saved: data=%x save=%x goal=%x.\n",
  6203. (unsigned) INL (nc_temp),
  6204. (unsigned) scr_to_cpu(np->header.savep),
  6205. (unsigned) scr_to_cpu(np->header.goalp));
  6206. break;
  6207. #endif
  6208. }
  6209. out:
  6210. OUTONB_STD ();
  6211. }
  6212. /*==========================================================
  6213. **
  6214. **
  6215. ** Acquire a control block
  6216. **
  6217. **
  6218. **==========================================================
  6219. */
  6220. static struct ccb *ncr_get_ccb(struct ncb *np, struct scsi_cmnd *cmd)
  6221. {
  6222. u_char tn = cmd->device->id;
  6223. u_char ln = cmd->device->lun;
  6224. struct tcb *tp = &np->target[tn];
  6225. struct lcb *lp = tp->lp[ln];
  6226. u_char tag = NO_TAG;
  6227. struct ccb *cp = NULL;
  6228. /*
  6229. ** Lun structure available ?
  6230. */
  6231. if (lp) {
  6232. struct list_head *qp;
  6233. /*
  6234. ** Keep from using more tags than we can handle.
  6235. */
  6236. if (lp->usetags && lp->busyccbs >= lp->maxnxs)
  6237. return NULL;
  6238. /*
  6239. ** Allocate a new CCB if needed.
  6240. */
  6241. if (list_empty(&lp->free_ccbq))
  6242. ncr_alloc_ccb(np, tn, ln);
  6243. /*
  6244. ** Look for free CCB
  6245. */
  6246. qp = ncr_list_pop(&lp->free_ccbq);
  6247. if (qp) {
  6248. cp = list_entry(qp, struct ccb, link_ccbq);
  6249. if (cp->magic) {
  6250. PRINT_ADDR(cmd, "ccb free list corrupted "
  6251. "(@%p)\n", cp);
  6252. cp = NULL;
  6253. } else {
  6254. list_add_tail(qp, &lp->wait_ccbq);
  6255. ++lp->busyccbs;
  6256. }
  6257. }
  6258. /*
  6259. ** If a CCB is available,
  6260. ** Get a tag for this nexus if required.
  6261. */
  6262. if (cp) {
  6263. if (lp->usetags)
  6264. tag = lp->cb_tags[lp->ia_tag];
  6265. }
  6266. else if (lp->actccbs > 0)
  6267. return NULL;
  6268. }
  6269. /*
  6270. ** if nothing available, take the default.
  6271. */
  6272. if (!cp)
  6273. cp = np->ccb;
  6274. /*
  6275. ** Wait until available.
  6276. */
  6277. #if 0
  6278. while (cp->magic) {
  6279. if (flags & SCSI_NOSLEEP) break;
  6280. if (tsleep ((caddr_t)cp, PRIBIO|PCATCH, "ncr", 0))
  6281. break;
  6282. }
  6283. #endif
  6284. if (cp->magic)
  6285. return NULL;
  6286. cp->magic = 1;
  6287. /*
  6288. ** Move to next available tag if tag used.
  6289. */
  6290. if (lp) {
  6291. if (tag != NO_TAG) {
  6292. ++lp->ia_tag;
  6293. if (lp->ia_tag == MAX_TAGS)
  6294. lp->ia_tag = 0;
  6295. lp->tags_umap |= (((tagmap_t) 1) << tag);
  6296. }
  6297. }
  6298. /*
  6299. ** Remember all informations needed to free this CCB.
  6300. */
  6301. cp->tag = tag;
  6302. cp->target = tn;
  6303. cp->lun = ln;
  6304. if (DEBUG_FLAGS & DEBUG_TAGS) {
  6305. PRINT_ADDR(cmd, "ccb @%p using tag %d.\n", cp, tag);
  6306. }
  6307. return cp;
  6308. }
  6309. /*==========================================================
  6310. **
  6311. **
  6312. ** Release one control block
  6313. **
  6314. **
  6315. **==========================================================
  6316. */
  6317. static void ncr_free_ccb (struct ncb *np, struct ccb *cp)
  6318. {
  6319. struct tcb *tp = &np->target[cp->target];
  6320. struct lcb *lp = tp->lp[cp->lun];
  6321. if (DEBUG_FLAGS & DEBUG_TAGS) {
  6322. PRINT_ADDR(cp->cmd, "ccb @%p freeing tag %d.\n", cp, cp->tag);
  6323. }
  6324. /*
  6325. ** If lun control block available,
  6326. ** decrement active commands and increment credit,
  6327. ** free the tag if any and remove the JUMP for reselect.
  6328. */
  6329. if (lp) {
  6330. if (cp->tag != NO_TAG) {
  6331. lp->cb_tags[lp->if_tag++] = cp->tag;
  6332. if (lp->if_tag == MAX_TAGS)
  6333. lp->if_tag = 0;
  6334. lp->tags_umap &= ~(((tagmap_t) 1) << cp->tag);
  6335. lp->tags_smap &= lp->tags_umap;
  6336. lp->jump_ccb[cp->tag] =
  6337. cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l_q));
  6338. } else {
  6339. lp->jump_ccb[0] =
  6340. cpu_to_scr(NCB_SCRIPTH_PHYS(np, bad_i_t_l));
  6341. }
  6342. }
  6343. /*
  6344. ** Make this CCB available.
  6345. */
  6346. if (lp) {
  6347. if (cp != np->ccb)
  6348. list_move(&cp->link_ccbq, &lp->free_ccbq);
  6349. --lp->busyccbs;
  6350. if (cp->queued) {
  6351. --lp->queuedccbs;
  6352. }
  6353. }
  6354. cp -> host_status = HS_IDLE;
  6355. cp -> magic = 0;
  6356. if (cp->queued) {
  6357. --np->queuedccbs;
  6358. cp->queued = 0;
  6359. }
  6360. #if 0
  6361. if (cp == np->ccb)
  6362. wakeup ((caddr_t) cp);
  6363. #endif
  6364. }
  6365. #define ncr_reg_bus_addr(r) (np->paddr + offsetof (struct ncr_reg, r))
  6366. /*------------------------------------------------------------------------
  6367. ** Initialize the fixed part of a CCB structure.
  6368. **------------------------------------------------------------------------
  6369. **------------------------------------------------------------------------
  6370. */
  6371. static void ncr_init_ccb(struct ncb *np, struct ccb *cp)
  6372. {
  6373. ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
  6374. /*
  6375. ** Remember virtual and bus address of this ccb.
  6376. */
  6377. cp->p_ccb = vtobus(cp);
  6378. cp->phys.header.cp = cp;
  6379. /*
  6380. ** This allows list_del to work for the default ccb.
  6381. */
  6382. INIT_LIST_HEAD(&cp->link_ccbq);
  6383. /*
  6384. ** Initialyze the start and restart launch script.
  6385. **
  6386. ** COPY(4) @(...p_phys), @(dsa)
  6387. ** JUMP @(sched_point)
  6388. */
  6389. cp->start.setup_dsa[0] = cpu_to_scr(copy_4);
  6390. cp->start.setup_dsa[1] = cpu_to_scr(CCB_PHYS(cp, start.p_phys));
  6391. cp->start.setup_dsa[2] = cpu_to_scr(ncr_reg_bus_addr(nc_dsa));
  6392. cp->start.schedule.l_cmd = cpu_to_scr(SCR_JUMP);
  6393. cp->start.p_phys = cpu_to_scr(CCB_PHYS(cp, phys));
  6394. memcpy(&cp->restart, &cp->start, sizeof(cp->restart));
  6395. cp->start.schedule.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
  6396. cp->restart.schedule.l_paddr = cpu_to_scr(NCB_SCRIPTH_PHYS (np, abort));
  6397. }
  6398. /*------------------------------------------------------------------------
  6399. ** Allocate a CCB and initialize its fixed part.
  6400. **------------------------------------------------------------------------
  6401. **------------------------------------------------------------------------
  6402. */
  6403. static void ncr_alloc_ccb(struct ncb *np, u_char tn, u_char ln)
  6404. {
  6405. struct tcb *tp = &np->target[tn];
  6406. struct lcb *lp = tp->lp[ln];
  6407. struct ccb *cp = NULL;
  6408. /*
  6409. ** Allocate memory for this CCB.
  6410. */
  6411. cp = m_calloc_dma(sizeof(struct ccb), "CCB");
  6412. if (!cp)
  6413. return;
  6414. /*
  6415. ** Count it and initialyze it.
  6416. */
  6417. lp->actccbs++;
  6418. np->actccbs++;
  6419. memset(cp, 0, sizeof (*cp));
  6420. ncr_init_ccb(np, cp);
  6421. /*
  6422. ** Chain into wakeup list and free ccb queue and take it
  6423. ** into account for tagged commands.
  6424. */
  6425. cp->link_ccb = np->ccb->link_ccb;
  6426. np->ccb->link_ccb = cp;
  6427. list_add(&cp->link_ccbq, &lp->free_ccbq);
  6428. }
  6429. /*==========================================================
  6430. **
  6431. **
  6432. ** Allocation of resources for Targets/Luns/Tags.
  6433. **
  6434. **
  6435. **==========================================================
  6436. */
  6437. /*------------------------------------------------------------------------
  6438. ** Target control block initialisation.
  6439. **------------------------------------------------------------------------
  6440. ** This data structure is fully initialized after a SCSI command
  6441. ** has been successfully completed for this target.
  6442. ** It contains a SCRIPT that is called on target reselection.
  6443. **------------------------------------------------------------------------
  6444. */
  6445. static void ncr_init_tcb (struct ncb *np, u_char tn)
  6446. {
  6447. struct tcb *tp = &np->target[tn];
  6448. ncrcmd copy_1 = np->features & FE_PFEN ? SCR_COPY(1) : SCR_COPY_F(1);
  6449. int th = tn & 3;
  6450. int i;
  6451. /*
  6452. ** Jump to next tcb if SFBR does not match this target.
  6453. ** JUMP IF (SFBR != #target#), @(next tcb)
  6454. */
  6455. tp->jump_tcb.l_cmd =
  6456. cpu_to_scr((SCR_JUMP ^ IFFALSE (DATA (0x80 + tn))));
  6457. tp->jump_tcb.l_paddr = np->jump_tcb[th].l_paddr;
  6458. /*
  6459. ** Load the synchronous transfer register.
  6460. ** COPY @(tp->sval), @(sxfer)
  6461. */
  6462. tp->getscr[0] = cpu_to_scr(copy_1);
  6463. tp->getscr[1] = cpu_to_scr(vtobus (&tp->sval));
  6464. #ifdef SCSI_NCR_BIG_ENDIAN
  6465. tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer) ^ 3);
  6466. #else
  6467. tp->getscr[2] = cpu_to_scr(ncr_reg_bus_addr(nc_sxfer));
  6468. #endif
  6469. /*
  6470. ** Load the timing register.
  6471. ** COPY @(tp->wval), @(scntl3)
  6472. */
  6473. tp->getscr[3] = cpu_to_scr(copy_1);
  6474. tp->getscr[4] = cpu_to_scr(vtobus (&tp->wval));
  6475. #ifdef SCSI_NCR_BIG_ENDIAN
  6476. tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3) ^ 3);
  6477. #else
  6478. tp->getscr[5] = cpu_to_scr(ncr_reg_bus_addr(nc_scntl3));
  6479. #endif
  6480. /*
  6481. ** Get the IDENTIFY message and the lun.
  6482. ** CALL @script(resel_lun)
  6483. */
  6484. tp->call_lun.l_cmd = cpu_to_scr(SCR_CALL);
  6485. tp->call_lun.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_lun));
  6486. /*
  6487. ** Look for the lun control block of this nexus.
  6488. ** For i = 0 to 3
  6489. ** JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
  6490. */
  6491. for (i = 0 ; i < 4 ; i++) {
  6492. tp->jump_lcb[i].l_cmd =
  6493. cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
  6494. tp->jump_lcb[i].l_paddr =
  6495. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_identify));
  6496. }
  6497. /*
  6498. ** Link this target control block to the JUMP chain.
  6499. */
  6500. np->jump_tcb[th].l_paddr = cpu_to_scr(vtobus (&tp->jump_tcb));
  6501. /*
  6502. ** These assert's should be moved at driver initialisations.
  6503. */
  6504. #ifdef SCSI_NCR_BIG_ENDIAN
  6505. BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
  6506. offsetof(struct tcb , sval )) &3) != 3);
  6507. BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
  6508. offsetof(struct tcb , wval )) &3) != 3);
  6509. #else
  6510. BUG_ON(((offsetof(struct ncr_reg, nc_sxfer) ^
  6511. offsetof(struct tcb , sval )) &3) != 0);
  6512. BUG_ON(((offsetof(struct ncr_reg, nc_scntl3) ^
  6513. offsetof(struct tcb , wval )) &3) != 0);
  6514. #endif
  6515. }
  6516. /*------------------------------------------------------------------------
  6517. ** Lun control block allocation and initialization.
  6518. **------------------------------------------------------------------------
  6519. ** This data structure is allocated and initialized after a SCSI
  6520. ** command has been successfully completed for this target/lun.
  6521. **------------------------------------------------------------------------
  6522. */
  6523. static struct lcb *ncr_alloc_lcb (struct ncb *np, u_char tn, u_char ln)
  6524. {
  6525. struct tcb *tp = &np->target[tn];
  6526. struct lcb *lp = tp->lp[ln];
  6527. ncrcmd copy_4 = np->features & FE_PFEN ? SCR_COPY(4) : SCR_COPY_F(4);
  6528. int lh = ln & 3;
  6529. /*
  6530. ** Already done, return.
  6531. */
  6532. if (lp)
  6533. return lp;
  6534. /*
  6535. ** Allocate the lcb.
  6536. */
  6537. lp = m_calloc_dma(sizeof(struct lcb), "LCB");
  6538. if (!lp)
  6539. goto fail;
  6540. memset(lp, 0, sizeof(*lp));
  6541. tp->lp[ln] = lp;
  6542. /*
  6543. ** Initialize the target control block if not yet.
  6544. */
  6545. if (!tp->jump_tcb.l_cmd)
  6546. ncr_init_tcb(np, tn);
  6547. /*
  6548. ** Initialize the CCB queue headers.
  6549. */
  6550. INIT_LIST_HEAD(&lp->free_ccbq);
  6551. INIT_LIST_HEAD(&lp->busy_ccbq);
  6552. INIT_LIST_HEAD(&lp->wait_ccbq);
  6553. INIT_LIST_HEAD(&lp->skip_ccbq);
  6554. /*
  6555. ** Set max CCBs to 1 and use the default 1 entry
  6556. ** jump table by default.
  6557. */
  6558. lp->maxnxs = 1;
  6559. lp->jump_ccb = &lp->jump_ccb_0;
  6560. lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
  6561. /*
  6562. ** Initilialyze the reselect script:
  6563. **
  6564. ** Jump to next lcb if SFBR does not match this lun.
  6565. ** Load TEMP with the CCB direct jump table bus address.
  6566. ** Get the SIMPLE TAG message and the tag.
  6567. **
  6568. ** JUMP IF (SFBR != #lun#), @(next lcb)
  6569. ** COPY @(lp->p_jump_ccb), @(temp)
  6570. ** JUMP @script(resel_notag)
  6571. */
  6572. lp->jump_lcb.l_cmd =
  6573. cpu_to_scr((SCR_JUMP ^ IFFALSE (MASK (0x80+ln, 0xff))));
  6574. lp->jump_lcb.l_paddr = tp->jump_lcb[lh].l_paddr;
  6575. lp->load_jump_ccb[0] = cpu_to_scr(copy_4);
  6576. lp->load_jump_ccb[1] = cpu_to_scr(vtobus (&lp->p_jump_ccb));
  6577. lp->load_jump_ccb[2] = cpu_to_scr(ncr_reg_bus_addr(nc_temp));
  6578. lp->jump_tag.l_cmd = cpu_to_scr(SCR_JUMP);
  6579. lp->jump_tag.l_paddr = cpu_to_scr(NCB_SCRIPT_PHYS (np, resel_notag));
  6580. /*
  6581. ** Link this lun control block to the JUMP chain.
  6582. */
  6583. tp->jump_lcb[lh].l_paddr = cpu_to_scr(vtobus (&lp->jump_lcb));
  6584. /*
  6585. ** Initialize command queuing control.
  6586. */
  6587. lp->busyccbs = 1;
  6588. lp->queuedccbs = 1;
  6589. lp->queuedepth = 1;
  6590. fail:
  6591. return lp;
  6592. }
  6593. /*------------------------------------------------------------------------
  6594. ** Lun control block setup on INQUIRY data received.
  6595. **------------------------------------------------------------------------
  6596. ** We only support WIDE, SYNC for targets and CMDQ for logical units.
  6597. ** This setup is done on each INQUIRY since we are expecting user
  6598. ** will play with CHANGE DEFINITION commands. :-)
  6599. **------------------------------------------------------------------------
  6600. */
  6601. static struct lcb *ncr_setup_lcb (struct ncb *np, struct scsi_device *sdev)
  6602. {
  6603. unsigned char tn = sdev->id, ln = sdev->lun;
  6604. struct tcb *tp = &np->target[tn];
  6605. struct lcb *lp = tp->lp[ln];
  6606. /* If no lcb, try to allocate it. */
  6607. if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
  6608. goto fail;
  6609. /*
  6610. ** If unit supports tagged commands, allocate the
  6611. ** CCB JUMP table if not yet.
  6612. */
  6613. if (sdev->tagged_supported && lp->jump_ccb == &lp->jump_ccb_0) {
  6614. int i;
  6615. lp->jump_ccb = m_calloc_dma(256, "JUMP_CCB");
  6616. if (!lp->jump_ccb) {
  6617. lp->jump_ccb = &lp->jump_ccb_0;
  6618. goto fail;
  6619. }
  6620. lp->p_jump_ccb = cpu_to_scr(vtobus(lp->jump_ccb));
  6621. for (i = 0 ; i < 64 ; i++)
  6622. lp->jump_ccb[i] =
  6623. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l_q));
  6624. for (i = 0 ; i < MAX_TAGS ; i++)
  6625. lp->cb_tags[i] = i;
  6626. lp->maxnxs = MAX_TAGS;
  6627. lp->tags_stime = jiffies + 3*HZ;
  6628. ncr_setup_tags (np, sdev);
  6629. }
  6630. fail:
  6631. return lp;
  6632. }
  6633. /*==========================================================
  6634. **
  6635. **
  6636. ** Build Scatter Gather Block
  6637. **
  6638. **
  6639. **==========================================================
  6640. **
  6641. ** The transfer area may be scattered among
  6642. ** several non adjacent physical pages.
  6643. **
  6644. ** We may use MAX_SCATTER blocks.
  6645. **
  6646. **----------------------------------------------------------
  6647. */
  6648. /*
  6649. ** We try to reduce the number of interrupts caused
  6650. ** by unexpected phase changes due to disconnects.
  6651. ** A typical harddisk may disconnect before ANY block.
  6652. ** If we wanted to avoid unexpected phase changes at all
  6653. ** we had to use a break point every 512 bytes.
  6654. ** Of course the number of scatter/gather blocks is
  6655. ** limited.
  6656. ** Under Linux, the scatter/gatter blocks are provided by
  6657. ** the generic driver. We just have to copy addresses and
  6658. ** sizes to the data segment array.
  6659. */
  6660. static int ncr_scatter(struct ncb *np, struct ccb *cp, struct scsi_cmnd *cmd)
  6661. {
  6662. int segment = 0;
  6663. int use_sg = scsi_sg_count(cmd);
  6664. cp->data_len = 0;
  6665. use_sg = map_scsi_sg_data(np, cmd);
  6666. if (use_sg > 0) {
  6667. struct scatterlist *sg;
  6668. struct scr_tblmove *data;
  6669. if (use_sg > MAX_SCATTER) {
  6670. unmap_scsi_data(np, cmd);
  6671. return -1;
  6672. }
  6673. data = &cp->phys.data[MAX_SCATTER - use_sg];
  6674. scsi_for_each_sg(cmd, sg, use_sg, segment) {
  6675. dma_addr_t baddr = sg_dma_address(sg);
  6676. unsigned int len = sg_dma_len(sg);
  6677. ncr_build_sge(np, &data[segment], baddr, len);
  6678. cp->data_len += len;
  6679. }
  6680. } else
  6681. segment = -2;
  6682. return segment;
  6683. }
  6684. /*==========================================================
  6685. **
  6686. **
  6687. ** Test the bus snoop logic :-(
  6688. **
  6689. ** Has to be called with interrupts disabled.
  6690. **
  6691. **
  6692. **==========================================================
  6693. */
  6694. static int __init ncr_regtest (struct ncb* np)
  6695. {
  6696. register volatile u32 data;
  6697. /*
  6698. ** ncr registers may NOT be cached.
  6699. ** write 0xffffffff to a read only register area,
  6700. ** and try to read it back.
  6701. */
  6702. data = 0xffffffff;
  6703. OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
  6704. data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
  6705. #if 1
  6706. if (data == 0xffffffff) {
  6707. #else
  6708. if ((data & 0xe2f0fffd) != 0x02000080) {
  6709. #endif
  6710. printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  6711. (unsigned) data);
  6712. return (0x10);
  6713. }
  6714. return (0);
  6715. }
  6716. static int __init ncr_snooptest (struct ncb* np)
  6717. {
  6718. u32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
  6719. int i, err=0;
  6720. if (np->reg) {
  6721. err |= ncr_regtest (np);
  6722. if (err)
  6723. return (err);
  6724. }
  6725. /* init */
  6726. pc = NCB_SCRIPTH_PHYS (np, snooptest);
  6727. host_wr = 1;
  6728. ncr_wr = 2;
  6729. /*
  6730. ** Set memory and register.
  6731. */
  6732. np->ncr_cache = cpu_to_scr(host_wr);
  6733. OUTL (nc_temp, ncr_wr);
  6734. /*
  6735. ** Start script (exchange values)
  6736. */
  6737. OUTL_DSP (pc);
  6738. /*
  6739. ** Wait 'til done (with timeout)
  6740. */
  6741. for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
  6742. if (INB(nc_istat) & (INTF|SIP|DIP))
  6743. break;
  6744. /*
  6745. ** Save termination position.
  6746. */
  6747. pc = INL (nc_dsp);
  6748. /*
  6749. ** Read memory and register.
  6750. */
  6751. host_rd = scr_to_cpu(np->ncr_cache);
  6752. ncr_rd = INL (nc_scratcha);
  6753. ncr_bk = INL (nc_temp);
  6754. /*
  6755. ** Reset ncr chip
  6756. */
  6757. ncr_chip_reset(np, 100);
  6758. /*
  6759. ** check for timeout
  6760. */
  6761. if (i>=NCR_SNOOP_TIMEOUT) {
  6762. printk ("CACHE TEST FAILED: timeout.\n");
  6763. return (0x20);
  6764. }
  6765. /*
  6766. ** Check termination position.
  6767. */
  6768. if (pc != NCB_SCRIPTH_PHYS (np, snoopend)+8) {
  6769. printk ("CACHE TEST FAILED: script execution failed.\n");
  6770. printk ("start=%08lx, pc=%08lx, end=%08lx\n",
  6771. (u_long) NCB_SCRIPTH_PHYS (np, snooptest), (u_long) pc,
  6772. (u_long) NCB_SCRIPTH_PHYS (np, snoopend) +8);
  6773. return (0x40);
  6774. }
  6775. /*
  6776. ** Show results.
  6777. */
  6778. if (host_wr != ncr_rd) {
  6779. printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
  6780. (int) host_wr, (int) ncr_rd);
  6781. err |= 1;
  6782. }
  6783. if (host_rd != ncr_wr) {
  6784. printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
  6785. (int) ncr_wr, (int) host_rd);
  6786. err |= 2;
  6787. }
  6788. if (ncr_bk != ncr_wr) {
  6789. printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
  6790. (int) ncr_wr, (int) ncr_bk);
  6791. err |= 4;
  6792. }
  6793. return (err);
  6794. }
  6795. /*==========================================================
  6796. **
  6797. ** Determine the ncr's clock frequency.
  6798. ** This is essential for the negotiation
  6799. ** of the synchronous transfer rate.
  6800. **
  6801. **==========================================================
  6802. **
  6803. ** Note: we have to return the correct value.
  6804. ** THERE IS NO SAFE DEFAULT VALUE.
  6805. **
  6806. ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
  6807. ** 53C860 and 53C875 rev. 1 support fast20 transfers but
  6808. ** do not have a clock doubler and so are provided with a
  6809. ** 80 MHz clock. All other fast20 boards incorporate a doubler
  6810. ** and so should be delivered with a 40 MHz clock.
  6811. ** The future fast40 chips (895/895) use a 40 Mhz base clock
  6812. ** and provide a clock quadrupler (160 Mhz). The code below
  6813. ** tries to deal as cleverly as possible with all this stuff.
  6814. **
  6815. **----------------------------------------------------------
  6816. */
  6817. /*
  6818. * Select NCR SCSI clock frequency
  6819. */
  6820. static void ncr_selectclock(struct ncb *np, u_char scntl3)
  6821. {
  6822. if (np->multiplier < 2) {
  6823. OUTB(nc_scntl3, scntl3);
  6824. return;
  6825. }
  6826. if (bootverbose >= 2)
  6827. printk ("%s: enabling clock multiplier\n", ncr_name(np));
  6828. OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
  6829. if (np->multiplier > 2) { /* Poll bit 5 of stest4 for quadrupler */
  6830. int i = 20;
  6831. while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
  6832. udelay(20);
  6833. if (!i)
  6834. printk("%s: the chip cannot lock the frequency\n", ncr_name(np));
  6835. } else /* Wait 20 micro-seconds for doubler */
  6836. udelay(20);
  6837. OUTB(nc_stest3, HSC); /* Halt the scsi clock */
  6838. OUTB(nc_scntl3, scntl3);
  6839. OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
  6840. OUTB(nc_stest3, 0x00); /* Restart scsi clock */
  6841. }
  6842. /*
  6843. * calculate NCR SCSI clock frequency (in KHz)
  6844. */
  6845. static unsigned __init ncrgetfreq (struct ncb *np, int gen)
  6846. {
  6847. unsigned ms = 0;
  6848. char count = 0;
  6849. /*
  6850. * Measure GEN timer delay in order
  6851. * to calculate SCSI clock frequency
  6852. *
  6853. * This code will never execute too
  6854. * many loop iterations (if DELAY is
  6855. * reasonably correct). It could get
  6856. * too low a delay (too high a freq.)
  6857. * if the CPU is slow executing the
  6858. * loop for some reason (an NMI, for
  6859. * example). For this reason we will
  6860. * if multiple measurements are to be
  6861. * performed trust the higher delay
  6862. * (lower frequency returned).
  6863. */
  6864. OUTB (nc_stest1, 0); /* make sure clock doubler is OFF */
  6865. OUTW (nc_sien , 0); /* mask all scsi interrupts */
  6866. (void) INW (nc_sist); /* clear pending scsi interrupt */
  6867. OUTB (nc_dien , 0); /* mask all dma interrupts */
  6868. (void) INW (nc_sist); /* another one, just to be sure :) */
  6869. OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
  6870. OUTB (nc_stime1, 0); /* disable general purpose timer */
  6871. OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
  6872. while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
  6873. for (count = 0; count < 10; count ++)
  6874. udelay(100); /* count ms */
  6875. }
  6876. OUTB (nc_stime1, 0); /* disable general purpose timer */
  6877. /*
  6878. * set prescaler to divide by whatever 0 means
  6879. * 0 ought to choose divide by 2, but appears
  6880. * to set divide by 3.5 mode in my 53c810 ...
  6881. */
  6882. OUTB (nc_scntl3, 0);
  6883. if (bootverbose >= 2)
  6884. printk ("%s: Delay (GEN=%d): %u msec\n", ncr_name(np), gen, ms);
  6885. /*
  6886. * adjust for prescaler, and convert into KHz
  6887. */
  6888. return ms ? ((1 << gen) * 4340) / ms : 0;
  6889. }
  6890. /*
  6891. * Get/probe NCR SCSI clock frequency
  6892. */
  6893. static void __init ncr_getclock (struct ncb *np, int mult)
  6894. {
  6895. unsigned char scntl3 = INB(nc_scntl3);
  6896. unsigned char stest1 = INB(nc_stest1);
  6897. unsigned f1;
  6898. np->multiplier = 1;
  6899. f1 = 40000;
  6900. /*
  6901. ** True with 875 or 895 with clock multiplier selected
  6902. */
  6903. if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
  6904. if (bootverbose >= 2)
  6905. printk ("%s: clock multiplier found\n", ncr_name(np));
  6906. np->multiplier = mult;
  6907. }
  6908. /*
  6909. ** If multiplier not found or scntl3 not 7,5,3,
  6910. ** reset chip and get frequency from general purpose timer.
  6911. ** Otherwise trust scntl3 BIOS setting.
  6912. */
  6913. if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
  6914. unsigned f2;
  6915. ncr_chip_reset(np, 5);
  6916. (void) ncrgetfreq (np, 11); /* throw away first result */
  6917. f1 = ncrgetfreq (np, 11);
  6918. f2 = ncrgetfreq (np, 11);
  6919. if(bootverbose)
  6920. printk ("%s: NCR clock is %uKHz, %uKHz\n", ncr_name(np), f1, f2);
  6921. if (f1 > f2) f1 = f2; /* trust lower result */
  6922. if (f1 < 45000) f1 = 40000;
  6923. else if (f1 < 55000) f1 = 50000;
  6924. else f1 = 80000;
  6925. if (f1 < 80000 && mult > 1) {
  6926. if (bootverbose >= 2)
  6927. printk ("%s: clock multiplier assumed\n", ncr_name(np));
  6928. np->multiplier = mult;
  6929. }
  6930. } else {
  6931. if ((scntl3 & 7) == 3) f1 = 40000;
  6932. else if ((scntl3 & 7) == 5) f1 = 80000;
  6933. else f1 = 160000;
  6934. f1 /= np->multiplier;
  6935. }
  6936. /*
  6937. ** Compute controller synchronous parameters.
  6938. */
  6939. f1 *= np->multiplier;
  6940. np->clock_khz = f1;
  6941. }
  6942. /*===================== LINUX ENTRY POINTS SECTION ==========================*/
  6943. static int ncr53c8xx_slave_alloc(struct scsi_device *device)
  6944. {
  6945. struct Scsi_Host *host = device->host;
  6946. struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
  6947. struct tcb *tp = &np->target[device->id];
  6948. tp->starget = device->sdev_target;
  6949. return 0;
  6950. }
  6951. static int ncr53c8xx_slave_configure(struct scsi_device *device)
  6952. {
  6953. struct Scsi_Host *host = device->host;
  6954. struct ncb *np = ((struct host_data *) host->hostdata)->ncb;
  6955. struct tcb *tp = &np->target[device->id];
  6956. struct lcb *lp = tp->lp[device->lun];
  6957. int numtags, depth_to_use;
  6958. ncr_setup_lcb(np, device);
  6959. /*
  6960. ** Select queue depth from driver setup.
  6961. ** Donnot use more than configured by user.
  6962. ** Use at least 2.
  6963. ** Donnot use more than our maximum.
  6964. */
  6965. numtags = device_queue_depth(np->unit, device->id, device->lun);
  6966. if (numtags > tp->usrtags)
  6967. numtags = tp->usrtags;
  6968. if (!device->tagged_supported)
  6969. numtags = 1;
  6970. depth_to_use = numtags;
  6971. if (depth_to_use < 2)
  6972. depth_to_use = 2;
  6973. if (depth_to_use > MAX_TAGS)
  6974. depth_to_use = MAX_TAGS;
  6975. scsi_change_queue_depth(device, depth_to_use);
  6976. /*
  6977. ** Since the queue depth is not tunable under Linux,
  6978. ** we need to know this value in order not to
  6979. ** announce stupid things to user.
  6980. **
  6981. ** XXX(hch): As of Linux 2.6 it certainly _is_ tunable..
  6982. ** In fact we just tuned it, or did I miss
  6983. ** something important? :)
  6984. */
  6985. if (lp) {
  6986. lp->numtags = lp->maxtags = numtags;
  6987. lp->scdev_depth = depth_to_use;
  6988. }
  6989. ncr_setup_tags (np, device);
  6990. #ifdef DEBUG_NCR53C8XX
  6991. printk("ncr53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
  6992. np->unit, device->id, device->lun, depth_to_use);
  6993. #endif
  6994. if (spi_support_sync(device->sdev_target) &&
  6995. !spi_initial_dv(device->sdev_target))
  6996. spi_dv_device(device);
  6997. return 0;
  6998. }
  6999. static int ncr53c8xx_queue_command_lck(struct scsi_cmnd *cmd)
  7000. {
  7001. struct ncr_cmd_priv *cmd_priv = scsi_cmd_priv(cmd);
  7002. void (*done)(struct scsi_cmnd *) = scsi_done;
  7003. struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
  7004. unsigned long flags;
  7005. int sts;
  7006. #ifdef DEBUG_NCR53C8XX
  7007. printk("ncr53c8xx_queue_command\n");
  7008. #endif
  7009. cmd->host_scribble = NULL;
  7010. cmd_priv->data_mapped = 0;
  7011. cmd_priv->data_mapping = 0;
  7012. spin_lock_irqsave(&np->smp_lock, flags);
  7013. if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
  7014. set_host_byte(cmd, sts);
  7015. #ifdef DEBUG_NCR53C8XX
  7016. printk("ncr53c8xx : command not queued - result=%d\n", sts);
  7017. #endif
  7018. }
  7019. #ifdef DEBUG_NCR53C8XX
  7020. else
  7021. printk("ncr53c8xx : command successfully queued\n");
  7022. #endif
  7023. spin_unlock_irqrestore(&np->smp_lock, flags);
  7024. if (sts != DID_OK) {
  7025. unmap_scsi_data(np, cmd);
  7026. done(cmd);
  7027. sts = 0;
  7028. }
  7029. return sts;
  7030. }
  7031. static DEF_SCSI_QCMD(ncr53c8xx_queue_command)
  7032. irqreturn_t ncr53c8xx_intr(int irq, void *dev_id)
  7033. {
  7034. unsigned long flags;
  7035. struct Scsi_Host *shost = (struct Scsi_Host *)dev_id;
  7036. struct host_data *host_data = (struct host_data *)shost->hostdata;
  7037. struct ncb *np = host_data->ncb;
  7038. struct scsi_cmnd *done_list;
  7039. #ifdef DEBUG_NCR53C8XX
  7040. printk("ncr53c8xx : interrupt received\n");
  7041. #endif
  7042. if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
  7043. spin_lock_irqsave(&np->smp_lock, flags);
  7044. ncr_exception(np);
  7045. done_list = np->done_list;
  7046. np->done_list = NULL;
  7047. spin_unlock_irqrestore(&np->smp_lock, flags);
  7048. if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
  7049. if (done_list)
  7050. ncr_flush_done_cmds(done_list);
  7051. return IRQ_HANDLED;
  7052. }
  7053. static void ncr53c8xx_timeout(struct timer_list *t)
  7054. {
  7055. struct ncb *np = from_timer(np, t, timer);
  7056. unsigned long flags;
  7057. struct scsi_cmnd *done_list;
  7058. spin_lock_irqsave(&np->smp_lock, flags);
  7059. ncr_timeout(np);
  7060. done_list = np->done_list;
  7061. np->done_list = NULL;
  7062. spin_unlock_irqrestore(&np->smp_lock, flags);
  7063. if (done_list)
  7064. ncr_flush_done_cmds(done_list);
  7065. }
  7066. static int ncr53c8xx_bus_reset(struct scsi_cmnd *cmd)
  7067. {
  7068. struct ncb *np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
  7069. int sts;
  7070. unsigned long flags;
  7071. struct scsi_cmnd *done_list;
  7072. /*
  7073. * If the mid-level driver told us reset is synchronous, it seems
  7074. * that we must call the done() callback for the involved command,
  7075. * even if this command was not queued to the low-level driver,
  7076. * before returning SUCCESS.
  7077. */
  7078. spin_lock_irqsave(&np->smp_lock, flags);
  7079. sts = ncr_reset_bus(np);
  7080. done_list = np->done_list;
  7081. np->done_list = NULL;
  7082. spin_unlock_irqrestore(&np->smp_lock, flags);
  7083. ncr_flush_done_cmds(done_list);
  7084. return sts;
  7085. }
  7086. /*
  7087. ** Scsi command waiting list management.
  7088. **
  7089. ** It may happen that we cannot insert a scsi command into the start queue,
  7090. ** in the following circumstances.
  7091. ** Too few preallocated ccb(s),
  7092. ** maxtags < cmd_per_lun of the Linux host control block,
  7093. ** etc...
  7094. ** Such scsi commands are inserted into a waiting list.
  7095. ** When a scsi command complete, we try to requeue the commands of the
  7096. ** waiting list.
  7097. */
  7098. #define next_wcmd host_scribble
  7099. static void insert_into_waiting_list(struct ncb *np, struct scsi_cmnd *cmd)
  7100. {
  7101. struct scsi_cmnd *wcmd;
  7102. #ifdef DEBUG_WAITING_LIST
  7103. printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
  7104. #endif
  7105. cmd->next_wcmd = NULL;
  7106. if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
  7107. else {
  7108. while (wcmd->next_wcmd)
  7109. wcmd = (struct scsi_cmnd *) wcmd->next_wcmd;
  7110. wcmd->next_wcmd = (char *) cmd;
  7111. }
  7112. }
  7113. static void process_waiting_list(struct ncb *np, int sts)
  7114. {
  7115. struct scsi_cmnd *waiting_list, *wcmd;
  7116. waiting_list = np->waiting_list;
  7117. np->waiting_list = NULL;
  7118. #ifdef DEBUG_WAITING_LIST
  7119. if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
  7120. #endif
  7121. while ((wcmd = waiting_list) != NULL) {
  7122. waiting_list = (struct scsi_cmnd *) wcmd->next_wcmd;
  7123. wcmd->next_wcmd = NULL;
  7124. if (sts == DID_OK) {
  7125. #ifdef DEBUG_WAITING_LIST
  7126. printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
  7127. #endif
  7128. sts = ncr_queue_command(np, wcmd);
  7129. }
  7130. if (sts != DID_OK) {
  7131. #ifdef DEBUG_WAITING_LIST
  7132. printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
  7133. #endif
  7134. set_host_byte(wcmd, sts);
  7135. ncr_queue_done_cmd(np, wcmd);
  7136. }
  7137. }
  7138. }
  7139. #undef next_wcmd
  7140. static ssize_t show_ncr53c8xx_revision(struct device *dev,
  7141. struct device_attribute *attr, char *buf)
  7142. {
  7143. struct Scsi_Host *host = class_to_shost(dev);
  7144. struct host_data *host_data = (struct host_data *)host->hostdata;
  7145. return snprintf(buf, 20, "0x%x\n", host_data->ncb->revision_id);
  7146. }
  7147. static struct device_attribute ncr53c8xx_revision_attr = {
  7148. .attr = { .name = "revision", .mode = S_IRUGO, },
  7149. .show = show_ncr53c8xx_revision,
  7150. };
  7151. static struct attribute *ncr53c8xx_host_attrs[] = {
  7152. &ncr53c8xx_revision_attr.attr,
  7153. NULL
  7154. };
  7155. ATTRIBUTE_GROUPS(ncr53c8xx_host);
  7156. /*==========================================================
  7157. **
  7158. ** Boot command line.
  7159. **
  7160. **==========================================================
  7161. */
  7162. #ifdef MODULE
  7163. char *ncr53c8xx; /* command line passed by insmod */
  7164. module_param(ncr53c8xx, charp, 0);
  7165. #endif
  7166. #ifndef MODULE
  7167. static int __init ncr53c8xx_setup(char *str)
  7168. {
  7169. return sym53c8xx__setup(str);
  7170. }
  7171. __setup("ncr53c8xx=", ncr53c8xx_setup);
  7172. #endif
  7173. /*
  7174. * Host attach and initialisations.
  7175. *
  7176. * Allocate host data and ncb structure.
  7177. * Request IO region and remap MMIO region.
  7178. * Do chip initialization.
  7179. * If all is OK, install interrupt handling and
  7180. * start the timer daemon.
  7181. */
  7182. struct Scsi_Host * __init ncr_attach(struct scsi_host_template *tpnt,
  7183. int unit, struct ncr_device *device)
  7184. {
  7185. struct host_data *host_data;
  7186. struct ncb *np = NULL;
  7187. struct Scsi_Host *instance = NULL;
  7188. u_long flags = 0;
  7189. int i;
  7190. WARN_ON_ONCE(tpnt->cmd_size < sizeof(struct ncr_cmd_priv));
  7191. if (!tpnt->name)
  7192. tpnt->name = SCSI_NCR_DRIVER_NAME;
  7193. if (!tpnt->shost_groups)
  7194. tpnt->shost_groups = ncr53c8xx_host_groups;
  7195. tpnt->queuecommand = ncr53c8xx_queue_command;
  7196. tpnt->slave_configure = ncr53c8xx_slave_configure;
  7197. tpnt->slave_alloc = ncr53c8xx_slave_alloc;
  7198. tpnt->eh_bus_reset_handler = ncr53c8xx_bus_reset;
  7199. tpnt->can_queue = SCSI_NCR_CAN_QUEUE;
  7200. tpnt->this_id = 7;
  7201. tpnt->sg_tablesize = SCSI_NCR_SG_TABLESIZE;
  7202. tpnt->cmd_per_lun = SCSI_NCR_CMD_PER_LUN;
  7203. if (device->differential)
  7204. driver_setup.diff_support = device->differential;
  7205. printk(KERN_INFO "ncr53c720-%d: rev 0x%x irq %d\n",
  7206. unit, device->chip.revision_id, device->slot.irq);
  7207. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  7208. if (!instance)
  7209. goto attach_error;
  7210. host_data = (struct host_data *) instance->hostdata;
  7211. np = __m_calloc_dma(device->dev, sizeof(struct ncb), "NCB");
  7212. if (!np)
  7213. goto attach_error;
  7214. spin_lock_init(&np->smp_lock);
  7215. np->dev = device->dev;
  7216. np->p_ncb = vtobus(np);
  7217. host_data->ncb = np;
  7218. np->ccb = m_calloc_dma(sizeof(struct ccb), "CCB");
  7219. if (!np->ccb)
  7220. goto attach_error;
  7221. /* Store input information in the host data structure. */
  7222. np->unit = unit;
  7223. np->verbose = driver_setup.verbose;
  7224. sprintf(np->inst_name, "ncr53c720-%d", np->unit);
  7225. np->revision_id = device->chip.revision_id;
  7226. np->features = device->chip.features;
  7227. np->clock_divn = device->chip.nr_divisor;
  7228. np->maxoffs = device->chip.offset_max;
  7229. np->maxburst = device->chip.burst_max;
  7230. np->myaddr = device->host_id;
  7231. /* Allocate SCRIPTS areas. */
  7232. np->script0 = m_calloc_dma(sizeof(struct script), "SCRIPT");
  7233. if (!np->script0)
  7234. goto attach_error;
  7235. np->scripth0 = m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
  7236. if (!np->scripth0)
  7237. goto attach_error;
  7238. timer_setup(&np->timer, ncr53c8xx_timeout, 0);
  7239. /* Try to map the controller chip to virtual and physical memory. */
  7240. np->paddr = device->slot.base;
  7241. np->paddr2 = (np->features & FE_RAM) ? device->slot.base_2 : 0;
  7242. if (device->slot.base_v)
  7243. np->vaddr = device->slot.base_v;
  7244. else
  7245. np->vaddr = ioremap(device->slot.base_c, 128);
  7246. if (!np->vaddr) {
  7247. printk(KERN_ERR
  7248. "%s: can't map memory mapped IO region\n",ncr_name(np));
  7249. goto attach_error;
  7250. } else {
  7251. if (bootverbose > 1)
  7252. printk(KERN_INFO
  7253. "%s: using memory mapped IO at virtual address 0x%lx\n", ncr_name(np), (u_long) np->vaddr);
  7254. }
  7255. /* Make the controller's registers available. Now the INB INW INL
  7256. * OUTB OUTW OUTL macros can be used safely.
  7257. */
  7258. np->reg = (struct ncr_reg __iomem *)np->vaddr;
  7259. /* Do chip dependent initialization. */
  7260. ncr_prepare_setting(np);
  7261. if (np->paddr2 && sizeof(struct script) > 4096) {
  7262. np->paddr2 = 0;
  7263. printk(KERN_WARNING "%s: script too large, NOT using on chip RAM.\n",
  7264. ncr_name(np));
  7265. }
  7266. instance->max_channel = 0;
  7267. instance->this_id = np->myaddr;
  7268. instance->max_id = np->maxwide ? 16 : 8;
  7269. instance->max_lun = SCSI_NCR_MAX_LUN;
  7270. instance->base = (unsigned long) np->reg;
  7271. instance->irq = device->slot.irq;
  7272. instance->unique_id = device->slot.base;
  7273. instance->dma_channel = 0;
  7274. instance->cmd_per_lun = MAX_TAGS;
  7275. instance->can_queue = (MAX_START-4);
  7276. /* This can happen if you forget to call ncr53c8xx_init from
  7277. * your module_init */
  7278. BUG_ON(!ncr53c8xx_transport_template);
  7279. instance->transportt = ncr53c8xx_transport_template;
  7280. /* Patch script to physical addresses */
  7281. ncr_script_fill(&script0, &scripth0);
  7282. np->scripth = np->scripth0;
  7283. np->p_scripth = vtobus(np->scripth);
  7284. np->p_script = (np->paddr2) ? np->paddr2 : vtobus(np->script0);
  7285. ncr_script_copy_and_bind(np, (ncrcmd *) &script0,
  7286. (ncrcmd *) np->script0, sizeof(struct script));
  7287. ncr_script_copy_and_bind(np, (ncrcmd *) &scripth0,
  7288. (ncrcmd *) np->scripth0, sizeof(struct scripth));
  7289. np->ccb->p_ccb = vtobus (np->ccb);
  7290. /* Patch the script for LED support. */
  7291. if (np->features & FE_LED0) {
  7292. np->script0->idle[0] =
  7293. cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
  7294. np->script0->reselected[0] =
  7295. cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
  7296. np->script0->start[0] =
  7297. cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
  7298. }
  7299. /*
  7300. * Look for the target control block of this nexus.
  7301. * For i = 0 to 3
  7302. * JUMP ^ IFTRUE (MASK (i, 3)), @(next_lcb)
  7303. */
  7304. for (i = 0 ; i < 4 ; i++) {
  7305. np->jump_tcb[i].l_cmd =
  7306. cpu_to_scr((SCR_JUMP ^ IFTRUE (MASK (i, 3))));
  7307. np->jump_tcb[i].l_paddr =
  7308. cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_target));
  7309. }
  7310. ncr_chip_reset(np, 100);
  7311. /* Now check the cache handling of the chipset. */
  7312. if (ncr_snooptest(np)) {
  7313. printk(KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
  7314. goto attach_error;
  7315. }
  7316. /* Install the interrupt handler. */
  7317. np->irq = device->slot.irq;
  7318. /* Initialize the fixed part of the default ccb. */
  7319. ncr_init_ccb(np, np->ccb);
  7320. /*
  7321. * After SCSI devices have been opened, we cannot reset the bus
  7322. * safely, so we do it here. Interrupt handler does the real work.
  7323. * Process the reset exception if interrupts are not enabled yet.
  7324. * Then enable disconnects.
  7325. */
  7326. spin_lock_irqsave(&np->smp_lock, flags);
  7327. if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
  7328. printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
  7329. spin_unlock_irqrestore(&np->smp_lock, flags);
  7330. goto attach_error;
  7331. }
  7332. ncr_exception(np);
  7333. np->disc = 1;
  7334. /*
  7335. * The middle-level SCSI driver does not wait for devices to settle.
  7336. * Wait synchronously if more than 2 seconds.
  7337. */
  7338. if (driver_setup.settle_delay > 2) {
  7339. printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
  7340. ncr_name(np), driver_setup.settle_delay);
  7341. mdelay(1000 * driver_setup.settle_delay);
  7342. }
  7343. /* start the timeout daemon */
  7344. np->lasttime=0;
  7345. ncr_timeout (np);
  7346. /* use SIMPLE TAG messages by default */
  7347. #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
  7348. np->order = SIMPLE_QUEUE_TAG;
  7349. #endif
  7350. spin_unlock_irqrestore(&np->smp_lock, flags);
  7351. return instance;
  7352. attach_error:
  7353. if (!instance)
  7354. return NULL;
  7355. printk(KERN_INFO "%s: detaching...\n", ncr_name(np));
  7356. if (!np)
  7357. goto unregister;
  7358. if (np->scripth0)
  7359. m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
  7360. if (np->script0)
  7361. m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
  7362. if (np->ccb)
  7363. m_free_dma(np->ccb, sizeof(struct ccb), "CCB");
  7364. m_free_dma(np, sizeof(struct ncb), "NCB");
  7365. host_data->ncb = NULL;
  7366. unregister:
  7367. scsi_host_put(instance);
  7368. return NULL;
  7369. }
  7370. void ncr53c8xx_release(struct Scsi_Host *host)
  7371. {
  7372. struct host_data *host_data = shost_priv(host);
  7373. #ifdef DEBUG_NCR53C8XX
  7374. printk("ncr53c8xx: release\n");
  7375. #endif
  7376. if (host_data->ncb)
  7377. ncr_detach(host_data->ncb);
  7378. scsi_host_put(host);
  7379. }
  7380. static void ncr53c8xx_set_period(struct scsi_target *starget, int period)
  7381. {
  7382. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7383. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7384. struct tcb *tp = &np->target[starget->id];
  7385. if (period > np->maxsync)
  7386. period = np->maxsync;
  7387. else if (period < np->minsync)
  7388. period = np->minsync;
  7389. tp->usrsync = period;
  7390. ncr_negotiate(np, tp);
  7391. }
  7392. static void ncr53c8xx_set_offset(struct scsi_target *starget, int offset)
  7393. {
  7394. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7395. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7396. struct tcb *tp = &np->target[starget->id];
  7397. if (offset > np->maxoffs)
  7398. offset = np->maxoffs;
  7399. else if (offset < 0)
  7400. offset = 0;
  7401. tp->maxoffs = offset;
  7402. ncr_negotiate(np, tp);
  7403. }
  7404. static void ncr53c8xx_set_width(struct scsi_target *starget, int width)
  7405. {
  7406. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  7407. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7408. struct tcb *tp = &np->target[starget->id];
  7409. if (width > np->maxwide)
  7410. width = np->maxwide;
  7411. else if (width < 0)
  7412. width = 0;
  7413. tp->usrwide = width;
  7414. ncr_negotiate(np, tp);
  7415. }
  7416. static void ncr53c8xx_get_signalling(struct Scsi_Host *shost)
  7417. {
  7418. struct ncb *np = ((struct host_data *)shost->hostdata)->ncb;
  7419. enum spi_signal_type type;
  7420. switch (np->scsi_mode) {
  7421. case SMODE_SE:
  7422. type = SPI_SIGNAL_SE;
  7423. break;
  7424. case SMODE_HVD:
  7425. type = SPI_SIGNAL_HVD;
  7426. break;
  7427. default:
  7428. type = SPI_SIGNAL_UNKNOWN;
  7429. break;
  7430. }
  7431. spi_signalling(shost) = type;
  7432. }
  7433. static struct spi_function_template ncr53c8xx_transport_functions = {
  7434. .set_period = ncr53c8xx_set_period,
  7435. .show_period = 1,
  7436. .set_offset = ncr53c8xx_set_offset,
  7437. .show_offset = 1,
  7438. .set_width = ncr53c8xx_set_width,
  7439. .show_width = 1,
  7440. .get_signalling = ncr53c8xx_get_signalling,
  7441. };
  7442. int __init ncr53c8xx_init(void)
  7443. {
  7444. ncr53c8xx_transport_template = spi_attach_transport(&ncr53c8xx_transport_functions);
  7445. if (!ncr53c8xx_transport_template)
  7446. return -ENODEV;
  7447. return 0;
  7448. }
  7449. void ncr53c8xx_exit(void)
  7450. {
  7451. spi_release_transport(ncr53c8xx_transport_template);
  7452. }