sym_hipd.c 144 KB

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