sym_hipd.c 141 KB

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