53c7xx.c 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103
  1. /*
  2. * 53c710 driver. Modified from Drew Eckhardts driver
  3. * for 53c810 by Richard Hirst [richard@sleepie.demon.co.uk]
  4. * Check out PERM_OPTIONS and EXPECTED_CLOCK, which may be defined in the
  5. * relevant machine specific file (eg. mvme16x.[ch], amiga7xx.[ch]).
  6. * There are also currently some defines at the top of 53c7xx.scr.
  7. * The chip type is #defined in script_asm.pl, as well as the Makefile.
  8. * Host scsi ID expected to be 7 - see NCR53c7x0_init().
  9. *
  10. * I have removed the PCI code and some of the 53c8xx specific code -
  11. * simply to make this file smaller and easier to manage.
  12. *
  13. * MVME16x issues:
  14. * Problems trying to read any chip registers in NCR53c7x0_init(), as they
  15. * may never have been set by 16xBug (eg. If kernel has come in over tftp).
  16. */
  17. /*
  18. * Adapted for Linux/m68k Amiga platforms for the A4000T/A4091 and
  19. * WarpEngine SCSI controllers.
  20. * By Alan Hourihane <alanh@fairlite.demon.co.uk>
  21. * Thanks to Richard Hirst for making it possible with the MVME additions
  22. */
  23. /*
  24. * 53c710 rev 0 doesn't support add with carry. Rev 1 and 2 does. To
  25. * overcome this problem you can define FORCE_DSA_ALIGNMENT, which ensures
  26. * that the DSA address is always xxxxxx00. If disconnection is not allowed,
  27. * then the script only ever tries to add small (< 256) positive offsets to
  28. * DSA, so lack of carry isn't a problem. FORCE_DSA_ALIGNMENT can, of course,
  29. * be defined for all chip revisions at a small cost in memory usage.
  30. */
  31. #define FORCE_DSA_ALIGNMENT
  32. /*
  33. * Selection timer does not always work on the 53c710, depending on the
  34. * timing at the last disconnect, if this is a problem for you, try
  35. * using validids as detailed below.
  36. *
  37. * Options for the NCR7xx driver
  38. *
  39. * noasync:0 - disables sync and asynchronous negotiation
  40. * nosync:0 - disables synchronous negotiation (does async)
  41. * nodisconnect:0 - disables disconnection
  42. * validids:0x?? - Bitmask field that disallows certain ID's.
  43. * - e.g. 0x03 allows ID 0,1
  44. * - 0x1F allows ID 0,1,2,3,4
  45. * opthi:n - replace top word of options with 'n'
  46. * optlo:n - replace bottom word of options with 'n'
  47. * - ALWAYS SPECIFY opthi THEN optlo <<<<<<<<<<
  48. */
  49. /*
  50. * PERM_OPTIONS are driver options which will be enabled for all NCR boards
  51. * in the system at driver initialization time.
  52. *
  53. * Don't THINK about touching these in PERM_OPTIONS :
  54. * OPTION_MEMORY_MAPPED
  55. * 680x0 doesn't have an IO map!
  56. *
  57. * OPTION_DEBUG_TEST1
  58. * Test 1 does bus mastering and interrupt tests, which will help weed
  59. * out brain damaged main boards.
  60. *
  61. * Other PERM_OPTIONS settings are listed below. Note the actual options
  62. * required are set in the relevant file (mvme16x.c, amiga7xx.c, etc):
  63. *
  64. * OPTION_NO_ASYNC
  65. * Don't negotiate for asynchronous transfers on the first command
  66. * when OPTION_ALWAYS_SYNCHRONOUS is set. Useful for dain bramaged
  67. * devices which do something bad rather than sending a MESSAGE
  68. * REJECT back to us like they should if they can't cope.
  69. *
  70. * OPTION_SYNCHRONOUS
  71. * Enable support for synchronous transfers. Target negotiated
  72. * synchronous transfers will be responded to. To initiate
  73. * a synchronous transfer request, call
  74. *
  75. * request_synchronous (hostno, target)
  76. *
  77. * from within KGDB.
  78. *
  79. * OPTION_ALWAYS_SYNCHRONOUS
  80. * Negotiate for synchronous transfers with every target after
  81. * driver initialization or a SCSI bus reset. This is a bit dangerous,
  82. * since there are some dain bramaged SCSI devices which will accept
  83. * SDTR messages but keep talking asynchronously.
  84. *
  85. * OPTION_DISCONNECT
  86. * Enable support for disconnect/reconnect. To change the
  87. * default setting on a given host adapter, call
  88. *
  89. * request_disconnect (hostno, allow)
  90. *
  91. * where allow is non-zero to allow, 0 to disallow.
  92. *
  93. * If you really want to run 10MHz FAST SCSI-II transfers, you should
  94. * know that the NCR driver currently ignores parity information. Most
  95. * systems do 5MHz SCSI fine. I've seen a lot that have problems faster
  96. * than 8MHz. To play it safe, we only request 5MHz transfers.
  97. *
  98. * If you'd rather get 10MHz transfers, edit sdtr_message and change
  99. * the fourth byte from 50 to 25.
  100. */
  101. /*
  102. * Sponsored by
  103. * iX Multiuser Multitasking Magazine
  104. * Hannover, Germany
  105. * hm@ix.de
  106. *
  107. * Copyright 1993, 1994, 1995 Drew Eckhardt
  108. * Visionary Computing
  109. * (Unix and Linux consulting and custom programming)
  110. * drew@PoohSticks.ORG
  111. * +1 (303) 786-7975
  112. *
  113. * TolerANT and SCSI SCRIPTS are registered trademarks of NCR Corporation.
  114. *
  115. * For more information, please consult
  116. *
  117. * NCR53C810
  118. * SCSI I/O Processor
  119. * Programmer's Guide
  120. *
  121. * NCR 53C810
  122. * PCI-SCSI I/O Processor
  123. * Data Manual
  124. *
  125. * NCR 53C810/53C820
  126. * PCI-SCSI I/O Processor Design In Guide
  127. *
  128. * For literature on Symbios Logic Inc. formerly NCR, SCSI,
  129. * and Communication products please call (800) 334-5454 or
  130. * (719) 536-3300.
  131. *
  132. * PCI BIOS Specification Revision
  133. * PCI Local Bus Specification
  134. * PCI System Design Guide
  135. *
  136. * PCI Special Interest Group
  137. * M/S HF3-15A
  138. * 5200 N.E. Elam Young Parkway
  139. * Hillsboro, Oregon 97124-6497
  140. * +1 (503) 696-2000
  141. * +1 (800) 433-5177
  142. */
  143. /*
  144. * Design issues :
  145. * The cumulative latency needed to propagate a read/write request
  146. * through the file system, buffer cache, driver stacks, SCSI host, and
  147. * SCSI device is ultimately the limiting factor in throughput once we
  148. * have a sufficiently fast host adapter.
  149. *
  150. * So, to maximize performance we want to keep the ratio of latency to data
  151. * transfer time to a minimum by
  152. * 1. Minimizing the total number of commands sent (typical command latency
  153. * including drive and bus mastering host overhead is as high as 4.5ms)
  154. * to transfer a given amount of data.
  155. *
  156. * This is accomplished by placing no arbitrary limit on the number
  157. * of scatter/gather buffers supported, since we can transfer 1K
  158. * per scatter/gather buffer without Eric's cluster patches,
  159. * 4K with.
  160. *
  161. * 2. Minimizing the number of fatal interrupts serviced, since
  162. * fatal interrupts halt the SCSI I/O processor. Basically,
  163. * this means offloading the practical maximum amount of processing
  164. * to the SCSI chip.
  165. *
  166. * On the NCR53c810/820/720, this is accomplished by using
  167. * interrupt-on-the-fly signals when commands complete,
  168. * and only handling fatal errors and SDTR / WDTR messages
  169. * in the host code.
  170. *
  171. * On the NCR53c710, interrupts are generated as on the NCR53c8x0,
  172. * only the lack of a interrupt-on-the-fly facility complicates
  173. * things. Also, SCSI ID registers and commands are
  174. * bit fielded rather than binary encoded.
  175. *
  176. * On the NCR53c700 and NCR53c700-66, operations that are done via
  177. * indirect, table mode on the more advanced chips must be
  178. * replaced by calls through a jump table which
  179. * acts as a surrogate for the DSA. Unfortunately, this
  180. * will mean that we must service an interrupt for each
  181. * disconnect/reconnect.
  182. *
  183. * 3. Eliminating latency by pipelining operations at the different levels.
  184. *
  185. * This driver allows a configurable number of commands to be enqueued
  186. * for each target/lun combination (experimentally, I have discovered
  187. * that two seems to work best) and will ultimately allow for
  188. * SCSI-II tagged queuing.
  189. *
  190. *
  191. * Architecture :
  192. * This driver is built around a Linux queue of commands waiting to
  193. * be executed, and a shared Linux/NCR array of commands to start. Commands
  194. * are transferred to the array by the run_process_issue_queue() function
  195. * which is called whenever a command completes.
  196. *
  197. * As commands are completed, the interrupt routine is triggered,
  198. * looks for commands in the linked list of completed commands with
  199. * valid status, removes these commands from a list of running commands,
  200. * calls the done routine, and flags their target/luns as not busy.
  201. *
  202. * Due to limitations in the intelligence of the NCR chips, certain
  203. * concessions are made. In many cases, it is easier to dynamically
  204. * generate/fix-up code rather than calculate on the NCR at run time.
  205. * So, code is generated or fixed up for
  206. *
  207. * - Handling data transfers, using a variable number of MOVE instructions
  208. * interspersed with CALL MSG_IN, WHEN MSGIN instructions.
  209. *
  210. * The DATAIN and DATAOUT routines are separate, so that an incorrect
  211. * direction can be trapped, and space isn't wasted.
  212. *
  213. * It may turn out that we're better off using some sort
  214. * of table indirect instruction in a loop with a variable
  215. * sized table on the NCR53c710 and newer chips.
  216. *
  217. * - Checking for reselection (NCR53c710 and better)
  218. *
  219. * - Handling the details of SCSI context switches (NCR53c710 and better),
  220. * such as reprogramming appropriate synchronous parameters,
  221. * removing the dsa structure from the NCR's queue of outstanding
  222. * commands, etc.
  223. *
  224. */
  225. #include <linux/module.h>
  226. #include <linux/types.h>
  227. #include <asm/setup.h>
  228. #include <asm/dma.h>
  229. #include <asm/io.h>
  230. #include <asm/system.h>
  231. #include <linux/delay.h>
  232. #include <linux/signal.h>
  233. #include <linux/sched.h>
  234. #include <linux/errno.h>
  235. #include <linux/string.h>
  236. #include <linux/slab.h>
  237. #include <linux/vmalloc.h>
  238. #include <linux/mm.h>
  239. #include <linux/ioport.h>
  240. #include <linux/time.h>
  241. #include <linux/blkdev.h>
  242. #include <linux/spinlock.h>
  243. #include <linux/interrupt.h>
  244. #include <asm/pgtable.h>
  245. #ifdef CONFIG_AMIGA
  246. #include <asm/amigahw.h>
  247. #include <asm/amigaints.h>
  248. #include <asm/irq.h>
  249. #define BIG_ENDIAN
  250. #define NO_IO_SPACE
  251. #endif
  252. #ifdef CONFIG_MVME16x
  253. #include <asm/mvme16xhw.h>
  254. #define BIG_ENDIAN
  255. #define NO_IO_SPACE
  256. #define VALID_IDS
  257. #endif
  258. #ifdef CONFIG_BVME6000
  259. #include <asm/bvme6000hw.h>
  260. #define BIG_ENDIAN
  261. #define NO_IO_SPACE
  262. #define VALID_IDS
  263. #endif
  264. #include "scsi.h"
  265. #include <scsi/scsi_dbg.h>
  266. #include <scsi/scsi_host.h>
  267. #include <scsi/scsi_transport_spi.h>
  268. #include "53c7xx.h"
  269. #include <linux/stat.h>
  270. #include <linux/stddef.h>
  271. #ifdef NO_IO_SPACE
  272. /*
  273. * The following make the definitions in 53c7xx.h (write8, etc) smaller,
  274. * we don't have separate i/o space anyway.
  275. */
  276. #undef inb
  277. #undef outb
  278. #undef inw
  279. #undef outw
  280. #undef inl
  281. #undef outl
  282. #define inb(x) 1
  283. #define inw(x) 1
  284. #define inl(x) 1
  285. #define outb(x,y) 1
  286. #define outw(x,y) 1
  287. #define outl(x,y) 1
  288. #endif
  289. static int check_address (unsigned long addr, int size);
  290. static void dump_events (struct Scsi_Host *host, int count);
  291. static Scsi_Cmnd * return_outstanding_commands (struct Scsi_Host *host,
  292. int free, int issue);
  293. static void hard_reset (struct Scsi_Host *host);
  294. static void ncr_scsi_reset (struct Scsi_Host *host);
  295. static void print_lots (struct Scsi_Host *host);
  296. static void set_synchronous (struct Scsi_Host *host, int target, int sxfer,
  297. int scntl3, int now_connected);
  298. static int datapath_residual (struct Scsi_Host *host);
  299. static const char * sbcl_to_phase (int sbcl);
  300. static void print_progress (Scsi_Cmnd *cmd);
  301. static void print_queues (struct Scsi_Host *host);
  302. static void process_issue_queue (unsigned long flags);
  303. static int shutdown (struct Scsi_Host *host);
  304. static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int result);
  305. static int disable (struct Scsi_Host *host);
  306. static int NCR53c7xx_run_tests (struct Scsi_Host *host);
  307. static irqreturn_t NCR53c7x0_intr(int irq, void *dev_id);
  308. static void NCR53c7x0_intfly (struct Scsi_Host *host);
  309. static int ncr_halt (struct Scsi_Host *host);
  310. static void intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd
  311. *cmd);
  312. static void intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd);
  313. static void print_dsa (struct Scsi_Host *host, u32 *dsa,
  314. const char *prefix);
  315. static int print_insn (struct Scsi_Host *host, const u32 *insn,
  316. const char *prefix, int kernel);
  317. static void NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd);
  318. static void NCR53c7x0_init_fixup (struct Scsi_Host *host);
  319. static int NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host, struct
  320. NCR53c7x0_cmd *cmd);
  321. static void NCR53c7x0_soft_reset (struct Scsi_Host *host);
  322. /* Size of event list (per host adapter) */
  323. static int track_events = 0;
  324. static struct Scsi_Host *first_host = NULL; /* Head of list of NCR boards */
  325. static struct scsi_host_template *the_template = NULL;
  326. /* NCR53c710 script handling code */
  327. #include "53c7xx_d.h"
  328. #ifdef A_int_debug_sync
  329. #define DEBUG_SYNC_INTR A_int_debug_sync
  330. #endif
  331. int NCR53c7xx_script_len = sizeof (SCRIPT);
  332. int NCR53c7xx_dsa_len = A_dsa_end + Ent_dsa_zero - Ent_dsa_code_template;
  333. #ifdef FORCE_DSA_ALIGNMENT
  334. int CmdPageStart = (0 - Ent_dsa_zero - sizeof(struct NCR53c7x0_cmd)) & 0xff;
  335. #endif
  336. static char *setup_strings[] =
  337. {"","","","","","","",""};
  338. #define MAX_SETUP_STRINGS ARRAY_SIZE(setup_strings)
  339. #define SETUP_BUFFER_SIZE 200
  340. static char setup_buffer[SETUP_BUFFER_SIZE];
  341. static char setup_used[MAX_SETUP_STRINGS];
  342. void ncr53c7xx_setup (char *str, int *ints)
  343. {
  344. int i;
  345. char *p1, *p2;
  346. p1 = setup_buffer;
  347. *p1 = '\0';
  348. if (str)
  349. strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
  350. setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
  351. p1 = setup_buffer;
  352. i = 0;
  353. while (*p1 && (i < MAX_SETUP_STRINGS)) {
  354. p2 = strchr(p1, ',');
  355. if (p2) {
  356. *p2 = '\0';
  357. if (p1 != p2)
  358. setup_strings[i] = p1;
  359. p1 = p2 + 1;
  360. i++;
  361. }
  362. else {
  363. setup_strings[i] = p1;
  364. break;
  365. }
  366. }
  367. for (i=0; i<MAX_SETUP_STRINGS; i++)
  368. setup_used[i] = 0;
  369. }
  370. /* check_setup_strings() returns index if key found, 0 if not
  371. */
  372. static int check_setup_strings(char *key, int *flags, int *val, char *buf)
  373. {
  374. int x;
  375. char *cp;
  376. for (x=0; x<MAX_SETUP_STRINGS; x++) {
  377. if (setup_used[x])
  378. continue;
  379. if (!strncmp(setup_strings[x], key, strlen(key)))
  380. break;
  381. if (!strncmp(setup_strings[x], "next", strlen("next")))
  382. return 0;
  383. }
  384. if (x == MAX_SETUP_STRINGS)
  385. return 0;
  386. setup_used[x] = 1;
  387. cp = setup_strings[x] + strlen(key);
  388. *val = -1;
  389. if (*cp != ':')
  390. return ++x;
  391. cp++;
  392. if ((*cp >= '0') && (*cp <= '9')) {
  393. *val = simple_strtoul(cp,NULL,0);
  394. }
  395. return ++x;
  396. }
  397. /*
  398. * KNOWN BUGS :
  399. * - There is some sort of conflict when the PPP driver is compiled with
  400. * support for 16 channels?
  401. *
  402. * - On systems which predate the 1.3.x initialization order change,
  403. * the NCR driver will cause Cannot get free page messages to appear.
  404. * These are harmless, but I don't know of an easy way to avoid them.
  405. *
  406. * - With OPTION_DISCONNECT, on two systems under unknown circumstances,
  407. * we get a PHASE MISMATCH with DSA set to zero (suggests that we
  408. * are occurring somewhere in the reselection code) where
  409. * DSP=some value DCMD|DBC=same value.
  410. *
  411. * Closer inspection suggests that we may be trying to execute
  412. * some portion of the DSA?
  413. * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
  414. * scsi0 : handling residual transfer (+ 0 bytes from DMA FIFO)
  415. * scsi0 : no current command : unexpected phase MSGIN.
  416. * DSP=0x1c46cc, DCMD|DBC=0x1c46ac, DSA=0x0
  417. * DSPS=0x0, TEMP=0x1c3e70, DMODE=0x80
  418. * scsi0 : DSP->
  419. * 001c46cc : 0x001c46cc 0x00000000
  420. * 001c46d4 : 0x001c5ea0 0x000011f8
  421. *
  422. * Changed the print code in the phase_mismatch handler so
  423. * that we call print_lots to try to diagnose this.
  424. *
  425. */
  426. /*
  427. * Possible future direction of architecture for max performance :
  428. *
  429. * We're using a single start array for the NCR chip. This is
  430. * sub-optimal, because we cannot add a command which would conflict with
  431. * an executing command to this start queue, and therefore must insert the
  432. * next command for a given I/T/L combination after the first has completed;
  433. * incurring our interrupt latency between SCSI commands.
  434. *
  435. * To allow further pipelining of the NCR and host CPU operation, we want
  436. * to set things up so that immediately on termination of a command destined
  437. * for a given LUN, we get that LUN busy again.
  438. *
  439. * To do this, we need to add a 32 bit pointer to which is jumped to
  440. * on completion of a command. If no new command is available, this
  441. * would point to the usual DSA issue queue select routine.
  442. *
  443. * If one were, it would point to a per-NCR53c7x0_cmd select routine
  444. * which starts execution immediately, inserting the command at the head
  445. * of the start queue if the NCR chip is selected or reselected.
  446. *
  447. * We would change so that we keep a list of outstanding commands
  448. * for each unit, rather than a single running_list. We'd insert
  449. * a new command into the right running list; if the NCR didn't
  450. * have something running for that yet, we'd put it in the
  451. * start queue as well. Some magic needs to happen to handle the
  452. * race condition between the first command terminating before the
  453. * new one is written.
  454. *
  455. * Potential for profiling :
  456. * Call do_gettimeofday(struct timeval *tv) to get 800ns resolution.
  457. */
  458. /*
  459. * TODO :
  460. * 1. To support WIDE transfers, not much needs to happen. We
  461. * should do CHMOVE instructions instead of MOVEs when
  462. * we have scatter/gather segments of uneven length. When
  463. * we do this, we need to handle the case where we disconnect
  464. * between segments.
  465. *
  466. * 2. Currently, when Icky things happen we do a FATAL(). Instead,
  467. * we want to do an integrity check on the parts of the NCR hostdata
  468. * structure which were initialized at boot time; FATAL() if that
  469. * fails, and otherwise try to recover. Keep track of how many
  470. * times this has happened within a single SCSI command; if it
  471. * gets excessive, then FATAL().
  472. *
  473. * 3. Parity checking is currently disabled, and a few things should
  474. * happen here now that we support synchronous SCSI transfers :
  475. * 1. On soft-reset, we shoould set the EPC (Enable Parity Checking)
  476. * and AAP (Assert SATN/ on parity error) bits in SCNTL0.
  477. *
  478. * 2. We should enable the parity interrupt in the SIEN0 register.
  479. *
  480. * 3. intr_phase_mismatch() needs to believe that message out is
  481. * always an "acceptable" phase to have a mismatch in. If
  482. * the old phase was MSG_IN, we should send a MESSAGE PARITY
  483. * error. If the old phase was something else, we should send
  484. * a INITIATOR_DETECTED_ERROR message. Note that this could
  485. * cause a RESTORE POINTERS message; so we should handle that
  486. * correctly first. Instead, we should probably do an
  487. * initiator_abort.
  488. *
  489. * 4. MPEE bit of CTEST4 should be set so we get interrupted if
  490. * we detect an error.
  491. *
  492. *
  493. * 5. The initial code has been tested on the NCR53c810. I don't
  494. * have access to NCR53c700, 700-66 (Forex boards), NCR53c710
  495. * (NCR Pentium systems), NCR53c720, NCR53c820, or NCR53c825 boards to
  496. * finish development on those platforms.
  497. *
  498. * NCR53c820/825/720 - need to add wide transfer support, including WDTR
  499. * negotiation, programming of wide transfer capabilities
  500. * on reselection and table indirect selection.
  501. *
  502. * NCR53c710 - need to add fatal interrupt or GEN code for
  503. * command completion signaling. Need to modify all
  504. * SDID, SCID, etc. registers, and table indirect select code
  505. * since these use bit fielded (ie 1<<target) instead of
  506. * binary encoded target ids. Need to accommodate
  507. * different register mappings, probably scan through
  508. * the SCRIPT code and change the non SFBR register operand
  509. * of all MOVE instructions.
  510. *
  511. * It is rather worse than this actually, the 710 corrupts
  512. * both TEMP and DSA when you do a MOVE MEMORY. This
  513. * screws you up all over the place. MOVE MEMORY 4 with a
  514. * destination of DSA seems to work OK, which helps some.
  515. * Richard Hirst richard@sleepie.demon.co.uk
  516. *
  517. * NCR53c700/700-66 - need to add code to refix addresses on
  518. * every nexus change, eliminate all table indirect code,
  519. * very messy.
  520. *
  521. * 6. The NCR53c7x0 series is very popular on other platforms that
  522. * could be running Linux - ie, some high performance AMIGA SCSI
  523. * boards use it.
  524. *
  525. * So, I should include #ifdef'd code so that it is
  526. * compatible with these systems.
  527. *
  528. * Specifically, the little Endian assumptions I made in my
  529. * bit fields need to change, and if the NCR doesn't see memory
  530. * the right way, we need to provide options to reverse words
  531. * when the scripts are relocated.
  532. *
  533. * 7. Use vremap() to access memory mapped boards.
  534. */
  535. /*
  536. * Allow for simultaneous existence of multiple SCSI scripts so we
  537. * can have a single driver binary for all of the family.
  538. *
  539. * - one for NCR53c700 and NCR53c700-66 chips (not yet supported)
  540. * - one for rest (only the NCR53c810, 815, 820, and 825 are currently
  541. * supported)
  542. *
  543. * So that we only need two SCSI scripts, we need to modify things so
  544. * that we fixup register accesses in READ/WRITE instructions, and
  545. * we'll also have to accommodate the bit vs. binary encoding of IDs
  546. * with the 7xx chips.
  547. */
  548. #define ROUNDUP(adr,type) \
  549. ((void *) (((long) (adr) + sizeof(type) - 1) & ~(sizeof(type) - 1)))
  550. /*
  551. * Function: issue_to_cmd
  552. *
  553. * Purpose: convert jump instruction in issue array to NCR53c7x0_cmd
  554. * structure pointer.
  555. *
  556. * Inputs; issue - pointer to start of NOP or JUMP instruction
  557. * in issue array.
  558. *
  559. * Returns: pointer to command on success; 0 if opcode is NOP.
  560. */
  561. static inline struct NCR53c7x0_cmd *
  562. issue_to_cmd (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  563. u32 *issue)
  564. {
  565. return (issue[0] != hostdata->NOP_insn) ?
  566. /*
  567. * If the IF TRUE bit is set, it's a JUMP instruction. The
  568. * operand is a bus pointer to the dsa_begin routine for this DSA. The
  569. * dsa field of the NCR53c7x0_cmd structure starts with the
  570. * DSA code template. By converting to a virtual address,
  571. * subtracting the code template size, and offset of the
  572. * dsa field, we end up with a pointer to the start of the
  573. * structure (alternatively, we could use the
  574. * dsa_cmnd field, an anachronism from when we weren't
  575. * sure what the relationship between the NCR structures
  576. * and host structures were going to be.
  577. */
  578. (struct NCR53c7x0_cmd *) ((char *) bus_to_virt (issue[1]) -
  579. (hostdata->E_dsa_code_begin - hostdata->E_dsa_code_template) -
  580. offsetof(struct NCR53c7x0_cmd, dsa))
  581. /* If the IF TRUE bit is not set, it's a NOP */
  582. : NULL;
  583. }
  584. /*
  585. * FIXME: we should junk these, in favor of synchronous_want and
  586. * wide_want in the NCR53c7x0_hostdata structure.
  587. */
  588. /* Template for "preferred" synchronous transfer parameters. */
  589. static const unsigned char sdtr_message[] = {
  590. #ifdef CONFIG_SCSI_NCR53C7xx_FAST
  591. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 25 /* *4ns */, 8 /* off */
  592. #else
  593. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 50 /* *4ns */, 8 /* off */
  594. #endif
  595. };
  596. /* Template to request asynchronous transfers */
  597. static const unsigned char async_message[] = {
  598. EXTENDED_MESSAGE, 3 /* length */, EXTENDED_SDTR, 0, 0 /* asynchronous */
  599. };
  600. /* Template for "preferred" WIDE transfer parameters */
  601. static const unsigned char wdtr_message[] = {
  602. EXTENDED_MESSAGE, 2 /* length */, EXTENDED_WDTR, 1 /* 2^1 bytes */
  603. };
  604. #if 0
  605. /*
  606. * Function : struct Scsi_Host *find_host (int host)
  607. *
  608. * Purpose : KGDB support function which translates a host number
  609. * to a host structure.
  610. *
  611. * Inputs : host - number of SCSI host
  612. *
  613. * Returns : NULL on failure, pointer to host structure on success.
  614. */
  615. static struct Scsi_Host *
  616. find_host (int host) {
  617. struct Scsi_Host *h;
  618. for (h = first_host; h && h->host_no != host; h = h->next);
  619. if (!h) {
  620. printk (KERN_ALERT "scsi%d not found\n", host);
  621. return NULL;
  622. } else if (h->hostt != the_template) {
  623. printk (KERN_ALERT "scsi%d is not a NCR board\n", host);
  624. return NULL;
  625. }
  626. return h;
  627. }
  628. #if 0
  629. /*
  630. * Function : request_synchronous (int host, int target)
  631. *
  632. * Purpose : KGDB interface which will allow us to negotiate for
  633. * synchronous transfers. This ill be replaced with a more
  634. * integrated function; perhaps a new entry in the scsi_host
  635. * structure, accessible via an ioctl() or perhaps /proc/scsi.
  636. *
  637. * Inputs : host - number of SCSI host; target - number of target.
  638. *
  639. * Returns : 0 when negotiation has been setup for next SCSI command,
  640. * -1 on failure.
  641. */
  642. static int
  643. request_synchronous (int host, int target) {
  644. struct Scsi_Host *h;
  645. struct NCR53c7x0_hostdata *hostdata;
  646. unsigned long flags;
  647. if (target < 0) {
  648. printk (KERN_ALERT "target %d is bogus\n", target);
  649. return -1;
  650. }
  651. if (!(h = find_host (host)))
  652. return -1;
  653. else if (h->this_id == target) {
  654. printk (KERN_ALERT "target %d is host ID\n", target);
  655. return -1;
  656. }
  657. else if (target >= h->max_id) {
  658. printk (KERN_ALERT "target %d exceeds maximum of %d\n", target,
  659. h->max_id);
  660. return -1;
  661. }
  662. hostdata = (struct NCR53c7x0_hostdata *)h->hostdata[0];
  663. local_irq_save(flags);
  664. if (hostdata->initiate_sdtr & (1 << target)) {
  665. local_irq_restore(flags);
  666. printk (KERN_ALERT "target %d already doing SDTR\n", target);
  667. return -1;
  668. }
  669. hostdata->initiate_sdtr |= (1 << target);
  670. local_irq_restore(flags);
  671. return 0;
  672. }
  673. #endif
  674. /*
  675. * Function : request_disconnect (int host, int on_or_off)
  676. *
  677. * Purpose : KGDB support function, tells us to allow or disallow
  678. * disconnections.
  679. *
  680. * Inputs : host - number of SCSI host; on_or_off - non-zero to allow,
  681. * zero to disallow.
  682. *
  683. * Returns : 0 on success, * -1 on failure.
  684. */
  685. static int
  686. request_disconnect (int host, int on_or_off) {
  687. struct Scsi_Host *h;
  688. struct NCR53c7x0_hostdata *hostdata;
  689. if (!(h = find_host (host)))
  690. return -1;
  691. hostdata = (struct NCR53c7x0_hostdata *) h->hostdata[0];
  692. if (on_or_off)
  693. hostdata->options |= OPTION_DISCONNECT;
  694. else
  695. hostdata->options &= ~OPTION_DISCONNECT;
  696. return 0;
  697. }
  698. #endif
  699. /*
  700. * Function : static void NCR53c7x0_driver_init (struct Scsi_Host *host)
  701. *
  702. * Purpose : Initialize internal structures, as required on startup, or
  703. * after a SCSI bus reset.
  704. *
  705. * Inputs : host - pointer to this host adapter's structure
  706. */
  707. static void
  708. NCR53c7x0_driver_init (struct Scsi_Host *host) {
  709. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  710. host->hostdata[0];
  711. int i, j;
  712. u32 *ncrcurrent;
  713. for (i = 0; i < 16; ++i) {
  714. hostdata->request_sense[i] = 0;
  715. for (j = 0; j < 8; ++j)
  716. hostdata->busy[i][j] = 0;
  717. set_synchronous (host, i, /* sxfer */ 0, hostdata->saved_scntl3, 0);
  718. }
  719. hostdata->issue_queue = NULL;
  720. hostdata->running_list = hostdata->finished_queue =
  721. hostdata->ncrcurrent = NULL;
  722. for (i = 0, ncrcurrent = (u32 *) hostdata->schedule;
  723. i < host->can_queue; ++i, ncrcurrent += 2) {
  724. ncrcurrent[0] = hostdata->NOP_insn;
  725. ncrcurrent[1] = 0xdeadbeef;
  726. }
  727. ncrcurrent[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) | DBC_TCI_TRUE;
  728. ncrcurrent[1] = (u32) virt_to_bus (hostdata->script) +
  729. hostdata->E_wait_reselect;
  730. hostdata->reconnect_dsa_head = 0;
  731. hostdata->addr_reconnect_dsa_head = (u32)
  732. virt_to_bus((void *) &(hostdata->reconnect_dsa_head));
  733. hostdata->expecting_iid = 0;
  734. hostdata->expecting_sto = 0;
  735. if (hostdata->options & OPTION_ALWAYS_SYNCHRONOUS)
  736. hostdata->initiate_sdtr = 0xffff;
  737. else
  738. hostdata->initiate_sdtr = 0;
  739. hostdata->talked_to = 0;
  740. hostdata->idle = 1;
  741. }
  742. /*
  743. * Function : static int clock_to_ccf_710 (int clock)
  744. *
  745. * Purpose : Return the clock conversion factor for a given SCSI clock.
  746. *
  747. * Inputs : clock - SCSI clock expressed in Hz.
  748. *
  749. * Returns : ccf on success, -1 on failure.
  750. */
  751. static int
  752. clock_to_ccf_710 (int clock) {
  753. if (clock <= 16666666)
  754. return -1;
  755. if (clock <= 25000000)
  756. return 2; /* Divide by 1.0 */
  757. else if (clock <= 37500000)
  758. return 1; /* Divide by 1.5 */
  759. else if (clock <= 50000000)
  760. return 0; /* Divide by 2.0 */
  761. else if (clock <= 66000000)
  762. return 3; /* Divide by 3.0 */
  763. else
  764. return -1;
  765. }
  766. /*
  767. * Function : static int NCR53c7x0_init (struct Scsi_Host *host)
  768. *
  769. * Purpose : initialize the internal structures for a given SCSI host
  770. *
  771. * Inputs : host - pointer to this host adapter's structure
  772. *
  773. * Preconditions : when this function is called, the chip_type
  774. * field of the hostdata structure MUST have been set.
  775. *
  776. * Returns : 0 on success, -1 on failure.
  777. */
  778. int
  779. NCR53c7x0_init (struct Scsi_Host *host) {
  780. NCR53c7x0_local_declare();
  781. int i, ccf;
  782. unsigned char revision;
  783. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  784. host->hostdata[0];
  785. /*
  786. * There are some things which we need to know about in order to provide
  787. * a semblance of support. Print 'em if they aren't what we expect,
  788. * otherwise don't add to the noise.
  789. *
  790. * -1 means we don't know what to expect.
  791. */
  792. int val, flags;
  793. char buf[32];
  794. int expected_id = -1;
  795. int expected_clock = -1;
  796. int uninitialized = 0;
  797. #ifdef NO_IO_SPACE
  798. int expected_mapping = OPTION_MEMORY_MAPPED;
  799. #else
  800. int expected_mapping = OPTION_IO_MAPPED;
  801. #endif
  802. for (i=0;i<7;i++)
  803. hostdata->valid_ids[i] = 1; /* Default all ID's to scan */
  804. /* Parse commandline flags */
  805. if (check_setup_strings("noasync",&flags,&val,buf))
  806. {
  807. hostdata->options |= OPTION_NO_ASYNC;
  808. hostdata->options &= ~(OPTION_SYNCHRONOUS | OPTION_ALWAYS_SYNCHRONOUS);
  809. }
  810. if (check_setup_strings("nosync",&flags,&val,buf))
  811. {
  812. hostdata->options &= ~(OPTION_SYNCHRONOUS | OPTION_ALWAYS_SYNCHRONOUS);
  813. }
  814. if (check_setup_strings("nodisconnect",&flags,&val,buf))
  815. hostdata->options &= ~OPTION_DISCONNECT;
  816. if (check_setup_strings("validids",&flags,&val,buf))
  817. {
  818. for (i=0;i<7;i++)
  819. hostdata->valid_ids[i] = val & (1<<i);
  820. }
  821. if ((i = check_setup_strings("next",&flags,&val,buf)))
  822. {
  823. while (i)
  824. setup_used[--i] = 1;
  825. }
  826. if (check_setup_strings("opthi",&flags,&val,buf))
  827. hostdata->options = (long long)val << 32;
  828. if (check_setup_strings("optlo",&flags,&val,buf))
  829. hostdata->options |= val;
  830. NCR53c7x0_local_setup(host);
  831. switch (hostdata->chip) {
  832. case 710:
  833. case 770:
  834. hostdata->dstat_sir_intr = NCR53c7x0_dstat_sir_intr;
  835. hostdata->init_save_regs = NULL;
  836. hostdata->dsa_fixup = NCR53c7xx_dsa_fixup;
  837. hostdata->init_fixup = NCR53c7x0_init_fixup;
  838. hostdata->soft_reset = NCR53c7x0_soft_reset;
  839. hostdata->run_tests = NCR53c7xx_run_tests;
  840. expected_clock = hostdata->scsi_clock;
  841. expected_id = 7;
  842. break;
  843. default:
  844. printk ("scsi%d : chip type of %d is not supported yet, detaching.\n",
  845. host->host_no, hostdata->chip);
  846. scsi_unregister (host);
  847. return -1;
  848. }
  849. /* Assign constants accessed by NCR */
  850. hostdata->NCR53c7xx_zero = 0;
  851. hostdata->NCR53c7xx_msg_reject = MESSAGE_REJECT;
  852. hostdata->NCR53c7xx_msg_abort = ABORT;
  853. hostdata->NCR53c7xx_msg_nop = NOP;
  854. hostdata->NOP_insn = (DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24;
  855. if (expected_mapping == -1 ||
  856. (hostdata->options & (OPTION_MEMORY_MAPPED)) !=
  857. (expected_mapping & OPTION_MEMORY_MAPPED))
  858. printk ("scsi%d : using %s mapped access\n", host->host_no,
  859. (hostdata->options & OPTION_MEMORY_MAPPED) ? "memory" :
  860. "io");
  861. hostdata->dmode = (hostdata->chip == 700 || hostdata->chip == 70066) ?
  862. DMODE_REG_00 : DMODE_REG_10;
  863. hostdata->istat = ((hostdata->chip / 100) == 8) ?
  864. ISTAT_REG_800 : ISTAT_REG_700;
  865. /* We have to assume that this may be the first access to the chip, so
  866. * we must set EA in DCNTL. */
  867. NCR53c7x0_write8 (DCNTL_REG, DCNTL_10_EA|DCNTL_10_COM);
  868. /* Only the ISTAT register is readable when the NCR is running, so make
  869. sure it's halted. */
  870. ncr_halt(host);
  871. /*
  872. * XXX - the NCR53c700 uses bitfielded registers for SCID, SDID, etc,
  873. * as does the 710 with one bit per SCSI ID. Conversely, the NCR
  874. * uses a normal, 3 bit binary representation of these values.
  875. *
  876. * Get the rest of the NCR documentation, and FIND OUT where the change
  877. * was.
  878. */
  879. #if 0
  880. /* May not be able to do this - chip my not have been set up yet */
  881. tmp = hostdata->this_id_mask = NCR53c7x0_read8(SCID_REG);
  882. for (host->this_id = 0; tmp != 1; tmp >>=1, ++host->this_id);
  883. #else
  884. host->this_id = 7;
  885. #endif
  886. /*
  887. * Note : we should never encounter a board setup for ID0. So,
  888. * if we see ID0, assume that it was uninitialized and set it
  889. * to the industry standard 7.
  890. */
  891. if (!host->this_id) {
  892. printk("scsi%d : initiator ID was %d, changing to 7\n",
  893. host->host_no, host->this_id);
  894. host->this_id = 7;
  895. hostdata->this_id_mask = 1 << 7;
  896. uninitialized = 1;
  897. };
  898. if (expected_id == -1 || host->this_id != expected_id)
  899. printk("scsi%d : using initiator ID %d\n", host->host_no,
  900. host->this_id);
  901. /*
  902. * Save important registers to allow a soft reset.
  903. */
  904. /*
  905. * CTEST7 controls cache snooping, burst mode, and support for
  906. * external differential drivers. This isn't currently used - the
  907. * default value may not be optimal anyway.
  908. * Even worse, it may never have been set up since reset.
  909. */
  910. hostdata->saved_ctest7 = NCR53c7x0_read8(CTEST7_REG) & CTEST7_SAVE;
  911. revision = (NCR53c7x0_read8(CTEST8_REG) & 0xF0) >> 4;
  912. switch (revision) {
  913. case 1: revision = 0; break;
  914. case 2: revision = 1; break;
  915. case 4: revision = 2; break;
  916. case 8: revision = 3; break;
  917. default: revision = 255; break;
  918. }
  919. printk("scsi%d: Revision 0x%x\n",host->host_no,revision);
  920. if ((revision == 0 || revision == 255) && (hostdata->options & (OPTION_SYNCHRONOUS|OPTION_DISCONNECT|OPTION_ALWAYS_SYNCHRONOUS)))
  921. {
  922. printk ("scsi%d: Disabling sync working and disconnect/reselect\n",
  923. host->host_no);
  924. hostdata->options &= ~(OPTION_SYNCHRONOUS|OPTION_DISCONNECT|OPTION_ALWAYS_SYNCHRONOUS);
  925. }
  926. /*
  927. * On NCR53c700 series chips, DCNTL controls the SCSI clock divisor,
  928. * on 800 series chips, it allows for a totem-pole IRQ driver.
  929. * NOTE saved_dcntl currently overwritten in init function.
  930. * The value read here may be garbage anyway, MVME16x board at least
  931. * does not initialise chip if kernel arrived via tftp.
  932. */
  933. hostdata->saved_dcntl = NCR53c7x0_read8(DCNTL_REG);
  934. /*
  935. * DMODE controls DMA burst length, and on 700 series chips,
  936. * 286 mode and bus width
  937. * NOTE: On MVME16x, chip may have been reset, so this could be a
  938. * power-on/reset default value.
  939. */
  940. hostdata->saved_dmode = NCR53c7x0_read8(hostdata->dmode);
  941. /*
  942. * Now that burst length and enabled/disabled status is known,
  943. * clue the user in on it.
  944. */
  945. ccf = clock_to_ccf_710 (expected_clock);
  946. for (i = 0; i < 16; ++i)
  947. hostdata->cmd_allocated[i] = 0;
  948. if (hostdata->init_save_regs)
  949. hostdata->init_save_regs (host);
  950. if (hostdata->init_fixup)
  951. hostdata->init_fixup (host);
  952. if (!the_template) {
  953. the_template = host->hostt;
  954. first_host = host;
  955. }
  956. /*
  957. * Linux SCSI drivers have always been plagued with initialization
  958. * problems - some didn't work with the BIOS disabled since they expected
  959. * initialization from it, some didn't work when the networking code
  960. * was enabled and registers got scrambled, etc.
  961. *
  962. * To avoid problems like this, in the future, we will do a soft
  963. * reset on the SCSI chip, taking it back to a sane state.
  964. */
  965. hostdata->soft_reset (host);
  966. #if 1
  967. hostdata->debug_count_limit = -1;
  968. #else
  969. hostdata->debug_count_limit = 1;
  970. #endif
  971. hostdata->intrs = -1;
  972. hostdata->resets = -1;
  973. memcpy ((void *) hostdata->synchronous_want, (void *) sdtr_message,
  974. sizeof (hostdata->synchronous_want));
  975. NCR53c7x0_driver_init (host);
  976. if (request_irq(host->irq, NCR53c7x0_intr, IRQF_SHARED, "53c7xx", host))
  977. {
  978. printk("scsi%d : IRQ%d not free, detaching\n",
  979. host->host_no, host->irq);
  980. goto err_unregister;
  981. }
  982. if ((hostdata->run_tests && hostdata->run_tests(host) == -1) ||
  983. (hostdata->options & OPTION_DEBUG_TESTS_ONLY)) {
  984. /* XXX Should disable interrupts, etc. here */
  985. goto err_free_irq;
  986. } else {
  987. if (host->io_port) {
  988. host->n_io_port = 128;
  989. if (!request_region (host->io_port, host->n_io_port, "ncr53c7xx"))
  990. goto err_free_irq;
  991. }
  992. }
  993. if (NCR53c7x0_read8 (SBCL_REG) & SBCL_BSY) {
  994. printk ("scsi%d : bus wedge, doing SCSI reset\n", host->host_no);
  995. hard_reset (host);
  996. }
  997. return 0;
  998. err_free_irq:
  999. free_irq(host->irq, NCR53c7x0_intr);
  1000. err_unregister:
  1001. scsi_unregister(host);
  1002. return -1;
  1003. }
  1004. /*
  1005. * Function : int ncr53c7xx_init(struct scsi_host_template *tpnt, int board, int chip,
  1006. * unsigned long base, int io_port, int irq, int dma, long long options,
  1007. * int clock);
  1008. *
  1009. * Purpose : initializes a NCR53c7,8x0 based on base addresses,
  1010. * IRQ, and DMA channel.
  1011. *
  1012. * Inputs : tpnt - Template for this SCSI adapter, board - board level
  1013. * product, chip - 710
  1014. *
  1015. * Returns : 0 on success, -1 on failure.
  1016. *
  1017. */
  1018. int
  1019. ncr53c7xx_init (struct scsi_host_template *tpnt, int board, int chip,
  1020. unsigned long base, int io_port, int irq, int dma,
  1021. long long options, int clock)
  1022. {
  1023. struct Scsi_Host *instance;
  1024. struct NCR53c7x0_hostdata *hostdata;
  1025. char chip_str[80];
  1026. int script_len = 0, dsa_len = 0, size = 0, max_cmd_size = 0,
  1027. schedule_size = 0, ok = 0;
  1028. void *tmp;
  1029. unsigned long page;
  1030. switch (chip) {
  1031. case 710:
  1032. case 770:
  1033. schedule_size = (tpnt->can_queue + 1) * 8 /* JUMP instruction size */;
  1034. script_len = NCR53c7xx_script_len;
  1035. dsa_len = NCR53c7xx_dsa_len;
  1036. options |= OPTION_INTFLY;
  1037. sprintf (chip_str, "NCR53c%d", chip);
  1038. break;
  1039. default:
  1040. printk("scsi-ncr53c7xx : unsupported SCSI chip %d\n", chip);
  1041. return -1;
  1042. }
  1043. printk("scsi-ncr53c7xx : %s at memory 0x%lx, io 0x%x, irq %d",
  1044. chip_str, base, io_port, irq);
  1045. if (dma == DMA_NONE)
  1046. printk("\n");
  1047. else
  1048. printk(", dma %d\n", dma);
  1049. if (options & OPTION_DEBUG_PROBE_ONLY) {
  1050. printk ("scsi-ncr53c7xx : probe only enabled, aborting initialization\n");
  1051. return -1;
  1052. }
  1053. max_cmd_size = sizeof(struct NCR53c7x0_cmd) + dsa_len +
  1054. /* Size of dynamic part of command structure : */
  1055. 2 * /* Worst case : we don't know if we need DATA IN or DATA out */
  1056. ( 2 * /* Current instructions per scatter/gather segment */
  1057. tpnt->sg_tablesize +
  1058. 3 /* Current startup / termination required per phase */
  1059. ) *
  1060. 8 /* Each instruction is eight bytes */;
  1061. /* Allocate fixed part of hostdata, dynamic part to hold appropriate
  1062. SCSI SCRIPT(tm) plus a single, maximum-sized NCR53c7x0_cmd structure.
  1063. We need a NCR53c7x0_cmd structure for scan_scsis() when we are
  1064. not loaded as a module, and when we're loaded as a module, we
  1065. can't use a non-dynamically allocated structure because modules
  1066. are vmalloc()'d, which can allow structures to cross page
  1067. boundaries and breaks our physical/virtual address assumptions
  1068. for DMA.
  1069. So, we stick it past the end of our hostdata structure.
  1070. ASSUMPTION :
  1071. Regardless of how many simultaneous SCSI commands we allow,
  1072. the probe code only executes a _single_ instruction at a time,
  1073. so we only need one here, and don't need to allocate NCR53c7x0_cmd
  1074. structures for each target until we are no longer in scan_scsis
  1075. and kmalloc() has become functional (memory_init() happens
  1076. after all device driver initialization).
  1077. */
  1078. size = sizeof(struct NCR53c7x0_hostdata) + script_len +
  1079. /* Note that alignment will be guaranteed, since we put the command
  1080. allocated at probe time after the fixed-up SCSI script, which
  1081. consists of 32 bit words, aligned on a 32 bit boundary. But
  1082. on a 64bit machine we need 8 byte alignment for hostdata->free, so
  1083. we add in another 4 bytes to take care of potential misalignment
  1084. */
  1085. (sizeof(void *) - sizeof(u32)) + max_cmd_size + schedule_size;
  1086. page = __get_free_pages(GFP_ATOMIC,1);
  1087. if(page==0)
  1088. {
  1089. printk(KERN_ERR "53c7xx: out of memory.\n");
  1090. return -ENOMEM;
  1091. }
  1092. #ifdef FORCE_DSA_ALIGNMENT
  1093. /*
  1094. * 53c710 rev.0 doesn't have an add-with-carry instruction.
  1095. * Ensure we allocate enough memory to force DSA alignment.
  1096. */
  1097. size += 256;
  1098. #endif
  1099. /* Size should be < 8K, so we can fit it in two pages. */
  1100. if (size > 8192) {
  1101. printk(KERN_ERR "53c7xx: hostdata > 8K\n");
  1102. return -1;
  1103. }
  1104. instance = scsi_register (tpnt, 4);
  1105. if (!instance)
  1106. {
  1107. free_page(page);
  1108. return -1;
  1109. }
  1110. instance->hostdata[0] = page;
  1111. memset((void *)instance->hostdata[0], 0, 8192);
  1112. cache_push(virt_to_phys((void *)(instance->hostdata[0])), 8192);
  1113. cache_clear(virt_to_phys((void *)(instance->hostdata[0])), 8192);
  1114. kernel_set_cachemode((void *)instance->hostdata[0], 8192, IOMAP_NOCACHE_SER);
  1115. /* FIXME : if we ever support an ISA NCR53c7xx based board, we
  1116. need to check if the chip is running in a 16 bit mode, and if so
  1117. unregister it if it is past the 16M (0x1000000) mark */
  1118. hostdata = (struct NCR53c7x0_hostdata *)instance->hostdata[0];
  1119. hostdata->size = size;
  1120. hostdata->script_count = script_len / sizeof(u32);
  1121. hostdata->board = board;
  1122. hostdata->chip = chip;
  1123. /*
  1124. * Being memory mapped is more desirable, since
  1125. *
  1126. * - Memory accesses may be faster.
  1127. *
  1128. * - The destination and source address spaces are the same for
  1129. * all instructions, meaning we don't have to twiddle dmode or
  1130. * any other registers.
  1131. *
  1132. * So, we try for memory mapped, and if we don't get it,
  1133. * we go for port mapped, and that failing we tell the user
  1134. * it can't work.
  1135. */
  1136. if (base) {
  1137. instance->base = base;
  1138. /* Check for forced I/O mapping */
  1139. if (!(options & OPTION_IO_MAPPED)) {
  1140. options |= OPTION_MEMORY_MAPPED;
  1141. ok = 1;
  1142. }
  1143. } else {
  1144. options &= ~OPTION_MEMORY_MAPPED;
  1145. }
  1146. if (io_port) {
  1147. instance->io_port = io_port;
  1148. options |= OPTION_IO_MAPPED;
  1149. ok = 1;
  1150. } else {
  1151. options &= ~OPTION_IO_MAPPED;
  1152. }
  1153. if (!ok) {
  1154. printk ("scsi%d : not initializing, no I/O or memory mapping known \n",
  1155. instance->host_no);
  1156. scsi_unregister (instance);
  1157. return -1;
  1158. }
  1159. instance->irq = irq;
  1160. instance->dma_channel = dma;
  1161. hostdata->options = options;
  1162. hostdata->dsa_len = dsa_len;
  1163. hostdata->max_cmd_size = max_cmd_size;
  1164. hostdata->num_cmds = 1;
  1165. hostdata->scsi_clock = clock;
  1166. /* Initialize single command */
  1167. tmp = (hostdata->script + hostdata->script_count);
  1168. #ifdef FORCE_DSA_ALIGNMENT
  1169. {
  1170. void *t = ROUNDUP(tmp, void *);
  1171. if (((u32)t & 0xff) > CmdPageStart)
  1172. t = (void *)((u32)t + 255);
  1173. t = (void *)(((u32)t & ~0xff) + CmdPageStart);
  1174. hostdata->free = t;
  1175. #if 0
  1176. printk ("scsi: Registered size increased by 256 to %d\n", size);
  1177. printk ("scsi: CmdPageStart = 0x%02x\n", CmdPageStart);
  1178. printk ("scsi: tmp = 0x%08x, hostdata->free set to 0x%08x\n",
  1179. (u32)tmp, (u32)t);
  1180. #endif
  1181. }
  1182. #else
  1183. hostdata->free = ROUNDUP(tmp, void *);
  1184. #endif
  1185. hostdata->free->real = tmp;
  1186. hostdata->free->size = max_cmd_size;
  1187. hostdata->free->free = NULL;
  1188. hostdata->free->next = NULL;
  1189. hostdata->extra_allocate = 0;
  1190. /* Allocate command start code space */
  1191. hostdata->schedule = (chip == 700 || chip == 70066) ?
  1192. NULL : (u32 *) ((char *)hostdata->free + max_cmd_size);
  1193. /*
  1194. * For diagnostic purposes, we don't really care how fast things blaze.
  1195. * For profiling, we want to access the 800ns resolution system clock,
  1196. * using a 'C' call on the host processor.
  1197. *
  1198. * Therefore, there's no need for the NCR chip to directly manipulate
  1199. * this data, and we should put it wherever is most convenient for
  1200. * Linux.
  1201. */
  1202. if (track_events)
  1203. hostdata->events = (struct NCR53c7x0_event *) (track_events ?
  1204. vmalloc (sizeof (struct NCR53c7x0_event) * track_events) : NULL);
  1205. else
  1206. hostdata->events = NULL;
  1207. if (hostdata->events) {
  1208. memset ((void *) hostdata->events, 0, sizeof(struct NCR53c7x0_event) *
  1209. track_events);
  1210. hostdata->event_size = track_events;
  1211. hostdata->event_index = 0;
  1212. } else
  1213. hostdata->event_size = 0;
  1214. return NCR53c7x0_init(instance);
  1215. }
  1216. /*
  1217. * Function : static void NCR53c7x0_init_fixup (struct Scsi_Host *host)
  1218. *
  1219. * Purpose : copy and fixup the SCSI SCRIPTS(tm) code for this device.
  1220. *
  1221. * Inputs : host - pointer to this host adapter's structure
  1222. *
  1223. */
  1224. static void
  1225. NCR53c7x0_init_fixup (struct Scsi_Host *host) {
  1226. NCR53c7x0_local_declare();
  1227. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1228. host->hostdata[0];
  1229. unsigned char tmp;
  1230. int i, ncr_to_memory, memory_to_ncr;
  1231. u32 base;
  1232. NCR53c7x0_local_setup(host);
  1233. /* XXX - NOTE : this code MUST be made endian aware */
  1234. /* Copy code into buffer that was allocated at detection time. */
  1235. memcpy ((void *) hostdata->script, (void *) SCRIPT,
  1236. sizeof(SCRIPT));
  1237. /* Fixup labels */
  1238. for (i = 0; i < PATCHES; ++i)
  1239. hostdata->script[LABELPATCHES[i]] +=
  1240. virt_to_bus(hostdata->script);
  1241. /* Fixup addresses of constants that used to be EXTERNAL */
  1242. patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_abort,
  1243. virt_to_bus(&(hostdata->NCR53c7xx_msg_abort)));
  1244. patch_abs_32 (hostdata->script, 0, NCR53c7xx_msg_reject,
  1245. virt_to_bus(&(hostdata->NCR53c7xx_msg_reject)));
  1246. patch_abs_32 (hostdata->script, 0, NCR53c7xx_zero,
  1247. virt_to_bus(&(hostdata->NCR53c7xx_zero)));
  1248. patch_abs_32 (hostdata->script, 0, NCR53c7xx_sink,
  1249. virt_to_bus(&(hostdata->NCR53c7xx_sink)));
  1250. patch_abs_32 (hostdata->script, 0, NOP_insn,
  1251. virt_to_bus(&(hostdata->NOP_insn)));
  1252. patch_abs_32 (hostdata->script, 0, schedule,
  1253. virt_to_bus((void *) hostdata->schedule));
  1254. /* Fixup references to external variables: */
  1255. for (i = 0; i < EXTERNAL_PATCHES_LEN; ++i)
  1256. hostdata->script[EXTERNAL_PATCHES[i].offset] +=
  1257. virt_to_bus(EXTERNAL_PATCHES[i].address);
  1258. /*
  1259. * Fixup absolutes set at boot-time.
  1260. *
  1261. * All non-code absolute variables suffixed with "dsa_" and "int_"
  1262. * are constants, and need no fixup provided the assembler has done
  1263. * it for us (I don't know what the "real" NCR assembler does in
  1264. * this case, my assembler does the right magic).
  1265. */
  1266. patch_abs_rwri_data (hostdata->script, 0, dsa_save_data_pointer,
  1267. Ent_dsa_code_save_data_pointer - Ent_dsa_zero);
  1268. patch_abs_rwri_data (hostdata->script, 0, dsa_restore_pointers,
  1269. Ent_dsa_code_restore_pointers - Ent_dsa_zero);
  1270. patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
  1271. Ent_dsa_code_check_reselect - Ent_dsa_zero);
  1272. /*
  1273. * Just for the hell of it, preserve the settings of
  1274. * Burst Length and Enable Read Line bits from the DMODE
  1275. * register. Make sure SCRIPTS start automagically.
  1276. */
  1277. #if defined(CONFIG_MVME16x) || defined(CONFIG_BVME6000)
  1278. /* We know better what we want than 16xBug does! */
  1279. tmp = DMODE_10_BL_8 | DMODE_10_FC2;
  1280. #else
  1281. tmp = NCR53c7x0_read8(DMODE_REG_10);
  1282. tmp &= (DMODE_BL_MASK | DMODE_10_FC2 | DMODE_10_FC1 | DMODE_710_PD |
  1283. DMODE_710_UO);
  1284. #endif
  1285. if (!(hostdata->options & OPTION_MEMORY_MAPPED)) {
  1286. base = (u32) host->io_port;
  1287. memory_to_ncr = tmp|DMODE_800_DIOM;
  1288. ncr_to_memory = tmp|DMODE_800_SIOM;
  1289. } else {
  1290. base = virt_to_bus((void *)host->base);
  1291. memory_to_ncr = ncr_to_memory = tmp;
  1292. }
  1293. /* SCRATCHB_REG_10 == SCRATCHA_REG_800, as it happens */
  1294. patch_abs_32 (hostdata->script, 0, addr_scratch, base + SCRATCHA_REG_800);
  1295. patch_abs_32 (hostdata->script, 0, addr_temp, base + TEMP_REG);
  1296. patch_abs_32 (hostdata->script, 0, addr_dsa, base + DSA_REG);
  1297. /*
  1298. * I needed some variables in the script to be accessible to
  1299. * both the NCR chip and the host processor. For these variables,
  1300. * I made the arbitrary decision to store them directly in the
  1301. * hostdata structure rather than in the RELATIVE area of the
  1302. * SCRIPTS.
  1303. */
  1304. patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_memory, tmp);
  1305. patch_abs_rwri_data (hostdata->script, 0, dmode_memory_to_ncr, memory_to_ncr);
  1306. patch_abs_rwri_data (hostdata->script, 0, dmode_ncr_to_memory, ncr_to_memory);
  1307. patch_abs_32 (hostdata->script, 0, msg_buf,
  1308. virt_to_bus((void *)&(hostdata->msg_buf)));
  1309. patch_abs_32 (hostdata->script, 0, reconnect_dsa_head,
  1310. virt_to_bus((void *)&(hostdata->reconnect_dsa_head)));
  1311. patch_abs_32 (hostdata->script, 0, addr_reconnect_dsa_head,
  1312. virt_to_bus((void *)&(hostdata->addr_reconnect_dsa_head)));
  1313. patch_abs_32 (hostdata->script, 0, reselected_identify,
  1314. virt_to_bus((void *)&(hostdata->reselected_identify)));
  1315. /* reselected_tag is currently unused */
  1316. #if 0
  1317. patch_abs_32 (hostdata->script, 0, reselected_tag,
  1318. virt_to_bus((void *)&(hostdata->reselected_tag)));
  1319. #endif
  1320. patch_abs_32 (hostdata->script, 0, test_dest,
  1321. virt_to_bus((void*)&hostdata->test_dest));
  1322. patch_abs_32 (hostdata->script, 0, test_src,
  1323. virt_to_bus(&hostdata->test_source));
  1324. patch_abs_32 (hostdata->script, 0, saved_dsa,
  1325. virt_to_bus((void *)&hostdata->saved2_dsa));
  1326. patch_abs_32 (hostdata->script, 0, emulfly,
  1327. virt_to_bus((void *)&hostdata->emulated_intfly));
  1328. patch_abs_rwri_data (hostdata->script, 0, dsa_check_reselect,
  1329. (unsigned char)(Ent_dsa_code_check_reselect - Ent_dsa_zero));
  1330. /* These are for event logging; the ncr_event enum contains the
  1331. actual interrupt numbers. */
  1332. #ifdef A_int_EVENT_SELECT
  1333. patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT, (u32) EVENT_SELECT);
  1334. #endif
  1335. #ifdef A_int_EVENT_DISCONNECT
  1336. patch_abs_32 (hostdata->script, 0, int_EVENT_DISCONNECT, (u32) EVENT_DISCONNECT);
  1337. #endif
  1338. #ifdef A_int_EVENT_RESELECT
  1339. patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT, (u32) EVENT_RESELECT);
  1340. #endif
  1341. #ifdef A_int_EVENT_COMPLETE
  1342. patch_abs_32 (hostdata->script, 0, int_EVENT_COMPLETE, (u32) EVENT_COMPLETE);
  1343. #endif
  1344. #ifdef A_int_EVENT_IDLE
  1345. patch_abs_32 (hostdata->script, 0, int_EVENT_IDLE, (u32) EVENT_IDLE);
  1346. #endif
  1347. #ifdef A_int_EVENT_SELECT_FAILED
  1348. patch_abs_32 (hostdata->script, 0, int_EVENT_SELECT_FAILED,
  1349. (u32) EVENT_SELECT_FAILED);
  1350. #endif
  1351. #ifdef A_int_EVENT_BEFORE_SELECT
  1352. patch_abs_32 (hostdata->script, 0, int_EVENT_BEFORE_SELECT,
  1353. (u32) EVENT_BEFORE_SELECT);
  1354. #endif
  1355. #ifdef A_int_EVENT_RESELECT_FAILED
  1356. patch_abs_32 (hostdata->script, 0, int_EVENT_RESELECT_FAILED,
  1357. (u32) EVENT_RESELECT_FAILED);
  1358. #endif
  1359. /*
  1360. * Make sure the NCR and Linux code agree on the location of
  1361. * certain fields.
  1362. */
  1363. hostdata->E_accept_message = Ent_accept_message;
  1364. hostdata->E_command_complete = Ent_command_complete;
  1365. hostdata->E_cmdout_cmdout = Ent_cmdout_cmdout;
  1366. hostdata->E_data_transfer = Ent_data_transfer;
  1367. hostdata->E_debug_break = Ent_debug_break;
  1368. hostdata->E_dsa_code_template = Ent_dsa_code_template;
  1369. hostdata->E_dsa_code_template_end = Ent_dsa_code_template_end;
  1370. hostdata->E_end_data_transfer = Ent_end_data_transfer;
  1371. hostdata->E_initiator_abort = Ent_initiator_abort;
  1372. hostdata->E_msg_in = Ent_msg_in;
  1373. hostdata->E_other_transfer = Ent_other_transfer;
  1374. hostdata->E_other_in = Ent_other_in;
  1375. hostdata->E_other_out = Ent_other_out;
  1376. hostdata->E_reject_message = Ent_reject_message;
  1377. hostdata->E_respond_message = Ent_respond_message;
  1378. hostdata->E_select = Ent_select;
  1379. hostdata->E_select_msgout = Ent_select_msgout;
  1380. hostdata->E_target_abort = Ent_target_abort;
  1381. #ifdef Ent_test_0
  1382. hostdata->E_test_0 = Ent_test_0;
  1383. #endif
  1384. hostdata->E_test_1 = Ent_test_1;
  1385. hostdata->E_test_2 = Ent_test_2;
  1386. #ifdef Ent_test_3
  1387. hostdata->E_test_3 = Ent_test_3;
  1388. #endif
  1389. hostdata->E_wait_reselect = Ent_wait_reselect;
  1390. hostdata->E_dsa_code_begin = Ent_dsa_code_begin;
  1391. hostdata->dsa_cmdout = A_dsa_cmdout;
  1392. hostdata->dsa_cmnd = A_dsa_cmnd;
  1393. hostdata->dsa_datain = A_dsa_datain;
  1394. hostdata->dsa_dataout = A_dsa_dataout;
  1395. hostdata->dsa_end = A_dsa_end;
  1396. hostdata->dsa_msgin = A_dsa_msgin;
  1397. hostdata->dsa_msgout = A_dsa_msgout;
  1398. hostdata->dsa_msgout_other = A_dsa_msgout_other;
  1399. hostdata->dsa_next = A_dsa_next;
  1400. hostdata->dsa_select = A_dsa_select;
  1401. hostdata->dsa_start = Ent_dsa_code_template - Ent_dsa_zero;
  1402. hostdata->dsa_status = A_dsa_status;
  1403. hostdata->dsa_jump_dest = Ent_dsa_code_fix_jump - Ent_dsa_zero +
  1404. 8 /* destination operand */;
  1405. /* sanity check */
  1406. if (A_dsa_fields_start != Ent_dsa_code_template_end -
  1407. Ent_dsa_zero)
  1408. printk("scsi%d : NCR dsa_fields start is %d not %d\n",
  1409. host->host_no, A_dsa_fields_start, Ent_dsa_code_template_end -
  1410. Ent_dsa_zero);
  1411. printk("scsi%d : NCR code relocated to 0x%lx (virt 0x%p)\n", host->host_no,
  1412. virt_to_bus(hostdata->script), hostdata->script);
  1413. }
  1414. /*
  1415. * Function : static int NCR53c7xx_run_tests (struct Scsi_Host *host)
  1416. *
  1417. * Purpose : run various verification tests on the NCR chip,
  1418. * including interrupt generation, and proper bus mastering
  1419. * operation.
  1420. *
  1421. * Inputs : host - a properly initialized Scsi_Host structure
  1422. *
  1423. * Preconditions : the NCR chip must be in a halted state.
  1424. *
  1425. * Returns : 0 if all tests were successful, -1 on error.
  1426. *
  1427. */
  1428. static int
  1429. NCR53c7xx_run_tests (struct Scsi_Host *host) {
  1430. NCR53c7x0_local_declare();
  1431. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1432. host->hostdata[0];
  1433. unsigned long timeout;
  1434. u32 start;
  1435. int failed, i;
  1436. unsigned long flags;
  1437. NCR53c7x0_local_setup(host);
  1438. /* The NCR chip _must_ be idle to run the test scripts */
  1439. local_irq_save(flags);
  1440. if (!hostdata->idle) {
  1441. printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
  1442. local_irq_restore(flags);
  1443. return -1;
  1444. }
  1445. /*
  1446. * Check for functional interrupts, this could work as an
  1447. * autoprobe routine.
  1448. */
  1449. if ((hostdata->options & OPTION_DEBUG_TEST1) &&
  1450. hostdata->state != STATE_DISABLED) {
  1451. hostdata->idle = 0;
  1452. hostdata->test_running = 1;
  1453. hostdata->test_completed = -1;
  1454. hostdata->test_dest = 0;
  1455. hostdata->test_source = 0xdeadbeef;
  1456. start = virt_to_bus (hostdata->script) + hostdata->E_test_1;
  1457. hostdata->state = STATE_RUNNING;
  1458. printk ("scsi%d : test 1", host->host_no);
  1459. NCR53c7x0_write32 (DSP_REG, start);
  1460. if (hostdata->options & OPTION_DEBUG_TRACE)
  1461. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl | DCNTL_SSM |
  1462. DCNTL_STD);
  1463. printk (" started\n");
  1464. local_irq_restore(flags);
  1465. /*
  1466. * This is currently a .5 second timeout, since (in theory) no slow
  1467. * board will take that long. In practice, we've seen one
  1468. * pentium which occassionally fails with this, but works with
  1469. * 10 times as much?
  1470. */
  1471. timeout = jiffies + 5 * HZ / 10;
  1472. while ((hostdata->test_completed == -1) && time_before(jiffies, timeout))
  1473. barrier();
  1474. failed = 1;
  1475. if (hostdata->test_completed == -1)
  1476. printk ("scsi%d : driver test 1 timed out%s\n",host->host_no ,
  1477. (hostdata->test_dest == 0xdeadbeef) ?
  1478. " due to lost interrupt.\n"
  1479. " Please verify that the correct IRQ is being used for your board,\n"
  1480. : "");
  1481. else if (hostdata->test_completed != 1)
  1482. printk ("scsi%d : test 1 bad interrupt value (%d)\n",
  1483. host->host_no, hostdata->test_completed);
  1484. else
  1485. failed = (hostdata->test_dest != 0xdeadbeef);
  1486. if (hostdata->test_dest != 0xdeadbeef) {
  1487. printk ("scsi%d : driver test 1 read 0x%x instead of 0xdeadbeef indicating a\n"
  1488. " probable cache invalidation problem. Please configure caching\n"
  1489. " as write-through or disabled\n",
  1490. host->host_no, hostdata->test_dest);
  1491. }
  1492. if (failed) {
  1493. printk ("scsi%d : DSP = 0x%p (script at 0x%p, start at 0x%x)\n",
  1494. host->host_no, bus_to_virt(NCR53c7x0_read32(DSP_REG)),
  1495. hostdata->script, start);
  1496. printk ("scsi%d : DSPS = 0x%x\n", host->host_no,
  1497. NCR53c7x0_read32(DSPS_REG));
  1498. local_irq_restore(flags);
  1499. return -1;
  1500. }
  1501. hostdata->test_running = 0;
  1502. }
  1503. if ((hostdata->options & OPTION_DEBUG_TEST2) &&
  1504. hostdata->state != STATE_DISABLED) {
  1505. u32 dsa[48];
  1506. unsigned char identify = IDENTIFY(0, 0);
  1507. unsigned char cmd[6];
  1508. unsigned char data[36];
  1509. unsigned char status = 0xff;
  1510. unsigned char msg = 0xff;
  1511. cmd[0] = INQUIRY;
  1512. cmd[1] = cmd[2] = cmd[3] = cmd[5] = 0;
  1513. cmd[4] = sizeof(data);
  1514. dsa[2] = 1;
  1515. dsa[3] = virt_to_bus(&identify);
  1516. dsa[4] = 6;
  1517. dsa[5] = virt_to_bus(&cmd);
  1518. dsa[6] = sizeof(data);
  1519. dsa[7] = virt_to_bus(&data);
  1520. dsa[8] = 1;
  1521. dsa[9] = virt_to_bus(&status);
  1522. dsa[10] = 1;
  1523. dsa[11] = virt_to_bus(&msg);
  1524. for (i = 0; i < 6; ++i) {
  1525. #ifdef VALID_IDS
  1526. if (!hostdata->valid_ids[i])
  1527. continue;
  1528. #endif
  1529. local_irq_disable();
  1530. if (!hostdata->idle) {
  1531. printk ("scsi%d : chip not idle, aborting tests\n", host->host_no);
  1532. local_irq_restore(flags);
  1533. return -1;
  1534. }
  1535. /* 710: bit mapped scsi ID, async */
  1536. dsa[0] = (1 << i) << 16;
  1537. hostdata->idle = 0;
  1538. hostdata->test_running = 2;
  1539. hostdata->test_completed = -1;
  1540. start = virt_to_bus(hostdata->script) + hostdata->E_test_2;
  1541. hostdata->state = STATE_RUNNING;
  1542. NCR53c7x0_write32 (DSA_REG, virt_to_bus(dsa));
  1543. NCR53c7x0_write32 (DSP_REG, start);
  1544. if (hostdata->options & OPTION_DEBUG_TRACE)
  1545. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl |
  1546. DCNTL_SSM | DCNTL_STD);
  1547. local_irq_restore(flags);
  1548. timeout = jiffies + 5 * HZ; /* arbitrary */
  1549. while ((hostdata->test_completed == -1) && time_before(jiffies, timeout))
  1550. barrier();
  1551. NCR53c7x0_write32 (DSA_REG, 0);
  1552. if (hostdata->test_completed == 2) {
  1553. data[35] = 0;
  1554. printk ("scsi%d : test 2 INQUIRY to target %d, lun 0 : %s\n",
  1555. host->host_no, i, data + 8);
  1556. printk ("scsi%d : status ", host->host_no);
  1557. scsi_print_status (status);
  1558. printk ("\nscsi%d : message ", host->host_no);
  1559. spi_print_msg(&msg);
  1560. printk ("\n");
  1561. } else if (hostdata->test_completed == 3) {
  1562. printk("scsi%d : test 2 no connection with target %d\n",
  1563. host->host_no, i);
  1564. if (!hostdata->idle) {
  1565. printk("scsi%d : not idle\n", host->host_no);
  1566. local_irq_restore(flags);
  1567. return -1;
  1568. }
  1569. } else if (hostdata->test_completed == -1) {
  1570. printk ("scsi%d : test 2 timed out\n", host->host_no);
  1571. local_irq_restore(flags);
  1572. return -1;
  1573. }
  1574. hostdata->test_running = 0;
  1575. }
  1576. }
  1577. local_irq_restore(flags);
  1578. return 0;
  1579. }
  1580. /*
  1581. * Function : static void NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd)
  1582. *
  1583. * Purpose : copy the NCR53c8xx dsa structure into cmd's dsa buffer,
  1584. * performing all necessary relocation.
  1585. *
  1586. * Inputs : cmd, a NCR53c7x0_cmd structure with a dsa area large
  1587. * enough to hold the NCR53c8xx dsa.
  1588. */
  1589. static void
  1590. NCR53c7xx_dsa_fixup (struct NCR53c7x0_cmd *cmd) {
  1591. Scsi_Cmnd *c = cmd->cmd;
  1592. struct Scsi_Host *host = c->device->host;
  1593. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1594. host->hostdata[0];
  1595. int i;
  1596. memcpy (cmd->dsa, hostdata->script + (hostdata->E_dsa_code_template / 4),
  1597. hostdata->E_dsa_code_template_end - hostdata->E_dsa_code_template);
  1598. /*
  1599. * Note : within the NCR 'C' code, dsa points to the _start_
  1600. * of the DSA structure, and _not_ the offset of dsa_zero within
  1601. * that structure used to facilitate shorter signed offsets
  1602. * for the 8 bit ALU.
  1603. *
  1604. * The implications of this are that
  1605. *
  1606. * - 32 bit A_dsa_* absolute values require an additional
  1607. * dsa_zero added to their value to be correct, since they are
  1608. * relative to dsa_zero which is in essentially a separate
  1609. * space from the code symbols.
  1610. *
  1611. * - All other symbols require no special treatment.
  1612. */
  1613. patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1614. dsa_temp_lun, c->device->lun);
  1615. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1616. dsa_temp_addr_next, virt_to_bus(&cmd->dsa_next_addr));
  1617. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1618. dsa_temp_next, virt_to_bus(cmd->dsa) + Ent_dsa_zero -
  1619. Ent_dsa_code_template + A_dsa_next);
  1620. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1621. dsa_temp_sync, virt_to_bus((void *)hostdata->sync[c->device->id].script));
  1622. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1623. dsa_sscf_710, virt_to_bus((void *)&hostdata->sync[c->device->id].sscf_710));
  1624. patch_abs_tci_data (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1625. dsa_temp_target, 1 << c->device->id);
  1626. /* XXX - new pointer stuff */
  1627. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1628. dsa_temp_addr_saved_pointer, virt_to_bus(&cmd->saved_data_pointer));
  1629. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1630. dsa_temp_addr_saved_residual, virt_to_bus(&cmd->saved_residual));
  1631. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1632. dsa_temp_addr_residual, virt_to_bus(&cmd->residual));
  1633. /* XXX - new start stuff */
  1634. patch_abs_32 (cmd->dsa, Ent_dsa_code_template / sizeof(u32),
  1635. dsa_temp_addr_dsa_value, virt_to_bus(&cmd->dsa_addr));
  1636. }
  1637. /*
  1638. * Function : run_process_issue_queue (void)
  1639. *
  1640. * Purpose : insure that the coroutine is running and will process our
  1641. * request. process_issue_queue_running is checked/set here (in an
  1642. * inline function) rather than in process_issue_queue itself to reduce
  1643. * the chances of stack overflow.
  1644. *
  1645. */
  1646. static volatile int process_issue_queue_running = 0;
  1647. static __inline__ void
  1648. run_process_issue_queue(void) {
  1649. unsigned long flags;
  1650. local_irq_save(flags);
  1651. if (!process_issue_queue_running) {
  1652. process_issue_queue_running = 1;
  1653. process_issue_queue(flags);
  1654. /*
  1655. * process_issue_queue_running is cleared in process_issue_queue
  1656. * once it can't do more work, and process_issue_queue exits with
  1657. * interrupts disabled.
  1658. */
  1659. }
  1660. local_irq_restore(flags);
  1661. }
  1662. /*
  1663. * Function : static void abnormal_finished (struct NCR53c7x0_cmd *cmd, int
  1664. * result)
  1665. *
  1666. * Purpose : mark SCSI command as finished, OR'ing the host portion
  1667. * of the result word into the result field of the corresponding
  1668. * Scsi_Cmnd structure, and removing it from the internal queues.
  1669. *
  1670. * Inputs : cmd - command, result - entire result field
  1671. *
  1672. * Preconditions : the NCR chip should be in a halted state when
  1673. * abnormal_finished is run, since it modifies structures which
  1674. * the NCR expects to have exclusive access to.
  1675. */
  1676. static void
  1677. abnormal_finished (struct NCR53c7x0_cmd *cmd, int result) {
  1678. Scsi_Cmnd *c = cmd->cmd;
  1679. struct Scsi_Host *host = c->device->host;
  1680. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1681. host->hostdata[0];
  1682. unsigned long flags;
  1683. int left, found;
  1684. volatile struct NCR53c7x0_cmd * linux_search;
  1685. volatile struct NCR53c7x0_cmd * volatile *linux_prev;
  1686. volatile u32 *ncr_prev, *ncrcurrent, ncr_search;
  1687. #if 0
  1688. printk ("scsi%d: abnormal finished\n", host->host_no);
  1689. #endif
  1690. local_irq_save(flags);
  1691. found = 0;
  1692. /*
  1693. * Traverse the NCR issue array until we find a match or run out
  1694. * of instructions. Instructions in the NCR issue array are
  1695. * either JUMP or NOP instructions, which are 2 words in length.
  1696. */
  1697. for (found = 0, left = host->can_queue, ncrcurrent = hostdata->schedule;
  1698. left > 0; --left, ncrcurrent += 2)
  1699. {
  1700. if (issue_to_cmd (host, hostdata, (u32 *) ncrcurrent) == cmd)
  1701. {
  1702. ncrcurrent[0] = hostdata->NOP_insn;
  1703. ncrcurrent[1] = 0xdeadbeef;
  1704. ++found;
  1705. break;
  1706. }
  1707. }
  1708. /*
  1709. * Traverse the NCR reconnect list of DSA structures until we find
  1710. * a pointer to this dsa or have found too many command structures.
  1711. * We let prev point at the next field of the previous element or
  1712. * head of the list, so we don't do anything different for removing
  1713. * the head element.
  1714. */
  1715. for (left = host->can_queue,
  1716. ncr_search = hostdata->reconnect_dsa_head,
  1717. ncr_prev = &hostdata->reconnect_dsa_head;
  1718. left >= 0 && ncr_search &&
  1719. ((char*)bus_to_virt(ncr_search) + hostdata->dsa_start)
  1720. != (char *) cmd->dsa;
  1721. ncr_prev = (u32*) ((char*)bus_to_virt(ncr_search) +
  1722. hostdata->dsa_next), ncr_search = *ncr_prev, --left);
  1723. if (left < 0)
  1724. printk("scsi%d: loop detected in ncr reconncect list\n",
  1725. host->host_no);
  1726. else if (ncr_search) {
  1727. if (found)
  1728. printk("scsi%d: scsi %ld in ncr issue array and reconnect lists\n",
  1729. host->host_no, c->pid);
  1730. else {
  1731. volatile u32 * next = (u32 *)
  1732. ((char *)bus_to_virt(ncr_search) + hostdata->dsa_next);
  1733. *ncr_prev = *next;
  1734. /* If we're at the tail end of the issue queue, update that pointer too. */
  1735. found = 1;
  1736. }
  1737. }
  1738. /*
  1739. * Traverse the host running list until we find this command or discover
  1740. * we have too many elements, pointing linux_prev at the next field of the
  1741. * linux_previous element or head of the list, search at this element.
  1742. */
  1743. for (left = host->can_queue, linux_search = hostdata->running_list,
  1744. linux_prev = &hostdata->running_list;
  1745. left >= 0 && linux_search && linux_search != cmd;
  1746. linux_prev = &(linux_search->next),
  1747. linux_search = linux_search->next, --left);
  1748. if (left < 0)
  1749. printk ("scsi%d: loop detected in host running list for scsi pid %ld\n",
  1750. host->host_no, c->pid);
  1751. else if (linux_search) {
  1752. *linux_prev = linux_search->next;
  1753. --hostdata->busy[c->device->id][c->device->lun];
  1754. }
  1755. /* Return the NCR command structure to the free list */
  1756. cmd->next = hostdata->free;
  1757. hostdata->free = cmd;
  1758. c->host_scribble = NULL;
  1759. /* And return */
  1760. c->result = result;
  1761. c->scsi_done(c);
  1762. local_irq_restore(flags);
  1763. run_process_issue_queue();
  1764. }
  1765. /*
  1766. * Function : static void intr_break (struct Scsi_Host *host,
  1767. * struct NCR53c7x0_cmd *cmd)
  1768. *
  1769. * Purpose : Handler for breakpoint interrupts from a SCSI script
  1770. *
  1771. * Inputs : host - pointer to this host adapter's structure,
  1772. * cmd - pointer to the command (if any) dsa was pointing
  1773. * to.
  1774. *
  1775. */
  1776. static void
  1777. intr_break (struct Scsi_Host *host, struct
  1778. NCR53c7x0_cmd *cmd) {
  1779. NCR53c7x0_local_declare();
  1780. struct NCR53c7x0_break *bp;
  1781. #if 0
  1782. Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
  1783. #endif
  1784. u32 *dsp;
  1785. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1786. host->hostdata[0];
  1787. unsigned long flags;
  1788. NCR53c7x0_local_setup(host);
  1789. /*
  1790. * Find the break point corresponding to this address, and
  1791. * dump the appropriate debugging information to standard
  1792. * output.
  1793. */
  1794. local_irq_save(flags);
  1795. dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
  1796. for (bp = hostdata->breakpoints; bp && bp->address != dsp;
  1797. bp = bp->next);
  1798. if (!bp)
  1799. panic("scsi%d : break point interrupt from %p with no breakpoint!",
  1800. host->host_no, dsp);
  1801. /*
  1802. * Configure the NCR chip for manual start mode, so that we can
  1803. * point the DSP register at the instruction that follows the
  1804. * INT int_debug_break instruction.
  1805. */
  1806. NCR53c7x0_write8 (hostdata->dmode,
  1807. NCR53c7x0_read8(hostdata->dmode)|DMODE_MAN);
  1808. /*
  1809. * And update the DSP register, using the size of the old
  1810. * instruction in bytes.
  1811. */
  1812. local_irq_restore(flags);
  1813. }
  1814. /*
  1815. * Function : static void print_synchronous (const char *prefix,
  1816. * const unsigned char *msg)
  1817. *
  1818. * Purpose : print a pretty, user and machine parsable representation
  1819. * of a SDTR message, including the "real" parameters, data
  1820. * clock so we can tell transfer rate at a glance.
  1821. *
  1822. * Inputs ; prefix - text to prepend, msg - SDTR message (5 bytes)
  1823. */
  1824. static void
  1825. print_synchronous (const char *prefix, const unsigned char *msg) {
  1826. if (msg[4]) {
  1827. int Hz = 1000000000 / (msg[3] * 4);
  1828. int integer = Hz / 1000000;
  1829. int fraction = (Hz - (integer * 1000000)) / 10000;
  1830. printk ("%speriod %dns offset %d %d.%02dMHz %s SCSI%s\n",
  1831. prefix, (int) msg[3] * 4, (int) msg[4], integer, fraction,
  1832. (((msg[3] * 4) < 200) ? "FAST" : "synchronous"),
  1833. (((msg[3] * 4) < 200) ? "-II" : ""));
  1834. } else
  1835. printk ("%sasynchronous SCSI\n", prefix);
  1836. }
  1837. /*
  1838. * Function : static void set_synchronous (struct Scsi_Host *host,
  1839. * int target, int sxfer, int scntl3, int now_connected)
  1840. *
  1841. * Purpose : reprogram transfers between the selected SCSI initiator and
  1842. * target with the given register values; in the indirect
  1843. * select operand, reselection script, and chip registers.
  1844. *
  1845. * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
  1846. * sxfer and scntl3 - NCR registers. now_connected - if non-zero,
  1847. * we should reprogram the registers now too.
  1848. *
  1849. * NOTE: For 53c710, scntl3 is actually used for SCF bits from
  1850. * SBCL, as we don't have a SCNTL3.
  1851. */
  1852. static void
  1853. set_synchronous (struct Scsi_Host *host, int target, int sxfer, int scntl3,
  1854. int now_connected) {
  1855. NCR53c7x0_local_declare();
  1856. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1857. host->hostdata[0];
  1858. u32 *script;
  1859. NCR53c7x0_local_setup(host);
  1860. /* These are eight bit registers */
  1861. sxfer &= 0xff;
  1862. scntl3 &= 0xff;
  1863. hostdata->sync[target].sxfer_sanity = sxfer;
  1864. hostdata->sync[target].scntl3_sanity = scntl3;
  1865. /*
  1866. * HARD CODED : synchronous script is EIGHT words long. This
  1867. * must agree with 53c7.8xx.h
  1868. */
  1869. if ((hostdata->chip != 700) && (hostdata->chip != 70066)) {
  1870. hostdata->sync[target].select_indirect = (1 << target) << 16 |
  1871. (sxfer << 8);
  1872. hostdata->sync[target].sscf_710 = scntl3;
  1873. script = (u32 *) hostdata->sync[target].script;
  1874. /* XXX - add NCR53c7x0 code to reprogram SCF bits if we want to */
  1875. script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
  1876. DCMD_RWRI_OP_MOVE) << 24) |
  1877. (SBCL_REG << 16) | (scntl3 << 8);
  1878. script[1] = 0;
  1879. script += 2;
  1880. script[0] = ((DCMD_TYPE_RWRI | DCMD_RWRI_OPC_MODIFY |
  1881. DCMD_RWRI_OP_MOVE) << 24) |
  1882. (SXFER_REG << 16) | (sxfer << 8);
  1883. script[1] = 0;
  1884. script += 2;
  1885. #ifdef DEBUG_SYNC_INTR
  1886. if (hostdata->options & OPTION_DEBUG_DISCONNECT) {
  1887. script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_INT) << 24) | DBC_TCI_TRUE;
  1888. script[1] = DEBUG_SYNC_INTR;
  1889. script += 2;
  1890. }
  1891. #endif
  1892. script[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_RETURN) << 24) | DBC_TCI_TRUE;
  1893. script[1] = 0;
  1894. script += 2;
  1895. }
  1896. if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS)
  1897. printk ("scsi%d : target %d sync parameters are sxfer=0x%x, scntl3=0x%x\n",
  1898. host->host_no, target, sxfer, scntl3);
  1899. if (now_connected) {
  1900. NCR53c7x0_write8(SBCL_REG, scntl3);
  1901. NCR53c7x0_write8(SXFER_REG, sxfer);
  1902. }
  1903. }
  1904. /*
  1905. * Function : static int asynchronous (struct Scsi_Host *host, int target)
  1906. *
  1907. * Purpose : reprogram between the selected SCSI Host adapter and target
  1908. * (assumed to be currently connected) for asynchronous transfers.
  1909. *
  1910. * Inputs : host - SCSI host structure, target - numeric target ID.
  1911. *
  1912. * Preconditions : the NCR chip should be in one of the halted states
  1913. */
  1914. static void
  1915. asynchronous (struct Scsi_Host *host, int target) {
  1916. NCR53c7x0_local_declare();
  1917. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1918. host->hostdata[0];
  1919. NCR53c7x0_local_setup(host);
  1920. set_synchronous (host, target, /* no offset */ 0, hostdata->saved_scntl3,
  1921. 1);
  1922. printk ("scsi%d : setting target %d to asynchronous SCSI\n",
  1923. host->host_no, target);
  1924. }
  1925. /*
  1926. * XXX - do we want to go out of our way (ie, add extra code to selection
  1927. * in the NCR53c710/NCR53c720 script) to reprogram the synchronous
  1928. * conversion bits, or can we be content in just setting the
  1929. * sxfer bits? I chose to do so [richard@sleepie.demon.co.uk]
  1930. */
  1931. /* Table for NCR53c8xx synchronous values */
  1932. /* This table is also correct for 710, allowing that scf=4 is equivalent
  1933. * of SSCF=0 (ie use DCNTL, divide by 3) for a 50.01-66.00MHz clock.
  1934. * For any other clock values, we cannot use entries with SCF values of
  1935. * 4. I guess that for a 66MHz clock, the slowest it will set is 2MHz,
  1936. * and for a 50MHz clock, the slowest will be 2.27Mhz. Should check
  1937. * that a device doesn't try and negotiate sync below these limits!
  1938. */
  1939. static const struct {
  1940. int div; /* Total clock divisor * 10 */
  1941. unsigned char scf; /* */
  1942. unsigned char tp; /* 4 + tp = xferp divisor */
  1943. } syncs[] = {
  1944. /* div scf tp div scf tp div scf tp */
  1945. { 40, 1, 0}, { 50, 1, 1}, { 60, 1, 2},
  1946. { 70, 1, 3}, { 75, 2, 1}, { 80, 1, 4},
  1947. { 90, 1, 5}, { 100, 1, 6}, { 105, 2, 3},
  1948. { 110, 1, 7}, { 120, 2, 4}, { 135, 2, 5},
  1949. { 140, 3, 3}, { 150, 2, 6}, { 160, 3, 4},
  1950. { 165, 2, 7}, { 180, 3, 5}, { 200, 3, 6},
  1951. { 210, 4, 3}, { 220, 3, 7}, { 240, 4, 4},
  1952. { 270, 4, 5}, { 300, 4, 6}, { 330, 4, 7}
  1953. };
  1954. /*
  1955. * Function : static void synchronous (struct Scsi_Host *host, int target,
  1956. * char *msg)
  1957. *
  1958. * Purpose : reprogram transfers between the selected SCSI initiator and
  1959. * target for synchronous SCSI transfers such that the synchronous
  1960. * offset is less than that requested and period at least as long
  1961. * as that requested. Also modify *msg such that it contains
  1962. * an appropriate response.
  1963. *
  1964. * Inputs : host - NCR53c7,8xx SCSI host, target - number SCSI target id,
  1965. * msg - synchronous transfer request.
  1966. */
  1967. static void
  1968. synchronous (struct Scsi_Host *host, int target, char *msg) {
  1969. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  1970. host->hostdata[0];
  1971. int desire, divisor, i, limit;
  1972. unsigned char scntl3, sxfer;
  1973. /* The diagnostic message fits on one line, even with max. width integers */
  1974. char buf[80];
  1975. /* Desired transfer clock in Hz */
  1976. desire = 1000000000L / (msg[3] * 4);
  1977. /* Scale the available SCSI clock by 10 so we get tenths */
  1978. divisor = (hostdata->scsi_clock * 10) / desire;
  1979. /* NCR chips can handle at most an offset of 8 */
  1980. if (msg[4] > 8)
  1981. msg[4] = 8;
  1982. if (hostdata->options & OPTION_DEBUG_SDTR)
  1983. printk("scsi%d : optimal synchronous divisor of %d.%01d\n",
  1984. host->host_no, divisor / 10, divisor % 10);
  1985. limit = ARRAY_SIZE(syncs) - 1;
  1986. for (i = 0; (i < limit) && (divisor > syncs[i].div); ++i);
  1987. if (hostdata->options & OPTION_DEBUG_SDTR)
  1988. printk("scsi%d : selected synchronous divisor of %d.%01d\n",
  1989. host->host_no, syncs[i].div / 10, syncs[i].div % 10);
  1990. msg[3] = ((1000000000L / hostdata->scsi_clock) * syncs[i].div / 10 / 4);
  1991. if (hostdata->options & OPTION_DEBUG_SDTR)
  1992. printk("scsi%d : selected synchronous period of %dns\n", host->host_no,
  1993. msg[3] * 4);
  1994. scntl3 = syncs[i].scf;
  1995. sxfer = (msg[4] << SXFER_MO_SHIFT) | (syncs[i].tp << 4);
  1996. if (hostdata->options & OPTION_DEBUG_SDTR)
  1997. printk ("scsi%d : sxfer=0x%x scntl3=0x%x\n",
  1998. host->host_no, (int) sxfer, (int) scntl3);
  1999. set_synchronous (host, target, sxfer, scntl3, 1);
  2000. sprintf (buf, "scsi%d : setting target %d to ", host->host_no, target);
  2001. print_synchronous (buf, msg);
  2002. }
  2003. /*
  2004. * Function : static int NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host,
  2005. * struct NCR53c7x0_cmd *cmd)
  2006. *
  2007. * Purpose : Handler for INT generated instructions for the
  2008. * NCR53c810/820 SCSI SCRIPT
  2009. *
  2010. * Inputs : host - pointer to this host adapter's structure,
  2011. * cmd - pointer to the command (if any) dsa was pointing
  2012. * to.
  2013. *
  2014. */
  2015. static int
  2016. NCR53c7x0_dstat_sir_intr (struct Scsi_Host *host, struct
  2017. NCR53c7x0_cmd *cmd) {
  2018. NCR53c7x0_local_declare();
  2019. int print;
  2020. Scsi_Cmnd *c = cmd ? cmd->cmd : NULL;
  2021. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2022. host->hostdata[0];
  2023. u32 dsps,*dsp; /* Argument of the INT instruction */
  2024. NCR53c7x0_local_setup(host);
  2025. dsps = NCR53c7x0_read32(DSPS_REG);
  2026. dsp = (u32 *) bus_to_virt(NCR53c7x0_read32(DSP_REG));
  2027. /* RGH 150597: Frig. Commands which fail with Check Condition are
  2028. * Flagged as successful - hack dsps to indicate check condition */
  2029. #if 0
  2030. /* RGH 200597: Need to disable for BVME6000, as it gets Check Conditions
  2031. * and then dies. Seems to handle Check Condition at startup, but
  2032. * not mid kernel build. */
  2033. if (dsps == A_int_norm_emulateintfly && cmd && cmd->result == 2)
  2034. dsps = A_int_err_check_condition;
  2035. #endif
  2036. if (hostdata->options & OPTION_DEBUG_INTR)
  2037. printk ("scsi%d : DSPS = 0x%x\n", host->host_no, dsps);
  2038. switch (dsps) {
  2039. case A_int_msg_1:
  2040. print = 1;
  2041. switch (hostdata->msg_buf[0]) {
  2042. /*
  2043. * Unless we've initiated synchronous negotiation, I don't
  2044. * think that this should happen.
  2045. */
  2046. case MESSAGE_REJECT:
  2047. hostdata->dsp = hostdata->script + hostdata->E_accept_message /
  2048. sizeof(u32);
  2049. hostdata->dsp_changed = 1;
  2050. if (cmd && (cmd->flags & CMD_FLAG_SDTR)) {
  2051. printk ("scsi%d : target %d rejected SDTR\n", host->host_no,
  2052. c->device->id);
  2053. cmd->flags &= ~CMD_FLAG_SDTR;
  2054. asynchronous (host, c->device->id);
  2055. print = 0;
  2056. }
  2057. break;
  2058. case INITIATE_RECOVERY:
  2059. printk ("scsi%d : extended contingent allegiance not supported yet, rejecting\n",
  2060. host->host_no);
  2061. /* Fall through to default */
  2062. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2063. sizeof(u32);
  2064. hostdata->dsp_changed = 1;
  2065. break;
  2066. default:
  2067. printk ("scsi%d : unsupported message, rejecting\n",
  2068. host->host_no);
  2069. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2070. sizeof(u32);
  2071. hostdata->dsp_changed = 1;
  2072. }
  2073. if (print) {
  2074. printk ("scsi%d : received message", host->host_no);
  2075. if (c)
  2076. printk (" from target %d lun %d ", c->device->id, c->device->lun);
  2077. spi_print_msg((unsigned char *) hostdata->msg_buf);
  2078. printk("\n");
  2079. }
  2080. return SPECIFIC_INT_NOTHING;
  2081. case A_int_msg_sdtr:
  2082. /*
  2083. * At this point, hostdata->msg_buf contains
  2084. * 0 EXTENDED MESSAGE
  2085. * 1 length
  2086. * 2 SDTR
  2087. * 3 period * 4ns
  2088. * 4 offset
  2089. */
  2090. if (cmd) {
  2091. char buf[80];
  2092. sprintf (buf, "scsi%d : target %d %s ", host->host_no, c->device->id,
  2093. (cmd->flags & CMD_FLAG_SDTR) ? "accepting" : "requesting");
  2094. print_synchronous (buf, (unsigned char *) hostdata->msg_buf);
  2095. /*
  2096. * Initiator initiated, won't happen unless synchronous
  2097. * transfers are enabled. If we get a SDTR message in
  2098. * response to our SDTR, we should program our parameters
  2099. * such that
  2100. * offset <= requested offset
  2101. * period >= requested period
  2102. */
  2103. if (cmd->flags & CMD_FLAG_SDTR) {
  2104. cmd->flags &= ~CMD_FLAG_SDTR;
  2105. if (hostdata->msg_buf[4])
  2106. synchronous (host, c->device->id, (unsigned char *)
  2107. hostdata->msg_buf);
  2108. else
  2109. asynchronous (host, c->device->id);
  2110. hostdata->dsp = hostdata->script + hostdata->E_accept_message /
  2111. sizeof(u32);
  2112. hostdata->dsp_changed = 1;
  2113. return SPECIFIC_INT_NOTHING;
  2114. } else {
  2115. if (hostdata->options & OPTION_SYNCHRONOUS) {
  2116. cmd->flags |= CMD_FLAG_DID_SDTR;
  2117. synchronous (host, c->device->id, (unsigned char *)
  2118. hostdata->msg_buf);
  2119. } else {
  2120. hostdata->msg_buf[4] = 0; /* 0 offset = async */
  2121. asynchronous (host, c->device->id);
  2122. }
  2123. patch_dsa_32 (cmd->dsa, dsa_msgout_other, 0, 5);
  2124. patch_dsa_32 (cmd->dsa, dsa_msgout_other, 1, (u32)
  2125. virt_to_bus ((void *)&hostdata->msg_buf));
  2126. hostdata->dsp = hostdata->script +
  2127. hostdata->E_respond_message / sizeof(u32);
  2128. hostdata->dsp_changed = 1;
  2129. }
  2130. return SPECIFIC_INT_NOTHING;
  2131. }
  2132. /* Fall through to abort if we couldn't find a cmd, and
  2133. therefore a dsa structure to twiddle */
  2134. case A_int_msg_wdtr:
  2135. hostdata->dsp = hostdata->script + hostdata->E_reject_message /
  2136. sizeof(u32);
  2137. hostdata->dsp_changed = 1;
  2138. return SPECIFIC_INT_NOTHING;
  2139. case A_int_err_unexpected_phase:
  2140. if (hostdata->options & OPTION_DEBUG_INTR)
  2141. printk ("scsi%d : unexpected phase\n", host->host_no);
  2142. return SPECIFIC_INT_ABORT;
  2143. case A_int_err_selected:
  2144. if ((hostdata->chip / 100) == 8)
  2145. printk ("scsi%d : selected by target %d\n", host->host_no,
  2146. (int) NCR53c7x0_read8(SDID_REG_800) &7);
  2147. else
  2148. printk ("scsi%d : selected by target LCRC=0x%02x\n", host->host_no,
  2149. (int) NCR53c7x0_read8(LCRC_REG_10));
  2150. hostdata->dsp = hostdata->script + hostdata->E_target_abort /
  2151. sizeof(u32);
  2152. hostdata->dsp_changed = 1;
  2153. return SPECIFIC_INT_NOTHING;
  2154. case A_int_err_unexpected_reselect:
  2155. if ((hostdata->chip / 100) == 8)
  2156. printk ("scsi%d : unexpected reselect by target %d lun %d\n",
  2157. host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & 7,
  2158. hostdata->reselected_identify & 7);
  2159. else
  2160. printk ("scsi%d : unexpected reselect LCRC=0x%02x\n", host->host_no,
  2161. (int) NCR53c7x0_read8(LCRC_REG_10));
  2162. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  2163. sizeof(u32);
  2164. hostdata->dsp_changed = 1;
  2165. return SPECIFIC_INT_NOTHING;
  2166. /*
  2167. * Since contingent allegiance conditions are cleared by the next
  2168. * command issued to a target, we must issue a REQUEST SENSE
  2169. * command after receiving a CHECK CONDITION status, before
  2170. * another command is issued.
  2171. *
  2172. * Since this NCR53c7x0_cmd will be freed after use, we don't
  2173. * care if we step on the various fields, so modify a few things.
  2174. */
  2175. case A_int_err_check_condition:
  2176. #if 0
  2177. if (hostdata->options & OPTION_DEBUG_INTR)
  2178. #endif
  2179. printk ("scsi%d : CHECK CONDITION\n", host->host_no);
  2180. if (!c) {
  2181. printk("scsi%d : CHECK CONDITION with no SCSI command\n",
  2182. host->host_no);
  2183. return SPECIFIC_INT_PANIC;
  2184. }
  2185. /*
  2186. * FIXME : this uses the normal one-byte selection message.
  2187. * We may want to renegotiate for synchronous & WIDE transfers
  2188. * since these could be the crux of our problem.
  2189. *
  2190. hostdata->NOP_insn* FIXME : once SCSI-II tagged queuing is implemented, we'll
  2191. * have to set this up so that the rest of the DSA
  2192. * agrees with this being an untagged queue'd command.
  2193. */
  2194. patch_dsa_32 (cmd->dsa, dsa_msgout, 0, 1);
  2195. /*
  2196. * Modify the table indirect for COMMAND OUT phase, since
  2197. * Request Sense is a six byte command.
  2198. */
  2199. patch_dsa_32 (cmd->dsa, dsa_cmdout, 0, 6);
  2200. /*
  2201. * The CDB is now mirrored in our local non-cached
  2202. * structure, but keep the old structure up to date as well,
  2203. * just in case anyone looks at it.
  2204. */
  2205. /*
  2206. * XXX Need to worry about data buffer alignment/cache state
  2207. * XXX here, but currently never get A_int_err_check_condition,
  2208. * XXX so ignore problem for now.
  2209. */
  2210. cmd->cmnd[0] = c->cmnd[0] = REQUEST_SENSE;
  2211. cmd->cmnd[0] = c->cmnd[1] &= 0xe0; /* Zero all but LUN */
  2212. cmd->cmnd[0] = c->cmnd[2] = 0;
  2213. cmd->cmnd[0] = c->cmnd[3] = 0;
  2214. cmd->cmnd[0] = c->cmnd[4] = sizeof(c->sense_buffer);
  2215. cmd->cmnd[0] = c->cmnd[5] = 0;
  2216. /*
  2217. * Disable dataout phase, and program datain to transfer to the
  2218. * sense buffer, and add a jump to other_transfer after the
  2219. * command so overflow/underrun conditions are detected.
  2220. */
  2221. patch_dsa_32 (cmd->dsa, dsa_dataout, 0,
  2222. virt_to_bus(hostdata->script) + hostdata->E_other_transfer);
  2223. patch_dsa_32 (cmd->dsa, dsa_datain, 0,
  2224. virt_to_bus(cmd->data_transfer_start));
  2225. cmd->data_transfer_start[0] = (((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I |
  2226. DCMD_BMI_IO)) << 24) | sizeof(c->sense_buffer);
  2227. cmd->data_transfer_start[1] = (u32) virt_to_bus(c->sense_buffer);
  2228. cmd->data_transfer_start[2] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP)
  2229. << 24) | DBC_TCI_TRUE;
  2230. cmd->data_transfer_start[3] = (u32) virt_to_bus(hostdata->script) +
  2231. hostdata->E_other_transfer;
  2232. /*
  2233. * Currently, this command is flagged as completed, ie
  2234. * it has valid status and message data. Reflag it as
  2235. * incomplete. Q - need to do something so that original
  2236. * status, etc are used.
  2237. */
  2238. cmd->result = cmd->cmd->result = 0xffff;
  2239. /*
  2240. * Restart command as a REQUEST SENSE.
  2241. */
  2242. hostdata->dsp = (u32 *) hostdata->script + hostdata->E_select /
  2243. sizeof(u32);
  2244. hostdata->dsp_changed = 1;
  2245. return SPECIFIC_INT_NOTHING;
  2246. case A_int_debug_break:
  2247. return SPECIFIC_INT_BREAK;
  2248. case A_int_norm_aborted:
  2249. hostdata->dsp = (u32 *) hostdata->schedule;
  2250. hostdata->dsp_changed = 1;
  2251. if (cmd)
  2252. abnormal_finished (cmd, DID_ERROR << 16);
  2253. return SPECIFIC_INT_NOTHING;
  2254. case A_int_norm_emulateintfly:
  2255. NCR53c7x0_intfly(host);
  2256. return SPECIFIC_INT_NOTHING;
  2257. case A_int_test_1:
  2258. case A_int_test_2:
  2259. hostdata->idle = 1;
  2260. hostdata->test_completed = (dsps - A_int_test_1) / 0x00010000 + 1;
  2261. if (hostdata->options & OPTION_DEBUG_INTR)
  2262. printk("scsi%d : test%d complete\n", host->host_no,
  2263. hostdata->test_completed);
  2264. return SPECIFIC_INT_NOTHING;
  2265. #ifdef A_int_debug_reselected_ok
  2266. case A_int_debug_reselected_ok:
  2267. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2268. OPTION_DEBUG_DISCONNECT)) {
  2269. /*
  2270. * Note - this dsa is not based on location relative to
  2271. * the command structure, but to location relative to the
  2272. * DSA register
  2273. */
  2274. u32 *dsa;
  2275. dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2276. printk("scsi%d : reselected_ok (DSA = 0x%x (virt 0x%p)\n",
  2277. host->host_no, NCR53c7x0_read32(DSA_REG), dsa);
  2278. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
  2279. host->host_no, cmd->saved_data_pointer,
  2280. bus_to_virt(cmd->saved_data_pointer));
  2281. print_insn (host, hostdata->script + Ent_reselected_ok /
  2282. sizeof(u32), "", 1);
  2283. if ((hostdata->chip / 100) == 8)
  2284. printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
  2285. host->host_no, NCR53c7x0_read8(SXFER_REG),
  2286. NCR53c7x0_read8(SCNTL3_REG_800));
  2287. else
  2288. printk ("scsi%d : sxfer=0x%x, cannot read SBCL\n",
  2289. host->host_no, NCR53c7x0_read8(SXFER_REG));
  2290. if (c) {
  2291. print_insn (host, (u32 *)
  2292. hostdata->sync[c->device->id].script, "", 1);
  2293. print_insn (host, (u32 *)
  2294. hostdata->sync[c->device->id].script + 2, "", 1);
  2295. }
  2296. }
  2297. return SPECIFIC_INT_RESTART;
  2298. #endif
  2299. #ifdef A_int_debug_reselect_check
  2300. case A_int_debug_reselect_check:
  2301. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2302. u32 *dsa;
  2303. #if 0
  2304. u32 *code;
  2305. #endif
  2306. /*
  2307. * Note - this dsa is not based on location relative to
  2308. * the command structure, but to location relative to the
  2309. * DSA register
  2310. */
  2311. dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2312. printk("scsi%d : reselected_check_next (DSA = 0x%lx (virt 0x%p))\n",
  2313. host->host_no, virt_to_bus(dsa), dsa);
  2314. if (dsa) {
  2315. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n",
  2316. host->host_no, cmd->saved_data_pointer,
  2317. bus_to_virt (cmd->saved_data_pointer));
  2318. #if 0
  2319. printk("scsi%d : template code :\n", host->host_no);
  2320. for (code = dsa + (Ent_dsa_code_check_reselect - Ent_dsa_zero)
  2321. / sizeof(u32); code < (dsa + Ent_dsa_zero / sizeof(u32));
  2322. code += print_insn (host, code, "", 1));
  2323. #endif
  2324. }
  2325. print_insn (host, hostdata->script + Ent_reselected_ok /
  2326. sizeof(u32), "", 1);
  2327. }
  2328. return SPECIFIC_INT_RESTART;
  2329. #endif
  2330. #ifdef A_int_debug_dsa_schedule
  2331. case A_int_debug_dsa_schedule:
  2332. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2333. u32 *dsa;
  2334. /*
  2335. * Note - this dsa is not based on location relative to
  2336. * the command structure, but to location relative to the
  2337. * DSA register
  2338. */
  2339. dsa = (u32 *) bus_to_virt (NCR53c7x0_read32(DSA_REG));
  2340. printk("scsi%d : dsa_schedule (old DSA = 0x%lx (virt 0x%p))\n",
  2341. host->host_no, virt_to_bus(dsa), dsa);
  2342. if (dsa)
  2343. printk("scsi%d : resume address is 0x%x (virt 0x%p)\n"
  2344. " (temp was 0x%x (virt 0x%p))\n",
  2345. host->host_no, cmd->saved_data_pointer,
  2346. bus_to_virt (cmd->saved_data_pointer),
  2347. NCR53c7x0_read32 (TEMP_REG),
  2348. bus_to_virt (NCR53c7x0_read32(TEMP_REG)));
  2349. }
  2350. return SPECIFIC_INT_RESTART;
  2351. #endif
  2352. #ifdef A_int_debug_scheduled
  2353. case A_int_debug_scheduled:
  2354. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2355. printk("scsi%d : new I/O 0x%x (virt 0x%p) scheduled\n",
  2356. host->host_no, NCR53c7x0_read32(DSA_REG),
  2357. bus_to_virt(NCR53c7x0_read32(DSA_REG)));
  2358. }
  2359. return SPECIFIC_INT_RESTART;
  2360. #endif
  2361. #ifdef A_int_debug_idle
  2362. case A_int_debug_idle:
  2363. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2364. printk("scsi%d : idle\n", host->host_no);
  2365. }
  2366. return SPECIFIC_INT_RESTART;
  2367. #endif
  2368. #ifdef A_int_debug_cmd
  2369. case A_int_debug_cmd:
  2370. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2371. printk("scsi%d : command sent\n");
  2372. }
  2373. return SPECIFIC_INT_RESTART;
  2374. #endif
  2375. #ifdef A_int_debug_dsa_loaded
  2376. case A_int_debug_dsa_loaded:
  2377. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2378. printk("scsi%d : DSA loaded with 0x%x (virt 0x%p)\n", host->host_no,
  2379. NCR53c7x0_read32(DSA_REG),
  2380. bus_to_virt(NCR53c7x0_read32(DSA_REG)));
  2381. }
  2382. return SPECIFIC_INT_RESTART;
  2383. #endif
  2384. #ifdef A_int_debug_reselected
  2385. case A_int_debug_reselected:
  2386. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2387. OPTION_DEBUG_DISCONNECT)) {
  2388. if ((hostdata->chip / 100) == 8)
  2389. printk("scsi%d : reselected by target %d lun %d\n",
  2390. host->host_no, (int) NCR53c7x0_read8(SDID_REG_800) & ~0x80,
  2391. (int) hostdata->reselected_identify & 7);
  2392. else
  2393. printk("scsi%d : reselected by LCRC=0x%02x lun %d\n",
  2394. host->host_no, (int) NCR53c7x0_read8(LCRC_REG_10),
  2395. (int) hostdata->reselected_identify & 7);
  2396. print_queues(host);
  2397. }
  2398. return SPECIFIC_INT_RESTART;
  2399. #endif
  2400. #ifdef A_int_debug_disconnect_msg
  2401. case A_int_debug_disconnect_msg:
  2402. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR)) {
  2403. if (c)
  2404. printk("scsi%d : target %d lun %d disconnecting\n",
  2405. host->host_no, c->device->id, c->device->lun);
  2406. else
  2407. printk("scsi%d : unknown target disconnecting\n",
  2408. host->host_no);
  2409. }
  2410. return SPECIFIC_INT_RESTART;
  2411. #endif
  2412. #ifdef A_int_debug_disconnected
  2413. case A_int_debug_disconnected:
  2414. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2415. OPTION_DEBUG_DISCONNECT)) {
  2416. printk ("scsi%d : disconnected, new queues are\n",
  2417. host->host_no);
  2418. print_queues(host);
  2419. #if 0
  2420. /* Not valid on ncr53c710! */
  2421. printk ("scsi%d : sxfer=0x%x, scntl3=0x%x\n",
  2422. host->host_no, NCR53c7x0_read8(SXFER_REG),
  2423. NCR53c7x0_read8(SCNTL3_REG_800));
  2424. #endif
  2425. if (c) {
  2426. print_insn (host, (u32 *)
  2427. hostdata->sync[c->device->id].script, "", 1);
  2428. print_insn (host, (u32 *)
  2429. hostdata->sync[c->device->id].script + 2, "", 1);
  2430. }
  2431. }
  2432. return SPECIFIC_INT_RESTART;
  2433. #endif
  2434. #ifdef A_int_debug_panic
  2435. case A_int_debug_panic:
  2436. printk("scsi%d : int_debug_panic received\n", host->host_no);
  2437. print_lots (host);
  2438. return SPECIFIC_INT_PANIC;
  2439. #endif
  2440. #ifdef A_int_debug_saved
  2441. case A_int_debug_saved:
  2442. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2443. OPTION_DEBUG_DISCONNECT)) {
  2444. printk ("scsi%d : saved data pointer 0x%x (virt 0x%p)\n",
  2445. host->host_no, cmd->saved_data_pointer,
  2446. bus_to_virt (cmd->saved_data_pointer));
  2447. print_progress (c);
  2448. }
  2449. return SPECIFIC_INT_RESTART;
  2450. #endif
  2451. #ifdef A_int_debug_restored
  2452. case A_int_debug_restored:
  2453. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2454. OPTION_DEBUG_DISCONNECT)) {
  2455. if (cmd) {
  2456. int size;
  2457. printk ("scsi%d : restored data pointer 0x%x (virt 0x%p)\n",
  2458. host->host_no, cmd->saved_data_pointer, bus_to_virt (
  2459. cmd->saved_data_pointer));
  2460. size = print_insn (host, (u32 *)
  2461. bus_to_virt(cmd->saved_data_pointer), "", 1);
  2462. size = print_insn (host, (u32 *)
  2463. bus_to_virt(cmd->saved_data_pointer) + size, "", 1);
  2464. print_progress (c);
  2465. }
  2466. #if 0
  2467. printk ("scsi%d : datapath residual %d\n",
  2468. host->host_no, datapath_residual (host)) ;
  2469. #endif
  2470. }
  2471. return SPECIFIC_INT_RESTART;
  2472. #endif
  2473. #ifdef A_int_debug_sync
  2474. case A_int_debug_sync:
  2475. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2476. OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
  2477. unsigned char sxfer = NCR53c7x0_read8 (SXFER_REG), scntl3;
  2478. if ((hostdata->chip / 100) == 8) {
  2479. scntl3 = NCR53c7x0_read8 (SCNTL3_REG_800);
  2480. if (c) {
  2481. if (sxfer != hostdata->sync[c->device->id].sxfer_sanity ||
  2482. scntl3 != hostdata->sync[c->device->id].scntl3_sanity) {
  2483. printk ("scsi%d : sync sanity check failed sxfer=0x%x, scntl3=0x%x",
  2484. host->host_no, sxfer, scntl3);
  2485. NCR53c7x0_write8 (SXFER_REG, sxfer);
  2486. NCR53c7x0_write8 (SCNTL3_REG_800, scntl3);
  2487. }
  2488. } else
  2489. printk ("scsi%d : unknown command sxfer=0x%x, scntl3=0x%x\n",
  2490. host->host_no, (int) sxfer, (int) scntl3);
  2491. } else {
  2492. if (c) {
  2493. if (sxfer != hostdata->sync[c->device->id].sxfer_sanity) {
  2494. printk ("scsi%d : sync sanity check failed sxfer=0x%x",
  2495. host->host_no, sxfer);
  2496. NCR53c7x0_write8 (SXFER_REG, sxfer);
  2497. NCR53c7x0_write8 (SBCL_REG,
  2498. hostdata->sync[c->device->id].sscf_710);
  2499. }
  2500. } else
  2501. printk ("scsi%d : unknown command sxfer=0x%x\n",
  2502. host->host_no, (int) sxfer);
  2503. }
  2504. }
  2505. return SPECIFIC_INT_RESTART;
  2506. #endif
  2507. #ifdef A_int_debug_datain
  2508. case A_int_debug_datain:
  2509. if (hostdata->options & (OPTION_DEBUG_SCRIPT|OPTION_DEBUG_INTR|
  2510. OPTION_DEBUG_DISCONNECT|OPTION_DEBUG_SDTR)) {
  2511. int size;
  2512. if ((hostdata->chip / 100) == 8)
  2513. printk ("scsi%d : In do_datain (%s) sxfer=0x%x, scntl3=0x%x\n"
  2514. " datapath residual=%d\n",
  2515. host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
  2516. (int) NCR53c7x0_read8(SXFER_REG),
  2517. (int) NCR53c7x0_read8(SCNTL3_REG_800),
  2518. datapath_residual (host)) ;
  2519. else
  2520. printk ("scsi%d : In do_datain (%s) sxfer=0x%x\n"
  2521. " datapath residual=%d\n",
  2522. host->host_no, sbcl_to_phase (NCR53c7x0_read8 (SBCL_REG)),
  2523. (int) NCR53c7x0_read8(SXFER_REG),
  2524. datapath_residual (host)) ;
  2525. print_insn (host, dsp, "", 1);
  2526. size = print_insn (host, (u32 *) bus_to_virt(dsp[1]), "", 1);
  2527. print_insn (host, (u32 *) bus_to_virt(dsp[1]) + size, "", 1);
  2528. }
  2529. return SPECIFIC_INT_RESTART;
  2530. #endif
  2531. #ifdef A_int_debug_check_dsa
  2532. case A_int_debug_check_dsa:
  2533. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  2534. int sdid;
  2535. int tmp;
  2536. char *where;
  2537. if (hostdata->chip / 100 == 8)
  2538. sdid = NCR53c7x0_read8 (SDID_REG_800) & 15;
  2539. else {
  2540. tmp = NCR53c7x0_read8 (SDID_REG_700);
  2541. if (!tmp)
  2542. panic ("SDID_REG_700 = 0");
  2543. tmp >>= 1;
  2544. sdid = 0;
  2545. while (tmp) {
  2546. tmp >>= 1;
  2547. sdid++;
  2548. }
  2549. }
  2550. where = dsp - NCR53c7x0_insn_size(NCR53c7x0_read8
  2551. (DCMD_REG)) == hostdata->script +
  2552. Ent_select_check_dsa / sizeof(u32) ?
  2553. "selection" : "reselection";
  2554. if (c && sdid != c->device->id) {
  2555. printk ("scsi%d : SDID target %d != DSA target %d at %s\n",
  2556. host->host_no, sdid, c->device->id, where);
  2557. print_lots(host);
  2558. dump_events (host, 20);
  2559. return SPECIFIC_INT_PANIC;
  2560. }
  2561. }
  2562. return SPECIFIC_INT_RESTART;
  2563. #endif
  2564. default:
  2565. if ((dsps & 0xff000000) == 0x03000000) {
  2566. printk ("scsi%d : misc debug interrupt 0x%x\n",
  2567. host->host_no, dsps);
  2568. return SPECIFIC_INT_RESTART;
  2569. } else if ((dsps & 0xff000000) == 0x05000000) {
  2570. if (hostdata->events) {
  2571. struct NCR53c7x0_event *event;
  2572. ++hostdata->event_index;
  2573. if (hostdata->event_index >= hostdata->event_size)
  2574. hostdata->event_index = 0;
  2575. event = (struct NCR53c7x0_event *) hostdata->events +
  2576. hostdata->event_index;
  2577. event->event = (enum ncr_event) dsps;
  2578. event->dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  2579. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  2580. if (hostdata->chip / 100 == 8)
  2581. event->target = NCR53c7x0_read8(SSID_REG_800);
  2582. else {
  2583. unsigned char tmp, sdid;
  2584. tmp = NCR53c7x0_read8 (SDID_REG_700);
  2585. if (!tmp)
  2586. panic ("SDID_REG_700 = 0");
  2587. tmp >>= 1;
  2588. sdid = 0;
  2589. while (tmp) {
  2590. tmp >>= 1;
  2591. sdid++;
  2592. }
  2593. event->target = sdid;
  2594. }
  2595. }
  2596. else
  2597. event->target = 255;
  2598. if (event->event == EVENT_RESELECT)
  2599. event->lun = hostdata->reselected_identify & 0xf;
  2600. else if (c)
  2601. event->lun = c->device->lun;
  2602. else
  2603. event->lun = 255;
  2604. do_gettimeofday(&(event->time));
  2605. if (c) {
  2606. event->pid = c->pid;
  2607. memcpy ((void *) event->cmnd, (void *) c->cmnd,
  2608. sizeof (event->cmnd));
  2609. } else {
  2610. event->pid = -1;
  2611. }
  2612. }
  2613. return SPECIFIC_INT_RESTART;
  2614. }
  2615. printk ("scsi%d : unknown user interrupt 0x%x\n",
  2616. host->host_no, (unsigned) dsps);
  2617. return SPECIFIC_INT_PANIC;
  2618. }
  2619. }
  2620. /*
  2621. * XXX - the stock NCR assembler won't output the scriptu.h file,
  2622. * which undefine's all #define'd CPP symbols from the script.h
  2623. * file, which will create problems if you use multiple scripts
  2624. * with the same symbol names.
  2625. *
  2626. * If you insist on using NCR's assembler, you could generate
  2627. * scriptu.h from script.h using something like
  2628. *
  2629. * grep #define script.h | \
  2630. * sed 's/#define[ ][ ]*\([_a-zA-Z][_a-zA-Z0-9]*\).*$/#undefine \1/' \
  2631. * > scriptu.h
  2632. */
  2633. #include "53c7xx_u.h"
  2634. /* XXX - add alternate script handling code here */
  2635. /*
  2636. * Function : static void NCR537xx_soft_reset (struct Scsi_Host *host)
  2637. *
  2638. * Purpose : perform a soft reset of the NCR53c7xx chip
  2639. *
  2640. * Inputs : host - pointer to this host adapter's structure
  2641. *
  2642. * Preconditions : NCR53c7x0_init must have been called for this
  2643. * host.
  2644. *
  2645. */
  2646. static void
  2647. NCR53c7x0_soft_reset (struct Scsi_Host *host) {
  2648. NCR53c7x0_local_declare();
  2649. unsigned long flags;
  2650. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2651. host->hostdata[0];
  2652. NCR53c7x0_local_setup(host);
  2653. local_irq_save(flags);
  2654. /* Disable scsi chip and s/w level 7 ints */
  2655. #ifdef CONFIG_MVME16x
  2656. if (MACH_IS_MVME16x)
  2657. {
  2658. volatile unsigned long v;
  2659. v = *(volatile unsigned long *)0xfff4006c;
  2660. v &= ~0x8000;
  2661. *(volatile unsigned long *)0xfff4006c = v;
  2662. v = *(volatile unsigned long *)0xfff4202c;
  2663. v &= ~0x10;
  2664. *(volatile unsigned long *)0xfff4202c = v;
  2665. }
  2666. #endif
  2667. /* Anything specific for your hardware? */
  2668. /*
  2669. * Do a soft reset of the chip so that everything is
  2670. * reinitialized to the power-on state.
  2671. *
  2672. * Basically follow the procedure outlined in the NCR53c700
  2673. * data manual under Chapter Six, How to Use, Steps Necessary to
  2674. * Start SCRIPTS, with the exception of actually starting the
  2675. * script and setting up the synchronous transfer gunk.
  2676. */
  2677. /* Should we reset the scsi bus here??????????????????? */
  2678. NCR53c7x0_write8(ISTAT_REG_700, ISTAT_10_SRST);
  2679. NCR53c7x0_write8(ISTAT_REG_700, 0);
  2680. /*
  2681. * saved_dcntl is set up in NCR53c7x0_init() before it is overwritten
  2682. * here. We should have some better way of working out the CF bit
  2683. * setting..
  2684. */
  2685. hostdata->saved_dcntl = DCNTL_10_EA|DCNTL_10_COM;
  2686. if (hostdata->scsi_clock > 50000000)
  2687. hostdata->saved_dcntl |= DCNTL_700_CF_3;
  2688. else
  2689. if (hostdata->scsi_clock > 37500000)
  2690. hostdata->saved_dcntl |= DCNTL_700_CF_2;
  2691. #if 0
  2692. else
  2693. /* Any clocks less than 37.5MHz? */
  2694. #endif
  2695. if (hostdata->options & OPTION_DEBUG_TRACE)
  2696. NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl | DCNTL_SSM);
  2697. else
  2698. NCR53c7x0_write8(DCNTL_REG, hostdata->saved_dcntl);
  2699. /* Following disables snooping - snooping is not required, as non-
  2700. * cached pages are used for shared data, and appropriate use is
  2701. * made of cache_push/cache_clear. Indeed, for 68060
  2702. * enabling snooping causes disk corruption of ext2fs free block
  2703. * bitmaps and the like. If you have a 68060 with snooping hardwared
  2704. * on, then you need to enable CONFIG_060_WRITETHROUGH.
  2705. */
  2706. NCR53c7x0_write8(CTEST7_REG, CTEST7_10_TT1|CTEST7_STD);
  2707. /* Actually burst of eight, according to my 53c710 databook */
  2708. NCR53c7x0_write8(hostdata->dmode, DMODE_10_BL_8 | DMODE_10_FC2);
  2709. NCR53c7x0_write8(SCID_REG, 1 << host->this_id);
  2710. NCR53c7x0_write8(SBCL_REG, 0);
  2711. NCR53c7x0_write8(SCNTL1_REG, SCNTL1_ESR_700);
  2712. NCR53c7x0_write8(SCNTL0_REG, ((hostdata->options & OPTION_PARITY) ?
  2713. SCNTL0_EPC : 0) | SCNTL0_EPG_700 | SCNTL0_ARB1 | SCNTL0_ARB2);
  2714. /*
  2715. * Enable all interrupts, except parity which we only want when
  2716. * the user requests it.
  2717. */
  2718. NCR53c7x0_write8(DIEN_REG, DIEN_700_BF |
  2719. DIEN_ABRT | DIEN_SSI | DIEN_SIR | DIEN_700_OPC);
  2720. NCR53c7x0_write8(SIEN_REG_700, ((hostdata->options & OPTION_PARITY) ?
  2721. SIEN_PAR : 0) | SIEN_700_STO | SIEN_RST | SIEN_UDC |
  2722. SIEN_SGE | SIEN_MA);
  2723. #ifdef CONFIG_MVME16x
  2724. if (MACH_IS_MVME16x)
  2725. {
  2726. volatile unsigned long v;
  2727. /* Enable scsi chip and s/w level 7 ints */
  2728. v = *(volatile unsigned long *)0xfff40080;
  2729. v = (v & ~(0xf << 28)) | (4 << 28);
  2730. *(volatile unsigned long *)0xfff40080 = v;
  2731. v = *(volatile unsigned long *)0xfff4006c;
  2732. v |= 0x8000;
  2733. *(volatile unsigned long *)0xfff4006c = v;
  2734. v = *(volatile unsigned long *)0xfff4202c;
  2735. v = (v & ~0xff) | 0x10 | 4;
  2736. *(volatile unsigned long *)0xfff4202c = v;
  2737. }
  2738. #endif
  2739. /* Anything needed for your hardware? */
  2740. local_irq_restore(flags);
  2741. }
  2742. /*
  2743. * Function static struct NCR53c7x0_cmd *allocate_cmd (Scsi_Cmnd *cmd)
  2744. *
  2745. * Purpose : Return the first free NCR53c7x0_cmd structure (which are
  2746. * reused in a LIFO manner to minimize cache thrashing).
  2747. *
  2748. * Side effects : If we haven't yet scheduled allocation of NCR53c7x0_cmd
  2749. * structures for this device, do so. Attempt to complete all scheduled
  2750. * allocations using get_zeroed_page(), putting NCR53c7x0_cmd structures on
  2751. * the free list. Teach programmers not to drink and hack.
  2752. *
  2753. * Inputs : cmd - SCSI command
  2754. *
  2755. * Returns : NCR53c7x0_cmd structure allocated on behalf of cmd;
  2756. * NULL on failure.
  2757. */
  2758. static void
  2759. my_free_page (void *addr, int dummy)
  2760. {
  2761. /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING, which
  2762. * XXX may be invalid (CONFIG_060_WRITETHROUGH)
  2763. */
  2764. kernel_set_cachemode((void *)addr, 4096, IOMAP_FULL_CACHING);
  2765. free_page ((u32)addr);
  2766. }
  2767. static struct NCR53c7x0_cmd *
  2768. allocate_cmd (Scsi_Cmnd *cmd) {
  2769. struct Scsi_Host *host = cmd->device->host;
  2770. struct NCR53c7x0_hostdata *hostdata =
  2771. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  2772. u32 real; /* Real address */
  2773. int size; /* Size of *tmp */
  2774. struct NCR53c7x0_cmd *tmp;
  2775. unsigned long flags;
  2776. if (hostdata->options & OPTION_DEBUG_ALLOCATION)
  2777. printk ("scsi%d : num_cmds = %d, can_queue = %d\n"
  2778. " target = %d, lun = %d, %s\n",
  2779. host->host_no, hostdata->num_cmds, host->can_queue,
  2780. cmd->device->id, cmd->device->lun, (hostdata->cmd_allocated[cmd->device->id] &
  2781. (1 << cmd->device->lun)) ? "already allocated" : "not allocated");
  2782. /*
  2783. * If we have not yet reserved commands for this I_T_L nexus, and
  2784. * the device exists (as indicated by permanent Scsi_Cmnd structures
  2785. * being allocated under 1.3.x, or being outside of scan_scsis in
  2786. * 1.2.x), do so now.
  2787. */
  2788. if (!(hostdata->cmd_allocated[cmd->device->id] & (1 << cmd->device->lun)) &&
  2789. cmd->device && cmd->device->has_cmdblocks) {
  2790. if ((hostdata->extra_allocate + hostdata->num_cmds) < host->can_queue)
  2791. hostdata->extra_allocate += host->cmd_per_lun;
  2792. hostdata->cmd_allocated[cmd->device->id] |= (1 << cmd->device->lun);
  2793. }
  2794. for (; hostdata->extra_allocate > 0 ; --hostdata->extra_allocate,
  2795. ++hostdata->num_cmds) {
  2796. /* historically, kmalloc has returned unaligned addresses; pad so we
  2797. have enough room to ROUNDUP */
  2798. size = hostdata->max_cmd_size + sizeof (void *);
  2799. #ifdef FORCE_DSA_ALIGNMENT
  2800. /*
  2801. * 53c710 rev.0 doesn't have an add-with-carry instruction.
  2802. * Ensure we allocate enough memory to force alignment.
  2803. */
  2804. size += 256;
  2805. #endif
  2806. /* FIXME: for ISA bus '7xx chips, we need to or GFP_DMA in here */
  2807. if (size > 4096) {
  2808. printk (KERN_ERR "53c7xx: allocate_cmd size > 4K\n");
  2809. return NULL;
  2810. }
  2811. real = get_zeroed_page(GFP_ATOMIC);
  2812. if (real == 0)
  2813. return NULL;
  2814. memset((void *)real, 0, 4096);
  2815. cache_push(virt_to_phys((void *)real), 4096);
  2816. cache_clear(virt_to_phys((void *)real), 4096);
  2817. kernel_set_cachemode((void *)real, 4096, IOMAP_NOCACHE_SER);
  2818. tmp = ROUNDUP(real, void *);
  2819. #ifdef FORCE_DSA_ALIGNMENT
  2820. {
  2821. if (((u32)tmp & 0xff) > CmdPageStart)
  2822. tmp = (struct NCR53c7x0_cmd *)((u32)tmp + 255);
  2823. tmp = (struct NCR53c7x0_cmd *)(((u32)tmp & ~0xff) + CmdPageStart);
  2824. #if 0
  2825. printk ("scsi: size = %d, real = 0x%08x, tmp set to 0x%08x\n",
  2826. size, real, (u32)tmp);
  2827. #endif
  2828. }
  2829. #endif
  2830. tmp->real = (void *)real;
  2831. tmp->size = size;
  2832. tmp->free = ((void (*)(void *, int)) my_free_page);
  2833. local_irq_save(flags);
  2834. tmp->next = hostdata->free;
  2835. hostdata->free = tmp;
  2836. local_irq_restore(flags);
  2837. }
  2838. local_irq_save(flags);
  2839. tmp = (struct NCR53c7x0_cmd *) hostdata->free;
  2840. if (tmp) {
  2841. hostdata->free = tmp->next;
  2842. }
  2843. local_irq_restore(flags);
  2844. if (!tmp)
  2845. printk ("scsi%d : can't allocate command for target %d lun %d\n",
  2846. host->host_no, cmd->device->id, cmd->device->lun);
  2847. return tmp;
  2848. }
  2849. /*
  2850. * Function static struct NCR53c7x0_cmd *create_cmd (Scsi_Cmnd *cmd)
  2851. *
  2852. *
  2853. * Purpose : allocate a NCR53c7x0_cmd structure, initialize it based on the
  2854. * Scsi_Cmnd structure passed in cmd, including dsa and Linux field
  2855. * initialization, and dsa code relocation.
  2856. *
  2857. * Inputs : cmd - SCSI command
  2858. *
  2859. * Returns : NCR53c7x0_cmd structure corresponding to cmd,
  2860. * NULL on failure.
  2861. */
  2862. static struct NCR53c7x0_cmd *
  2863. create_cmd (Scsi_Cmnd *cmd) {
  2864. NCR53c7x0_local_declare();
  2865. struct Scsi_Host *host = cmd->device->host;
  2866. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  2867. host->hostdata[0];
  2868. struct NCR53c7x0_cmd *tmp; /* NCR53c7x0_cmd structure for this command */
  2869. int datain, /* Number of instructions per phase */
  2870. dataout;
  2871. int data_transfer_instructions, /* Count of dynamic instructions */
  2872. i; /* Counter */
  2873. u32 *cmd_datain, /* Address of datain/dataout code */
  2874. *cmd_dataout; /* Incremented as we assemble */
  2875. #ifdef notyet
  2876. unsigned char *msgptr; /* Current byte in select message */
  2877. int msglen; /* Length of whole select message */
  2878. #endif
  2879. unsigned long flags;
  2880. u32 exp_select_indirect; /* Used in sanity check */
  2881. NCR53c7x0_local_setup(cmd->device->host);
  2882. if (!(tmp = allocate_cmd (cmd)))
  2883. return NULL;
  2884. /*
  2885. * Copy CDB and initialised result fields from Scsi_Cmnd to NCR53c7x0_cmd.
  2886. * We do this because NCR53c7x0_cmd may have a special cache mode
  2887. * selected to cope with lack of bus snooping, etc.
  2888. */
  2889. memcpy(tmp->cmnd, cmd->cmnd, 12);
  2890. tmp->result = cmd->result;
  2891. /*
  2892. * Decide whether we need to generate commands for DATA IN,
  2893. * DATA OUT, neither, or both based on the SCSI command
  2894. */
  2895. switch (cmd->cmnd[0]) {
  2896. /* These commands do DATA IN */
  2897. case INQUIRY:
  2898. case MODE_SENSE:
  2899. case READ_6:
  2900. case READ_10:
  2901. case READ_CAPACITY:
  2902. case REQUEST_SENSE:
  2903. case READ_BLOCK_LIMITS:
  2904. case READ_TOC:
  2905. datain = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2906. dataout = 0;
  2907. break;
  2908. /* These commands do DATA OUT */
  2909. case MODE_SELECT:
  2910. case WRITE_6:
  2911. case WRITE_10:
  2912. #if 0
  2913. printk("scsi%d : command is ", host->host_no);
  2914. __scsi_print_command(cmd->cmnd);
  2915. #endif
  2916. #if 0
  2917. printk ("scsi%d : %d scatter/gather segments\n", host->host_no,
  2918. cmd->use_sg);
  2919. #endif
  2920. datain = 0;
  2921. dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2922. #if 0
  2923. hostdata->options |= OPTION_DEBUG_INTR;
  2924. #endif
  2925. break;
  2926. /*
  2927. * These commands do no data transfer, we should force an
  2928. * interrupt if a data phase is attempted on them.
  2929. */
  2930. case TEST_UNIT_READY:
  2931. case ALLOW_MEDIUM_REMOVAL:
  2932. case START_STOP:
  2933. datain = dataout = 0;
  2934. break;
  2935. /*
  2936. * We don't know about these commands, so generate code to handle
  2937. * both DATA IN and DATA OUT phases. More efficient to identify them
  2938. * and add them to the above cases.
  2939. */
  2940. default:
  2941. printk("scsi%d : datain+dataout for command ", host->host_no);
  2942. __scsi_print_command(cmd->cmnd);
  2943. datain = dataout = 2 * (cmd->use_sg ? cmd->use_sg : 1) + 3;
  2944. }
  2945. /*
  2946. * New code : so that active pointers work correctly regardless
  2947. * of where the saved data pointer is at, we want to immediately
  2948. * enter the dynamic code after selection, and on a non-data
  2949. * phase perform a CALL to the non-data phase handler, with
  2950. * returns back to this address.
  2951. *
  2952. * If a phase mismatch is encountered in the middle of a
  2953. * Block MOVE instruction, we want to _leave_ that instruction
  2954. * unchanged as the current case is, modify a temporary buffer,
  2955. * and point the active pointer (TEMP) at that.
  2956. *
  2957. * Furthermore, we want to implement a saved data pointer,
  2958. * set by the SAVE_DATA_POINTERs message.
  2959. *
  2960. * So, the data transfer segments will change to
  2961. * CALL data_transfer, WHEN NOT data phase
  2962. * MOVE x, x, WHEN data phase
  2963. * ( repeat )
  2964. * JUMP other_transfer
  2965. */
  2966. data_transfer_instructions = datain + dataout;
  2967. /*
  2968. * When we perform a request sense, we overwrite various things,
  2969. * including the data transfer code. Make sure we have enough
  2970. * space to do that.
  2971. */
  2972. if (data_transfer_instructions < 2)
  2973. data_transfer_instructions = 2;
  2974. /*
  2975. * The saved data pointer is set up so that a RESTORE POINTERS message
  2976. * will start the data transfer over at the beginning.
  2977. */
  2978. tmp->saved_data_pointer = virt_to_bus (hostdata->script) +
  2979. hostdata->E_data_transfer;
  2980. /*
  2981. * Initialize Linux specific fields.
  2982. */
  2983. tmp->cmd = cmd;
  2984. tmp->next = NULL;
  2985. tmp->flags = 0;
  2986. tmp->dsa_next_addr = virt_to_bus(tmp->dsa) + hostdata->dsa_next -
  2987. hostdata->dsa_start;
  2988. tmp->dsa_addr = virt_to_bus(tmp->dsa) - hostdata->dsa_start;
  2989. /*
  2990. * Calculate addresses of dynamic code to fill in DSA
  2991. */
  2992. tmp->data_transfer_start = tmp->dsa + (hostdata->dsa_end -
  2993. hostdata->dsa_start) / sizeof(u32);
  2994. tmp->data_transfer_end = tmp->data_transfer_start +
  2995. 2 * data_transfer_instructions;
  2996. cmd_datain = datain ? tmp->data_transfer_start : NULL;
  2997. cmd_dataout = dataout ? (datain ? cmd_datain + 2 * datain : tmp->
  2998. data_transfer_start) : NULL;
  2999. /*
  3000. * Fill in the NCR53c7x0_cmd structure as follows
  3001. * dsa, with fixed up DSA code
  3002. * datain code
  3003. * dataout code
  3004. */
  3005. /* Copy template code into dsa and perform all necessary fixups */
  3006. if (hostdata->dsa_fixup)
  3007. hostdata->dsa_fixup(tmp);
  3008. patch_dsa_32(tmp->dsa, dsa_next, 0, 0);
  3009. /*
  3010. * XXX is this giving 53c710 access to the Scsi_Cmnd in some way?
  3011. * Do we need to change it for caching reasons?
  3012. */
  3013. patch_dsa_32(tmp->dsa, dsa_cmnd, 0, virt_to_bus(cmd));
  3014. if (hostdata->options & OPTION_DEBUG_SYNCHRONOUS) {
  3015. exp_select_indirect = ((1 << cmd->device->id) << 16) |
  3016. (hostdata->sync[cmd->device->id].sxfer_sanity << 8);
  3017. if (hostdata->sync[cmd->device->id].select_indirect !=
  3018. exp_select_indirect) {
  3019. printk ("scsi%d : sanity check failed select_indirect=0x%x\n",
  3020. host->host_no, hostdata->sync[cmd->device->id].select_indirect);
  3021. FATAL(host);
  3022. }
  3023. }
  3024. patch_dsa_32(tmp->dsa, dsa_select, 0,
  3025. hostdata->sync[cmd->device->id].select_indirect);
  3026. /*
  3027. * Right now, we'll do the WIDE and SYNCHRONOUS negotiations on
  3028. * different commands; although it should be trivial to do them
  3029. * both at the same time.
  3030. */
  3031. if (hostdata->initiate_wdtr & (1 << cmd->device->id)) {
  3032. memcpy ((void *) (tmp->select + 1), (void *) wdtr_message,
  3033. sizeof(wdtr_message));
  3034. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(wdtr_message));
  3035. local_irq_save(flags);
  3036. hostdata->initiate_wdtr &= ~(1 << cmd->device->id);
  3037. local_irq_restore(flags);
  3038. } else if (hostdata->initiate_sdtr & (1 << cmd->device->id)) {
  3039. memcpy ((void *) (tmp->select + 1), (void *) sdtr_message,
  3040. sizeof(sdtr_message));
  3041. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(sdtr_message));
  3042. tmp->flags |= CMD_FLAG_SDTR;
  3043. local_irq_save(flags);
  3044. hostdata->initiate_sdtr &= ~(1 << cmd->device->id);
  3045. local_irq_restore(flags);
  3046. }
  3047. #if 1
  3048. else if (!(hostdata->talked_to & (1 << cmd->device->id)) &&
  3049. !(hostdata->options & OPTION_NO_ASYNC)) {
  3050. memcpy ((void *) (tmp->select + 1), (void *) async_message,
  3051. sizeof(async_message));
  3052. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1 + sizeof(async_message));
  3053. tmp->flags |= CMD_FLAG_SDTR;
  3054. }
  3055. #endif
  3056. else
  3057. patch_dsa_32(tmp->dsa, dsa_msgout, 0, 1);
  3058. hostdata->talked_to |= (1 << cmd->device->id);
  3059. tmp->select[0] = (hostdata->options & OPTION_DISCONNECT) ?
  3060. IDENTIFY (1, cmd->device->lun) : IDENTIFY (0, cmd->device->lun);
  3061. patch_dsa_32(tmp->dsa, dsa_msgout, 1, virt_to_bus(tmp->select));
  3062. patch_dsa_32(tmp->dsa, dsa_cmdout, 0, cmd->cmd_len);
  3063. patch_dsa_32(tmp->dsa, dsa_cmdout, 1, virt_to_bus(tmp->cmnd));
  3064. patch_dsa_32(tmp->dsa, dsa_dataout, 0, cmd_dataout ?
  3065. virt_to_bus (cmd_dataout)
  3066. : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
  3067. patch_dsa_32(tmp->dsa, dsa_datain, 0, cmd_datain ?
  3068. virt_to_bus (cmd_datain)
  3069. : virt_to_bus (hostdata->script) + hostdata->E_other_transfer);
  3070. /*
  3071. * XXX - need to make endian aware, should use separate variables
  3072. * for both status and message bytes.
  3073. */
  3074. patch_dsa_32(tmp->dsa, dsa_msgin, 0, 1);
  3075. /*
  3076. * FIXME : these only works for little endian. We probably want to
  3077. * provide message and status fields in the NCR53c7x0_cmd
  3078. * structure, and assign them to cmd->result when we're done.
  3079. */
  3080. #ifdef BIG_ENDIAN
  3081. patch_dsa_32(tmp->dsa, dsa_msgin, 1, virt_to_bus(&tmp->result) + 2);
  3082. patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
  3083. patch_dsa_32(tmp->dsa, dsa_status, 1, virt_to_bus(&tmp->result) + 3);
  3084. #else
  3085. patch_dsa_32(tmp->dsa, dsa_msgin, 1, virt_to_bus(&tmp->result) + 1);
  3086. patch_dsa_32(tmp->dsa, dsa_status, 0, 1);
  3087. patch_dsa_32(tmp->dsa, dsa_status, 1, virt_to_bus(&tmp->result));
  3088. #endif
  3089. patch_dsa_32(tmp->dsa, dsa_msgout_other, 0, 1);
  3090. patch_dsa_32(tmp->dsa, dsa_msgout_other, 1,
  3091. virt_to_bus(&(hostdata->NCR53c7xx_msg_nop)));
  3092. /*
  3093. * Generate code for zero or more of the DATA IN, DATA OUT phases
  3094. * in the format
  3095. *
  3096. * CALL data_transfer, WHEN NOT phase
  3097. * MOVE first buffer length, first buffer address, WHEN phase
  3098. * ...
  3099. * MOVE last buffer length, last buffer address, WHEN phase
  3100. * JUMP other_transfer
  3101. */
  3102. /*
  3103. * See if we're getting to data transfer by generating an unconditional
  3104. * interrupt.
  3105. */
  3106. #if 0
  3107. if (datain) {
  3108. cmd_datain[0] = 0x98080000;
  3109. cmd_datain[1] = 0x03ffd00d;
  3110. cmd_datain += 2;
  3111. }
  3112. #endif
  3113. /*
  3114. * XXX - I'm undecided whether all of this nonsense is faster
  3115. * in the long run, or whether I should just go and implement a loop
  3116. * on the NCR chip using table indirect mode?
  3117. *
  3118. * In any case, this is how it _must_ be done for 53c700/700-66 chips,
  3119. * so this stays even when we come up with something better.
  3120. *
  3121. * When we're limited to 1 simultaneous command, no overlapping processing,
  3122. * we're seeing 630K/sec, with 7% CPU usage on a slow Syquest 45M
  3123. * drive.
  3124. *
  3125. * Not bad, not good. We'll see.
  3126. */
  3127. tmp->bounce.len = 0; /* Assume aligned buffer */
  3128. for (i = 0; cmd->use_sg ? (i < cmd->use_sg) : !i; cmd_datain += 4,
  3129. cmd_dataout += 4, ++i) {
  3130. u32 vbuf = cmd->use_sg
  3131. ? (u32)page_address(((struct scatterlist *)cmd->request_buffer)[i].page)+
  3132. ((struct scatterlist *)cmd->request_buffer)[i].offset
  3133. : (u32)(cmd->request_buffer);
  3134. u32 bbuf = virt_to_bus((void *)vbuf);
  3135. u32 count = cmd->use_sg ?
  3136. ((struct scatterlist *)cmd->request_buffer)[i].length :
  3137. cmd->request_bufflen;
  3138. /*
  3139. * If we have buffers which are not aligned with 16 byte cache
  3140. * lines, then we just hope nothing accesses the other parts of
  3141. * those cache lines while the transfer is in progress. That would
  3142. * fill the cache, and subsequent reads of the dma data would pick
  3143. * up the wrong thing.
  3144. * XXX We need a bounce buffer to handle that correctly.
  3145. */
  3146. if (((bbuf & 15) || (count & 15)) && (datain || dataout))
  3147. {
  3148. /* Bounce buffer needed */
  3149. if (cmd->use_sg)
  3150. printk ("53c7xx: Non-aligned buffer with use_sg\n");
  3151. else if (datain && dataout)
  3152. printk ("53c7xx: Non-aligned buffer with datain && dataout\n");
  3153. else if (count > 256)
  3154. printk ("53c7xx: Non-aligned transfer > 256 bytes\n");
  3155. else
  3156. {
  3157. if (datain)
  3158. {
  3159. tmp->bounce.len = count;
  3160. tmp->bounce.addr = vbuf;
  3161. bbuf = virt_to_bus(tmp->bounce.buf);
  3162. tmp->bounce.buf[0] = 0xff;
  3163. tmp->bounce.buf[1] = 0xfe;
  3164. tmp->bounce.buf[2] = 0xfd;
  3165. tmp->bounce.buf[3] = 0xfc;
  3166. }
  3167. if (dataout)
  3168. {
  3169. memcpy ((void *)tmp->bounce.buf, (void *)vbuf, count);
  3170. bbuf = virt_to_bus(tmp->bounce.buf);
  3171. }
  3172. }
  3173. }
  3174. if (datain) {
  3175. cache_clear(virt_to_phys((void *)vbuf), count);
  3176. /* CALL other_in, WHEN NOT DATA_IN */
  3177. cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
  3178. DCMD_TCI_IO) << 24) |
  3179. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  3180. cmd_datain[1] = virt_to_bus (hostdata->script) +
  3181. hostdata->E_other_in;
  3182. /* MOVE count, buf, WHEN DATA_IN */
  3183. cmd_datain[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I | DCMD_BMI_IO)
  3184. << 24) | count;
  3185. cmd_datain[3] = bbuf;
  3186. #if 0
  3187. print_insn (host, cmd_datain, "dynamic ", 1);
  3188. print_insn (host, cmd_datain + 2, "dynamic ", 1);
  3189. #endif
  3190. }
  3191. if (dataout) {
  3192. cache_push(virt_to_phys((void *)vbuf), count);
  3193. /* CALL other_out, WHEN NOT DATA_OUT */
  3194. cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL) << 24) |
  3195. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  3196. cmd_dataout[1] = virt_to_bus(hostdata->script) +
  3197. hostdata->E_other_out;
  3198. /* MOVE count, buf, WHEN DATA+OUT */
  3199. cmd_dataout[2] = ((DCMD_TYPE_BMI | DCMD_BMI_OP_MOVE_I) << 24)
  3200. | count;
  3201. cmd_dataout[3] = bbuf;
  3202. #if 0
  3203. print_insn (host, cmd_dataout, "dynamic ", 1);
  3204. print_insn (host, cmd_dataout + 2, "dynamic ", 1);
  3205. #endif
  3206. }
  3207. }
  3208. /*
  3209. * Install JUMP instructions after the data transfer routines to return
  3210. * control to the do_other_transfer routines.
  3211. */
  3212. if (datain) {
  3213. cmd_datain[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
  3214. DBC_TCI_TRUE;
  3215. cmd_datain[1] = virt_to_bus(hostdata->script) +
  3216. hostdata->E_other_transfer;
  3217. #if 0
  3218. print_insn (host, cmd_datain, "dynamic jump ", 1);
  3219. #endif
  3220. cmd_datain += 2;
  3221. }
  3222. #if 0
  3223. if (datain) {
  3224. cmd_datain[0] = 0x98080000;
  3225. cmd_datain[1] = 0x03ffdeed;
  3226. cmd_datain += 2;
  3227. }
  3228. #endif
  3229. if (dataout) {
  3230. cmd_dataout[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_JUMP) << 24) |
  3231. DBC_TCI_TRUE;
  3232. cmd_dataout[1] = virt_to_bus(hostdata->script) +
  3233. hostdata->E_other_transfer;
  3234. #if 0
  3235. print_insn (host, cmd_dataout, "dynamic jump ", 1);
  3236. #endif
  3237. cmd_dataout += 2;
  3238. }
  3239. return tmp;
  3240. }
  3241. /*
  3242. * Function : int NCR53c7xx_queue_command (Scsi_Cmnd *cmd,
  3243. * void (*done)(Scsi_Cmnd *))
  3244. *
  3245. * Purpose : enqueues a SCSI command
  3246. *
  3247. * Inputs : cmd - SCSI command, done - function called on completion, with
  3248. * a pointer to the command descriptor.
  3249. *
  3250. * Returns : 0
  3251. *
  3252. * Side effects :
  3253. * cmd is added to the per instance driver issue_queue, with major
  3254. * twiddling done to the host specific fields of cmd. If the
  3255. * process_issue_queue coroutine isn't running, it is restarted.
  3256. *
  3257. * NOTE : we use the host_scribble field of the Scsi_Cmnd structure to
  3258. * hold our own data, and pervert the ptr field of the SCp field
  3259. * to create a linked list.
  3260. */
  3261. int
  3262. NCR53c7xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *)) {
  3263. struct Scsi_Host *host = cmd->device->host;
  3264. struct NCR53c7x0_hostdata *hostdata =
  3265. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3266. unsigned long flags;
  3267. Scsi_Cmnd *tmp;
  3268. cmd->scsi_done = done;
  3269. cmd->host_scribble = NULL;
  3270. cmd->SCp.ptr = NULL;
  3271. cmd->SCp.buffer = NULL;
  3272. #ifdef VALID_IDS
  3273. /* Ignore commands on invalid IDs */
  3274. if (!hostdata->valid_ids[cmd->device->id]) {
  3275. printk("scsi%d : ignoring target %d lun %d\n", host->host_no,
  3276. cmd->device->id, cmd->device->lun);
  3277. cmd->result = (DID_BAD_TARGET << 16);
  3278. done(cmd);
  3279. return 0;
  3280. }
  3281. #endif
  3282. local_irq_save(flags);
  3283. if ((hostdata->options & (OPTION_DEBUG_INIT_ONLY|OPTION_DEBUG_PROBE_ONLY))
  3284. || ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
  3285. !(hostdata->debug_lun_limit[cmd->device->id] & (1 << cmd->device->lun)))
  3286. #ifdef LINUX_1_2
  3287. || cmd->device->id > 7
  3288. #else
  3289. || cmd->device->id >= host->max_id
  3290. #endif
  3291. || cmd->device->id == host->this_id
  3292. || hostdata->state == STATE_DISABLED) {
  3293. printk("scsi%d : disabled or bad target %d lun %d\n", host->host_no,
  3294. cmd->device->id, cmd->device->lun);
  3295. cmd->result = (DID_BAD_TARGET << 16);
  3296. done(cmd);
  3297. local_irq_restore(flags);
  3298. return 0;
  3299. }
  3300. if ((hostdata->options & OPTION_DEBUG_NCOMMANDS_LIMIT) &&
  3301. (hostdata->debug_count_limit == 0)) {
  3302. printk("scsi%d : maximum commands exceeded\n", host->host_no);
  3303. cmd->result = (DID_BAD_TARGET << 16);
  3304. done(cmd);
  3305. local_irq_restore(flags);
  3306. return 0;
  3307. }
  3308. if (hostdata->options & OPTION_DEBUG_READ_ONLY) {
  3309. switch (cmd->cmnd[0]) {
  3310. case WRITE_6:
  3311. case WRITE_10:
  3312. printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
  3313. host->host_no);
  3314. cmd->result = (DID_BAD_TARGET << 16);
  3315. done(cmd);
  3316. local_irq_restore(flags);
  3317. return 0;
  3318. }
  3319. }
  3320. if ((hostdata->options & OPTION_DEBUG_TARGET_LIMIT) &&
  3321. hostdata->debug_count_limit != -1)
  3322. --hostdata->debug_count_limit;
  3323. cmd->result = 0xffff; /* The NCR will overwrite message
  3324. and status with valid data */
  3325. cmd->host_scribble = (unsigned char *) tmp = create_cmd (cmd);
  3326. /*
  3327. * REQUEST SENSE commands are inserted at the head of the queue
  3328. * so that we do not clear the contingent allegiance condition
  3329. * they may be looking at.
  3330. */
  3331. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  3332. cmd->SCp.ptr = (unsigned char *) hostdata->issue_queue;
  3333. hostdata->issue_queue = cmd;
  3334. } else {
  3335. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->SCp.ptr;
  3336. tmp = (Scsi_Cmnd *) tmp->SCp.ptr);
  3337. tmp->SCp.ptr = (unsigned char *) cmd;
  3338. }
  3339. local_irq_restore(flags);
  3340. run_process_issue_queue();
  3341. return 0;
  3342. }
  3343. /*
  3344. * Function : void to_schedule_list (struct Scsi_Host *host,
  3345. * struct NCR53c7x0_hostdata * hostdata, Scsi_Cmnd *cmd)
  3346. *
  3347. * Purpose : takes a SCSI command which was just removed from the
  3348. * issue queue, and deals with it by inserting it in the first
  3349. * free slot in the schedule list or by terminating it immediately.
  3350. *
  3351. * Inputs :
  3352. * host - SCSI host adapter; hostdata - hostdata structure for
  3353. * this adapter; cmd - a pointer to the command; should have
  3354. * the host_scribble field initialized to point to a valid
  3355. *
  3356. * Side effects :
  3357. * cmd is added to the per instance schedule list, with minor
  3358. * twiddling done to the host specific fields of cmd.
  3359. *
  3360. */
  3361. static __inline__ void
  3362. to_schedule_list (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  3363. struct NCR53c7x0_cmd *cmd) {
  3364. NCR53c7x0_local_declare();
  3365. Scsi_Cmnd *tmp = cmd->cmd;
  3366. unsigned long flags;
  3367. /* dsa start is negative, so subtraction is used */
  3368. volatile u32 *ncrcurrent;
  3369. int i;
  3370. NCR53c7x0_local_setup(host);
  3371. #if 0
  3372. printk("scsi%d : new dsa is 0x%lx (virt 0x%p)\n", host->host_no,
  3373. virt_to_bus(hostdata->dsa), hostdata->dsa);
  3374. #endif
  3375. local_irq_save(flags);
  3376. /*
  3377. * Work around race condition : if an interrupt fired and we
  3378. * got disabled forget about this command.
  3379. */
  3380. if (hostdata->state == STATE_DISABLED) {
  3381. printk("scsi%d : driver disabled\n", host->host_no);
  3382. tmp->result = (DID_BAD_TARGET << 16);
  3383. cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
  3384. hostdata->free = cmd;
  3385. tmp->scsi_done(tmp);
  3386. local_irq_restore(flags);
  3387. return;
  3388. }
  3389. for (i = host->can_queue, ncrcurrent = hostdata->schedule;
  3390. i > 0 && ncrcurrent[0] != hostdata->NOP_insn;
  3391. --i, ncrcurrent += 2 /* JUMP instructions are two words */);
  3392. if (i > 0) {
  3393. ++hostdata->busy[tmp->device->id][tmp->device->lun];
  3394. cmd->next = hostdata->running_list;
  3395. hostdata->running_list = cmd;
  3396. /* Restore this instruction to a NOP once the command starts */
  3397. cmd->dsa [(hostdata->dsa_jump_dest - hostdata->dsa_start) /
  3398. sizeof(u32)] = (u32) virt_to_bus ((void *)ncrcurrent);
  3399. /* Replace the current jump operand. */
  3400. ncrcurrent[1] =
  3401. virt_to_bus ((void *) cmd->dsa) + hostdata->E_dsa_code_begin -
  3402. hostdata->E_dsa_code_template;
  3403. /* Replace the NOP instruction with a JUMP */
  3404. ncrcurrent[0] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP) << 24) |
  3405. DBC_TCI_TRUE;
  3406. } else {
  3407. printk ("scsi%d: no free slot\n", host->host_no);
  3408. disable(host);
  3409. tmp->result = (DID_ERROR << 16);
  3410. cmd->next = (struct NCR53c7x0_cmd *) hostdata->free;
  3411. hostdata->free = cmd;
  3412. tmp->scsi_done(tmp);
  3413. local_irq_restore(flags);
  3414. return;
  3415. }
  3416. /*
  3417. * If the NCR chip is in an idle state, start it running the scheduler
  3418. * immediately. Otherwise, signal the chip to jump to schedule as
  3419. * soon as it is idle.
  3420. */
  3421. if (hostdata->idle) {
  3422. hostdata->idle = 0;
  3423. hostdata->state = STATE_RUNNING;
  3424. NCR53c7x0_write32 (DSP_REG, virt_to_bus ((void *)hostdata->schedule));
  3425. if (hostdata->options & OPTION_DEBUG_TRACE)
  3426. NCR53c7x0_write8 (DCNTL_REG, hostdata->saved_dcntl |
  3427. DCNTL_SSM | DCNTL_STD);
  3428. } else {
  3429. NCR53c7x0_write8(hostdata->istat, ISTAT_10_SIGP);
  3430. }
  3431. local_irq_restore(flags);
  3432. }
  3433. /*
  3434. * Function : busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata
  3435. * *hostdata, Scsi_Cmnd *cmd)
  3436. *
  3437. * Purpose : decide if we can pass the given SCSI command on to the
  3438. * device in question or not.
  3439. *
  3440. * Returns : non-zero when we're busy, 0 when we aren't.
  3441. */
  3442. static __inline__ int
  3443. busyp (struct Scsi_Host *host, struct NCR53c7x0_hostdata *hostdata,
  3444. Scsi_Cmnd *cmd) {
  3445. /* FIXME : in the future, this needs to accommodate SCSI-II tagged
  3446. queuing, and we may be able to play with fairness here a bit.
  3447. */
  3448. return hostdata->busy[cmd->device->id][cmd->device->lun];
  3449. }
  3450. /*
  3451. * Function : process_issue_queue (void)
  3452. *
  3453. * Purpose : transfer commands from the issue queue to NCR start queue
  3454. * of each NCR53c7/8xx in the system, avoiding kernel stack
  3455. * overflows when the scsi_done() function is invoked recursively.
  3456. *
  3457. * NOTE : process_issue_queue exits with interrupts *disabled*, so the
  3458. * caller must reenable them if it desires.
  3459. *
  3460. * NOTE : process_issue_queue should be called from both
  3461. * NCR53c7x0_queue_command() and from the interrupt handler
  3462. * after command completion in case NCR53c7x0_queue_command()
  3463. * isn't invoked again but we've freed up resources that are
  3464. * needed.
  3465. */
  3466. static void
  3467. process_issue_queue (unsigned long flags) {
  3468. Scsi_Cmnd *tmp, *prev;
  3469. struct Scsi_Host *host;
  3470. struct NCR53c7x0_hostdata *hostdata;
  3471. int done;
  3472. /*
  3473. * We run (with interrupts disabled) until we're sure that none of
  3474. * the host adapters have anything that can be done, at which point
  3475. * we set process_issue_queue_running to 0 and exit.
  3476. *
  3477. * Interrupts are enabled before doing various other internal
  3478. * instructions, after we've decided that we need to run through
  3479. * the loop again.
  3480. *
  3481. */
  3482. do {
  3483. local_irq_disable(); /* Freeze request queues */
  3484. done = 1;
  3485. for (host = first_host; host && host->hostt == the_template;
  3486. host = host->next) {
  3487. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3488. local_irq_disable();
  3489. if (hostdata->issue_queue) {
  3490. if (hostdata->state == STATE_DISABLED) {
  3491. tmp = (Scsi_Cmnd *) hostdata->issue_queue;
  3492. hostdata->issue_queue = (Scsi_Cmnd *) tmp->SCp.ptr;
  3493. tmp->result = (DID_BAD_TARGET << 16);
  3494. if (tmp->host_scribble) {
  3495. ((struct NCR53c7x0_cmd *)tmp->host_scribble)->next =
  3496. hostdata->free;
  3497. hostdata->free =
  3498. (struct NCR53c7x0_cmd *)tmp->host_scribble;
  3499. tmp->host_scribble = NULL;
  3500. }
  3501. tmp->scsi_done (tmp);
  3502. done = 0;
  3503. } else
  3504. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue,
  3505. prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *)
  3506. tmp->SCp.ptr)
  3507. if (!tmp->host_scribble ||
  3508. !busyp (host, hostdata, tmp)) {
  3509. if (prev)
  3510. prev->SCp.ptr = tmp->SCp.ptr;
  3511. else
  3512. hostdata->issue_queue = (Scsi_Cmnd *)
  3513. tmp->SCp.ptr;
  3514. tmp->SCp.ptr = NULL;
  3515. if (tmp->host_scribble) {
  3516. if (hostdata->options & OPTION_DEBUG_QUEUES)
  3517. printk ("scsi%d : moving command for target %d lun %d to start list\n",
  3518. host->host_no, tmp->device->id, tmp->device->lun);
  3519. to_schedule_list (host, hostdata,
  3520. (struct NCR53c7x0_cmd *)
  3521. tmp->host_scribble);
  3522. } else {
  3523. if (((tmp->result & 0xff) == 0xff) ||
  3524. ((tmp->result & 0xff00) == 0xff00)) {
  3525. printk ("scsi%d : danger Will Robinson!\n",
  3526. host->host_no);
  3527. tmp->result = DID_ERROR << 16;
  3528. disable (host);
  3529. }
  3530. tmp->scsi_done(tmp);
  3531. }
  3532. done = 0;
  3533. } /* if target/lun is not busy */
  3534. } /* if hostdata->issue_queue */
  3535. if (!done)
  3536. local_irq_restore(flags);
  3537. } /* for host */
  3538. } while (!done);
  3539. process_issue_queue_running = 0;
  3540. }
  3541. /*
  3542. * Function : static void intr_scsi (struct Scsi_Host *host,
  3543. * struct NCR53c7x0_cmd *cmd)
  3544. *
  3545. * Purpose : handle all SCSI interrupts, indicated by the setting
  3546. * of the SIP bit in the ISTAT register.
  3547. *
  3548. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  3549. * may be NULL.
  3550. */
  3551. static void
  3552. intr_scsi (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  3553. NCR53c7x0_local_declare();
  3554. struct NCR53c7x0_hostdata *hostdata =
  3555. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3556. unsigned char sstat0_sist0, sist1, /* Registers */
  3557. fatal; /* Did a fatal interrupt
  3558. occur ? */
  3559. NCR53c7x0_local_setup(host);
  3560. fatal = 0;
  3561. sstat0_sist0 = NCR53c7x0_read8(SSTAT0_REG);
  3562. sist1 = 0;
  3563. if (hostdata->options & OPTION_DEBUG_INTR)
  3564. printk ("scsi%d : SIST0 0x%0x, SIST1 0x%0x\n", host->host_no,
  3565. sstat0_sist0, sist1);
  3566. /* 250ms selection timeout */
  3567. if (sstat0_sist0 & SSTAT0_700_STO) {
  3568. fatal = 1;
  3569. if (hostdata->options & OPTION_DEBUG_INTR) {
  3570. printk ("scsi%d : Selection Timeout\n", host->host_no);
  3571. if (cmd) {
  3572. printk("scsi%d : target %d, lun %d, command ",
  3573. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3574. __scsi_print_command (cmd->cmd->cmnd);
  3575. printk("scsi%d : dsp = 0x%x (virt 0x%p)\n", host->host_no,
  3576. NCR53c7x0_read32(DSP_REG),
  3577. bus_to_virt(NCR53c7x0_read32(DSP_REG)));
  3578. } else {
  3579. printk("scsi%d : no command\n", host->host_no);
  3580. }
  3581. }
  3582. /*
  3583. * XXX - question : how do we want to handle the Illegal Instruction
  3584. * interrupt, which may occur before or after the Selection Timeout
  3585. * interrupt?
  3586. */
  3587. if (1) {
  3588. hostdata->idle = 1;
  3589. hostdata->expecting_sto = 0;
  3590. if (hostdata->test_running) {
  3591. hostdata->test_running = 0;
  3592. hostdata->test_completed = 3;
  3593. } else if (cmd) {
  3594. abnormal_finished(cmd, DID_BAD_TARGET << 16);
  3595. }
  3596. #if 0
  3597. hostdata->intrs = 0;
  3598. #endif
  3599. }
  3600. }
  3601. /*
  3602. * FIXME : in theory, we can also get a UDC when a STO occurs.
  3603. */
  3604. if (sstat0_sist0 & SSTAT0_UDC) {
  3605. fatal = 1;
  3606. if (cmd) {
  3607. printk("scsi%d : target %d lun %d unexpected disconnect\n",
  3608. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3609. print_lots (host);
  3610. abnormal_finished(cmd, DID_ERROR << 16);
  3611. } else
  3612. printk("scsi%d : unexpected disconnect (no command)\n",
  3613. host->host_no);
  3614. hostdata->dsp = (u32 *) hostdata->schedule;
  3615. hostdata->dsp_changed = 1;
  3616. }
  3617. /* SCSI PARITY error */
  3618. if (sstat0_sist0 & SSTAT0_PAR) {
  3619. fatal = 1;
  3620. if (cmd && cmd->cmd) {
  3621. printk("scsi%d : target %d lun %d parity error.\n",
  3622. host->host_no, cmd->cmd->device->id, cmd->cmd->device->lun);
  3623. abnormal_finished (cmd, DID_PARITY << 16);
  3624. } else
  3625. printk("scsi%d : parity error\n", host->host_no);
  3626. /* Should send message out, parity error */
  3627. /* XXX - Reduce synchronous transfer rate! */
  3628. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3629. sizeof(u32);
  3630. hostdata->dsp_changed = 1;
  3631. /* SCSI GROSS error */
  3632. }
  3633. if (sstat0_sist0 & SSTAT0_SGE) {
  3634. fatal = 1;
  3635. printk("scsi%d : gross error, saved2_dsa = 0x%x\n", host->host_no,
  3636. (unsigned int)hostdata->saved2_dsa);
  3637. print_lots (host);
  3638. /*
  3639. * A SCSI gross error may occur when we have
  3640. *
  3641. * - A synchronous offset which causes the SCSI FIFO to be overwritten.
  3642. *
  3643. * - A REQ which causes the maximum synchronous offset programmed in
  3644. * the SXFER register to be exceeded.
  3645. *
  3646. * - A phase change with an outstanding synchronous offset.
  3647. *
  3648. * - Residual data in the synchronous data FIFO, with a transfer
  3649. * other than a synchronous receive is started.$#
  3650. */
  3651. /* XXX Should deduce synchronous transfer rate! */
  3652. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3653. sizeof(u32);
  3654. hostdata->dsp_changed = 1;
  3655. /* Phase mismatch */
  3656. }
  3657. if (sstat0_sist0 & SSTAT0_MA) {
  3658. fatal = 1;
  3659. if (hostdata->options & OPTION_DEBUG_INTR)
  3660. printk ("scsi%d : SSTAT0_MA\n", host->host_no);
  3661. intr_phase_mismatch (host, cmd);
  3662. }
  3663. #if 0
  3664. if (sstat0_sist0 & SIST0_800_RSL)
  3665. printk ("scsi%d : Oh no Mr. Bill!\n", host->host_no);
  3666. #endif
  3667. /*
  3668. * If a fatal SCSI interrupt occurs, we must insure that the DMA and
  3669. * SCSI FIFOs were flushed.
  3670. */
  3671. if (fatal) {
  3672. if (!hostdata->dstat_valid) {
  3673. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  3674. hostdata->dstat_valid = 1;
  3675. }
  3676. if (!(hostdata->dstat & DSTAT_DFE)) {
  3677. printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
  3678. /*
  3679. * Really need to check this code for 710 RGH.
  3680. * Havn't seen any problems, but maybe we should FLUSH before
  3681. * clearing sometimes.
  3682. */
  3683. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  3684. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF)
  3685. ;
  3686. hostdata->dstat |= DSTAT_DFE;
  3687. }
  3688. }
  3689. }
  3690. #ifdef CYCLIC_TRACE
  3691. /*
  3692. * The following implements a cyclic log of instructions executed, if you turn
  3693. * TRACE on. It will also print the log for you. Very useful when debugging
  3694. * 53c710 support, possibly not really needed any more.
  3695. */
  3696. u32 insn_log[4096];
  3697. u32 insn_log_index = 0;
  3698. void log1 (u32 i)
  3699. {
  3700. insn_log[insn_log_index++] = i;
  3701. if (insn_log_index == 4096)
  3702. insn_log_index = 0;
  3703. }
  3704. void log_insn (u32 *ip)
  3705. {
  3706. log1 ((u32)ip);
  3707. log1 (*ip);
  3708. log1 (*(ip+1));
  3709. if (((*ip >> 24) & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)
  3710. log1 (*(ip+2));
  3711. }
  3712. void dump_log(void)
  3713. {
  3714. int cnt = 0;
  3715. int i = insn_log_index;
  3716. int size;
  3717. struct Scsi_Host *host = first_host;
  3718. while (cnt < 4096) {
  3719. printk ("%08x (+%6x): ", insn_log[i], (insn_log[i] - (u32)&(((struct NCR53c7x0_hostdata *)host->hostdata[0])->script))/4);
  3720. if (++i == 4096)
  3721. i = 0;
  3722. cnt++;
  3723. if (((insn_log[i] >> 24) & DCMD_TYPE_MASK) == DCMD_TYPE_MMI)
  3724. size = 3;
  3725. else
  3726. size = 2;
  3727. while (size--) {
  3728. printk ("%08x ", insn_log[i]);
  3729. if (++i == 4096)
  3730. i = 0;
  3731. cnt++;
  3732. }
  3733. printk ("\n");
  3734. }
  3735. }
  3736. #endif
  3737. /*
  3738. * Function : static void NCR53c7x0_intfly (struct Scsi_Host *host)
  3739. *
  3740. * Purpose : Scan command queue for specified host, looking for completed
  3741. * commands.
  3742. *
  3743. * Inputs : Scsi_Host pointer.
  3744. *
  3745. * This is called from the interrupt handler, when a simulated INTFLY
  3746. * interrupt occurs.
  3747. */
  3748. static void
  3749. NCR53c7x0_intfly (struct Scsi_Host *host)
  3750. {
  3751. NCR53c7x0_local_declare();
  3752. struct NCR53c7x0_hostdata *hostdata; /* host->hostdata[0] */
  3753. struct NCR53c7x0_cmd *cmd, /* command which halted */
  3754. **cmd_prev_ptr;
  3755. unsigned long flags;
  3756. char search_found = 0; /* Got at least one ? */
  3757. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3758. NCR53c7x0_local_setup(host);
  3759. if (hostdata->options & OPTION_DEBUG_INTR)
  3760. printk ("scsi%d : INTFLY\n", host->host_no);
  3761. /*
  3762. * Traverse our list of running commands, and look
  3763. * for those with valid (non-0xff ff) status and message
  3764. * bytes encoded in the result which signify command
  3765. * completion.
  3766. */
  3767. local_irq_save(flags);
  3768. restart:
  3769. for (cmd_prev_ptr = (struct NCR53c7x0_cmd **)&(hostdata->running_list),
  3770. cmd = (struct NCR53c7x0_cmd *) hostdata->running_list; cmd ;
  3771. cmd_prev_ptr = (struct NCR53c7x0_cmd **) &(cmd->next),
  3772. cmd = (struct NCR53c7x0_cmd *) cmd->next)
  3773. {
  3774. Scsi_Cmnd *tmp;
  3775. if (!cmd) {
  3776. printk("scsi%d : very weird.\n", host->host_no);
  3777. break;
  3778. }
  3779. if (!(tmp = cmd->cmd)) {
  3780. printk("scsi%d : weird. NCR53c7x0_cmd has no Scsi_Cmnd\n",
  3781. host->host_no);
  3782. continue;
  3783. }
  3784. /* Copy the result over now; may not be complete,
  3785. * but subsequent tests may as well be done on
  3786. * cached memory.
  3787. */
  3788. tmp->result = cmd->result;
  3789. if (((tmp->result & 0xff) == 0xff) ||
  3790. ((tmp->result & 0xff00) == 0xff00))
  3791. continue;
  3792. search_found = 1;
  3793. if (cmd->bounce.len)
  3794. memcpy ((void *)cmd->bounce.addr,
  3795. (void *)cmd->bounce.buf, cmd->bounce.len);
  3796. /* Important - remove from list _before_ done is called */
  3797. if (cmd_prev_ptr)
  3798. *cmd_prev_ptr = (struct NCR53c7x0_cmd *) cmd->next;
  3799. --hostdata->busy[tmp->device->id][tmp->device->lun];
  3800. cmd->next = hostdata->free;
  3801. hostdata->free = cmd;
  3802. tmp->host_scribble = NULL;
  3803. if (hostdata->options & OPTION_DEBUG_INTR) {
  3804. printk ("scsi%d : command complete : pid %lu, id %d,lun %d result 0x%x ",
  3805. host->host_no, tmp->pid, tmp->device->id, tmp->device->lun, tmp->result);
  3806. __scsi_print_command (tmp->cmnd);
  3807. }
  3808. tmp->scsi_done(tmp);
  3809. goto restart;
  3810. }
  3811. local_irq_restore(flags);
  3812. if (!search_found) {
  3813. printk ("scsi%d : WARNING : INTFLY with no completed commands.\n",
  3814. host->host_no);
  3815. } else {
  3816. run_process_issue_queue();
  3817. }
  3818. return;
  3819. }
  3820. /*
  3821. * Function : static irqreturn_t NCR53c7x0_intr (int irq, void *dev_id)
  3822. *
  3823. * Purpose : handle NCR53c7x0 interrupts for all NCR devices sharing
  3824. * the same IRQ line.
  3825. *
  3826. * Inputs : Since we're using the IRQF_DISABLED interrupt handler
  3827. * semantics, irq indicates the interrupt which invoked
  3828. * this handler.
  3829. *
  3830. * On the 710 we simualte an INTFLY with a script interrupt, and the
  3831. * script interrupt handler will call back to this function.
  3832. */
  3833. static irqreturn_t
  3834. NCR53c7x0_intr (int irq, void *dev_id)
  3835. {
  3836. NCR53c7x0_local_declare();
  3837. struct Scsi_Host *host; /* Host we are looking at */
  3838. unsigned char istat; /* Values of interrupt regs */
  3839. struct NCR53c7x0_hostdata *hostdata; /* host->hostdata[0] */
  3840. struct NCR53c7x0_cmd *cmd; /* command which halted */
  3841. u32 *dsa; /* DSA */
  3842. int handled = 0;
  3843. #ifdef NCR_DEBUG
  3844. char buf[80]; /* Debugging sprintf buffer */
  3845. size_t buflen; /* Length of same */
  3846. #endif
  3847. host = (struct Scsi_Host *)dev_id;
  3848. hostdata = (struct NCR53c7x0_hostdata *) host->hostdata[0];
  3849. NCR53c7x0_local_setup(host);
  3850. /*
  3851. * Only read istat once per loop, since reading it again will unstack
  3852. * interrupts
  3853. */
  3854. while ((istat = NCR53c7x0_read8(hostdata->istat)) & (ISTAT_SIP|ISTAT_DIP)) {
  3855. handled = 1;
  3856. hostdata->dsp_changed = 0;
  3857. hostdata->dstat_valid = 0;
  3858. hostdata->state = STATE_HALTED;
  3859. if (NCR53c7x0_read8 (SSTAT2_REG) & SSTAT2_FF_MASK)
  3860. printk ("scsi%d : SCSI FIFO not empty\n", host->host_no);
  3861. /*
  3862. * NCR53c700 and NCR53c700-66 change the current SCSI
  3863. * process, hostdata->ncrcurrent, in the Linux driver so
  3864. * cmd = hostdata->ncrcurrent.
  3865. *
  3866. * With other chips, we must look through the commands
  3867. * executing and find the command structure which
  3868. * corresponds to the DSA register.
  3869. */
  3870. if (hostdata->options & OPTION_700) {
  3871. cmd = (struct NCR53c7x0_cmd *) hostdata->ncrcurrent;
  3872. } else {
  3873. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  3874. for (cmd = (struct NCR53c7x0_cmd *) hostdata->running_list;
  3875. cmd && (dsa + (hostdata->dsa_start / sizeof(u32))) != cmd->dsa;
  3876. cmd = (struct NCR53c7x0_cmd *)(cmd->next))
  3877. ;
  3878. }
  3879. if (hostdata->options & OPTION_DEBUG_INTR) {
  3880. if (cmd) {
  3881. printk("scsi%d : interrupt for pid %lu, id %d, lun %d ",
  3882. host->host_no, cmd->cmd->pid, (int) cmd->cmd->device->id,
  3883. (int) cmd->cmd->device->lun);
  3884. __scsi_print_command (cmd->cmd->cmnd);
  3885. } else {
  3886. printk("scsi%d : no active command\n", host->host_no);
  3887. }
  3888. }
  3889. if (istat & ISTAT_SIP) {
  3890. if (hostdata->options & OPTION_DEBUG_INTR)
  3891. printk ("scsi%d : ISTAT_SIP\n", host->host_no);
  3892. intr_scsi (host, cmd);
  3893. }
  3894. if (istat & ISTAT_DIP) {
  3895. if (hostdata->options & OPTION_DEBUG_INTR)
  3896. printk ("scsi%d : ISTAT_DIP\n", host->host_no);
  3897. intr_dma (host, cmd);
  3898. }
  3899. if (!hostdata->dstat_valid) {
  3900. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  3901. hostdata->dstat_valid = 1;
  3902. }
  3903. if (!(hostdata->dstat & DSTAT_DFE)) {
  3904. printk ("scsi%d : DMA FIFO not empty\n", host->host_no);
  3905. /* Really need to check this out for 710 RGH */
  3906. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  3907. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF)
  3908. ;
  3909. hostdata->dstat |= DSTAT_DFE;
  3910. }
  3911. if (!hostdata->idle && hostdata->state == STATE_HALTED) {
  3912. if (!hostdata->dsp_changed)
  3913. hostdata->dsp = (u32 *)bus_to_virt(NCR53c7x0_read32(DSP_REG));
  3914. #if 0
  3915. printk("scsi%d : new dsp is 0x%lx (virt 0x%p)\n",
  3916. host->host_no, virt_to_bus(hostdata->dsp), hostdata->dsp);
  3917. #endif
  3918. hostdata->state = STATE_RUNNING;
  3919. NCR53c7x0_write32 (DSP_REG, virt_to_bus(hostdata->dsp));
  3920. if (hostdata->options & OPTION_DEBUG_TRACE) {
  3921. #ifdef CYCLIC_TRACE
  3922. log_insn (hostdata->dsp);
  3923. #else
  3924. print_insn (host, hostdata->dsp, "t ", 1);
  3925. #endif
  3926. NCR53c7x0_write8 (DCNTL_REG,
  3927. hostdata->saved_dcntl | DCNTL_SSM | DCNTL_STD);
  3928. }
  3929. }
  3930. }
  3931. return IRQ_HANDLED;
  3932. }
  3933. /*
  3934. * Function : static int abort_connected (struct Scsi_Host *host)
  3935. *
  3936. * Purpose : Assuming that the NCR SCSI processor is currently
  3937. * halted, break the currently established nexus. Clean
  3938. * up of the NCR53c7x0_cmd and Scsi_Cmnd structures should
  3939. * be done on receipt of the abort interrupt.
  3940. *
  3941. * Inputs : host - SCSI host
  3942. *
  3943. */
  3944. static int
  3945. abort_connected (struct Scsi_Host *host) {
  3946. #ifdef NEW_ABORT
  3947. NCR53c7x0_local_declare();
  3948. #endif
  3949. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  3950. host->hostdata[0];
  3951. /* FIXME : this probably should change for production kernels; at the
  3952. least, counter should move to a per-host structure. */
  3953. static int counter = 5;
  3954. #ifdef NEW_ABORT
  3955. int sstat, phase, offset;
  3956. u32 *script;
  3957. NCR53c7x0_local_setup(host);
  3958. #endif
  3959. if (--counter <= 0) {
  3960. disable(host);
  3961. return 0;
  3962. }
  3963. printk ("scsi%d : DANGER : abort_connected() called \n",
  3964. host->host_no);
  3965. #ifdef NEW_ABORT
  3966. /*
  3967. * New strategy : Rather than using a generic abort routine,
  3968. * we'll specifically try to source or sink the appropriate
  3969. * amount of data for the phase we're currently in (taking into
  3970. * account the current synchronous offset)
  3971. */
  3972. sstat = (NCR53c8x0_read8 (SSTAT2_REG);
  3973. offset = OFFSET (sstat & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
  3974. phase = sstat & SSTAT2_PHASE_MASK;
  3975. /*
  3976. * SET ATN
  3977. * MOVE source_or_sink, WHEN CURRENT PHASE
  3978. * < repeat for each outstanding byte >
  3979. * JUMP send_abort_message
  3980. */
  3981. script = hostdata->abort_script = kmalloc (
  3982. 8 /* instruction size */ * (
  3983. 1 /* set ATN */ +
  3984. (!offset ? 1 : offset) /* One transfer per outstanding byte */ +
  3985. 1 /* send abort message */),
  3986. GFP_ATOMIC);
  3987. #else /* def NEW_ABORT */
  3988. hostdata->dsp = hostdata->script + hostdata->E_initiator_abort /
  3989. sizeof(u32);
  3990. #endif /* def NEW_ABORT */
  3991. hostdata->dsp_changed = 1;
  3992. /* XXX - need to flag the command as aborted after the abort_connected
  3993. code runs
  3994. */
  3995. return 0;
  3996. }
  3997. /*
  3998. * Function : static int datapath_residual (Scsi_Host *host)
  3999. *
  4000. * Purpose : return residual data count of what's in the chip.
  4001. *
  4002. * Inputs : host - SCSI host
  4003. */
  4004. static int
  4005. datapath_residual (struct Scsi_Host *host) {
  4006. NCR53c7x0_local_declare();
  4007. int count, synchronous, sstat;
  4008. unsigned int ddir;
  4009. NCR53c7x0_local_setup(host);
  4010. /* COMPAT : the 700 and 700-66 need to use DFIFO_00_BO_MASK */
  4011. count = ((NCR53c7x0_read8 (DFIFO_REG) & DFIFO_10_BO_MASK) -
  4012. (NCR53c7x0_read32 (DBC_REG) & DFIFO_10_BO_MASK)) & DFIFO_10_BO_MASK;
  4013. synchronous = NCR53c7x0_read8 (SXFER_REG) & SXFER_MO_MASK;
  4014. /* COMPAT : DDIR is elsewhere on non-'8xx chips. */
  4015. ddir = NCR53c7x0_read8 (CTEST0_REG_700) & CTEST0_700_DDIR;
  4016. if (ddir) {
  4017. /* Receive */
  4018. if (synchronous)
  4019. count += (NCR53c7x0_read8 (SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT;
  4020. else
  4021. if (NCR53c7x0_read8 (SSTAT1_REG) & SSTAT1_ILF)
  4022. ++count;
  4023. } else {
  4024. /* Send */
  4025. sstat = NCR53c7x0_read8 (SSTAT1_REG);
  4026. if (sstat & SSTAT1_OLF)
  4027. ++count;
  4028. if (synchronous && (sstat & SSTAT1_ORF))
  4029. ++count;
  4030. }
  4031. return count;
  4032. }
  4033. /*
  4034. * Function : static const char * sbcl_to_phase (int sbcl)_
  4035. *
  4036. * Purpose : Convert SBCL register to user-parsable phase representation
  4037. *
  4038. * Inputs : sbcl - value of sbcl register
  4039. */
  4040. static const char *
  4041. sbcl_to_phase (int sbcl) {
  4042. switch (sbcl & SBCL_PHASE_MASK) {
  4043. case SBCL_PHASE_DATAIN:
  4044. return "DATAIN";
  4045. case SBCL_PHASE_DATAOUT:
  4046. return "DATAOUT";
  4047. case SBCL_PHASE_MSGIN:
  4048. return "MSGIN";
  4049. case SBCL_PHASE_MSGOUT:
  4050. return "MSGOUT";
  4051. case SBCL_PHASE_CMDOUT:
  4052. return "CMDOUT";
  4053. case SBCL_PHASE_STATIN:
  4054. return "STATUSIN";
  4055. default:
  4056. return "unknown";
  4057. }
  4058. }
  4059. /*
  4060. * Function : static const char * sstat2_to_phase (int sstat)_
  4061. *
  4062. * Purpose : Convert SSTAT2 register to user-parsable phase representation
  4063. *
  4064. * Inputs : sstat - value of sstat register
  4065. */
  4066. static const char *
  4067. sstat2_to_phase (int sstat) {
  4068. switch (sstat & SSTAT2_PHASE_MASK) {
  4069. case SSTAT2_PHASE_DATAIN:
  4070. return "DATAIN";
  4071. case SSTAT2_PHASE_DATAOUT:
  4072. return "DATAOUT";
  4073. case SSTAT2_PHASE_MSGIN:
  4074. return "MSGIN";
  4075. case SSTAT2_PHASE_MSGOUT:
  4076. return "MSGOUT";
  4077. case SSTAT2_PHASE_CMDOUT:
  4078. return "CMDOUT";
  4079. case SSTAT2_PHASE_STATIN:
  4080. return "STATUSIN";
  4081. default:
  4082. return "unknown";
  4083. }
  4084. }
  4085. /*
  4086. * Function : static void intr_phase_mismatch (struct Scsi_Host *host,
  4087. * struct NCR53c7x0_cmd *cmd)
  4088. *
  4089. * Purpose : Handle phase mismatch interrupts
  4090. *
  4091. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4092. * may be NULL.
  4093. *
  4094. * Side effects : The abort_connected() routine is called or the NCR chip
  4095. * is restarted, jumping to the command_complete entry point, or
  4096. * patching the address and transfer count of the current instruction
  4097. * and calling the msg_in entry point as appropriate.
  4098. */
  4099. static void
  4100. intr_phase_mismatch (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4101. NCR53c7x0_local_declare();
  4102. u32 dbc_dcmd, *dsp, *dsp_next;
  4103. unsigned char dcmd, sbcl;
  4104. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4105. host->hostdata[0];
  4106. int residual;
  4107. enum {ACTION_ABORT, ACTION_ABORT_PRINT, ACTION_CONTINUE} action =
  4108. ACTION_ABORT_PRINT;
  4109. const char *where = NULL;
  4110. NCR53c7x0_local_setup(host);
  4111. /*
  4112. * Corrective action is based on where in the SCSI SCRIPT(tm) the error
  4113. * occurred, as well as which SCSI phase we are currently in.
  4114. */
  4115. dsp_next = bus_to_virt(NCR53c7x0_read32(DSP_REG));
  4116. /*
  4117. * Fetch the current instruction, and remove the operands for easier
  4118. * interpretation.
  4119. */
  4120. dbc_dcmd = NCR53c7x0_read32(DBC_REG);
  4121. dcmd = (dbc_dcmd & 0xff000000) >> 24;
  4122. /*
  4123. * Like other processors, the NCR adjusts the instruction pointer before
  4124. * instruction decode. Set the DSP address back to what it should
  4125. * be for this instruction based on its size (2 or 3 32 bit words).
  4126. */
  4127. dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
  4128. /*
  4129. * Read new SCSI phase from the SBCL lines. Since all of our code uses
  4130. * a WHEN conditional instead of an IF conditional, we don't need to
  4131. * wait for a new REQ.
  4132. */
  4133. sbcl = NCR53c7x0_read8(SBCL_REG) & SBCL_PHASE_MASK;
  4134. if (!cmd) {
  4135. action = ACTION_ABORT_PRINT;
  4136. where = "no current command";
  4137. /*
  4138. * The way my SCSI SCRIPTS(tm) are architected, recoverable phase
  4139. * mismatches should only occur where we're doing a multi-byte
  4140. * BMI instruction. Specifically, this means
  4141. *
  4142. * - select messages (a SCSI-I target may ignore additional messages
  4143. * after the IDENTIFY; any target may reject a SDTR or WDTR)
  4144. *
  4145. * - command out (targets may send a message to signal an error
  4146. * condition, or go into STATUSIN after they've decided
  4147. * they don't like the command.
  4148. *
  4149. * - reply_message (targets may reject a multi-byte message in the
  4150. * middle)
  4151. *
  4152. * - data transfer routines (command completion with buffer space
  4153. * left, disconnect message, or error message)
  4154. */
  4155. } else if (((dsp >= cmd->data_transfer_start &&
  4156. dsp < cmd->data_transfer_end)) || dsp == (cmd->residual + 2)) {
  4157. if ((dcmd & (DCMD_TYPE_MASK|DCMD_BMI_OP_MASK|DCMD_BMI_INDIRECT|
  4158. DCMD_BMI_MSG|DCMD_BMI_CD)) == (DCMD_TYPE_BMI|
  4159. DCMD_BMI_OP_MOVE_I)) {
  4160. residual = datapath_residual (host);
  4161. if (hostdata->options & OPTION_DEBUG_DISCONNECT)
  4162. printk ("scsi%d : handling residual transfer (+ %d bytes from DMA FIFO)\n",
  4163. host->host_no, residual);
  4164. /*
  4165. * The first instruction is a CALL to the alternate handler for
  4166. * this data transfer phase, so we can do calls to
  4167. * munge_msg_restart as we would if control were passed
  4168. * from normal dynamic code.
  4169. */
  4170. if (dsp != cmd->residual + 2) {
  4171. cmd->residual[0] = ((DCMD_TYPE_TCI | DCMD_TCI_OP_CALL |
  4172. ((dcmd & DCMD_BMI_IO) ? DCMD_TCI_IO : 0)) << 24) |
  4173. DBC_TCI_WAIT_FOR_VALID | DBC_TCI_COMPARE_PHASE;
  4174. cmd->residual[1] = virt_to_bus(hostdata->script)
  4175. + ((dcmd & DCMD_BMI_IO)
  4176. ? hostdata->E_other_in : hostdata->E_other_out);
  4177. }
  4178. /*
  4179. * The second instruction is the a data transfer block
  4180. * move instruction, reflecting the pointer and count at the
  4181. * time of the phase mismatch.
  4182. */
  4183. cmd->residual[2] = dbc_dcmd + residual;
  4184. cmd->residual[3] = NCR53c7x0_read32(DNAD_REG) - residual;
  4185. /*
  4186. * The third and final instruction is a jump to the instruction
  4187. * which follows the instruction which had to be 'split'
  4188. */
  4189. if (dsp != cmd->residual + 2) {
  4190. cmd->residual[4] = ((DCMD_TYPE_TCI|DCMD_TCI_OP_JUMP)
  4191. << 24) | DBC_TCI_TRUE;
  4192. cmd->residual[5] = virt_to_bus(dsp_next);
  4193. }
  4194. /*
  4195. * For the sake of simplicity, transfer control to the
  4196. * conditional CALL at the start of the residual buffer.
  4197. */
  4198. hostdata->dsp = cmd->residual;
  4199. hostdata->dsp_changed = 1;
  4200. action = ACTION_CONTINUE;
  4201. } else {
  4202. where = "non-BMI dynamic DSA code";
  4203. action = ACTION_ABORT_PRINT;
  4204. }
  4205. } else if (dsp == (hostdata->script + hostdata->E_select_msgout / 4 + 2)) {
  4206. /* RGH 290697: Added +2 above, to compensate for the script
  4207. * instruction which disables the selection timer. */
  4208. /* Release ATN */
  4209. NCR53c7x0_write8 (SOCL_REG, 0);
  4210. switch (sbcl) {
  4211. /*
  4212. * Some devices (SQ555 come to mind) grab the IDENTIFY message
  4213. * sent on selection, and decide to go into COMMAND OUT phase
  4214. * rather than accepting the rest of the messages or rejecting
  4215. * them. Handle these devices gracefully.
  4216. */
  4217. case SBCL_PHASE_CMDOUT:
  4218. hostdata->dsp = dsp + 2 /* two _words_ */;
  4219. hostdata->dsp_changed = 1;
  4220. printk ("scsi%d : target %d ignored SDTR and went into COMMAND OUT\n",
  4221. host->host_no, cmd->cmd->device->id);
  4222. cmd->flags &= ~CMD_FLAG_SDTR;
  4223. action = ACTION_CONTINUE;
  4224. break;
  4225. case SBCL_PHASE_MSGIN:
  4226. hostdata->dsp = hostdata->script + hostdata->E_msg_in /
  4227. sizeof(u32);
  4228. hostdata->dsp_changed = 1;
  4229. action = ACTION_CONTINUE;
  4230. break;
  4231. default:
  4232. where="select message out";
  4233. action = ACTION_ABORT_PRINT;
  4234. }
  4235. /*
  4236. * Some SCSI devices will interpret a command as they read the bytes
  4237. * off the SCSI bus, and may decide that the command is Bogus before
  4238. * they've read the entire command off the bus.
  4239. */
  4240. } else if (dsp == hostdata->script + hostdata->E_cmdout_cmdout / sizeof
  4241. (u32)) {
  4242. hostdata->dsp = hostdata->script + hostdata->E_data_transfer /
  4243. sizeof (u32);
  4244. hostdata->dsp_changed = 1;
  4245. action = ACTION_CONTINUE;
  4246. /* FIXME : we need to handle message reject, etc. within msg_respond. */
  4247. #ifdef notyet
  4248. } else if (dsp == hostdata->script + hostdata->E_reply_message) {
  4249. switch (sbcl) {
  4250. /* Any other phase mismatches abort the currently executing command. */
  4251. #endif
  4252. } else {
  4253. where = "unknown location";
  4254. action = ACTION_ABORT_PRINT;
  4255. }
  4256. /* Flush DMA FIFO */
  4257. if (!hostdata->dstat_valid) {
  4258. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  4259. hostdata->dstat_valid = 1;
  4260. }
  4261. if (!(hostdata->dstat & DSTAT_DFE)) {
  4262. /* Really need to check this out for 710 RGH */
  4263. NCR53c7x0_write8 (CTEST8_REG, CTEST8_10_CLF);
  4264. while (NCR53c7x0_read8 (CTEST8_REG) & CTEST8_10_CLF);
  4265. hostdata->dstat |= DSTAT_DFE;
  4266. }
  4267. switch (action) {
  4268. case ACTION_ABORT_PRINT:
  4269. printk("scsi%d : %s : unexpected phase %s.\n",
  4270. host->host_no, where ? where : "unknown location",
  4271. sbcl_to_phase(sbcl));
  4272. print_lots (host);
  4273. /* Fall through to ACTION_ABORT */
  4274. case ACTION_ABORT:
  4275. abort_connected (host);
  4276. break;
  4277. case ACTION_CONTINUE:
  4278. break;
  4279. }
  4280. #if 0
  4281. if (hostdata->dsp_changed) {
  4282. printk("scsi%d: new dsp 0x%p\n", host->host_no, hostdata->dsp);
  4283. print_insn (host, hostdata->dsp, "", 1);
  4284. }
  4285. #endif
  4286. }
  4287. /*
  4288. * Function : static void intr_bf (struct Scsi_Host *host,
  4289. * struct NCR53c7x0_cmd *cmd)
  4290. *
  4291. * Purpose : handle BUS FAULT interrupts
  4292. *
  4293. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4294. * may be NULL.
  4295. */
  4296. static void
  4297. intr_bf (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4298. NCR53c7x0_local_declare();
  4299. u32 *dsp,
  4300. *next_dsp, /* Current dsp */
  4301. *dsa,
  4302. dbc_dcmd; /* DCMD (high eight bits) + DBC */
  4303. char *reason = NULL;
  4304. /* Default behavior is for a silent error, with a retry until we've
  4305. exhausted retries. */
  4306. enum {MAYBE, ALWAYS, NEVER} retry = MAYBE;
  4307. int report = 0;
  4308. NCR53c7x0_local_setup(host);
  4309. dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
  4310. next_dsp = bus_to_virt (NCR53c7x0_read32(DSP_REG));
  4311. dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
  4312. /* FIXME - check chip type */
  4313. dsa = bus_to_virt (NCR53c7x0_read32(DSA_REG));
  4314. /*
  4315. * Bus faults can be caused by either a Bad Address or
  4316. * Target Abort. We should check the Received Target Abort
  4317. * bit of the PCI status register and Master Abort Bit.
  4318. *
  4319. * - Master Abort bit indicates that no device claimed
  4320. * the address with DEVSEL within five clocks
  4321. *
  4322. * - Target Abort bit indicates that a target claimed it,
  4323. * but changed its mind once it saw the byte enables.
  4324. *
  4325. */
  4326. /* 53c710, not PCI system */
  4327. report = 1;
  4328. reason = "Unknown";
  4329. #ifndef notyet
  4330. report = 1;
  4331. #endif
  4332. if (report && reason)
  4333. {
  4334. printk(KERN_ALERT "scsi%d : BUS FAULT reason = %s\n",
  4335. host->host_no, reason ? reason : "unknown");
  4336. print_lots (host);
  4337. }
  4338. #ifndef notyet
  4339. retry = NEVER;
  4340. #endif
  4341. /*
  4342. * TODO : we should attempt to recover from any spurious bus
  4343. * faults. After X retries, we should figure that things are
  4344. * sufficiently wedged, and call NCR53c7xx_reset.
  4345. *
  4346. * This code should only get executed once we've decided that we
  4347. * cannot retry.
  4348. */
  4349. if (retry == NEVER) {
  4350. printk(KERN_ALERT " mail richard@sleepie.demon.co.uk\n");
  4351. FATAL (host);
  4352. }
  4353. }
  4354. /*
  4355. * Function : static void intr_dma (struct Scsi_Host *host,
  4356. * struct NCR53c7x0_cmd *cmd)
  4357. *
  4358. * Purpose : handle all DMA interrupts, indicated by the setting
  4359. * of the DIP bit in the ISTAT register.
  4360. *
  4361. * Inputs : host, cmd - host and NCR command causing the interrupt, cmd
  4362. * may be NULL.
  4363. */
  4364. static void
  4365. intr_dma (struct Scsi_Host *host, struct NCR53c7x0_cmd *cmd) {
  4366. NCR53c7x0_local_declare();
  4367. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4368. host->hostdata[0];
  4369. unsigned char dstat; /* DSTAT */
  4370. u32 *dsp,
  4371. *next_dsp, /* Current dsp */
  4372. *dsa,
  4373. dbc_dcmd; /* DCMD (high eight bits) + DBC */
  4374. int tmp;
  4375. unsigned long flags;
  4376. NCR53c7x0_local_setup(host);
  4377. if (!hostdata->dstat_valid) {
  4378. hostdata->dstat = NCR53c7x0_read8(DSTAT_REG);
  4379. hostdata->dstat_valid = 1;
  4380. }
  4381. dstat = hostdata->dstat;
  4382. if (hostdata->options & OPTION_DEBUG_INTR)
  4383. printk("scsi%d : DSTAT=0x%x\n", host->host_no, (int) dstat);
  4384. dbc_dcmd = NCR53c7x0_read32 (DBC_REG);
  4385. next_dsp = bus_to_virt(NCR53c7x0_read32(DSP_REG));
  4386. dsp = next_dsp - NCR53c7x0_insn_size ((dbc_dcmd >> 24) & 0xff);
  4387. /* XXX - check chip type */
  4388. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  4389. /*
  4390. * DSTAT_ABRT is the aborted interrupt. This is set whenever the
  4391. * SCSI chip is aborted.
  4392. *
  4393. * With NCR53c700 and NCR53c700-66 style chips, we should only
  4394. * get this when the chip is currently running the accept
  4395. * reselect/select code and we have set the abort bit in the
  4396. * ISTAT register.
  4397. *
  4398. */
  4399. if (dstat & DSTAT_ABRT) {
  4400. #if 0
  4401. /* XXX - add code here to deal with normal abort */
  4402. if ((hostdata->options & OPTION_700) && (hostdata->state ==
  4403. STATE_ABORTING)) {
  4404. } else
  4405. #endif
  4406. {
  4407. printk(KERN_ALERT "scsi%d : unexpected abort interrupt at\n"
  4408. " ", host->host_no);
  4409. print_insn (host, dsp, KERN_ALERT "s ", 1);
  4410. FATAL (host);
  4411. }
  4412. }
  4413. /*
  4414. * DSTAT_SSI is the single step interrupt. Should be generated
  4415. * whenever we have single stepped or are tracing.
  4416. */
  4417. if (dstat & DSTAT_SSI) {
  4418. if (hostdata->options & OPTION_DEBUG_TRACE) {
  4419. /* Don't print instr. until we write DSP at end of intr function */
  4420. } else if (hostdata->options & OPTION_DEBUG_SINGLE) {
  4421. print_insn (host, dsp, "s ", 0);
  4422. local_irq_save(flags);
  4423. /* XXX - should we do this, or can we get away with writing dsp? */
  4424. NCR53c7x0_write8 (DCNTL_REG, (NCR53c7x0_read8(DCNTL_REG) &
  4425. ~DCNTL_SSM) | DCNTL_STD);
  4426. local_irq_restore(flags);
  4427. } else {
  4428. printk(KERN_ALERT "scsi%d : unexpected single step interrupt at\n"
  4429. " ", host->host_no);
  4430. print_insn (host, dsp, KERN_ALERT "", 1);
  4431. printk(KERN_ALERT " mail drew@PoohSticks.ORG\n");
  4432. FATAL (host);
  4433. }
  4434. }
  4435. /*
  4436. * DSTAT_IID / DSTAT_OPC (same bit, same meaning, only the name
  4437. * is different) is generated whenever an illegal instruction is
  4438. * encountered.
  4439. *
  4440. * XXX - we may want to emulate INTFLY here, so we can use
  4441. * the same SCSI SCRIPT (tm) for NCR53c710 through NCR53c810
  4442. * chips.
  4443. */
  4444. if (dstat & DSTAT_OPC) {
  4445. /*
  4446. * Ascertain if this IID interrupts occurred before or after a STO
  4447. * interrupt. Since the interrupt handling code now leaves
  4448. * DSP unmodified until _after_ all stacked interrupts have been
  4449. * processed, reading the DSP returns the original DSP register.
  4450. * This means that if dsp lies between the select code, and
  4451. * message out following the selection code (where the IID interrupt
  4452. * would have to have occurred by due to the implicit wait for REQ),
  4453. * we have an IID interrupt resulting from a STO condition and
  4454. * can ignore it.
  4455. */
  4456. if (((dsp >= (hostdata->script + hostdata->E_select / sizeof(u32))) &&
  4457. (dsp <= (hostdata->script + hostdata->E_select_msgout /
  4458. sizeof(u32) + 8))) || (hostdata->test_running == 2)) {
  4459. if (hostdata->options & OPTION_DEBUG_INTR)
  4460. printk ("scsi%d : ignoring DSTAT_IID for SSTAT_STO\n",
  4461. host->host_no);
  4462. if (hostdata->expecting_iid) {
  4463. hostdata->expecting_iid = 0;
  4464. hostdata->idle = 1;
  4465. if (hostdata->test_running == 2) {
  4466. hostdata->test_running = 0;
  4467. hostdata->test_completed = 3;
  4468. } else if (cmd)
  4469. abnormal_finished (cmd, DID_BAD_TARGET << 16);
  4470. } else {
  4471. hostdata->expecting_sto = 1;
  4472. }
  4473. /*
  4474. * We can't guarantee we'll be able to execute the WAIT DISCONNECT
  4475. * instruction within the 3.4us of bus free and arbitration delay
  4476. * that a target can RESELECT in and assert REQ after we've dropped
  4477. * ACK. If this happens, we'll get an illegal instruction interrupt.
  4478. * Doing away with the WAIT DISCONNECT instructions broke everything,
  4479. * so instead I'll settle for moving one WAIT DISCONNECT a few
  4480. * instructions closer to the CLEAR ACK before it to minimize the
  4481. * chances of this happening, and handle it if it occurs anyway.
  4482. *
  4483. * Simply continue with what we were doing, and control should
  4484. * be transferred to the schedule routine which will ultimately
  4485. * pass control onto the reselection or selection (not yet)
  4486. * code.
  4487. */
  4488. } else if (dbc_dcmd == 0x48000000 && (NCR53c7x0_read8 (SBCL_REG) &
  4489. SBCL_REQ)) {
  4490. if (!(hostdata->options & OPTION_NO_PRINT_RACE))
  4491. {
  4492. printk("scsi%d: REQ before WAIT DISCONNECT IID\n",
  4493. host->host_no);
  4494. hostdata->options |= OPTION_NO_PRINT_RACE;
  4495. }
  4496. } else {
  4497. printk(KERN_ALERT "scsi%d : invalid instruction\n", host->host_no);
  4498. print_lots (host);
  4499. printk(KERN_ALERT " mail Richard@sleepie.demon.co.uk with ALL\n"
  4500. " boot messages and diagnostic output\n");
  4501. FATAL (host);
  4502. }
  4503. }
  4504. /*
  4505. * DSTAT_BF are bus fault errors. DSTAT_800_BF is valid for 710 also.
  4506. */
  4507. if (dstat & DSTAT_800_BF) {
  4508. intr_bf (host, cmd);
  4509. }
  4510. /*
  4511. * DSTAT_SIR interrupts are generated by the execution of
  4512. * the INT instruction. Since the exact values available
  4513. * are determined entirely by the SCSI script running,
  4514. * and are local to a particular script, a unique handler
  4515. * is called for each script.
  4516. */
  4517. if (dstat & DSTAT_SIR) {
  4518. if (hostdata->options & OPTION_DEBUG_INTR)
  4519. printk ("scsi%d : DSTAT_SIR\n", host->host_no);
  4520. switch ((tmp = hostdata->dstat_sir_intr (host, cmd))) {
  4521. case SPECIFIC_INT_NOTHING:
  4522. case SPECIFIC_INT_RESTART:
  4523. break;
  4524. case SPECIFIC_INT_ABORT:
  4525. abort_connected(host);
  4526. break;
  4527. case SPECIFIC_INT_PANIC:
  4528. printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
  4529. print_insn (host, dsp, KERN_ALERT "", 1);
  4530. printk(KERN_ALERT " dstat_sir_intr() returned SPECIFIC_INT_PANIC\n");
  4531. FATAL (host);
  4532. break;
  4533. case SPECIFIC_INT_BREAK:
  4534. intr_break (host, cmd);
  4535. break;
  4536. default:
  4537. printk(KERN_ALERT "scsi%d : failure at ", host->host_no);
  4538. print_insn (host, dsp, KERN_ALERT "", 1);
  4539. printk(KERN_ALERT" dstat_sir_intr() returned unknown value %d\n",
  4540. tmp);
  4541. FATAL (host);
  4542. }
  4543. }
  4544. }
  4545. /*
  4546. * Function : static int print_insn (struct Scsi_Host *host,
  4547. * u32 *insn, int kernel)
  4548. *
  4549. * Purpose : print numeric representation of the instruction pointed
  4550. * to by insn to the debugging or kernel message buffer
  4551. * as appropriate.
  4552. *
  4553. * If desired, a user level program can interpret this
  4554. * information.
  4555. *
  4556. * Inputs : host, insn - host, pointer to instruction, prefix -
  4557. * string to prepend, kernel - use printk instead of debugging buffer.
  4558. *
  4559. * Returns : size, in u32s, of instruction printed.
  4560. */
  4561. /*
  4562. * FIXME: should change kernel parameter so that it takes an ENUM
  4563. * specifying severity - either KERN_ALERT or KERN_PANIC so
  4564. * all panic messages are output with the same severity.
  4565. */
  4566. static int
  4567. print_insn (struct Scsi_Host *host, const u32 *insn,
  4568. const char *prefix, int kernel) {
  4569. char buf[160], /* Temporary buffer and pointer. ICKY
  4570. arbitrary length. */
  4571. *tmp;
  4572. unsigned char dcmd; /* dcmd register for *insn */
  4573. int size;
  4574. /*
  4575. * Check to see if the instruction pointer is not bogus before
  4576. * indirecting through it; avoiding red-zone at start of
  4577. * memory.
  4578. *
  4579. * FIXME: icky magic needs to happen here on non-intel boxes which
  4580. * don't have kernel memory mapped in like this. Might be reasonable
  4581. * to use vverify()?
  4582. */
  4583. if (virt_to_phys((void *)insn) < PAGE_SIZE ||
  4584. virt_to_phys((void *)(insn + 8)) > virt_to_phys(high_memory) ||
  4585. ((((dcmd = (insn[0] >> 24) & 0xff) & DCMD_TYPE_MMI) == DCMD_TYPE_MMI) &&
  4586. virt_to_phys((void *)(insn + 12)) > virt_to_phys(high_memory))) {
  4587. size = 0;
  4588. sprintf (buf, "%s%p: address out of range\n",
  4589. prefix, insn);
  4590. } else {
  4591. /*
  4592. * FIXME : (void *) cast in virt_to_bus should be unnecessary, because
  4593. * it should take const void * as argument.
  4594. */
  4595. #if !defined(CONFIG_MVME16x) && !defined(CONFIG_BVME6000)
  4596. sprintf(buf, "%s0x%lx (virt 0x%p) : 0x%08x 0x%08x (virt 0x%p)",
  4597. (prefix ? prefix : ""), virt_to_bus((void *) insn), insn,
  4598. insn[0], insn[1], bus_to_virt (insn[1]));
  4599. #else
  4600. /* Remove virtual addresses to reduce output, as they are the same */
  4601. sprintf(buf, "%s0x%x (+%x) : 0x%08x 0x%08x",
  4602. (prefix ? prefix : ""), (u32)insn, ((u32)insn -
  4603. (u32)&(((struct NCR53c7x0_hostdata *)host->hostdata[0])->script))/4,
  4604. insn[0], insn[1]);
  4605. #endif
  4606. tmp = buf + strlen(buf);
  4607. if ((dcmd & DCMD_TYPE_MASK) == DCMD_TYPE_MMI) {
  4608. #if !defined(CONFIG_MVME16x) && !defined(CONFIG_BVME6000)
  4609. sprintf (tmp, " 0x%08x (virt 0x%p)\n", insn[2],
  4610. bus_to_virt(insn[2]));
  4611. #else
  4612. /* Remove virtual addr to reduce output, as it is the same */
  4613. sprintf (tmp, " 0x%08x\n", insn[2]);
  4614. #endif
  4615. size = 3;
  4616. } else {
  4617. sprintf (tmp, "\n");
  4618. size = 2;
  4619. }
  4620. }
  4621. if (kernel)
  4622. printk ("%s", buf);
  4623. #ifdef NCR_DEBUG
  4624. else {
  4625. size_t len = strlen(buf);
  4626. debugger_kernel_write(host, buf, len);
  4627. }
  4628. #endif
  4629. return size;
  4630. }
  4631. /*
  4632. * Function : int NCR53c7xx_abort (Scsi_Cmnd *cmd)
  4633. *
  4634. * Purpose : Abort an errant SCSI command, doing all necessary
  4635. * cleanup of the issue_queue, running_list, shared Linux/NCR
  4636. * dsa issue and reconnect queues.
  4637. *
  4638. * Inputs : cmd - command to abort, code - entire result field
  4639. *
  4640. * Returns : 0 on success, -1 on failure.
  4641. */
  4642. int
  4643. NCR53c7xx_abort (Scsi_Cmnd *cmd) {
  4644. NCR53c7x0_local_declare();
  4645. struct Scsi_Host *host = cmd->device->host;
  4646. struct NCR53c7x0_hostdata *hostdata = host ? (struct NCR53c7x0_hostdata *)
  4647. host->hostdata[0] : NULL;
  4648. unsigned long flags;
  4649. struct NCR53c7x0_cmd *curr, **prev;
  4650. Scsi_Cmnd *me, **last;
  4651. #if 0
  4652. static long cache_pid = -1;
  4653. #endif
  4654. if (!host) {
  4655. printk ("Bogus SCSI command pid %ld; no host structure\n",
  4656. cmd->pid);
  4657. return SCSI_ABORT_ERROR;
  4658. } else if (!hostdata) {
  4659. printk ("Bogus SCSI host %d; no hostdata\n", host->host_no);
  4660. return SCSI_ABORT_ERROR;
  4661. }
  4662. NCR53c7x0_local_setup(host);
  4663. /*
  4664. * CHECK : I don't think that reading ISTAT will unstack any interrupts,
  4665. * since we need to write the INTF bit to clear it, and SCSI/DMA
  4666. * interrupts don't clear until we read SSTAT/SIST and DSTAT registers.
  4667. *
  4668. * See that this is the case. Appears to be correct on the 710, at least.
  4669. *
  4670. * I suspect that several of our failures may be coming from a new fatal
  4671. * interrupt (possibly due to a phase mismatch) happening after we've left
  4672. * the interrupt handler, but before the PIC has had the interrupt condition
  4673. * cleared.
  4674. */
  4675. if (NCR53c7x0_read8(hostdata->istat) & (ISTAT_DIP|ISTAT_SIP)) {
  4676. printk ("scsi%d : dropped interrupt for command %ld\n", host->host_no,
  4677. cmd->pid);
  4678. NCR53c7x0_intr (host->irq, NULL, NULL);
  4679. return SCSI_ABORT_BUSY;
  4680. }
  4681. local_irq_save(flags);
  4682. #if 0
  4683. if (cache_pid == cmd->pid)
  4684. panic ("scsi%d : bloody fetus %d\n", host->host_no, cmd->pid);
  4685. else
  4686. cache_pid = cmd->pid;
  4687. #endif
  4688. /*
  4689. * The command could be hiding in the issue_queue. This would be very
  4690. * nice, as commands can't be moved from the high level driver's issue queue
  4691. * into the shared queue until an interrupt routine is serviced, and this
  4692. * moving is atomic.
  4693. *
  4694. * If this is the case, we don't have to worry about anything - we simply
  4695. * pull the command out of the old queue, and call it aborted.
  4696. */
  4697. for (me = (Scsi_Cmnd *) hostdata->issue_queue,
  4698. last = (Scsi_Cmnd **) &(hostdata->issue_queue);
  4699. me && me != cmd; last = (Scsi_Cmnd **)&(me->SCp.ptr),
  4700. me = (Scsi_Cmnd *)me->SCp.ptr);
  4701. if (me) {
  4702. *last = (Scsi_Cmnd *) me->SCp.ptr;
  4703. if (me->host_scribble) {
  4704. ((struct NCR53c7x0_cmd *)me->host_scribble)->next = hostdata->free;
  4705. hostdata->free = (struct NCR53c7x0_cmd *) me->host_scribble;
  4706. me->host_scribble = NULL;
  4707. }
  4708. cmd->result = DID_ABORT << 16;
  4709. cmd->scsi_done(cmd);
  4710. printk ("scsi%d : found command %ld in Linux issue queue\n",
  4711. host->host_no, me->pid);
  4712. local_irq_restore(flags);
  4713. run_process_issue_queue();
  4714. return SCSI_ABORT_SUCCESS;
  4715. }
  4716. /*
  4717. * That failing, the command could be in our list of already executing
  4718. * commands. If this is the case, drastic measures are called for.
  4719. */
  4720. for (curr = (struct NCR53c7x0_cmd *) hostdata->running_list,
  4721. prev = (struct NCR53c7x0_cmd **) &(hostdata->running_list);
  4722. curr && curr->cmd != cmd; prev = (struct NCR53c7x0_cmd **)
  4723. &(curr->next), curr = (struct NCR53c7x0_cmd *) curr->next);
  4724. if (curr) {
  4725. if ((curr->result & 0xff) != 0xff && (curr->result & 0xff00) != 0xff00) {
  4726. cmd->result = curr->result;
  4727. if (prev)
  4728. *prev = (struct NCR53c7x0_cmd *) curr->next;
  4729. curr->next = (struct NCR53c7x0_cmd *) hostdata->free;
  4730. cmd->host_scribble = NULL;
  4731. hostdata->free = curr;
  4732. cmd->scsi_done(cmd);
  4733. printk ("scsi%d : found finished command %ld in running list\n",
  4734. host->host_no, cmd->pid);
  4735. local_irq_restore(flags);
  4736. return SCSI_ABORT_NOT_RUNNING;
  4737. } else {
  4738. printk ("scsi%d : DANGER : command running, can not abort.\n",
  4739. cmd->device->host->host_no);
  4740. local_irq_restore(flags);
  4741. return SCSI_ABORT_BUSY;
  4742. }
  4743. }
  4744. /*
  4745. * And if we couldn't find it in any of our queues, it must have been
  4746. * a dropped interrupt.
  4747. */
  4748. curr = (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4749. if (curr) {
  4750. curr->next = hostdata->free;
  4751. hostdata->free = curr;
  4752. cmd->host_scribble = NULL;
  4753. }
  4754. if (curr == NULL || ((curr->result & 0xff00) == 0xff00) ||
  4755. ((curr->result & 0xff) == 0xff)) {
  4756. printk ("scsi%d : did this command ever run?\n", host->host_no);
  4757. cmd->result = DID_ABORT << 16;
  4758. } else {
  4759. printk ("scsi%d : probably lost INTFLY, normal completion\n",
  4760. host->host_no);
  4761. cmd->result = curr->result;
  4762. /*
  4763. * FIXME : We need to add an additional flag which indicates if a
  4764. * command was ever counted as BUSY, so if we end up here we can
  4765. * decrement the busy count if and only if it is necessary.
  4766. */
  4767. --hostdata->busy[cmd->device->id][cmd->device->lun];
  4768. }
  4769. local_irq_restore(flags);
  4770. cmd->scsi_done(cmd);
  4771. /*
  4772. * We need to run process_issue_queue since termination of this command
  4773. * may allow another queued command to execute first?
  4774. */
  4775. return SCSI_ABORT_NOT_RUNNING;
  4776. }
  4777. /*
  4778. * Function : int NCR53c7xx_reset (Scsi_Cmnd *cmd)
  4779. *
  4780. * Purpose : perform a hard reset of the SCSI bus and NCR
  4781. * chip.
  4782. *
  4783. * Inputs : cmd - command which caused the SCSI RESET
  4784. *
  4785. * Returns : 0 on success.
  4786. */
  4787. int
  4788. NCR53c7xx_reset (Scsi_Cmnd *cmd, unsigned int reset_flags) {
  4789. NCR53c7x0_local_declare();
  4790. unsigned long flags;
  4791. int found = 0;
  4792. struct NCR53c7x0_cmd * c;
  4793. Scsi_Cmnd *tmp;
  4794. /*
  4795. * When we call scsi_done(), it's going to wake up anything sleeping on the
  4796. * resources which were in use by the aborted commands, and we'll start to
  4797. * get new commands.
  4798. *
  4799. * We can't let this happen until after we've re-initialized the driver
  4800. * structures, and can't reinitialize those structures until after we've
  4801. * dealt with their contents.
  4802. *
  4803. * So, we need to find all of the commands which were running, stick
  4804. * them on a linked list of completed commands (we'll use the host_scribble
  4805. * pointer), do our reinitialization, and then call the done function for
  4806. * each command.
  4807. */
  4808. Scsi_Cmnd *nuke_list = NULL;
  4809. struct Scsi_Host *host = cmd->device->host;
  4810. struct NCR53c7x0_hostdata *hostdata =
  4811. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  4812. NCR53c7x0_local_setup(host);
  4813. local_irq_save(flags);
  4814. ncr_halt (host);
  4815. print_lots (host);
  4816. dump_events (host, 30);
  4817. ncr_scsi_reset (host);
  4818. for (tmp = nuke_list = return_outstanding_commands (host, 1 /* free */,
  4819. 0 /* issue */ ); tmp; tmp = (Scsi_Cmnd *) tmp->SCp.buffer)
  4820. if (tmp == cmd) {
  4821. found = 1;
  4822. break;
  4823. }
  4824. /*
  4825. * If we didn't find the command which caused this reset in our running
  4826. * list, then we've lost it. See that it terminates normally anyway.
  4827. */
  4828. if (!found) {
  4829. c = (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4830. if (c) {
  4831. cmd->host_scribble = NULL;
  4832. c->next = hostdata->free;
  4833. hostdata->free = c;
  4834. } else
  4835. printk ("scsi%d: lost command %ld\n", host->host_no, cmd->pid);
  4836. cmd->SCp.buffer = (struct scatterlist *) nuke_list;
  4837. nuke_list = cmd;
  4838. }
  4839. NCR53c7x0_driver_init (host);
  4840. hostdata->soft_reset (host);
  4841. if (hostdata->resets == 0)
  4842. disable(host);
  4843. else if (hostdata->resets != -1)
  4844. --hostdata->resets;
  4845. local_irq_restore(flags);
  4846. for (; nuke_list; nuke_list = tmp) {
  4847. tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
  4848. nuke_list->result = DID_RESET << 16;
  4849. nuke_list->scsi_done (nuke_list);
  4850. }
  4851. local_irq_restore(flags);
  4852. return SCSI_RESET_SUCCESS;
  4853. }
  4854. /*
  4855. * The NCR SDMS bios follows Annex A of the SCSI-CAM draft, and
  4856. * therefore shares the scsicam_bios_param function.
  4857. */
  4858. /*
  4859. * Function : int insn_to_offset (Scsi_Cmnd *cmd, u32 *insn)
  4860. *
  4861. * Purpose : convert instructions stored at NCR pointer into data
  4862. * pointer offset.
  4863. *
  4864. * Inputs : cmd - SCSI command; insn - pointer to instruction. Either current
  4865. * DSP, or saved data pointer.
  4866. *
  4867. * Returns : offset on success, -1 on failure.
  4868. */
  4869. static int
  4870. insn_to_offset (Scsi_Cmnd *cmd, u32 *insn) {
  4871. struct NCR53c7x0_hostdata *hostdata =
  4872. (struct NCR53c7x0_hostdata *) cmd->device->host->hostdata[0];
  4873. struct NCR53c7x0_cmd *ncmd =
  4874. (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4875. int offset = 0, buffers;
  4876. struct scatterlist *segment;
  4877. char *ptr;
  4878. int found = 0;
  4879. /*
  4880. * With the current code implementation, if the insn is inside dynamically
  4881. * generated code, the data pointer will be the instruction preceding
  4882. * the next transfer segment.
  4883. */
  4884. if (!check_address ((unsigned long) ncmd, sizeof (struct NCR53c7x0_cmd)) &&
  4885. ((insn >= ncmd->data_transfer_start &&
  4886. insn < ncmd->data_transfer_end) ||
  4887. (insn >= ncmd->residual &&
  4888. insn < (ncmd->residual +
  4889. sizeof(ncmd->residual))))) {
  4890. ptr = bus_to_virt(insn[3]);
  4891. if ((buffers = cmd->use_sg)) {
  4892. for (offset = 0,
  4893. segment = (struct scatterlist *) cmd->request_buffer;
  4894. buffers && !((found = ((ptr >= (char *)page_address(segment->page)+segment->offset) &&
  4895. (ptr < ((char *)page_address(segment->page)+segment->offset+segment->length)))));
  4896. --buffers, offset += segment->length, ++segment)
  4897. #if 0
  4898. printk("scsi%d: comparing 0x%p to 0x%p\n",
  4899. cmd->device->host->host_no, saved, page_address(segment->page+segment->offset);
  4900. #else
  4901. ;
  4902. #endif
  4903. offset += ptr - ((char *)page_address(segment->page)+segment->offset);
  4904. } else {
  4905. found = 1;
  4906. offset = ptr - (char *) (cmd->request_buffer);
  4907. }
  4908. } else if ((insn >= hostdata->script +
  4909. hostdata->E_data_transfer / sizeof(u32)) &&
  4910. (insn <= hostdata->script +
  4911. hostdata->E_end_data_transfer / sizeof(u32))) {
  4912. found = 1;
  4913. offset = 0;
  4914. }
  4915. return found ? offset : -1;
  4916. }
  4917. /*
  4918. * Function : void print_progress (Scsi_Cmnd *cmd)
  4919. *
  4920. * Purpose : print the current location of the saved data pointer
  4921. *
  4922. * Inputs : cmd - command we are interested in
  4923. *
  4924. */
  4925. static void
  4926. print_progress (Scsi_Cmnd *cmd) {
  4927. NCR53c7x0_local_declare();
  4928. struct NCR53c7x0_cmd *ncmd =
  4929. (struct NCR53c7x0_cmd *) cmd->host_scribble;
  4930. int offset, i;
  4931. char *where;
  4932. u32 *ptr;
  4933. NCR53c7x0_local_setup (cmd->device->host);
  4934. if (check_address ((unsigned long) ncmd,sizeof (struct NCR53c7x0_cmd)) == 0)
  4935. {
  4936. printk("\nNCR53c7x0_cmd fields:\n");
  4937. printk(" bounce.len=0x%x, addr=0x%0x, buf[]=0x%02x %02x %02x %02x\n",
  4938. ncmd->bounce.len, ncmd->bounce.addr, ncmd->bounce.buf[0],
  4939. ncmd->bounce.buf[1], ncmd->bounce.buf[2], ncmd->bounce.buf[3]);
  4940. printk(" result=%04x, cdb[0]=0x%02x\n", ncmd->result, ncmd->cmnd[0]);
  4941. }
  4942. for (i = 0; i < 2; ++i) {
  4943. if (check_address ((unsigned long) ncmd,
  4944. sizeof (struct NCR53c7x0_cmd)) == -1)
  4945. continue;
  4946. if (!i) {
  4947. where = "saved";
  4948. ptr = bus_to_virt(ncmd->saved_data_pointer);
  4949. } else {
  4950. where = "active";
  4951. ptr = bus_to_virt (NCR53c7x0_read32 (DSP_REG) -
  4952. NCR53c7x0_insn_size (NCR53c7x0_read8 (DCMD_REG)) *
  4953. sizeof(u32));
  4954. }
  4955. offset = insn_to_offset (cmd, ptr);
  4956. if (offset != -1)
  4957. printk ("scsi%d : %s data pointer at offset %d\n",
  4958. cmd->device->host->host_no, where, offset);
  4959. else {
  4960. int size;
  4961. printk ("scsi%d : can't determine %s data pointer offset\n",
  4962. cmd->device->host->host_no, where);
  4963. if (ncmd) {
  4964. size = print_insn (cmd->device->host,
  4965. bus_to_virt(ncmd->saved_data_pointer), "", 1);
  4966. print_insn (cmd->device->host,
  4967. bus_to_virt(ncmd->saved_data_pointer) + size * sizeof(u32),
  4968. "", 1);
  4969. }
  4970. }
  4971. }
  4972. }
  4973. static void
  4974. print_dsa (struct Scsi_Host *host, u32 *dsa, const char *prefix) {
  4975. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  4976. host->hostdata[0];
  4977. int i, len;
  4978. char *ptr;
  4979. Scsi_Cmnd *cmd;
  4980. if (check_address ((unsigned long) dsa, hostdata->dsa_end -
  4981. hostdata->dsa_start) == -1) {
  4982. printk("scsi%d : bad dsa virt 0x%p\n", host->host_no, dsa);
  4983. return;
  4984. }
  4985. printk("%sscsi%d : dsa at phys 0x%lx (virt 0x%p)\n"
  4986. " + %d : dsa_msgout length = %u, data = 0x%x (virt 0x%p)\n" ,
  4987. prefix ? prefix : "",
  4988. host->host_no, virt_to_bus (dsa), dsa, hostdata->dsa_msgout,
  4989. dsa[hostdata->dsa_msgout / sizeof(u32)],
  4990. dsa[hostdata->dsa_msgout / sizeof(u32) + 1],
  4991. bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]));
  4992. /*
  4993. * Only print messages if they're sane in length so we don't
  4994. * blow the kernel printk buffer on something which won't buy us
  4995. * anything.
  4996. */
  4997. if (dsa[hostdata->dsa_msgout / sizeof(u32)] <
  4998. sizeof (hostdata->free->select))
  4999. for (i = dsa[hostdata->dsa_msgout / sizeof(u32)],
  5000. ptr = bus_to_virt (dsa[hostdata->dsa_msgout / sizeof(u32) + 1]);
  5001. i > 0 && !check_address ((unsigned long) ptr, 1);
  5002. ptr += len, i -= len) {
  5003. printk(" ");
  5004. len = spi_print_msg(ptr);
  5005. printk("\n");
  5006. if (!len)
  5007. break;
  5008. }
  5009. printk(" + %d : select_indirect = 0x%x\n",
  5010. hostdata->dsa_select, dsa[hostdata->dsa_select / sizeof(u32)]);
  5011. cmd = (Scsi_Cmnd *) bus_to_virt(dsa[hostdata->dsa_cmnd / sizeof(u32)]);
  5012. printk(" + %d : dsa_cmnd = 0x%x ", hostdata->dsa_cmnd,
  5013. (u32) virt_to_bus(cmd));
  5014. /* XXX Maybe we should access cmd->host_scribble->result here. RGH */
  5015. if (cmd) {
  5016. printk(" result = 0x%x, target = %d, lun = %d, cmd = ",
  5017. cmd->result, cmd->device->id, cmd->device->lun);
  5018. __scsi_print_command(cmd->cmnd);
  5019. } else
  5020. printk("\n");
  5021. printk(" + %d : dsa_next = 0x%x\n", hostdata->dsa_next,
  5022. dsa[hostdata->dsa_next / sizeof(u32)]);
  5023. if (cmd) {
  5024. printk("scsi%d target %d : sxfer_sanity = 0x%x, scntl3_sanity = 0x%x\n"
  5025. " script : ",
  5026. host->host_no, cmd->device->id,
  5027. hostdata->sync[cmd->device->id].sxfer_sanity,
  5028. hostdata->sync[cmd->device->id].scntl3_sanity);
  5029. for (i = 0; i < (sizeof(hostdata->sync[cmd->device->id].script) / 4); ++i)
  5030. printk ("0x%x ", hostdata->sync[cmd->device->id].script[i]);
  5031. printk ("\n");
  5032. print_progress (cmd);
  5033. }
  5034. }
  5035. /*
  5036. * Function : void print_queues (Scsi_Host *host)
  5037. *
  5038. * Purpose : print the contents of the NCR issue and reconnect queues
  5039. *
  5040. * Inputs : host - SCSI host we are interested in
  5041. *
  5042. */
  5043. static void
  5044. print_queues (struct Scsi_Host *host) {
  5045. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5046. host->hostdata[0];
  5047. u32 *dsa, *next_dsa;
  5048. volatile u32 *ncrcurrent;
  5049. int left;
  5050. Scsi_Cmnd *cmd, *next_cmd;
  5051. unsigned long flags;
  5052. printk ("scsi%d : issue queue\n", host->host_no);
  5053. for (left = host->can_queue, cmd = (Scsi_Cmnd *) hostdata->issue_queue;
  5054. left >= 0 && cmd;
  5055. cmd = next_cmd) {
  5056. next_cmd = (Scsi_Cmnd *) cmd->SCp.ptr;
  5057. local_irq_save(flags);
  5058. if (cmd->host_scribble) {
  5059. if (check_address ((unsigned long) (cmd->host_scribble),
  5060. sizeof (cmd->host_scribble)) == -1)
  5061. printk ("scsi%d: scsi pid %ld bad pointer to NCR53c7x0_cmd\n",
  5062. host->host_no, cmd->pid);
  5063. /* print_dsa does sanity check on address, no need to check */
  5064. else
  5065. print_dsa (host, ((struct NCR53c7x0_cmd *) cmd->host_scribble)
  5066. -> dsa, "");
  5067. } else
  5068. printk ("scsi%d : scsi pid %ld for target %d lun %d has no NCR53c7x0_cmd\n",
  5069. host->host_no, cmd->pid, cmd->device->id, cmd->device->lun);
  5070. local_irq_restore(flags);
  5071. }
  5072. if (left <= 0) {
  5073. printk ("scsi%d : loop detected in issue queue\n",
  5074. host->host_no);
  5075. }
  5076. /*
  5077. * Traverse the NCR reconnect and start DSA structures, printing out
  5078. * each element until we hit the end or detect a loop. Currently,
  5079. * the reconnect structure is a linked list; and the start structure
  5080. * is an array. Eventually, the reconnect structure will become a
  5081. * list as well, since this simplifies the code.
  5082. */
  5083. printk ("scsi%d : schedule dsa array :\n", host->host_no);
  5084. for (left = host->can_queue, ncrcurrent = hostdata->schedule;
  5085. left > 0; ncrcurrent += 2, --left)
  5086. if (ncrcurrent[0] != hostdata->NOP_insn)
  5087. /* FIXME : convert pointer to dsa_begin to pointer to dsa. */
  5088. print_dsa (host, bus_to_virt (ncrcurrent[1] -
  5089. (hostdata->E_dsa_code_begin -
  5090. hostdata->E_dsa_code_template)), "");
  5091. printk ("scsi%d : end schedule dsa array\n", host->host_no);
  5092. printk ("scsi%d : reconnect_dsa_head :\n", host->host_no);
  5093. for (left = host->can_queue,
  5094. dsa = bus_to_virt (hostdata->reconnect_dsa_head);
  5095. left >= 0 && dsa;
  5096. dsa = next_dsa) {
  5097. local_irq_save(flags);
  5098. if (check_address ((unsigned long) dsa, sizeof(dsa)) == -1) {
  5099. printk ("scsi%d: bad DSA pointer 0x%p", host->host_no,
  5100. dsa);
  5101. next_dsa = NULL;
  5102. }
  5103. else
  5104. {
  5105. next_dsa = bus_to_virt(dsa[hostdata->dsa_next / sizeof(u32)]);
  5106. print_dsa (host, dsa, "");
  5107. }
  5108. local_irq_restore(flags);
  5109. }
  5110. printk ("scsi%d : end reconnect_dsa_head\n", host->host_no);
  5111. if (left < 0)
  5112. printk("scsi%d: possible loop in ncr reconnect list\n",
  5113. host->host_no);
  5114. }
  5115. static void
  5116. print_lots (struct Scsi_Host *host) {
  5117. NCR53c7x0_local_declare();
  5118. struct NCR53c7x0_hostdata *hostdata =
  5119. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  5120. u32 *dsp_next, *dsp, *dsa, dbc_dcmd;
  5121. unsigned char dcmd, sbcl;
  5122. int i, size;
  5123. NCR53c7x0_local_setup(host);
  5124. if ((dsp_next = bus_to_virt(NCR53c7x0_read32 (DSP_REG)))) {
  5125. dbc_dcmd = NCR53c7x0_read32(DBC_REG);
  5126. dcmd = (dbc_dcmd & 0xff000000) >> 24;
  5127. dsp = dsp_next - NCR53c7x0_insn_size(dcmd);
  5128. dsa = bus_to_virt(NCR53c7x0_read32(DSA_REG));
  5129. sbcl = NCR53c7x0_read8 (SBCL_REG);
  5130. /*
  5131. * For the 53c710, the following will report value 0 for SCNTL3
  5132. * and STEST0 - we don't have these registers.
  5133. */
  5134. printk ("scsi%d : DCMD|DBC=0x%x, DNAD=0x%x (virt 0x%p)\n"
  5135. " DSA=0x%lx (virt 0x%p)\n"
  5136. " DSPS=0x%x, TEMP=0x%x (virt 0x%p), DMODE=0x%x\n"
  5137. " SXFER=0x%x, SCNTL3=0x%x\n"
  5138. " %s%s%sphase=%s, %d bytes in SCSI FIFO\n"
  5139. " SCRATCH=0x%x, saved2_dsa=0x%0lx\n",
  5140. host->host_no, dbc_dcmd, NCR53c7x0_read32(DNAD_REG),
  5141. bus_to_virt(NCR53c7x0_read32(DNAD_REG)),
  5142. virt_to_bus(dsa), dsa,
  5143. NCR53c7x0_read32(DSPS_REG), NCR53c7x0_read32(TEMP_REG),
  5144. bus_to_virt (NCR53c7x0_read32(TEMP_REG)),
  5145. (int) NCR53c7x0_read8(hostdata->dmode),
  5146. (int) NCR53c7x0_read8(SXFER_REG),
  5147. ((hostdata->chip / 100) == 8) ?
  5148. (int) NCR53c7x0_read8(SCNTL3_REG_800) : 0,
  5149. (sbcl & SBCL_BSY) ? "BSY " : "",
  5150. (sbcl & SBCL_SEL) ? "SEL " : "",
  5151. (sbcl & SBCL_REQ) ? "REQ " : "",
  5152. sstat2_to_phase(NCR53c7x0_read8 (((hostdata->chip / 100) == 8) ?
  5153. SSTAT1_REG : SSTAT2_REG)),
  5154. (NCR53c7x0_read8 ((hostdata->chip / 100) == 8 ?
  5155. SSTAT1_REG : SSTAT2_REG) & SSTAT2_FF_MASK) >> SSTAT2_FF_SHIFT,
  5156. ((hostdata->chip / 100) == 8) ? NCR53c7x0_read8 (STEST0_REG_800) :
  5157. NCR53c7x0_read32(SCRATCHA_REG_800),
  5158. hostdata->saved2_dsa);
  5159. printk ("scsi%d : DSP 0x%lx (virt 0x%p) ->\n", host->host_no,
  5160. virt_to_bus(dsp), dsp);
  5161. for (i = 6; i > 0; --i, dsp += size)
  5162. size = print_insn (host, dsp, "", 1);
  5163. if (NCR53c7x0_read8 (SCNTL1_REG) & SCNTL1_CON) {
  5164. if ((hostdata->chip / 100) == 8)
  5165. printk ("scsi%d : connected (SDID=0x%x, SSID=0x%x)\n",
  5166. host->host_no, NCR53c7x0_read8 (SDID_REG_800),
  5167. NCR53c7x0_read8 (SSID_REG_800));
  5168. else
  5169. printk ("scsi%d : connected (SDID=0x%x)\n",
  5170. host->host_no, NCR53c7x0_read8 (SDID_REG_700));
  5171. print_dsa (host, dsa, "");
  5172. }
  5173. #if 1
  5174. print_queues (host);
  5175. #endif
  5176. }
  5177. }
  5178. /*
  5179. * Function : static int shutdown (struct Scsi_Host *host)
  5180. *
  5181. * Purpose : does a clean (we hope) shutdown of the NCR SCSI
  5182. * chip. Use prior to dumping core, unloading the NCR driver,
  5183. *
  5184. * Returns : 0 on success
  5185. */
  5186. static int
  5187. shutdown (struct Scsi_Host *host) {
  5188. NCR53c7x0_local_declare();
  5189. unsigned long flags;
  5190. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5191. host->hostdata[0];
  5192. NCR53c7x0_local_setup(host);
  5193. local_irq_save(flags);
  5194. /* Get in a state where we can reset the SCSI bus */
  5195. ncr_halt (host);
  5196. ncr_scsi_reset (host);
  5197. hostdata->soft_reset(host);
  5198. disable (host);
  5199. local_irq_restore(flags);
  5200. return 0;
  5201. }
  5202. /*
  5203. * Function : void ncr_scsi_reset (struct Scsi_Host *host)
  5204. *
  5205. * Purpose : reset the SCSI bus.
  5206. */
  5207. static void
  5208. ncr_scsi_reset (struct Scsi_Host *host) {
  5209. NCR53c7x0_local_declare();
  5210. unsigned long flags;
  5211. NCR53c7x0_local_setup(host);
  5212. local_irq_save(flags);
  5213. NCR53c7x0_write8(SCNTL1_REG, SCNTL1_RST);
  5214. udelay(25); /* Minimum amount of time to assert RST */
  5215. NCR53c7x0_write8(SCNTL1_REG, 0);
  5216. local_irq_restore(flags);
  5217. }
  5218. /*
  5219. * Function : void hard_reset (struct Scsi_Host *host)
  5220. *
  5221. */
  5222. static void
  5223. hard_reset (struct Scsi_Host *host) {
  5224. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5225. host->hostdata[0];
  5226. unsigned long flags;
  5227. local_irq_save(flags);
  5228. ncr_scsi_reset(host);
  5229. NCR53c7x0_driver_init (host);
  5230. if (hostdata->soft_reset)
  5231. hostdata->soft_reset (host);
  5232. local_irq_restore(flags);
  5233. }
  5234. /*
  5235. * Function : Scsi_Cmnd *return_outstanding_commands (struct Scsi_Host *host,
  5236. * int free, int issue)
  5237. *
  5238. * Purpose : return a linked list (using the SCp.buffer field as next,
  5239. * so we don't perturb hostdata. We don't use a field of the
  5240. * NCR53c7x0_cmd structure since we may not have allocated one
  5241. * for the command causing the reset.) of Scsi_Cmnd structures that
  5242. * had propagated below the Linux issue queue level. If free is set,
  5243. * free the NCR53c7x0_cmd structures which are associated with
  5244. * the Scsi_Cmnd structures, and clean up any internal
  5245. * NCR lists that the commands were on. If issue is set,
  5246. * also return commands in the issue queue.
  5247. *
  5248. * Returns : linked list of commands
  5249. *
  5250. * NOTE : the caller should insure that the NCR chip is halted
  5251. * if the free flag is set.
  5252. */
  5253. static Scsi_Cmnd *
  5254. return_outstanding_commands (struct Scsi_Host *host, int free, int issue) {
  5255. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5256. host->hostdata[0];
  5257. struct NCR53c7x0_cmd *c;
  5258. int i;
  5259. u32 *ncrcurrent;
  5260. Scsi_Cmnd *list = NULL, *tmp;
  5261. for (c = (struct NCR53c7x0_cmd *) hostdata->running_list; c;
  5262. c = (struct NCR53c7x0_cmd *) c->next) {
  5263. if (c->cmd->SCp.buffer) {
  5264. printk ("scsi%d : loop detected in running list!\n", host->host_no);
  5265. break;
  5266. } else {
  5267. printk ("Duh? Bad things happening in the NCR driver\n");
  5268. break;
  5269. }
  5270. c->cmd->SCp.buffer = (struct scatterlist *) list;
  5271. list = c->cmd;
  5272. if (free) {
  5273. c->next = hostdata->free;
  5274. hostdata->free = c;
  5275. }
  5276. }
  5277. if (free) {
  5278. for (i = 0, ncrcurrent = (u32 *) hostdata->schedule;
  5279. i < host->can_queue; ++i, ncrcurrent += 2) {
  5280. ncrcurrent[0] = hostdata->NOP_insn;
  5281. ncrcurrent[1] = 0xdeadbeef;
  5282. }
  5283. hostdata->ncrcurrent = NULL;
  5284. }
  5285. if (issue) {
  5286. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; tmp = tmp->next) {
  5287. if (tmp->SCp.buffer) {
  5288. printk ("scsi%d : loop detected in issue queue!\n",
  5289. host->host_no);
  5290. break;
  5291. }
  5292. tmp->SCp.buffer = (struct scatterlist *) list;
  5293. list = tmp;
  5294. }
  5295. if (free)
  5296. hostdata->issue_queue = NULL;
  5297. }
  5298. return list;
  5299. }
  5300. /*
  5301. * Function : static int disable (struct Scsi_Host *host)
  5302. *
  5303. * Purpose : disables the given NCR host, causing all commands
  5304. * to return a driver error. Call this so we can unload the
  5305. * module during development and try again. Eventually,
  5306. * we should be able to find clean workarounds for these
  5307. * problems.
  5308. *
  5309. * Inputs : host - hostadapter to twiddle
  5310. *
  5311. * Returns : 0 on success.
  5312. */
  5313. static int
  5314. disable (struct Scsi_Host *host) {
  5315. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5316. host->hostdata[0];
  5317. unsigned long flags;
  5318. Scsi_Cmnd *nuke_list, *tmp;
  5319. local_irq_save(flags);
  5320. if (hostdata->state != STATE_HALTED)
  5321. ncr_halt (host);
  5322. nuke_list = return_outstanding_commands (host, 1 /* free */, 1 /* issue */);
  5323. hard_reset (host);
  5324. hostdata->state = STATE_DISABLED;
  5325. local_irq_restore(flags);
  5326. printk ("scsi%d : nuking commands\n", host->host_no);
  5327. for (; nuke_list; nuke_list = tmp) {
  5328. tmp = (Scsi_Cmnd *) nuke_list->SCp.buffer;
  5329. nuke_list->result = DID_ERROR << 16;
  5330. nuke_list->scsi_done(nuke_list);
  5331. }
  5332. printk ("scsi%d : done. \n", host->host_no);
  5333. printk (KERN_ALERT "scsi%d : disabled. Unload and reload\n",
  5334. host->host_no);
  5335. return 0;
  5336. }
  5337. /*
  5338. * Function : static int ncr_halt (struct Scsi_Host *host)
  5339. *
  5340. * Purpose : halts the SCSI SCRIPTS(tm) processor on the NCR chip
  5341. *
  5342. * Inputs : host - SCSI chip to halt
  5343. *
  5344. * Returns : 0 on success
  5345. */
  5346. static int
  5347. ncr_halt (struct Scsi_Host *host) {
  5348. NCR53c7x0_local_declare();
  5349. unsigned long flags;
  5350. unsigned char istat, tmp;
  5351. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5352. host->hostdata[0];
  5353. int stage;
  5354. NCR53c7x0_local_setup(host);
  5355. local_irq_save(flags);
  5356. /* Stage 0 : eat all interrupts
  5357. Stage 1 : set ABORT
  5358. Stage 2 : eat all but abort interrupts
  5359. Stage 3 : eat all interrupts
  5360. */
  5361. for (stage = 0;;) {
  5362. if (stage == 1) {
  5363. NCR53c7x0_write8(hostdata->istat, ISTAT_ABRT);
  5364. ++stage;
  5365. }
  5366. istat = NCR53c7x0_read8 (hostdata->istat);
  5367. if (istat & ISTAT_SIP) {
  5368. tmp = NCR53c7x0_read8(SSTAT0_REG);
  5369. } else if (istat & ISTAT_DIP) {
  5370. tmp = NCR53c7x0_read8(DSTAT_REG);
  5371. if (stage == 2) {
  5372. if (tmp & DSTAT_ABRT) {
  5373. NCR53c7x0_write8(hostdata->istat, 0);
  5374. ++stage;
  5375. } else {
  5376. printk(KERN_ALERT "scsi%d : could not halt NCR chip\n",
  5377. host->host_no);
  5378. disable (host);
  5379. }
  5380. }
  5381. }
  5382. if (!(istat & (ISTAT_SIP|ISTAT_DIP))) {
  5383. if (stage == 0)
  5384. ++stage;
  5385. else if (stage == 3)
  5386. break;
  5387. }
  5388. }
  5389. hostdata->state = STATE_HALTED;
  5390. local_irq_restore(flags);
  5391. #if 0
  5392. print_lots (host);
  5393. #endif
  5394. return 0;
  5395. }
  5396. /*
  5397. * Function: event_name (int event)
  5398. *
  5399. * Purpose: map event enum into user-readable strings.
  5400. */
  5401. static const char *
  5402. event_name (int event) {
  5403. switch (event) {
  5404. case EVENT_NONE: return "none";
  5405. case EVENT_ISSUE_QUEUE: return "to issue queue";
  5406. case EVENT_START_QUEUE: return "to start queue";
  5407. case EVENT_SELECT: return "selected";
  5408. case EVENT_DISCONNECT: return "disconnected";
  5409. case EVENT_RESELECT: return "reselected";
  5410. case EVENT_COMPLETE: return "completed";
  5411. case EVENT_IDLE: return "idle";
  5412. case EVENT_SELECT_FAILED: return "select failed";
  5413. case EVENT_BEFORE_SELECT: return "before select";
  5414. case EVENT_RESELECT_FAILED: return "reselect failed";
  5415. default: return "unknown";
  5416. }
  5417. }
  5418. /*
  5419. * Function : void dump_events (struct Scsi_Host *host, count)
  5420. *
  5421. * Purpose : print last count events which have occurred.
  5422. */
  5423. static void
  5424. dump_events (struct Scsi_Host *host, int count) {
  5425. struct NCR53c7x0_hostdata *hostdata = (struct NCR53c7x0_hostdata *)
  5426. host->hostdata[0];
  5427. struct NCR53c7x0_event event;
  5428. int i;
  5429. unsigned long flags;
  5430. if (hostdata->events) {
  5431. if (count > hostdata->event_size)
  5432. count = hostdata->event_size;
  5433. for (i = hostdata->event_index; count > 0;
  5434. i = (i ? i - 1 : hostdata->event_size -1), --count) {
  5435. /*
  5436. * By copying the event we're currently examining with interrupts
  5437. * disabled, we can do multiple printk(), etc. operations and
  5438. * still be guaranteed that they're happening on the same
  5439. * event structure.
  5440. */
  5441. local_irq_save(flags);
  5442. #if 0
  5443. event = hostdata->events[i];
  5444. #else
  5445. memcpy ((void *) &event, (void *) &(hostdata->events[i]),
  5446. sizeof(event));
  5447. #endif
  5448. local_irq_restore(flags);
  5449. printk ("scsi%d : %s event %d at %ld secs %ld usecs target %d lun %d\n",
  5450. host->host_no, event_name (event.event), count,
  5451. (long) event.time.tv_sec, (long) event.time.tv_usec,
  5452. event.target, event.lun);
  5453. if (event.dsa)
  5454. printk (" event for dsa 0x%lx (virt 0x%p)\n",
  5455. virt_to_bus(event.dsa), event.dsa);
  5456. if (event.pid != -1) {
  5457. printk (" event for pid %ld ", event.pid);
  5458. __scsi_print_command (event.cmnd);
  5459. }
  5460. }
  5461. }
  5462. }
  5463. /*
  5464. * Function: check_address
  5465. *
  5466. * Purpose: Check to see if a possibly corrupt pointer will fault the
  5467. * kernel.
  5468. *
  5469. * Inputs: addr - address; size - size of area
  5470. *
  5471. * Returns: 0 if area is OK, -1 on error.
  5472. *
  5473. * NOTES: should be implemented in terms of vverify on kernels
  5474. * that have it.
  5475. */
  5476. static int
  5477. check_address (unsigned long addr, int size) {
  5478. return (virt_to_phys((void *)addr) < PAGE_SIZE || virt_to_phys((void *)(addr + size)) > virt_to_phys(high_memory) ? -1 : 0);
  5479. }
  5480. #ifdef MODULE
  5481. int
  5482. NCR53c7x0_release(struct Scsi_Host *host) {
  5483. struct NCR53c7x0_hostdata *hostdata =
  5484. (struct NCR53c7x0_hostdata *) host->hostdata[0];
  5485. struct NCR53c7x0_cmd *cmd, *tmp;
  5486. shutdown (host);
  5487. if (host->irq != SCSI_IRQ_NONE)
  5488. {
  5489. int irq_count;
  5490. struct Scsi_Host *tmp;
  5491. for (irq_count = 0, tmp = first_host; tmp; tmp = tmp->next)
  5492. if (tmp->hostt == the_template && tmp->irq == host->irq)
  5493. ++irq_count;
  5494. if (irq_count == 1)
  5495. free_irq(host->irq, NULL);
  5496. }
  5497. if (host->dma_channel != DMA_NONE)
  5498. free_dma(host->dma_channel);
  5499. if (host->io_port)
  5500. release_region(host->io_port, host->n_io_port);
  5501. for (cmd = (struct NCR53c7x0_cmd *) hostdata->free; cmd; cmd = tmp,
  5502. --hostdata->num_cmds) {
  5503. tmp = (struct NCR53c7x0_cmd *) cmd->next;
  5504. /*
  5505. * If we're going to loop, try to stop it to get a more accurate
  5506. * count of the leaked commands.
  5507. */
  5508. cmd->next = NULL;
  5509. if (cmd->free)
  5510. cmd->free ((void *) cmd->real, cmd->size);
  5511. }
  5512. if (hostdata->num_cmds)
  5513. printk ("scsi%d : leaked %d NCR53c7x0_cmd structures\n",
  5514. host->host_no, hostdata->num_cmds);
  5515. vfree(hostdata->events);
  5516. /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING, which
  5517. * XXX may be invalid (CONFIG_060_WRITETHROUGH)
  5518. */
  5519. kernel_set_cachemode((void *)hostdata, 8192, IOMAP_FULL_CACHING);
  5520. free_pages ((u32)hostdata, 1);
  5521. return 1;
  5522. }
  5523. #endif /* def MODULE */