sym_hipd.c 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872
  1. /*
  2. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  3. * of PCI-SCSI IO processors.
  4. *
  5. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  6. * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
  7. *
  8. * This driver is derived from the Linux sym53c8xx driver.
  9. * Copyright (C) 1998-2000 Gerard Roudier
  10. *
  11. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  12. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  13. *
  14. * The original ncr driver has been written for 386bsd and FreeBSD by
  15. * Wolfgang Stanglmeier <wolf@cologne.de>
  16. * Stefan Esser <se@mi.Uni-Koeln.de>
  17. * Copyright (C) 1994 Wolfgang Stanglmeier
  18. *
  19. * Other major contributions:
  20. *
  21. * NVRAM detection and reading.
  22. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  23. *
  24. *-----------------------------------------------------------------------------
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include "sym_glue.h"
  41. #include "sym_nvram.h"
  42. #if 0
  43. #define SYM_DEBUG_GENERIC_SUPPORT
  44. #endif
  45. /*
  46. * Needed function prototypes.
  47. */
  48. static void sym_int_ma (struct sym_hcb *np);
  49. static void sym_int_sir (struct sym_hcb *np);
  50. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
  51. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
  52. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
  53. static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
  54. static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
  55. static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
  56. /*
  57. * Print a buffer in hexadecimal format with a ".\n" at end.
  58. */
  59. static void sym_printl_hex(u_char *p, int n)
  60. {
  61. while (n-- > 0)
  62. printf (" %x", *p++);
  63. printf (".\n");
  64. }
  65. /*
  66. * Print out the content of a SCSI message.
  67. */
  68. static int sym_show_msg (u_char * msg)
  69. {
  70. u_char i;
  71. printf ("%x",*msg);
  72. if (*msg==M_EXTENDED) {
  73. for (i=1;i<8;i++) {
  74. if (i-1>msg[1]) break;
  75. printf ("-%x",msg[i]);
  76. }
  77. return (i+1);
  78. } else if ((*msg & 0xf0) == 0x20) {
  79. printf ("-%x",msg[1]);
  80. return (2);
  81. }
  82. return (1);
  83. }
  84. static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
  85. {
  86. sym_print_addr(cp->cmd, "%s: ", label);
  87. sym_show_msg(msg);
  88. printf(".\n");
  89. }
  90. static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
  91. {
  92. struct sym_tcb *tp = &np->target[target];
  93. dev_info(&tp->starget->dev, "%s: ", label);
  94. sym_show_msg(msg);
  95. printf(".\n");
  96. }
  97. /*
  98. * Print something that tells about extended errors.
  99. */
  100. void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
  101. {
  102. if (x_status & XE_PARITY_ERR) {
  103. sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
  104. }
  105. if (x_status & XE_EXTRA_DATA) {
  106. sym_print_addr(cmd, "extraneous data discarded.\n");
  107. }
  108. if (x_status & XE_BAD_PHASE) {
  109. sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
  110. }
  111. if (x_status & XE_SODL_UNRUN) {
  112. sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
  113. }
  114. if (x_status & XE_SWIDE_OVRUN) {
  115. sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
  116. }
  117. }
  118. /*
  119. * Return a string for SCSI BUS mode.
  120. */
  121. static char *sym_scsi_bus_mode(int mode)
  122. {
  123. switch(mode) {
  124. case SMODE_HVD: return "HVD";
  125. case SMODE_SE: return "SE";
  126. case SMODE_LVD: return "LVD";
  127. }
  128. return "??";
  129. }
  130. /*
  131. * Soft reset the chip.
  132. *
  133. * Raising SRST when the chip is running may cause
  134. * problems on dual function chips (see below).
  135. * On the other hand, LVD devices need some delay
  136. * to settle and report actual BUS mode in STEST4.
  137. */
  138. static void sym_chip_reset (struct sym_hcb *np)
  139. {
  140. OUTB(np, nc_istat, SRST);
  141. INB(np, nc_mbox1);
  142. udelay(10);
  143. OUTB(np, nc_istat, 0);
  144. INB(np, nc_mbox1);
  145. udelay(2000); /* For BUS MODE to settle */
  146. }
  147. /*
  148. * Really soft reset the chip.:)
  149. *
  150. * Some 896 and 876 chip revisions may hang-up if we set
  151. * the SRST (soft reset) bit at the wrong time when SCRIPTS
  152. * are running.
  153. * So, we need to abort the current operation prior to
  154. * soft resetting the chip.
  155. */
  156. static void sym_soft_reset (struct sym_hcb *np)
  157. {
  158. u_char istat = 0;
  159. int i;
  160. if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
  161. goto do_chip_reset;
  162. OUTB(np, nc_istat, CABRT);
  163. for (i = 100000 ; i ; --i) {
  164. istat = INB(np, nc_istat);
  165. if (istat & SIP) {
  166. INW(np, nc_sist);
  167. }
  168. else if (istat & DIP) {
  169. if (INB(np, nc_dstat) & ABRT)
  170. break;
  171. }
  172. udelay(5);
  173. }
  174. OUTB(np, nc_istat, 0);
  175. if (!i)
  176. printf("%s: unable to abort current chip operation, "
  177. "ISTAT=0x%02x.\n", sym_name(np), istat);
  178. do_chip_reset:
  179. sym_chip_reset(np);
  180. }
  181. /*
  182. * Start reset process.
  183. *
  184. * The interrupt handler will reinitialize the chip.
  185. */
  186. static void sym_start_reset(struct sym_hcb *np)
  187. {
  188. sym_reset_scsi_bus(np, 1);
  189. }
  190. int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
  191. {
  192. u32 term;
  193. int retv = 0;
  194. sym_soft_reset(np); /* Soft reset the chip */
  195. if (enab_int)
  196. OUTW(np, nc_sien, RST);
  197. /*
  198. * Enable Tolerant, reset IRQD if present and
  199. * properly set IRQ mode, prior to resetting the bus.
  200. */
  201. OUTB(np, nc_stest3, TE);
  202. OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
  203. OUTB(np, nc_scntl1, CRST);
  204. INB(np, nc_mbox1);
  205. udelay(200);
  206. if (!SYM_SETUP_SCSI_BUS_CHECK)
  207. goto out;
  208. /*
  209. * Check for no terminators or SCSI bus shorts to ground.
  210. * Read SCSI data bus, data parity bits and control signals.
  211. * We are expecting RESET to be TRUE and other signals to be
  212. * FALSE.
  213. */
  214. term = INB(np, nc_sstat0);
  215. term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
  216. term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
  217. ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
  218. ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
  219. INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
  220. if (!np->maxwide)
  221. term &= 0x3ffff;
  222. if (term != (2<<7)) {
  223. printf("%s: suspicious SCSI data while resetting the BUS.\n",
  224. sym_name(np));
  225. printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
  226. "0x%lx, expecting 0x%lx\n",
  227. sym_name(np),
  228. (np->features & FE_WIDE) ? "dp1,d15-8," : "",
  229. (u_long)term, (u_long)(2<<7));
  230. if (SYM_SETUP_SCSI_BUS_CHECK == 1)
  231. retv = 1;
  232. }
  233. out:
  234. OUTB(np, nc_scntl1, 0);
  235. return retv;
  236. }
  237. /*
  238. * Select SCSI clock frequency
  239. */
  240. static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
  241. {
  242. /*
  243. * If multiplier not present or not selected, leave here.
  244. */
  245. if (np->multiplier <= 1) {
  246. OUTB(np, nc_scntl3, scntl3);
  247. return;
  248. }
  249. if (sym_verbose >= 2)
  250. printf ("%s: enabling clock multiplier\n", sym_name(np));
  251. OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
  252. /*
  253. * Wait for the LCKFRQ bit to be set if supported by the chip.
  254. * Otherwise wait 50 micro-seconds (at least).
  255. */
  256. if (np->features & FE_LCKFRQ) {
  257. int i = 20;
  258. while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
  259. udelay(20);
  260. if (!i)
  261. printf("%s: the chip cannot lock the frequency\n",
  262. sym_name(np));
  263. } else {
  264. INB(np, nc_mbox1);
  265. udelay(50+10);
  266. }
  267. OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
  268. OUTB(np, nc_scntl3, scntl3);
  269. OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
  270. OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
  271. }
  272. /*
  273. * Determine the chip's clock frequency.
  274. *
  275. * This is essential for the negotiation of the synchronous
  276. * transfer rate.
  277. *
  278. * Note: we have to return the correct value.
  279. * THERE IS NO SAFE DEFAULT VALUE.
  280. *
  281. * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
  282. * 53C860 and 53C875 rev. 1 support fast20 transfers but
  283. * do not have a clock doubler and so are provided with a
  284. * 80 MHz clock. All other fast20 boards incorporate a doubler
  285. * and so should be delivered with a 40 MHz clock.
  286. * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
  287. * clock and provide a clock quadrupler (160 Mhz).
  288. */
  289. /*
  290. * calculate SCSI clock frequency (in KHz)
  291. */
  292. static unsigned getfreq (struct sym_hcb *np, int gen)
  293. {
  294. unsigned int ms = 0;
  295. unsigned int f;
  296. /*
  297. * Measure GEN timer delay in order
  298. * to calculate SCSI clock frequency
  299. *
  300. * This code will never execute too
  301. * many loop iterations (if DELAY is
  302. * reasonably correct). It could get
  303. * too low a delay (too high a freq.)
  304. * if the CPU is slow executing the
  305. * loop for some reason (an NMI, for
  306. * example). For this reason we will
  307. * if multiple measurements are to be
  308. * performed trust the higher delay
  309. * (lower frequency returned).
  310. */
  311. OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
  312. INW(np, nc_sist); /* clear pending scsi interrupt */
  313. OUTB(np, nc_dien, 0); /* mask all dma interrupts */
  314. INW(np, nc_sist); /* another one, just to be sure :) */
  315. /*
  316. * The C1010-33 core does not report GEN in SIST,
  317. * if this interrupt is masked in SIEN.
  318. * I don't know yet if the C1010-66 behaves the same way.
  319. */
  320. if (np->features & FE_C10) {
  321. OUTW(np, nc_sien, GEN);
  322. OUTB(np, nc_istat1, SIRQD);
  323. }
  324. OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
  325. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  326. OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
  327. while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
  328. udelay(1000/4); /* count in 1/4 of ms */
  329. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  330. /*
  331. * Undo C1010-33 specific settings.
  332. */
  333. if (np->features & FE_C10) {
  334. OUTW(np, nc_sien, 0);
  335. OUTB(np, nc_istat1, 0);
  336. }
  337. /*
  338. * set prescaler to divide by whatever 0 means
  339. * 0 ought to choose divide by 2, but appears
  340. * to set divide by 3.5 mode in my 53c810 ...
  341. */
  342. OUTB(np, nc_scntl3, 0);
  343. /*
  344. * adjust for prescaler, and convert into KHz
  345. */
  346. f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
  347. /*
  348. * The C1010-33 result is biased by a factor
  349. * of 2/3 compared to earlier chips.
  350. */
  351. if (np->features & FE_C10)
  352. f = (f * 2) / 3;
  353. if (sym_verbose >= 2)
  354. printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
  355. sym_name(np), gen, ms/4, f);
  356. return f;
  357. }
  358. static unsigned sym_getfreq (struct sym_hcb *np)
  359. {
  360. u_int f1, f2;
  361. int gen = 8;
  362. getfreq (np, gen); /* throw away first result */
  363. f1 = getfreq (np, gen);
  364. f2 = getfreq (np, gen);
  365. if (f1 > f2) f1 = f2; /* trust lower result */
  366. return f1;
  367. }
  368. /*
  369. * Get/probe chip SCSI clock frequency
  370. */
  371. static void sym_getclock (struct sym_hcb *np, int mult)
  372. {
  373. unsigned char scntl3 = np->sv_scntl3;
  374. unsigned char stest1 = np->sv_stest1;
  375. unsigned f1;
  376. np->multiplier = 1;
  377. f1 = 40000;
  378. /*
  379. * True with 875/895/896/895A with clock multiplier selected
  380. */
  381. if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
  382. if (sym_verbose >= 2)
  383. printf ("%s: clock multiplier found\n", sym_name(np));
  384. np->multiplier = mult;
  385. }
  386. /*
  387. * If multiplier not found or scntl3 not 7,5,3,
  388. * reset chip and get frequency from general purpose timer.
  389. * Otherwise trust scntl3 BIOS setting.
  390. */
  391. if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
  392. OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
  393. f1 = sym_getfreq (np);
  394. if (sym_verbose)
  395. printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
  396. if (f1 < 45000) f1 = 40000;
  397. else if (f1 < 55000) f1 = 50000;
  398. else f1 = 80000;
  399. if (f1 < 80000 && mult > 1) {
  400. if (sym_verbose >= 2)
  401. printf ("%s: clock multiplier assumed\n",
  402. sym_name(np));
  403. np->multiplier = mult;
  404. }
  405. } else {
  406. if ((scntl3 & 7) == 3) f1 = 40000;
  407. else if ((scntl3 & 7) == 5) f1 = 80000;
  408. else f1 = 160000;
  409. f1 /= np->multiplier;
  410. }
  411. /*
  412. * Compute controller synchronous parameters.
  413. */
  414. f1 *= np->multiplier;
  415. np->clock_khz = f1;
  416. }
  417. /*
  418. * Get/probe PCI clock frequency
  419. */
  420. static int sym_getpciclock (struct sym_hcb *np)
  421. {
  422. int f = 0;
  423. /*
  424. * For now, we only need to know about the actual
  425. * PCI BUS clock frequency for C1010-66 chips.
  426. */
  427. #if 1
  428. if (np->features & FE_66MHZ) {
  429. #else
  430. if (1) {
  431. #endif
  432. OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
  433. f = sym_getfreq(np);
  434. OUTB(np, nc_stest1, 0);
  435. }
  436. np->pciclk_khz = f;
  437. return f;
  438. }
  439. /*
  440. * SYMBIOS chip clock divisor table.
  441. *
  442. * Divisors are multiplied by 10,000,000 in order to make
  443. * calculations more simple.
  444. */
  445. #define _5M 5000000
  446. static u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
  447. /*
  448. * Get clock factor and sync divisor for a given
  449. * synchronous factor period.
  450. */
  451. static int
  452. sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
  453. {
  454. u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
  455. int div = np->clock_divn; /* Number of divisors supported */
  456. u32 fak; /* Sync factor in sxfer */
  457. u32 per; /* Period in tenths of ns */
  458. u32 kpc; /* (per * clk) */
  459. int ret;
  460. /*
  461. * Compute the synchronous period in tenths of nano-seconds
  462. */
  463. if (dt && sfac <= 9) per = 125;
  464. else if (sfac <= 10) per = 250;
  465. else if (sfac == 11) per = 303;
  466. else if (sfac == 12) per = 500;
  467. else per = 40 * sfac;
  468. ret = per;
  469. kpc = per * clk;
  470. if (dt)
  471. kpc <<= 1;
  472. /*
  473. * For earliest C10 revision 0, we cannot use extra
  474. * clocks for the setting of the SCSI clocking.
  475. * Note that this limits the lowest sync data transfer
  476. * to 5 Mega-transfers per second and may result in
  477. * using higher clock divisors.
  478. */
  479. #if 1
  480. if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
  481. /*
  482. * Look for the lowest clock divisor that allows an
  483. * output speed not faster than the period.
  484. */
  485. while (div > 0) {
  486. --div;
  487. if (kpc > (div_10M[div] << 2)) {
  488. ++div;
  489. break;
  490. }
  491. }
  492. fak = 0; /* No extra clocks */
  493. if (div == np->clock_divn) { /* Are we too fast ? */
  494. ret = -1;
  495. }
  496. *divp = div;
  497. *fakp = fak;
  498. return ret;
  499. }
  500. #endif
  501. /*
  502. * Look for the greatest clock divisor that allows an
  503. * input speed faster than the period.
  504. */
  505. while (div-- > 0)
  506. if (kpc >= (div_10M[div] << 2)) break;
  507. /*
  508. * Calculate the lowest clock factor that allows an output
  509. * speed not faster than the period, and the max output speed.
  510. * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
  511. * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
  512. */
  513. if (dt) {
  514. fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
  515. /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
  516. } else {
  517. fak = (kpc - 1) / div_10M[div] + 1 - 4;
  518. /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
  519. }
  520. /*
  521. * Check against our hardware limits, or bugs :).
  522. */
  523. if (fak > 2) {
  524. fak = 2;
  525. ret = -1;
  526. }
  527. /*
  528. * Compute and return sync parameters.
  529. */
  530. *divp = div;
  531. *fakp = fak;
  532. return ret;
  533. }
  534. /*
  535. * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
  536. * 128 transfers. All chips support at least 16 transfers
  537. * bursts. The 825A, 875 and 895 chips support bursts of up
  538. * to 128 transfers and the 895A and 896 support bursts of up
  539. * to 64 transfers. All other chips support up to 16
  540. * transfers bursts.
  541. *
  542. * For PCI 32 bit data transfers each transfer is a DWORD.
  543. * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
  544. *
  545. * We use log base 2 (burst length) as internal code, with
  546. * value 0 meaning "burst disabled".
  547. */
  548. /*
  549. * Burst length from burst code.
  550. */
  551. #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
  552. /*
  553. * Burst code from io register bits.
  554. */
  555. #define burst_code(dmode, ctest4, ctest5) \
  556. (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
  557. /*
  558. * Set initial io register bits from burst code.
  559. */
  560. static __inline void sym_init_burst(struct sym_hcb *np, u_char bc)
  561. {
  562. np->rv_ctest4 &= ~0x80;
  563. np->rv_dmode &= ~(0x3 << 6);
  564. np->rv_ctest5 &= ~0x4;
  565. if (!bc) {
  566. np->rv_ctest4 |= 0x80;
  567. }
  568. else {
  569. --bc;
  570. np->rv_dmode |= ((bc & 0x3) << 6);
  571. np->rv_ctest5 |= (bc & 0x4);
  572. }
  573. }
  574. /*
  575. * Print out the list of targets that have some flag disabled by user.
  576. */
  577. static void sym_print_targets_flag(struct sym_hcb *np, int mask, char *msg)
  578. {
  579. int cnt;
  580. int i;
  581. for (cnt = 0, i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  582. if (i == np->myaddr)
  583. continue;
  584. if (np->target[i].usrflags & mask) {
  585. if (!cnt++)
  586. printf("%s: %s disabled for targets",
  587. sym_name(np), msg);
  588. printf(" %d", i);
  589. }
  590. }
  591. if (cnt)
  592. printf(".\n");
  593. }
  594. /*
  595. * Save initial settings of some IO registers.
  596. * Assumed to have been set by BIOS.
  597. * We cannot reset the chip prior to reading the
  598. * IO registers, since informations will be lost.
  599. * Since the SCRIPTS processor may be running, this
  600. * is not safe on paper, but it seems to work quite
  601. * well. :)
  602. */
  603. static void sym_save_initial_setting (struct sym_hcb *np)
  604. {
  605. np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
  606. np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
  607. np->sv_dmode = INB(np, nc_dmode) & 0xce;
  608. np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
  609. np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
  610. np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
  611. np->sv_gpcntl = INB(np, nc_gpcntl);
  612. np->sv_stest1 = INB(np, nc_stest1);
  613. np->sv_stest2 = INB(np, nc_stest2) & 0x20;
  614. np->sv_stest4 = INB(np, nc_stest4);
  615. if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
  616. np->sv_scntl4 = INB(np, nc_scntl4);
  617. np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
  618. }
  619. else
  620. np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
  621. }
  622. /*
  623. * Prepare io register values used by sym_start_up()
  624. * according to selected and supported features.
  625. */
  626. static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
  627. {
  628. u_char burst_max;
  629. u32 period;
  630. int i;
  631. /*
  632. * Wide ?
  633. */
  634. np->maxwide = (np->features & FE_WIDE)? 1 : 0;
  635. /*
  636. * Guess the frequency of the chip's clock.
  637. */
  638. if (np->features & (FE_ULTRA3 | FE_ULTRA2))
  639. np->clock_khz = 160000;
  640. else if (np->features & FE_ULTRA)
  641. np->clock_khz = 80000;
  642. else
  643. np->clock_khz = 40000;
  644. /*
  645. * Get the clock multiplier factor.
  646. */
  647. if (np->features & FE_QUAD)
  648. np->multiplier = 4;
  649. else if (np->features & FE_DBLR)
  650. np->multiplier = 2;
  651. else
  652. np->multiplier = 1;
  653. /*
  654. * Measure SCSI clock frequency for chips
  655. * it may vary from assumed one.
  656. */
  657. if (np->features & FE_VARCLK)
  658. sym_getclock(np, np->multiplier);
  659. /*
  660. * Divisor to be used for async (timer pre-scaler).
  661. */
  662. i = np->clock_divn - 1;
  663. while (--i >= 0) {
  664. if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
  665. ++i;
  666. break;
  667. }
  668. }
  669. np->rv_scntl3 = i+1;
  670. /*
  671. * The C1010 uses hardwired divisors for async.
  672. * So, we just throw away, the async. divisor.:-)
  673. */
  674. if (np->features & FE_C10)
  675. np->rv_scntl3 = 0;
  676. /*
  677. * Minimum synchronous period factor supported by the chip.
  678. * Btw, 'period' is in tenths of nanoseconds.
  679. */
  680. period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
  681. if (period <= 250) np->minsync = 10;
  682. else if (period <= 303) np->minsync = 11;
  683. else if (period <= 500) np->minsync = 12;
  684. else np->minsync = (period + 40 - 1) / 40;
  685. /*
  686. * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
  687. */
  688. if (np->minsync < 25 &&
  689. !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
  690. np->minsync = 25;
  691. else if (np->minsync < 12 &&
  692. !(np->features & (FE_ULTRA2|FE_ULTRA3)))
  693. np->minsync = 12;
  694. /*
  695. * Maximum synchronous period factor supported by the chip.
  696. */
  697. period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
  698. np->maxsync = period > 2540 ? 254 : period / 10;
  699. /*
  700. * If chip is a C1010, guess the sync limits in DT mode.
  701. */
  702. if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
  703. if (np->clock_khz == 160000) {
  704. np->minsync_dt = 9;
  705. np->maxsync_dt = 50;
  706. np->maxoffs_dt = nvram->type ? 62 : 31;
  707. }
  708. }
  709. /*
  710. * 64 bit addressing (895A/896/1010) ?
  711. */
  712. if (np->features & FE_DAC) {
  713. #if SYM_CONF_DMA_ADDRESSING_MODE == 0
  714. np->rv_ccntl1 |= (DDAC);
  715. #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
  716. if (!np->use_dac)
  717. np->rv_ccntl1 |= (DDAC);
  718. else
  719. np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
  720. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  721. if (!np->use_dac)
  722. np->rv_ccntl1 |= (DDAC);
  723. else
  724. np->rv_ccntl1 |= (0 | EXTIBMV);
  725. #endif
  726. }
  727. /*
  728. * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
  729. */
  730. if (np->features & FE_NOPM)
  731. np->rv_ccntl0 |= (ENPMJ);
  732. /*
  733. * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
  734. * In dual channel mode, contention occurs if internal cycles
  735. * are used. Disable internal cycles.
  736. */
  737. if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
  738. np->revision_id < 0x1)
  739. np->rv_ccntl0 |= DILS;
  740. /*
  741. * Select burst length (dwords)
  742. */
  743. burst_max = SYM_SETUP_BURST_ORDER;
  744. if (burst_max == 255)
  745. burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
  746. np->sv_ctest5);
  747. if (burst_max > 7)
  748. burst_max = 7;
  749. if (burst_max > np->maxburst)
  750. burst_max = np->maxburst;
  751. /*
  752. * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
  753. * This chip and the 860 Rev 1 may wrongly use PCI cache line
  754. * based transactions on LOAD/STORE instructions. So we have
  755. * to prevent these chips from using such PCI transactions in
  756. * this driver. The generic ncr driver that does not use
  757. * LOAD/STORE instructions does not need this work-around.
  758. */
  759. if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
  760. np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
  761. (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
  762. np->revision_id <= 0x1))
  763. np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
  764. /*
  765. * Select all supported special features.
  766. * If we are using on-board RAM for scripts, prefetch (PFEN)
  767. * does not help, but burst op fetch (BOF) does.
  768. * Disabling PFEN makes sure BOF will be used.
  769. */
  770. if (np->features & FE_ERL)
  771. np->rv_dmode |= ERL; /* Enable Read Line */
  772. if (np->features & FE_BOF)
  773. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  774. if (np->features & FE_ERMP)
  775. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  776. #if 1
  777. if ((np->features & FE_PFEN) && !np->ram_ba)
  778. #else
  779. if (np->features & FE_PFEN)
  780. #endif
  781. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  782. if (np->features & FE_CLSE)
  783. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  784. if (np->features & FE_WRIE)
  785. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  786. if (np->features & FE_DFS)
  787. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  788. /*
  789. * Select some other
  790. */
  791. np->rv_ctest4 |= MPEE; /* Master parity checking */
  792. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  793. /*
  794. * Get parity checking, host ID and verbose mode from NVRAM
  795. */
  796. np->myaddr = 255;
  797. sym_nvram_setup_host(shost, np, nvram);
  798. /*
  799. * Get SCSI addr of host adapter (set by bios?).
  800. */
  801. if (np->myaddr == 255) {
  802. np->myaddr = INB(np, nc_scid) & 0x07;
  803. if (!np->myaddr)
  804. np->myaddr = SYM_SETUP_HOST_ID;
  805. }
  806. /*
  807. * Prepare initial io register bits for burst length
  808. */
  809. sym_init_burst(np, burst_max);
  810. /*
  811. * Set SCSI BUS mode.
  812. * - LVD capable chips (895/895A/896/1010) report the
  813. * current BUS mode through the STEST4 IO register.
  814. * - For previous generation chips (825/825A/875),
  815. * user has to tell us how to check against HVD,
  816. * since a 100% safe algorithm is not possible.
  817. */
  818. np->scsi_mode = SMODE_SE;
  819. if (np->features & (FE_ULTRA2|FE_ULTRA3))
  820. np->scsi_mode = (np->sv_stest4 & SMODE);
  821. else if (np->features & FE_DIFF) {
  822. if (SYM_SETUP_SCSI_DIFF == 1) {
  823. if (np->sv_scntl3) {
  824. if (np->sv_stest2 & 0x20)
  825. np->scsi_mode = SMODE_HVD;
  826. }
  827. else if (nvram->type == SYM_SYMBIOS_NVRAM) {
  828. if (!(INB(np, nc_gpreg) & 0x08))
  829. np->scsi_mode = SMODE_HVD;
  830. }
  831. }
  832. else if (SYM_SETUP_SCSI_DIFF == 2)
  833. np->scsi_mode = SMODE_HVD;
  834. }
  835. if (np->scsi_mode == SMODE_HVD)
  836. np->rv_stest2 |= 0x20;
  837. /*
  838. * Set LED support from SCRIPTS.
  839. * Ignore this feature for boards known to use a
  840. * specific GPIO wiring and for the 895A, 896
  841. * and 1010 that drive the LED directly.
  842. */
  843. if ((SYM_SETUP_SCSI_LED ||
  844. (nvram->type == SYM_SYMBIOS_NVRAM ||
  845. (nvram->type == SYM_TEKRAM_NVRAM &&
  846. np->device_id == PCI_DEVICE_ID_NCR_53C895))) &&
  847. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  848. np->features |= FE_LED0;
  849. /*
  850. * Set irq mode.
  851. */
  852. switch(SYM_SETUP_IRQ_MODE & 3) {
  853. case 2:
  854. np->rv_dcntl |= IRQM;
  855. break;
  856. case 1:
  857. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  858. break;
  859. default:
  860. break;
  861. }
  862. /*
  863. * Configure targets according to driver setup.
  864. * If NVRAM present get targets setup from NVRAM.
  865. */
  866. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  867. struct sym_tcb *tp = &np->target[i];
  868. tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  869. tp->usrtags = SYM_SETUP_MAX_TAG;
  870. sym_nvram_setup_target(np, i, nvram);
  871. if (!tp->usrtags)
  872. tp->usrflags &= ~SYM_TAGS_ENABLED;
  873. }
  874. /*
  875. * Let user know about the settings.
  876. */
  877. printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
  878. sym_nvram_type(nvram), np->myaddr,
  879. (np->features & FE_ULTRA3) ? 80 :
  880. (np->features & FE_ULTRA2) ? 40 :
  881. (np->features & FE_ULTRA) ? 20 : 10,
  882. sym_scsi_bus_mode(np->scsi_mode),
  883. (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
  884. /*
  885. * Tell him more on demand.
  886. */
  887. if (sym_verbose) {
  888. printf("%s: %s IRQ line driver%s\n",
  889. sym_name(np),
  890. np->rv_dcntl & IRQM ? "totem pole" : "open drain",
  891. np->ram_ba ? ", using on-chip SRAM" : "");
  892. printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
  893. if (np->features & FE_NOPM)
  894. printf("%s: handling phase mismatch from SCRIPTS.\n",
  895. sym_name(np));
  896. }
  897. /*
  898. * And still more.
  899. */
  900. if (sym_verbose >= 2) {
  901. printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  902. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  903. sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  904. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  905. printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  906. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  907. sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  908. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  909. }
  910. /*
  911. * Let user be aware of targets that have some disable flags set.
  912. */
  913. sym_print_targets_flag(np, SYM_SCAN_BOOT_DISABLED, "SCAN AT BOOT");
  914. if (sym_verbose)
  915. sym_print_targets_flag(np, SYM_SCAN_LUNS_DISABLED,
  916. "SCAN FOR LUNS");
  917. return 0;
  918. }
  919. /*
  920. * Test the pci bus snoop logic :-(
  921. *
  922. * Has to be called with interrupts disabled.
  923. */
  924. #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
  925. static int sym_regtest (struct sym_hcb *np)
  926. {
  927. register volatile u32 data;
  928. /*
  929. * chip registers may NOT be cached.
  930. * write 0xffffffff to a read only register area,
  931. * and try to read it back.
  932. */
  933. data = 0xffffffff;
  934. OUTL(np, nc_dstat, data);
  935. data = INL(np, nc_dstat);
  936. #if 1
  937. if (data == 0xffffffff) {
  938. #else
  939. if ((data & 0xe2f0fffd) != 0x02000080) {
  940. #endif
  941. printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  942. (unsigned) data);
  943. return (0x10);
  944. }
  945. return (0);
  946. }
  947. #endif
  948. static int sym_snooptest (struct sym_hcb *np)
  949. {
  950. u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
  951. int i, err=0;
  952. #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
  953. err |= sym_regtest (np);
  954. if (err) return (err);
  955. #endif
  956. restart_test:
  957. /*
  958. * Enable Master Parity Checking as we intend
  959. * to enable it for normal operations.
  960. */
  961. OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
  962. /*
  963. * init
  964. */
  965. pc = SCRIPTZ_BA(np, snooptest);
  966. host_wr = 1;
  967. sym_wr = 2;
  968. /*
  969. * Set memory and register.
  970. */
  971. np->scratch = cpu_to_scr(host_wr);
  972. OUTL(np, nc_temp, sym_wr);
  973. /*
  974. * Start script (exchange values)
  975. */
  976. OUTL(np, nc_dsa, np->hcb_ba);
  977. OUTL_DSP(np, pc);
  978. /*
  979. * Wait 'til done (with timeout)
  980. */
  981. for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
  982. if (INB(np, nc_istat) & (INTF|SIP|DIP))
  983. break;
  984. if (i>=SYM_SNOOP_TIMEOUT) {
  985. printf ("CACHE TEST FAILED: timeout.\n");
  986. return (0x20);
  987. }
  988. /*
  989. * Check for fatal DMA errors.
  990. */
  991. dstat = INB(np, nc_dstat);
  992. #if 1 /* Band aiding for broken hardwares that fail PCI parity */
  993. if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
  994. printf ("%s: PCI DATA PARITY ERROR DETECTED - "
  995. "DISABLING MASTER DATA PARITY CHECKING.\n",
  996. sym_name(np));
  997. np->rv_ctest4 &= ~MPEE;
  998. goto restart_test;
  999. }
  1000. #endif
  1001. if (dstat & (MDPE|BF|IID)) {
  1002. printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
  1003. return (0x80);
  1004. }
  1005. /*
  1006. * Save termination position.
  1007. */
  1008. pc = INL(np, nc_dsp);
  1009. /*
  1010. * Read memory and register.
  1011. */
  1012. host_rd = scr_to_cpu(np->scratch);
  1013. sym_rd = INL(np, nc_scratcha);
  1014. sym_bk = INL(np, nc_temp);
  1015. /*
  1016. * Check termination position.
  1017. */
  1018. if (pc != SCRIPTZ_BA(np, snoopend)+8) {
  1019. printf ("CACHE TEST FAILED: script execution failed.\n");
  1020. printf ("start=%08lx, pc=%08lx, end=%08lx\n",
  1021. (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
  1022. (u_long) SCRIPTZ_BA(np, snoopend) +8);
  1023. return (0x40);
  1024. }
  1025. /*
  1026. * Show results.
  1027. */
  1028. if (host_wr != sym_rd) {
  1029. printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
  1030. (int) host_wr, (int) sym_rd);
  1031. err |= 1;
  1032. }
  1033. if (host_rd != sym_wr) {
  1034. printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
  1035. (int) sym_wr, (int) host_rd);
  1036. err |= 2;
  1037. }
  1038. if (sym_bk != sym_wr) {
  1039. printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
  1040. (int) sym_wr, (int) sym_bk);
  1041. err |= 4;
  1042. }
  1043. return (err);
  1044. }
  1045. /*
  1046. * log message for real hard errors
  1047. *
  1048. * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
  1049. * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
  1050. *
  1051. * exception register:
  1052. * ds: dstat
  1053. * si: sist
  1054. *
  1055. * SCSI bus lines:
  1056. * so: control lines as driven by chip.
  1057. * si: control lines as seen by chip.
  1058. * sd: scsi data lines as seen by chip.
  1059. *
  1060. * wide/fastmode:
  1061. * sx: sxfer (see the manual)
  1062. * s3: scntl3 (see the manual)
  1063. * s4: scntl4 (see the manual)
  1064. *
  1065. * current script command:
  1066. * dsp: script address (relative to start of script).
  1067. * dbc: first word of script command.
  1068. *
  1069. * First 24 register of the chip:
  1070. * r0..rf
  1071. */
  1072. static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
  1073. {
  1074. u32 dsp;
  1075. int script_ofs;
  1076. int script_size;
  1077. char *script_name;
  1078. u_char *script_base;
  1079. int i;
  1080. dsp = INL(np, nc_dsp);
  1081. if (dsp > np->scripta_ba &&
  1082. dsp <= np->scripta_ba + np->scripta_sz) {
  1083. script_ofs = dsp - np->scripta_ba;
  1084. script_size = np->scripta_sz;
  1085. script_base = (u_char *) np->scripta0;
  1086. script_name = "scripta";
  1087. }
  1088. else if (np->scriptb_ba < dsp &&
  1089. dsp <= np->scriptb_ba + np->scriptb_sz) {
  1090. script_ofs = dsp - np->scriptb_ba;
  1091. script_size = np->scriptb_sz;
  1092. script_base = (u_char *) np->scriptb0;
  1093. script_name = "scriptb";
  1094. } else {
  1095. script_ofs = dsp;
  1096. script_size = 0;
  1097. script_base = NULL;
  1098. script_name = "mem";
  1099. }
  1100. printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
  1101. sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
  1102. (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
  1103. (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
  1104. (unsigned)INB(np, nc_scntl3),
  1105. (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
  1106. script_name, script_ofs, (unsigned)INL(np, nc_dbc));
  1107. if (((script_ofs & 3) == 0) &&
  1108. (unsigned)script_ofs < script_size) {
  1109. printf ("%s: script cmd = %08x\n", sym_name(np),
  1110. scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
  1111. }
  1112. printf ("%s: regdump:", sym_name(np));
  1113. for (i=0; i<24;i++)
  1114. printf (" %02x", (unsigned)INB_OFF(np, i));
  1115. printf (".\n");
  1116. /*
  1117. * PCI BUS error.
  1118. */
  1119. if (dstat & (MDPE|BF))
  1120. sym_log_bus_error(np);
  1121. }
  1122. static struct sym_chip sym_dev_table[] = {
  1123. {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
  1124. FE_ERL}
  1125. ,
  1126. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1127. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1128. FE_BOF}
  1129. ,
  1130. #else
  1131. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1132. FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
  1133. ,
  1134. #endif
  1135. {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
  1136. FE_BOF|FE_ERL}
  1137. ,
  1138. {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
  1139. FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
  1140. ,
  1141. {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
  1142. FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
  1143. ,
  1144. {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
  1145. FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
  1146. ,
  1147. {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
  1148. FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1149. FE_RAM|FE_DIFF|FE_VARCLK}
  1150. ,
  1151. {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
  1152. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1153. FE_RAM|FE_DIFF|FE_VARCLK}
  1154. ,
  1155. {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
  1156. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1157. FE_RAM|FE_DIFF|FE_VARCLK}
  1158. ,
  1159. {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
  1160. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1161. FE_RAM|FE_DIFF|FE_VARCLK}
  1162. ,
  1163. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1164. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1165. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
  1166. FE_RAM|FE_LCKFRQ}
  1167. ,
  1168. #else
  1169. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1170. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1171. FE_RAM|FE_LCKFRQ}
  1172. ,
  1173. #endif
  1174. {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
  1175. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1176. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1177. ,
  1178. {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
  1179. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1180. FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1181. ,
  1182. {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
  1183. FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1184. FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1185. ,
  1186. {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
  1187. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1188. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1189. FE_C10}
  1190. ,
  1191. {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
  1192. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1193. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1194. FE_C10|FE_U3EN}
  1195. ,
  1196. {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
  1197. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1198. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
  1199. FE_C10|FE_U3EN}
  1200. ,
  1201. {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
  1202. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1203. FE_RAM|FE_IO256|FE_LEDC}
  1204. };
  1205. #define sym_num_devs \
  1206. (sizeof(sym_dev_table) / sizeof(sym_dev_table[0]))
  1207. /*
  1208. * Look up the chip table.
  1209. *
  1210. * Return a pointer to the chip entry if found,
  1211. * zero otherwise.
  1212. */
  1213. struct sym_chip *
  1214. sym_lookup_chip_table (u_short device_id, u_char revision)
  1215. {
  1216. struct sym_chip *chip;
  1217. int i;
  1218. for (i = 0; i < sym_num_devs; i++) {
  1219. chip = &sym_dev_table[i];
  1220. if (device_id != chip->device_id)
  1221. continue;
  1222. if (revision > chip->revision_id)
  1223. continue;
  1224. return chip;
  1225. }
  1226. return NULL;
  1227. }
  1228. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1229. /*
  1230. * Lookup the 64 bit DMA segments map.
  1231. * This is only used if the direct mapping
  1232. * has been unsuccessful.
  1233. */
  1234. int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
  1235. {
  1236. int i;
  1237. if (!np->use_dac)
  1238. goto weird;
  1239. /* Look up existing mappings */
  1240. for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
  1241. if (h == np->dmap_bah[i])
  1242. return i;
  1243. }
  1244. /* If direct mapping is free, get it */
  1245. if (!np->dmap_bah[s])
  1246. goto new;
  1247. /* Collision -> lookup free mappings */
  1248. for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
  1249. if (!np->dmap_bah[s])
  1250. goto new;
  1251. }
  1252. weird:
  1253. panic("sym: ran out of 64 bit DMA segment registers");
  1254. return -1;
  1255. new:
  1256. np->dmap_bah[s] = h;
  1257. np->dmap_dirty = 1;
  1258. return s;
  1259. }
  1260. /*
  1261. * Update IO registers scratch C..R so they will be
  1262. * in sync. with queued CCB expectations.
  1263. */
  1264. static void sym_update_dmap_regs(struct sym_hcb *np)
  1265. {
  1266. int o, i;
  1267. if (!np->dmap_dirty)
  1268. return;
  1269. o = offsetof(struct sym_reg, nc_scrx[0]);
  1270. for (i = 0; i < SYM_DMAP_SIZE; i++) {
  1271. OUTL_OFF(np, o, np->dmap_bah[i]);
  1272. o += 4;
  1273. }
  1274. np->dmap_dirty = 0;
  1275. }
  1276. #endif
  1277. /* Enforce all the fiddly SPI rules and the chip limitations */
  1278. static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
  1279. struct sym_trans *goal)
  1280. {
  1281. if (!spi_support_wide(starget))
  1282. goal->width = 0;
  1283. if (!spi_support_sync(starget)) {
  1284. goal->iu = 0;
  1285. goal->dt = 0;
  1286. goal->qas = 0;
  1287. goal->period = 0;
  1288. goal->offset = 0;
  1289. return;
  1290. }
  1291. if (spi_support_dt(starget)) {
  1292. if (spi_support_dt_only(starget))
  1293. goal->dt = 1;
  1294. if (goal->offset == 0)
  1295. goal->dt = 0;
  1296. } else {
  1297. goal->dt = 0;
  1298. }
  1299. /* Some targets fail to properly negotiate DT in SE mode */
  1300. if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
  1301. goal->dt = 0;
  1302. if (goal->dt) {
  1303. /* all DT transfers must be wide */
  1304. goal->width = 1;
  1305. if (goal->offset > np->maxoffs_dt)
  1306. goal->offset = np->maxoffs_dt;
  1307. if (goal->period < np->minsync_dt)
  1308. goal->period = np->minsync_dt;
  1309. if (goal->period > np->maxsync_dt)
  1310. goal->period = np->maxsync_dt;
  1311. } else {
  1312. goal->iu = goal->qas = 0;
  1313. if (goal->offset > np->maxoffs)
  1314. goal->offset = np->maxoffs;
  1315. if (goal->period < np->minsync)
  1316. goal->period = np->minsync;
  1317. if (goal->period > np->maxsync)
  1318. goal->period = np->maxsync;
  1319. }
  1320. }
  1321. /*
  1322. * Prepare the next negotiation message if needed.
  1323. *
  1324. * Fill in the part of message buffer that contains the
  1325. * negotiation and the nego_status field of the CCB.
  1326. * Returns the size of the message in bytes.
  1327. */
  1328. static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
  1329. {
  1330. struct sym_tcb *tp = &np->target[cp->target];
  1331. struct scsi_target *starget = tp->starget;
  1332. struct sym_trans *goal = &tp->tgoal;
  1333. int msglen = 0;
  1334. int nego;
  1335. sym_check_goals(np, starget, goal);
  1336. /*
  1337. * Many devices implement PPR in a buggy way, so only use it if we
  1338. * really want to.
  1339. */
  1340. if (goal->iu || goal->dt || goal->qas || (goal->period < 0xa)) {
  1341. nego = NS_PPR;
  1342. } else if (spi_width(starget) != goal->width) {
  1343. nego = NS_WIDE;
  1344. } else if (spi_period(starget) != goal->period ||
  1345. spi_offset(starget) != goal->offset) {
  1346. nego = NS_SYNC;
  1347. } else {
  1348. goal->check_nego = 0;
  1349. nego = 0;
  1350. }
  1351. switch (nego) {
  1352. case NS_SYNC:
  1353. msgptr[msglen++] = M_EXTENDED;
  1354. msgptr[msglen++] = 3;
  1355. msgptr[msglen++] = M_X_SYNC_REQ;
  1356. msgptr[msglen++] = goal->period;
  1357. msgptr[msglen++] = goal->offset;
  1358. break;
  1359. case NS_WIDE:
  1360. msgptr[msglen++] = M_EXTENDED;
  1361. msgptr[msglen++] = 2;
  1362. msgptr[msglen++] = M_X_WIDE_REQ;
  1363. msgptr[msglen++] = goal->width;
  1364. break;
  1365. case NS_PPR:
  1366. msgptr[msglen++] = M_EXTENDED;
  1367. msgptr[msglen++] = 6;
  1368. msgptr[msglen++] = M_X_PPR_REQ;
  1369. msgptr[msglen++] = goal->period;
  1370. msgptr[msglen++] = 0;
  1371. msgptr[msglen++] = goal->offset;
  1372. msgptr[msglen++] = goal->width;
  1373. msgptr[msglen++] = (goal->iu ? PPR_OPT_IU : 0) |
  1374. (goal->dt ? PPR_OPT_DT : 0) |
  1375. (goal->qas ? PPR_OPT_QAS : 0);
  1376. break;
  1377. }
  1378. cp->nego_status = nego;
  1379. if (nego) {
  1380. tp->nego_cp = cp; /* Keep track a nego will be performed */
  1381. if (DEBUG_FLAGS & DEBUG_NEGO) {
  1382. sym_print_nego_msg(np, cp->target,
  1383. nego == NS_SYNC ? "sync msgout" :
  1384. nego == NS_WIDE ? "wide msgout" :
  1385. "ppr msgout", msgptr);
  1386. }
  1387. }
  1388. return msglen;
  1389. }
  1390. /*
  1391. * Insert a job into the start queue.
  1392. */
  1393. void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
  1394. {
  1395. u_short qidx;
  1396. #ifdef SYM_CONF_IARB_SUPPORT
  1397. /*
  1398. * If the previously queued CCB is not yet done,
  1399. * set the IARB hint. The SCRIPTS will go with IARB
  1400. * for this job when starting the previous one.
  1401. * We leave devices a chance to win arbitration by
  1402. * not using more than 'iarb_max' consecutive
  1403. * immediate arbitrations.
  1404. */
  1405. if (np->last_cp && np->iarb_count < np->iarb_max) {
  1406. np->last_cp->host_flags |= HF_HINT_IARB;
  1407. ++np->iarb_count;
  1408. }
  1409. else
  1410. np->iarb_count = 0;
  1411. np->last_cp = cp;
  1412. #endif
  1413. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1414. /*
  1415. * Make SCRIPTS aware of the 64 bit DMA
  1416. * segment registers not being up-to-date.
  1417. */
  1418. if (np->dmap_dirty)
  1419. cp->host_xflags |= HX_DMAP_DIRTY;
  1420. #endif
  1421. /*
  1422. * Insert first the idle task and then our job.
  1423. * The MBs should ensure proper ordering.
  1424. */
  1425. qidx = np->squeueput + 2;
  1426. if (qidx >= MAX_QUEUE*2) qidx = 0;
  1427. np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
  1428. MEMORY_WRITE_BARRIER();
  1429. np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
  1430. np->squeueput = qidx;
  1431. if (DEBUG_FLAGS & DEBUG_QUEUE)
  1432. printf ("%s: queuepos=%d.\n", sym_name (np), np->squeueput);
  1433. /*
  1434. * Script processor may be waiting for reselect.
  1435. * Wake it up.
  1436. */
  1437. MEMORY_WRITE_BARRIER();
  1438. OUTB(np, nc_istat, SIGP|np->istat_sem);
  1439. }
  1440. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1441. /*
  1442. * Start next ready-to-start CCBs.
  1443. */
  1444. void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
  1445. {
  1446. SYM_QUEHEAD *qp;
  1447. struct sym_ccb *cp;
  1448. /*
  1449. * Paranoia, as usual. :-)
  1450. */
  1451. assert(!lp->started_tags || !lp->started_no_tag);
  1452. /*
  1453. * Try to start as many commands as asked by caller.
  1454. * Prevent from having both tagged and untagged
  1455. * commands queued to the device at the same time.
  1456. */
  1457. while (maxn--) {
  1458. qp = sym_remque_head(&lp->waiting_ccbq);
  1459. if (!qp)
  1460. break;
  1461. cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
  1462. if (cp->tag != NO_TAG) {
  1463. if (lp->started_no_tag ||
  1464. lp->started_tags >= lp->started_max) {
  1465. sym_insque_head(qp, &lp->waiting_ccbq);
  1466. break;
  1467. }
  1468. lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
  1469. lp->head.resel_sa =
  1470. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  1471. ++lp->started_tags;
  1472. } else {
  1473. if (lp->started_no_tag || lp->started_tags) {
  1474. sym_insque_head(qp, &lp->waiting_ccbq);
  1475. break;
  1476. }
  1477. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  1478. lp->head.resel_sa =
  1479. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  1480. ++lp->started_no_tag;
  1481. }
  1482. cp->started = 1;
  1483. sym_insque_tail(qp, &lp->started_ccbq);
  1484. sym_put_start_queue(np, cp);
  1485. }
  1486. }
  1487. #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
  1488. /*
  1489. * The chip may have completed jobs. Look at the DONE QUEUE.
  1490. *
  1491. * On paper, memory read barriers may be needed here to
  1492. * prevent out of order LOADs by the CPU from having
  1493. * prefetched stale data prior to DMA having occurred.
  1494. */
  1495. static int sym_wakeup_done (struct sym_hcb *np)
  1496. {
  1497. struct sym_ccb *cp;
  1498. int i, n;
  1499. u32 dsa;
  1500. n = 0;
  1501. i = np->dqueueget;
  1502. /* MEMORY_READ_BARRIER(); */
  1503. while (1) {
  1504. dsa = scr_to_cpu(np->dqueue[i]);
  1505. if (!dsa)
  1506. break;
  1507. np->dqueue[i] = 0;
  1508. if ((i = i+2) >= MAX_QUEUE*2)
  1509. i = 0;
  1510. cp = sym_ccb_from_dsa(np, dsa);
  1511. if (cp) {
  1512. MEMORY_READ_BARRIER();
  1513. sym_complete_ok (np, cp);
  1514. ++n;
  1515. }
  1516. else
  1517. printf ("%s: bad DSA (%x) in done queue.\n",
  1518. sym_name(np), (u_int) dsa);
  1519. }
  1520. np->dqueueget = i;
  1521. return n;
  1522. }
  1523. /*
  1524. * Complete all CCBs queued to the COMP queue.
  1525. *
  1526. * These CCBs are assumed:
  1527. * - Not to be referenced either by devices or
  1528. * SCRIPTS-related queues and datas.
  1529. * - To have to be completed with an error condition
  1530. * or requeued.
  1531. *
  1532. * The device queue freeze count is incremented
  1533. * for each CCB that does not prevent this.
  1534. * This function is called when all CCBs involved
  1535. * in error handling/recovery have been reaped.
  1536. */
  1537. static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
  1538. {
  1539. SYM_QUEHEAD *qp;
  1540. struct sym_ccb *cp;
  1541. while ((qp = sym_remque_head(&np->comp_ccbq)) != 0) {
  1542. struct scsi_cmnd *cmd;
  1543. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1544. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  1545. /* Leave quiet CCBs waiting for resources */
  1546. if (cp->host_status == HS_WAIT)
  1547. continue;
  1548. cmd = cp->cmd;
  1549. if (cam_status)
  1550. sym_set_cam_status(cmd, cam_status);
  1551. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1552. if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
  1553. struct sym_tcb *tp = &np->target[cp->target];
  1554. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  1555. if (lp) {
  1556. sym_remque(&cp->link2_ccbq);
  1557. sym_insque_tail(&cp->link2_ccbq,
  1558. &lp->waiting_ccbq);
  1559. if (cp->started) {
  1560. if (cp->tag != NO_TAG)
  1561. --lp->started_tags;
  1562. else
  1563. --lp->started_no_tag;
  1564. }
  1565. }
  1566. cp->started = 0;
  1567. continue;
  1568. }
  1569. #endif
  1570. sym_free_ccb(np, cp);
  1571. sym_xpt_done(np, cmd);
  1572. }
  1573. }
  1574. /*
  1575. * Complete all active CCBs with error.
  1576. * Used on CHIP/SCSI RESET.
  1577. */
  1578. static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
  1579. {
  1580. /*
  1581. * Move all active CCBs to the COMP queue
  1582. * and flush this queue.
  1583. */
  1584. sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
  1585. sym_que_init(&np->busy_ccbq);
  1586. sym_flush_comp_queue(np, cam_status);
  1587. }
  1588. /*
  1589. * Start chip.
  1590. *
  1591. * 'reason' means:
  1592. * 0: initialisation.
  1593. * 1: SCSI BUS RESET delivered or received.
  1594. * 2: SCSI BUS MODE changed.
  1595. */
  1596. void sym_start_up (struct sym_hcb *np, int reason)
  1597. {
  1598. int i;
  1599. u32 phys;
  1600. /*
  1601. * Reset chip if asked, otherwise just clear fifos.
  1602. */
  1603. if (reason == 1)
  1604. sym_soft_reset(np);
  1605. else {
  1606. OUTB(np, nc_stest3, TE|CSF);
  1607. OUTONB(np, nc_ctest3, CLF);
  1608. }
  1609. /*
  1610. * Clear Start Queue
  1611. */
  1612. phys = np->squeue_ba;
  1613. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1614. np->squeue[i] = cpu_to_scr(np->idletask_ba);
  1615. np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1616. }
  1617. np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1618. /*
  1619. * Start at first entry.
  1620. */
  1621. np->squeueput = 0;
  1622. /*
  1623. * Clear Done Queue
  1624. */
  1625. phys = np->dqueue_ba;
  1626. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1627. np->dqueue[i] = 0;
  1628. np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1629. }
  1630. np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1631. /*
  1632. * Start at first entry.
  1633. */
  1634. np->dqueueget = 0;
  1635. /*
  1636. * Install patches in scripts.
  1637. * This also let point to first position the start
  1638. * and done queue pointers used from SCRIPTS.
  1639. */
  1640. np->fw_patch(np);
  1641. /*
  1642. * Wakeup all pending jobs.
  1643. */
  1644. sym_flush_busy_queue(np, DID_RESET);
  1645. /*
  1646. * Init chip.
  1647. */
  1648. OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
  1649. INB(np, nc_mbox1);
  1650. udelay(2000); /* The 895 needs time for the bus mode to settle */
  1651. OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
  1652. /* full arb., ena parity, par->ATN */
  1653. OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
  1654. sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
  1655. OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
  1656. OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
  1657. OUTB(np, nc_istat , SIGP ); /* Signal Process */
  1658. OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
  1659. OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
  1660. OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
  1661. OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
  1662. OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
  1663. /* Extended Sreq/Sack filtering not supported on the C10 */
  1664. if (np->features & FE_C10)
  1665. OUTB(np, nc_stest2, np->rv_stest2);
  1666. else
  1667. OUTB(np, nc_stest2, EXT|np->rv_stest2);
  1668. OUTB(np, nc_stest3, TE); /* TolerANT enable */
  1669. OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
  1670. /*
  1671. * For now, disable AIP generation on C1010-66.
  1672. */
  1673. if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)
  1674. OUTB(np, nc_aipcntl1, DISAIP);
  1675. /*
  1676. * C10101 rev. 0 errata.
  1677. * Errant SGE's when in narrow. Write bits 4 & 5 of
  1678. * STEST1 register to disable SGE. We probably should do
  1679. * that from SCRIPTS for each selection/reselection, but
  1680. * I just don't want. :)
  1681. */
  1682. if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
  1683. np->revision_id < 1)
  1684. OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
  1685. /*
  1686. * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
  1687. * Disable overlapped arbitration for some dual function devices,
  1688. * regardless revision id (kind of post-chip-design feature. ;-))
  1689. */
  1690. if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
  1691. OUTB(np, nc_ctest0, (1<<5));
  1692. else if (np->device_id == PCI_DEVICE_ID_NCR_53C896)
  1693. np->rv_ccntl0 |= DPR;
  1694. /*
  1695. * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
  1696. * and/or hardware phase mismatch, since only such chips
  1697. * seem to support those IO registers.
  1698. */
  1699. if (np->features & (FE_DAC|FE_NOPM)) {
  1700. OUTB(np, nc_ccntl0, np->rv_ccntl0);
  1701. OUTB(np, nc_ccntl1, np->rv_ccntl1);
  1702. }
  1703. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1704. /*
  1705. * Set up scratch C and DRS IO registers to map the 32 bit
  1706. * DMA address range our data structures are located in.
  1707. */
  1708. if (np->use_dac) {
  1709. np->dmap_bah[0] = 0; /* ??? */
  1710. OUTL(np, nc_scrx[0], np->dmap_bah[0]);
  1711. OUTL(np, nc_drs, np->dmap_bah[0]);
  1712. }
  1713. #endif
  1714. /*
  1715. * If phase mismatch handled by scripts (895A/896/1010),
  1716. * set PM jump addresses.
  1717. */
  1718. if (np->features & FE_NOPM) {
  1719. OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
  1720. OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
  1721. }
  1722. /*
  1723. * Enable GPIO0 pin for writing if LED support from SCRIPTS.
  1724. * Also set GPIO5 and clear GPIO6 if hardware LED control.
  1725. */
  1726. if (np->features & FE_LED0)
  1727. OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
  1728. else if (np->features & FE_LEDC)
  1729. OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
  1730. /*
  1731. * enable ints
  1732. */
  1733. OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
  1734. OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
  1735. /*
  1736. * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
  1737. * Try to eat the spurious SBMC interrupt that may occur when
  1738. * we reset the chip but not the SCSI BUS (at initialization).
  1739. */
  1740. if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
  1741. OUTONW(np, nc_sien, SBMC);
  1742. if (reason == 0) {
  1743. INB(np, nc_mbox1);
  1744. mdelay(100);
  1745. INW(np, nc_sist);
  1746. }
  1747. np->scsi_mode = INB(np, nc_stest4) & SMODE;
  1748. }
  1749. /*
  1750. * Fill in target structure.
  1751. * Reinitialize usrsync.
  1752. * Reinitialize usrwide.
  1753. * Prepare sync negotiation according to actual SCSI bus mode.
  1754. */
  1755. for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
  1756. struct sym_tcb *tp = &np->target[i];
  1757. tp->to_reset = 0;
  1758. tp->head.sval = 0;
  1759. tp->head.wval = np->rv_scntl3;
  1760. tp->head.uval = 0;
  1761. }
  1762. /*
  1763. * Download SCSI SCRIPTS to on-chip RAM if present,
  1764. * and start script processor.
  1765. * We do the download preferently from the CPU.
  1766. * For platforms that may not support PCI memory mapping,
  1767. * we use simple SCRIPTS that performs MEMORY MOVEs.
  1768. */
  1769. phys = SCRIPTA_BA(np, init);
  1770. if (np->ram_ba) {
  1771. if (sym_verbose >= 2)
  1772. printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
  1773. memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
  1774. if (np->ram_ws == 8192) {
  1775. memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
  1776. phys = scr_to_cpu(np->scr_ram_seg);
  1777. OUTL(np, nc_mmws, phys);
  1778. OUTL(np, nc_mmrs, phys);
  1779. OUTL(np, nc_sfs, phys);
  1780. phys = SCRIPTB_BA(np, start64);
  1781. }
  1782. }
  1783. np->istat_sem = 0;
  1784. OUTL(np, nc_dsa, np->hcb_ba);
  1785. OUTL_DSP(np, phys);
  1786. /*
  1787. * Notify the XPT about the RESET condition.
  1788. */
  1789. if (reason != 0)
  1790. sym_xpt_async_bus_reset(np);
  1791. }
  1792. /*
  1793. * Switch trans mode for current job and its target.
  1794. */
  1795. static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1796. u_char per, u_char wide, u_char div, u_char fak)
  1797. {
  1798. SYM_QUEHEAD *qp;
  1799. u_char sval, wval, uval;
  1800. struct sym_tcb *tp = &np->target[target];
  1801. assert(target == (INB(np, nc_sdid) & 0x0f));
  1802. sval = tp->head.sval;
  1803. wval = tp->head.wval;
  1804. uval = tp->head.uval;
  1805. #if 0
  1806. printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
  1807. sval, wval, uval, np->rv_scntl3);
  1808. #endif
  1809. /*
  1810. * Set the offset.
  1811. */
  1812. if (!(np->features & FE_C10))
  1813. sval = (sval & ~0x1f) | ofs;
  1814. else
  1815. sval = (sval & ~0x3f) | ofs;
  1816. /*
  1817. * Set the sync divisor and extra clock factor.
  1818. */
  1819. if (ofs != 0) {
  1820. wval = (wval & ~0x70) | ((div+1) << 4);
  1821. if (!(np->features & FE_C10))
  1822. sval = (sval & ~0xe0) | (fak << 5);
  1823. else {
  1824. uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
  1825. if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
  1826. if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
  1827. }
  1828. }
  1829. /*
  1830. * Set the bus width.
  1831. */
  1832. wval = wval & ~EWS;
  1833. if (wide != 0)
  1834. wval |= EWS;
  1835. /*
  1836. * Set misc. ultra enable bits.
  1837. */
  1838. if (np->features & FE_C10) {
  1839. uval = uval & ~(U3EN|AIPCKEN);
  1840. if (opts) {
  1841. assert(np->features & FE_U3EN);
  1842. uval |= U3EN;
  1843. }
  1844. } else {
  1845. wval = wval & ~ULTRA;
  1846. if (per <= 12) wval |= ULTRA;
  1847. }
  1848. /*
  1849. * Stop there if sync parameters are unchanged.
  1850. */
  1851. if (tp->head.sval == sval &&
  1852. tp->head.wval == wval &&
  1853. tp->head.uval == uval)
  1854. return;
  1855. tp->head.sval = sval;
  1856. tp->head.wval = wval;
  1857. tp->head.uval = uval;
  1858. /*
  1859. * Disable extended Sreq/Sack filtering if per < 50.
  1860. * Not supported on the C1010.
  1861. */
  1862. if (per < 50 && !(np->features & FE_C10))
  1863. OUTOFFB(np, nc_stest2, EXT);
  1864. /*
  1865. * set actual value and sync_status
  1866. */
  1867. OUTB(np, nc_sxfer, tp->head.sval);
  1868. OUTB(np, nc_scntl3, tp->head.wval);
  1869. if (np->features & FE_C10) {
  1870. OUTB(np, nc_scntl4, tp->head.uval);
  1871. }
  1872. /*
  1873. * patch ALL busy ccbs of this target.
  1874. */
  1875. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  1876. struct sym_ccb *cp;
  1877. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1878. if (cp->target != target)
  1879. continue;
  1880. cp->phys.select.sel_scntl3 = tp->head.wval;
  1881. cp->phys.select.sel_sxfer = tp->head.sval;
  1882. if (np->features & FE_C10) {
  1883. cp->phys.select.sel_scntl4 = tp->head.uval;
  1884. }
  1885. }
  1886. }
  1887. /*
  1888. * We received a WDTR.
  1889. * Let everything be aware of the changes.
  1890. */
  1891. static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
  1892. {
  1893. struct sym_tcb *tp = &np->target[target];
  1894. struct scsi_target *starget = tp->starget;
  1895. if (spi_width(starget) == wide)
  1896. return;
  1897. sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
  1898. tp->tgoal.width = wide;
  1899. spi_offset(starget) = 0;
  1900. spi_period(starget) = 0;
  1901. spi_width(starget) = wide;
  1902. spi_iu(starget) = 0;
  1903. spi_dt(starget) = 0;
  1904. spi_qas(starget) = 0;
  1905. if (sym_verbose >= 3)
  1906. spi_display_xfer_agreement(starget);
  1907. }
  1908. /*
  1909. * We received a SDTR.
  1910. * Let everything be aware of the changes.
  1911. */
  1912. static void
  1913. sym_setsync(struct sym_hcb *np, int target,
  1914. u_char ofs, u_char per, u_char div, u_char fak)
  1915. {
  1916. struct sym_tcb *tp = &np->target[target];
  1917. struct scsi_target *starget = tp->starget;
  1918. u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
  1919. sym_settrans(np, target, 0, ofs, per, wide, div, fak);
  1920. spi_period(starget) = per;
  1921. spi_offset(starget) = ofs;
  1922. spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
  1923. if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
  1924. tp->tgoal.period = per;
  1925. tp->tgoal.offset = ofs;
  1926. tp->tgoal.check_nego = 0;
  1927. }
  1928. spi_display_xfer_agreement(starget);
  1929. }
  1930. /*
  1931. * We received a PPR.
  1932. * Let everything be aware of the changes.
  1933. */
  1934. static void
  1935. sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1936. u_char per, u_char wide, u_char div, u_char fak)
  1937. {
  1938. struct sym_tcb *tp = &np->target[target];
  1939. struct scsi_target *starget = tp->starget;
  1940. sym_settrans(np, target, opts, ofs, per, wide, div, fak);
  1941. spi_width(starget) = tp->tgoal.width = wide;
  1942. spi_period(starget) = tp->tgoal.period = per;
  1943. spi_offset(starget) = tp->tgoal.offset = ofs;
  1944. spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
  1945. spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
  1946. spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
  1947. tp->tgoal.check_nego = 0;
  1948. spi_display_xfer_agreement(starget);
  1949. }
  1950. /*
  1951. * generic recovery from scsi interrupt
  1952. *
  1953. * The doc says that when the chip gets an SCSI interrupt,
  1954. * it tries to stop in an orderly fashion, by completing
  1955. * an instruction fetch that had started or by flushing
  1956. * the DMA fifo for a write to memory that was executing.
  1957. * Such a fashion is not enough to know if the instruction
  1958. * that was just before the current DSP value has been
  1959. * executed or not.
  1960. *
  1961. * There are some small SCRIPTS sections that deal with
  1962. * the start queue and the done queue that may break any
  1963. * assomption from the C code if we are interrupted
  1964. * inside, so we reset if this happens. Btw, since these
  1965. * SCRIPTS sections are executed while the SCRIPTS hasn't
  1966. * started SCSI operations, it is very unlikely to happen.
  1967. *
  1968. * All the driver data structures are supposed to be
  1969. * allocated from the same 4 GB memory window, so there
  1970. * is a 1 to 1 relationship between DSA and driver data
  1971. * structures. Since we are careful :) to invalidate the
  1972. * DSA when we complete a command or when the SCRIPTS
  1973. * pushes a DSA into a queue, we can trust it when it
  1974. * points to a CCB.
  1975. */
  1976. static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
  1977. {
  1978. u32 dsp = INL(np, nc_dsp);
  1979. u32 dsa = INL(np, nc_dsa);
  1980. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  1981. /*
  1982. * If we haven't been interrupted inside the SCRIPTS
  1983. * critical pathes, we can safely restart the SCRIPTS
  1984. * and trust the DSA value if it matches a CCB.
  1985. */
  1986. if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
  1987. dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
  1988. (!(dsp > SCRIPTA_BA(np, ungetjob) &&
  1989. dsp < SCRIPTA_BA(np, reselect) + 1)) &&
  1990. (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
  1991. dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
  1992. (!(dsp > SCRIPTA_BA(np, done) &&
  1993. dsp < SCRIPTA_BA(np, done_end) + 1))) {
  1994. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  1995. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  1996. /*
  1997. * If we have a CCB, let the SCRIPTS call us back for
  1998. * the handling of the error with SCRATCHA filled with
  1999. * STARTPOS. This way, we will be able to freeze the
  2000. * device queue and requeue awaiting IOs.
  2001. */
  2002. if (cp) {
  2003. cp->host_status = hsts;
  2004. OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
  2005. }
  2006. /*
  2007. * Otherwise just restart the SCRIPTS.
  2008. */
  2009. else {
  2010. OUTL(np, nc_dsa, 0xffffff);
  2011. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2012. }
  2013. }
  2014. else
  2015. goto reset_all;
  2016. return;
  2017. reset_all:
  2018. sym_start_reset(np);
  2019. }
  2020. /*
  2021. * chip exception handler for selection timeout
  2022. */
  2023. static void sym_int_sto (struct sym_hcb *np)
  2024. {
  2025. u32 dsp = INL(np, nc_dsp);
  2026. if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
  2027. if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
  2028. sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
  2029. else
  2030. sym_start_reset(np);
  2031. }
  2032. /*
  2033. * chip exception handler for unexpected disconnect
  2034. */
  2035. static void sym_int_udc (struct sym_hcb *np)
  2036. {
  2037. printf ("%s: unexpected disconnect\n", sym_name(np));
  2038. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2039. }
  2040. /*
  2041. * chip exception handler for SCSI bus mode change
  2042. *
  2043. * spi2-r12 11.2.3 says a transceiver mode change must
  2044. * generate a reset event and a device that detects a reset
  2045. * event shall initiate a hard reset. It says also that a
  2046. * device that detects a mode change shall set data transfer
  2047. * mode to eight bit asynchronous, etc...
  2048. * So, just reinitializing all except chip should be enough.
  2049. */
  2050. static void sym_int_sbmc (struct sym_hcb *np)
  2051. {
  2052. u_char scsi_mode = INB(np, nc_stest4) & SMODE;
  2053. /*
  2054. * Notify user.
  2055. */
  2056. printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
  2057. sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
  2058. /*
  2059. * Should suspend command processing for a few seconds and
  2060. * reinitialize all except the chip.
  2061. */
  2062. sym_start_up (np, 2);
  2063. }
  2064. /*
  2065. * chip exception handler for SCSI parity error.
  2066. *
  2067. * When the chip detects a SCSI parity error and is
  2068. * currently executing a (CH)MOV instruction, it does
  2069. * not interrupt immediately, but tries to finish the
  2070. * transfer of the current scatter entry before
  2071. * interrupting. The following situations may occur:
  2072. *
  2073. * - The complete scatter entry has been transferred
  2074. * without the device having changed phase.
  2075. * The chip will then interrupt with the DSP pointing
  2076. * to the instruction that follows the MOV.
  2077. *
  2078. * - A phase mismatch occurs before the MOV finished
  2079. * and phase errors are to be handled by the C code.
  2080. * The chip will then interrupt with both PAR and MA
  2081. * conditions set.
  2082. *
  2083. * - A phase mismatch occurs before the MOV finished and
  2084. * phase errors are to be handled by SCRIPTS.
  2085. * The chip will load the DSP with the phase mismatch
  2086. * JUMP address and interrupt the host processor.
  2087. */
  2088. static void sym_int_par (struct sym_hcb *np, u_short sist)
  2089. {
  2090. u_char hsts = INB(np, HS_PRT);
  2091. u32 dsp = INL(np, nc_dsp);
  2092. u32 dbc = INL(np, nc_dbc);
  2093. u32 dsa = INL(np, nc_dsa);
  2094. u_char sbcl = INB(np, nc_sbcl);
  2095. u_char cmd = dbc >> 24;
  2096. int phase = cmd & 7;
  2097. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  2098. printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
  2099. sym_name(np), hsts, dbc, sbcl);
  2100. /*
  2101. * Check that the chip is connected to the SCSI BUS.
  2102. */
  2103. if (!(INB(np, nc_scntl1) & ISCON)) {
  2104. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2105. return;
  2106. }
  2107. /*
  2108. * If the nexus is not clearly identified, reset the bus.
  2109. * We will try to do better later.
  2110. */
  2111. if (!cp)
  2112. goto reset_all;
  2113. /*
  2114. * Check instruction was a MOV, direction was INPUT and
  2115. * ATN is asserted.
  2116. */
  2117. if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
  2118. goto reset_all;
  2119. /*
  2120. * Keep track of the parity error.
  2121. */
  2122. OUTONB(np, HF_PRT, HF_EXT_ERR);
  2123. cp->xerr_status |= XE_PARITY_ERR;
  2124. /*
  2125. * Prepare the message to send to the device.
  2126. */
  2127. np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
  2128. /*
  2129. * If the old phase was DATA IN phase, we have to deal with
  2130. * the 3 situations described above.
  2131. * For other input phases (MSG IN and STATUS), the device
  2132. * must resend the whole thing that failed parity checking
  2133. * or signal error. So, jumping to dispatcher should be OK.
  2134. */
  2135. if (phase == 1 || phase == 5) {
  2136. /* Phase mismatch handled by SCRIPTS */
  2137. if (dsp == SCRIPTB_BA(np, pm_handle))
  2138. OUTL_DSP(np, dsp);
  2139. /* Phase mismatch handled by the C code */
  2140. else if (sist & MA)
  2141. sym_int_ma (np);
  2142. /* No phase mismatch occurred */
  2143. else {
  2144. sym_set_script_dp (np, cp, dsp);
  2145. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2146. }
  2147. }
  2148. else if (phase == 7) /* We definitely cannot handle parity errors */
  2149. #if 1 /* in message-in phase due to the relection */
  2150. goto reset_all; /* path and various message anticipations. */
  2151. #else
  2152. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  2153. #endif
  2154. else
  2155. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2156. return;
  2157. reset_all:
  2158. sym_start_reset(np);
  2159. return;
  2160. }
  2161. /*
  2162. * chip exception handler for phase errors.
  2163. *
  2164. * We have to construct a new transfer descriptor,
  2165. * to transfer the rest of the current block.
  2166. */
  2167. static void sym_int_ma (struct sym_hcb *np)
  2168. {
  2169. u32 dbc;
  2170. u32 rest;
  2171. u32 dsp;
  2172. u32 dsa;
  2173. u32 nxtdsp;
  2174. u32 *vdsp;
  2175. u32 oadr, olen;
  2176. u32 *tblp;
  2177. u32 newcmd;
  2178. u_int delta;
  2179. u_char cmd;
  2180. u_char hflags, hflags0;
  2181. struct sym_pmc *pm;
  2182. struct sym_ccb *cp;
  2183. dsp = INL(np, nc_dsp);
  2184. dbc = INL(np, nc_dbc);
  2185. dsa = INL(np, nc_dsa);
  2186. cmd = dbc >> 24;
  2187. rest = dbc & 0xffffff;
  2188. delta = 0;
  2189. /*
  2190. * locate matching cp if any.
  2191. */
  2192. cp = sym_ccb_from_dsa(np, dsa);
  2193. /*
  2194. * Donnot take into account dma fifo and various buffers in
  2195. * INPUT phase since the chip flushes everything before
  2196. * raising the MA interrupt for interrupted INPUT phases.
  2197. * For DATA IN phase, we will check for the SWIDE later.
  2198. */
  2199. if ((cmd & 7) != 1 && (cmd & 7) != 5) {
  2200. u_char ss0, ss2;
  2201. if (np->features & FE_DFBC)
  2202. delta = INW(np, nc_dfbc);
  2203. else {
  2204. u32 dfifo;
  2205. /*
  2206. * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
  2207. */
  2208. dfifo = INL(np, nc_dfifo);
  2209. /*
  2210. * Calculate remaining bytes in DMA fifo.
  2211. * (CTEST5 = dfifo >> 16)
  2212. */
  2213. if (dfifo & (DFS << 16))
  2214. delta = ((((dfifo >> 8) & 0x300) |
  2215. (dfifo & 0xff)) - rest) & 0x3ff;
  2216. else
  2217. delta = ((dfifo & 0xff) - rest) & 0x7f;
  2218. }
  2219. /*
  2220. * The data in the dma fifo has not been transfered to
  2221. * the target -> add the amount to the rest
  2222. * and clear the data.
  2223. * Check the sstat2 register in case of wide transfer.
  2224. */
  2225. rest += delta;
  2226. ss0 = INB(np, nc_sstat0);
  2227. if (ss0 & OLF) rest++;
  2228. if (!(np->features & FE_C10))
  2229. if (ss0 & ORF) rest++;
  2230. if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
  2231. ss2 = INB(np, nc_sstat2);
  2232. if (ss2 & OLF1) rest++;
  2233. if (!(np->features & FE_C10))
  2234. if (ss2 & ORF1) rest++;
  2235. }
  2236. /*
  2237. * Clear fifos.
  2238. */
  2239. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
  2240. OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
  2241. }
  2242. /*
  2243. * log the information
  2244. */
  2245. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  2246. printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
  2247. (unsigned) rest, (unsigned) delta);
  2248. /*
  2249. * try to find the interrupted script command,
  2250. * and the address at which to continue.
  2251. */
  2252. vdsp = NULL;
  2253. nxtdsp = 0;
  2254. if (dsp > np->scripta_ba &&
  2255. dsp <= np->scripta_ba + np->scripta_sz) {
  2256. vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
  2257. nxtdsp = dsp;
  2258. }
  2259. else if (dsp > np->scriptb_ba &&
  2260. dsp <= np->scriptb_ba + np->scriptb_sz) {
  2261. vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
  2262. nxtdsp = dsp;
  2263. }
  2264. /*
  2265. * log the information
  2266. */
  2267. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2268. printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
  2269. cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
  2270. }
  2271. if (!vdsp) {
  2272. printf ("%s: interrupted SCRIPT address not found.\n",
  2273. sym_name (np));
  2274. goto reset_all;
  2275. }
  2276. if (!cp) {
  2277. printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
  2278. sym_name (np));
  2279. goto reset_all;
  2280. }
  2281. /*
  2282. * get old startaddress and old length.
  2283. */
  2284. oadr = scr_to_cpu(vdsp[1]);
  2285. if (cmd & 0x10) { /* Table indirect */
  2286. tblp = (u32 *) ((char*) &cp->phys + oadr);
  2287. olen = scr_to_cpu(tblp[0]);
  2288. oadr = scr_to_cpu(tblp[1]);
  2289. } else {
  2290. tblp = (u32 *) 0;
  2291. olen = scr_to_cpu(vdsp[0]) & 0xffffff;
  2292. }
  2293. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2294. printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
  2295. (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
  2296. tblp,
  2297. (unsigned) olen,
  2298. (unsigned) oadr);
  2299. }
  2300. /*
  2301. * check cmd against assumed interrupted script command.
  2302. * If dt data phase, the MOVE instruction hasn't bit 4 of
  2303. * the phase.
  2304. */
  2305. if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
  2306. sym_print_addr(cp->cmd,
  2307. "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
  2308. cmd, scr_to_cpu(vdsp[0]) >> 24);
  2309. goto reset_all;
  2310. }
  2311. /*
  2312. * if old phase not dataphase, leave here.
  2313. */
  2314. if (cmd & 2) {
  2315. sym_print_addr(cp->cmd,
  2316. "phase change %x-%x %d@%08x resid=%d.\n",
  2317. cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
  2318. (unsigned)oadr, (unsigned)rest);
  2319. goto unexpected_phase;
  2320. }
  2321. /*
  2322. * Choose the correct PM save area.
  2323. *
  2324. * Look at the PM_SAVE SCRIPT if you want to understand
  2325. * this stuff. The equivalent code is implemented in
  2326. * SCRIPTS for the 895A, 896 and 1010 that are able to
  2327. * handle PM from the SCRIPTS processor.
  2328. */
  2329. hflags0 = INB(np, HF_PRT);
  2330. hflags = hflags0;
  2331. if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
  2332. if (hflags & HF_IN_PM0)
  2333. nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
  2334. else if (hflags & HF_IN_PM1)
  2335. nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
  2336. if (hflags & HF_DP_SAVED)
  2337. hflags ^= HF_ACT_PM;
  2338. }
  2339. if (!(hflags & HF_ACT_PM)) {
  2340. pm = &cp->phys.pm0;
  2341. newcmd = SCRIPTA_BA(np, pm0_data);
  2342. }
  2343. else {
  2344. pm = &cp->phys.pm1;
  2345. newcmd = SCRIPTA_BA(np, pm1_data);
  2346. }
  2347. hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
  2348. if (hflags != hflags0)
  2349. OUTB(np, HF_PRT, hflags);
  2350. /*
  2351. * fillin the phase mismatch context
  2352. */
  2353. pm->sg.addr = cpu_to_scr(oadr + olen - rest);
  2354. pm->sg.size = cpu_to_scr(rest);
  2355. pm->ret = cpu_to_scr(nxtdsp);
  2356. /*
  2357. * If we have a SWIDE,
  2358. * - prepare the address to write the SWIDE from SCRIPTS,
  2359. * - compute the SCRIPTS address to restart from,
  2360. * - move current data pointer context by one byte.
  2361. */
  2362. nxtdsp = SCRIPTA_BA(np, dispatch);
  2363. if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
  2364. (INB(np, nc_scntl2) & WSR)) {
  2365. u32 tmp;
  2366. /*
  2367. * Set up the table indirect for the MOVE
  2368. * of the residual byte and adjust the data
  2369. * pointer context.
  2370. */
  2371. tmp = scr_to_cpu(pm->sg.addr);
  2372. cp->phys.wresid.addr = cpu_to_scr(tmp);
  2373. pm->sg.addr = cpu_to_scr(tmp + 1);
  2374. tmp = scr_to_cpu(pm->sg.size);
  2375. cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
  2376. pm->sg.size = cpu_to_scr(tmp - 1);
  2377. /*
  2378. * If only the residual byte is to be moved,
  2379. * no PM context is needed.
  2380. */
  2381. if ((tmp&0xffffff) == 1)
  2382. newcmd = pm->ret;
  2383. /*
  2384. * Prepare the address of SCRIPTS that will
  2385. * move the residual byte to memory.
  2386. */
  2387. nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
  2388. }
  2389. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2390. sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
  2391. hflags0, hflags, newcmd,
  2392. (unsigned)scr_to_cpu(pm->sg.addr),
  2393. (unsigned)scr_to_cpu(pm->sg.size),
  2394. (unsigned)scr_to_cpu(pm->ret));
  2395. }
  2396. /*
  2397. * Restart the SCRIPTS processor.
  2398. */
  2399. sym_set_script_dp (np, cp, newcmd);
  2400. OUTL_DSP(np, nxtdsp);
  2401. return;
  2402. /*
  2403. * Unexpected phase changes that occurs when the current phase
  2404. * is not a DATA IN or DATA OUT phase are due to error conditions.
  2405. * Such event may only happen when the SCRIPTS is using a
  2406. * multibyte SCSI MOVE.
  2407. *
  2408. * Phase change Some possible cause
  2409. *
  2410. * COMMAND --> MSG IN SCSI parity error detected by target.
  2411. * COMMAND --> STATUS Bad command or refused by target.
  2412. * MSG OUT --> MSG IN Message rejected by target.
  2413. * MSG OUT --> COMMAND Bogus target that discards extended
  2414. * negotiation messages.
  2415. *
  2416. * The code below does not care of the new phase and so
  2417. * trusts the target. Why to annoy it ?
  2418. * If the interrupted phase is COMMAND phase, we restart at
  2419. * dispatcher.
  2420. * If a target does not get all the messages after selection,
  2421. * the code assumes blindly that the target discards extended
  2422. * messages and clears the negotiation status.
  2423. * If the target does not want all our response to negotiation,
  2424. * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
  2425. * bloat for such a should_not_happen situation).
  2426. * In all other situation, we reset the BUS.
  2427. * Are these assumptions reasonnable ? (Wait and see ...)
  2428. */
  2429. unexpected_phase:
  2430. dsp -= 8;
  2431. nxtdsp = 0;
  2432. switch (cmd & 7) {
  2433. case 2: /* COMMAND phase */
  2434. nxtdsp = SCRIPTA_BA(np, dispatch);
  2435. break;
  2436. #if 0
  2437. case 3: /* STATUS phase */
  2438. nxtdsp = SCRIPTA_BA(np, dispatch);
  2439. break;
  2440. #endif
  2441. case 6: /* MSG OUT phase */
  2442. /*
  2443. * If the device may want to use untagged when we want
  2444. * tagged, we prepare an IDENTIFY without disc. granted,
  2445. * since we will not be able to handle reselect.
  2446. * Otherwise, we just don't care.
  2447. */
  2448. if (dsp == SCRIPTA_BA(np, send_ident)) {
  2449. if (cp->tag != NO_TAG && olen - rest <= 3) {
  2450. cp->host_status = HS_BUSY;
  2451. np->msgout[0] = IDENTIFY(0, cp->lun);
  2452. nxtdsp = SCRIPTB_BA(np, ident_break_atn);
  2453. }
  2454. else
  2455. nxtdsp = SCRIPTB_BA(np, ident_break);
  2456. }
  2457. else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
  2458. dsp == SCRIPTB_BA(np, send_sdtr) ||
  2459. dsp == SCRIPTB_BA(np, send_ppr)) {
  2460. nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
  2461. if (dsp == SCRIPTB_BA(np, send_ppr)) {
  2462. struct scsi_device *dev = cp->cmd->device;
  2463. dev->ppr = 0;
  2464. }
  2465. }
  2466. break;
  2467. #if 0
  2468. case 7: /* MSG IN phase */
  2469. nxtdsp = SCRIPTA_BA(np, clrack);
  2470. break;
  2471. #endif
  2472. }
  2473. if (nxtdsp) {
  2474. OUTL_DSP(np, nxtdsp);
  2475. return;
  2476. }
  2477. reset_all:
  2478. sym_start_reset(np);
  2479. }
  2480. /*
  2481. * chip interrupt handler
  2482. *
  2483. * In normal situations, interrupt conditions occur one at
  2484. * a time. But when something bad happens on the SCSI BUS,
  2485. * the chip may raise several interrupt flags before
  2486. * stopping and interrupting the CPU. The additionnal
  2487. * interrupt flags are stacked in some extra registers
  2488. * after the SIP and/or DIP flag has been raised in the
  2489. * ISTAT. After the CPU has read the interrupt condition
  2490. * flag from SIST or DSTAT, the chip unstacks the other
  2491. * interrupt flags and sets the corresponding bits in
  2492. * SIST or DSTAT. Since the chip starts stacking once the
  2493. * SIP or DIP flag is set, there is a small window of time
  2494. * where the stacking does not occur.
  2495. *
  2496. * Typically, multiple interrupt conditions may happen in
  2497. * the following situations:
  2498. *
  2499. * - SCSI parity error + Phase mismatch (PAR|MA)
  2500. * When an parity error is detected in input phase
  2501. * and the device switches to msg-in phase inside a
  2502. * block MOV.
  2503. * - SCSI parity error + Unexpected disconnect (PAR|UDC)
  2504. * When a stupid device does not want to handle the
  2505. * recovery of an SCSI parity error.
  2506. * - Some combinations of STO, PAR, UDC, ...
  2507. * When using non compliant SCSI stuff, when user is
  2508. * doing non compliant hot tampering on the BUS, when
  2509. * something really bad happens to a device, etc ...
  2510. *
  2511. * The heuristic suggested by SYMBIOS to handle
  2512. * multiple interrupts is to try unstacking all
  2513. * interrupts conditions and to handle them on some
  2514. * priority based on error severity.
  2515. * This will work when the unstacking has been
  2516. * successful, but we cannot be 100 % sure of that,
  2517. * since the CPU may have been faster to unstack than
  2518. * the chip is able to stack. Hmmm ... But it seems that
  2519. * such a situation is very unlikely to happen.
  2520. *
  2521. * If this happen, for example STO caught by the CPU
  2522. * then UDC happenning before the CPU have restarted
  2523. * the SCRIPTS, the driver may wrongly complete the
  2524. * same command on UDC, since the SCRIPTS didn't restart
  2525. * and the DSA still points to the same command.
  2526. * We avoid this situation by setting the DSA to an
  2527. * invalid value when the CCB is completed and before
  2528. * restarting the SCRIPTS.
  2529. *
  2530. * Another issue is that we need some section of our
  2531. * recovery procedures to be somehow uninterruptible but
  2532. * the SCRIPTS processor does not provides such a
  2533. * feature. For this reason, we handle recovery preferently
  2534. * from the C code and check against some SCRIPTS critical
  2535. * sections from the C code.
  2536. *
  2537. * Hopefully, the interrupt handling of the driver is now
  2538. * able to resist to weird BUS error conditions, but donnot
  2539. * ask me for any guarantee that it will never fail. :-)
  2540. * Use at your own decision and risk.
  2541. */
  2542. void sym_interrupt (struct sym_hcb *np)
  2543. {
  2544. u_char istat, istatc;
  2545. u_char dstat;
  2546. u_short sist;
  2547. /*
  2548. * interrupt on the fly ?
  2549. * (SCRIPTS may still be running)
  2550. *
  2551. * A `dummy read' is needed to ensure that the
  2552. * clear of the INTF flag reaches the device
  2553. * and that posted writes are flushed to memory
  2554. * before the scanning of the DONE queue.
  2555. * Note that SCRIPTS also (dummy) read to memory
  2556. * prior to deliver the INTF interrupt condition.
  2557. */
  2558. istat = INB(np, nc_istat);
  2559. if (istat & INTF) {
  2560. OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
  2561. istat = INB(np, nc_istat); /* DUMMY READ */
  2562. if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
  2563. sym_wakeup_done(np);
  2564. }
  2565. if (!(istat & (SIP|DIP)))
  2566. return;
  2567. #if 0 /* We should never get this one */
  2568. if (istat & CABRT)
  2569. OUTB(np, nc_istat, CABRT);
  2570. #endif
  2571. /*
  2572. * PAR and MA interrupts may occur at the same time,
  2573. * and we need to know of both in order to handle
  2574. * this situation properly. We try to unstack SCSI
  2575. * interrupts for that reason. BTW, I dislike a LOT
  2576. * such a loop inside the interrupt routine.
  2577. * Even if DMA interrupt stacking is very unlikely to
  2578. * happen, we also try unstacking these ones, since
  2579. * this has no performance impact.
  2580. */
  2581. sist = 0;
  2582. dstat = 0;
  2583. istatc = istat;
  2584. do {
  2585. if (istatc & SIP)
  2586. sist |= INW(np, nc_sist);
  2587. if (istatc & DIP)
  2588. dstat |= INB(np, nc_dstat);
  2589. istatc = INB(np, nc_istat);
  2590. istat |= istatc;
  2591. } while (istatc & (SIP|DIP));
  2592. if (DEBUG_FLAGS & DEBUG_TINY)
  2593. printf ("<%d|%x:%x|%x:%x>",
  2594. (int)INB(np, nc_scr0),
  2595. dstat,sist,
  2596. (unsigned)INL(np, nc_dsp),
  2597. (unsigned)INL(np, nc_dbc));
  2598. /*
  2599. * On paper, a memory read barrier may be needed here to
  2600. * prevent out of order LOADs by the CPU from having
  2601. * prefetched stale data prior to DMA having occurred.
  2602. * And since we are paranoid ... :)
  2603. */
  2604. MEMORY_READ_BARRIER();
  2605. /*
  2606. * First, interrupts we want to service cleanly.
  2607. *
  2608. * Phase mismatch (MA) is the most frequent interrupt
  2609. * for chip earlier than the 896 and so we have to service
  2610. * it as quickly as possible.
  2611. * A SCSI parity error (PAR) may be combined with a phase
  2612. * mismatch condition (MA).
  2613. * Programmed interrupts (SIR) are used to call the C code
  2614. * from SCRIPTS.
  2615. * The single step interrupt (SSI) is not used in this
  2616. * driver.
  2617. */
  2618. if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
  2619. !(dstat & (MDPE|BF|ABRT|IID))) {
  2620. if (sist & PAR) sym_int_par (np, sist);
  2621. else if (sist & MA) sym_int_ma (np);
  2622. else if (dstat & SIR) sym_int_sir (np);
  2623. else if (dstat & SSI) OUTONB_STD();
  2624. else goto unknown_int;
  2625. return;
  2626. }
  2627. /*
  2628. * Now, interrupts that donnot happen in normal
  2629. * situations and that we may need to recover from.
  2630. *
  2631. * On SCSI RESET (RST), we reset everything.
  2632. * On SCSI BUS MODE CHANGE (SBMC), we complete all
  2633. * active CCBs with RESET status, prepare all devices
  2634. * for negotiating again and restart the SCRIPTS.
  2635. * On STO and UDC, we complete the CCB with the corres-
  2636. * ponding status and restart the SCRIPTS.
  2637. */
  2638. if (sist & RST) {
  2639. printf("%s: SCSI BUS reset detected.\n", sym_name(np));
  2640. sym_start_up (np, 1);
  2641. return;
  2642. }
  2643. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  2644. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  2645. if (!(sist & (GEN|HTH|SGE)) &&
  2646. !(dstat & (MDPE|BF|ABRT|IID))) {
  2647. if (sist & SBMC) sym_int_sbmc (np);
  2648. else if (sist & STO) sym_int_sto (np);
  2649. else if (sist & UDC) sym_int_udc (np);
  2650. else goto unknown_int;
  2651. return;
  2652. }
  2653. /*
  2654. * Now, interrupts we are not able to recover cleanly.
  2655. *
  2656. * Log message for hard errors.
  2657. * Reset everything.
  2658. */
  2659. sym_log_hard_error(np, sist, dstat);
  2660. if ((sist & (GEN|HTH|SGE)) ||
  2661. (dstat & (MDPE|BF|ABRT|IID))) {
  2662. sym_start_reset(np);
  2663. return;
  2664. }
  2665. unknown_int:
  2666. /*
  2667. * We just miss the cause of the interrupt. :(
  2668. * Print a message. The timeout will do the real work.
  2669. */
  2670. printf( "%s: unknown interrupt(s) ignored, "
  2671. "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
  2672. sym_name(np), istat, dstat, sist);
  2673. }
  2674. /*
  2675. * Dequeue from the START queue all CCBs that match
  2676. * a given target/lun/task condition (-1 means all),
  2677. * and move them from the BUSY queue to the COMP queue
  2678. * with DID_SOFT_ERROR status condition.
  2679. * This function is used during error handling/recovery.
  2680. * It is called with SCRIPTS not running.
  2681. */
  2682. static int
  2683. sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
  2684. {
  2685. int j;
  2686. struct sym_ccb *cp;
  2687. /*
  2688. * Make sure the starting index is within range.
  2689. */
  2690. assert((i >= 0) && (i < 2*MAX_QUEUE));
  2691. /*
  2692. * Walk until end of START queue and dequeue every job
  2693. * that matches the target/lun/task condition.
  2694. */
  2695. j = i;
  2696. while (i != np->squeueput) {
  2697. cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
  2698. assert(cp);
  2699. #ifdef SYM_CONF_IARB_SUPPORT
  2700. /* Forget hints for IARB, they may be no longer relevant */
  2701. cp->host_flags &= ~HF_HINT_IARB;
  2702. #endif
  2703. if ((target == -1 || cp->target == target) &&
  2704. (lun == -1 || cp->lun == lun) &&
  2705. (task == -1 || cp->tag == task)) {
  2706. sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
  2707. sym_remque(&cp->link_ccbq);
  2708. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2709. }
  2710. else {
  2711. if (i != j)
  2712. np->squeue[j] = np->squeue[i];
  2713. if ((j += 2) >= MAX_QUEUE*2) j = 0;
  2714. }
  2715. if ((i += 2) >= MAX_QUEUE*2) i = 0;
  2716. }
  2717. if (i != j) /* Copy back the idle task if needed */
  2718. np->squeue[j] = np->squeue[i];
  2719. np->squeueput = j; /* Update our current start queue pointer */
  2720. return (i - j) / 2;
  2721. }
  2722. /*
  2723. * chip handler for bad SCSI status condition
  2724. *
  2725. * In case of bad SCSI status, we unqueue all the tasks
  2726. * currently queued to the controller but not yet started
  2727. * and then restart the SCRIPTS processor immediately.
  2728. *
  2729. * QUEUE FULL and BUSY conditions are handled the same way.
  2730. * Basically all the not yet started tasks are requeued in
  2731. * device queue and the queue is frozen until a completion.
  2732. *
  2733. * For CHECK CONDITION and COMMAND TERMINATED status, we use
  2734. * the CCB of the failed command to prepare a REQUEST SENSE
  2735. * SCSI command and queue it to the controller queue.
  2736. *
  2737. * SCRATCHA is assumed to have been loaded with STARTPOS
  2738. * before the SCRIPTS called the C code.
  2739. */
  2740. static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
  2741. {
  2742. u32 startp;
  2743. u_char s_status = cp->ssss_status;
  2744. u_char h_flags = cp->host_flags;
  2745. int msglen;
  2746. int i;
  2747. /*
  2748. * Compute the index of the next job to start from SCRIPTS.
  2749. */
  2750. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  2751. /*
  2752. * The last CCB queued used for IARB hint may be
  2753. * no longer relevant. Forget it.
  2754. */
  2755. #ifdef SYM_CONF_IARB_SUPPORT
  2756. if (np->last_cp)
  2757. np->last_cp = 0;
  2758. #endif
  2759. /*
  2760. * Now deal with the SCSI status.
  2761. */
  2762. switch(s_status) {
  2763. case S_BUSY:
  2764. case S_QUEUE_FULL:
  2765. if (sym_verbose >= 2) {
  2766. sym_print_addr(cp->cmd, "%s\n",
  2767. s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
  2768. }
  2769. default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
  2770. sym_complete_error (np, cp);
  2771. break;
  2772. case S_TERMINATED:
  2773. case S_CHECK_COND:
  2774. /*
  2775. * If we get an SCSI error when requesting sense, give up.
  2776. */
  2777. if (h_flags & HF_SENSE) {
  2778. sym_complete_error (np, cp);
  2779. break;
  2780. }
  2781. /*
  2782. * Dequeue all queued CCBs for that device not yet started,
  2783. * and restart the SCRIPTS processor immediately.
  2784. */
  2785. sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  2786. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2787. /*
  2788. * Save some info of the actual IO.
  2789. * Compute the data residual.
  2790. */
  2791. cp->sv_scsi_status = cp->ssss_status;
  2792. cp->sv_xerr_status = cp->xerr_status;
  2793. cp->sv_resid = sym_compute_residual(np, cp);
  2794. /*
  2795. * Prepare all needed data structures for
  2796. * requesting sense data.
  2797. */
  2798. cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
  2799. msglen = 1;
  2800. /*
  2801. * If we are currently using anything different from
  2802. * async. 8 bit data transfers with that target,
  2803. * start a negotiation, since the device may want
  2804. * to report us a UNIT ATTENTION condition due to
  2805. * a cause we currently ignore, and we donnot want
  2806. * to be stuck with WIDE and/or SYNC data transfer.
  2807. *
  2808. * cp->nego_status is filled by sym_prepare_nego().
  2809. */
  2810. cp->nego_status = 0;
  2811. msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
  2812. /*
  2813. * Message table indirect structure.
  2814. */
  2815. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
  2816. cp->phys.smsg.size = cpu_to_scr(msglen);
  2817. /*
  2818. * sense command
  2819. */
  2820. cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
  2821. cp->phys.cmd.size = cpu_to_scr(6);
  2822. /*
  2823. * patch requested size into sense command
  2824. */
  2825. cp->sensecmd[0] = REQUEST_SENSE;
  2826. cp->sensecmd[1] = 0;
  2827. if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
  2828. cp->sensecmd[1] = cp->lun << 5;
  2829. cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
  2830. cp->data_len = SYM_SNS_BBUF_LEN;
  2831. /*
  2832. * sense data
  2833. */
  2834. memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
  2835. cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
  2836. cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
  2837. /*
  2838. * requeue the command.
  2839. */
  2840. startp = SCRIPTB_BA(np, sdata_in);
  2841. cp->phys.head.savep = cpu_to_scr(startp);
  2842. cp->phys.head.lastp = cpu_to_scr(startp);
  2843. cp->startp = cpu_to_scr(startp);
  2844. cp->goalp = cpu_to_scr(startp + 16);
  2845. cp->host_xflags = 0;
  2846. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  2847. cp->ssss_status = S_ILLEGAL;
  2848. cp->host_flags = (HF_SENSE|HF_DATA_IN);
  2849. cp->xerr_status = 0;
  2850. cp->extra_bytes = 0;
  2851. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  2852. /*
  2853. * Requeue the command.
  2854. */
  2855. sym_put_start_queue(np, cp);
  2856. /*
  2857. * Give back to upper layer everything we have dequeued.
  2858. */
  2859. sym_flush_comp_queue(np, 0);
  2860. break;
  2861. }
  2862. }
  2863. /*
  2864. * After a device has accepted some management message
  2865. * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
  2866. * a device signals a UNIT ATTENTION condition, some
  2867. * tasks are thrown away by the device. We are required
  2868. * to reflect that on our tasks list since the device
  2869. * will never complete these tasks.
  2870. *
  2871. * This function move from the BUSY queue to the COMP
  2872. * queue all disconnected CCBs for a given target that
  2873. * match the following criteria:
  2874. * - lun=-1 means any logical UNIT otherwise a given one.
  2875. * - task=-1 means any task, otherwise a given one.
  2876. */
  2877. int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
  2878. {
  2879. SYM_QUEHEAD qtmp, *qp;
  2880. int i = 0;
  2881. struct sym_ccb *cp;
  2882. /*
  2883. * Move the entire BUSY queue to our temporary queue.
  2884. */
  2885. sym_que_init(&qtmp);
  2886. sym_que_splice(&np->busy_ccbq, &qtmp);
  2887. sym_que_init(&np->busy_ccbq);
  2888. /*
  2889. * Put all CCBs that matches our criteria into
  2890. * the COMP queue and put back other ones into
  2891. * the BUSY queue.
  2892. */
  2893. while ((qp = sym_remque_head(&qtmp)) != 0) {
  2894. struct scsi_cmnd *cmd;
  2895. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2896. cmd = cp->cmd;
  2897. if (cp->host_status != HS_DISCONNECT ||
  2898. cp->target != target ||
  2899. (lun != -1 && cp->lun != lun) ||
  2900. (task != -1 &&
  2901. (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
  2902. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  2903. continue;
  2904. }
  2905. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2906. /* Preserve the software timeout condition */
  2907. if (sym_get_cam_status(cmd) != DID_TIME_OUT)
  2908. sym_set_cam_status(cmd, cam_status);
  2909. ++i;
  2910. #if 0
  2911. printf("XXXX TASK @%p CLEARED\n", cp);
  2912. #endif
  2913. }
  2914. return i;
  2915. }
  2916. /*
  2917. * chip handler for TASKS recovery
  2918. *
  2919. * We cannot safely abort a command, while the SCRIPTS
  2920. * processor is running, since we just would be in race
  2921. * with it.
  2922. *
  2923. * As long as we have tasks to abort, we keep the SEM
  2924. * bit set in the ISTAT. When this bit is set, the
  2925. * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
  2926. * each time it enters the scheduler.
  2927. *
  2928. * If we have to reset a target, clear tasks of a unit,
  2929. * or to perform the abort of a disconnected job, we
  2930. * restart the SCRIPTS for selecting the target. Once
  2931. * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
  2932. * If it loses arbitration, the SCRIPTS will interrupt again
  2933. * the next time it will enter its scheduler, and so on ...
  2934. *
  2935. * On SIR_TARGET_SELECTED, we scan for the more
  2936. * appropriate thing to do:
  2937. *
  2938. * - If nothing, we just sent a M_ABORT message to the
  2939. * target to get rid of the useless SCSI bus ownership.
  2940. * According to the specs, no tasks shall be affected.
  2941. * - If the target is to be reset, we send it a M_RESET
  2942. * message.
  2943. * - If a logical UNIT is to be cleared , we send the
  2944. * IDENTIFY(lun) + M_ABORT.
  2945. * - If an untagged task is to be aborted, we send the
  2946. * IDENTIFY(lun) + M_ABORT.
  2947. * - If a tagged task is to be aborted, we send the
  2948. * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
  2949. *
  2950. * Once our 'kiss of death' :) message has been accepted
  2951. * by the target, the SCRIPTS interrupts again
  2952. * (SIR_ABORT_SENT). On this interrupt, we complete
  2953. * all the CCBs that should have been aborted by the
  2954. * target according to our message.
  2955. */
  2956. static void sym_sir_task_recovery(struct sym_hcb *np, int num)
  2957. {
  2958. SYM_QUEHEAD *qp;
  2959. struct sym_ccb *cp;
  2960. struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
  2961. struct scsi_target *starget;
  2962. int target=-1, lun=-1, task;
  2963. int i, k;
  2964. switch(num) {
  2965. /*
  2966. * The SCRIPTS processor stopped before starting
  2967. * the next command in order to allow us to perform
  2968. * some task recovery.
  2969. */
  2970. case SIR_SCRIPT_STOPPED:
  2971. /*
  2972. * Do we have any target to reset or unit to clear ?
  2973. */
  2974. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  2975. tp = &np->target[i];
  2976. if (tp->to_reset ||
  2977. (tp->lun0p && tp->lun0p->to_clear)) {
  2978. target = i;
  2979. break;
  2980. }
  2981. if (!tp->lunmp)
  2982. continue;
  2983. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  2984. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  2985. target = i;
  2986. break;
  2987. }
  2988. }
  2989. if (target != -1)
  2990. break;
  2991. }
  2992. /*
  2993. * If not, walk the busy queue for any
  2994. * disconnected CCB to be aborted.
  2995. */
  2996. if (target == -1) {
  2997. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  2998. cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
  2999. if (cp->host_status != HS_DISCONNECT)
  3000. continue;
  3001. if (cp->to_abort) {
  3002. target = cp->target;
  3003. break;
  3004. }
  3005. }
  3006. }
  3007. /*
  3008. * If some target is to be selected,
  3009. * prepare and start the selection.
  3010. */
  3011. if (target != -1) {
  3012. tp = &np->target[target];
  3013. np->abrt_sel.sel_id = target;
  3014. np->abrt_sel.sel_scntl3 = tp->head.wval;
  3015. np->abrt_sel.sel_sxfer = tp->head.sval;
  3016. OUTL(np, nc_dsa, np->hcb_ba);
  3017. OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
  3018. return;
  3019. }
  3020. /*
  3021. * Now look for a CCB to abort that haven't started yet.
  3022. * Btw, the SCRIPTS processor is still stopped, so
  3023. * we are not in race.
  3024. */
  3025. i = 0;
  3026. cp = NULL;
  3027. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3028. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3029. if (cp->host_status != HS_BUSY &&
  3030. cp->host_status != HS_NEGOTIATE)
  3031. continue;
  3032. if (!cp->to_abort)
  3033. continue;
  3034. #ifdef SYM_CONF_IARB_SUPPORT
  3035. /*
  3036. * If we are using IMMEDIATE ARBITRATION, we donnot
  3037. * want to cancel the last queued CCB, since the
  3038. * SCRIPTS may have anticipated the selection.
  3039. */
  3040. if (cp == np->last_cp) {
  3041. cp->to_abort = 0;
  3042. continue;
  3043. }
  3044. #endif
  3045. i = 1; /* Means we have found some */
  3046. break;
  3047. }
  3048. if (!i) {
  3049. /*
  3050. * We are done, so we donnot need
  3051. * to synchronize with the SCRIPTS anylonger.
  3052. * Remove the SEM flag from the ISTAT.
  3053. */
  3054. np->istat_sem = 0;
  3055. OUTB(np, nc_istat, SIGP);
  3056. break;
  3057. }
  3058. /*
  3059. * Compute index of next position in the start
  3060. * queue the SCRIPTS intends to start and dequeue
  3061. * all CCBs for that device that haven't been started.
  3062. */
  3063. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3064. i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  3065. /*
  3066. * Make sure at least our IO to abort has been dequeued.
  3067. */
  3068. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  3069. assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
  3070. #else
  3071. sym_remque(&cp->link_ccbq);
  3072. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  3073. #endif
  3074. /*
  3075. * Keep track in cam status of the reason of the abort.
  3076. */
  3077. if (cp->to_abort == 2)
  3078. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3079. else
  3080. sym_set_cam_status(cp->cmd, DID_ABORT);
  3081. /*
  3082. * Complete with error everything that we have dequeued.
  3083. */
  3084. sym_flush_comp_queue(np, 0);
  3085. break;
  3086. /*
  3087. * The SCRIPTS processor has selected a target
  3088. * we may have some manual recovery to perform for.
  3089. */
  3090. case SIR_TARGET_SELECTED:
  3091. target = INB(np, nc_sdid) & 0xf;
  3092. tp = &np->target[target];
  3093. np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
  3094. /*
  3095. * If the target is to be reset, prepare a
  3096. * M_RESET message and clear the to_reset flag
  3097. * since we donnot expect this operation to fail.
  3098. */
  3099. if (tp->to_reset) {
  3100. np->abrt_msg[0] = M_RESET;
  3101. np->abrt_tbl.size = 1;
  3102. tp->to_reset = 0;
  3103. break;
  3104. }
  3105. /*
  3106. * Otherwise, look for some logical unit to be cleared.
  3107. */
  3108. if (tp->lun0p && tp->lun0p->to_clear)
  3109. lun = 0;
  3110. else if (tp->lunmp) {
  3111. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  3112. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  3113. lun = k;
  3114. break;
  3115. }
  3116. }
  3117. }
  3118. /*
  3119. * If a logical unit is to be cleared, prepare
  3120. * an IDENTIFY(lun) + ABORT MESSAGE.
  3121. */
  3122. if (lun != -1) {
  3123. struct sym_lcb *lp = sym_lp(tp, lun);
  3124. lp->to_clear = 0; /* We don't expect to fail here */
  3125. np->abrt_msg[0] = IDENTIFY(0, lun);
  3126. np->abrt_msg[1] = M_ABORT;
  3127. np->abrt_tbl.size = 2;
  3128. break;
  3129. }
  3130. /*
  3131. * Otherwise, look for some disconnected job to
  3132. * abort for this target.
  3133. */
  3134. i = 0;
  3135. cp = NULL;
  3136. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3137. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3138. if (cp->host_status != HS_DISCONNECT)
  3139. continue;
  3140. if (cp->target != target)
  3141. continue;
  3142. if (!cp->to_abort)
  3143. continue;
  3144. i = 1; /* Means we have some */
  3145. break;
  3146. }
  3147. /*
  3148. * If we have none, probably since the device has
  3149. * completed the command before we won abitration,
  3150. * send a M_ABORT message without IDENTIFY.
  3151. * According to the specs, the device must just
  3152. * disconnect the BUS and not abort any task.
  3153. */
  3154. if (!i) {
  3155. np->abrt_msg[0] = M_ABORT;
  3156. np->abrt_tbl.size = 1;
  3157. break;
  3158. }
  3159. /*
  3160. * We have some task to abort.
  3161. * Set the IDENTIFY(lun)
  3162. */
  3163. np->abrt_msg[0] = IDENTIFY(0, cp->lun);
  3164. /*
  3165. * If we want to abort an untagged command, we
  3166. * will send a IDENTIFY + M_ABORT.
  3167. * Otherwise (tagged command), we will send
  3168. * a IDENTITFY + task attributes + ABORT TAG.
  3169. */
  3170. if (cp->tag == NO_TAG) {
  3171. np->abrt_msg[1] = M_ABORT;
  3172. np->abrt_tbl.size = 2;
  3173. } else {
  3174. np->abrt_msg[1] = cp->scsi_smsg[1];
  3175. np->abrt_msg[2] = cp->scsi_smsg[2];
  3176. np->abrt_msg[3] = M_ABORT_TAG;
  3177. np->abrt_tbl.size = 4;
  3178. }
  3179. /*
  3180. * Keep track of software timeout condition, since the
  3181. * peripheral driver may not count retries on abort
  3182. * conditions not due to timeout.
  3183. */
  3184. if (cp->to_abort == 2)
  3185. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3186. cp->to_abort = 0; /* We donnot expect to fail here */
  3187. break;
  3188. /*
  3189. * The target has accepted our message and switched
  3190. * to BUS FREE phase as we expected.
  3191. */
  3192. case SIR_ABORT_SENT:
  3193. target = INB(np, nc_sdid) & 0xf;
  3194. tp = &np->target[target];
  3195. starget = tp->starget;
  3196. /*
  3197. ** If we didn't abort anything, leave here.
  3198. */
  3199. if (np->abrt_msg[0] == M_ABORT)
  3200. break;
  3201. /*
  3202. * If we sent a M_RESET, then a hardware reset has
  3203. * been performed by the target.
  3204. * - Reset everything to async 8 bit
  3205. * - Tell ourself to negotiate next time :-)
  3206. * - Prepare to clear all disconnected CCBs for
  3207. * this target from our task list (lun=task=-1)
  3208. */
  3209. lun = -1;
  3210. task = -1;
  3211. if (np->abrt_msg[0] == M_RESET) {
  3212. tp->head.sval = 0;
  3213. tp->head.wval = np->rv_scntl3;
  3214. tp->head.uval = 0;
  3215. spi_period(starget) = 0;
  3216. spi_offset(starget) = 0;
  3217. spi_width(starget) = 0;
  3218. spi_iu(starget) = 0;
  3219. spi_dt(starget) = 0;
  3220. spi_qas(starget) = 0;
  3221. tp->tgoal.check_nego = 1;
  3222. }
  3223. /*
  3224. * Otherwise, check for the LUN and TASK(s)
  3225. * concerned by the cancelation.
  3226. * If it is not ABORT_TAG then it is CLEAR_QUEUE
  3227. * or an ABORT message :-)
  3228. */
  3229. else {
  3230. lun = np->abrt_msg[0] & 0x3f;
  3231. if (np->abrt_msg[1] == M_ABORT_TAG)
  3232. task = np->abrt_msg[2];
  3233. }
  3234. /*
  3235. * Complete all the CCBs the device should have
  3236. * aborted due to our 'kiss of death' message.
  3237. */
  3238. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3239. sym_dequeue_from_squeue(np, i, target, lun, -1);
  3240. sym_clear_tasks(np, DID_ABORT, target, lun, task);
  3241. sym_flush_comp_queue(np, 0);
  3242. /*
  3243. * If we sent a BDR, make upper layer aware of that.
  3244. */
  3245. if (np->abrt_msg[0] == M_RESET)
  3246. sym_xpt_async_sent_bdr(np, target);
  3247. break;
  3248. }
  3249. /*
  3250. * Print to the log the message we intend to send.
  3251. */
  3252. if (num == SIR_TARGET_SELECTED) {
  3253. dev_info(&tp->starget->dev, "control msgout:");
  3254. sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
  3255. np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
  3256. }
  3257. /*
  3258. * Let the SCRIPTS processor continue.
  3259. */
  3260. OUTONB_STD();
  3261. }
  3262. /*
  3263. * Gerard's alchemy:) that deals with with the data
  3264. * pointer for both MDP and the residual calculation.
  3265. *
  3266. * I didn't want to bloat the code by more than 200
  3267. * lines for the handling of both MDP and the residual.
  3268. * This has been achieved by using a data pointer
  3269. * representation consisting in an index in the data
  3270. * array (dp_sg) and a negative offset (dp_ofs) that
  3271. * have the following meaning:
  3272. *
  3273. * - dp_sg = SYM_CONF_MAX_SG
  3274. * we are at the end of the data script.
  3275. * - dp_sg < SYM_CONF_MAX_SG
  3276. * dp_sg points to the next entry of the scatter array
  3277. * we want to transfer.
  3278. * - dp_ofs < 0
  3279. * dp_ofs represents the residual of bytes of the
  3280. * previous entry scatter entry we will send first.
  3281. * - dp_ofs = 0
  3282. * no residual to send first.
  3283. *
  3284. * The function sym_evaluate_dp() accepts an arbitray
  3285. * offset (basically from the MDP message) and returns
  3286. * the corresponding values of dp_sg and dp_ofs.
  3287. */
  3288. static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
  3289. {
  3290. u32 dp_scr;
  3291. int dp_ofs, dp_sg, dp_sgmin;
  3292. int tmp;
  3293. struct sym_pmc *pm;
  3294. /*
  3295. * Compute the resulted data pointer in term of a script
  3296. * address within some DATA script and a signed byte offset.
  3297. */
  3298. dp_scr = scr;
  3299. dp_ofs = *ofs;
  3300. if (dp_scr == SCRIPTA_BA(np, pm0_data))
  3301. pm = &cp->phys.pm0;
  3302. else if (dp_scr == SCRIPTA_BA(np, pm1_data))
  3303. pm = &cp->phys.pm1;
  3304. else
  3305. pm = NULL;
  3306. if (pm) {
  3307. dp_scr = scr_to_cpu(pm->ret);
  3308. dp_ofs -= scr_to_cpu(pm->sg.size);
  3309. }
  3310. /*
  3311. * If we are auto-sensing, then we are done.
  3312. */
  3313. if (cp->host_flags & HF_SENSE) {
  3314. *ofs = dp_ofs;
  3315. return 0;
  3316. }
  3317. /*
  3318. * Deduce the index of the sg entry.
  3319. * Keep track of the index of the first valid entry.
  3320. * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
  3321. * end of the data.
  3322. */
  3323. tmp = scr_to_cpu(sym_goalp(cp));
  3324. dp_sg = SYM_CONF_MAX_SG;
  3325. if (dp_scr != tmp)
  3326. dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
  3327. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3328. /*
  3329. * Move to the sg entry the data pointer belongs to.
  3330. *
  3331. * If we are inside the data area, we expect result to be:
  3332. *
  3333. * Either,
  3334. * dp_ofs = 0 and dp_sg is the index of the sg entry
  3335. * the data pointer belongs to (or the end of the data)
  3336. * Or,
  3337. * dp_ofs < 0 and dp_sg is the index of the sg entry
  3338. * the data pointer belongs to + 1.
  3339. */
  3340. if (dp_ofs < 0) {
  3341. int n;
  3342. while (dp_sg > dp_sgmin) {
  3343. --dp_sg;
  3344. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3345. n = dp_ofs + (tmp & 0xffffff);
  3346. if (n > 0) {
  3347. ++dp_sg;
  3348. break;
  3349. }
  3350. dp_ofs = n;
  3351. }
  3352. }
  3353. else if (dp_ofs > 0) {
  3354. while (dp_sg < SYM_CONF_MAX_SG) {
  3355. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3356. dp_ofs -= (tmp & 0xffffff);
  3357. ++dp_sg;
  3358. if (dp_ofs <= 0)
  3359. break;
  3360. }
  3361. }
  3362. /*
  3363. * Make sure the data pointer is inside the data area.
  3364. * If not, return some error.
  3365. */
  3366. if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
  3367. goto out_err;
  3368. else if (dp_sg > SYM_CONF_MAX_SG ||
  3369. (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
  3370. goto out_err;
  3371. /*
  3372. * Save the extreme pointer if needed.
  3373. */
  3374. if (dp_sg > cp->ext_sg ||
  3375. (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
  3376. cp->ext_sg = dp_sg;
  3377. cp->ext_ofs = dp_ofs;
  3378. }
  3379. /*
  3380. * Return data.
  3381. */
  3382. *ofs = dp_ofs;
  3383. return dp_sg;
  3384. out_err:
  3385. return -1;
  3386. }
  3387. /*
  3388. * chip handler for MODIFY DATA POINTER MESSAGE
  3389. *
  3390. * We also call this function on IGNORE WIDE RESIDUE
  3391. * messages that do not match a SWIDE full condition.
  3392. * Btw, we assume in that situation that such a message
  3393. * is equivalent to a MODIFY DATA POINTER (offset=-1).
  3394. */
  3395. static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
  3396. {
  3397. int dp_ofs = ofs;
  3398. u32 dp_scr = sym_get_script_dp (np, cp);
  3399. u32 dp_ret;
  3400. u32 tmp;
  3401. u_char hflags;
  3402. int dp_sg;
  3403. struct sym_pmc *pm;
  3404. /*
  3405. * Not supported for auto-sense.
  3406. */
  3407. if (cp->host_flags & HF_SENSE)
  3408. goto out_reject;
  3409. /*
  3410. * Apply our alchemy:) (see comments in sym_evaluate_dp()),
  3411. * to the resulted data pointer.
  3412. */
  3413. dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
  3414. if (dp_sg < 0)
  3415. goto out_reject;
  3416. /*
  3417. * And our alchemy:) allows to easily calculate the data
  3418. * script address we want to return for the next data phase.
  3419. */
  3420. dp_ret = cpu_to_scr(sym_goalp(cp));
  3421. dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
  3422. /*
  3423. * If offset / scatter entry is zero we donnot need
  3424. * a context for the new current data pointer.
  3425. */
  3426. if (dp_ofs == 0) {
  3427. dp_scr = dp_ret;
  3428. goto out_ok;
  3429. }
  3430. /*
  3431. * Get a context for the new current data pointer.
  3432. */
  3433. hflags = INB(np, HF_PRT);
  3434. if (hflags & HF_DP_SAVED)
  3435. hflags ^= HF_ACT_PM;
  3436. if (!(hflags & HF_ACT_PM)) {
  3437. pm = &cp->phys.pm0;
  3438. dp_scr = SCRIPTA_BA(np, pm0_data);
  3439. }
  3440. else {
  3441. pm = &cp->phys.pm1;
  3442. dp_scr = SCRIPTA_BA(np, pm1_data);
  3443. }
  3444. hflags &= ~(HF_DP_SAVED);
  3445. OUTB(np, HF_PRT, hflags);
  3446. /*
  3447. * Set up the new current data pointer.
  3448. * ofs < 0 there, and for the next data phase, we
  3449. * want to transfer part of the data of the sg entry
  3450. * corresponding to index dp_sg-1 prior to returning
  3451. * to the main data script.
  3452. */
  3453. pm->ret = cpu_to_scr(dp_ret);
  3454. tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
  3455. tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
  3456. pm->sg.addr = cpu_to_scr(tmp);
  3457. pm->sg.size = cpu_to_scr(-dp_ofs);
  3458. out_ok:
  3459. sym_set_script_dp (np, cp, dp_scr);
  3460. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3461. return;
  3462. out_reject:
  3463. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3464. }
  3465. /*
  3466. * chip calculation of the data residual.
  3467. *
  3468. * As I used to say, the requirement of data residual
  3469. * in SCSI is broken, useless and cannot be achieved
  3470. * without huge complexity.
  3471. * But most OSes and even the official CAM require it.
  3472. * When stupidity happens to be so widely spread inside
  3473. * a community, it gets hard to convince.
  3474. *
  3475. * Anyway, I don't care, since I am not going to use
  3476. * any software that considers this data residual as
  3477. * a relevant information. :)
  3478. */
  3479. int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
  3480. {
  3481. int dp_sg, dp_sgmin, resid = 0;
  3482. int dp_ofs = 0;
  3483. /*
  3484. * Check for some data lost or just thrown away.
  3485. * We are not required to be quite accurate in this
  3486. * situation. Btw, if we are odd for output and the
  3487. * device claims some more data, it may well happen
  3488. * than our residual be zero. :-)
  3489. */
  3490. if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
  3491. if (cp->xerr_status & XE_EXTRA_DATA)
  3492. resid -= cp->extra_bytes;
  3493. if (cp->xerr_status & XE_SODL_UNRUN)
  3494. ++resid;
  3495. if (cp->xerr_status & XE_SWIDE_OVRUN)
  3496. --resid;
  3497. }
  3498. /*
  3499. * If all data has been transferred,
  3500. * there is no residual.
  3501. */
  3502. if (cp->phys.head.lastp == sym_goalp(cp))
  3503. return resid;
  3504. /*
  3505. * If no data transfer occurs, or if the data
  3506. * pointer is weird, return full residual.
  3507. */
  3508. if (cp->startp == cp->phys.head.lastp ||
  3509. sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
  3510. &dp_ofs) < 0) {
  3511. return cp->data_len;
  3512. }
  3513. /*
  3514. * If we were auto-sensing, then we are done.
  3515. */
  3516. if (cp->host_flags & HF_SENSE) {
  3517. return -dp_ofs;
  3518. }
  3519. /*
  3520. * We are now full comfortable in the computation
  3521. * of the data residual (2's complement).
  3522. */
  3523. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3524. resid = -cp->ext_ofs;
  3525. for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
  3526. u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3527. resid += (tmp & 0xffffff);
  3528. }
  3529. resid -= cp->odd_byte_adjustment;
  3530. /*
  3531. * Hopefully, the result is not too wrong.
  3532. */
  3533. return resid;
  3534. }
  3535. /*
  3536. * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
  3537. *
  3538. * When we try to negotiate, we append the negotiation message
  3539. * to the identify and (maybe) simple tag message.
  3540. * The host status field is set to HS_NEGOTIATE to mark this
  3541. * situation.
  3542. *
  3543. * If the target doesn't answer this message immediately
  3544. * (as required by the standard), the SIR_NEGO_FAILED interrupt
  3545. * will be raised eventually.
  3546. * The handler removes the HS_NEGOTIATE status, and sets the
  3547. * negotiated value to the default (async / nowide).
  3548. *
  3549. * If we receive a matching answer immediately, we check it
  3550. * for validity, and set the values.
  3551. *
  3552. * If we receive a Reject message immediately, we assume the
  3553. * negotiation has failed, and fall back to standard values.
  3554. *
  3555. * If we receive a negotiation message while not in HS_NEGOTIATE
  3556. * state, it's a target initiated negotiation. We prepare a
  3557. * (hopefully) valid answer, set our parameters, and send back
  3558. * this answer to the target.
  3559. *
  3560. * If the target doesn't fetch the answer (no message out phase),
  3561. * we assume the negotiation has failed, and fall back to default
  3562. * settings (SIR_NEGO_PROTO interrupt).
  3563. *
  3564. * When we set the values, we adjust them in all ccbs belonging
  3565. * to this target, in the controller's register, and in the "phys"
  3566. * field of the controller's struct sym_hcb.
  3567. */
  3568. /*
  3569. * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
  3570. */
  3571. static int
  3572. sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3573. {
  3574. int target = cp->target;
  3575. u_char chg, ofs, per, fak, div;
  3576. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3577. sym_print_nego_msg(np, target, "sync msgin", np->msgin);
  3578. }
  3579. /*
  3580. * Get requested values.
  3581. */
  3582. chg = 0;
  3583. per = np->msgin[3];
  3584. ofs = np->msgin[4];
  3585. /*
  3586. * Check values against our limits.
  3587. */
  3588. if (ofs) {
  3589. if (ofs > np->maxoffs)
  3590. {chg = 1; ofs = np->maxoffs;}
  3591. }
  3592. if (ofs) {
  3593. if (per < np->minsync)
  3594. {chg = 1; per = np->minsync;}
  3595. }
  3596. /*
  3597. * Get new chip synchronous parameters value.
  3598. */
  3599. div = fak = 0;
  3600. if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
  3601. goto reject_it;
  3602. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3603. sym_print_addr(cp->cmd,
  3604. "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
  3605. ofs, per, div, fak, chg);
  3606. }
  3607. /*
  3608. * If it was an answer we want to change,
  3609. * then it isn't acceptable. Reject it.
  3610. */
  3611. if (!req && chg)
  3612. goto reject_it;
  3613. /*
  3614. * Apply new values.
  3615. */
  3616. sym_setsync (np, target, ofs, per, div, fak);
  3617. /*
  3618. * It was an answer. We are done.
  3619. */
  3620. if (!req)
  3621. return 0;
  3622. /*
  3623. * It was a request. Prepare an answer message.
  3624. */
  3625. np->msgout[0] = M_EXTENDED;
  3626. np->msgout[1] = 3;
  3627. np->msgout[2] = M_X_SYNC_REQ;
  3628. np->msgout[3] = per;
  3629. np->msgout[4] = ofs;
  3630. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3631. sym_print_nego_msg(np, target, "sync msgout", np->msgout);
  3632. }
  3633. np->msgin [0] = M_NOOP;
  3634. return 0;
  3635. reject_it:
  3636. sym_setsync (np, target, 0, 0, 0, 0);
  3637. return -1;
  3638. }
  3639. static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3640. {
  3641. int req = 1;
  3642. int result;
  3643. /*
  3644. * Request or answer ?
  3645. */
  3646. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3647. OUTB(np, HS_PRT, HS_BUSY);
  3648. if (cp->nego_status && cp->nego_status != NS_SYNC)
  3649. goto reject_it;
  3650. req = 0;
  3651. }
  3652. /*
  3653. * Check and apply new values.
  3654. */
  3655. result = sym_sync_nego_check(np, req, cp);
  3656. if (result) /* Not acceptable, reject it */
  3657. goto reject_it;
  3658. if (req) { /* Was a request, send response. */
  3659. cp->nego_status = NS_SYNC;
  3660. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3661. }
  3662. else /* Was a response, we are done. */
  3663. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3664. return;
  3665. reject_it:
  3666. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3667. }
  3668. /*
  3669. * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
  3670. */
  3671. static int
  3672. sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
  3673. {
  3674. struct sym_tcb *tp = &np->target[target];
  3675. unsigned char fak, div;
  3676. int dt, chg = 0;
  3677. unsigned char per = np->msgin[3];
  3678. unsigned char ofs = np->msgin[5];
  3679. unsigned char wide = np->msgin[6];
  3680. unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
  3681. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3682. sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
  3683. }
  3684. /*
  3685. * Check values against our limits.
  3686. */
  3687. if (wide > np->maxwide) {
  3688. chg = 1;
  3689. wide = np->maxwide;
  3690. }
  3691. if (!wide || !(np->features & FE_U3EN))
  3692. opts = 0;
  3693. if (opts != (np->msgin[7] & PPR_OPT_MASK))
  3694. chg = 1;
  3695. dt = opts & PPR_OPT_DT;
  3696. if (ofs) {
  3697. unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
  3698. if (ofs > maxoffs) {
  3699. chg = 1;
  3700. ofs = maxoffs;
  3701. }
  3702. }
  3703. if (ofs) {
  3704. unsigned char minsync = dt ? np->minsync_dt : np->minsync;
  3705. if (per < minsync) {
  3706. chg = 1;
  3707. per = minsync;
  3708. }
  3709. }
  3710. /*
  3711. * Get new chip synchronous parameters value.
  3712. */
  3713. div = fak = 0;
  3714. if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
  3715. goto reject_it;
  3716. /*
  3717. * If it was an answer we want to change,
  3718. * then it isn't acceptable. Reject it.
  3719. */
  3720. if (!req && chg)
  3721. goto reject_it;
  3722. /*
  3723. * Apply new values.
  3724. */
  3725. sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
  3726. /*
  3727. * It was an answer. We are done.
  3728. */
  3729. if (!req)
  3730. return 0;
  3731. /*
  3732. * It was a request. Prepare an answer message.
  3733. */
  3734. np->msgout[0] = M_EXTENDED;
  3735. np->msgout[1] = 6;
  3736. np->msgout[2] = M_X_PPR_REQ;
  3737. np->msgout[3] = per;
  3738. np->msgout[4] = 0;
  3739. np->msgout[5] = ofs;
  3740. np->msgout[6] = wide;
  3741. np->msgout[7] = opts;
  3742. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3743. sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
  3744. }
  3745. np->msgin [0] = M_NOOP;
  3746. return 0;
  3747. reject_it:
  3748. sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
  3749. /*
  3750. * If it is a device response that should result in
  3751. * ST, we may want to try a legacy negotiation later.
  3752. */
  3753. if (!req && !opts) {
  3754. tp->tgoal.period = per;
  3755. tp->tgoal.offset = ofs;
  3756. tp->tgoal.width = wide;
  3757. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3758. tp->tgoal.check_nego = 1;
  3759. }
  3760. return -1;
  3761. }
  3762. static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3763. {
  3764. int req = 1;
  3765. int result;
  3766. /*
  3767. * Request or answer ?
  3768. */
  3769. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3770. OUTB(np, HS_PRT, HS_BUSY);
  3771. if (cp->nego_status && cp->nego_status != NS_PPR)
  3772. goto reject_it;
  3773. req = 0;
  3774. }
  3775. /*
  3776. * Check and apply new values.
  3777. */
  3778. result = sym_ppr_nego_check(np, req, cp->target);
  3779. if (result) /* Not acceptable, reject it */
  3780. goto reject_it;
  3781. if (req) { /* Was a request, send response. */
  3782. cp->nego_status = NS_PPR;
  3783. OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
  3784. }
  3785. else /* Was a response, we are done. */
  3786. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3787. return;
  3788. reject_it:
  3789. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3790. }
  3791. /*
  3792. * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
  3793. */
  3794. static int
  3795. sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3796. {
  3797. int target = cp->target;
  3798. u_char chg, wide;
  3799. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3800. sym_print_nego_msg(np, target, "wide msgin", np->msgin);
  3801. }
  3802. /*
  3803. * Get requested values.
  3804. */
  3805. chg = 0;
  3806. wide = np->msgin[3];
  3807. /*
  3808. * Check values against our limits.
  3809. */
  3810. if (wide > np->maxwide) {
  3811. chg = 1;
  3812. wide = np->maxwide;
  3813. }
  3814. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3815. sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
  3816. wide, chg);
  3817. }
  3818. /*
  3819. * If it was an answer we want to change,
  3820. * then it isn't acceptable. Reject it.
  3821. */
  3822. if (!req && chg)
  3823. goto reject_it;
  3824. /*
  3825. * Apply new values.
  3826. */
  3827. sym_setwide (np, target, wide);
  3828. /*
  3829. * It was an answer. We are done.
  3830. */
  3831. if (!req)
  3832. return 0;
  3833. /*
  3834. * It was a request. Prepare an answer message.
  3835. */
  3836. np->msgout[0] = M_EXTENDED;
  3837. np->msgout[1] = 2;
  3838. np->msgout[2] = M_X_WIDE_REQ;
  3839. np->msgout[3] = wide;
  3840. np->msgin [0] = M_NOOP;
  3841. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3842. sym_print_nego_msg(np, target, "wide msgout", np->msgout);
  3843. }
  3844. return 0;
  3845. reject_it:
  3846. return -1;
  3847. }
  3848. static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3849. {
  3850. int req = 1;
  3851. int result;
  3852. /*
  3853. * Request or answer ?
  3854. */
  3855. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3856. OUTB(np, HS_PRT, HS_BUSY);
  3857. if (cp->nego_status && cp->nego_status != NS_WIDE)
  3858. goto reject_it;
  3859. req = 0;
  3860. }
  3861. /*
  3862. * Check and apply new values.
  3863. */
  3864. result = sym_wide_nego_check(np, req, cp);
  3865. if (result) /* Not acceptable, reject it */
  3866. goto reject_it;
  3867. if (req) { /* Was a request, send response. */
  3868. cp->nego_status = NS_WIDE;
  3869. OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
  3870. } else { /* Was a response. */
  3871. /*
  3872. * Negotiate for SYNC immediately after WIDE response.
  3873. * This allows to negotiate for both WIDE and SYNC on
  3874. * a single SCSI command (Suggested by Justin Gibbs).
  3875. */
  3876. if (tp->tgoal.offset) {
  3877. np->msgout[0] = M_EXTENDED;
  3878. np->msgout[1] = 3;
  3879. np->msgout[2] = M_X_SYNC_REQ;
  3880. np->msgout[3] = tp->tgoal.period;
  3881. np->msgout[4] = tp->tgoal.offset;
  3882. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3883. sym_print_nego_msg(np, cp->target,
  3884. "sync msgout", np->msgout);
  3885. }
  3886. cp->nego_status = NS_SYNC;
  3887. OUTB(np, HS_PRT, HS_NEGOTIATE);
  3888. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3889. return;
  3890. } else
  3891. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3892. }
  3893. return;
  3894. reject_it:
  3895. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3896. }
  3897. /*
  3898. * Reset DT, SYNC or WIDE to default settings.
  3899. *
  3900. * Called when a negotiation does not succeed either
  3901. * on rejection or on protocol error.
  3902. *
  3903. * A target that understands a PPR message should never
  3904. * reject it, and messing with it is very unlikely.
  3905. * So, if a PPR makes problems, we may just want to
  3906. * try a legacy negotiation later.
  3907. */
  3908. static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3909. {
  3910. switch (cp->nego_status) {
  3911. case NS_PPR:
  3912. #if 0
  3913. sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
  3914. #else
  3915. if (tp->tgoal.period < np->minsync)
  3916. tp->tgoal.period = np->minsync;
  3917. if (tp->tgoal.offset > np->maxoffs)
  3918. tp->tgoal.offset = np->maxoffs;
  3919. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3920. tp->tgoal.check_nego = 1;
  3921. #endif
  3922. break;
  3923. case NS_SYNC:
  3924. sym_setsync (np, cp->target, 0, 0, 0, 0);
  3925. break;
  3926. case NS_WIDE:
  3927. sym_setwide (np, cp->target, 0);
  3928. break;
  3929. }
  3930. np->msgin [0] = M_NOOP;
  3931. np->msgout[0] = M_NOOP;
  3932. cp->nego_status = 0;
  3933. }
  3934. /*
  3935. * chip handler for MESSAGE REJECT received in response to
  3936. * PPR, WIDE or SYNCHRONOUS negotiation.
  3937. */
  3938. static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3939. {
  3940. sym_nego_default(np, tp, cp);
  3941. OUTB(np, HS_PRT, HS_BUSY);
  3942. }
  3943. /*
  3944. * chip exception handler for programmed interrupts.
  3945. */
  3946. static void sym_int_sir (struct sym_hcb *np)
  3947. {
  3948. u_char num = INB(np, nc_dsps);
  3949. u32 dsa = INL(np, nc_dsa);
  3950. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  3951. u_char target = INB(np, nc_sdid) & 0x0f;
  3952. struct sym_tcb *tp = &np->target[target];
  3953. int tmp;
  3954. if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
  3955. switch (num) {
  3956. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  3957. /*
  3958. * SCRIPTS tell us that we may have to update
  3959. * 64 bit DMA segment registers.
  3960. */
  3961. case SIR_DMAP_DIRTY:
  3962. sym_update_dmap_regs(np);
  3963. goto out;
  3964. #endif
  3965. /*
  3966. * Command has been completed with error condition
  3967. * or has been auto-sensed.
  3968. */
  3969. case SIR_COMPLETE_ERROR:
  3970. sym_complete_error(np, cp);
  3971. return;
  3972. /*
  3973. * The C code is currently trying to recover from something.
  3974. * Typically, user want to abort some command.
  3975. */
  3976. case SIR_SCRIPT_STOPPED:
  3977. case SIR_TARGET_SELECTED:
  3978. case SIR_ABORT_SENT:
  3979. sym_sir_task_recovery(np, num);
  3980. return;
  3981. /*
  3982. * The device didn't go to MSG OUT phase after having
  3983. * been selected with ATN. We donnot want to handle
  3984. * that.
  3985. */
  3986. case SIR_SEL_ATN_NO_MSG_OUT:
  3987. printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
  3988. sym_name (np), target);
  3989. goto out_stuck;
  3990. /*
  3991. * The device didn't switch to MSG IN phase after
  3992. * having reseleted the initiator.
  3993. */
  3994. case SIR_RESEL_NO_MSG_IN:
  3995. printf ("%s:%d: No MSG IN phase after reselection.\n",
  3996. sym_name (np), target);
  3997. goto out_stuck;
  3998. /*
  3999. * After reselection, the device sent a message that wasn't
  4000. * an IDENTIFY.
  4001. */
  4002. case SIR_RESEL_NO_IDENTIFY:
  4003. printf ("%s:%d: No IDENTIFY after reselection.\n",
  4004. sym_name (np), target);
  4005. goto out_stuck;
  4006. /*
  4007. * The device reselected a LUN we donnot know about.
  4008. */
  4009. case SIR_RESEL_BAD_LUN:
  4010. np->msgout[0] = M_RESET;
  4011. goto out;
  4012. /*
  4013. * The device reselected for an untagged nexus and we
  4014. * haven't any.
  4015. */
  4016. case SIR_RESEL_BAD_I_T_L:
  4017. np->msgout[0] = M_ABORT;
  4018. goto out;
  4019. /*
  4020. * The device reselected for a tagged nexus that we donnot
  4021. * have.
  4022. */
  4023. case SIR_RESEL_BAD_I_T_L_Q:
  4024. np->msgout[0] = M_ABORT_TAG;
  4025. goto out;
  4026. /*
  4027. * The SCRIPTS let us know that the device has grabbed
  4028. * our message and will abort the job.
  4029. */
  4030. case SIR_RESEL_ABORTED:
  4031. np->lastmsg = np->msgout[0];
  4032. np->msgout[0] = M_NOOP;
  4033. printf ("%s:%d: message %x sent on bad reselection.\n",
  4034. sym_name (np), target, np->lastmsg);
  4035. goto out;
  4036. /*
  4037. * The SCRIPTS let us know that a message has been
  4038. * successfully sent to the device.
  4039. */
  4040. case SIR_MSG_OUT_DONE:
  4041. np->lastmsg = np->msgout[0];
  4042. np->msgout[0] = M_NOOP;
  4043. /* Should we really care of that */
  4044. if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
  4045. if (cp) {
  4046. cp->xerr_status &= ~XE_PARITY_ERR;
  4047. if (!cp->xerr_status)
  4048. OUTOFFB(np, HF_PRT, HF_EXT_ERR);
  4049. }
  4050. }
  4051. goto out;
  4052. /*
  4053. * The device didn't send a GOOD SCSI status.
  4054. * We may have some work to do prior to allow
  4055. * the SCRIPTS processor to continue.
  4056. */
  4057. case SIR_BAD_SCSI_STATUS:
  4058. if (!cp)
  4059. goto out;
  4060. sym_sir_bad_scsi_status(np, num, cp);
  4061. return;
  4062. /*
  4063. * We are asked by the SCRIPTS to prepare a
  4064. * REJECT message.
  4065. */
  4066. case SIR_REJECT_TO_SEND:
  4067. sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
  4068. np->msgout[0] = M_REJECT;
  4069. goto out;
  4070. /*
  4071. * We have been ODD at the end of a DATA IN
  4072. * transfer and the device didn't send a
  4073. * IGNORE WIDE RESIDUE message.
  4074. * It is a data overrun condition.
  4075. */
  4076. case SIR_SWIDE_OVERRUN:
  4077. if (cp) {
  4078. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4079. cp->xerr_status |= XE_SWIDE_OVRUN;
  4080. }
  4081. goto out;
  4082. /*
  4083. * We have been ODD at the end of a DATA OUT
  4084. * transfer.
  4085. * It is a data underrun condition.
  4086. */
  4087. case SIR_SODL_UNDERRUN:
  4088. if (cp) {
  4089. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4090. cp->xerr_status |= XE_SODL_UNRUN;
  4091. }
  4092. goto out;
  4093. /*
  4094. * The device wants us to tranfer more data than
  4095. * expected or in the wrong direction.
  4096. * The number of extra bytes is in scratcha.
  4097. * It is a data overrun condition.
  4098. */
  4099. case SIR_DATA_OVERRUN:
  4100. if (cp) {
  4101. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4102. cp->xerr_status |= XE_EXTRA_DATA;
  4103. cp->extra_bytes += INL(np, nc_scratcha);
  4104. }
  4105. goto out;
  4106. /*
  4107. * The device switched to an illegal phase (4/5).
  4108. */
  4109. case SIR_BAD_PHASE:
  4110. if (cp) {
  4111. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4112. cp->xerr_status |= XE_BAD_PHASE;
  4113. }
  4114. goto out;
  4115. /*
  4116. * We received a message.
  4117. */
  4118. case SIR_MSG_RECEIVED:
  4119. if (!cp)
  4120. goto out_stuck;
  4121. switch (np->msgin [0]) {
  4122. /*
  4123. * We received an extended message.
  4124. * We handle MODIFY DATA POINTER, SDTR, WDTR
  4125. * and reject all other extended messages.
  4126. */
  4127. case M_EXTENDED:
  4128. switch (np->msgin [2]) {
  4129. case M_X_MODIFY_DP:
  4130. if (DEBUG_FLAGS & DEBUG_POINTER)
  4131. sym_print_msg(cp,"modify DP",np->msgin);
  4132. tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
  4133. (np->msgin[5]<<8) + (np->msgin[6]);
  4134. sym_modify_dp(np, tp, cp, tmp);
  4135. return;
  4136. case M_X_SYNC_REQ:
  4137. sym_sync_nego(np, tp, cp);
  4138. return;
  4139. case M_X_PPR_REQ:
  4140. sym_ppr_nego(np, tp, cp);
  4141. return;
  4142. case M_X_WIDE_REQ:
  4143. sym_wide_nego(np, tp, cp);
  4144. return;
  4145. default:
  4146. goto out_reject;
  4147. }
  4148. break;
  4149. /*
  4150. * We received a 1/2 byte message not handled from SCRIPTS.
  4151. * We are only expecting MESSAGE REJECT and IGNORE WIDE
  4152. * RESIDUE messages that haven't been anticipated by
  4153. * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
  4154. * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
  4155. */
  4156. case M_IGN_RESIDUE:
  4157. if (DEBUG_FLAGS & DEBUG_POINTER)
  4158. sym_print_msg(cp,"ign wide residue", np->msgin);
  4159. if (cp->host_flags & HF_SENSE)
  4160. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4161. else
  4162. sym_modify_dp(np, tp, cp, -1);
  4163. return;
  4164. case M_REJECT:
  4165. if (INB(np, HS_PRT) == HS_NEGOTIATE)
  4166. sym_nego_rejected(np, tp, cp);
  4167. else {
  4168. sym_print_addr(cp->cmd,
  4169. "M_REJECT received (%x:%x).\n",
  4170. scr_to_cpu(np->lastmsg), np->msgout[0]);
  4171. }
  4172. goto out_clrack;
  4173. break;
  4174. default:
  4175. goto out_reject;
  4176. }
  4177. break;
  4178. /*
  4179. * We received an unknown message.
  4180. * Ignore all MSG IN phases and reject it.
  4181. */
  4182. case SIR_MSG_WEIRD:
  4183. sym_print_msg(cp, "WEIRD message received", np->msgin);
  4184. OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
  4185. return;
  4186. /*
  4187. * Negotiation failed.
  4188. * Target does not send us the reply.
  4189. * Remove the HS_NEGOTIATE status.
  4190. */
  4191. case SIR_NEGO_FAILED:
  4192. OUTB(np, HS_PRT, HS_BUSY);
  4193. /*
  4194. * Negotiation failed.
  4195. * Target does not want answer message.
  4196. */
  4197. case SIR_NEGO_PROTO:
  4198. sym_nego_default(np, tp, cp);
  4199. goto out;
  4200. }
  4201. out:
  4202. OUTONB_STD();
  4203. return;
  4204. out_reject:
  4205. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  4206. return;
  4207. out_clrack:
  4208. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4209. return;
  4210. out_stuck:
  4211. return;
  4212. }
  4213. /*
  4214. * Acquire a control block
  4215. */
  4216. struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
  4217. {
  4218. u_char tn = cmd->device->id;
  4219. u_char ln = cmd->device->lun;
  4220. struct sym_tcb *tp = &np->target[tn];
  4221. struct sym_lcb *lp = sym_lp(tp, ln);
  4222. u_short tag = NO_TAG;
  4223. SYM_QUEHEAD *qp;
  4224. struct sym_ccb *cp = NULL;
  4225. /*
  4226. * Look for a free CCB
  4227. */
  4228. if (sym_que_empty(&np->free_ccbq))
  4229. sym_alloc_ccb(np);
  4230. qp = sym_remque_head(&np->free_ccbq);
  4231. if (!qp)
  4232. goto out;
  4233. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4234. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4235. /*
  4236. * If the LCB is not yet available and the LUN
  4237. * has been probed ok, try to allocate the LCB.
  4238. */
  4239. if (!lp && sym_is_bit(tp->lun_map, ln)) {
  4240. lp = sym_alloc_lcb(np, tn, ln);
  4241. if (!lp)
  4242. goto out_free;
  4243. }
  4244. #endif
  4245. /*
  4246. * If the LCB is not available here, then the
  4247. * logical unit is not yet discovered. For those
  4248. * ones only accept 1 SCSI IO per logical unit,
  4249. * since we cannot allow disconnections.
  4250. */
  4251. if (!lp) {
  4252. if (!sym_is_bit(tp->busy0_map, ln))
  4253. sym_set_bit(tp->busy0_map, ln);
  4254. else
  4255. goto out_free;
  4256. } else {
  4257. /*
  4258. * If we have been asked for a tagged command.
  4259. */
  4260. if (tag_order) {
  4261. /*
  4262. * Debugging purpose.
  4263. */
  4264. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4265. assert(lp->busy_itl == 0);
  4266. #endif
  4267. /*
  4268. * Allocate resources for tags if not yet.
  4269. */
  4270. if (!lp->cb_tags) {
  4271. sym_alloc_lcb_tags(np, tn, ln);
  4272. if (!lp->cb_tags)
  4273. goto out_free;
  4274. }
  4275. /*
  4276. * Get a tag for this SCSI IO and set up
  4277. * the CCB bus address for reselection,
  4278. * and count it for this LUN.
  4279. * Toggle reselect path to tagged.
  4280. */
  4281. if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
  4282. tag = lp->cb_tags[lp->ia_tag];
  4283. if (++lp->ia_tag == SYM_CONF_MAX_TASK)
  4284. lp->ia_tag = 0;
  4285. ++lp->busy_itlq;
  4286. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4287. lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
  4288. lp->head.resel_sa =
  4289. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  4290. #endif
  4291. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4292. cp->tags_si = lp->tags_si;
  4293. ++lp->tags_sum[cp->tags_si];
  4294. ++lp->tags_since;
  4295. #endif
  4296. }
  4297. else
  4298. goto out_free;
  4299. }
  4300. /*
  4301. * This command will not be tagged.
  4302. * If we already have either a tagged or untagged
  4303. * one, refuse to overlap this untagged one.
  4304. */
  4305. else {
  4306. /*
  4307. * Debugging purpose.
  4308. */
  4309. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4310. assert(lp->busy_itl == 0 && lp->busy_itlq == 0);
  4311. #endif
  4312. /*
  4313. * Count this nexus for this LUN.
  4314. * Set up the CCB bus address for reselection.
  4315. * Toggle reselect path to untagged.
  4316. */
  4317. ++lp->busy_itl;
  4318. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4319. if (lp->busy_itl == 1) {
  4320. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  4321. lp->head.resel_sa =
  4322. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  4323. }
  4324. else
  4325. goto out_free;
  4326. #endif
  4327. }
  4328. }
  4329. /*
  4330. * Put the CCB into the busy queue.
  4331. */
  4332. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  4333. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4334. if (lp) {
  4335. sym_remque(&cp->link2_ccbq);
  4336. sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
  4337. }
  4338. #endif
  4339. cp->to_abort = 0;
  4340. cp->odd_byte_adjustment = 0;
  4341. cp->tag = tag;
  4342. cp->order = tag_order;
  4343. cp->target = tn;
  4344. cp->lun = ln;
  4345. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4346. sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
  4347. }
  4348. out:
  4349. return cp;
  4350. out_free:
  4351. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4352. return NULL;
  4353. }
  4354. /*
  4355. * Release one control block
  4356. */
  4357. void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
  4358. {
  4359. struct sym_tcb *tp = &np->target[cp->target];
  4360. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  4361. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4362. sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
  4363. cp, cp->tag);
  4364. }
  4365. /*
  4366. * If LCB available,
  4367. */
  4368. if (lp) {
  4369. /*
  4370. * If tagged, release the tag, set the relect path
  4371. */
  4372. if (cp->tag != NO_TAG) {
  4373. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4374. --lp->tags_sum[cp->tags_si];
  4375. #endif
  4376. /*
  4377. * Free the tag value.
  4378. */
  4379. lp->cb_tags[lp->if_tag] = cp->tag;
  4380. if (++lp->if_tag == SYM_CONF_MAX_TASK)
  4381. lp->if_tag = 0;
  4382. /*
  4383. * Make the reselect path invalid,
  4384. * and uncount this CCB.
  4385. */
  4386. lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
  4387. --lp->busy_itlq;
  4388. } else { /* Untagged */
  4389. /*
  4390. * Make the reselect path invalid,
  4391. * and uncount this CCB.
  4392. */
  4393. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4394. --lp->busy_itl;
  4395. }
  4396. /*
  4397. * If no JOB active, make the LUN reselect path invalid.
  4398. */
  4399. if (lp->busy_itlq == 0 && lp->busy_itl == 0)
  4400. lp->head.resel_sa =
  4401. cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4402. }
  4403. /*
  4404. * Otherwise, we only accept 1 IO per LUN.
  4405. * Clear the bit that keeps track of this IO.
  4406. */
  4407. else
  4408. sym_clr_bit(tp->busy0_map, cp->lun);
  4409. /*
  4410. * We donnot queue more than 1 ccb per target
  4411. * with negotiation at any time. If this ccb was
  4412. * used for negotiation, clear this info in the tcb.
  4413. */
  4414. if (cp == tp->nego_cp)
  4415. tp->nego_cp = NULL;
  4416. #ifdef SYM_CONF_IARB_SUPPORT
  4417. /*
  4418. * If we just complete the last queued CCB,
  4419. * clear this info that is no longer relevant.
  4420. */
  4421. if (cp == np->last_cp)
  4422. np->last_cp = 0;
  4423. #endif
  4424. /*
  4425. * Make this CCB available.
  4426. */
  4427. cp->cmd = NULL;
  4428. cp->host_status = HS_IDLE;
  4429. sym_remque(&cp->link_ccbq);
  4430. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4431. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4432. if (lp) {
  4433. sym_remque(&cp->link2_ccbq);
  4434. sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
  4435. if (cp->started) {
  4436. if (cp->tag != NO_TAG)
  4437. --lp->started_tags;
  4438. else
  4439. --lp->started_no_tag;
  4440. }
  4441. }
  4442. cp->started = 0;
  4443. #endif
  4444. }
  4445. /*
  4446. * Allocate a CCB from memory and initialize its fixed part.
  4447. */
  4448. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
  4449. {
  4450. struct sym_ccb *cp = NULL;
  4451. int hcode;
  4452. /*
  4453. * Prevent from allocating more CCBs than we can
  4454. * queue to the controller.
  4455. */
  4456. if (np->actccbs >= SYM_CONF_MAX_START)
  4457. return NULL;
  4458. /*
  4459. * Allocate memory for this CCB.
  4460. */
  4461. cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
  4462. if (!cp)
  4463. goto out_free;
  4464. /*
  4465. * Count it.
  4466. */
  4467. np->actccbs++;
  4468. /*
  4469. * Compute the bus address of this ccb.
  4470. */
  4471. cp->ccb_ba = vtobus(cp);
  4472. /*
  4473. * Insert this ccb into the hashed list.
  4474. */
  4475. hcode = CCB_HASH_CODE(cp->ccb_ba);
  4476. cp->link_ccbh = np->ccbh[hcode];
  4477. np->ccbh[hcode] = cp;
  4478. /*
  4479. * Initialyze the start and restart actions.
  4480. */
  4481. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  4482. cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  4483. /*
  4484. * Initilialyze some other fields.
  4485. */
  4486. cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
  4487. /*
  4488. * Chain into free ccb queue.
  4489. */
  4490. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4491. /*
  4492. * Chain into optionnal lists.
  4493. */
  4494. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4495. sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
  4496. #endif
  4497. return cp;
  4498. out_free:
  4499. if (cp)
  4500. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  4501. return NULL;
  4502. }
  4503. /*
  4504. * Look up a CCB from a DSA value.
  4505. */
  4506. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
  4507. {
  4508. int hcode;
  4509. struct sym_ccb *cp;
  4510. hcode = CCB_HASH_CODE(dsa);
  4511. cp = np->ccbh[hcode];
  4512. while (cp) {
  4513. if (cp->ccb_ba == dsa)
  4514. break;
  4515. cp = cp->link_ccbh;
  4516. }
  4517. return cp;
  4518. }
  4519. /*
  4520. * Target control block initialisation.
  4521. * Nothing important to do at the moment.
  4522. */
  4523. static void sym_init_tcb (struct sym_hcb *np, u_char tn)
  4524. {
  4525. #if 0 /* Hmmm... this checking looks paranoid. */
  4526. /*
  4527. * Check some alignments required by the chip.
  4528. */
  4529. assert (((offsetof(struct sym_reg, nc_sxfer) ^
  4530. offsetof(struct sym_tcb, head.sval)) &3) == 0);
  4531. assert (((offsetof(struct sym_reg, nc_scntl3) ^
  4532. offsetof(struct sym_tcb, head.wval)) &3) == 0);
  4533. #endif
  4534. }
  4535. /*
  4536. * Lun control block allocation and initialization.
  4537. */
  4538. struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
  4539. {
  4540. struct sym_tcb *tp = &np->target[tn];
  4541. struct sym_lcb *lp = sym_lp(tp, ln);
  4542. /*
  4543. * Already done, just return.
  4544. */
  4545. if (lp)
  4546. return lp;
  4547. /*
  4548. * Donnot allow LUN control block
  4549. * allocation for not probed LUNs.
  4550. */
  4551. if (!sym_is_bit(tp->lun_map, ln))
  4552. return NULL;
  4553. /*
  4554. * Initialize the target control block if not yet.
  4555. */
  4556. sym_init_tcb (np, tn);
  4557. /*
  4558. * Allocate the LCB bus address array.
  4559. * Compute the bus address of this table.
  4560. */
  4561. if (ln && !tp->luntbl) {
  4562. int i;
  4563. tp->luntbl = sym_calloc_dma(256, "LUNTBL");
  4564. if (!tp->luntbl)
  4565. goto fail;
  4566. for (i = 0 ; i < 64 ; i++)
  4567. tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  4568. tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
  4569. }
  4570. /*
  4571. * Allocate the table of pointers for LUN(s) > 0, if needed.
  4572. */
  4573. if (ln && !tp->lunmp) {
  4574. tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
  4575. GFP_KERNEL);
  4576. if (!tp->lunmp)
  4577. goto fail;
  4578. }
  4579. /*
  4580. * Allocate the lcb.
  4581. * Make it available to the chip.
  4582. */
  4583. lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
  4584. if (!lp)
  4585. goto fail;
  4586. if (ln) {
  4587. tp->lunmp[ln] = lp;
  4588. tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
  4589. }
  4590. else {
  4591. tp->lun0p = lp;
  4592. tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
  4593. }
  4594. /*
  4595. * Let the itl task point to error handling.
  4596. */
  4597. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4598. /*
  4599. * Set the reselect pattern to our default. :)
  4600. */
  4601. lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4602. /*
  4603. * Set user capabilities.
  4604. */
  4605. lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  4606. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4607. /*
  4608. * Initialize device queueing.
  4609. */
  4610. sym_que_init(&lp->waiting_ccbq);
  4611. sym_que_init(&lp->started_ccbq);
  4612. lp->started_max = SYM_CONF_MAX_TASK;
  4613. lp->started_limit = SYM_CONF_MAX_TASK;
  4614. #endif
  4615. /*
  4616. * If we are busy, count the IO.
  4617. */
  4618. if (sym_is_bit(tp->busy0_map, ln)) {
  4619. lp->busy_itl = 1;
  4620. sym_clr_bit(tp->busy0_map, ln);
  4621. }
  4622. fail:
  4623. return lp;
  4624. }
  4625. /*
  4626. * Allocate LCB resources for tagged command queuing.
  4627. */
  4628. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
  4629. {
  4630. struct sym_tcb *tp = &np->target[tn];
  4631. struct sym_lcb *lp = sym_lp(tp, ln);
  4632. int i;
  4633. /*
  4634. * If LCB not available, try to allocate it.
  4635. */
  4636. if (!lp && !(lp = sym_alloc_lcb(np, tn, ln)))
  4637. goto fail;
  4638. /*
  4639. * Allocate the task table and and the tag allocation
  4640. * circular buffer. We want both or none.
  4641. */
  4642. lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4643. if (!lp->itlq_tbl)
  4644. goto fail;
  4645. lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
  4646. if (!lp->cb_tags) {
  4647. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4648. lp->itlq_tbl = NULL;
  4649. goto fail;
  4650. }
  4651. /*
  4652. * Initialize the task table with invalid entries.
  4653. */
  4654. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4655. lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
  4656. /*
  4657. * Fill up the tag buffer with tag numbers.
  4658. */
  4659. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4660. lp->cb_tags[i] = i;
  4661. /*
  4662. * Make the task table available to SCRIPTS,
  4663. * And accept tagged commands now.
  4664. */
  4665. lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
  4666. return;
  4667. fail:
  4668. return;
  4669. }
  4670. /*
  4671. * Queue a SCSI IO to the controller.
  4672. */
  4673. int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  4674. {
  4675. struct scsi_device *sdev = cmd->device;
  4676. struct sym_tcb *tp;
  4677. struct sym_lcb *lp;
  4678. u_char *msgptr;
  4679. u_int msglen;
  4680. int can_disconnect;
  4681. /*
  4682. * Keep track of the IO in our CCB.
  4683. */
  4684. cp->cmd = cmd;
  4685. /*
  4686. * Retrieve the target descriptor.
  4687. */
  4688. tp = &np->target[cp->target];
  4689. /*
  4690. * Retrieve the lun descriptor.
  4691. */
  4692. lp = sym_lp(tp, sdev->lun);
  4693. can_disconnect = (cp->tag != NO_TAG) ||
  4694. (lp && (lp->curr_flags & SYM_DISC_ENABLED));
  4695. msgptr = cp->scsi_smsg;
  4696. msglen = 0;
  4697. msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
  4698. /*
  4699. * Build the tag message if present.
  4700. */
  4701. if (cp->tag != NO_TAG) {
  4702. u_char order = cp->order;
  4703. switch(order) {
  4704. case M_ORDERED_TAG:
  4705. break;
  4706. case M_HEAD_TAG:
  4707. break;
  4708. default:
  4709. order = M_SIMPLE_TAG;
  4710. }
  4711. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4712. /*
  4713. * Avoid too much reordering of SCSI commands.
  4714. * The algorithm tries to prevent completion of any
  4715. * tagged command from being delayed against more
  4716. * than 3 times the max number of queued commands.
  4717. */
  4718. if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
  4719. lp->tags_si = !(lp->tags_si);
  4720. if (lp->tags_sum[lp->tags_si]) {
  4721. order = M_ORDERED_TAG;
  4722. if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
  4723. sym_print_addr(cmd,
  4724. "ordered tag forced.\n");
  4725. }
  4726. }
  4727. lp->tags_since = 0;
  4728. }
  4729. #endif
  4730. msgptr[msglen++] = order;
  4731. /*
  4732. * For less than 128 tags, actual tags are numbered
  4733. * 1,3,5,..2*MAXTAGS+1,since we may have to deal
  4734. * with devices that have problems with #TAG 0 or too
  4735. * great #TAG numbers. For more tags (up to 256),
  4736. * we use directly our tag number.
  4737. */
  4738. #if SYM_CONF_MAX_TASK > (512/4)
  4739. msgptr[msglen++] = cp->tag;
  4740. #else
  4741. msgptr[msglen++] = (cp->tag << 1) + 1;
  4742. #endif
  4743. }
  4744. /*
  4745. * Build a negotiation message if needed.
  4746. * (nego_status is filled by sym_prepare_nego())
  4747. */
  4748. cp->nego_status = 0;
  4749. if (tp->tgoal.check_nego && !tp->nego_cp && lp) {
  4750. msglen += sym_prepare_nego(np, cp, msgptr + msglen);
  4751. }
  4752. /*
  4753. * Startqueue
  4754. */
  4755. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  4756. cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
  4757. /*
  4758. * select
  4759. */
  4760. cp->phys.select.sel_id = cp->target;
  4761. cp->phys.select.sel_scntl3 = tp->head.wval;
  4762. cp->phys.select.sel_sxfer = tp->head.sval;
  4763. cp->phys.select.sel_scntl4 = tp->head.uval;
  4764. /*
  4765. * message
  4766. */
  4767. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
  4768. cp->phys.smsg.size = cpu_to_scr(msglen);
  4769. /*
  4770. * status
  4771. */
  4772. cp->host_xflags = 0;
  4773. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  4774. cp->ssss_status = S_ILLEGAL;
  4775. cp->xerr_status = 0;
  4776. cp->host_flags = 0;
  4777. cp->extra_bytes = 0;
  4778. /*
  4779. * extreme data pointer.
  4780. * shall be positive, so -1 is lower than lowest.:)
  4781. */
  4782. cp->ext_sg = -1;
  4783. cp->ext_ofs = 0;
  4784. /*
  4785. * Build the CDB and DATA descriptor block
  4786. * and start the IO.
  4787. */
  4788. return sym_setup_data_and_start(np, cmd, cp);
  4789. }
  4790. /*
  4791. * Reset a SCSI target (all LUNs of this target).
  4792. */
  4793. int sym_reset_scsi_target(struct sym_hcb *np, int target)
  4794. {
  4795. struct sym_tcb *tp;
  4796. if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
  4797. return -1;
  4798. tp = &np->target[target];
  4799. tp->to_reset = 1;
  4800. np->istat_sem = SEM;
  4801. OUTB(np, nc_istat, SIGP|SEM);
  4802. return 0;
  4803. }
  4804. /*
  4805. * Abort a SCSI IO.
  4806. */
  4807. static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
  4808. {
  4809. /*
  4810. * Check that the IO is active.
  4811. */
  4812. if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
  4813. return -1;
  4814. /*
  4815. * If a previous abort didn't succeed in time,
  4816. * perform a BUS reset.
  4817. */
  4818. if (cp->to_abort) {
  4819. sym_reset_scsi_bus(np, 1);
  4820. return 0;
  4821. }
  4822. /*
  4823. * Mark the CCB for abort and allow time for.
  4824. */
  4825. cp->to_abort = timed_out ? 2 : 1;
  4826. /*
  4827. * Tell the SCRIPTS processor to stop and synchronize with us.
  4828. */
  4829. np->istat_sem = SEM;
  4830. OUTB(np, nc_istat, SIGP|SEM);
  4831. return 0;
  4832. }
  4833. int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
  4834. {
  4835. struct sym_ccb *cp;
  4836. SYM_QUEHEAD *qp;
  4837. /*
  4838. * Look up our CCB control block.
  4839. */
  4840. cp = NULL;
  4841. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  4842. struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4843. if (cp2->cmd == cmd) {
  4844. cp = cp2;
  4845. break;
  4846. }
  4847. }
  4848. return sym_abort_ccb(np, cp, timed_out);
  4849. }
  4850. /*
  4851. * Complete execution of a SCSI command with extended
  4852. * error, SCSI status error, or having been auto-sensed.
  4853. *
  4854. * The SCRIPTS processor is not running there, so we
  4855. * can safely access IO registers and remove JOBs from
  4856. * the START queue.
  4857. * SCRATCHA is assumed to have been loaded with STARTPOS
  4858. * before the SCRIPTS called the C code.
  4859. */
  4860. void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
  4861. {
  4862. struct scsi_device *sdev;
  4863. struct scsi_cmnd *cmd;
  4864. struct sym_tcb *tp;
  4865. struct sym_lcb *lp;
  4866. int resid;
  4867. int i;
  4868. /*
  4869. * Paranoid check. :)
  4870. */
  4871. if (!cp || !cp->cmd)
  4872. return;
  4873. cmd = cp->cmd;
  4874. sdev = cmd->device;
  4875. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
  4876. dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
  4877. cp->host_status, cp->ssss_status, cp->host_flags);
  4878. }
  4879. /*
  4880. * Get target and lun pointers.
  4881. */
  4882. tp = &np->target[cp->target];
  4883. lp = sym_lp(tp, sdev->lun);
  4884. /*
  4885. * Check for extended errors.
  4886. */
  4887. if (cp->xerr_status) {
  4888. if (sym_verbose)
  4889. sym_print_xerr(cmd, cp->xerr_status);
  4890. if (cp->host_status == HS_COMPLETE)
  4891. cp->host_status = HS_COMP_ERR;
  4892. }
  4893. /*
  4894. * Calculate the residual.
  4895. */
  4896. resid = sym_compute_residual(np, cp);
  4897. if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
  4898. resid = 0; /* throw them away. :) */
  4899. cp->sv_resid = 0;
  4900. }
  4901. #ifdef DEBUG_2_0_X
  4902. if (resid)
  4903. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4904. #endif
  4905. /*
  4906. * Dequeue all queued CCBs for that device
  4907. * not yet started by SCRIPTS.
  4908. */
  4909. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  4910. i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
  4911. /*
  4912. * Restart the SCRIPTS processor.
  4913. */
  4914. OUTL_DSP(np, SCRIPTA_BA(np, start));
  4915. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4916. if (cp->host_status == HS_COMPLETE &&
  4917. cp->ssss_status == S_QUEUE_FULL) {
  4918. if (!lp || lp->started_tags - i < 2)
  4919. goto weirdness;
  4920. /*
  4921. * Decrease queue depth as needed.
  4922. */
  4923. lp->started_max = lp->started_tags - i - 1;
  4924. lp->num_sgood = 0;
  4925. if (sym_verbose >= 2) {
  4926. sym_print_addr(cmd, " queue depth is now %d\n",
  4927. lp->started_max);
  4928. }
  4929. /*
  4930. * Repair the CCB.
  4931. */
  4932. cp->host_status = HS_BUSY;
  4933. cp->ssss_status = S_ILLEGAL;
  4934. /*
  4935. * Let's requeue it to device.
  4936. */
  4937. sym_set_cam_status(cmd, DID_SOFT_ERROR);
  4938. goto finish;
  4939. }
  4940. weirdness:
  4941. #endif
  4942. /*
  4943. * Build result in CAM ccb.
  4944. */
  4945. sym_set_cam_result_error(np, cp, resid);
  4946. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4947. finish:
  4948. #endif
  4949. /*
  4950. * Add this one to the COMP queue.
  4951. */
  4952. sym_remque(&cp->link_ccbq);
  4953. sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
  4954. /*
  4955. * Complete all those commands with either error
  4956. * or requeue condition.
  4957. */
  4958. sym_flush_comp_queue(np, 0);
  4959. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4960. /*
  4961. * Donnot start more than 1 command after an error.
  4962. */
  4963. if (lp)
  4964. sym_start_next_ccbs(np, lp, 1);
  4965. #endif
  4966. }
  4967. /*
  4968. * Complete execution of a successful SCSI command.
  4969. *
  4970. * Only successful commands go to the DONE queue,
  4971. * since we need to have the SCRIPTS processor
  4972. * stopped on any error condition.
  4973. * The SCRIPTS processor is running while we are
  4974. * completing successful commands.
  4975. */
  4976. void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
  4977. {
  4978. struct sym_tcb *tp;
  4979. struct sym_lcb *lp;
  4980. struct scsi_cmnd *cmd;
  4981. int resid;
  4982. /*
  4983. * Paranoid check. :)
  4984. */
  4985. if (!cp || !cp->cmd)
  4986. return;
  4987. assert (cp->host_status == HS_COMPLETE);
  4988. /*
  4989. * Get user command.
  4990. */
  4991. cmd = cp->cmd;
  4992. /*
  4993. * Get target and lun pointers.
  4994. */
  4995. tp = &np->target[cp->target];
  4996. lp = sym_lp(tp, cp->lun);
  4997. /*
  4998. * Assume device discovered on first success.
  4999. */
  5000. if (!lp)
  5001. sym_set_bit(tp->lun_map, cp->lun);
  5002. /*
  5003. * If all data have been transferred, given than no
  5004. * extended error did occur, there is no residual.
  5005. */
  5006. resid = 0;
  5007. if (cp->phys.head.lastp != sym_goalp(cp))
  5008. resid = sym_compute_residual(np, cp);
  5009. /*
  5010. * Wrong transfer residuals may be worse than just always
  5011. * returning zero. User can disable this feature in
  5012. * sym53c8xx.h. Residual support is enabled by default.
  5013. */
  5014. if (!SYM_SETUP_RESIDUAL_SUPPORT)
  5015. resid = 0;
  5016. #ifdef DEBUG_2_0_X
  5017. if (resid)
  5018. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  5019. #endif
  5020. /*
  5021. * Build result in CAM ccb.
  5022. */
  5023. sym_set_cam_result_ok(cp, cmd, resid);
  5024. #ifdef SYM_OPT_SNIFF_INQUIRY
  5025. /*
  5026. * On standard INQUIRY response (EVPD and CmDt
  5027. * not set), sniff out device capabilities.
  5028. */
  5029. if (cp->cdb_buf[0] == INQUIRY && !(cp->cdb_buf[1] & 0x3))
  5030. sym_sniff_inquiry(np, cmd, resid);
  5031. #endif
  5032. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5033. /*
  5034. * If max number of started ccbs had been reduced,
  5035. * increase it if 200 good status received.
  5036. */
  5037. if (lp && lp->started_max < lp->started_limit) {
  5038. ++lp->num_sgood;
  5039. if (lp->num_sgood >= 200) {
  5040. lp->num_sgood = 0;
  5041. ++lp->started_max;
  5042. if (sym_verbose >= 2) {
  5043. sym_print_addr(cmd, " queue depth is now %d\n",
  5044. lp->started_max);
  5045. }
  5046. }
  5047. }
  5048. #endif
  5049. /*
  5050. * Free our CCB.
  5051. */
  5052. sym_free_ccb (np, cp);
  5053. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5054. /*
  5055. * Requeue a couple of awaiting scsi commands.
  5056. */
  5057. if (lp && !sym_que_empty(&lp->waiting_ccbq))
  5058. sym_start_next_ccbs(np, lp, 2);
  5059. #endif
  5060. /*
  5061. * Complete the command.
  5062. */
  5063. sym_xpt_done(np, cmd);
  5064. }
  5065. /*
  5066. * Soft-attach the controller.
  5067. */
  5068. int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
  5069. {
  5070. struct sym_hcb *np = sym_get_hcb(shost);
  5071. int i;
  5072. /*
  5073. * Get some info about the firmware.
  5074. */
  5075. np->scripta_sz = fw->a_size;
  5076. np->scriptb_sz = fw->b_size;
  5077. np->scriptz_sz = fw->z_size;
  5078. np->fw_setup = fw->setup;
  5079. np->fw_patch = fw->patch;
  5080. np->fw_name = fw->name;
  5081. /*
  5082. * Save setting of some IO registers, so we will
  5083. * be able to probe specific implementations.
  5084. */
  5085. sym_save_initial_setting (np);
  5086. /*
  5087. * Reset the chip now, since it has been reported
  5088. * that SCSI clock calibration may not work properly
  5089. * if the chip is currently active.
  5090. */
  5091. sym_chip_reset(np);
  5092. /*
  5093. * Prepare controller and devices settings, according
  5094. * to chip features, user set-up and driver set-up.
  5095. */
  5096. sym_prepare_setting(shost, np, nvram);
  5097. /*
  5098. * Check the PCI clock frequency.
  5099. * Must be performed after prepare_setting since it destroys
  5100. * STEST1 that is used to probe for the clock doubler.
  5101. */
  5102. i = sym_getpciclock(np);
  5103. if (i > 37000 && !(np->features & FE_66MHZ))
  5104. printf("%s: PCI BUS clock seems too high: %u KHz.\n",
  5105. sym_name(np), i);
  5106. /*
  5107. * Allocate the start queue.
  5108. */
  5109. np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
  5110. if (!np->squeue)
  5111. goto attach_failed;
  5112. np->squeue_ba = vtobus(np->squeue);
  5113. /*
  5114. * Allocate the done queue.
  5115. */
  5116. np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
  5117. if (!np->dqueue)
  5118. goto attach_failed;
  5119. np->dqueue_ba = vtobus(np->dqueue);
  5120. /*
  5121. * Allocate the target bus address array.
  5122. */
  5123. np->targtbl = sym_calloc_dma(256, "TARGTBL");
  5124. if (!np->targtbl)
  5125. goto attach_failed;
  5126. np->targtbl_ba = vtobus(np->targtbl);
  5127. /*
  5128. * Allocate SCRIPTS areas.
  5129. */
  5130. np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
  5131. np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
  5132. np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
  5133. if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
  5134. goto attach_failed;
  5135. /*
  5136. * Allocate the array of lists of CCBs hashed by DSA.
  5137. */
  5138. np->ccbh = kcalloc(sizeof(struct sym_ccb **), CCB_HASH_SIZE, GFP_KERNEL);
  5139. if (!np->ccbh)
  5140. goto attach_failed;
  5141. /*
  5142. * Initialyze the CCB free and busy queues.
  5143. */
  5144. sym_que_init(&np->free_ccbq);
  5145. sym_que_init(&np->busy_ccbq);
  5146. sym_que_init(&np->comp_ccbq);
  5147. /*
  5148. * Initialization for optional handling
  5149. * of device queueing.
  5150. */
  5151. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5152. sym_que_init(&np->dummy_ccbq);
  5153. #endif
  5154. /*
  5155. * Allocate some CCB. We need at least ONE.
  5156. */
  5157. if (!sym_alloc_ccb(np))
  5158. goto attach_failed;
  5159. /*
  5160. * Calculate BUS addresses where we are going
  5161. * to load the SCRIPTS.
  5162. */
  5163. np->scripta_ba = vtobus(np->scripta0);
  5164. np->scriptb_ba = vtobus(np->scriptb0);
  5165. np->scriptz_ba = vtobus(np->scriptz0);
  5166. if (np->ram_ba) {
  5167. np->scripta_ba = np->ram_ba;
  5168. if (np->features & FE_RAM8K) {
  5169. np->ram_ws = 8192;
  5170. np->scriptb_ba = np->scripta_ba + 4096;
  5171. #if 0 /* May get useful for 64 BIT PCI addressing */
  5172. np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
  5173. #endif
  5174. }
  5175. else
  5176. np->ram_ws = 4096;
  5177. }
  5178. /*
  5179. * Copy scripts to controller instance.
  5180. */
  5181. memcpy(np->scripta0, fw->a_base, np->scripta_sz);
  5182. memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
  5183. memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
  5184. /*
  5185. * Setup variable parts in scripts and compute
  5186. * scripts bus addresses used from the C code.
  5187. */
  5188. np->fw_setup(np, fw);
  5189. /*
  5190. * Bind SCRIPTS with physical addresses usable by the
  5191. * SCRIPTS processor (as seen from the BUS = BUS addresses).
  5192. */
  5193. sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
  5194. sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
  5195. sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
  5196. #ifdef SYM_CONF_IARB_SUPPORT
  5197. /*
  5198. * If user wants IARB to be set when we win arbitration
  5199. * and have other jobs, compute the max number of consecutive
  5200. * settings of IARB hints before we leave devices a chance to
  5201. * arbitrate for reselection.
  5202. */
  5203. #ifdef SYM_SETUP_IARB_MAX
  5204. np->iarb_max = SYM_SETUP_IARB_MAX;
  5205. #else
  5206. np->iarb_max = 4;
  5207. #endif
  5208. #endif
  5209. /*
  5210. * Prepare the idle and invalid task actions.
  5211. */
  5212. np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5213. np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5214. np->idletask_ba = vtobus(&np->idletask);
  5215. np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5216. np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5217. np->notask_ba = vtobus(&np->notask);
  5218. np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5219. np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5220. np->bad_itl_ba = vtobus(&np->bad_itl);
  5221. np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5222. np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
  5223. np->bad_itlq_ba = vtobus(&np->bad_itlq);
  5224. /*
  5225. * Allocate and prepare the lun JUMP table that is used
  5226. * for a target prior the probing of devices (bad lun table).
  5227. * A private table will be allocated for the target on the
  5228. * first INQUIRY response received.
  5229. */
  5230. np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
  5231. if (!np->badluntbl)
  5232. goto attach_failed;
  5233. np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  5234. for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
  5235. np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  5236. /*
  5237. * Prepare the bus address array that contains the bus
  5238. * address of each target control block.
  5239. * For now, assume all logical units are wrong. :)
  5240. */
  5241. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  5242. np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
  5243. np->target[i].head.luntbl_sa =
  5244. cpu_to_scr(vtobus(np->badluntbl));
  5245. np->target[i].head.lun0_sa =
  5246. cpu_to_scr(vtobus(&np->badlun_sa));
  5247. }
  5248. /*
  5249. * Now check the cache handling of the pci chipset.
  5250. */
  5251. if (sym_snooptest (np)) {
  5252. printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
  5253. goto attach_failed;
  5254. }
  5255. /*
  5256. * Sigh! we are done.
  5257. */
  5258. return 0;
  5259. attach_failed:
  5260. return -ENXIO;
  5261. }
  5262. /*
  5263. * Free everything that has been allocated for this device.
  5264. */
  5265. void sym_hcb_free(struct sym_hcb *np)
  5266. {
  5267. SYM_QUEHEAD *qp;
  5268. struct sym_ccb *cp;
  5269. struct sym_tcb *tp;
  5270. struct sym_lcb *lp;
  5271. int target, lun;
  5272. if (np->scriptz0)
  5273. sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
  5274. if (np->scriptb0)
  5275. sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
  5276. if (np->scripta0)
  5277. sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
  5278. if (np->squeue)
  5279. sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
  5280. if (np->dqueue)
  5281. sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
  5282. if (np->actccbs) {
  5283. while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
  5284. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  5285. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  5286. }
  5287. }
  5288. kfree(np->ccbh);
  5289. if (np->badluntbl)
  5290. sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
  5291. for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
  5292. tp = &np->target[target];
  5293. for (lun = 0 ; lun < SYM_CONF_MAX_LUN ; lun++) {
  5294. lp = sym_lp(tp, lun);
  5295. if (!lp)
  5296. continue;
  5297. if (lp->itlq_tbl)
  5298. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4,
  5299. "ITLQ_TBL");
  5300. kfree(lp->cb_tags);
  5301. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  5302. }
  5303. #if SYM_CONF_MAX_LUN > 1
  5304. kfree(tp->lunmp);
  5305. #endif
  5306. }
  5307. if (np->targtbl)
  5308. sym_mfree_dma(np->targtbl, 256, "TARGTBL");
  5309. }