ncr53c8xx.c 211 KB

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