sym_hipd.c 142 KB

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