sym_hipd.c 143 KB

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