sym_hipd.c 141 KB

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