ncr53c8xx.c 197 KB

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