sym_hipd.c 141 KB

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