sym_hipd.c 144 KB

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