sym_hipd.c 141 KB

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