sym_hipd.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732
  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 SYM_CONF_DMA_ADDRESSING_MODE == 0
  706. np->rv_ccntl1 |= (DDAC);
  707. #elif SYM_CONF_DMA_ADDRESSING_MODE == 1
  708. if (!np->use_dac)
  709. np->rv_ccntl1 |= (DDAC);
  710. else
  711. np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
  712. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  713. if (!np->use_dac)
  714. np->rv_ccntl1 |= (DDAC);
  715. else
  716. np->rv_ccntl1 |= (0 | EXTIBMV);
  717. #endif
  718. }
  719. /*
  720. * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
  721. */
  722. if (np->features & FE_NOPM)
  723. np->rv_ccntl0 |= (ENPMJ);
  724. /*
  725. * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
  726. * In dual channel mode, contention occurs if internal cycles
  727. * are used. Disable internal cycles.
  728. */
  729. if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
  730. np->revision_id < 0x1)
  731. np->rv_ccntl0 |= DILS;
  732. /*
  733. * Select burst length (dwords)
  734. */
  735. burst_max = SYM_SETUP_BURST_ORDER;
  736. if (burst_max == 255)
  737. burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
  738. np->sv_ctest5);
  739. if (burst_max > 7)
  740. burst_max = 7;
  741. if (burst_max > np->maxburst)
  742. burst_max = np->maxburst;
  743. /*
  744. * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
  745. * This chip and the 860 Rev 1 may wrongly use PCI cache line
  746. * based transactions on LOAD/STORE instructions. So we have
  747. * to prevent these chips from using such PCI transactions in
  748. * this driver. The generic ncr driver that does not use
  749. * LOAD/STORE instructions does not need this work-around.
  750. */
  751. if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
  752. np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
  753. (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
  754. np->revision_id <= 0x1))
  755. np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
  756. /*
  757. * Select all supported special features.
  758. * If we are using on-board RAM for scripts, prefetch (PFEN)
  759. * does not help, but burst op fetch (BOF) does.
  760. * Disabling PFEN makes sure BOF will be used.
  761. */
  762. if (np->features & FE_ERL)
  763. np->rv_dmode |= ERL; /* Enable Read Line */
  764. if (np->features & FE_BOF)
  765. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  766. if (np->features & FE_ERMP)
  767. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  768. #if 1
  769. if ((np->features & FE_PFEN) && !np->ram_ba)
  770. #else
  771. if (np->features & FE_PFEN)
  772. #endif
  773. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  774. if (np->features & FE_CLSE)
  775. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  776. if (np->features & FE_WRIE)
  777. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  778. if (np->features & FE_DFS)
  779. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  780. /*
  781. * Select some other
  782. */
  783. np->rv_ctest4 |= MPEE; /* Master parity checking */
  784. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  785. /*
  786. * Get parity checking, host ID and verbose mode from NVRAM
  787. */
  788. np->myaddr = 255;
  789. np->scsi_mode = 0;
  790. sym_nvram_setup_host(shost, np, nvram);
  791. /*
  792. * Get SCSI addr of host adapter (set by bios?).
  793. */
  794. if (np->myaddr == 255) {
  795. np->myaddr = INB(np, nc_scid) & 0x07;
  796. if (!np->myaddr)
  797. np->myaddr = SYM_SETUP_HOST_ID;
  798. }
  799. /*
  800. * Prepare initial io register bits for burst length
  801. */
  802. sym_init_burst(np, burst_max);
  803. sym_set_bus_mode(np, nvram);
  804. /*
  805. * Set LED support from SCRIPTS.
  806. * Ignore this feature for boards known to use a
  807. * specific GPIO wiring and for the 895A, 896
  808. * and 1010 that drive the LED directly.
  809. */
  810. if ((SYM_SETUP_SCSI_LED ||
  811. (nvram->type == SYM_SYMBIOS_NVRAM ||
  812. (nvram->type == SYM_TEKRAM_NVRAM &&
  813. np->device_id == PCI_DEVICE_ID_NCR_53C895))) &&
  814. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  815. np->features |= FE_LED0;
  816. /*
  817. * Set irq mode.
  818. */
  819. switch(SYM_SETUP_IRQ_MODE & 3) {
  820. case 2:
  821. np->rv_dcntl |= IRQM;
  822. break;
  823. case 1:
  824. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  825. break;
  826. default:
  827. break;
  828. }
  829. /*
  830. * Configure targets according to driver setup.
  831. * If NVRAM present get targets setup from NVRAM.
  832. */
  833. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  834. struct sym_tcb *tp = &np->target[i];
  835. tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  836. tp->usrtags = SYM_SETUP_MAX_TAG;
  837. tp->usr_width = np->maxwide;
  838. tp->usr_period = 9;
  839. sym_nvram_setup_target(tp, i, nvram);
  840. if (!tp->usrtags)
  841. tp->usrflags &= ~SYM_TAGS_ENABLED;
  842. }
  843. /*
  844. * Let user know about the settings.
  845. */
  846. printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
  847. sym_nvram_type(nvram), np->myaddr,
  848. (np->features & FE_ULTRA3) ? 80 :
  849. (np->features & FE_ULTRA2) ? 40 :
  850. (np->features & FE_ULTRA) ? 20 : 10,
  851. sym_scsi_bus_mode(np->scsi_mode),
  852. (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
  853. /*
  854. * Tell him more on demand.
  855. */
  856. if (sym_verbose) {
  857. printf("%s: %s IRQ line driver%s\n",
  858. sym_name(np),
  859. np->rv_dcntl & IRQM ? "totem pole" : "open drain",
  860. np->ram_ba ? ", using on-chip SRAM" : "");
  861. printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
  862. if (np->features & FE_NOPM)
  863. printf("%s: handling phase mismatch from SCRIPTS.\n",
  864. sym_name(np));
  865. }
  866. /*
  867. * And still more.
  868. */
  869. if (sym_verbose >= 2) {
  870. printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  871. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  872. sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  873. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  874. printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  875. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  876. sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  877. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  878. }
  879. return 0;
  880. }
  881. /*
  882. * Test the pci bus snoop logic :-(
  883. *
  884. * Has to be called with interrupts disabled.
  885. */
  886. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  887. static int sym_regtest(struct sym_hcb *np)
  888. {
  889. register volatile u32 data;
  890. /*
  891. * chip registers may NOT be cached.
  892. * write 0xffffffff to a read only register area,
  893. * and try to read it back.
  894. */
  895. data = 0xffffffff;
  896. OUTL(np, nc_dstat, data);
  897. data = INL(np, nc_dstat);
  898. #if 1
  899. if (data == 0xffffffff) {
  900. #else
  901. if ((data & 0xe2f0fffd) != 0x02000080) {
  902. #endif
  903. printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  904. (unsigned) data);
  905. return 0x10;
  906. }
  907. return 0;
  908. }
  909. #else
  910. static inline int sym_regtest(struct sym_hcb *np)
  911. {
  912. return 0;
  913. }
  914. #endif
  915. static int sym_snooptest(struct sym_hcb *np)
  916. {
  917. u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
  918. int i, err;
  919. err = sym_regtest(np);
  920. if (err)
  921. return err;
  922. restart_test:
  923. /*
  924. * Enable Master Parity Checking as we intend
  925. * to enable it for normal operations.
  926. */
  927. OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
  928. /*
  929. * init
  930. */
  931. pc = SCRIPTZ_BA(np, snooptest);
  932. host_wr = 1;
  933. sym_wr = 2;
  934. /*
  935. * Set memory and register.
  936. */
  937. np->scratch = cpu_to_scr(host_wr);
  938. OUTL(np, nc_temp, sym_wr);
  939. /*
  940. * Start script (exchange values)
  941. */
  942. OUTL(np, nc_dsa, np->hcb_ba);
  943. OUTL_DSP(np, pc);
  944. /*
  945. * Wait 'til done (with timeout)
  946. */
  947. for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
  948. if (INB(np, nc_istat) & (INTF|SIP|DIP))
  949. break;
  950. if (i>=SYM_SNOOP_TIMEOUT) {
  951. printf ("CACHE TEST FAILED: timeout.\n");
  952. return (0x20);
  953. }
  954. /*
  955. * Check for fatal DMA errors.
  956. */
  957. dstat = INB(np, nc_dstat);
  958. #if 1 /* Band aiding for broken hardwares that fail PCI parity */
  959. if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
  960. printf ("%s: PCI DATA PARITY ERROR DETECTED - "
  961. "DISABLING MASTER DATA PARITY CHECKING.\n",
  962. sym_name(np));
  963. np->rv_ctest4 &= ~MPEE;
  964. goto restart_test;
  965. }
  966. #endif
  967. if (dstat & (MDPE|BF|IID)) {
  968. printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
  969. return (0x80);
  970. }
  971. /*
  972. * Save termination position.
  973. */
  974. pc = INL(np, nc_dsp);
  975. /*
  976. * Read memory and register.
  977. */
  978. host_rd = scr_to_cpu(np->scratch);
  979. sym_rd = INL(np, nc_scratcha);
  980. sym_bk = INL(np, nc_temp);
  981. /*
  982. * Check termination position.
  983. */
  984. if (pc != SCRIPTZ_BA(np, snoopend)+8) {
  985. printf ("CACHE TEST FAILED: script execution failed.\n");
  986. printf ("start=%08lx, pc=%08lx, end=%08lx\n",
  987. (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
  988. (u_long) SCRIPTZ_BA(np, snoopend) +8);
  989. return (0x40);
  990. }
  991. /*
  992. * Show results.
  993. */
  994. if (host_wr != sym_rd) {
  995. printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
  996. (int) host_wr, (int) sym_rd);
  997. err |= 1;
  998. }
  999. if (host_rd != sym_wr) {
  1000. printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
  1001. (int) sym_wr, (int) host_rd);
  1002. err |= 2;
  1003. }
  1004. if (sym_bk != sym_wr) {
  1005. printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
  1006. (int) sym_wr, (int) sym_bk);
  1007. err |= 4;
  1008. }
  1009. return err;
  1010. }
  1011. /*
  1012. * log message for real hard errors
  1013. *
  1014. * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
  1015. * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
  1016. *
  1017. * exception register:
  1018. * ds: dstat
  1019. * si: sist
  1020. *
  1021. * SCSI bus lines:
  1022. * so: control lines as driven by chip.
  1023. * si: control lines as seen by chip.
  1024. * sd: scsi data lines as seen by chip.
  1025. *
  1026. * wide/fastmode:
  1027. * sx: sxfer (see the manual)
  1028. * s3: scntl3 (see the manual)
  1029. * s4: scntl4 (see the manual)
  1030. *
  1031. * current script command:
  1032. * dsp: script address (relative to start of script).
  1033. * dbc: first word of script command.
  1034. *
  1035. * First 24 register of the chip:
  1036. * r0..rf
  1037. */
  1038. static void sym_log_hard_error(struct sym_hcb *np, u_short sist, u_char dstat)
  1039. {
  1040. u32 dsp;
  1041. int script_ofs;
  1042. int script_size;
  1043. char *script_name;
  1044. u_char *script_base;
  1045. int i;
  1046. dsp = INL(np, nc_dsp);
  1047. if (dsp > np->scripta_ba &&
  1048. dsp <= np->scripta_ba + np->scripta_sz) {
  1049. script_ofs = dsp - np->scripta_ba;
  1050. script_size = np->scripta_sz;
  1051. script_base = (u_char *) np->scripta0;
  1052. script_name = "scripta";
  1053. }
  1054. else if (np->scriptb_ba < dsp &&
  1055. dsp <= np->scriptb_ba + np->scriptb_sz) {
  1056. script_ofs = dsp - np->scriptb_ba;
  1057. script_size = np->scriptb_sz;
  1058. script_base = (u_char *) np->scriptb0;
  1059. script_name = "scriptb";
  1060. } else {
  1061. script_ofs = dsp;
  1062. script_size = 0;
  1063. script_base = NULL;
  1064. script_name = "mem";
  1065. }
  1066. printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
  1067. sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
  1068. (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
  1069. (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
  1070. (unsigned)INB(np, nc_scntl3),
  1071. (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
  1072. script_name, script_ofs, (unsigned)INL(np, nc_dbc));
  1073. if (((script_ofs & 3) == 0) &&
  1074. (unsigned)script_ofs < script_size) {
  1075. printf ("%s: script cmd = %08x\n", sym_name(np),
  1076. scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
  1077. }
  1078. printf ("%s: regdump:", sym_name(np));
  1079. for (i=0; i<24;i++)
  1080. printf (" %02x", (unsigned)INB_OFF(np, i));
  1081. printf (".\n");
  1082. /*
  1083. * PCI BUS error.
  1084. */
  1085. if (dstat & (MDPE|BF))
  1086. sym_log_bus_error(np);
  1087. }
  1088. static struct sym_chip sym_dev_table[] = {
  1089. {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
  1090. FE_ERL}
  1091. ,
  1092. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1093. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1094. FE_BOF}
  1095. ,
  1096. #else
  1097. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1098. FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
  1099. ,
  1100. #endif
  1101. {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
  1102. FE_BOF|FE_ERL}
  1103. ,
  1104. {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
  1105. FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
  1106. ,
  1107. {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
  1108. FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
  1109. ,
  1110. {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
  1111. FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
  1112. ,
  1113. {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
  1114. FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1115. FE_RAM|FE_DIFF|FE_VARCLK}
  1116. ,
  1117. {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
  1118. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1119. FE_RAM|FE_DIFF|FE_VARCLK}
  1120. ,
  1121. {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
  1122. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1123. FE_RAM|FE_DIFF|FE_VARCLK}
  1124. ,
  1125. {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
  1126. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1127. FE_RAM|FE_DIFF|FE_VARCLK}
  1128. ,
  1129. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1130. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1131. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
  1132. FE_RAM|FE_LCKFRQ}
  1133. ,
  1134. #else
  1135. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1136. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1137. FE_RAM|FE_LCKFRQ}
  1138. ,
  1139. #endif
  1140. {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
  1141. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1142. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1143. ,
  1144. {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
  1145. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1146. FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1147. ,
  1148. {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
  1149. FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1150. FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1151. ,
  1152. {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
  1153. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1154. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1155. FE_C10}
  1156. ,
  1157. {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
  1158. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1159. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1160. FE_C10|FE_U3EN}
  1161. ,
  1162. {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
  1163. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1164. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
  1165. FE_C10|FE_U3EN}
  1166. ,
  1167. {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
  1168. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1169. FE_RAM|FE_IO256|FE_LEDC}
  1170. };
  1171. #define sym_num_devs \
  1172. (sizeof(sym_dev_table) / sizeof(sym_dev_table[0]))
  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 (!np->use_dac)
  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->device_id == 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->device_id == PCI_DEVICE_ID_LSI_53C1010_33 &&
  1638. np->revision_id < 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->device_id == PCI_DEVICE_ID_NCR_53C875)
  1646. OUTB(np, nc_ctest0, (1<<5));
  1647. else if (np->device_id == 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 (np->use_dac) {
  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->ram_ws == 8192) {
  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. } while (istatc & (SIP|DIP));
  2547. if (DEBUG_FLAGS & DEBUG_TINY)
  2548. printf ("<%d|%x:%x|%x:%x>",
  2549. (int)INB(np, nc_scr0),
  2550. dstat,sist,
  2551. (unsigned)INL(np, nc_dsp),
  2552. (unsigned)INL(np, nc_dbc));
  2553. /*
  2554. * On paper, a memory read barrier may be needed here to
  2555. * prevent out of order LOADs by the CPU from having
  2556. * prefetched stale data prior to DMA having occurred.
  2557. * And since we are paranoid ... :)
  2558. */
  2559. MEMORY_READ_BARRIER();
  2560. /*
  2561. * First, interrupts we want to service cleanly.
  2562. *
  2563. * Phase mismatch (MA) is the most frequent interrupt
  2564. * for chip earlier than the 896 and so we have to service
  2565. * it as quickly as possible.
  2566. * A SCSI parity error (PAR) may be combined with a phase
  2567. * mismatch condition (MA).
  2568. * Programmed interrupts (SIR) are used to call the C code
  2569. * from SCRIPTS.
  2570. * The single step interrupt (SSI) is not used in this
  2571. * driver.
  2572. */
  2573. if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
  2574. !(dstat & (MDPE|BF|ABRT|IID))) {
  2575. if (sist & PAR) sym_int_par (np, sist);
  2576. else if (sist & MA) sym_int_ma (np);
  2577. else if (dstat & SIR) sym_int_sir (np);
  2578. else if (dstat & SSI) OUTONB_STD();
  2579. else goto unknown_int;
  2580. return;
  2581. }
  2582. /*
  2583. * Now, interrupts that donnot happen in normal
  2584. * situations and that we may need to recover from.
  2585. *
  2586. * On SCSI RESET (RST), we reset everything.
  2587. * On SCSI BUS MODE CHANGE (SBMC), we complete all
  2588. * active CCBs with RESET status, prepare all devices
  2589. * for negotiating again and restart the SCRIPTS.
  2590. * On STO and UDC, we complete the CCB with the corres-
  2591. * ponding status and restart the SCRIPTS.
  2592. */
  2593. if (sist & RST) {
  2594. printf("%s: SCSI BUS reset detected.\n", sym_name(np));
  2595. sym_start_up (np, 1);
  2596. return;
  2597. }
  2598. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  2599. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  2600. if (!(sist & (GEN|HTH|SGE)) &&
  2601. !(dstat & (MDPE|BF|ABRT|IID))) {
  2602. if (sist & SBMC) sym_int_sbmc (np);
  2603. else if (sist & STO) sym_int_sto (np);
  2604. else if (sist & UDC) sym_int_udc (np);
  2605. else goto unknown_int;
  2606. return;
  2607. }
  2608. /*
  2609. * Now, interrupts we are not able to recover cleanly.
  2610. *
  2611. * Log message for hard errors.
  2612. * Reset everything.
  2613. */
  2614. sym_log_hard_error(np, sist, dstat);
  2615. if ((sist & (GEN|HTH|SGE)) ||
  2616. (dstat & (MDPE|BF|ABRT|IID))) {
  2617. sym_start_reset(np);
  2618. return;
  2619. }
  2620. unknown_int:
  2621. /*
  2622. * We just miss the cause of the interrupt. :(
  2623. * Print a message. The timeout will do the real work.
  2624. */
  2625. printf( "%s: unknown interrupt(s) ignored, "
  2626. "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
  2627. sym_name(np), istat, dstat, sist);
  2628. }
  2629. /*
  2630. * Dequeue from the START queue all CCBs that match
  2631. * a given target/lun/task condition (-1 means all),
  2632. * and move them from the BUSY queue to the COMP queue
  2633. * with DID_SOFT_ERROR status condition.
  2634. * This function is used during error handling/recovery.
  2635. * It is called with SCRIPTS not running.
  2636. */
  2637. static int
  2638. sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
  2639. {
  2640. int j;
  2641. struct sym_ccb *cp;
  2642. /*
  2643. * Make sure the starting index is within range.
  2644. */
  2645. assert((i >= 0) && (i < 2*MAX_QUEUE));
  2646. /*
  2647. * Walk until end of START queue and dequeue every job
  2648. * that matches the target/lun/task condition.
  2649. */
  2650. j = i;
  2651. while (i != np->squeueput) {
  2652. cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
  2653. assert(cp);
  2654. #ifdef SYM_CONF_IARB_SUPPORT
  2655. /* Forget hints for IARB, they may be no longer relevant */
  2656. cp->host_flags &= ~HF_HINT_IARB;
  2657. #endif
  2658. if ((target == -1 || cp->target == target) &&
  2659. (lun == -1 || cp->lun == lun) &&
  2660. (task == -1 || cp->tag == task)) {
  2661. sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
  2662. sym_remque(&cp->link_ccbq);
  2663. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2664. }
  2665. else {
  2666. if (i != j)
  2667. np->squeue[j] = np->squeue[i];
  2668. if ((j += 2) >= MAX_QUEUE*2) j = 0;
  2669. }
  2670. if ((i += 2) >= MAX_QUEUE*2) i = 0;
  2671. }
  2672. if (i != j) /* Copy back the idle task if needed */
  2673. np->squeue[j] = np->squeue[i];
  2674. np->squeueput = j; /* Update our current start queue pointer */
  2675. return (i - j) / 2;
  2676. }
  2677. /*
  2678. * chip handler for bad SCSI status condition
  2679. *
  2680. * In case of bad SCSI status, we unqueue all the tasks
  2681. * currently queued to the controller but not yet started
  2682. * and then restart the SCRIPTS processor immediately.
  2683. *
  2684. * QUEUE FULL and BUSY conditions are handled the same way.
  2685. * Basically all the not yet started tasks are requeued in
  2686. * device queue and the queue is frozen until a completion.
  2687. *
  2688. * For CHECK CONDITION and COMMAND TERMINATED status, we use
  2689. * the CCB of the failed command to prepare a REQUEST SENSE
  2690. * SCSI command and queue it to the controller queue.
  2691. *
  2692. * SCRATCHA is assumed to have been loaded with STARTPOS
  2693. * before the SCRIPTS called the C code.
  2694. */
  2695. static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
  2696. {
  2697. u32 startp;
  2698. u_char s_status = cp->ssss_status;
  2699. u_char h_flags = cp->host_flags;
  2700. int msglen;
  2701. int i;
  2702. /*
  2703. * Compute the index of the next job to start from SCRIPTS.
  2704. */
  2705. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  2706. /*
  2707. * The last CCB queued used for IARB hint may be
  2708. * no longer relevant. Forget it.
  2709. */
  2710. #ifdef SYM_CONF_IARB_SUPPORT
  2711. if (np->last_cp)
  2712. np->last_cp = 0;
  2713. #endif
  2714. /*
  2715. * Now deal with the SCSI status.
  2716. */
  2717. switch(s_status) {
  2718. case S_BUSY:
  2719. case S_QUEUE_FULL:
  2720. if (sym_verbose >= 2) {
  2721. sym_print_addr(cp->cmd, "%s\n",
  2722. s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
  2723. }
  2724. default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
  2725. sym_complete_error (np, cp);
  2726. break;
  2727. case S_TERMINATED:
  2728. case S_CHECK_COND:
  2729. /*
  2730. * If we get an SCSI error when requesting sense, give up.
  2731. */
  2732. if (h_flags & HF_SENSE) {
  2733. sym_complete_error (np, cp);
  2734. break;
  2735. }
  2736. /*
  2737. * Dequeue all queued CCBs for that device not yet started,
  2738. * and restart the SCRIPTS processor immediately.
  2739. */
  2740. sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  2741. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2742. /*
  2743. * Save some info of the actual IO.
  2744. * Compute the data residual.
  2745. */
  2746. cp->sv_scsi_status = cp->ssss_status;
  2747. cp->sv_xerr_status = cp->xerr_status;
  2748. cp->sv_resid = sym_compute_residual(np, cp);
  2749. /*
  2750. * Prepare all needed data structures for
  2751. * requesting sense data.
  2752. */
  2753. cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
  2754. msglen = 1;
  2755. /*
  2756. * If we are currently using anything different from
  2757. * async. 8 bit data transfers with that target,
  2758. * start a negotiation, since the device may want
  2759. * to report us a UNIT ATTENTION condition due to
  2760. * a cause we currently ignore, and we donnot want
  2761. * to be stuck with WIDE and/or SYNC data transfer.
  2762. *
  2763. * cp->nego_status is filled by sym_prepare_nego().
  2764. */
  2765. cp->nego_status = 0;
  2766. msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
  2767. /*
  2768. * Message table indirect structure.
  2769. */
  2770. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
  2771. cp->phys.smsg.size = cpu_to_scr(msglen);
  2772. /*
  2773. * sense command
  2774. */
  2775. cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
  2776. cp->phys.cmd.size = cpu_to_scr(6);
  2777. /*
  2778. * patch requested size into sense command
  2779. */
  2780. cp->sensecmd[0] = REQUEST_SENSE;
  2781. cp->sensecmd[1] = 0;
  2782. if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
  2783. cp->sensecmd[1] = cp->lun << 5;
  2784. cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
  2785. cp->data_len = SYM_SNS_BBUF_LEN;
  2786. /*
  2787. * sense data
  2788. */
  2789. memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
  2790. cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
  2791. cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
  2792. /*
  2793. * requeue the command.
  2794. */
  2795. startp = SCRIPTB_BA(np, sdata_in);
  2796. cp->phys.head.savep = cpu_to_scr(startp);
  2797. cp->phys.head.lastp = cpu_to_scr(startp);
  2798. cp->startp = cpu_to_scr(startp);
  2799. cp->goalp = cpu_to_scr(startp + 16);
  2800. cp->host_xflags = 0;
  2801. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  2802. cp->ssss_status = S_ILLEGAL;
  2803. cp->host_flags = (HF_SENSE|HF_DATA_IN);
  2804. cp->xerr_status = 0;
  2805. cp->extra_bytes = 0;
  2806. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  2807. /*
  2808. * Requeue the command.
  2809. */
  2810. sym_put_start_queue(np, cp);
  2811. /*
  2812. * Give back to upper layer everything we have dequeued.
  2813. */
  2814. sym_flush_comp_queue(np, 0);
  2815. break;
  2816. }
  2817. }
  2818. /*
  2819. * After a device has accepted some management message
  2820. * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
  2821. * a device signals a UNIT ATTENTION condition, some
  2822. * tasks are thrown away by the device. We are required
  2823. * to reflect that on our tasks list since the device
  2824. * will never complete these tasks.
  2825. *
  2826. * This function move from the BUSY queue to the COMP
  2827. * queue all disconnected CCBs for a given target that
  2828. * match the following criteria:
  2829. * - lun=-1 means any logical UNIT otherwise a given one.
  2830. * - task=-1 means any task, otherwise a given one.
  2831. */
  2832. int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
  2833. {
  2834. SYM_QUEHEAD qtmp, *qp;
  2835. int i = 0;
  2836. struct sym_ccb *cp;
  2837. /*
  2838. * Move the entire BUSY queue to our temporary queue.
  2839. */
  2840. sym_que_init(&qtmp);
  2841. sym_que_splice(&np->busy_ccbq, &qtmp);
  2842. sym_que_init(&np->busy_ccbq);
  2843. /*
  2844. * Put all CCBs that matches our criteria into
  2845. * the COMP queue and put back other ones into
  2846. * the BUSY queue.
  2847. */
  2848. while ((qp = sym_remque_head(&qtmp)) != 0) {
  2849. struct scsi_cmnd *cmd;
  2850. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2851. cmd = cp->cmd;
  2852. if (cp->host_status != HS_DISCONNECT ||
  2853. cp->target != target ||
  2854. (lun != -1 && cp->lun != lun) ||
  2855. (task != -1 &&
  2856. (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
  2857. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  2858. continue;
  2859. }
  2860. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2861. /* Preserve the software timeout condition */
  2862. if (sym_get_cam_status(cmd) != DID_TIME_OUT)
  2863. sym_set_cam_status(cmd, cam_status);
  2864. ++i;
  2865. #if 0
  2866. printf("XXXX TASK @%p CLEARED\n", cp);
  2867. #endif
  2868. }
  2869. return i;
  2870. }
  2871. /*
  2872. * chip handler for TASKS recovery
  2873. *
  2874. * We cannot safely abort a command, while the SCRIPTS
  2875. * processor is running, since we just would be in race
  2876. * with it.
  2877. *
  2878. * As long as we have tasks to abort, we keep the SEM
  2879. * bit set in the ISTAT. When this bit is set, the
  2880. * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
  2881. * each time it enters the scheduler.
  2882. *
  2883. * If we have to reset a target, clear tasks of a unit,
  2884. * or to perform the abort of a disconnected job, we
  2885. * restart the SCRIPTS for selecting the target. Once
  2886. * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
  2887. * If it loses arbitration, the SCRIPTS will interrupt again
  2888. * the next time it will enter its scheduler, and so on ...
  2889. *
  2890. * On SIR_TARGET_SELECTED, we scan for the more
  2891. * appropriate thing to do:
  2892. *
  2893. * - If nothing, we just sent a M_ABORT message to the
  2894. * target to get rid of the useless SCSI bus ownership.
  2895. * According to the specs, no tasks shall be affected.
  2896. * - If the target is to be reset, we send it a M_RESET
  2897. * message.
  2898. * - If a logical UNIT is to be cleared , we send the
  2899. * IDENTIFY(lun) + M_ABORT.
  2900. * - If an untagged task is to be aborted, we send the
  2901. * IDENTIFY(lun) + M_ABORT.
  2902. * - If a tagged task is to be aborted, we send the
  2903. * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
  2904. *
  2905. * Once our 'kiss of death' :) message has been accepted
  2906. * by the target, the SCRIPTS interrupts again
  2907. * (SIR_ABORT_SENT). On this interrupt, we complete
  2908. * all the CCBs that should have been aborted by the
  2909. * target according to our message.
  2910. */
  2911. static void sym_sir_task_recovery(struct sym_hcb *np, int num)
  2912. {
  2913. SYM_QUEHEAD *qp;
  2914. struct sym_ccb *cp;
  2915. struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
  2916. struct scsi_target *starget;
  2917. int target=-1, lun=-1, task;
  2918. int i, k;
  2919. switch(num) {
  2920. /*
  2921. * The SCRIPTS processor stopped before starting
  2922. * the next command in order to allow us to perform
  2923. * some task recovery.
  2924. */
  2925. case SIR_SCRIPT_STOPPED:
  2926. /*
  2927. * Do we have any target to reset or unit to clear ?
  2928. */
  2929. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  2930. tp = &np->target[i];
  2931. if (tp->to_reset ||
  2932. (tp->lun0p && tp->lun0p->to_clear)) {
  2933. target = i;
  2934. break;
  2935. }
  2936. if (!tp->lunmp)
  2937. continue;
  2938. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  2939. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  2940. target = i;
  2941. break;
  2942. }
  2943. }
  2944. if (target != -1)
  2945. break;
  2946. }
  2947. /*
  2948. * If not, walk the busy queue for any
  2949. * disconnected CCB to be aborted.
  2950. */
  2951. if (target == -1) {
  2952. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  2953. cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
  2954. if (cp->host_status != HS_DISCONNECT)
  2955. continue;
  2956. if (cp->to_abort) {
  2957. target = cp->target;
  2958. break;
  2959. }
  2960. }
  2961. }
  2962. /*
  2963. * If some target is to be selected,
  2964. * prepare and start the selection.
  2965. */
  2966. if (target != -1) {
  2967. tp = &np->target[target];
  2968. np->abrt_sel.sel_id = target;
  2969. np->abrt_sel.sel_scntl3 = tp->head.wval;
  2970. np->abrt_sel.sel_sxfer = tp->head.sval;
  2971. OUTL(np, nc_dsa, np->hcb_ba);
  2972. OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
  2973. return;
  2974. }
  2975. /*
  2976. * Now look for a CCB to abort that haven't started yet.
  2977. * Btw, the SCRIPTS processor is still stopped, so
  2978. * we are not in race.
  2979. */
  2980. i = 0;
  2981. cp = NULL;
  2982. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  2983. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2984. if (cp->host_status != HS_BUSY &&
  2985. cp->host_status != HS_NEGOTIATE)
  2986. continue;
  2987. if (!cp->to_abort)
  2988. continue;
  2989. #ifdef SYM_CONF_IARB_SUPPORT
  2990. /*
  2991. * If we are using IMMEDIATE ARBITRATION, we donnot
  2992. * want to cancel the last queued CCB, since the
  2993. * SCRIPTS may have anticipated the selection.
  2994. */
  2995. if (cp == np->last_cp) {
  2996. cp->to_abort = 0;
  2997. continue;
  2998. }
  2999. #endif
  3000. i = 1; /* Means we have found some */
  3001. break;
  3002. }
  3003. if (!i) {
  3004. /*
  3005. * We are done, so we donnot need
  3006. * to synchronize with the SCRIPTS anylonger.
  3007. * Remove the SEM flag from the ISTAT.
  3008. */
  3009. np->istat_sem = 0;
  3010. OUTB(np, nc_istat, SIGP);
  3011. break;
  3012. }
  3013. /*
  3014. * Compute index of next position in the start
  3015. * queue the SCRIPTS intends to start and dequeue
  3016. * all CCBs for that device that haven't been started.
  3017. */
  3018. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3019. i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  3020. /*
  3021. * Make sure at least our IO to abort has been dequeued.
  3022. */
  3023. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  3024. assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
  3025. #else
  3026. sym_remque(&cp->link_ccbq);
  3027. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  3028. #endif
  3029. /*
  3030. * Keep track in cam status of the reason of the abort.
  3031. */
  3032. if (cp->to_abort == 2)
  3033. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3034. else
  3035. sym_set_cam_status(cp->cmd, DID_ABORT);
  3036. /*
  3037. * Complete with error everything that we have dequeued.
  3038. */
  3039. sym_flush_comp_queue(np, 0);
  3040. break;
  3041. /*
  3042. * The SCRIPTS processor has selected a target
  3043. * we may have some manual recovery to perform for.
  3044. */
  3045. case SIR_TARGET_SELECTED:
  3046. target = INB(np, nc_sdid) & 0xf;
  3047. tp = &np->target[target];
  3048. np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
  3049. /*
  3050. * If the target is to be reset, prepare a
  3051. * M_RESET message and clear the to_reset flag
  3052. * since we donnot expect this operation to fail.
  3053. */
  3054. if (tp->to_reset) {
  3055. np->abrt_msg[0] = M_RESET;
  3056. np->abrt_tbl.size = 1;
  3057. tp->to_reset = 0;
  3058. break;
  3059. }
  3060. /*
  3061. * Otherwise, look for some logical unit to be cleared.
  3062. */
  3063. if (tp->lun0p && tp->lun0p->to_clear)
  3064. lun = 0;
  3065. else if (tp->lunmp) {
  3066. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  3067. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  3068. lun = k;
  3069. break;
  3070. }
  3071. }
  3072. }
  3073. /*
  3074. * If a logical unit is to be cleared, prepare
  3075. * an IDENTIFY(lun) + ABORT MESSAGE.
  3076. */
  3077. if (lun != -1) {
  3078. struct sym_lcb *lp = sym_lp(tp, lun);
  3079. lp->to_clear = 0; /* We don't expect to fail here */
  3080. np->abrt_msg[0] = IDENTIFY(0, lun);
  3081. np->abrt_msg[1] = M_ABORT;
  3082. np->abrt_tbl.size = 2;
  3083. break;
  3084. }
  3085. /*
  3086. * Otherwise, look for some disconnected job to
  3087. * abort for this target.
  3088. */
  3089. i = 0;
  3090. cp = NULL;
  3091. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3092. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3093. if (cp->host_status != HS_DISCONNECT)
  3094. continue;
  3095. if (cp->target != target)
  3096. continue;
  3097. if (!cp->to_abort)
  3098. continue;
  3099. i = 1; /* Means we have some */
  3100. break;
  3101. }
  3102. /*
  3103. * If we have none, probably since the device has
  3104. * completed the command before we won abitration,
  3105. * send a M_ABORT message without IDENTIFY.
  3106. * According to the specs, the device must just
  3107. * disconnect the BUS and not abort any task.
  3108. */
  3109. if (!i) {
  3110. np->abrt_msg[0] = M_ABORT;
  3111. np->abrt_tbl.size = 1;
  3112. break;
  3113. }
  3114. /*
  3115. * We have some task to abort.
  3116. * Set the IDENTIFY(lun)
  3117. */
  3118. np->abrt_msg[0] = IDENTIFY(0, cp->lun);
  3119. /*
  3120. * If we want to abort an untagged command, we
  3121. * will send a IDENTIFY + M_ABORT.
  3122. * Otherwise (tagged command), we will send
  3123. * a IDENTITFY + task attributes + ABORT TAG.
  3124. */
  3125. if (cp->tag == NO_TAG) {
  3126. np->abrt_msg[1] = M_ABORT;
  3127. np->abrt_tbl.size = 2;
  3128. } else {
  3129. np->abrt_msg[1] = cp->scsi_smsg[1];
  3130. np->abrt_msg[2] = cp->scsi_smsg[2];
  3131. np->abrt_msg[3] = M_ABORT_TAG;
  3132. np->abrt_tbl.size = 4;
  3133. }
  3134. /*
  3135. * Keep track of software timeout condition, since the
  3136. * peripheral driver may not count retries on abort
  3137. * conditions not due to timeout.
  3138. */
  3139. if (cp->to_abort == 2)
  3140. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3141. cp->to_abort = 0; /* We donnot expect to fail here */
  3142. break;
  3143. /*
  3144. * The target has accepted our message and switched
  3145. * to BUS FREE phase as we expected.
  3146. */
  3147. case SIR_ABORT_SENT:
  3148. target = INB(np, nc_sdid) & 0xf;
  3149. tp = &np->target[target];
  3150. starget = tp->starget;
  3151. /*
  3152. ** If we didn't abort anything, leave here.
  3153. */
  3154. if (np->abrt_msg[0] == M_ABORT)
  3155. break;
  3156. /*
  3157. * If we sent a M_RESET, then a hardware reset has
  3158. * been performed by the target.
  3159. * - Reset everything to async 8 bit
  3160. * - Tell ourself to negotiate next time :-)
  3161. * - Prepare to clear all disconnected CCBs for
  3162. * this target from our task list (lun=task=-1)
  3163. */
  3164. lun = -1;
  3165. task = -1;
  3166. if (np->abrt_msg[0] == M_RESET) {
  3167. tp->head.sval = 0;
  3168. tp->head.wval = np->rv_scntl3;
  3169. tp->head.uval = 0;
  3170. spi_period(starget) = 0;
  3171. spi_offset(starget) = 0;
  3172. spi_width(starget) = 0;
  3173. spi_iu(starget) = 0;
  3174. spi_dt(starget) = 0;
  3175. spi_qas(starget) = 0;
  3176. tp->tgoal.check_nego = 1;
  3177. }
  3178. /*
  3179. * Otherwise, check for the LUN and TASK(s)
  3180. * concerned by the cancelation.
  3181. * If it is not ABORT_TAG then it is CLEAR_QUEUE
  3182. * or an ABORT message :-)
  3183. */
  3184. else {
  3185. lun = np->abrt_msg[0] & 0x3f;
  3186. if (np->abrt_msg[1] == M_ABORT_TAG)
  3187. task = np->abrt_msg[2];
  3188. }
  3189. /*
  3190. * Complete all the CCBs the device should have
  3191. * aborted due to our 'kiss of death' message.
  3192. */
  3193. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3194. sym_dequeue_from_squeue(np, i, target, lun, -1);
  3195. sym_clear_tasks(np, DID_ABORT, target, lun, task);
  3196. sym_flush_comp_queue(np, 0);
  3197. /*
  3198. * If we sent a BDR, make upper layer aware of that.
  3199. */
  3200. if (np->abrt_msg[0] == M_RESET)
  3201. sym_xpt_async_sent_bdr(np, target);
  3202. break;
  3203. }
  3204. /*
  3205. * Print to the log the message we intend to send.
  3206. */
  3207. if (num == SIR_TARGET_SELECTED) {
  3208. dev_info(&tp->starget->dev, "control msgout:");
  3209. sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
  3210. np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
  3211. }
  3212. /*
  3213. * Let the SCRIPTS processor continue.
  3214. */
  3215. OUTONB_STD();
  3216. }
  3217. /*
  3218. * Gerard's alchemy:) that deals with with the data
  3219. * pointer for both MDP and the residual calculation.
  3220. *
  3221. * I didn't want to bloat the code by more than 200
  3222. * lines for the handling of both MDP and the residual.
  3223. * This has been achieved by using a data pointer
  3224. * representation consisting in an index in the data
  3225. * array (dp_sg) and a negative offset (dp_ofs) that
  3226. * have the following meaning:
  3227. *
  3228. * - dp_sg = SYM_CONF_MAX_SG
  3229. * we are at the end of the data script.
  3230. * - dp_sg < SYM_CONF_MAX_SG
  3231. * dp_sg points to the next entry of the scatter array
  3232. * we want to transfer.
  3233. * - dp_ofs < 0
  3234. * dp_ofs represents the residual of bytes of the
  3235. * previous entry scatter entry we will send first.
  3236. * - dp_ofs = 0
  3237. * no residual to send first.
  3238. *
  3239. * The function sym_evaluate_dp() accepts an arbitray
  3240. * offset (basically from the MDP message) and returns
  3241. * the corresponding values of dp_sg and dp_ofs.
  3242. */
  3243. static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
  3244. {
  3245. u32 dp_scr;
  3246. int dp_ofs, dp_sg, dp_sgmin;
  3247. int tmp;
  3248. struct sym_pmc *pm;
  3249. /*
  3250. * Compute the resulted data pointer in term of a script
  3251. * address within some DATA script and a signed byte offset.
  3252. */
  3253. dp_scr = scr;
  3254. dp_ofs = *ofs;
  3255. if (dp_scr == SCRIPTA_BA(np, pm0_data))
  3256. pm = &cp->phys.pm0;
  3257. else if (dp_scr == SCRIPTA_BA(np, pm1_data))
  3258. pm = &cp->phys.pm1;
  3259. else
  3260. pm = NULL;
  3261. if (pm) {
  3262. dp_scr = scr_to_cpu(pm->ret);
  3263. dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
  3264. }
  3265. /*
  3266. * If we are auto-sensing, then we are done.
  3267. */
  3268. if (cp->host_flags & HF_SENSE) {
  3269. *ofs = dp_ofs;
  3270. return 0;
  3271. }
  3272. /*
  3273. * Deduce the index of the sg entry.
  3274. * Keep track of the index of the first valid entry.
  3275. * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
  3276. * end of the data.
  3277. */
  3278. tmp = scr_to_cpu(cp->goalp);
  3279. dp_sg = SYM_CONF_MAX_SG;
  3280. if (dp_scr != tmp)
  3281. dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
  3282. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3283. /*
  3284. * Move to the sg entry the data pointer belongs to.
  3285. *
  3286. * If we are inside the data area, we expect result to be:
  3287. *
  3288. * Either,
  3289. * dp_ofs = 0 and dp_sg is the index of the sg entry
  3290. * the data pointer belongs to (or the end of the data)
  3291. * Or,
  3292. * dp_ofs < 0 and dp_sg is the index of the sg entry
  3293. * the data pointer belongs to + 1.
  3294. */
  3295. if (dp_ofs < 0) {
  3296. int n;
  3297. while (dp_sg > dp_sgmin) {
  3298. --dp_sg;
  3299. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3300. n = dp_ofs + (tmp & 0xffffff);
  3301. if (n > 0) {
  3302. ++dp_sg;
  3303. break;
  3304. }
  3305. dp_ofs = n;
  3306. }
  3307. }
  3308. else if (dp_ofs > 0) {
  3309. while (dp_sg < SYM_CONF_MAX_SG) {
  3310. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3311. dp_ofs -= (tmp & 0xffffff);
  3312. ++dp_sg;
  3313. if (dp_ofs <= 0)
  3314. break;
  3315. }
  3316. }
  3317. /*
  3318. * Make sure the data pointer is inside the data area.
  3319. * If not, return some error.
  3320. */
  3321. if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
  3322. goto out_err;
  3323. else if (dp_sg > SYM_CONF_MAX_SG ||
  3324. (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
  3325. goto out_err;
  3326. /*
  3327. * Save the extreme pointer if needed.
  3328. */
  3329. if (dp_sg > cp->ext_sg ||
  3330. (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
  3331. cp->ext_sg = dp_sg;
  3332. cp->ext_ofs = dp_ofs;
  3333. }
  3334. /*
  3335. * Return data.
  3336. */
  3337. *ofs = dp_ofs;
  3338. return dp_sg;
  3339. out_err:
  3340. return -1;
  3341. }
  3342. /*
  3343. * chip handler for MODIFY DATA POINTER MESSAGE
  3344. *
  3345. * We also call this function on IGNORE WIDE RESIDUE
  3346. * messages that do not match a SWIDE full condition.
  3347. * Btw, we assume in that situation that such a message
  3348. * is equivalent to a MODIFY DATA POINTER (offset=-1).
  3349. */
  3350. static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
  3351. {
  3352. int dp_ofs = ofs;
  3353. u32 dp_scr = sym_get_script_dp (np, cp);
  3354. u32 dp_ret;
  3355. u32 tmp;
  3356. u_char hflags;
  3357. int dp_sg;
  3358. struct sym_pmc *pm;
  3359. /*
  3360. * Not supported for auto-sense.
  3361. */
  3362. if (cp->host_flags & HF_SENSE)
  3363. goto out_reject;
  3364. /*
  3365. * Apply our alchemy:) (see comments in sym_evaluate_dp()),
  3366. * to the resulted data pointer.
  3367. */
  3368. dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
  3369. if (dp_sg < 0)
  3370. goto out_reject;
  3371. /*
  3372. * And our alchemy:) allows to easily calculate the data
  3373. * script address we want to return for the next data phase.
  3374. */
  3375. dp_ret = cpu_to_scr(cp->goalp);
  3376. dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
  3377. /*
  3378. * If offset / scatter entry is zero we donnot need
  3379. * a context for the new current data pointer.
  3380. */
  3381. if (dp_ofs == 0) {
  3382. dp_scr = dp_ret;
  3383. goto out_ok;
  3384. }
  3385. /*
  3386. * Get a context for the new current data pointer.
  3387. */
  3388. hflags = INB(np, HF_PRT);
  3389. if (hflags & HF_DP_SAVED)
  3390. hflags ^= HF_ACT_PM;
  3391. if (!(hflags & HF_ACT_PM)) {
  3392. pm = &cp->phys.pm0;
  3393. dp_scr = SCRIPTA_BA(np, pm0_data);
  3394. }
  3395. else {
  3396. pm = &cp->phys.pm1;
  3397. dp_scr = SCRIPTA_BA(np, pm1_data);
  3398. }
  3399. hflags &= ~(HF_DP_SAVED);
  3400. OUTB(np, HF_PRT, hflags);
  3401. /*
  3402. * Set up the new current data pointer.
  3403. * ofs < 0 there, and for the next data phase, we
  3404. * want to transfer part of the data of the sg entry
  3405. * corresponding to index dp_sg-1 prior to returning
  3406. * to the main data script.
  3407. */
  3408. pm->ret = cpu_to_scr(dp_ret);
  3409. tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
  3410. tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
  3411. pm->sg.addr = cpu_to_scr(tmp);
  3412. pm->sg.size = cpu_to_scr(-dp_ofs);
  3413. out_ok:
  3414. sym_set_script_dp (np, cp, dp_scr);
  3415. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3416. return;
  3417. out_reject:
  3418. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3419. }
  3420. /*
  3421. * chip calculation of the data residual.
  3422. *
  3423. * As I used to say, the requirement of data residual
  3424. * in SCSI is broken, useless and cannot be achieved
  3425. * without huge complexity.
  3426. * But most OSes and even the official CAM require it.
  3427. * When stupidity happens to be so widely spread inside
  3428. * a community, it gets hard to convince.
  3429. *
  3430. * Anyway, I don't care, since I am not going to use
  3431. * any software that considers this data residual as
  3432. * a relevant information. :)
  3433. */
  3434. int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
  3435. {
  3436. int dp_sg, dp_sgmin, resid = 0;
  3437. int dp_ofs = 0;
  3438. /*
  3439. * Check for some data lost or just thrown away.
  3440. * We are not required to be quite accurate in this
  3441. * situation. Btw, if we are odd for output and the
  3442. * device claims some more data, it may well happen
  3443. * than our residual be zero. :-)
  3444. */
  3445. if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
  3446. if (cp->xerr_status & XE_EXTRA_DATA)
  3447. resid -= cp->extra_bytes;
  3448. if (cp->xerr_status & XE_SODL_UNRUN)
  3449. ++resid;
  3450. if (cp->xerr_status & XE_SWIDE_OVRUN)
  3451. --resid;
  3452. }
  3453. /*
  3454. * If all data has been transferred,
  3455. * there is no residual.
  3456. */
  3457. if (cp->phys.head.lastp == cp->goalp)
  3458. return resid;
  3459. /*
  3460. * If no data transfer occurs, or if the data
  3461. * pointer is weird, return full residual.
  3462. */
  3463. if (cp->startp == cp->phys.head.lastp ||
  3464. sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
  3465. &dp_ofs) < 0) {
  3466. return cp->data_len;
  3467. }
  3468. /*
  3469. * If we were auto-sensing, then we are done.
  3470. */
  3471. if (cp->host_flags & HF_SENSE) {
  3472. return -dp_ofs;
  3473. }
  3474. /*
  3475. * We are now full comfortable in the computation
  3476. * of the data residual (2's complement).
  3477. */
  3478. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3479. resid = -cp->ext_ofs;
  3480. for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
  3481. u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3482. resid += (tmp & 0xffffff);
  3483. }
  3484. resid -= cp->odd_byte_adjustment;
  3485. /*
  3486. * Hopefully, the result is not too wrong.
  3487. */
  3488. return resid;
  3489. }
  3490. /*
  3491. * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
  3492. *
  3493. * When we try to negotiate, we append the negotiation message
  3494. * to the identify and (maybe) simple tag message.
  3495. * The host status field is set to HS_NEGOTIATE to mark this
  3496. * situation.
  3497. *
  3498. * If the target doesn't answer this message immediately
  3499. * (as required by the standard), the SIR_NEGO_FAILED interrupt
  3500. * will be raised eventually.
  3501. * The handler removes the HS_NEGOTIATE status, and sets the
  3502. * negotiated value to the default (async / nowide).
  3503. *
  3504. * If we receive a matching answer immediately, we check it
  3505. * for validity, and set the values.
  3506. *
  3507. * If we receive a Reject message immediately, we assume the
  3508. * negotiation has failed, and fall back to standard values.
  3509. *
  3510. * If we receive a negotiation message while not in HS_NEGOTIATE
  3511. * state, it's a target initiated negotiation. We prepare a
  3512. * (hopefully) valid answer, set our parameters, and send back
  3513. * this answer to the target.
  3514. *
  3515. * If the target doesn't fetch the answer (no message out phase),
  3516. * we assume the negotiation has failed, and fall back to default
  3517. * settings (SIR_NEGO_PROTO interrupt).
  3518. *
  3519. * When we set the values, we adjust them in all ccbs belonging
  3520. * to this target, in the controller's register, and in the "phys"
  3521. * field of the controller's struct sym_hcb.
  3522. */
  3523. /*
  3524. * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
  3525. */
  3526. static int
  3527. sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3528. {
  3529. int target = cp->target;
  3530. u_char chg, ofs, per, fak, div;
  3531. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3532. sym_print_nego_msg(np, target, "sync msgin", np->msgin);
  3533. }
  3534. /*
  3535. * Get requested values.
  3536. */
  3537. chg = 0;
  3538. per = np->msgin[3];
  3539. ofs = np->msgin[4];
  3540. /*
  3541. * Check values against our limits.
  3542. */
  3543. if (ofs) {
  3544. if (ofs > np->maxoffs)
  3545. {chg = 1; ofs = np->maxoffs;}
  3546. }
  3547. if (ofs) {
  3548. if (per < np->minsync)
  3549. {chg = 1; per = np->minsync;}
  3550. }
  3551. /*
  3552. * Get new chip synchronous parameters value.
  3553. */
  3554. div = fak = 0;
  3555. if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
  3556. goto reject_it;
  3557. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3558. sym_print_addr(cp->cmd,
  3559. "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
  3560. ofs, per, div, fak, chg);
  3561. }
  3562. /*
  3563. * If it was an answer we want to change,
  3564. * then it isn't acceptable. Reject it.
  3565. */
  3566. if (!req && chg)
  3567. goto reject_it;
  3568. /*
  3569. * Apply new values.
  3570. */
  3571. sym_setsync (np, target, ofs, per, div, fak);
  3572. /*
  3573. * It was an answer. We are done.
  3574. */
  3575. if (!req)
  3576. return 0;
  3577. /*
  3578. * It was a request. Prepare an answer message.
  3579. */
  3580. spi_populate_sync_msg(np->msgout, per, ofs);
  3581. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3582. sym_print_nego_msg(np, target, "sync msgout", np->msgout);
  3583. }
  3584. np->msgin [0] = M_NOOP;
  3585. return 0;
  3586. reject_it:
  3587. sym_setsync (np, target, 0, 0, 0, 0);
  3588. return -1;
  3589. }
  3590. static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3591. {
  3592. int req = 1;
  3593. int result;
  3594. /*
  3595. * Request or answer ?
  3596. */
  3597. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3598. OUTB(np, HS_PRT, HS_BUSY);
  3599. if (cp->nego_status && cp->nego_status != NS_SYNC)
  3600. goto reject_it;
  3601. req = 0;
  3602. }
  3603. /*
  3604. * Check and apply new values.
  3605. */
  3606. result = sym_sync_nego_check(np, req, cp);
  3607. if (result) /* Not acceptable, reject it */
  3608. goto reject_it;
  3609. if (req) { /* Was a request, send response. */
  3610. cp->nego_status = NS_SYNC;
  3611. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3612. }
  3613. else /* Was a response, we are done. */
  3614. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3615. return;
  3616. reject_it:
  3617. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3618. }
  3619. /*
  3620. * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
  3621. */
  3622. static int
  3623. sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
  3624. {
  3625. struct sym_tcb *tp = &np->target[target];
  3626. unsigned char fak, div;
  3627. int dt, chg = 0;
  3628. unsigned char per = np->msgin[3];
  3629. unsigned char ofs = np->msgin[5];
  3630. unsigned char wide = np->msgin[6];
  3631. unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
  3632. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3633. sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
  3634. }
  3635. /*
  3636. * Check values against our limits.
  3637. */
  3638. if (wide > np->maxwide) {
  3639. chg = 1;
  3640. wide = np->maxwide;
  3641. }
  3642. if (!wide || !(np->features & FE_U3EN))
  3643. opts = 0;
  3644. if (opts != (np->msgin[7] & PPR_OPT_MASK))
  3645. chg = 1;
  3646. dt = opts & PPR_OPT_DT;
  3647. if (ofs) {
  3648. unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
  3649. if (ofs > maxoffs) {
  3650. chg = 1;
  3651. ofs = maxoffs;
  3652. }
  3653. }
  3654. if (ofs) {
  3655. unsigned char minsync = dt ? np->minsync_dt : np->minsync;
  3656. if (per < minsync) {
  3657. chg = 1;
  3658. per = minsync;
  3659. }
  3660. }
  3661. /*
  3662. * Get new chip synchronous parameters value.
  3663. */
  3664. div = fak = 0;
  3665. if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
  3666. goto reject_it;
  3667. /*
  3668. * If it was an answer we want to change,
  3669. * then it isn't acceptable. Reject it.
  3670. */
  3671. if (!req && chg)
  3672. goto reject_it;
  3673. /*
  3674. * Apply new values.
  3675. */
  3676. sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
  3677. /*
  3678. * It was an answer. We are done.
  3679. */
  3680. if (!req)
  3681. return 0;
  3682. /*
  3683. * It was a request. Prepare an answer message.
  3684. */
  3685. spi_populate_ppr_msg(np->msgout, per, ofs, wide, opts);
  3686. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3687. sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
  3688. }
  3689. np->msgin [0] = M_NOOP;
  3690. return 0;
  3691. reject_it:
  3692. sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
  3693. /*
  3694. * If it is a device response that should result in
  3695. * ST, we may want to try a legacy negotiation later.
  3696. */
  3697. if (!req && !opts) {
  3698. tp->tgoal.period = per;
  3699. tp->tgoal.offset = ofs;
  3700. tp->tgoal.width = wide;
  3701. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3702. tp->tgoal.check_nego = 1;
  3703. }
  3704. return -1;
  3705. }
  3706. static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3707. {
  3708. int req = 1;
  3709. int result;
  3710. /*
  3711. * Request or answer ?
  3712. */
  3713. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3714. OUTB(np, HS_PRT, HS_BUSY);
  3715. if (cp->nego_status && cp->nego_status != NS_PPR)
  3716. goto reject_it;
  3717. req = 0;
  3718. }
  3719. /*
  3720. * Check and apply new values.
  3721. */
  3722. result = sym_ppr_nego_check(np, req, cp->target);
  3723. if (result) /* Not acceptable, reject it */
  3724. goto reject_it;
  3725. if (req) { /* Was a request, send response. */
  3726. cp->nego_status = NS_PPR;
  3727. OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
  3728. }
  3729. else /* Was a response, we are done. */
  3730. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3731. return;
  3732. reject_it:
  3733. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3734. }
  3735. /*
  3736. * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
  3737. */
  3738. static int
  3739. sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3740. {
  3741. int target = cp->target;
  3742. u_char chg, wide;
  3743. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3744. sym_print_nego_msg(np, target, "wide msgin", np->msgin);
  3745. }
  3746. /*
  3747. * Get requested values.
  3748. */
  3749. chg = 0;
  3750. wide = np->msgin[3];
  3751. /*
  3752. * Check values against our limits.
  3753. */
  3754. if (wide > np->maxwide) {
  3755. chg = 1;
  3756. wide = np->maxwide;
  3757. }
  3758. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3759. sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
  3760. wide, chg);
  3761. }
  3762. /*
  3763. * If it was an answer we want to change,
  3764. * then it isn't acceptable. Reject it.
  3765. */
  3766. if (!req && chg)
  3767. goto reject_it;
  3768. /*
  3769. * Apply new values.
  3770. */
  3771. sym_setwide (np, target, wide);
  3772. /*
  3773. * It was an answer. We are done.
  3774. */
  3775. if (!req)
  3776. return 0;
  3777. /*
  3778. * It was a request. Prepare an answer message.
  3779. */
  3780. spi_populate_width_msg(np->msgout, wide);
  3781. np->msgin [0] = M_NOOP;
  3782. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3783. sym_print_nego_msg(np, target, "wide msgout", np->msgout);
  3784. }
  3785. return 0;
  3786. reject_it:
  3787. return -1;
  3788. }
  3789. static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3790. {
  3791. int req = 1;
  3792. int result;
  3793. /*
  3794. * Request or answer ?
  3795. */
  3796. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3797. OUTB(np, HS_PRT, HS_BUSY);
  3798. if (cp->nego_status && cp->nego_status != NS_WIDE)
  3799. goto reject_it;
  3800. req = 0;
  3801. }
  3802. /*
  3803. * Check and apply new values.
  3804. */
  3805. result = sym_wide_nego_check(np, req, cp);
  3806. if (result) /* Not acceptable, reject it */
  3807. goto reject_it;
  3808. if (req) { /* Was a request, send response. */
  3809. cp->nego_status = NS_WIDE;
  3810. OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
  3811. } else { /* Was a response. */
  3812. /*
  3813. * Negotiate for SYNC immediately after WIDE response.
  3814. * This allows to negotiate for both WIDE and SYNC on
  3815. * a single SCSI command (Suggested by Justin Gibbs).
  3816. */
  3817. if (tp->tgoal.offset) {
  3818. spi_populate_sync_msg(np->msgout, tp->tgoal.period,
  3819. tp->tgoal.offset);
  3820. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3821. sym_print_nego_msg(np, cp->target,
  3822. "sync msgout", np->msgout);
  3823. }
  3824. cp->nego_status = NS_SYNC;
  3825. OUTB(np, HS_PRT, HS_NEGOTIATE);
  3826. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3827. return;
  3828. } else
  3829. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3830. }
  3831. return;
  3832. reject_it:
  3833. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3834. }
  3835. /*
  3836. * Reset DT, SYNC or WIDE to default settings.
  3837. *
  3838. * Called when a negotiation does not succeed either
  3839. * on rejection or on protocol error.
  3840. *
  3841. * A target that understands a PPR message should never
  3842. * reject it, and messing with it is very unlikely.
  3843. * So, if a PPR makes problems, we may just want to
  3844. * try a legacy negotiation later.
  3845. */
  3846. static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3847. {
  3848. switch (cp->nego_status) {
  3849. case NS_PPR:
  3850. #if 0
  3851. sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
  3852. #else
  3853. if (tp->tgoal.period < np->minsync)
  3854. tp->tgoal.period = np->minsync;
  3855. if (tp->tgoal.offset > np->maxoffs)
  3856. tp->tgoal.offset = np->maxoffs;
  3857. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3858. tp->tgoal.check_nego = 1;
  3859. #endif
  3860. break;
  3861. case NS_SYNC:
  3862. sym_setsync (np, cp->target, 0, 0, 0, 0);
  3863. break;
  3864. case NS_WIDE:
  3865. sym_setwide (np, cp->target, 0);
  3866. break;
  3867. }
  3868. np->msgin [0] = M_NOOP;
  3869. np->msgout[0] = M_NOOP;
  3870. cp->nego_status = 0;
  3871. }
  3872. /*
  3873. * chip handler for MESSAGE REJECT received in response to
  3874. * PPR, WIDE or SYNCHRONOUS negotiation.
  3875. */
  3876. static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3877. {
  3878. sym_nego_default(np, tp, cp);
  3879. OUTB(np, HS_PRT, HS_BUSY);
  3880. }
  3881. /*
  3882. * chip exception handler for programmed interrupts.
  3883. */
  3884. static void sym_int_sir (struct sym_hcb *np)
  3885. {
  3886. u_char num = INB(np, nc_dsps);
  3887. u32 dsa = INL(np, nc_dsa);
  3888. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  3889. u_char target = INB(np, nc_sdid) & 0x0f;
  3890. struct sym_tcb *tp = &np->target[target];
  3891. int tmp;
  3892. if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
  3893. switch (num) {
  3894. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  3895. /*
  3896. * SCRIPTS tell us that we may have to update
  3897. * 64 bit DMA segment registers.
  3898. */
  3899. case SIR_DMAP_DIRTY:
  3900. sym_update_dmap_regs(np);
  3901. goto out;
  3902. #endif
  3903. /*
  3904. * Command has been completed with error condition
  3905. * or has been auto-sensed.
  3906. */
  3907. case SIR_COMPLETE_ERROR:
  3908. sym_complete_error(np, cp);
  3909. return;
  3910. /*
  3911. * The C code is currently trying to recover from something.
  3912. * Typically, user want to abort some command.
  3913. */
  3914. case SIR_SCRIPT_STOPPED:
  3915. case SIR_TARGET_SELECTED:
  3916. case SIR_ABORT_SENT:
  3917. sym_sir_task_recovery(np, num);
  3918. return;
  3919. /*
  3920. * The device didn't go to MSG OUT phase after having
  3921. * been selected with ATN. We donnot want to handle
  3922. * that.
  3923. */
  3924. case SIR_SEL_ATN_NO_MSG_OUT:
  3925. printf ("%s:%d: No MSG OUT phase after selection with ATN.\n",
  3926. sym_name (np), target);
  3927. goto out_stuck;
  3928. /*
  3929. * The device didn't switch to MSG IN phase after
  3930. * having reseleted the initiator.
  3931. */
  3932. case SIR_RESEL_NO_MSG_IN:
  3933. printf ("%s:%d: No MSG IN phase after reselection.\n",
  3934. sym_name (np), target);
  3935. goto out_stuck;
  3936. /*
  3937. * After reselection, the device sent a message that wasn't
  3938. * an IDENTIFY.
  3939. */
  3940. case SIR_RESEL_NO_IDENTIFY:
  3941. printf ("%s:%d: No IDENTIFY after reselection.\n",
  3942. sym_name (np), target);
  3943. goto out_stuck;
  3944. /*
  3945. * The device reselected a LUN we donnot know about.
  3946. */
  3947. case SIR_RESEL_BAD_LUN:
  3948. np->msgout[0] = M_RESET;
  3949. goto out;
  3950. /*
  3951. * The device reselected for an untagged nexus and we
  3952. * haven't any.
  3953. */
  3954. case SIR_RESEL_BAD_I_T_L:
  3955. np->msgout[0] = M_ABORT;
  3956. goto out;
  3957. /*
  3958. * The device reselected for a tagged nexus that we donnot
  3959. * have.
  3960. */
  3961. case SIR_RESEL_BAD_I_T_L_Q:
  3962. np->msgout[0] = M_ABORT_TAG;
  3963. goto out;
  3964. /*
  3965. * The SCRIPTS let us know that the device has grabbed
  3966. * our message and will abort the job.
  3967. */
  3968. case SIR_RESEL_ABORTED:
  3969. np->lastmsg = np->msgout[0];
  3970. np->msgout[0] = M_NOOP;
  3971. printf ("%s:%d: message %x sent on bad reselection.\n",
  3972. sym_name (np), target, np->lastmsg);
  3973. goto out;
  3974. /*
  3975. * The SCRIPTS let us know that a message has been
  3976. * successfully sent to the device.
  3977. */
  3978. case SIR_MSG_OUT_DONE:
  3979. np->lastmsg = np->msgout[0];
  3980. np->msgout[0] = M_NOOP;
  3981. /* Should we really care of that */
  3982. if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
  3983. if (cp) {
  3984. cp->xerr_status &= ~XE_PARITY_ERR;
  3985. if (!cp->xerr_status)
  3986. OUTOFFB(np, HF_PRT, HF_EXT_ERR);
  3987. }
  3988. }
  3989. goto out;
  3990. /*
  3991. * The device didn't send a GOOD SCSI status.
  3992. * We may have some work to do prior to allow
  3993. * the SCRIPTS processor to continue.
  3994. */
  3995. case SIR_BAD_SCSI_STATUS:
  3996. if (!cp)
  3997. goto out;
  3998. sym_sir_bad_scsi_status(np, num, cp);
  3999. return;
  4000. /*
  4001. * We are asked by the SCRIPTS to prepare a
  4002. * REJECT message.
  4003. */
  4004. case SIR_REJECT_TO_SEND:
  4005. sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
  4006. np->msgout[0] = M_REJECT;
  4007. goto out;
  4008. /*
  4009. * We have been ODD at the end of a DATA IN
  4010. * transfer and the device didn't send a
  4011. * IGNORE WIDE RESIDUE message.
  4012. * It is a data overrun condition.
  4013. */
  4014. case SIR_SWIDE_OVERRUN:
  4015. if (cp) {
  4016. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4017. cp->xerr_status |= XE_SWIDE_OVRUN;
  4018. }
  4019. goto out;
  4020. /*
  4021. * We have been ODD at the end of a DATA OUT
  4022. * transfer.
  4023. * It is a data underrun condition.
  4024. */
  4025. case SIR_SODL_UNDERRUN:
  4026. if (cp) {
  4027. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4028. cp->xerr_status |= XE_SODL_UNRUN;
  4029. }
  4030. goto out;
  4031. /*
  4032. * The device wants us to tranfer more data than
  4033. * expected or in the wrong direction.
  4034. * The number of extra bytes is in scratcha.
  4035. * It is a data overrun condition.
  4036. */
  4037. case SIR_DATA_OVERRUN:
  4038. if (cp) {
  4039. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4040. cp->xerr_status |= XE_EXTRA_DATA;
  4041. cp->extra_bytes += INL(np, nc_scratcha);
  4042. }
  4043. goto out;
  4044. /*
  4045. * The device switched to an illegal phase (4/5).
  4046. */
  4047. case SIR_BAD_PHASE:
  4048. if (cp) {
  4049. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4050. cp->xerr_status |= XE_BAD_PHASE;
  4051. }
  4052. goto out;
  4053. /*
  4054. * We received a message.
  4055. */
  4056. case SIR_MSG_RECEIVED:
  4057. if (!cp)
  4058. goto out_stuck;
  4059. switch (np->msgin [0]) {
  4060. /*
  4061. * We received an extended message.
  4062. * We handle MODIFY DATA POINTER, SDTR, WDTR
  4063. * and reject all other extended messages.
  4064. */
  4065. case M_EXTENDED:
  4066. switch (np->msgin [2]) {
  4067. case M_X_MODIFY_DP:
  4068. if (DEBUG_FLAGS & DEBUG_POINTER)
  4069. sym_print_msg(cp, NULL, np->msgin);
  4070. tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
  4071. (np->msgin[5]<<8) + (np->msgin[6]);
  4072. sym_modify_dp(np, tp, cp, tmp);
  4073. return;
  4074. case M_X_SYNC_REQ:
  4075. sym_sync_nego(np, tp, cp);
  4076. return;
  4077. case M_X_PPR_REQ:
  4078. sym_ppr_nego(np, tp, cp);
  4079. return;
  4080. case M_X_WIDE_REQ:
  4081. sym_wide_nego(np, tp, cp);
  4082. return;
  4083. default:
  4084. goto out_reject;
  4085. }
  4086. break;
  4087. /*
  4088. * We received a 1/2 byte message not handled from SCRIPTS.
  4089. * We are only expecting MESSAGE REJECT and IGNORE WIDE
  4090. * RESIDUE messages that haven't been anticipated by
  4091. * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
  4092. * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
  4093. */
  4094. case M_IGN_RESIDUE:
  4095. if (DEBUG_FLAGS & DEBUG_POINTER)
  4096. sym_print_msg(cp, NULL, np->msgin);
  4097. if (cp->host_flags & HF_SENSE)
  4098. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4099. else
  4100. sym_modify_dp(np, tp, cp, -1);
  4101. return;
  4102. case M_REJECT:
  4103. if (INB(np, HS_PRT) == HS_NEGOTIATE)
  4104. sym_nego_rejected(np, tp, cp);
  4105. else {
  4106. sym_print_addr(cp->cmd,
  4107. "M_REJECT received (%x:%x).\n",
  4108. scr_to_cpu(np->lastmsg), np->msgout[0]);
  4109. }
  4110. goto out_clrack;
  4111. break;
  4112. default:
  4113. goto out_reject;
  4114. }
  4115. break;
  4116. /*
  4117. * We received an unknown message.
  4118. * Ignore all MSG IN phases and reject it.
  4119. */
  4120. case SIR_MSG_WEIRD:
  4121. sym_print_msg(cp, "WEIRD message received", np->msgin);
  4122. OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
  4123. return;
  4124. /*
  4125. * Negotiation failed.
  4126. * Target does not send us the reply.
  4127. * Remove the HS_NEGOTIATE status.
  4128. */
  4129. case SIR_NEGO_FAILED:
  4130. OUTB(np, HS_PRT, HS_BUSY);
  4131. /*
  4132. * Negotiation failed.
  4133. * Target does not want answer message.
  4134. */
  4135. case SIR_NEGO_PROTO:
  4136. sym_nego_default(np, tp, cp);
  4137. goto out;
  4138. }
  4139. out:
  4140. OUTONB_STD();
  4141. return;
  4142. out_reject:
  4143. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  4144. return;
  4145. out_clrack:
  4146. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4147. return;
  4148. out_stuck:
  4149. return;
  4150. }
  4151. /*
  4152. * Acquire a control block
  4153. */
  4154. struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
  4155. {
  4156. u_char tn = cmd->device->id;
  4157. u_char ln = cmd->device->lun;
  4158. struct sym_tcb *tp = &np->target[tn];
  4159. struct sym_lcb *lp = sym_lp(tp, ln);
  4160. u_short tag = NO_TAG;
  4161. SYM_QUEHEAD *qp;
  4162. struct sym_ccb *cp = NULL;
  4163. /*
  4164. * Look for a free CCB
  4165. */
  4166. if (sym_que_empty(&np->free_ccbq))
  4167. sym_alloc_ccb(np);
  4168. qp = sym_remque_head(&np->free_ccbq);
  4169. if (!qp)
  4170. goto out;
  4171. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4172. {
  4173. /*
  4174. * If we have been asked for a tagged command.
  4175. */
  4176. if (tag_order) {
  4177. /*
  4178. * Debugging purpose.
  4179. */
  4180. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4181. if (lp->busy_itl != 0)
  4182. goto out_free;
  4183. #endif
  4184. /*
  4185. * Allocate resources for tags if not yet.
  4186. */
  4187. if (!lp->cb_tags) {
  4188. sym_alloc_lcb_tags(np, tn, ln);
  4189. if (!lp->cb_tags)
  4190. goto out_free;
  4191. }
  4192. /*
  4193. * Get a tag for this SCSI IO and set up
  4194. * the CCB bus address for reselection,
  4195. * and count it for this LUN.
  4196. * Toggle reselect path to tagged.
  4197. */
  4198. if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
  4199. tag = lp->cb_tags[lp->ia_tag];
  4200. if (++lp->ia_tag == SYM_CONF_MAX_TASK)
  4201. lp->ia_tag = 0;
  4202. ++lp->busy_itlq;
  4203. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4204. lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
  4205. lp->head.resel_sa =
  4206. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  4207. #endif
  4208. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4209. cp->tags_si = lp->tags_si;
  4210. ++lp->tags_sum[cp->tags_si];
  4211. ++lp->tags_since;
  4212. #endif
  4213. }
  4214. else
  4215. goto out_free;
  4216. }
  4217. /*
  4218. * This command will not be tagged.
  4219. * If we already have either a tagged or untagged
  4220. * one, refuse to overlap this untagged one.
  4221. */
  4222. else {
  4223. /*
  4224. * Debugging purpose.
  4225. */
  4226. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4227. if (lp->busy_itl != 0 || lp->busy_itlq != 0)
  4228. goto out_free;
  4229. #endif
  4230. /*
  4231. * Count this nexus for this LUN.
  4232. * Set up the CCB bus address for reselection.
  4233. * Toggle reselect path to untagged.
  4234. */
  4235. ++lp->busy_itl;
  4236. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4237. if (lp->busy_itl == 1) {
  4238. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  4239. lp->head.resel_sa =
  4240. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  4241. }
  4242. else
  4243. goto out_free;
  4244. #endif
  4245. }
  4246. }
  4247. /*
  4248. * Put the CCB into the busy queue.
  4249. */
  4250. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  4251. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4252. if (lp) {
  4253. sym_remque(&cp->link2_ccbq);
  4254. sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
  4255. }
  4256. #endif
  4257. cp->to_abort = 0;
  4258. cp->odd_byte_adjustment = 0;
  4259. cp->tag = tag;
  4260. cp->order = tag_order;
  4261. cp->target = tn;
  4262. cp->lun = ln;
  4263. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4264. sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
  4265. }
  4266. out:
  4267. return cp;
  4268. out_free:
  4269. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4270. return NULL;
  4271. }
  4272. /*
  4273. * Release one control block
  4274. */
  4275. void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
  4276. {
  4277. struct sym_tcb *tp = &np->target[cp->target];
  4278. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  4279. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4280. sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
  4281. cp, cp->tag);
  4282. }
  4283. /*
  4284. * If LCB available,
  4285. */
  4286. if (lp) {
  4287. /*
  4288. * If tagged, release the tag, set the relect path
  4289. */
  4290. if (cp->tag != NO_TAG) {
  4291. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4292. --lp->tags_sum[cp->tags_si];
  4293. #endif
  4294. /*
  4295. * Free the tag value.
  4296. */
  4297. lp->cb_tags[lp->if_tag] = cp->tag;
  4298. if (++lp->if_tag == SYM_CONF_MAX_TASK)
  4299. lp->if_tag = 0;
  4300. /*
  4301. * Make the reselect path invalid,
  4302. * and uncount this CCB.
  4303. */
  4304. lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
  4305. --lp->busy_itlq;
  4306. } else { /* Untagged */
  4307. /*
  4308. * Make the reselect path invalid,
  4309. * and uncount this CCB.
  4310. */
  4311. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4312. --lp->busy_itl;
  4313. }
  4314. /*
  4315. * If no JOB active, make the LUN reselect path invalid.
  4316. */
  4317. if (lp->busy_itlq == 0 && lp->busy_itl == 0)
  4318. lp->head.resel_sa =
  4319. cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4320. }
  4321. /*
  4322. * We donnot queue more than 1 ccb per target
  4323. * with negotiation at any time. If this ccb was
  4324. * used for negotiation, clear this info in the tcb.
  4325. */
  4326. if (cp == tp->nego_cp)
  4327. tp->nego_cp = NULL;
  4328. #ifdef SYM_CONF_IARB_SUPPORT
  4329. /*
  4330. * If we just complete the last queued CCB,
  4331. * clear this info that is no longer relevant.
  4332. */
  4333. if (cp == np->last_cp)
  4334. np->last_cp = 0;
  4335. #endif
  4336. /*
  4337. * Make this CCB available.
  4338. */
  4339. cp->cmd = NULL;
  4340. cp->host_status = HS_IDLE;
  4341. sym_remque(&cp->link_ccbq);
  4342. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4343. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4344. if (lp) {
  4345. sym_remque(&cp->link2_ccbq);
  4346. sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
  4347. if (cp->started) {
  4348. if (cp->tag != NO_TAG)
  4349. --lp->started_tags;
  4350. else
  4351. --lp->started_no_tag;
  4352. }
  4353. }
  4354. cp->started = 0;
  4355. #endif
  4356. }
  4357. /*
  4358. * Allocate a CCB from memory and initialize its fixed part.
  4359. */
  4360. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
  4361. {
  4362. struct sym_ccb *cp = NULL;
  4363. int hcode;
  4364. /*
  4365. * Prevent from allocating more CCBs than we can
  4366. * queue to the controller.
  4367. */
  4368. if (np->actccbs >= SYM_CONF_MAX_START)
  4369. return NULL;
  4370. /*
  4371. * Allocate memory for this CCB.
  4372. */
  4373. cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
  4374. if (!cp)
  4375. goto out_free;
  4376. /*
  4377. * Count it.
  4378. */
  4379. np->actccbs++;
  4380. /*
  4381. * Compute the bus address of this ccb.
  4382. */
  4383. cp->ccb_ba = vtobus(cp);
  4384. /*
  4385. * Insert this ccb into the hashed list.
  4386. */
  4387. hcode = CCB_HASH_CODE(cp->ccb_ba);
  4388. cp->link_ccbh = np->ccbh[hcode];
  4389. np->ccbh[hcode] = cp;
  4390. /*
  4391. * Initialyze the start and restart actions.
  4392. */
  4393. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  4394. cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  4395. /*
  4396. * Initilialyze some other fields.
  4397. */
  4398. cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
  4399. /*
  4400. * Chain into free ccb queue.
  4401. */
  4402. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4403. /*
  4404. * Chain into optionnal lists.
  4405. */
  4406. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4407. sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
  4408. #endif
  4409. return cp;
  4410. out_free:
  4411. if (cp)
  4412. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  4413. return NULL;
  4414. }
  4415. /*
  4416. * Look up a CCB from a DSA value.
  4417. */
  4418. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
  4419. {
  4420. int hcode;
  4421. struct sym_ccb *cp;
  4422. hcode = CCB_HASH_CODE(dsa);
  4423. cp = np->ccbh[hcode];
  4424. while (cp) {
  4425. if (cp->ccb_ba == dsa)
  4426. break;
  4427. cp = cp->link_ccbh;
  4428. }
  4429. return cp;
  4430. }
  4431. /*
  4432. * Target control block initialisation.
  4433. * Nothing important to do at the moment.
  4434. */
  4435. static void sym_init_tcb (struct sym_hcb *np, u_char tn)
  4436. {
  4437. #if 0 /* Hmmm... this checking looks paranoid. */
  4438. /*
  4439. * Check some alignments required by the chip.
  4440. */
  4441. assert (((offsetof(struct sym_reg, nc_sxfer) ^
  4442. offsetof(struct sym_tcb, head.sval)) &3) == 0);
  4443. assert (((offsetof(struct sym_reg, nc_scntl3) ^
  4444. offsetof(struct sym_tcb, head.wval)) &3) == 0);
  4445. #endif
  4446. }
  4447. /*
  4448. * Lun control block allocation and initialization.
  4449. */
  4450. struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
  4451. {
  4452. struct sym_tcb *tp = &np->target[tn];
  4453. struct sym_lcb *lp = NULL;
  4454. /*
  4455. * Initialize the target control block if not yet.
  4456. */
  4457. sym_init_tcb (np, tn);
  4458. /*
  4459. * Allocate the LCB bus address array.
  4460. * Compute the bus address of this table.
  4461. */
  4462. if (ln && !tp->luntbl) {
  4463. int i;
  4464. tp->luntbl = sym_calloc_dma(256, "LUNTBL");
  4465. if (!tp->luntbl)
  4466. goto fail;
  4467. for (i = 0 ; i < 64 ; i++)
  4468. tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  4469. tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
  4470. }
  4471. /*
  4472. * Allocate the table of pointers for LUN(s) > 0, if needed.
  4473. */
  4474. if (ln && !tp->lunmp) {
  4475. tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
  4476. GFP_KERNEL);
  4477. if (!tp->lunmp)
  4478. goto fail;
  4479. }
  4480. /*
  4481. * Allocate the lcb.
  4482. * Make it available to the chip.
  4483. */
  4484. lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
  4485. if (!lp)
  4486. goto fail;
  4487. if (ln) {
  4488. tp->lunmp[ln] = lp;
  4489. tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
  4490. }
  4491. else {
  4492. tp->lun0p = lp;
  4493. tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
  4494. }
  4495. /*
  4496. * Let the itl task point to error handling.
  4497. */
  4498. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4499. /*
  4500. * Set the reselect pattern to our default. :)
  4501. */
  4502. lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4503. /*
  4504. * Set user capabilities.
  4505. */
  4506. lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  4507. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4508. /*
  4509. * Initialize device queueing.
  4510. */
  4511. sym_que_init(&lp->waiting_ccbq);
  4512. sym_que_init(&lp->started_ccbq);
  4513. lp->started_max = SYM_CONF_MAX_TASK;
  4514. lp->started_limit = SYM_CONF_MAX_TASK;
  4515. #endif
  4516. fail:
  4517. return lp;
  4518. }
  4519. /*
  4520. * Allocate LCB resources for tagged command queuing.
  4521. */
  4522. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
  4523. {
  4524. struct sym_tcb *tp = &np->target[tn];
  4525. struct sym_lcb *lp = sym_lp(tp, ln);
  4526. int i;
  4527. /*
  4528. * Allocate the task table and and the tag allocation
  4529. * circular buffer. We want both or none.
  4530. */
  4531. lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4532. if (!lp->itlq_tbl)
  4533. goto fail;
  4534. lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
  4535. if (!lp->cb_tags) {
  4536. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4537. lp->itlq_tbl = NULL;
  4538. goto fail;
  4539. }
  4540. /*
  4541. * Initialize the task table with invalid entries.
  4542. */
  4543. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4544. lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
  4545. /*
  4546. * Fill up the tag buffer with tag numbers.
  4547. */
  4548. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4549. lp->cb_tags[i] = i;
  4550. /*
  4551. * Make the task table available to SCRIPTS,
  4552. * And accept tagged commands now.
  4553. */
  4554. lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
  4555. return;
  4556. fail:
  4557. return;
  4558. }
  4559. /*
  4560. * Queue a SCSI IO to the controller.
  4561. */
  4562. int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  4563. {
  4564. struct scsi_device *sdev = cmd->device;
  4565. struct sym_tcb *tp;
  4566. struct sym_lcb *lp;
  4567. u_char *msgptr;
  4568. u_int msglen;
  4569. int can_disconnect;
  4570. /*
  4571. * Keep track of the IO in our CCB.
  4572. */
  4573. cp->cmd = cmd;
  4574. /*
  4575. * Retrieve the target descriptor.
  4576. */
  4577. tp = &np->target[cp->target];
  4578. /*
  4579. * Retrieve the lun descriptor.
  4580. */
  4581. lp = sym_lp(tp, sdev->lun);
  4582. can_disconnect = (cp->tag != NO_TAG) ||
  4583. (lp && (lp->curr_flags & SYM_DISC_ENABLED));
  4584. msgptr = cp->scsi_smsg;
  4585. msglen = 0;
  4586. msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
  4587. /*
  4588. * Build the tag message if present.
  4589. */
  4590. if (cp->tag != NO_TAG) {
  4591. u_char order = cp->order;
  4592. switch(order) {
  4593. case M_ORDERED_TAG:
  4594. break;
  4595. case M_HEAD_TAG:
  4596. break;
  4597. default:
  4598. order = M_SIMPLE_TAG;
  4599. }
  4600. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4601. /*
  4602. * Avoid too much reordering of SCSI commands.
  4603. * The algorithm tries to prevent completion of any
  4604. * tagged command from being delayed against more
  4605. * than 3 times the max number of queued commands.
  4606. */
  4607. if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
  4608. lp->tags_si = !(lp->tags_si);
  4609. if (lp->tags_sum[lp->tags_si]) {
  4610. order = M_ORDERED_TAG;
  4611. if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
  4612. sym_print_addr(cmd,
  4613. "ordered tag forced.\n");
  4614. }
  4615. }
  4616. lp->tags_since = 0;
  4617. }
  4618. #endif
  4619. msgptr[msglen++] = order;
  4620. /*
  4621. * For less than 128 tags, actual tags are numbered
  4622. * 1,3,5,..2*MAXTAGS+1,since we may have to deal
  4623. * with devices that have problems with #TAG 0 or too
  4624. * great #TAG numbers. For more tags (up to 256),
  4625. * we use directly our tag number.
  4626. */
  4627. #if SYM_CONF_MAX_TASK > (512/4)
  4628. msgptr[msglen++] = cp->tag;
  4629. #else
  4630. msgptr[msglen++] = (cp->tag << 1) + 1;
  4631. #endif
  4632. }
  4633. /*
  4634. * Build a negotiation message if needed.
  4635. * (nego_status is filled by sym_prepare_nego())
  4636. */
  4637. cp->nego_status = 0;
  4638. if (tp->tgoal.check_nego && !tp->nego_cp && lp) {
  4639. msglen += sym_prepare_nego(np, cp, msgptr + msglen);
  4640. }
  4641. /*
  4642. * Startqueue
  4643. */
  4644. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  4645. cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
  4646. /*
  4647. * select
  4648. */
  4649. cp->phys.select.sel_id = cp->target;
  4650. cp->phys.select.sel_scntl3 = tp->head.wval;
  4651. cp->phys.select.sel_sxfer = tp->head.sval;
  4652. cp->phys.select.sel_scntl4 = tp->head.uval;
  4653. /*
  4654. * message
  4655. */
  4656. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
  4657. cp->phys.smsg.size = cpu_to_scr(msglen);
  4658. /*
  4659. * status
  4660. */
  4661. cp->host_xflags = 0;
  4662. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  4663. cp->ssss_status = S_ILLEGAL;
  4664. cp->xerr_status = 0;
  4665. cp->host_flags = 0;
  4666. cp->extra_bytes = 0;
  4667. /*
  4668. * extreme data pointer.
  4669. * shall be positive, so -1 is lower than lowest.:)
  4670. */
  4671. cp->ext_sg = -1;
  4672. cp->ext_ofs = 0;
  4673. /*
  4674. * Build the CDB and DATA descriptor block
  4675. * and start the IO.
  4676. */
  4677. return sym_setup_data_and_start(np, cmd, cp);
  4678. }
  4679. /*
  4680. * Reset a SCSI target (all LUNs of this target).
  4681. */
  4682. int sym_reset_scsi_target(struct sym_hcb *np, int target)
  4683. {
  4684. struct sym_tcb *tp;
  4685. if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
  4686. return -1;
  4687. tp = &np->target[target];
  4688. tp->to_reset = 1;
  4689. np->istat_sem = SEM;
  4690. OUTB(np, nc_istat, SIGP|SEM);
  4691. return 0;
  4692. }
  4693. /*
  4694. * Abort a SCSI IO.
  4695. */
  4696. static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
  4697. {
  4698. /*
  4699. * Check that the IO is active.
  4700. */
  4701. if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
  4702. return -1;
  4703. /*
  4704. * If a previous abort didn't succeed in time,
  4705. * perform a BUS reset.
  4706. */
  4707. if (cp->to_abort) {
  4708. sym_reset_scsi_bus(np, 1);
  4709. return 0;
  4710. }
  4711. /*
  4712. * Mark the CCB for abort and allow time for.
  4713. */
  4714. cp->to_abort = timed_out ? 2 : 1;
  4715. /*
  4716. * Tell the SCRIPTS processor to stop and synchronize with us.
  4717. */
  4718. np->istat_sem = SEM;
  4719. OUTB(np, nc_istat, SIGP|SEM);
  4720. return 0;
  4721. }
  4722. int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
  4723. {
  4724. struct sym_ccb *cp;
  4725. SYM_QUEHEAD *qp;
  4726. /*
  4727. * Look up our CCB control block.
  4728. */
  4729. cp = NULL;
  4730. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  4731. struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4732. if (cp2->cmd == cmd) {
  4733. cp = cp2;
  4734. break;
  4735. }
  4736. }
  4737. return sym_abort_ccb(np, cp, timed_out);
  4738. }
  4739. /*
  4740. * Complete execution of a SCSI command with extended
  4741. * error, SCSI status error, or having been auto-sensed.
  4742. *
  4743. * The SCRIPTS processor is not running there, so we
  4744. * can safely access IO registers and remove JOBs from
  4745. * the START queue.
  4746. * SCRATCHA is assumed to have been loaded with STARTPOS
  4747. * before the SCRIPTS called the C code.
  4748. */
  4749. void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
  4750. {
  4751. struct scsi_device *sdev;
  4752. struct scsi_cmnd *cmd;
  4753. struct sym_tcb *tp;
  4754. struct sym_lcb *lp;
  4755. int resid;
  4756. int i;
  4757. /*
  4758. * Paranoid check. :)
  4759. */
  4760. if (!cp || !cp->cmd)
  4761. return;
  4762. cmd = cp->cmd;
  4763. sdev = cmd->device;
  4764. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
  4765. dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
  4766. cp->host_status, cp->ssss_status, cp->host_flags);
  4767. }
  4768. /*
  4769. * Get target and lun pointers.
  4770. */
  4771. tp = &np->target[cp->target];
  4772. lp = sym_lp(tp, sdev->lun);
  4773. /*
  4774. * Check for extended errors.
  4775. */
  4776. if (cp->xerr_status) {
  4777. if (sym_verbose)
  4778. sym_print_xerr(cmd, cp->xerr_status);
  4779. if (cp->host_status == HS_COMPLETE)
  4780. cp->host_status = HS_COMP_ERR;
  4781. }
  4782. /*
  4783. * Calculate the residual.
  4784. */
  4785. resid = sym_compute_residual(np, cp);
  4786. if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
  4787. resid = 0; /* throw them away. :) */
  4788. cp->sv_resid = 0;
  4789. }
  4790. #ifdef DEBUG_2_0_X
  4791. if (resid)
  4792. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4793. #endif
  4794. /*
  4795. * Dequeue all queued CCBs for that device
  4796. * not yet started by SCRIPTS.
  4797. */
  4798. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  4799. i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
  4800. /*
  4801. * Restart the SCRIPTS processor.
  4802. */
  4803. OUTL_DSP(np, SCRIPTA_BA(np, start));
  4804. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4805. if (cp->host_status == HS_COMPLETE &&
  4806. cp->ssss_status == S_QUEUE_FULL) {
  4807. if (!lp || lp->started_tags - i < 2)
  4808. goto weirdness;
  4809. /*
  4810. * Decrease queue depth as needed.
  4811. */
  4812. lp->started_max = lp->started_tags - i - 1;
  4813. lp->num_sgood = 0;
  4814. if (sym_verbose >= 2) {
  4815. sym_print_addr(cmd, " queue depth is now %d\n",
  4816. lp->started_max);
  4817. }
  4818. /*
  4819. * Repair the CCB.
  4820. */
  4821. cp->host_status = HS_BUSY;
  4822. cp->ssss_status = S_ILLEGAL;
  4823. /*
  4824. * Let's requeue it to device.
  4825. */
  4826. sym_set_cam_status(cmd, DID_SOFT_ERROR);
  4827. goto finish;
  4828. }
  4829. weirdness:
  4830. #endif
  4831. /*
  4832. * Build result in CAM ccb.
  4833. */
  4834. sym_set_cam_result_error(np, cp, resid);
  4835. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4836. finish:
  4837. #endif
  4838. /*
  4839. * Add this one to the COMP queue.
  4840. */
  4841. sym_remque(&cp->link_ccbq);
  4842. sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
  4843. /*
  4844. * Complete all those commands with either error
  4845. * or requeue condition.
  4846. */
  4847. sym_flush_comp_queue(np, 0);
  4848. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4849. /*
  4850. * Donnot start more than 1 command after an error.
  4851. */
  4852. sym_start_next_ccbs(np, lp, 1);
  4853. #endif
  4854. }
  4855. /*
  4856. * Complete execution of a successful SCSI command.
  4857. *
  4858. * Only successful commands go to the DONE queue,
  4859. * since we need to have the SCRIPTS processor
  4860. * stopped on any error condition.
  4861. * The SCRIPTS processor is running while we are
  4862. * completing successful commands.
  4863. */
  4864. void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
  4865. {
  4866. struct sym_tcb *tp;
  4867. struct sym_lcb *lp;
  4868. struct scsi_cmnd *cmd;
  4869. int resid;
  4870. /*
  4871. * Paranoid check. :)
  4872. */
  4873. if (!cp || !cp->cmd)
  4874. return;
  4875. assert (cp->host_status == HS_COMPLETE);
  4876. /*
  4877. * Get user command.
  4878. */
  4879. cmd = cp->cmd;
  4880. /*
  4881. * Get target and lun pointers.
  4882. */
  4883. tp = &np->target[cp->target];
  4884. lp = sym_lp(tp, cp->lun);
  4885. /*
  4886. * If all data have been transferred, given than no
  4887. * extended error did occur, there is no residual.
  4888. */
  4889. resid = 0;
  4890. if (cp->phys.head.lastp != cp->goalp)
  4891. resid = sym_compute_residual(np, cp);
  4892. /*
  4893. * Wrong transfer residuals may be worse than just always
  4894. * returning zero. User can disable this feature in
  4895. * sym53c8xx.h. Residual support is enabled by default.
  4896. */
  4897. if (!SYM_SETUP_RESIDUAL_SUPPORT)
  4898. resid = 0;
  4899. #ifdef DEBUG_2_0_X
  4900. if (resid)
  4901. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4902. #endif
  4903. /*
  4904. * Build result in CAM ccb.
  4905. */
  4906. sym_set_cam_result_ok(cp, cmd, resid);
  4907. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4908. /*
  4909. * If max number of started ccbs had been reduced,
  4910. * increase it if 200 good status received.
  4911. */
  4912. if (lp && lp->started_max < lp->started_limit) {
  4913. ++lp->num_sgood;
  4914. if (lp->num_sgood >= 200) {
  4915. lp->num_sgood = 0;
  4916. ++lp->started_max;
  4917. if (sym_verbose >= 2) {
  4918. sym_print_addr(cmd, " queue depth is now %d\n",
  4919. lp->started_max);
  4920. }
  4921. }
  4922. }
  4923. #endif
  4924. /*
  4925. * Free our CCB.
  4926. */
  4927. sym_free_ccb (np, cp);
  4928. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4929. /*
  4930. * Requeue a couple of awaiting scsi commands.
  4931. */
  4932. if (!sym_que_empty(&lp->waiting_ccbq))
  4933. sym_start_next_ccbs(np, lp, 2);
  4934. #endif
  4935. /*
  4936. * Complete the command.
  4937. */
  4938. sym_xpt_done(np, cmd);
  4939. }
  4940. /*
  4941. * Soft-attach the controller.
  4942. */
  4943. int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
  4944. {
  4945. struct sym_hcb *np = sym_get_hcb(shost);
  4946. int i;
  4947. /*
  4948. * Get some info about the firmware.
  4949. */
  4950. np->scripta_sz = fw->a_size;
  4951. np->scriptb_sz = fw->b_size;
  4952. np->scriptz_sz = fw->z_size;
  4953. np->fw_setup = fw->setup;
  4954. np->fw_patch = fw->patch;
  4955. np->fw_name = fw->name;
  4956. /*
  4957. * Save setting of some IO registers, so we will
  4958. * be able to probe specific implementations.
  4959. */
  4960. sym_save_initial_setting (np);
  4961. /*
  4962. * Reset the chip now, since it has been reported
  4963. * that SCSI clock calibration may not work properly
  4964. * if the chip is currently active.
  4965. */
  4966. sym_chip_reset(np);
  4967. /*
  4968. * Prepare controller and devices settings, according
  4969. * to chip features, user set-up and driver set-up.
  4970. */
  4971. sym_prepare_setting(shost, np, nvram);
  4972. /*
  4973. * Check the PCI clock frequency.
  4974. * Must be performed after prepare_setting since it destroys
  4975. * STEST1 that is used to probe for the clock doubler.
  4976. */
  4977. i = sym_getpciclock(np);
  4978. if (i > 37000 && !(np->features & FE_66MHZ))
  4979. printf("%s: PCI BUS clock seems too high: %u KHz.\n",
  4980. sym_name(np), i);
  4981. /*
  4982. * Allocate the start queue.
  4983. */
  4984. np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
  4985. if (!np->squeue)
  4986. goto attach_failed;
  4987. np->squeue_ba = vtobus(np->squeue);
  4988. /*
  4989. * Allocate the done queue.
  4990. */
  4991. np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
  4992. if (!np->dqueue)
  4993. goto attach_failed;
  4994. np->dqueue_ba = vtobus(np->dqueue);
  4995. /*
  4996. * Allocate the target bus address array.
  4997. */
  4998. np->targtbl = sym_calloc_dma(256, "TARGTBL");
  4999. if (!np->targtbl)
  5000. goto attach_failed;
  5001. np->targtbl_ba = vtobus(np->targtbl);
  5002. /*
  5003. * Allocate SCRIPTS areas.
  5004. */
  5005. np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
  5006. np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
  5007. np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
  5008. if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
  5009. goto attach_failed;
  5010. /*
  5011. * Allocate the array of lists of CCBs hashed by DSA.
  5012. */
  5013. np->ccbh = kcalloc(sizeof(struct sym_ccb **), CCB_HASH_SIZE, GFP_KERNEL);
  5014. if (!np->ccbh)
  5015. goto attach_failed;
  5016. /*
  5017. * Initialyze the CCB free and busy queues.
  5018. */
  5019. sym_que_init(&np->free_ccbq);
  5020. sym_que_init(&np->busy_ccbq);
  5021. sym_que_init(&np->comp_ccbq);
  5022. /*
  5023. * Initialization for optional handling
  5024. * of device queueing.
  5025. */
  5026. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5027. sym_que_init(&np->dummy_ccbq);
  5028. #endif
  5029. /*
  5030. * Allocate some CCB. We need at least ONE.
  5031. */
  5032. if (!sym_alloc_ccb(np))
  5033. goto attach_failed;
  5034. /*
  5035. * Calculate BUS addresses where we are going
  5036. * to load the SCRIPTS.
  5037. */
  5038. np->scripta_ba = vtobus(np->scripta0);
  5039. np->scriptb_ba = vtobus(np->scriptb0);
  5040. np->scriptz_ba = vtobus(np->scriptz0);
  5041. if (np->ram_ba) {
  5042. np->scripta_ba = np->ram_ba;
  5043. if (np->features & FE_RAM8K) {
  5044. np->ram_ws = 8192;
  5045. np->scriptb_ba = np->scripta_ba + 4096;
  5046. #if 0 /* May get useful for 64 BIT PCI addressing */
  5047. np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
  5048. #endif
  5049. }
  5050. else
  5051. np->ram_ws = 4096;
  5052. }
  5053. /*
  5054. * Copy scripts to controller instance.
  5055. */
  5056. memcpy(np->scripta0, fw->a_base, np->scripta_sz);
  5057. memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
  5058. memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
  5059. /*
  5060. * Setup variable parts in scripts and compute
  5061. * scripts bus addresses used from the C code.
  5062. */
  5063. np->fw_setup(np, fw);
  5064. /*
  5065. * Bind SCRIPTS with physical addresses usable by the
  5066. * SCRIPTS processor (as seen from the BUS = BUS addresses).
  5067. */
  5068. sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
  5069. sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
  5070. sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
  5071. #ifdef SYM_CONF_IARB_SUPPORT
  5072. /*
  5073. * If user wants IARB to be set when we win arbitration
  5074. * and have other jobs, compute the max number of consecutive
  5075. * settings of IARB hints before we leave devices a chance to
  5076. * arbitrate for reselection.
  5077. */
  5078. #ifdef SYM_SETUP_IARB_MAX
  5079. np->iarb_max = SYM_SETUP_IARB_MAX;
  5080. #else
  5081. np->iarb_max = 4;
  5082. #endif
  5083. #endif
  5084. /*
  5085. * Prepare the idle and invalid task actions.
  5086. */
  5087. np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5088. np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5089. np->idletask_ba = vtobus(&np->idletask);
  5090. np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5091. np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5092. np->notask_ba = vtobus(&np->notask);
  5093. np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5094. np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5095. np->bad_itl_ba = vtobus(&np->bad_itl);
  5096. np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5097. np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
  5098. np->bad_itlq_ba = vtobus(&np->bad_itlq);
  5099. /*
  5100. * Allocate and prepare the lun JUMP table that is used
  5101. * for a target prior the probing of devices (bad lun table).
  5102. * A private table will be allocated for the target on the
  5103. * first INQUIRY response received.
  5104. */
  5105. np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
  5106. if (!np->badluntbl)
  5107. goto attach_failed;
  5108. np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  5109. for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
  5110. np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  5111. /*
  5112. * Prepare the bus address array that contains the bus
  5113. * address of each target control block.
  5114. * For now, assume all logical units are wrong. :)
  5115. */
  5116. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  5117. np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
  5118. np->target[i].head.luntbl_sa =
  5119. cpu_to_scr(vtobus(np->badluntbl));
  5120. np->target[i].head.lun0_sa =
  5121. cpu_to_scr(vtobus(&np->badlun_sa));
  5122. }
  5123. /*
  5124. * Now check the cache handling of the pci chipset.
  5125. */
  5126. if (sym_snooptest (np)) {
  5127. printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
  5128. goto attach_failed;
  5129. }
  5130. /*
  5131. * Sigh! we are done.
  5132. */
  5133. return 0;
  5134. attach_failed:
  5135. return -ENXIO;
  5136. }
  5137. /*
  5138. * Free everything that has been allocated for this device.
  5139. */
  5140. void sym_hcb_free(struct sym_hcb *np)
  5141. {
  5142. SYM_QUEHEAD *qp;
  5143. struct sym_ccb *cp;
  5144. struct sym_tcb *tp;
  5145. int target;
  5146. if (np->scriptz0)
  5147. sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
  5148. if (np->scriptb0)
  5149. sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
  5150. if (np->scripta0)
  5151. sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
  5152. if (np->squeue)
  5153. sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
  5154. if (np->dqueue)
  5155. sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
  5156. if (np->actccbs) {
  5157. while ((qp = sym_remque_head(&np->free_ccbq)) != 0) {
  5158. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  5159. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  5160. }
  5161. }
  5162. kfree(np->ccbh);
  5163. if (np->badluntbl)
  5164. sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
  5165. for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
  5166. tp = &np->target[target];
  5167. #if SYM_CONF_MAX_LUN > 1
  5168. kfree(tp->lunmp);
  5169. #endif
  5170. }
  5171. if (np->targtbl)
  5172. sym_mfree_dma(np->targtbl, 256, "TARGTBL");
  5173. }