pmcraid.c 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059
  1. /*
  2. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  3. *
  4. * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
  5. * PMC-Sierra Inc
  6. *
  7. * Copyright (C) 2008, 2009 PMC Sierra Inc
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
  22. * USA
  23. *
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/wait.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/firmware.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/io.h>
  43. #include <linux/slab.h>
  44. #include <asm/irq.h>
  45. #include <asm/processor.h>
  46. #include <linux/libata.h>
  47. #include <linux/mutex.h>
  48. #include <linux/ktime.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_tcq.h>
  53. #include <scsi/scsi_eh.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsicam.h>
  56. #include "pmcraid.h"
  57. /*
  58. * Module configuration parameters
  59. */
  60. static unsigned int pmcraid_debug_log;
  61. static unsigned int pmcraid_disable_aen;
  62. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  63. static unsigned int pmcraid_enable_msix;
  64. /*
  65. * Data structures to support multiple adapters by the LLD.
  66. * pmcraid_adapter_count - count of configured adapters
  67. */
  68. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  69. /*
  70. * Supporting user-level control interface through IOCTL commands.
  71. * pmcraid_major - major number to use
  72. * pmcraid_minor - minor number(s) to use
  73. */
  74. static unsigned int pmcraid_major;
  75. static struct class *pmcraid_class;
  76. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  77. /*
  78. * Module parameters
  79. */
  80. MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
  81. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  84. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  85. MODULE_PARM_DESC(log_level,
  86. "Enables firmware error code logging, default :1 high-severity"
  87. " errors, 2: all errors including high-severity errors,"
  88. " 0: disables logging");
  89. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  90. MODULE_PARM_DESC(debug,
  91. "Enable driver verbose message logging. Set 1 to enable."
  92. "(default: 0)");
  93. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  94. MODULE_PARM_DESC(disable_aen,
  95. "Disable driver aen notifications to apps. Set 1 to disable."
  96. "(default: 0)");
  97. /* chip specific constants for PMC MaxRAID controllers (same for
  98. * 0x5220 and 0x8010
  99. */
  100. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  101. {
  102. .ioastatus = 0x0,
  103. .ioarrin = 0x00040,
  104. .mailbox = 0x7FC30,
  105. .global_intr_mask = 0x00034,
  106. .ioa_host_intr = 0x0009C,
  107. .ioa_host_intr_clr = 0x000A0,
  108. .ioa_host_msix_intr = 0x7FC40,
  109. .ioa_host_mask = 0x7FC28,
  110. .ioa_host_mask_clr = 0x7FC28,
  111. .host_ioa_intr = 0x00020,
  112. .host_ioa_intr_clr = 0x00020,
  113. .transop_timeout = 300
  114. }
  115. };
  116. /*
  117. * PCI device ids supported by pmcraid driver
  118. */
  119. static struct pci_device_id pmcraid_pci_table[] = {
  120. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  121. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  122. },
  123. {}
  124. };
  125. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  126. /**
  127. * pmcraid_slave_alloc - Prepare for commands to a device
  128. * @scsi_dev: scsi device struct
  129. *
  130. * This function is called by mid-layer prior to sending any command to the new
  131. * device. Stores resource entry details of the device in scsi_device struct.
  132. * Queuecommand uses the resource handle and other details to fill up IOARCB
  133. * while sending commands to the device.
  134. *
  135. * Return value:
  136. * 0 on success / -ENXIO if device does not exist
  137. */
  138. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  139. {
  140. struct pmcraid_resource_entry *temp, *res = NULL;
  141. struct pmcraid_instance *pinstance;
  142. u8 target, bus, lun;
  143. unsigned long lock_flags;
  144. int rc = -ENXIO;
  145. u16 fw_version;
  146. pinstance = shost_priv(scsi_dev->host);
  147. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  148. /* Driver exposes VSET and GSCSI resources only; all other device types
  149. * are not exposed. Resource list is synchronized using resource lock
  150. * so any traversal or modifications to the list should be done inside
  151. * this lock
  152. */
  153. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  154. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  155. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  156. if (RES_IS_VSET(temp->cfg_entry)) {
  157. if (fw_version <= PMCRAID_FW_VERSION_1)
  158. target = temp->cfg_entry.unique_flags1;
  159. else
  160. target = temp->cfg_entry.array_id & 0xFF;
  161. if (target > PMCRAID_MAX_VSET_TARGETS)
  162. continue;
  163. bus = PMCRAID_VSET_BUS_ID;
  164. lun = 0;
  165. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  166. target = RES_TARGET(temp->cfg_entry.resource_address);
  167. bus = PMCRAID_PHYS_BUS_ID;
  168. lun = RES_LUN(temp->cfg_entry.resource_address);
  169. } else {
  170. continue;
  171. }
  172. if (bus == scsi_dev->channel &&
  173. target == scsi_dev->id &&
  174. lun == scsi_dev->lun) {
  175. res = temp;
  176. break;
  177. }
  178. }
  179. if (res) {
  180. res->scsi_dev = scsi_dev;
  181. scsi_dev->hostdata = res;
  182. res->change_detected = 0;
  183. atomic_set(&res->read_failures, 0);
  184. atomic_set(&res->write_failures, 0);
  185. rc = 0;
  186. }
  187. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  188. return rc;
  189. }
  190. /**
  191. * pmcraid_slave_configure - Configures a SCSI device
  192. * @scsi_dev: scsi device struct
  193. *
  194. * This function is executed by SCSI mid layer just after a device is first
  195. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  196. * timeout value (default 30s) will be over-written to a higher value (60s)
  197. * and max_sectors value will be over-written to 512. It also sets queue depth
  198. * to host->cmd_per_lun value
  199. *
  200. * Return value:
  201. * 0 on success
  202. */
  203. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  204. {
  205. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  206. if (!res)
  207. return 0;
  208. /* LLD exposes VSETs and Enclosure devices only */
  209. if (RES_IS_GSCSI(res->cfg_entry) &&
  210. scsi_dev->type != TYPE_ENCLOSURE)
  211. return -ENXIO;
  212. pmcraid_info("configuring %x:%x:%x:%x\n",
  213. scsi_dev->host->unique_id,
  214. scsi_dev->channel,
  215. scsi_dev->id,
  216. (u8)scsi_dev->lun);
  217. if (RES_IS_GSCSI(res->cfg_entry)) {
  218. scsi_dev->allow_restart = 1;
  219. } else if (RES_IS_VSET(res->cfg_entry)) {
  220. scsi_dev->allow_restart = 1;
  221. blk_queue_rq_timeout(scsi_dev->request_queue,
  222. PMCRAID_VSET_IO_TIMEOUT);
  223. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  224. PMCRAID_VSET_MAX_SECTORS);
  225. }
  226. /*
  227. * We never want to report TCQ support for these types of devices.
  228. */
  229. if (!RES_IS_GSCSI(res->cfg_entry) && !RES_IS_VSET(res->cfg_entry))
  230. scsi_dev->tagged_supported = 0;
  231. return 0;
  232. }
  233. /**
  234. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  235. *
  236. * @scsi_dev: scsi device struct
  237. *
  238. * This is called by mid-layer before removing a device. Pointer assignments
  239. * done in pmcraid_slave_alloc will be reset to NULL here.
  240. *
  241. * Return value
  242. * none
  243. */
  244. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  245. {
  246. struct pmcraid_resource_entry *res;
  247. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  248. if (res)
  249. res->scsi_dev = NULL;
  250. scsi_dev->hostdata = NULL;
  251. }
  252. /**
  253. * pmcraid_change_queue_depth - Change the device's queue depth
  254. * @scsi_dev: scsi device struct
  255. * @depth: depth to set
  256. *
  257. * Return value
  258. * actual depth set
  259. */
  260. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
  261. {
  262. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  263. depth = PMCRAID_MAX_CMD_PER_LUN;
  264. return scsi_change_queue_depth(scsi_dev, depth);
  265. }
  266. /**
  267. * pmcraid_init_cmdblk - initializes a command block
  268. *
  269. * @cmd: pointer to struct pmcraid_cmd to be initialized
  270. * @index: if >=0 first time initialization; otherwise reinitialization
  271. *
  272. * Return Value
  273. * None
  274. */
  275. static void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  276. {
  277. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  278. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  279. if (index >= 0) {
  280. /* first time initialization (called from probe) */
  281. u32 ioasa_offset =
  282. offsetof(struct pmcraid_control_block, ioasa);
  283. cmd->index = index;
  284. ioarcb->response_handle = cpu_to_le32(index << 2);
  285. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  286. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  287. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  288. } else {
  289. /* re-initialization of various lengths, called once command is
  290. * processed by IOA
  291. */
  292. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  293. ioarcb->hrrq_id = 0;
  294. ioarcb->request_flags0 = 0;
  295. ioarcb->request_flags1 = 0;
  296. ioarcb->cmd_timeout = 0;
  297. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  298. ioarcb->ioadl_bus_addr = 0;
  299. ioarcb->ioadl_length = 0;
  300. ioarcb->data_transfer_length = 0;
  301. ioarcb->add_cmd_param_length = 0;
  302. ioarcb->add_cmd_param_offset = 0;
  303. cmd->ioa_cb->ioasa.ioasc = 0;
  304. cmd->ioa_cb->ioasa.residual_data_length = 0;
  305. cmd->time_left = 0;
  306. }
  307. cmd->cmd_done = NULL;
  308. cmd->scsi_cmd = NULL;
  309. cmd->release = 0;
  310. cmd->completion_req = 0;
  311. cmd->sense_buffer = 0;
  312. cmd->sense_buffer_dma = 0;
  313. cmd->dma_handle = 0;
  314. init_timer(&cmd->timer);
  315. }
  316. /**
  317. * pmcraid_reinit_cmdblk - reinitialize a command block
  318. *
  319. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  320. *
  321. * Return Value
  322. * None
  323. */
  324. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  325. {
  326. pmcraid_init_cmdblk(cmd, -1);
  327. }
  328. /**
  329. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  330. * @pinstance: adapter instance structure
  331. *
  332. * Return Value:
  333. * returns pointer to cmd block or NULL if no blocks are available
  334. */
  335. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  336. struct pmcraid_instance *pinstance
  337. )
  338. {
  339. struct pmcraid_cmd *cmd = NULL;
  340. unsigned long lock_flags;
  341. /* free cmd block list is protected by free_pool_lock */
  342. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  343. if (!list_empty(&pinstance->free_cmd_pool)) {
  344. cmd = list_entry(pinstance->free_cmd_pool.next,
  345. struct pmcraid_cmd, free_list);
  346. list_del(&cmd->free_list);
  347. }
  348. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  349. /* Initialize the command block before giving it the caller */
  350. if (cmd != NULL)
  351. pmcraid_reinit_cmdblk(cmd);
  352. return cmd;
  353. }
  354. /**
  355. * pmcraid_return_cmd - return a completed command block back into free pool
  356. * @cmd: pointer to the command block
  357. *
  358. * Return Value:
  359. * nothing
  360. */
  361. static void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  362. {
  363. struct pmcraid_instance *pinstance = cmd->drv_inst;
  364. unsigned long lock_flags;
  365. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  366. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  367. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  368. }
  369. /**
  370. * pmcraid_read_interrupts - reads IOA interrupts
  371. *
  372. * @pinstance: pointer to adapter instance structure
  373. *
  374. * Return value
  375. * interrupts read from IOA
  376. */
  377. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  378. {
  379. return (pinstance->interrupt_mode) ?
  380. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  381. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  382. }
  383. /**
  384. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  385. *
  386. * @pinstance: pointer to per adapter instance structure
  387. * @intrs: interrupts to disable
  388. *
  389. * Return Value
  390. * None
  391. */
  392. static void pmcraid_disable_interrupts(
  393. struct pmcraid_instance *pinstance,
  394. u32 intrs
  395. )
  396. {
  397. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  398. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  399. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  400. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  401. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  402. if (!pinstance->interrupt_mode) {
  403. iowrite32(intrs,
  404. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  405. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  406. }
  407. }
  408. /**
  409. * pmcraid_enable_interrupts - Enables specified interrupts
  410. *
  411. * @pinstance: pointer to per adapter instance structure
  412. * @intr: interrupts to enable
  413. *
  414. * Return Value
  415. * None
  416. */
  417. static void pmcraid_enable_interrupts(
  418. struct pmcraid_instance *pinstance,
  419. u32 intrs
  420. )
  421. {
  422. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  423. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  424. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  425. if (!pinstance->interrupt_mode) {
  426. iowrite32(~intrs,
  427. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  428. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  429. }
  430. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  431. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  432. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  433. }
  434. /**
  435. * pmcraid_clr_trans_op - clear trans to op interrupt
  436. *
  437. * @pinstance: pointer to per adapter instance structure
  438. *
  439. * Return Value
  440. * None
  441. */
  442. static void pmcraid_clr_trans_op(
  443. struct pmcraid_instance *pinstance
  444. )
  445. {
  446. unsigned long lock_flags;
  447. if (!pinstance->interrupt_mode) {
  448. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  449. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  450. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  451. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  452. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  453. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  454. }
  455. if (pinstance->reset_cmd != NULL) {
  456. del_timer(&pinstance->reset_cmd->timer);
  457. spin_lock_irqsave(
  458. pinstance->host->host_lock, lock_flags);
  459. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  460. spin_unlock_irqrestore(
  461. pinstance->host->host_lock, lock_flags);
  462. }
  463. }
  464. /**
  465. * pmcraid_reset_type - Determine the required reset type
  466. * @pinstance: pointer to adapter instance structure
  467. *
  468. * IOA requires hard reset if any of the following conditions is true.
  469. * 1. If HRRQ valid interrupt is not masked
  470. * 2. IOA reset alert doorbell is set
  471. * 3. If there are any error interrupts
  472. */
  473. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  474. {
  475. u32 mask;
  476. u32 intrs;
  477. u32 alerts;
  478. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  479. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  480. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  481. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  482. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  483. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  484. pmcraid_info("IOA requires hard reset\n");
  485. pinstance->ioa_hard_reset = 1;
  486. }
  487. /* If unit check is active, trigger the dump */
  488. if (intrs & INTRS_IOA_UNIT_CHECK)
  489. pinstance->ioa_unit_check = 1;
  490. }
  491. /**
  492. * pmcraid_bist_done - completion function for PCI BIST
  493. * @cmd: pointer to reset command
  494. * Return Value
  495. * none
  496. */
  497. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  498. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  499. {
  500. struct pmcraid_instance *pinstance = cmd->drv_inst;
  501. unsigned long lock_flags;
  502. int rc;
  503. u16 pci_reg;
  504. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  505. /* If PCI config space can't be accessed wait for another two secs */
  506. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  507. cmd->time_left > 0) {
  508. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  509. cmd->timer.expires = jiffies + cmd->time_left;
  510. cmd->time_left = 0;
  511. cmd->timer.data = (unsigned long)cmd;
  512. cmd->timer.function =
  513. (void (*)(unsigned long))pmcraid_bist_done;
  514. add_timer(&cmd->timer);
  515. } else {
  516. cmd->time_left = 0;
  517. pmcraid_info("BIST is complete, proceeding with reset\n");
  518. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  519. pmcraid_ioa_reset(cmd);
  520. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  521. }
  522. }
  523. /**
  524. * pmcraid_start_bist - starts BIST
  525. * @cmd: pointer to reset cmd
  526. * Return Value
  527. * none
  528. */
  529. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  530. {
  531. struct pmcraid_instance *pinstance = cmd->drv_inst;
  532. u32 doorbells, intrs;
  533. /* proceed with bist and wait for 2 seconds */
  534. iowrite32(DOORBELL_IOA_START_BIST,
  535. pinstance->int_regs.host_ioa_interrupt_reg);
  536. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  537. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  538. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  539. doorbells, intrs);
  540. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  541. cmd->timer.data = (unsigned long)cmd;
  542. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  543. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  544. add_timer(&cmd->timer);
  545. }
  546. /**
  547. * pmcraid_reset_alert_done - completion routine for reset_alert
  548. * @cmd: pointer to command block used in reset sequence
  549. * Return value
  550. * None
  551. */
  552. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  553. {
  554. struct pmcraid_instance *pinstance = cmd->drv_inst;
  555. u32 status = ioread32(pinstance->ioa_status);
  556. unsigned long lock_flags;
  557. /* if the critical operation in progress bit is set or the wait times
  558. * out, invoke reset engine to proceed with hard reset. If there is
  559. * some more time to wait, restart the timer
  560. */
  561. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  562. cmd->time_left <= 0) {
  563. pmcraid_info("critical op is reset proceeding with reset\n");
  564. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  565. pmcraid_ioa_reset(cmd);
  566. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  567. } else {
  568. pmcraid_info("critical op is not yet reset waiting again\n");
  569. /* restart timer if some more time is available to wait */
  570. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  571. cmd->timer.data = (unsigned long)cmd;
  572. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  573. cmd->timer.function =
  574. (void (*)(unsigned long))pmcraid_reset_alert_done;
  575. add_timer(&cmd->timer);
  576. }
  577. }
  578. /**
  579. * pmcraid_reset_alert - alerts IOA for a possible reset
  580. * @cmd : command block to be used for reset sequence.
  581. *
  582. * Return Value
  583. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  584. * successfully written to IOA. Returns non-zero in case pci_config_space
  585. * is not accessible
  586. */
  587. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  588. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  589. {
  590. struct pmcraid_instance *pinstance = cmd->drv_inst;
  591. u32 doorbells;
  592. int rc;
  593. u16 pci_reg;
  594. /* If we are able to access IOA PCI config space, alert IOA that we are
  595. * going to reset it soon. This enables IOA to preserv persistent error
  596. * data if any. In case memory space is not accessible, proceed with
  597. * BIST or slot_reset
  598. */
  599. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  600. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  601. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  602. * reset IOA doesn't generate any interrupts when CRITICAL
  603. * OPERATION bit is reset. A timer is started to wait for this
  604. * bit to be reset.
  605. */
  606. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  607. cmd->timer.data = (unsigned long)cmd;
  608. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  609. cmd->timer.function =
  610. (void (*)(unsigned long))pmcraid_reset_alert_done;
  611. add_timer(&cmd->timer);
  612. iowrite32(DOORBELL_IOA_RESET_ALERT,
  613. pinstance->int_regs.host_ioa_interrupt_reg);
  614. doorbells =
  615. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  616. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  617. } else {
  618. pmcraid_info("PCI config is not accessible starting BIST\n");
  619. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  620. pmcraid_start_bist(cmd);
  621. }
  622. }
  623. /**
  624. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  625. *
  626. * @cmd : pointer to command structure, that got timedout
  627. *
  628. * This function blocks host requests and initiates an adapter reset.
  629. *
  630. * Return value:
  631. * None
  632. */
  633. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  634. {
  635. struct pmcraid_instance *pinstance = cmd->drv_inst;
  636. unsigned long lock_flags;
  637. dev_info(&pinstance->pdev->dev,
  638. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  639. cmd->ioa_cb->ioarcb.cdb[0]);
  640. /* Command timeouts result in hard reset sequence. The command that got
  641. * timed out may be the one used as part of reset sequence. In this
  642. * case restart reset sequence using the same command block even if
  643. * reset is in progress. Otherwise fail this command and get a free
  644. * command block to restart the reset sequence.
  645. */
  646. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  647. if (!pinstance->ioa_reset_in_progress) {
  648. pinstance->ioa_reset_attempts = 0;
  649. cmd = pmcraid_get_free_cmd(pinstance);
  650. /* If we are out of command blocks, just return here itself.
  651. * Some other command's timeout handler can do the reset job
  652. */
  653. if (cmd == NULL) {
  654. spin_unlock_irqrestore(pinstance->host->host_lock,
  655. lock_flags);
  656. pmcraid_err("no free cmnd block for timeout handler\n");
  657. return;
  658. }
  659. pinstance->reset_cmd = cmd;
  660. pinstance->ioa_reset_in_progress = 1;
  661. } else {
  662. pmcraid_info("reset is already in progress\n");
  663. if (pinstance->reset_cmd != cmd) {
  664. /* This command should have been given to IOA, this
  665. * command will be completed by fail_outstanding_cmds
  666. * anyway
  667. */
  668. pmcraid_err("cmd is pending but reset in progress\n");
  669. }
  670. /* If this command was being used as part of the reset
  671. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  672. * causes fail_outstanding_commands not to return the command
  673. * block back to free pool
  674. */
  675. if (cmd == pinstance->reset_cmd)
  676. cmd->cmd_done = pmcraid_ioa_reset;
  677. }
  678. /* Notify apps of important IOA bringup/bringdown sequences */
  679. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  680. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  681. pmcraid_notify_ioastate(pinstance,
  682. PMC_DEVICE_EVENT_RESET_START);
  683. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  684. scsi_block_requests(pinstance->host);
  685. pmcraid_reset_alert(cmd);
  686. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  687. }
  688. /**
  689. * pmcraid_internal_done - completion routine for internally generated cmds
  690. *
  691. * @cmd: command that got response from IOA
  692. *
  693. * Return Value:
  694. * none
  695. */
  696. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  697. {
  698. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  699. cmd->ioa_cb->ioarcb.cdb[0],
  700. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  701. /* Some of the internal commands are sent with callers blocking for the
  702. * response. Same will be indicated as part of cmd->completion_req
  703. * field. Response path needs to wake up any waiters waiting for cmd
  704. * completion if this flag is set.
  705. */
  706. if (cmd->completion_req) {
  707. cmd->completion_req = 0;
  708. complete(&cmd->wait_for_completion);
  709. }
  710. /* most of the internal commands are completed by caller itself, so
  711. * no need to return the command block back to free pool until we are
  712. * required to do so (e.g once done with initialization).
  713. */
  714. if (cmd->release) {
  715. cmd->release = 0;
  716. pmcraid_return_cmd(cmd);
  717. }
  718. }
  719. /**
  720. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  721. *
  722. * @cmd: command that got response from IOA
  723. *
  724. * This routine is called after driver re-reads configuration table due to a
  725. * lost CCN. It returns the command block back to free pool and schedules
  726. * worker thread to add/delete devices into the system.
  727. *
  728. * Return Value:
  729. * none
  730. */
  731. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  732. {
  733. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  734. cmd->ioa_cb->ioarcb.cdb[0],
  735. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  736. if (cmd->release) {
  737. cmd->release = 0;
  738. pmcraid_return_cmd(cmd);
  739. }
  740. pmcraid_info("scheduling worker for config table reinitialization\n");
  741. schedule_work(&cmd->drv_inst->worker_q);
  742. }
  743. /**
  744. * pmcraid_erp_done - Process completion of SCSI error response from device
  745. * @cmd: pmcraid_command
  746. *
  747. * This function copies the sense buffer into the scsi_cmd struct and completes
  748. * scsi_cmd by calling scsi_done function.
  749. *
  750. * Return value:
  751. * none
  752. */
  753. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  754. {
  755. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  756. struct pmcraid_instance *pinstance = cmd->drv_inst;
  757. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  758. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  759. scsi_cmd->result |= (DID_ERROR << 16);
  760. scmd_printk(KERN_INFO, scsi_cmd,
  761. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  762. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  763. }
  764. /* if we had allocated sense buffers for request sense, copy the sense
  765. * release the buffers
  766. */
  767. if (cmd->sense_buffer != NULL) {
  768. memcpy(scsi_cmd->sense_buffer,
  769. cmd->sense_buffer,
  770. SCSI_SENSE_BUFFERSIZE);
  771. pci_free_consistent(pinstance->pdev,
  772. SCSI_SENSE_BUFFERSIZE,
  773. cmd->sense_buffer, cmd->sense_buffer_dma);
  774. cmd->sense_buffer = NULL;
  775. cmd->sense_buffer_dma = 0;
  776. }
  777. scsi_dma_unmap(scsi_cmd);
  778. pmcraid_return_cmd(cmd);
  779. scsi_cmd->scsi_done(scsi_cmd);
  780. }
  781. /**
  782. * pmcraid_fire_command - sends an IOA command to adapter
  783. *
  784. * This function adds the given block into pending command list
  785. * and returns without waiting
  786. *
  787. * @cmd : command to be sent to the device
  788. *
  789. * Return Value
  790. * None
  791. */
  792. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  793. {
  794. struct pmcraid_instance *pinstance = cmd->drv_inst;
  795. unsigned long lock_flags;
  796. /* Add this command block to pending cmd pool. We do this prior to
  797. * writting IOARCB to ioarrin because IOA might complete the command
  798. * by the time we are about to add it to the list. Response handler
  799. * (isr/tasklet) looks for cmd block in the pending pending list.
  800. */
  801. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  802. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  803. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  804. atomic_inc(&pinstance->outstanding_cmds);
  805. /* driver writes lower 32-bit value of IOARCB address only */
  806. mb();
  807. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  808. pinstance->ioarrin);
  809. }
  810. /**
  811. * pmcraid_send_cmd - fires a command to IOA
  812. *
  813. * This function also sets up timeout function, and command completion
  814. * function
  815. *
  816. * @cmd: pointer to the command block to be fired to IOA
  817. * @cmd_done: command completion function, called once IOA responds
  818. * @timeout: timeout to wait for this command completion
  819. * @timeout_func: timeout handler
  820. *
  821. * Return value
  822. * none
  823. */
  824. static void pmcraid_send_cmd(
  825. struct pmcraid_cmd *cmd,
  826. void (*cmd_done) (struct pmcraid_cmd *),
  827. unsigned long timeout,
  828. void (*timeout_func) (struct pmcraid_cmd *)
  829. )
  830. {
  831. /* initialize done function */
  832. cmd->cmd_done = cmd_done;
  833. if (timeout_func) {
  834. /* setup timeout handler */
  835. cmd->timer.data = (unsigned long)cmd;
  836. cmd->timer.expires = jiffies + timeout;
  837. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  838. add_timer(&cmd->timer);
  839. }
  840. /* fire the command to IOA */
  841. _pmcraid_fire_command(cmd);
  842. }
  843. /**
  844. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  845. * @cmd: pointer to the command block used for sending IOA shutdown command
  846. *
  847. * Return value
  848. * None
  849. */
  850. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  851. {
  852. struct pmcraid_instance *pinstance = cmd->drv_inst;
  853. unsigned long lock_flags;
  854. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  855. pmcraid_ioa_reset(cmd);
  856. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  857. }
  858. /**
  859. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  860. *
  861. * @cmd: pointer to the command block used as part of reset sequence
  862. *
  863. * Return Value
  864. * None
  865. */
  866. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  867. {
  868. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  869. cmd->ioa_cb->ioarcb.cdb[0],
  870. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  871. /* Note that commands sent during reset require next command to be sent
  872. * to IOA. Hence reinit the done function as well as timeout function
  873. */
  874. pmcraid_reinit_cmdblk(cmd);
  875. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  876. cmd->ioa_cb->ioarcb.resource_handle =
  877. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  878. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  879. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  880. /* fire shutdown command to hardware. */
  881. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  882. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  883. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  884. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  885. PMCRAID_SHUTDOWN_TIMEOUT,
  886. pmcraid_timeout_handler);
  887. }
  888. /**
  889. * pmcraid_get_fwversion_done - completion function for get_fwversion
  890. *
  891. * @cmd: pointer to command block used to send INQUIRY command
  892. *
  893. * Return Value
  894. * none
  895. */
  896. static void pmcraid_querycfg(struct pmcraid_cmd *);
  897. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  898. {
  899. struct pmcraid_instance *pinstance = cmd->drv_inst;
  900. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  901. unsigned long lock_flags;
  902. /* configuration table entry size depends on firmware version. If fw
  903. * version is not known, it is not possible to interpret IOA config
  904. * table
  905. */
  906. if (ioasc) {
  907. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  908. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  909. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  910. pmcraid_reset_alert(cmd);
  911. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  912. } else {
  913. pmcraid_querycfg(cmd);
  914. }
  915. }
  916. /**
  917. * pmcraid_get_fwversion - reads firmware version information
  918. *
  919. * @cmd: pointer to command block used to send INQUIRY command
  920. *
  921. * Return Value
  922. * none
  923. */
  924. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  925. {
  926. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  927. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  928. struct pmcraid_instance *pinstance = cmd->drv_inst;
  929. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  930. pmcraid_reinit_cmdblk(cmd);
  931. ioarcb->request_type = REQ_TYPE_SCSI;
  932. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  933. ioarcb->cdb[0] = INQUIRY;
  934. ioarcb->cdb[1] = 1;
  935. ioarcb->cdb[2] = 0xD0;
  936. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  937. ioarcb->cdb[4] = data_size & 0xFF;
  938. /* Since entire inquiry data it can be part of IOARCB itself
  939. */
  940. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  941. offsetof(struct pmcraid_ioarcb,
  942. add_data.u.ioadl[0]));
  943. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  944. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  945. ioarcb->request_flags0 |= NO_LINK_DESCS;
  946. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  947. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  948. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  949. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  950. ioadl->data_len = cpu_to_le32(data_size);
  951. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  952. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  953. }
  954. /**
  955. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  956. * @cmd: pointer to command block to be used for identify hrrq
  957. *
  958. * Return Value
  959. * none
  960. */
  961. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  962. {
  963. struct pmcraid_instance *pinstance = cmd->drv_inst;
  964. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  965. int index = cmd->hrrq_index;
  966. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  967. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  968. void (*done_function)(struct pmcraid_cmd *);
  969. pmcraid_reinit_cmdblk(cmd);
  970. cmd->hrrq_index = index + 1;
  971. if (cmd->hrrq_index < pinstance->num_hrrq) {
  972. done_function = pmcraid_identify_hrrq;
  973. } else {
  974. cmd->hrrq_index = 0;
  975. done_function = pmcraid_get_fwversion;
  976. }
  977. /* Initialize ioarcb */
  978. ioarcb->request_type = REQ_TYPE_IOACMD;
  979. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  980. /* initialize the hrrq number where IOA will respond to this command */
  981. ioarcb->hrrq_id = index;
  982. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  983. ioarcb->cdb[1] = index;
  984. /* IOA expects 64-bit pci address to be written in B.E format
  985. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  986. */
  987. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  988. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  989. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  990. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  991. /* Subsequent commands require HRRQ identification to be successful.
  992. * Note that this gets called even during reset from SCSI mid-layer
  993. * or tasklet
  994. */
  995. pmcraid_send_cmd(cmd, done_function,
  996. PMCRAID_INTERNAL_TIMEOUT,
  997. pmcraid_timeout_handler);
  998. }
  999. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  1000. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  1001. /**
  1002. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  1003. *
  1004. * @cmd: initialized command block pointer
  1005. *
  1006. * Return Value
  1007. * none
  1008. */
  1009. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  1010. {
  1011. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  1012. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  1013. else
  1014. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  1015. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  1016. }
  1017. /**
  1018. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  1019. *
  1020. * @pinstance: pointer to adapter instance structure
  1021. * @type: HCAM type
  1022. *
  1023. * Return Value
  1024. * pointer to initialized pmcraid_cmd structure or NULL
  1025. */
  1026. static struct pmcraid_cmd *pmcraid_init_hcam
  1027. (
  1028. struct pmcraid_instance *pinstance,
  1029. u8 type
  1030. )
  1031. {
  1032. struct pmcraid_cmd *cmd;
  1033. struct pmcraid_ioarcb *ioarcb;
  1034. struct pmcraid_ioadl_desc *ioadl;
  1035. struct pmcraid_hostrcb *hcam;
  1036. void (*cmd_done) (struct pmcraid_cmd *);
  1037. dma_addr_t dma;
  1038. int rcb_size;
  1039. cmd = pmcraid_get_free_cmd(pinstance);
  1040. if (!cmd) {
  1041. pmcraid_err("no free command blocks for hcam\n");
  1042. return cmd;
  1043. }
  1044. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1045. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1046. cmd_done = pmcraid_process_ccn;
  1047. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1048. hcam = &pinstance->ccn;
  1049. } else {
  1050. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1051. cmd_done = pmcraid_process_ldn;
  1052. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1053. hcam = &pinstance->ldn;
  1054. }
  1055. /* initialize command pointer used for HCAM registration */
  1056. hcam->cmd = cmd;
  1057. ioarcb = &cmd->ioa_cb->ioarcb;
  1058. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1059. offsetof(struct pmcraid_ioarcb,
  1060. add_data.u.ioadl[0]));
  1061. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1062. ioadl = ioarcb->add_data.u.ioadl;
  1063. /* Initialize ioarcb */
  1064. ioarcb->request_type = REQ_TYPE_HCAM;
  1065. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1066. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1067. ioarcb->cdb[1] = type;
  1068. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1069. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1070. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1071. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1072. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1073. ioadl[0].address = cpu_to_le32(dma);
  1074. cmd->cmd_done = cmd_done;
  1075. return cmd;
  1076. }
  1077. /**
  1078. * pmcraid_send_hcam - Send an HCAM to IOA
  1079. * @pinstance: ioa config struct
  1080. * @type: HCAM type
  1081. *
  1082. * This function will send a Host Controlled Async command to IOA.
  1083. *
  1084. * Return value:
  1085. * none
  1086. */
  1087. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1088. {
  1089. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1090. pmcraid_send_hcam_cmd(cmd);
  1091. }
  1092. /**
  1093. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1094. *
  1095. * @cmd: pointer to cmd that is used as cancelling command
  1096. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1097. */
  1098. static void pmcraid_prepare_cancel_cmd(
  1099. struct pmcraid_cmd *cmd,
  1100. struct pmcraid_cmd *cmd_to_cancel
  1101. )
  1102. {
  1103. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1104. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  1105. /* Get the resource handle to where the command to be aborted has been
  1106. * sent.
  1107. */
  1108. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1109. ioarcb->request_type = REQ_TYPE_IOACMD;
  1110. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1111. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1112. /* IOARCB address of the command to be cancelled is given in
  1113. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1114. * IOARCB address are not masked.
  1115. */
  1116. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  1117. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1118. }
  1119. /**
  1120. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1121. *
  1122. * @cmd: command to be used as cancelling command
  1123. * @type: HCAM type
  1124. * @cmd_done: op done function for the cancelling command
  1125. */
  1126. static void pmcraid_cancel_hcam(
  1127. struct pmcraid_cmd *cmd,
  1128. u8 type,
  1129. void (*cmd_done) (struct pmcraid_cmd *)
  1130. )
  1131. {
  1132. struct pmcraid_instance *pinstance;
  1133. struct pmcraid_hostrcb *hcam;
  1134. pinstance = cmd->drv_inst;
  1135. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1136. &pinstance->ldn : &pinstance->ccn;
  1137. /* prepare for cancelling previous hcam command. If the HCAM is
  1138. * currently not pending with IOA, we would have hcam->cmd as non-null
  1139. */
  1140. if (hcam->cmd == NULL)
  1141. return;
  1142. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1143. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1144. * schedule queuecommand while we are doing this
  1145. */
  1146. pmcraid_send_cmd(cmd, cmd_done,
  1147. PMCRAID_INTERNAL_TIMEOUT,
  1148. pmcraid_timeout_handler);
  1149. }
  1150. /**
  1151. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1152. *
  1153. * @cmd: command block to be used for cancelling the HCAM
  1154. */
  1155. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1156. {
  1157. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1158. cmd->ioa_cb->ioarcb.cdb[0],
  1159. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1160. pmcraid_reinit_cmdblk(cmd);
  1161. pmcraid_cancel_hcam(cmd,
  1162. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1163. pmcraid_ioa_shutdown);
  1164. }
  1165. /**
  1166. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1167. *
  1168. * @cmd: command block to be used for cancelling the HCAM
  1169. */
  1170. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1171. {
  1172. pmcraid_cancel_hcam(cmd,
  1173. PMCRAID_HCAM_CODE_LOG_DATA,
  1174. pmcraid_cancel_ccn);
  1175. }
  1176. /**
  1177. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1178. *
  1179. * @fw_version: firmware version code
  1180. * @cfgte: pointer to configuration table entry of the resource
  1181. *
  1182. * Return value:
  1183. * true if resource can be added to midlayer, false(0) otherwise
  1184. */
  1185. static int pmcraid_expose_resource(u16 fw_version,
  1186. struct pmcraid_config_table_entry *cfgte)
  1187. {
  1188. int retval = 0;
  1189. if (cfgte->resource_type == RES_TYPE_VSET) {
  1190. if (fw_version <= PMCRAID_FW_VERSION_1)
  1191. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1192. else
  1193. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1194. (cfgte->unique_flags1 & 0x80) == 0);
  1195. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1196. retval = (RES_BUS(cfgte->resource_address) !=
  1197. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1198. return retval;
  1199. }
  1200. /* attributes supported by pmcraid_event_family */
  1201. enum {
  1202. PMCRAID_AEN_ATTR_UNSPEC,
  1203. PMCRAID_AEN_ATTR_EVENT,
  1204. __PMCRAID_AEN_ATTR_MAX,
  1205. };
  1206. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1207. /* commands supported by pmcraid_event_family */
  1208. enum {
  1209. PMCRAID_AEN_CMD_UNSPEC,
  1210. PMCRAID_AEN_CMD_EVENT,
  1211. __PMCRAID_AEN_CMD_MAX,
  1212. };
  1213. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1214. static struct genl_multicast_group pmcraid_mcgrps[] = {
  1215. { .name = "events", /* not really used - see ID discussion below */ },
  1216. };
  1217. static struct genl_family pmcraid_event_family __ro_after_init = {
  1218. .module = THIS_MODULE,
  1219. .name = "pmcraid",
  1220. .version = 1,
  1221. .maxattr = PMCRAID_AEN_ATTR_MAX,
  1222. .mcgrps = pmcraid_mcgrps,
  1223. .n_mcgrps = ARRAY_SIZE(pmcraid_mcgrps),
  1224. };
  1225. /**
  1226. * pmcraid_netlink_init - registers pmcraid_event_family
  1227. *
  1228. * Return value:
  1229. * 0 if the pmcraid_event_family is successfully registered
  1230. * with netlink generic, non-zero otherwise
  1231. */
  1232. static int __init pmcraid_netlink_init(void)
  1233. {
  1234. int result;
  1235. result = genl_register_family(&pmcraid_event_family);
  1236. if (result)
  1237. return result;
  1238. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1239. pmcraid_event_family.id);
  1240. return result;
  1241. }
  1242. /**
  1243. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1244. *
  1245. * Return value:
  1246. * none
  1247. */
  1248. static void pmcraid_netlink_release(void)
  1249. {
  1250. genl_unregister_family(&pmcraid_event_family);
  1251. }
  1252. /**
  1253. * pmcraid_notify_aen - sends event msg to user space application
  1254. * @pinstance: pointer to adapter instance structure
  1255. * @type: HCAM type
  1256. *
  1257. * Return value:
  1258. * 0 if success, error value in case of any failure.
  1259. */
  1260. static int pmcraid_notify_aen(
  1261. struct pmcraid_instance *pinstance,
  1262. struct pmcraid_aen_msg *aen_msg,
  1263. u32 data_size
  1264. )
  1265. {
  1266. struct sk_buff *skb;
  1267. void *msg_header;
  1268. u32 total_size, nla_genl_hdr_total_size;
  1269. int result;
  1270. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1271. MINOR(pinstance->cdev.dev));
  1272. aen_msg->length = data_size;
  1273. data_size += sizeof(*aen_msg);
  1274. total_size = nla_total_size(data_size);
  1275. /* Add GENL_HDR to total_size */
  1276. nla_genl_hdr_total_size =
  1277. (total_size + (GENL_HDRLEN +
  1278. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1279. + NLMSG_HDRLEN);
  1280. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1281. if (!skb) {
  1282. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1283. total_size);
  1284. return -ENOMEM;
  1285. }
  1286. /* add the genetlink message header */
  1287. msg_header = genlmsg_put(skb, 0, 0,
  1288. &pmcraid_event_family, 0,
  1289. PMCRAID_AEN_CMD_EVENT);
  1290. if (!msg_header) {
  1291. pmcraid_err("failed to copy command details\n");
  1292. nlmsg_free(skb);
  1293. return -ENOMEM;
  1294. }
  1295. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1296. if (result) {
  1297. pmcraid_err("failed to copy AEN attribute data\n");
  1298. nlmsg_free(skb);
  1299. return -EINVAL;
  1300. }
  1301. /* send genetlink multicast message to notify appplications */
  1302. genlmsg_end(skb, msg_header);
  1303. result = genlmsg_multicast(&pmcraid_event_family, skb,
  1304. 0, 0, GFP_ATOMIC);
  1305. /* If there are no listeners, genlmsg_multicast may return non-zero
  1306. * value.
  1307. */
  1308. if (result)
  1309. pmcraid_info("error (%x) sending aen event message\n", result);
  1310. return result;
  1311. }
  1312. /**
  1313. * pmcraid_notify_ccn - notifies about CCN event msg to user space
  1314. * @pinstance: pointer adapter instance structure
  1315. *
  1316. * Return value:
  1317. * 0 if success, error value in case of any failure
  1318. */
  1319. static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
  1320. {
  1321. return pmcraid_notify_aen(pinstance,
  1322. pinstance->ccn.msg,
  1323. pinstance->ccn.hcam->data_len +
  1324. sizeof(struct pmcraid_hcam_hdr));
  1325. }
  1326. /**
  1327. * pmcraid_notify_ldn - notifies about CCN event msg to user space
  1328. * @pinstance: pointer adapter instance structure
  1329. *
  1330. * Return value:
  1331. * 0 if success, error value in case of any failure
  1332. */
  1333. static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
  1334. {
  1335. return pmcraid_notify_aen(pinstance,
  1336. pinstance->ldn.msg,
  1337. pinstance->ldn.hcam->data_len +
  1338. sizeof(struct pmcraid_hcam_hdr));
  1339. }
  1340. /**
  1341. * pmcraid_notify_ioastate - sends IOA state event msg to user space
  1342. * @pinstance: pointer adapter instance structure
  1343. * @evt: controller state event to be sent
  1344. *
  1345. * Return value:
  1346. * 0 if success, error value in case of any failure
  1347. */
  1348. static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
  1349. {
  1350. pinstance->scn.ioa_state = evt;
  1351. pmcraid_notify_aen(pinstance,
  1352. &pinstance->scn.msg,
  1353. sizeof(u32));
  1354. }
  1355. /**
  1356. * pmcraid_handle_config_change - Handle a config change from the adapter
  1357. * @pinstance: pointer to per adapter instance structure
  1358. *
  1359. * Return value:
  1360. * none
  1361. */
  1362. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1363. {
  1364. struct pmcraid_config_table_entry *cfg_entry;
  1365. struct pmcraid_hcam_ccn *ccn_hcam;
  1366. struct pmcraid_cmd *cmd;
  1367. struct pmcraid_cmd *cfgcmd;
  1368. struct pmcraid_resource_entry *res = NULL;
  1369. unsigned long lock_flags;
  1370. unsigned long host_lock_flags;
  1371. u32 new_entry = 1;
  1372. u32 hidden_entry = 0;
  1373. u16 fw_version;
  1374. int rc;
  1375. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1376. cfg_entry = &ccn_hcam->cfg_entry;
  1377. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  1378. pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
  1379. res: %x:%x:%x:%x\n",
  1380. pinstance->ccn.hcam->ilid,
  1381. pinstance->ccn.hcam->op_code,
  1382. ((pinstance->ccn.hcam->timestamp1) |
  1383. ((pinstance->ccn.hcam->timestamp2 & 0xffffffffLL) << 32)),
  1384. pinstance->ccn.hcam->notification_type,
  1385. pinstance->ccn.hcam->notification_lost,
  1386. pinstance->ccn.hcam->flags,
  1387. pinstance->host->unique_id,
  1388. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1389. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1390. RES_BUS(cfg_entry->resource_address)),
  1391. RES_IS_VSET(*cfg_entry) ?
  1392. (fw_version <= PMCRAID_FW_VERSION_1 ?
  1393. cfg_entry->unique_flags1 :
  1394. cfg_entry->array_id & 0xFF) :
  1395. RES_TARGET(cfg_entry->resource_address),
  1396. RES_LUN(cfg_entry->resource_address));
  1397. /* If this HCAM indicates a lost notification, read the config table */
  1398. if (pinstance->ccn.hcam->notification_lost) {
  1399. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1400. if (cfgcmd) {
  1401. pmcraid_info("lost CCN, reading config table\b");
  1402. pinstance->reinit_cfg_table = 1;
  1403. pmcraid_querycfg(cfgcmd);
  1404. } else {
  1405. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1406. }
  1407. goto out_notify_apps;
  1408. }
  1409. /* If this resource is not going to be added to mid-layer, just notify
  1410. * applications and return. If this notification is about hiding a VSET
  1411. * resource, check if it was exposed already.
  1412. */
  1413. if (pinstance->ccn.hcam->notification_type ==
  1414. NOTIFICATION_TYPE_ENTRY_CHANGED &&
  1415. cfg_entry->resource_type == RES_TYPE_VSET) {
  1416. if (fw_version <= PMCRAID_FW_VERSION_1)
  1417. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1418. else
  1419. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1420. } else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
  1421. goto out_notify_apps;
  1422. }
  1423. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1424. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1425. rc = memcmp(&res->cfg_entry.resource_address,
  1426. &cfg_entry->resource_address,
  1427. sizeof(cfg_entry->resource_address));
  1428. if (!rc) {
  1429. new_entry = 0;
  1430. break;
  1431. }
  1432. }
  1433. if (new_entry) {
  1434. if (hidden_entry) {
  1435. spin_unlock_irqrestore(&pinstance->resource_lock,
  1436. lock_flags);
  1437. goto out_notify_apps;
  1438. }
  1439. /* If there are more number of resources than what driver can
  1440. * manage, do not notify the applications about the CCN. Just
  1441. * ignore this notifications and re-register the same HCAM
  1442. */
  1443. if (list_empty(&pinstance->free_res_q)) {
  1444. spin_unlock_irqrestore(&pinstance->resource_lock,
  1445. lock_flags);
  1446. pmcraid_err("too many resources attached\n");
  1447. spin_lock_irqsave(pinstance->host->host_lock,
  1448. host_lock_flags);
  1449. pmcraid_send_hcam(pinstance,
  1450. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1451. spin_unlock_irqrestore(pinstance->host->host_lock,
  1452. host_lock_flags);
  1453. return;
  1454. }
  1455. res = list_entry(pinstance->free_res_q.next,
  1456. struct pmcraid_resource_entry, queue);
  1457. list_del(&res->queue);
  1458. res->scsi_dev = NULL;
  1459. res->reset_progress = 0;
  1460. list_add_tail(&res->queue, &pinstance->used_res_q);
  1461. }
  1462. memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
  1463. if (pinstance->ccn.hcam->notification_type ==
  1464. NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
  1465. if (res->scsi_dev) {
  1466. if (fw_version <= PMCRAID_FW_VERSION_1)
  1467. res->cfg_entry.unique_flags1 &= 0x7F;
  1468. else
  1469. res->cfg_entry.array_id &= 0xFF;
  1470. res->change_detected = RES_CHANGE_DEL;
  1471. res->cfg_entry.resource_handle =
  1472. PMCRAID_INVALID_RES_HANDLE;
  1473. schedule_work(&pinstance->worker_q);
  1474. } else {
  1475. /* This may be one of the non-exposed resources */
  1476. list_move_tail(&res->queue, &pinstance->free_res_q);
  1477. }
  1478. } else if (!res->scsi_dev) {
  1479. res->change_detected = RES_CHANGE_ADD;
  1480. schedule_work(&pinstance->worker_q);
  1481. }
  1482. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1483. out_notify_apps:
  1484. /* Notify configuration changes to registered applications.*/
  1485. if (!pmcraid_disable_aen)
  1486. pmcraid_notify_ccn(pinstance);
  1487. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1488. if (cmd)
  1489. pmcraid_send_hcam_cmd(cmd);
  1490. }
  1491. /**
  1492. * pmcraid_get_error_info - return error string for an ioasc
  1493. * @ioasc: ioasc code
  1494. * Return Value
  1495. * none
  1496. */
  1497. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1498. {
  1499. int i;
  1500. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1501. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1502. return &pmcraid_ioasc_error_table[i];
  1503. }
  1504. return NULL;
  1505. }
  1506. /**
  1507. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1508. * @ioasc: ioasc code
  1509. * @cmd: pointer to command that resulted in 'ioasc'
  1510. */
  1511. static void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1512. {
  1513. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1514. if (error_info == NULL ||
  1515. cmd->drv_inst->current_log_level < error_info->log_level)
  1516. return;
  1517. /* log the error string */
  1518. pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
  1519. cmd->ioa_cb->ioarcb.cdb[0],
  1520. cmd->ioa_cb->ioarcb.resource_handle,
  1521. le32_to_cpu(ioasc), error_info->error_string);
  1522. }
  1523. /**
  1524. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1525. *
  1526. * @pinstance: pointer to per adapter instance structure
  1527. *
  1528. * Return value:
  1529. * none
  1530. */
  1531. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1532. {
  1533. struct pmcraid_hcam_ldn *hcam_ldn;
  1534. u32 ioasc;
  1535. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1536. pmcraid_info
  1537. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1538. pinstance->ldn.hcam->ilid,
  1539. pinstance->ldn.hcam->op_code,
  1540. pinstance->ldn.hcam->notification_type,
  1541. pinstance->ldn.hcam->notification_lost,
  1542. pinstance->ldn.hcam->flags,
  1543. pinstance->ldn.hcam->overlay_id);
  1544. /* log only the errors, no need to log informational log entries */
  1545. if (pinstance->ldn.hcam->notification_type !=
  1546. NOTIFICATION_TYPE_ERROR_LOG)
  1547. return;
  1548. if (pinstance->ldn.hcam->notification_lost ==
  1549. HOSTRCB_NOTIFICATIONS_LOST)
  1550. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1551. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1552. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1553. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1554. dev_info(&pinstance->pdev->dev,
  1555. "UnitAttention due to IOA Bus Reset\n");
  1556. scsi_report_bus_reset(
  1557. pinstance->host,
  1558. RES_BUS(hcam_ldn->error_log.fd_ra));
  1559. }
  1560. return;
  1561. }
  1562. /**
  1563. * pmcraid_process_ccn - Op done function for a CCN.
  1564. * @cmd: pointer to command struct
  1565. *
  1566. * This function is the op done function for a configuration
  1567. * change notification
  1568. *
  1569. * Return value:
  1570. * none
  1571. */
  1572. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1573. {
  1574. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1575. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1576. unsigned long lock_flags;
  1577. pinstance->ccn.cmd = NULL;
  1578. pmcraid_return_cmd(cmd);
  1579. /* If driver initiated IOA reset happened while this hcam was pending
  1580. * with IOA, or IOA bringdown sequence is in progress, no need to
  1581. * re-register the hcam
  1582. */
  1583. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1584. atomic_read(&pinstance->ccn.ignore) == 1) {
  1585. return;
  1586. } else if (ioasc) {
  1587. dev_info(&pinstance->pdev->dev,
  1588. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1589. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1590. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1591. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1592. } else {
  1593. pmcraid_handle_config_change(pinstance);
  1594. }
  1595. }
  1596. /**
  1597. * pmcraid_process_ldn - op done function for an LDN
  1598. * @cmd: pointer to command block
  1599. *
  1600. * Return value
  1601. * none
  1602. */
  1603. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1604. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
  1605. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1606. {
  1607. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1608. struct pmcraid_hcam_ldn *ldn_hcam =
  1609. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1610. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1611. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1612. unsigned long lock_flags;
  1613. /* return the command block back to freepool */
  1614. pinstance->ldn.cmd = NULL;
  1615. pmcraid_return_cmd(cmd);
  1616. /* If driver initiated IOA reset happened while this hcam was pending
  1617. * with IOA, no need to re-register the hcam as reset engine will do it
  1618. * once reset sequence is complete
  1619. */
  1620. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1621. atomic_read(&pinstance->ccn.ignore) == 1) {
  1622. return;
  1623. } else if (!ioasc) {
  1624. pmcraid_handle_error_log(pinstance);
  1625. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1626. spin_lock_irqsave(pinstance->host->host_lock,
  1627. lock_flags);
  1628. pmcraid_initiate_reset(pinstance);
  1629. spin_unlock_irqrestore(pinstance->host->host_lock,
  1630. lock_flags);
  1631. return;
  1632. }
  1633. if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
  1634. pinstance->timestamp_error = 1;
  1635. pmcraid_set_timestamp(cmd);
  1636. }
  1637. } else {
  1638. dev_info(&pinstance->pdev->dev,
  1639. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1640. }
  1641. /* send netlink message for HCAM notification if enabled */
  1642. if (!pmcraid_disable_aen)
  1643. pmcraid_notify_ldn(pinstance);
  1644. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1645. if (cmd)
  1646. pmcraid_send_hcam_cmd(cmd);
  1647. }
  1648. /**
  1649. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1650. *
  1651. * @pinstance: pointer per adapter instance structure
  1652. *
  1653. * Return Value
  1654. * none
  1655. */
  1656. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1657. {
  1658. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1659. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1660. }
  1661. /**
  1662. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1663. * @cmd: pointer to command used as part of reset sequence
  1664. */
  1665. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1666. {
  1667. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1668. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1669. * handling hcam response though it is not necessary. In order to
  1670. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1671. * re-send any more hcams
  1672. */
  1673. atomic_set(&pinstance->ccn.ignore, 1);
  1674. atomic_set(&pinstance->ldn.ignore, 1);
  1675. /* If adapter reset was forced as part of runtime reset sequence,
  1676. * start the reset sequence. Reset will be triggered even in case
  1677. * IOA unit_check.
  1678. */
  1679. if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
  1680. pinstance->ioa_unit_check) {
  1681. pinstance->force_ioa_reset = 0;
  1682. pinstance->ioa_unit_check = 0;
  1683. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1684. pmcraid_reset_alert(cmd);
  1685. return;
  1686. }
  1687. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1688. * one after the other. So CCN cancellation will be triggered by
  1689. * pmcraid_cancel_ldn itself.
  1690. */
  1691. pmcraid_cancel_ldn(cmd);
  1692. }
  1693. /**
  1694. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1695. * @pinstance: pointer to adapter instance structure
  1696. * Return Value
  1697. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1698. */
  1699. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1700. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1701. {
  1702. u32 intrs;
  1703. pmcraid_reinit_buffers(pinstance);
  1704. intrs = pmcraid_read_interrupts(pinstance);
  1705. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1706. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1707. if (!pinstance->interrupt_mode) {
  1708. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1709. pinstance->int_regs.
  1710. ioa_host_interrupt_mask_reg);
  1711. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1712. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1713. }
  1714. return 1;
  1715. } else {
  1716. return 0;
  1717. }
  1718. }
  1719. /**
  1720. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1721. * @cmd : pointer to reset command block
  1722. *
  1723. * Return Value
  1724. * none
  1725. */
  1726. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1727. {
  1728. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1729. u32 int_reg;
  1730. u32 doorbell;
  1731. /* There will be an interrupt when Transition to Operational bit is
  1732. * set so tasklet would execute next reset task. The timeout handler
  1733. * would re-initiate a reset
  1734. */
  1735. cmd->cmd_done = pmcraid_ioa_reset;
  1736. cmd->timer.data = (unsigned long)cmd;
  1737. cmd->timer.expires = jiffies +
  1738. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1739. cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
  1740. if (!timer_pending(&cmd->timer))
  1741. add_timer(&cmd->timer);
  1742. /* Enable destructive diagnostics on IOA if it is not yet in
  1743. * operational state
  1744. */
  1745. doorbell = DOORBELL_RUNTIME_RESET |
  1746. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1747. /* Since we do RESET_ALERT and Start BIST we have to again write
  1748. * MSIX Doorbell to indicate the interrupt mode
  1749. */
  1750. if (pinstance->interrupt_mode) {
  1751. iowrite32(DOORBELL_INTR_MODE_MSIX,
  1752. pinstance->int_regs.host_ioa_interrupt_reg);
  1753. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  1754. }
  1755. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1756. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1757. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1758. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1759. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1760. int_reg);
  1761. }
  1762. /**
  1763. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1764. *
  1765. * @pinstance: pointer to adapter instance structure
  1766. *
  1767. * Return Value
  1768. * none
  1769. */
  1770. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1771. {
  1772. pmcraid_info("%s is not yet implemented\n", __func__);
  1773. }
  1774. /**
  1775. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1776. * @pinstance: pointer to adapter instance structure
  1777. *
  1778. * This function fails all outstanding ops. If they are submitted to IOA
  1779. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1780. * otherwise just completes the commands and returns the cmd blocks to free
  1781. * pool.
  1782. *
  1783. * Return value:
  1784. * none
  1785. */
  1786. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1787. {
  1788. struct pmcraid_cmd *cmd, *temp;
  1789. unsigned long lock_flags;
  1790. /* pending command list is protected by pending_pool_lock. Its
  1791. * traversal must be done as within this lock
  1792. */
  1793. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1794. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1795. free_list) {
  1796. list_del(&cmd->free_list);
  1797. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1798. lock_flags);
  1799. cmd->ioa_cb->ioasa.ioasc =
  1800. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1801. cmd->ioa_cb->ioasa.ilid =
  1802. cpu_to_be32(PMCRAID_DRIVER_ILID);
  1803. /* In case the command timer is still running */
  1804. del_timer(&cmd->timer);
  1805. /* If this is an IO command, complete it by invoking scsi_done
  1806. * function. If this is one of the internal commands other
  1807. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1808. * complete it
  1809. */
  1810. if (cmd->scsi_cmd) {
  1811. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1812. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1813. scsi_cmd->result |= DID_ERROR << 16;
  1814. scsi_dma_unmap(scsi_cmd);
  1815. pmcraid_return_cmd(cmd);
  1816. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1817. le32_to_cpu(resp) >> 2,
  1818. cmd->ioa_cb->ioarcb.cdb[0],
  1819. scsi_cmd->result);
  1820. scsi_cmd->scsi_done(scsi_cmd);
  1821. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1822. cmd->cmd_done == pmcraid_erp_done) {
  1823. cmd->cmd_done(cmd);
  1824. } else if (cmd->cmd_done != pmcraid_ioa_reset &&
  1825. cmd->cmd_done != pmcraid_ioa_shutdown_done) {
  1826. pmcraid_return_cmd(cmd);
  1827. }
  1828. atomic_dec(&pinstance->outstanding_cmds);
  1829. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1830. }
  1831. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1832. }
  1833. /**
  1834. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1835. *
  1836. * @cmd: pointer to the cmd block to be used for entire reset process
  1837. *
  1838. * This function executes most of the steps required for IOA reset. This gets
  1839. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1840. * 'eh_' thread. Access to variables used for controlling the reset sequence is
  1841. * synchronized using host lock. Various functions called during reset process
  1842. * would make use of a single command block, pointer to which is also stored in
  1843. * adapter instance structure.
  1844. *
  1845. * Return Value
  1846. * None
  1847. */
  1848. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1849. {
  1850. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1851. u8 reset_complete = 0;
  1852. pinstance->ioa_reset_in_progress = 1;
  1853. if (pinstance->reset_cmd != cmd) {
  1854. pmcraid_err("reset is called with different command block\n");
  1855. pinstance->reset_cmd = cmd;
  1856. }
  1857. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1858. pinstance->ioa_state, cmd);
  1859. switch (pinstance->ioa_state) {
  1860. case IOA_STATE_DEAD:
  1861. /* If IOA is offline, whatever may be the reset reason, just
  1862. * return. callers might be waiting on the reset wait_q, wake
  1863. * up them
  1864. */
  1865. pmcraid_err("IOA is offline no reset is possible\n");
  1866. reset_complete = 1;
  1867. break;
  1868. case IOA_STATE_IN_BRINGDOWN:
  1869. /* we enter here, once ioa shutdown command is processed by IOA
  1870. * Alert IOA for a possible reset. If reset alert fails, IOA
  1871. * goes through hard-reset
  1872. */
  1873. pmcraid_disable_interrupts(pinstance, ~0);
  1874. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1875. pmcraid_reset_alert(cmd);
  1876. break;
  1877. case IOA_STATE_UNKNOWN:
  1878. /* We may be called during probe or resume. Some pre-processing
  1879. * is required for prior to reset
  1880. */
  1881. scsi_block_requests(pinstance->host);
  1882. /* If asked to reset while IOA was processing responses or
  1883. * there are any error responses then IOA may require
  1884. * hard-reset.
  1885. */
  1886. if (pinstance->ioa_hard_reset == 0) {
  1887. if (ioread32(pinstance->ioa_status) &
  1888. INTRS_TRANSITION_TO_OPERATIONAL) {
  1889. pmcraid_info("sticky bit set, bring-up\n");
  1890. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1891. pmcraid_reinit_cmdblk(cmd);
  1892. pmcraid_identify_hrrq(cmd);
  1893. } else {
  1894. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1895. pmcraid_soft_reset(cmd);
  1896. }
  1897. } else {
  1898. /* Alert IOA of a possible reset and wait for critical
  1899. * operation in progress bit to reset
  1900. */
  1901. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1902. pmcraid_reset_alert(cmd);
  1903. }
  1904. break;
  1905. case IOA_STATE_IN_RESET_ALERT:
  1906. /* If critical operation in progress bit is reset or wait gets
  1907. * timed out, reset proceeds with starting BIST on the IOA.
  1908. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1909. * they are 3 or more, reset engine marks IOA dead and returns
  1910. */
  1911. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1912. pmcraid_start_bist(cmd);
  1913. break;
  1914. case IOA_STATE_IN_HARD_RESET:
  1915. pinstance->ioa_reset_attempts++;
  1916. /* retry reset if we haven't reached maximum allowed limit */
  1917. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1918. pinstance->ioa_reset_attempts = 0;
  1919. pmcraid_err("IOA didn't respond marking it as dead\n");
  1920. pinstance->ioa_state = IOA_STATE_DEAD;
  1921. if (pinstance->ioa_bringdown)
  1922. pmcraid_notify_ioastate(pinstance,
  1923. PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
  1924. else
  1925. pmcraid_notify_ioastate(pinstance,
  1926. PMC_DEVICE_EVENT_RESET_FAILED);
  1927. reset_complete = 1;
  1928. break;
  1929. }
  1930. /* Once either bist or pci reset is done, restore PCI config
  1931. * space. If this fails, proceed with hard reset again
  1932. */
  1933. pci_restore_state(pinstance->pdev);
  1934. /* fail all pending commands */
  1935. pmcraid_fail_outstanding_cmds(pinstance);
  1936. /* check if unit check is active, if so extract dump */
  1937. if (pinstance->ioa_unit_check) {
  1938. pmcraid_info("unit check is active\n");
  1939. pinstance->ioa_unit_check = 0;
  1940. pmcraid_get_dump(pinstance);
  1941. pinstance->ioa_reset_attempts--;
  1942. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1943. pmcraid_reset_alert(cmd);
  1944. break;
  1945. }
  1946. /* if the reset reason is to bring-down the ioa, we might be
  1947. * done with the reset restore pci_config_space and complete
  1948. * the reset
  1949. */
  1950. if (pinstance->ioa_bringdown) {
  1951. pmcraid_info("bringing down the adapter\n");
  1952. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1953. pinstance->ioa_bringdown = 0;
  1954. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1955. pmcraid_notify_ioastate(pinstance,
  1956. PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
  1957. reset_complete = 1;
  1958. } else {
  1959. /* bring-up IOA, so proceed with soft reset
  1960. * Reinitialize hrrq_buffers and their indices also
  1961. * enable interrupts after a pci_restore_state
  1962. */
  1963. if (pmcraid_reset_enable_ioa(pinstance)) {
  1964. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1965. pmcraid_info("bringing up the adapter\n");
  1966. pmcraid_reinit_cmdblk(cmd);
  1967. pmcraid_identify_hrrq(cmd);
  1968. } else {
  1969. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1970. pmcraid_soft_reset(cmd);
  1971. }
  1972. }
  1973. break;
  1974. case IOA_STATE_IN_SOFT_RESET:
  1975. /* TRANSITION TO OPERATIONAL is on so start initialization
  1976. * sequence
  1977. */
  1978. pmcraid_info("In softreset proceeding with bring-up\n");
  1979. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1980. /* Initialization commands start with HRRQ identification. From
  1981. * now on tasklet completes most of the commands as IOA is up
  1982. * and intrs are enabled
  1983. */
  1984. pmcraid_identify_hrrq(cmd);
  1985. break;
  1986. case IOA_STATE_IN_BRINGUP:
  1987. /* we are done with bringing up of IOA, change the ioa_state to
  1988. * operational and wake up any waiters
  1989. */
  1990. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  1991. reset_complete = 1;
  1992. break;
  1993. case IOA_STATE_OPERATIONAL:
  1994. default:
  1995. /* When IOA is operational and a reset is requested, check for
  1996. * the reset reason. If reset is to bring down IOA, unregister
  1997. * HCAMs and initiate shutdown; if adapter reset is forced then
  1998. * restart reset sequence again
  1999. */
  2000. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  2001. pinstance->force_ioa_reset == 0) {
  2002. pmcraid_notify_ioastate(pinstance,
  2003. PMC_DEVICE_EVENT_RESET_SUCCESS);
  2004. reset_complete = 1;
  2005. } else {
  2006. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  2007. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  2008. pmcraid_reinit_cmdblk(cmd);
  2009. pmcraid_unregister_hcams(cmd);
  2010. }
  2011. break;
  2012. }
  2013. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  2014. * OPERATIONAL. Reset all control variables used during reset, wake up
  2015. * any waiting threads and let the SCSI mid-layer send commands. Note
  2016. * that host_lock must be held before invoking scsi_report_bus_reset.
  2017. */
  2018. if (reset_complete) {
  2019. pinstance->ioa_reset_in_progress = 0;
  2020. pinstance->ioa_reset_attempts = 0;
  2021. pinstance->reset_cmd = NULL;
  2022. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2023. pinstance->ioa_bringdown = 0;
  2024. pmcraid_return_cmd(cmd);
  2025. /* If target state is to bring up the adapter, proceed with
  2026. * hcam registration and resource exposure to mid-layer.
  2027. */
  2028. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  2029. pmcraid_register_hcams(pinstance);
  2030. wake_up_all(&pinstance->reset_wait_q);
  2031. }
  2032. return;
  2033. }
  2034. /**
  2035. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  2036. * ISR/tasklet during error interrupts including IOA unit check. If reset
  2037. * is already in progress, it just returns, otherwise initiates IOA reset
  2038. * to bring IOA up to operational state.
  2039. *
  2040. * @pinstance: pointer to adapter instance structure
  2041. *
  2042. * Return value
  2043. * none
  2044. */
  2045. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  2046. {
  2047. struct pmcraid_cmd *cmd;
  2048. /* If the reset is already in progress, just return, otherwise start
  2049. * reset sequence and return
  2050. */
  2051. if (!pinstance->ioa_reset_in_progress) {
  2052. scsi_block_requests(pinstance->host);
  2053. cmd = pmcraid_get_free_cmd(pinstance);
  2054. if (cmd == NULL) {
  2055. pmcraid_err("no cmnd blocks for initiate_reset\n");
  2056. return;
  2057. }
  2058. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2059. pinstance->reset_cmd = cmd;
  2060. pinstance->force_ioa_reset = 1;
  2061. pmcraid_notify_ioastate(pinstance,
  2062. PMC_DEVICE_EVENT_RESET_START);
  2063. pmcraid_ioa_reset(cmd);
  2064. }
  2065. }
  2066. /**
  2067. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  2068. * or bringdown IOA
  2069. * @pinstance: pointer adapter instance structure
  2070. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  2071. * @target_state: expected target state after reset
  2072. *
  2073. * Note: This command initiates reset and waits for its completion. Hence this
  2074. * should not be called from isr/timer/tasklet functions (timeout handlers,
  2075. * error response handlers and interrupt handlers).
  2076. *
  2077. * Return Value
  2078. * 1 in case ioa_state is not target_state, 0 otherwise.
  2079. */
  2080. static int pmcraid_reset_reload(
  2081. struct pmcraid_instance *pinstance,
  2082. u8 shutdown_type,
  2083. u8 target_state
  2084. )
  2085. {
  2086. struct pmcraid_cmd *reset_cmd = NULL;
  2087. unsigned long lock_flags;
  2088. int reset = 1;
  2089. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2090. if (pinstance->ioa_reset_in_progress) {
  2091. pmcraid_info("reset_reload: reset is already in progress\n");
  2092. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2093. wait_event(pinstance->reset_wait_q,
  2094. !pinstance->ioa_reset_in_progress);
  2095. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2096. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2097. spin_unlock_irqrestore(pinstance->host->host_lock,
  2098. lock_flags);
  2099. pmcraid_info("reset_reload: IOA is dead\n");
  2100. return reset;
  2101. } else if (pinstance->ioa_state == target_state) {
  2102. reset = 0;
  2103. }
  2104. }
  2105. if (reset) {
  2106. pmcraid_info("reset_reload: proceeding with reset\n");
  2107. scsi_block_requests(pinstance->host);
  2108. reset_cmd = pmcraid_get_free_cmd(pinstance);
  2109. if (reset_cmd == NULL) {
  2110. pmcraid_err("no free cmnd for reset_reload\n");
  2111. spin_unlock_irqrestore(pinstance->host->host_lock,
  2112. lock_flags);
  2113. return reset;
  2114. }
  2115. if (shutdown_type == SHUTDOWN_NORMAL)
  2116. pinstance->ioa_bringdown = 1;
  2117. pinstance->ioa_shutdown_type = shutdown_type;
  2118. pinstance->reset_cmd = reset_cmd;
  2119. pinstance->force_ioa_reset = reset;
  2120. pmcraid_info("reset_reload: initiating reset\n");
  2121. pmcraid_ioa_reset(reset_cmd);
  2122. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2123. pmcraid_info("reset_reload: waiting for reset to complete\n");
  2124. wait_event(pinstance->reset_wait_q,
  2125. !pinstance->ioa_reset_in_progress);
  2126. pmcraid_info("reset_reload: reset is complete !!\n");
  2127. scsi_unblock_requests(pinstance->host);
  2128. if (pinstance->ioa_state == target_state)
  2129. reset = 0;
  2130. }
  2131. return reset;
  2132. }
  2133. /**
  2134. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  2135. *
  2136. * @pinstance: pointer to adapter instance structure
  2137. *
  2138. * Return Value
  2139. * whatever is returned from pmcraid_reset_reload
  2140. */
  2141. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  2142. {
  2143. return pmcraid_reset_reload(pinstance,
  2144. SHUTDOWN_NORMAL,
  2145. IOA_STATE_UNKNOWN);
  2146. }
  2147. /**
  2148. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  2149. *
  2150. * @pinstance: pointer to adapter instance structure
  2151. *
  2152. * Return Value
  2153. * whatever is returned from pmcraid_reset_reload
  2154. */
  2155. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  2156. {
  2157. pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
  2158. return pmcraid_reset_reload(pinstance,
  2159. SHUTDOWN_NONE,
  2160. IOA_STATE_OPERATIONAL);
  2161. }
  2162. /**
  2163. * pmcraid_request_sense - Send request sense to a device
  2164. * @cmd: pmcraid command struct
  2165. *
  2166. * This function sends a request sense to a device as a result of a check
  2167. * condition. This method re-uses the same command block that failed earlier.
  2168. */
  2169. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  2170. {
  2171. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2172. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2173. /* allocate DMAable memory for sense buffers */
  2174. cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
  2175. SCSI_SENSE_BUFFERSIZE,
  2176. &cmd->sense_buffer_dma);
  2177. if (cmd->sense_buffer == NULL) {
  2178. pmcraid_err
  2179. ("couldn't allocate sense buffer for request sense\n");
  2180. pmcraid_erp_done(cmd);
  2181. return;
  2182. }
  2183. /* re-use the command block */
  2184. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  2185. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2186. ioarcb->request_flags0 = (SYNC_COMPLETE |
  2187. NO_LINK_DESCS |
  2188. INHIBIT_UL_CHECK);
  2189. ioarcb->request_type = REQ_TYPE_SCSI;
  2190. ioarcb->cdb[0] = REQUEST_SENSE;
  2191. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2192. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2193. offsetof(struct pmcraid_ioarcb,
  2194. add_data.u.ioadl[0]));
  2195. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  2196. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2197. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  2198. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2199. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  2200. /* request sense might be called as part of error response processing
  2201. * which runs in tasklets context. It is possible that mid-layer might
  2202. * schedule queuecommand during this time, hence, writting to IOARRIN
  2203. * must be protect by host_lock
  2204. */
  2205. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  2206. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2207. pmcraid_timeout_handler);
  2208. }
  2209. /**
  2210. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  2211. * @cmd: command that failed
  2212. * @sense: true if request_sense is required after cancel all
  2213. *
  2214. * This function sends a cancel all to a device to clear the queue.
  2215. */
  2216. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
  2217. {
  2218. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2219. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2220. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2221. void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
  2222. : pmcraid_request_sense;
  2223. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2224. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2225. ioarcb->request_type = REQ_TYPE_IOACMD;
  2226. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2227. if (RES_IS_GSCSI(res->cfg_entry))
  2228. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2229. ioarcb->ioadl_bus_addr = 0;
  2230. ioarcb->ioadl_length = 0;
  2231. ioarcb->data_transfer_length = 0;
  2232. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  2233. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2234. * schedule queuecommand while we are doing this
  2235. */
  2236. pmcraid_send_cmd(cmd, cmd_done,
  2237. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2238. pmcraid_timeout_handler);
  2239. }
  2240. /**
  2241. * pmcraid_frame_auto_sense: frame fixed format sense information
  2242. *
  2243. * @cmd: pointer to failing command block
  2244. *
  2245. * Return value
  2246. * none
  2247. */
  2248. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2249. {
  2250. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2251. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2252. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2253. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2254. u32 failing_lba = 0;
  2255. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2256. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2257. if (RES_IS_VSET(res->cfg_entry) &&
  2258. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2259. ioasa->u.vset.failing_lba_hi != 0) {
  2260. sense_buf[0] = 0x72;
  2261. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2262. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2263. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2264. sense_buf[7] = 12;
  2265. sense_buf[8] = 0;
  2266. sense_buf[9] = 0x0A;
  2267. sense_buf[10] = 0x80;
  2268. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2269. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2270. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2271. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2272. sense_buf[15] = failing_lba & 0x000000ff;
  2273. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2274. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2275. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2276. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2277. sense_buf[19] = failing_lba & 0x000000ff;
  2278. } else {
  2279. sense_buf[0] = 0x70;
  2280. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2281. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2282. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2283. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2284. if (RES_IS_VSET(res->cfg_entry))
  2285. failing_lba =
  2286. le32_to_cpu(ioasa->u.
  2287. vset.failing_lba_lo);
  2288. sense_buf[0] |= 0x80;
  2289. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2290. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2291. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2292. sense_buf[6] = failing_lba & 0xff;
  2293. }
  2294. sense_buf[7] = 6; /* additional length */
  2295. }
  2296. }
  2297. /**
  2298. * pmcraid_error_handler - Error response handlers for a SCSI op
  2299. * @cmd: pointer to pmcraid_cmd that has failed
  2300. *
  2301. * This function determines whether or not to initiate ERP on the affected
  2302. * device. This is called from a tasklet, which doesn't hold any locks.
  2303. *
  2304. * Return value:
  2305. * 0 it caller can complete the request, otherwise 1 where in error
  2306. * handler itself completes the request and returns the command block
  2307. * back to free-pool
  2308. */
  2309. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2310. {
  2311. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2312. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2313. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2314. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2315. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2316. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2317. u32 sense_copied = 0;
  2318. if (!res) {
  2319. pmcraid_info("resource pointer is NULL\n");
  2320. return 0;
  2321. }
  2322. /* If this was a SCSI read/write command keep count of errors */
  2323. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2324. atomic_inc(&res->read_failures);
  2325. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2326. atomic_inc(&res->write_failures);
  2327. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2328. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2329. pmcraid_frame_auto_sense(cmd);
  2330. }
  2331. /* Log IOASC/IOASA information based on user settings */
  2332. pmcraid_ioasc_logger(ioasc, cmd);
  2333. switch (masked_ioasc) {
  2334. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2335. scsi_cmd->result |= (DID_ABORT << 16);
  2336. break;
  2337. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2338. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2339. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2340. break;
  2341. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2342. res->sync_reqd = 1;
  2343. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2344. break;
  2345. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2346. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2347. break;
  2348. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2349. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2350. if (!res->reset_progress)
  2351. scsi_report_bus_reset(pinstance->host,
  2352. scsi_cmd->device->channel);
  2353. scsi_cmd->result |= (DID_ERROR << 16);
  2354. break;
  2355. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2356. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2357. res->sync_reqd = 1;
  2358. /* if check_condition is not active return with error otherwise
  2359. * get/frame the sense buffer
  2360. */
  2361. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2362. SAM_STAT_CHECK_CONDITION &&
  2363. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2364. return 0;
  2365. /* If we have auto sense data as part of IOASA pass it to
  2366. * mid-layer
  2367. */
  2368. if (ioasa->auto_sense_length != 0) {
  2369. short sense_len = ioasa->auto_sense_length;
  2370. int data_size = min_t(u16, le16_to_cpu(sense_len),
  2371. SCSI_SENSE_BUFFERSIZE);
  2372. memcpy(scsi_cmd->sense_buffer,
  2373. ioasa->sense_data,
  2374. data_size);
  2375. sense_copied = 1;
  2376. }
  2377. if (RES_IS_GSCSI(res->cfg_entry))
  2378. pmcraid_cancel_all(cmd, sense_copied);
  2379. else if (sense_copied)
  2380. pmcraid_erp_done(cmd);
  2381. else
  2382. pmcraid_request_sense(cmd);
  2383. return 1;
  2384. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2385. break;
  2386. default:
  2387. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2388. scsi_cmd->result |= (DID_ERROR << 16);
  2389. break;
  2390. }
  2391. return 0;
  2392. }
  2393. /**
  2394. * pmcraid_reset_device - device reset handler functions
  2395. *
  2396. * @scsi_cmd: scsi command struct
  2397. * @modifier: reset modifier indicating the reset sequence to be performed
  2398. *
  2399. * This function issues a device reset to the affected device.
  2400. * A LUN reset will be sent to the device first. If that does
  2401. * not work, a target reset will be sent.
  2402. *
  2403. * Return value:
  2404. * SUCCESS / FAILED
  2405. */
  2406. static int pmcraid_reset_device(
  2407. struct scsi_cmnd *scsi_cmd,
  2408. unsigned long timeout,
  2409. u8 modifier
  2410. )
  2411. {
  2412. struct pmcraid_cmd *cmd;
  2413. struct pmcraid_instance *pinstance;
  2414. struct pmcraid_resource_entry *res;
  2415. struct pmcraid_ioarcb *ioarcb;
  2416. unsigned long lock_flags;
  2417. u32 ioasc;
  2418. pinstance =
  2419. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2420. res = scsi_cmd->device->hostdata;
  2421. if (!res) {
  2422. sdev_printk(KERN_ERR, scsi_cmd->device,
  2423. "reset_device: NULL resource pointer\n");
  2424. return FAILED;
  2425. }
  2426. /* If adapter is currently going through reset/reload, return failed.
  2427. * This will force the mid-layer to call _eh_bus/host reset, which
  2428. * will then go to sleep and wait for the reset to complete
  2429. */
  2430. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2431. if (pinstance->ioa_reset_in_progress ||
  2432. pinstance->ioa_state == IOA_STATE_DEAD) {
  2433. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2434. return FAILED;
  2435. }
  2436. res->reset_progress = 1;
  2437. pmcraid_info("Resetting %s resource with addr %x\n",
  2438. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2439. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2440. le32_to_cpu(res->cfg_entry.resource_address));
  2441. /* get a free cmd block */
  2442. cmd = pmcraid_get_free_cmd(pinstance);
  2443. if (cmd == NULL) {
  2444. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2445. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2446. return FAILED;
  2447. }
  2448. ioarcb = &cmd->ioa_cb->ioarcb;
  2449. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2450. ioarcb->request_type = REQ_TYPE_IOACMD;
  2451. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2452. /* Initialize reset modifier bits */
  2453. if (modifier)
  2454. modifier = ENABLE_RESET_MODIFIER | modifier;
  2455. ioarcb->cdb[1] = modifier;
  2456. init_completion(&cmd->wait_for_completion);
  2457. cmd->completion_req = 1;
  2458. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2459. cmd->ioa_cb->ioarcb.cdb[0],
  2460. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2461. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2462. pmcraid_send_cmd(cmd,
  2463. pmcraid_internal_done,
  2464. timeout,
  2465. pmcraid_timeout_handler);
  2466. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2467. /* RESET_DEVICE command completes after all pending IOARCBs are
  2468. * completed. Once this command is completed, pmcraind_internal_done
  2469. * will wake up the 'completion' queue.
  2470. */
  2471. wait_for_completion(&cmd->wait_for_completion);
  2472. /* complete the command here itself and return the command block
  2473. * to free list
  2474. */
  2475. pmcraid_return_cmd(cmd);
  2476. res->reset_progress = 0;
  2477. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2478. /* set the return value based on the returned ioasc */
  2479. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2480. }
  2481. /**
  2482. * _pmcraid_io_done - helper for pmcraid_io_done function
  2483. *
  2484. * @cmd: pointer to pmcraid command struct
  2485. * @reslen: residual data length to be set in the ioasa
  2486. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2487. *
  2488. * This function is invoked by pmcraid_io_done to complete mid-layer
  2489. * scsi ops.
  2490. *
  2491. * Return value:
  2492. * 0 if caller is required to return it to free_pool. Returns 1 if
  2493. * caller need not worry about freeing command block as error handler
  2494. * will take care of that.
  2495. */
  2496. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2497. {
  2498. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2499. int rc = 0;
  2500. scsi_set_resid(scsi_cmd, reslen);
  2501. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2502. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2503. cmd->ioa_cb->ioarcb.cdb[0],
  2504. ioasc, scsi_cmd->result);
  2505. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2506. rc = pmcraid_error_handler(cmd);
  2507. if (rc == 0) {
  2508. scsi_dma_unmap(scsi_cmd);
  2509. scsi_cmd->scsi_done(scsi_cmd);
  2510. }
  2511. return rc;
  2512. }
  2513. /**
  2514. * pmcraid_io_done - SCSI completion function
  2515. *
  2516. * @cmd: pointer to pmcraid command struct
  2517. *
  2518. * This function is invoked by tasklet/mid-layer error handler to completing
  2519. * the SCSI ops sent from mid-layer.
  2520. *
  2521. * Return value
  2522. * none
  2523. */
  2524. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2525. {
  2526. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2527. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2528. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2529. pmcraid_return_cmd(cmd);
  2530. }
  2531. /**
  2532. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2533. *
  2534. * @cmd: command block of the command to be aborted
  2535. *
  2536. * Return Value:
  2537. * returns pointer to command structure used as cancelling cmd
  2538. */
  2539. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2540. {
  2541. struct pmcraid_cmd *cancel_cmd;
  2542. struct pmcraid_instance *pinstance;
  2543. struct pmcraid_resource_entry *res;
  2544. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2545. res = cmd->scsi_cmd->device->hostdata;
  2546. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2547. if (cancel_cmd == NULL) {
  2548. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2549. return NULL;
  2550. }
  2551. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2552. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2553. cmd->ioa_cb->ioarcb.cdb[0],
  2554. cmd->ioa_cb->ioarcb.response_handle >> 2);
  2555. init_completion(&cancel_cmd->wait_for_completion);
  2556. cancel_cmd->completion_req = 1;
  2557. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2558. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2559. cancel_cmd->ioa_cb->ioarcb.cdb[0],
  2560. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2561. pmcraid_send_cmd(cancel_cmd,
  2562. pmcraid_internal_done,
  2563. PMCRAID_INTERNAL_TIMEOUT,
  2564. pmcraid_timeout_handler);
  2565. return cancel_cmd;
  2566. }
  2567. /**
  2568. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2569. *
  2570. * @cancel_cmd: command block use as cancelling command
  2571. *
  2572. * Return Value:
  2573. * returns SUCCESS if ABORT TASK has good completion
  2574. * otherwise FAILED
  2575. */
  2576. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2577. {
  2578. struct pmcraid_resource_entry *res;
  2579. u32 ioasc;
  2580. wait_for_completion(&cancel_cmd->wait_for_completion);
  2581. res = cancel_cmd->res;
  2582. cancel_cmd->res = NULL;
  2583. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2584. /* If the abort task is not timed out we will get a Good completion
  2585. * as sense_key, otherwise we may get one the following responses
  2586. * due to subsequent bus reset or device reset. In case IOASC is
  2587. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2588. */
  2589. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2590. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2591. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2592. res->sync_reqd = 1;
  2593. ioasc = 0;
  2594. }
  2595. /* complete the command here itself */
  2596. pmcraid_return_cmd(cancel_cmd);
  2597. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2598. }
  2599. /**
  2600. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2601. *
  2602. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2603. * mid-layer ensures that no other commands are queued. This
  2604. * never gets called under interrupt, but a separate eh thread.
  2605. *
  2606. * Return value:
  2607. * SUCCESS / FAILED
  2608. */
  2609. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2610. {
  2611. struct pmcraid_instance *pinstance;
  2612. struct pmcraid_cmd *cmd;
  2613. struct pmcraid_resource_entry *res;
  2614. unsigned long host_lock_flags;
  2615. unsigned long pending_lock_flags;
  2616. struct pmcraid_cmd *cancel_cmd = NULL;
  2617. int cmd_found = 0;
  2618. int rc = FAILED;
  2619. pinstance =
  2620. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2621. scmd_printk(KERN_INFO, scsi_cmd,
  2622. "I/O command timed out, aborting it.\n");
  2623. res = scsi_cmd->device->hostdata;
  2624. if (res == NULL)
  2625. return rc;
  2626. /* If we are currently going through reset/reload, return failed.
  2627. * This will force the mid-layer to eventually call
  2628. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2629. * reset to complete
  2630. */
  2631. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2632. if (pinstance->ioa_reset_in_progress ||
  2633. pinstance->ioa_state == IOA_STATE_DEAD) {
  2634. spin_unlock_irqrestore(pinstance->host->host_lock,
  2635. host_lock_flags);
  2636. return rc;
  2637. }
  2638. /* loop over pending cmd list to find cmd corresponding to this
  2639. * scsi_cmd. Note that this command might not have been completed
  2640. * already. locking: all pending commands are protected with
  2641. * pending_pool_lock.
  2642. */
  2643. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2644. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2645. if (cmd->scsi_cmd == scsi_cmd) {
  2646. cmd_found = 1;
  2647. break;
  2648. }
  2649. }
  2650. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2651. pending_lock_flags);
  2652. /* If the command to be aborted was given to IOA and still pending with
  2653. * it, send ABORT_TASK to abort this and wait for its completion
  2654. */
  2655. if (cmd_found)
  2656. cancel_cmd = pmcraid_abort_cmd(cmd);
  2657. spin_unlock_irqrestore(pinstance->host->host_lock,
  2658. host_lock_flags);
  2659. if (cancel_cmd) {
  2660. cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
  2661. rc = pmcraid_abort_complete(cancel_cmd);
  2662. }
  2663. return cmd_found ? rc : SUCCESS;
  2664. }
  2665. /**
  2666. * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
  2667. *
  2668. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2669. *
  2670. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2671. * Since these are called from mid-layer EH thread, no other IO will be queued
  2672. * to the resource being reset. However, control path (IOCTL) may be active so
  2673. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2674. * takes care by locking/unlocking host_lock.
  2675. *
  2676. * Return value
  2677. * SUCCESS or FAILED
  2678. */
  2679. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2680. {
  2681. scmd_printk(KERN_INFO, scmd,
  2682. "resetting device due to an I/O command timeout.\n");
  2683. return pmcraid_reset_device(scmd,
  2684. PMCRAID_INTERNAL_TIMEOUT,
  2685. RESET_DEVICE_LUN);
  2686. }
  2687. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2688. {
  2689. scmd_printk(KERN_INFO, scmd,
  2690. "Doing bus reset due to an I/O command timeout.\n");
  2691. return pmcraid_reset_device(scmd,
  2692. PMCRAID_RESET_BUS_TIMEOUT,
  2693. RESET_DEVICE_BUS);
  2694. }
  2695. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2696. {
  2697. scmd_printk(KERN_INFO, scmd,
  2698. "Doing target reset due to an I/O command timeout.\n");
  2699. return pmcraid_reset_device(scmd,
  2700. PMCRAID_INTERNAL_TIMEOUT,
  2701. RESET_DEVICE_TARGET);
  2702. }
  2703. /**
  2704. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2705. *
  2706. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2707. *
  2708. * Initiates adapter reset to bring it up to operational state
  2709. *
  2710. * Return value
  2711. * SUCCESS or FAILED
  2712. */
  2713. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2714. {
  2715. unsigned long interval = 10000; /* 10 seconds interval */
  2716. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2717. struct pmcraid_instance *pinstance =
  2718. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2719. /* wait for an additional 150 seconds just in case firmware could come
  2720. * up and if it could complete all the pending commands excluding the
  2721. * two HCAM (CCN and LDN).
  2722. */
  2723. while (waits--) {
  2724. if (atomic_read(&pinstance->outstanding_cmds) <=
  2725. PMCRAID_MAX_HCAM_CMD)
  2726. return SUCCESS;
  2727. msleep(interval);
  2728. }
  2729. dev_err(&pinstance->pdev->dev,
  2730. "Adapter being reset due to an I/O command timeout.\n");
  2731. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2732. }
  2733. /**
  2734. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2735. * @cmd: pmcraid command struct
  2736. * @sgcount: count of scatter-gather elements
  2737. *
  2738. * Return value
  2739. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2740. * or external IOADLs
  2741. */
  2742. static struct pmcraid_ioadl_desc *
  2743. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2744. {
  2745. struct pmcraid_ioadl_desc *ioadl;
  2746. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2747. int ioadl_count = 0;
  2748. if (ioarcb->add_cmd_param_length)
  2749. ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
  2750. ioarcb->ioadl_length =
  2751. sizeof(struct pmcraid_ioadl_desc) * sgcount;
  2752. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2753. /* external ioadls start at offset 0x80 from control_block
  2754. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2755. * It is necessary to indicate to firmware that driver is
  2756. * using ioadls to be treated as external to IOARCB.
  2757. */
  2758. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  2759. ioarcb->ioadl_bus_addr =
  2760. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2761. offsetof(struct pmcraid_ioarcb,
  2762. add_data.u.ioadl[3]));
  2763. ioadl = &ioarcb->add_data.u.ioadl[3];
  2764. } else {
  2765. ioarcb->ioadl_bus_addr =
  2766. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2767. offsetof(struct pmcraid_ioarcb,
  2768. add_data.u.ioadl[ioadl_count]));
  2769. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2770. ioarcb->ioarcb_bus_addr |=
  2771. DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
  2772. }
  2773. return ioadl;
  2774. }
  2775. /**
  2776. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2777. * @pinstance: pointer to adapter instance structure
  2778. * @cmd: pmcraid command struct
  2779. *
  2780. * This function is invoked by queuecommand entry point while sending a command
  2781. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2782. *
  2783. * Return value:
  2784. * 0 on success or -1 on failure
  2785. */
  2786. static int pmcraid_build_ioadl(
  2787. struct pmcraid_instance *pinstance,
  2788. struct pmcraid_cmd *cmd
  2789. )
  2790. {
  2791. int i, nseg;
  2792. struct scatterlist *sglist;
  2793. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2794. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2795. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2796. u32 length = scsi_bufflen(scsi_cmd);
  2797. if (!length)
  2798. return 0;
  2799. nseg = scsi_dma_map(scsi_cmd);
  2800. if (nseg < 0) {
  2801. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2802. return -1;
  2803. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2804. scsi_dma_unmap(scsi_cmd);
  2805. scmd_printk(KERN_ERR, scsi_cmd,
  2806. "sg count is (%d) more than allowed!\n", nseg);
  2807. return -1;
  2808. }
  2809. /* Initialize IOARCB data transfer length fields */
  2810. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2811. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2812. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2813. ioarcb->data_transfer_length = cpu_to_le32(length);
  2814. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2815. /* Initialize IOADL descriptor addresses */
  2816. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2817. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2818. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2819. ioadl[i].flags = 0;
  2820. }
  2821. /* setup last descriptor */
  2822. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  2823. return 0;
  2824. }
  2825. /**
  2826. * pmcraid_free_sglist - Frees an allocated SG buffer list
  2827. * @sglist: scatter/gather list pointer
  2828. *
  2829. * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
  2830. *
  2831. * Return value:
  2832. * none
  2833. */
  2834. static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
  2835. {
  2836. int i;
  2837. for (i = 0; i < sglist->num_sg; i++)
  2838. __free_pages(sg_page(&(sglist->scatterlist[i])),
  2839. sglist->order);
  2840. kfree(sglist);
  2841. }
  2842. /**
  2843. * pmcraid_alloc_sglist - Allocates memory for a SG list
  2844. * @buflen: buffer length
  2845. *
  2846. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  2847. * list.
  2848. *
  2849. * Return value
  2850. * pointer to sglist / NULL on failure
  2851. */
  2852. static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
  2853. {
  2854. struct pmcraid_sglist *sglist;
  2855. struct scatterlist *scatterlist;
  2856. struct page *page;
  2857. int num_elem, i, j;
  2858. int sg_size;
  2859. int order;
  2860. int bsize_elem;
  2861. sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
  2862. order = (sg_size > 0) ? get_order(sg_size) : 0;
  2863. bsize_elem = PAGE_SIZE * (1 << order);
  2864. /* Determine the actual number of sg entries needed */
  2865. if (buflen % bsize_elem)
  2866. num_elem = (buflen / bsize_elem) + 1;
  2867. else
  2868. num_elem = buflen / bsize_elem;
  2869. /* Allocate a scatter/gather list for the DMA */
  2870. sglist = kzalloc(sizeof(struct pmcraid_sglist) +
  2871. (sizeof(struct scatterlist) * (num_elem - 1)),
  2872. GFP_KERNEL);
  2873. if (sglist == NULL)
  2874. return NULL;
  2875. scatterlist = sglist->scatterlist;
  2876. sg_init_table(scatterlist, num_elem);
  2877. sglist->order = order;
  2878. sglist->num_sg = num_elem;
  2879. sg_size = buflen;
  2880. for (i = 0; i < num_elem; i++) {
  2881. page = alloc_pages(GFP_KERNEL|GFP_DMA|__GFP_ZERO, order);
  2882. if (!page) {
  2883. for (j = i - 1; j >= 0; j--)
  2884. __free_pages(sg_page(&scatterlist[j]), order);
  2885. kfree(sglist);
  2886. return NULL;
  2887. }
  2888. sg_set_page(&scatterlist[i], page,
  2889. sg_size < bsize_elem ? sg_size : bsize_elem, 0);
  2890. sg_size -= bsize_elem;
  2891. }
  2892. return sglist;
  2893. }
  2894. /**
  2895. * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
  2896. * @sglist: scatter/gather list pointer
  2897. * @buffer: buffer pointer
  2898. * @len: buffer length
  2899. * @direction: data transfer direction
  2900. *
  2901. * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
  2902. *
  2903. * Return value:
  2904. * 0 on success / other on failure
  2905. */
  2906. static int pmcraid_copy_sglist(
  2907. struct pmcraid_sglist *sglist,
  2908. unsigned long buffer,
  2909. u32 len,
  2910. int direction
  2911. )
  2912. {
  2913. struct scatterlist *scatterlist;
  2914. void *kaddr;
  2915. int bsize_elem;
  2916. int i;
  2917. int rc = 0;
  2918. /* Determine the actual number of bytes per element */
  2919. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  2920. scatterlist = sglist->scatterlist;
  2921. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  2922. struct page *page = sg_page(&scatterlist[i]);
  2923. kaddr = kmap(page);
  2924. if (direction == DMA_TO_DEVICE)
  2925. rc = __copy_from_user(kaddr,
  2926. (void *)buffer,
  2927. bsize_elem);
  2928. else
  2929. rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
  2930. kunmap(page);
  2931. if (rc) {
  2932. pmcraid_err("failed to copy user data into sg list\n");
  2933. return -EFAULT;
  2934. }
  2935. scatterlist[i].length = bsize_elem;
  2936. }
  2937. if (len % bsize_elem) {
  2938. struct page *page = sg_page(&scatterlist[i]);
  2939. kaddr = kmap(page);
  2940. if (direction == DMA_TO_DEVICE)
  2941. rc = __copy_from_user(kaddr,
  2942. (void *)buffer,
  2943. len % bsize_elem);
  2944. else
  2945. rc = __copy_to_user((void *)buffer,
  2946. kaddr,
  2947. len % bsize_elem);
  2948. kunmap(page);
  2949. scatterlist[i].length = len % bsize_elem;
  2950. }
  2951. if (rc) {
  2952. pmcraid_err("failed to copy user data into sg list\n");
  2953. rc = -EFAULT;
  2954. }
  2955. return rc;
  2956. }
  2957. /**
  2958. * pmcraid_queuecommand - Queue a mid-layer request
  2959. * @scsi_cmd: scsi command struct
  2960. * @done: done function
  2961. *
  2962. * This function queues a request generated by the mid-layer. Midlayer calls
  2963. * this routine within host->lock. Some of the functions called by queuecommand
  2964. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  2965. *
  2966. * Return value:
  2967. * 0 on success
  2968. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  2969. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  2970. */
  2971. static int pmcraid_queuecommand_lck(
  2972. struct scsi_cmnd *scsi_cmd,
  2973. void (*done) (struct scsi_cmnd *)
  2974. )
  2975. {
  2976. struct pmcraid_instance *pinstance;
  2977. struct pmcraid_resource_entry *res;
  2978. struct pmcraid_ioarcb *ioarcb;
  2979. struct pmcraid_cmd *cmd;
  2980. u32 fw_version;
  2981. int rc = 0;
  2982. pinstance =
  2983. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2984. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  2985. scsi_cmd->scsi_done = done;
  2986. res = scsi_cmd->device->hostdata;
  2987. scsi_cmd->result = (DID_OK << 16);
  2988. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  2989. * the command
  2990. */
  2991. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2992. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  2993. scsi_cmd->result = (DID_NO_CONNECT << 16);
  2994. scsi_cmd->scsi_done(scsi_cmd);
  2995. return 0;
  2996. }
  2997. /* If IOA reset is in progress, can't queue the commands */
  2998. if (pinstance->ioa_reset_in_progress)
  2999. return SCSI_MLQUEUE_HOST_BUSY;
  3000. /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
  3001. * the command here itself with success return
  3002. */
  3003. if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
  3004. pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
  3005. scsi_cmd->scsi_done(scsi_cmd);
  3006. return 0;
  3007. }
  3008. /* initialize the command and IOARCB to be sent to IOA */
  3009. cmd = pmcraid_get_free_cmd(pinstance);
  3010. if (cmd == NULL) {
  3011. pmcraid_err("free command block is not available\n");
  3012. return SCSI_MLQUEUE_HOST_BUSY;
  3013. }
  3014. cmd->scsi_cmd = scsi_cmd;
  3015. ioarcb = &(cmd->ioa_cb->ioarcb);
  3016. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  3017. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  3018. ioarcb->request_type = REQ_TYPE_SCSI;
  3019. /* set hrrq number where the IOA should respond to. Note that all cmds
  3020. * generated internally uses hrrq_id 0, exception to this is the cmd
  3021. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3022. * hrrq_id assigned here in queuecommand
  3023. */
  3024. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3025. pinstance->num_hrrq;
  3026. cmd->cmd_done = pmcraid_io_done;
  3027. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  3028. if (scsi_cmd->underflow == 0)
  3029. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  3030. if (res->sync_reqd) {
  3031. ioarcb->request_flags0 |= SYNC_COMPLETE;
  3032. res->sync_reqd = 0;
  3033. }
  3034. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3035. if (scsi_cmd->flags & SCMD_TAGGED)
  3036. ioarcb->request_flags1 |= TASK_TAG_SIMPLE;
  3037. if (RES_IS_GSCSI(res->cfg_entry))
  3038. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  3039. }
  3040. rc = pmcraid_build_ioadl(pinstance, cmd);
  3041. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  3042. le32_to_cpu(ioarcb->response_handle) >> 2,
  3043. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  3044. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  3045. PMCRAID_PHYS_BUS_ID,
  3046. RES_IS_VSET(res->cfg_entry) ?
  3047. (fw_version <= PMCRAID_FW_VERSION_1 ?
  3048. res->cfg_entry.unique_flags1 :
  3049. res->cfg_entry.array_id & 0xFF) :
  3050. RES_TARGET(res->cfg_entry.resource_address),
  3051. RES_LUN(res->cfg_entry.resource_address));
  3052. if (likely(rc == 0)) {
  3053. _pmcraid_fire_command(cmd);
  3054. } else {
  3055. pmcraid_err("queuecommand could not build ioadl\n");
  3056. pmcraid_return_cmd(cmd);
  3057. rc = SCSI_MLQUEUE_HOST_BUSY;
  3058. }
  3059. return rc;
  3060. }
  3061. static DEF_SCSI_QCMD(pmcraid_queuecommand)
  3062. /**
  3063. * pmcraid_open -char node "open" entry, allowed only users with admin access
  3064. */
  3065. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  3066. {
  3067. struct pmcraid_instance *pinstance;
  3068. if (!capable(CAP_SYS_ADMIN))
  3069. return -EACCES;
  3070. /* Populate adapter instance * pointer for use by ioctl */
  3071. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  3072. filep->private_data = pinstance;
  3073. return 0;
  3074. }
  3075. /**
  3076. * pmcraid_fasync - Async notifier registration from applications
  3077. *
  3078. * This function adds the calling process to a driver global queue. When an
  3079. * event occurs, SIGIO will be sent to all processes in this queue.
  3080. */
  3081. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  3082. {
  3083. struct pmcraid_instance *pinstance;
  3084. int rc;
  3085. pinstance = filep->private_data;
  3086. mutex_lock(&pinstance->aen_queue_lock);
  3087. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  3088. mutex_unlock(&pinstance->aen_queue_lock);
  3089. return rc;
  3090. }
  3091. /**
  3092. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  3093. * commands sent over IOCTL interface
  3094. *
  3095. * @cmd : pointer to struct pmcraid_cmd
  3096. * @buflen : length of the request buffer
  3097. * @direction : data transfer direction
  3098. *
  3099. * Return value
  3100. * 0 on success, non-zero error code on failure
  3101. */
  3102. static int pmcraid_build_passthrough_ioadls(
  3103. struct pmcraid_cmd *cmd,
  3104. int buflen,
  3105. int direction
  3106. )
  3107. {
  3108. struct pmcraid_sglist *sglist = NULL;
  3109. struct scatterlist *sg = NULL;
  3110. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  3111. struct pmcraid_ioadl_desc *ioadl;
  3112. int i;
  3113. sglist = pmcraid_alloc_sglist(buflen);
  3114. if (!sglist) {
  3115. pmcraid_err("can't allocate memory for passthrough SGls\n");
  3116. return -ENOMEM;
  3117. }
  3118. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  3119. sglist->scatterlist,
  3120. sglist->num_sg, direction);
  3121. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  3122. dev_err(&cmd->drv_inst->pdev->dev,
  3123. "Failed to map passthrough buffer!\n");
  3124. pmcraid_free_sglist(sglist);
  3125. return -EIO;
  3126. }
  3127. cmd->sglist = sglist;
  3128. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3129. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  3130. /* Initialize IOADL descriptor addresses */
  3131. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  3132. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  3133. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  3134. ioadl[i].flags = 0;
  3135. }
  3136. /* setup the last descriptor */
  3137. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  3138. return 0;
  3139. }
  3140. /**
  3141. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  3142. *
  3143. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  3144. * @buflen: size of the request buffer
  3145. * @direction: data transfer direction
  3146. *
  3147. * Return value
  3148. * 0 on success, non-zero error code on failure
  3149. */
  3150. static void pmcraid_release_passthrough_ioadls(
  3151. struct pmcraid_cmd *cmd,
  3152. int buflen,
  3153. int direction
  3154. )
  3155. {
  3156. struct pmcraid_sglist *sglist = cmd->sglist;
  3157. if (buflen > 0) {
  3158. pci_unmap_sg(cmd->drv_inst->pdev,
  3159. sglist->scatterlist,
  3160. sglist->num_sg,
  3161. direction);
  3162. pmcraid_free_sglist(sglist);
  3163. cmd->sglist = NULL;
  3164. }
  3165. }
  3166. /**
  3167. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  3168. *
  3169. * @pinstance: pointer to adapter instance structure
  3170. * @cmd: ioctl code
  3171. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  3172. *
  3173. * Return value
  3174. * 0 on success, non-zero error code on failure
  3175. */
  3176. static long pmcraid_ioctl_passthrough(
  3177. struct pmcraid_instance *pinstance,
  3178. unsigned int ioctl_cmd,
  3179. unsigned int buflen,
  3180. unsigned long arg
  3181. )
  3182. {
  3183. struct pmcraid_passthrough_ioctl_buffer *buffer;
  3184. struct pmcraid_ioarcb *ioarcb;
  3185. struct pmcraid_cmd *cmd;
  3186. struct pmcraid_cmd *cancel_cmd;
  3187. unsigned long request_buffer;
  3188. unsigned long request_offset;
  3189. unsigned long lock_flags;
  3190. void *ioasa;
  3191. u32 ioasc;
  3192. int request_size;
  3193. int buffer_size;
  3194. u8 access, direction;
  3195. int rc = 0;
  3196. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  3197. if (pinstance->ioa_reset_in_progress) {
  3198. rc = wait_event_interruptible_timeout(
  3199. pinstance->reset_wait_q,
  3200. !pinstance->ioa_reset_in_progress,
  3201. msecs_to_jiffies(10000));
  3202. if (!rc)
  3203. return -ETIMEDOUT;
  3204. else if (rc < 0)
  3205. return -ERESTARTSYS;
  3206. }
  3207. /* If adapter is not in operational state, return error */
  3208. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3209. pmcraid_err("IOA is not operational\n");
  3210. return -ENOTTY;
  3211. }
  3212. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3213. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3214. if (!buffer) {
  3215. pmcraid_err("no memory for passthrough buffer\n");
  3216. return -ENOMEM;
  3217. }
  3218. request_offset =
  3219. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3220. request_buffer = arg + request_offset;
  3221. rc = __copy_from_user(buffer,
  3222. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3223. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3224. ioasa =
  3225. (void *)(arg +
  3226. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3227. if (rc) {
  3228. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3229. rc = -EFAULT;
  3230. goto out_free_buffer;
  3231. }
  3232. request_size = buffer->ioarcb.data_transfer_length;
  3233. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3234. access = VERIFY_READ;
  3235. direction = DMA_TO_DEVICE;
  3236. } else {
  3237. access = VERIFY_WRITE;
  3238. direction = DMA_FROM_DEVICE;
  3239. }
  3240. if (request_size > 0) {
  3241. rc = access_ok(access, arg, request_offset + request_size);
  3242. if (!rc) {
  3243. rc = -EFAULT;
  3244. goto out_free_buffer;
  3245. }
  3246. } else if (request_size < 0) {
  3247. rc = -EINVAL;
  3248. goto out_free_buffer;
  3249. }
  3250. /* check if we have any additional command parameters */
  3251. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3252. rc = -EINVAL;
  3253. goto out_free_buffer;
  3254. }
  3255. cmd = pmcraid_get_free_cmd(pinstance);
  3256. if (!cmd) {
  3257. pmcraid_err("free command block is not available\n");
  3258. rc = -ENOMEM;
  3259. goto out_free_buffer;
  3260. }
  3261. cmd->scsi_cmd = NULL;
  3262. ioarcb = &(cmd->ioa_cb->ioarcb);
  3263. /* Copy the user-provided IOARCB stuff field by field */
  3264. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3265. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3266. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3267. ioarcb->request_type = buffer->ioarcb.request_type;
  3268. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3269. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3270. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3271. if (buffer->ioarcb.add_cmd_param_length) {
  3272. ioarcb->add_cmd_param_length =
  3273. buffer->ioarcb.add_cmd_param_length;
  3274. ioarcb->add_cmd_param_offset =
  3275. buffer->ioarcb.add_cmd_param_offset;
  3276. memcpy(ioarcb->add_data.u.add_cmd_params,
  3277. buffer->ioarcb.add_data.u.add_cmd_params,
  3278. buffer->ioarcb.add_cmd_param_length);
  3279. }
  3280. /* set hrrq number where the IOA should respond to. Note that all cmds
  3281. * generated internally uses hrrq_id 0, exception to this is the cmd
  3282. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3283. * hrrq_id assigned here in queuecommand
  3284. */
  3285. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3286. pinstance->num_hrrq;
  3287. if (request_size) {
  3288. rc = pmcraid_build_passthrough_ioadls(cmd,
  3289. request_size,
  3290. direction);
  3291. if (rc) {
  3292. pmcraid_err("couldn't build passthrough ioadls\n");
  3293. goto out_free_cmd;
  3294. }
  3295. } else if (request_size < 0) {
  3296. rc = -EINVAL;
  3297. goto out_free_cmd;
  3298. }
  3299. /* If data is being written into the device, copy the data from user
  3300. * buffers
  3301. */
  3302. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3303. rc = pmcraid_copy_sglist(cmd->sglist,
  3304. request_buffer,
  3305. request_size,
  3306. direction);
  3307. if (rc) {
  3308. pmcraid_err("failed to copy user buffer\n");
  3309. goto out_free_sglist;
  3310. }
  3311. }
  3312. /* passthrough ioctl is a blocking command so, put the user to sleep
  3313. * until timeout. Note that a timeout value of 0 means, do timeout.
  3314. */
  3315. cmd->cmd_done = pmcraid_internal_done;
  3316. init_completion(&cmd->wait_for_completion);
  3317. cmd->completion_req = 1;
  3318. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3319. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3320. cmd->ioa_cb->ioarcb.cdb[0],
  3321. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3322. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3323. _pmcraid_fire_command(cmd);
  3324. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3325. /* NOTE ! Remove the below line once abort_task is implemented
  3326. * in firmware. This line disables ioctl command timeout handling logic
  3327. * similar to IO command timeout handling, making ioctl commands to wait
  3328. * until the command completion regardless of timeout value specified in
  3329. * ioarcb
  3330. */
  3331. buffer->ioarcb.cmd_timeout = 0;
  3332. /* If command timeout is specified put caller to wait till that time,
  3333. * otherwise it would be blocking wait. If command gets timed out, it
  3334. * will be aborted.
  3335. */
  3336. if (buffer->ioarcb.cmd_timeout == 0) {
  3337. wait_for_completion(&cmd->wait_for_completion);
  3338. } else if (!wait_for_completion_timeout(
  3339. &cmd->wait_for_completion,
  3340. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3341. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3342. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3343. cmd->ioa_cb->ioarcb.cdb[0]);
  3344. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3345. cancel_cmd = pmcraid_abort_cmd(cmd);
  3346. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3347. if (cancel_cmd) {
  3348. wait_for_completion(&cancel_cmd->wait_for_completion);
  3349. ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
  3350. pmcraid_return_cmd(cancel_cmd);
  3351. /* if abort task couldn't find the command i.e it got
  3352. * completed prior to aborting, return good completion.
  3353. * if command got aborted successfully or there was IOA
  3354. * reset due to abort task itself getting timedout then
  3355. * return -ETIMEDOUT
  3356. */
  3357. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  3358. PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
  3359. if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
  3360. rc = -ETIMEDOUT;
  3361. goto out_handle_response;
  3362. }
  3363. }
  3364. /* no command block for abort task or abort task failed to abort
  3365. * the IOARCB, then wait for 150 more seconds and initiate reset
  3366. * sequence after timeout
  3367. */
  3368. if (!wait_for_completion_timeout(
  3369. &cmd->wait_for_completion,
  3370. msecs_to_jiffies(150 * 1000))) {
  3371. pmcraid_reset_bringup(cmd->drv_inst);
  3372. rc = -ETIMEDOUT;
  3373. }
  3374. }
  3375. out_handle_response:
  3376. /* copy entire IOASA buffer and return IOCTL success.
  3377. * If copying IOASA to user-buffer fails, return
  3378. * EFAULT
  3379. */
  3380. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3381. sizeof(struct pmcraid_ioasa))) {
  3382. pmcraid_err("failed to copy ioasa buffer to user\n");
  3383. rc = -EFAULT;
  3384. }
  3385. /* If the data transfer was from device, copy the data onto user
  3386. * buffers
  3387. */
  3388. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3389. rc = pmcraid_copy_sglist(cmd->sglist,
  3390. request_buffer,
  3391. request_size,
  3392. direction);
  3393. if (rc) {
  3394. pmcraid_err("failed to copy user buffer\n");
  3395. rc = -EFAULT;
  3396. }
  3397. }
  3398. out_free_sglist:
  3399. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3400. out_free_cmd:
  3401. pmcraid_return_cmd(cmd);
  3402. out_free_buffer:
  3403. kfree(buffer);
  3404. return rc;
  3405. }
  3406. /**
  3407. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3408. *
  3409. * @pinstance: pointer to adapter instance structure
  3410. * @cmd: ioctl command passed in
  3411. * @buflen: length of user_buffer
  3412. * @user_buffer: user buffer pointer
  3413. *
  3414. * Return Value
  3415. * 0 in case of success, otherwise appropriate error code
  3416. */
  3417. static long pmcraid_ioctl_driver(
  3418. struct pmcraid_instance *pinstance,
  3419. unsigned int cmd,
  3420. unsigned int buflen,
  3421. void __user *user_buffer
  3422. )
  3423. {
  3424. int rc = -ENOSYS;
  3425. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3426. pmcraid_err("ioctl_driver: access fault in request buffer\n");
  3427. return -EFAULT;
  3428. }
  3429. switch (cmd) {
  3430. case PMCRAID_IOCTL_RESET_ADAPTER:
  3431. pmcraid_reset_bringup(pinstance);
  3432. rc = 0;
  3433. break;
  3434. default:
  3435. break;
  3436. }
  3437. return rc;
  3438. }
  3439. /**
  3440. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3441. *
  3442. * @cmd: ioctl command
  3443. * @arg: user buffer
  3444. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3445. *
  3446. * Return Value
  3447. * negetive error code if there are access issues, otherwise zero.
  3448. * Upon success, returns ioctl header copied out of user buffer.
  3449. */
  3450. static int pmcraid_check_ioctl_buffer(
  3451. int cmd,
  3452. void __user *arg,
  3453. struct pmcraid_ioctl_header *hdr
  3454. )
  3455. {
  3456. int rc = 0;
  3457. int access = VERIFY_READ;
  3458. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3459. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3460. return -EFAULT;
  3461. }
  3462. /* check for valid driver signature */
  3463. rc = memcmp(hdr->signature,
  3464. PMCRAID_IOCTL_SIGNATURE,
  3465. sizeof(hdr->signature));
  3466. if (rc) {
  3467. pmcraid_err("signature verification failed\n");
  3468. return -EINVAL;
  3469. }
  3470. /* check for appropriate buffer access */
  3471. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3472. access = VERIFY_WRITE;
  3473. rc = access_ok(access,
  3474. (arg + sizeof(struct pmcraid_ioctl_header)),
  3475. hdr->buffer_length);
  3476. if (!rc) {
  3477. pmcraid_err("access failed for user buffer of size %d\n",
  3478. hdr->buffer_length);
  3479. return -EFAULT;
  3480. }
  3481. return 0;
  3482. }
  3483. /**
  3484. * pmcraid_ioctl - char node ioctl entry point
  3485. */
  3486. static long pmcraid_chr_ioctl(
  3487. struct file *filep,
  3488. unsigned int cmd,
  3489. unsigned long arg
  3490. )
  3491. {
  3492. struct pmcraid_instance *pinstance = NULL;
  3493. struct pmcraid_ioctl_header *hdr = NULL;
  3494. int retval = -ENOTTY;
  3495. hdr = kmalloc(sizeof(struct pmcraid_ioctl_header), GFP_KERNEL);
  3496. if (!hdr) {
  3497. pmcraid_err("failed to allocate memory for ioctl header\n");
  3498. return -ENOMEM;
  3499. }
  3500. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3501. if (retval) {
  3502. pmcraid_info("chr_ioctl: header check failed\n");
  3503. kfree(hdr);
  3504. return retval;
  3505. }
  3506. pinstance = filep->private_data;
  3507. if (!pinstance) {
  3508. pmcraid_info("adapter instance is not found\n");
  3509. kfree(hdr);
  3510. return -ENOTTY;
  3511. }
  3512. switch (_IOC_TYPE(cmd)) {
  3513. case PMCRAID_PASSTHROUGH_IOCTL:
  3514. /* If ioctl code is to download microcode, we need to block
  3515. * mid-layer requests.
  3516. */
  3517. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3518. scsi_block_requests(pinstance->host);
  3519. retval = pmcraid_ioctl_passthrough(pinstance,
  3520. cmd,
  3521. hdr->buffer_length,
  3522. arg);
  3523. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3524. scsi_unblock_requests(pinstance->host);
  3525. break;
  3526. case PMCRAID_DRIVER_IOCTL:
  3527. arg += sizeof(struct pmcraid_ioctl_header);
  3528. retval = pmcraid_ioctl_driver(pinstance,
  3529. cmd,
  3530. hdr->buffer_length,
  3531. (void __user *)arg);
  3532. break;
  3533. default:
  3534. retval = -ENOTTY;
  3535. break;
  3536. }
  3537. kfree(hdr);
  3538. return retval;
  3539. }
  3540. /**
  3541. * File operations structure for management interface
  3542. */
  3543. static const struct file_operations pmcraid_fops = {
  3544. .owner = THIS_MODULE,
  3545. .open = pmcraid_chr_open,
  3546. .fasync = pmcraid_chr_fasync,
  3547. .unlocked_ioctl = pmcraid_chr_ioctl,
  3548. #ifdef CONFIG_COMPAT
  3549. .compat_ioctl = pmcraid_chr_ioctl,
  3550. #endif
  3551. .llseek = noop_llseek,
  3552. };
  3553. /**
  3554. * pmcraid_show_log_level - Display adapter's error logging level
  3555. * @dev: class device struct
  3556. * @buf: buffer
  3557. *
  3558. * Return value:
  3559. * number of bytes printed to buffer
  3560. */
  3561. static ssize_t pmcraid_show_log_level(
  3562. struct device *dev,
  3563. struct device_attribute *attr,
  3564. char *buf)
  3565. {
  3566. struct Scsi_Host *shost = class_to_shost(dev);
  3567. struct pmcraid_instance *pinstance =
  3568. (struct pmcraid_instance *)shost->hostdata;
  3569. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3570. }
  3571. /**
  3572. * pmcraid_store_log_level - Change the adapter's error logging level
  3573. * @dev: class device struct
  3574. * @buf: buffer
  3575. * @count: not used
  3576. *
  3577. * Return value:
  3578. * number of bytes printed to buffer
  3579. */
  3580. static ssize_t pmcraid_store_log_level(
  3581. struct device *dev,
  3582. struct device_attribute *attr,
  3583. const char *buf,
  3584. size_t count
  3585. )
  3586. {
  3587. struct Scsi_Host *shost;
  3588. struct pmcraid_instance *pinstance;
  3589. u8 val;
  3590. if (kstrtou8(buf, 10, &val))
  3591. return -EINVAL;
  3592. /* log-level should be from 0 to 2 */
  3593. if (val > 2)
  3594. return -EINVAL;
  3595. shost = class_to_shost(dev);
  3596. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3597. pinstance->current_log_level = val;
  3598. return strlen(buf);
  3599. }
  3600. static struct device_attribute pmcraid_log_level_attr = {
  3601. .attr = {
  3602. .name = "log_level",
  3603. .mode = S_IRUGO | S_IWUSR,
  3604. },
  3605. .show = pmcraid_show_log_level,
  3606. .store = pmcraid_store_log_level,
  3607. };
  3608. /**
  3609. * pmcraid_show_drv_version - Display driver version
  3610. * @dev: class device struct
  3611. * @buf: buffer
  3612. *
  3613. * Return value:
  3614. * number of bytes printed to buffer
  3615. */
  3616. static ssize_t pmcraid_show_drv_version(
  3617. struct device *dev,
  3618. struct device_attribute *attr,
  3619. char *buf
  3620. )
  3621. {
  3622. return snprintf(buf, PAGE_SIZE, "version: %s\n",
  3623. PMCRAID_DRIVER_VERSION);
  3624. }
  3625. static struct device_attribute pmcraid_driver_version_attr = {
  3626. .attr = {
  3627. .name = "drv_version",
  3628. .mode = S_IRUGO,
  3629. },
  3630. .show = pmcraid_show_drv_version,
  3631. };
  3632. /**
  3633. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3634. * @dev: class device struct
  3635. * @buf: buffer
  3636. *
  3637. * Return value:
  3638. * number of bytes printed to buffer
  3639. */
  3640. static ssize_t pmcraid_show_adapter_id(
  3641. struct device *dev,
  3642. struct device_attribute *attr,
  3643. char *buf
  3644. )
  3645. {
  3646. struct Scsi_Host *shost = class_to_shost(dev);
  3647. struct pmcraid_instance *pinstance =
  3648. (struct pmcraid_instance *)shost->hostdata;
  3649. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3650. pinstance->pdev->devfn;
  3651. u32 aen_group = pmcraid_event_family.id;
  3652. return snprintf(buf, PAGE_SIZE,
  3653. "adapter id: %d\nminor: %d\naen group: %d\n",
  3654. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3655. }
  3656. static struct device_attribute pmcraid_adapter_id_attr = {
  3657. .attr = {
  3658. .name = "adapter_id",
  3659. .mode = S_IRUGO,
  3660. },
  3661. .show = pmcraid_show_adapter_id,
  3662. };
  3663. static struct device_attribute *pmcraid_host_attrs[] = {
  3664. &pmcraid_log_level_attr,
  3665. &pmcraid_driver_version_attr,
  3666. &pmcraid_adapter_id_attr,
  3667. NULL,
  3668. };
  3669. /* host template structure for pmcraid driver */
  3670. static struct scsi_host_template pmcraid_host_template = {
  3671. .module = THIS_MODULE,
  3672. .name = PMCRAID_DRIVER_NAME,
  3673. .queuecommand = pmcraid_queuecommand,
  3674. .eh_abort_handler = pmcraid_eh_abort_handler,
  3675. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3676. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3677. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3678. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3679. .slave_alloc = pmcraid_slave_alloc,
  3680. .slave_configure = pmcraid_slave_configure,
  3681. .slave_destroy = pmcraid_slave_destroy,
  3682. .change_queue_depth = pmcraid_change_queue_depth,
  3683. .can_queue = PMCRAID_MAX_IO_CMD,
  3684. .this_id = -1,
  3685. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3686. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3687. .no_write_same = 1,
  3688. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3689. .use_clustering = ENABLE_CLUSTERING,
  3690. .shost_attrs = pmcraid_host_attrs,
  3691. .proc_name = PMCRAID_DRIVER_NAME,
  3692. };
  3693. /*
  3694. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3695. * @irq: interrupt vector number
  3696. * @dev_id: pointer hrrq_vector
  3697. *
  3698. * Return Value
  3699. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3700. */
  3701. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3702. {
  3703. struct pmcraid_isr_param *hrrq_vector;
  3704. struct pmcraid_instance *pinstance;
  3705. unsigned long lock_flags;
  3706. u32 intrs_val;
  3707. int hrrq_id;
  3708. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3709. hrrq_id = hrrq_vector->hrrq_id;
  3710. pinstance = hrrq_vector->drv_inst;
  3711. if (!hrrq_id) {
  3712. /* Read the interrupt */
  3713. intrs_val = pmcraid_read_interrupts(pinstance);
  3714. if (intrs_val &&
  3715. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3716. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3717. /* Any error interrupts including unit_check,
  3718. * initiate IOA reset.In case of unit check indicate
  3719. * to reset_sequence that IOA unit checked and prepare
  3720. * for a dump during reset sequence
  3721. */
  3722. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3723. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3724. pinstance->ioa_unit_check = 1;
  3725. pmcraid_err("ISR: error interrupts: %x \
  3726. initiating reset\n", intrs_val);
  3727. spin_lock_irqsave(pinstance->host->host_lock,
  3728. lock_flags);
  3729. pmcraid_initiate_reset(pinstance);
  3730. spin_unlock_irqrestore(
  3731. pinstance->host->host_lock,
  3732. lock_flags);
  3733. }
  3734. /* If interrupt was as part of the ioa initialization,
  3735. * clear it. Delete the timer and wakeup the
  3736. * reset engine to proceed with reset sequence
  3737. */
  3738. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3739. pmcraid_clr_trans_op(pinstance);
  3740. /* Clear the interrupt register by writing
  3741. * to host to ioa doorbell. Once done
  3742. * FW will clear the interrupt.
  3743. */
  3744. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3745. pinstance->int_regs.host_ioa_interrupt_reg);
  3746. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3747. }
  3748. }
  3749. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3750. return IRQ_HANDLED;
  3751. }
  3752. /**
  3753. * pmcraid_isr - implements legacy interrupt handling routine
  3754. *
  3755. * @irq: interrupt vector number
  3756. * @dev_id: pointer hrrq_vector
  3757. *
  3758. * Return Value
  3759. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3760. */
  3761. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3762. {
  3763. struct pmcraid_isr_param *hrrq_vector;
  3764. struct pmcraid_instance *pinstance;
  3765. u32 intrs;
  3766. unsigned long lock_flags;
  3767. int hrrq_id = 0;
  3768. /* In case of legacy interrupt mode where interrupts are shared across
  3769. * isrs, it may be possible that the current interrupt is not from IOA
  3770. */
  3771. if (!dev_id) {
  3772. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3773. return IRQ_NONE;
  3774. }
  3775. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3776. pinstance = hrrq_vector->drv_inst;
  3777. intrs = pmcraid_read_interrupts(pinstance);
  3778. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3779. return IRQ_NONE;
  3780. /* Any error interrupts including unit_check, initiate IOA reset.
  3781. * In case of unit check indicate to reset_sequence that IOA unit
  3782. * checked and prepare for a dump during reset sequence
  3783. */
  3784. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3785. if (intrs & INTRS_IOA_UNIT_CHECK)
  3786. pinstance->ioa_unit_check = 1;
  3787. iowrite32(intrs,
  3788. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3789. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3790. intrs);
  3791. intrs = ioread32(
  3792. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3793. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3794. pmcraid_initiate_reset(pinstance);
  3795. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3796. } else {
  3797. /* If interrupt was as part of the ioa initialization,
  3798. * clear. Delete the timer and wakeup the
  3799. * reset engine to proceed with reset sequence
  3800. */
  3801. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3802. pmcraid_clr_trans_op(pinstance);
  3803. } else {
  3804. iowrite32(intrs,
  3805. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3806. ioread32(
  3807. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3808. tasklet_schedule(
  3809. &(pinstance->isr_tasklet[hrrq_id]));
  3810. }
  3811. }
  3812. return IRQ_HANDLED;
  3813. }
  3814. /**
  3815. * pmcraid_worker_function - worker thread function
  3816. *
  3817. * @workp: pointer to struct work queue
  3818. *
  3819. * Return Value
  3820. * None
  3821. */
  3822. static void pmcraid_worker_function(struct work_struct *workp)
  3823. {
  3824. struct pmcraid_instance *pinstance;
  3825. struct pmcraid_resource_entry *res;
  3826. struct pmcraid_resource_entry *temp;
  3827. struct scsi_device *sdev;
  3828. unsigned long lock_flags;
  3829. unsigned long host_lock_flags;
  3830. u16 fw_version;
  3831. u8 bus, target, lun;
  3832. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3833. /* add resources only after host is added into system */
  3834. if (!atomic_read(&pinstance->expose_resources))
  3835. return;
  3836. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3837. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3838. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3839. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3840. sdev = res->scsi_dev;
  3841. /* host_lock must be held before calling
  3842. * scsi_device_get
  3843. */
  3844. spin_lock_irqsave(pinstance->host->host_lock,
  3845. host_lock_flags);
  3846. if (!scsi_device_get(sdev)) {
  3847. spin_unlock_irqrestore(
  3848. pinstance->host->host_lock,
  3849. host_lock_flags);
  3850. pmcraid_info("deleting %x from midlayer\n",
  3851. res->cfg_entry.resource_address);
  3852. list_move_tail(&res->queue,
  3853. &pinstance->free_res_q);
  3854. spin_unlock_irqrestore(
  3855. &pinstance->resource_lock,
  3856. lock_flags);
  3857. scsi_remove_device(sdev);
  3858. scsi_device_put(sdev);
  3859. spin_lock_irqsave(&pinstance->resource_lock,
  3860. lock_flags);
  3861. res->change_detected = 0;
  3862. } else {
  3863. spin_unlock_irqrestore(
  3864. pinstance->host->host_lock,
  3865. host_lock_flags);
  3866. }
  3867. }
  3868. }
  3869. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3870. if (res->change_detected == RES_CHANGE_ADD) {
  3871. if (!pmcraid_expose_resource(fw_version,
  3872. &res->cfg_entry))
  3873. continue;
  3874. if (RES_IS_VSET(res->cfg_entry)) {
  3875. bus = PMCRAID_VSET_BUS_ID;
  3876. if (fw_version <= PMCRAID_FW_VERSION_1)
  3877. target = res->cfg_entry.unique_flags1;
  3878. else
  3879. target = res->cfg_entry.array_id & 0xFF;
  3880. lun = PMCRAID_VSET_LUN_ID;
  3881. } else {
  3882. bus = PMCRAID_PHYS_BUS_ID;
  3883. target =
  3884. RES_TARGET(
  3885. res->cfg_entry.resource_address);
  3886. lun = RES_LUN(res->cfg_entry.resource_address);
  3887. }
  3888. res->change_detected = 0;
  3889. spin_unlock_irqrestore(&pinstance->resource_lock,
  3890. lock_flags);
  3891. scsi_add_device(pinstance->host, bus, target, lun);
  3892. spin_lock_irqsave(&pinstance->resource_lock,
  3893. lock_flags);
  3894. }
  3895. }
  3896. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3897. }
  3898. /**
  3899. * pmcraid_tasklet_function - Tasklet function
  3900. *
  3901. * @instance: pointer to msix param structure
  3902. *
  3903. * Return Value
  3904. * None
  3905. */
  3906. static void pmcraid_tasklet_function(unsigned long instance)
  3907. {
  3908. struct pmcraid_isr_param *hrrq_vector;
  3909. struct pmcraid_instance *pinstance;
  3910. unsigned long hrrq_lock_flags;
  3911. unsigned long pending_lock_flags;
  3912. unsigned long host_lock_flags;
  3913. spinlock_t *lockp; /* hrrq buffer lock */
  3914. int id;
  3915. __le32 resp;
  3916. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3917. pinstance = hrrq_vector->drv_inst;
  3918. id = hrrq_vector->hrrq_id;
  3919. lockp = &(pinstance->hrrq_lock[id]);
  3920. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3921. * protected by its own lock. Traversals must be done within this lock
  3922. * as there may be multiple tasklets running on multiple CPUs. Note
  3923. * that the lock is held just for picking up the response handle and
  3924. * manipulating hrrq_curr/toggle_bit values.
  3925. */
  3926. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3927. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3928. while ((resp & HRRQ_TOGGLE_BIT) ==
  3929. pinstance->host_toggle_bit[id]) {
  3930. int cmd_index = resp >> 2;
  3931. struct pmcraid_cmd *cmd = NULL;
  3932. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3933. pinstance->hrrq_curr[id]++;
  3934. } else {
  3935. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  3936. pinstance->host_toggle_bit[id] ^= 1u;
  3937. }
  3938. if (cmd_index >= PMCRAID_MAX_CMD) {
  3939. /* In case of invalid response handle, log message */
  3940. pmcraid_err("Invalid response handle %d\n", cmd_index);
  3941. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3942. continue;
  3943. }
  3944. cmd = pinstance->cmd_list[cmd_index];
  3945. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3946. spin_lock_irqsave(&pinstance->pending_pool_lock,
  3947. pending_lock_flags);
  3948. list_del(&cmd->free_list);
  3949. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  3950. pending_lock_flags);
  3951. del_timer(&cmd->timer);
  3952. atomic_dec(&pinstance->outstanding_cmds);
  3953. if (cmd->cmd_done == pmcraid_ioa_reset) {
  3954. spin_lock_irqsave(pinstance->host->host_lock,
  3955. host_lock_flags);
  3956. cmd->cmd_done(cmd);
  3957. spin_unlock_irqrestore(pinstance->host->host_lock,
  3958. host_lock_flags);
  3959. } else if (cmd->cmd_done != NULL) {
  3960. cmd->cmd_done(cmd);
  3961. }
  3962. /* loop over until we are done with all responses */
  3963. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3964. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3965. }
  3966. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3967. }
  3968. /**
  3969. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  3970. * @pinstance: pointer to adapter instance structure
  3971. *
  3972. * This routine un-registers registered interrupt handler and
  3973. * also frees irqs/vectors.
  3974. *
  3975. * Retun Value
  3976. * None
  3977. */
  3978. static
  3979. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  3980. {
  3981. int i;
  3982. for (i = 0; i < pinstance->num_hrrq; i++)
  3983. free_irq(pinstance->hrrq_vector[i].vector,
  3984. &(pinstance->hrrq_vector[i]));
  3985. if (pinstance->interrupt_mode) {
  3986. pci_disable_msix(pinstance->pdev);
  3987. pinstance->interrupt_mode = 0;
  3988. }
  3989. }
  3990. /**
  3991. * pmcraid_register_interrupt_handler - registers interrupt handler
  3992. * @pinstance: pointer to per-adapter instance structure
  3993. *
  3994. * Return Value
  3995. * 0 on success, non-zero error code otherwise.
  3996. */
  3997. static int
  3998. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  3999. {
  4000. int rc;
  4001. struct pci_dev *pdev = pinstance->pdev;
  4002. if ((pmcraid_enable_msix) &&
  4003. (pci_find_capability(pdev, PCI_CAP_ID_MSIX))) {
  4004. int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
  4005. struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
  4006. int i;
  4007. for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
  4008. entries[i].entry = i;
  4009. num_hrrq = pci_enable_msix_range(pdev, entries, 1, num_hrrq);
  4010. if (num_hrrq < 0)
  4011. goto pmcraid_isr_legacy;
  4012. for (i = 0; i < num_hrrq; i++) {
  4013. pinstance->hrrq_vector[i].hrrq_id = i;
  4014. pinstance->hrrq_vector[i].drv_inst = pinstance;
  4015. pinstance->hrrq_vector[i].vector = entries[i].vector;
  4016. rc = request_irq(pinstance->hrrq_vector[i].vector,
  4017. pmcraid_isr_msix, 0,
  4018. PMCRAID_DRIVER_NAME,
  4019. &(pinstance->hrrq_vector[i]));
  4020. if (rc) {
  4021. int j;
  4022. for (j = 0; j < i; j++)
  4023. free_irq(entries[j].vector,
  4024. &(pinstance->hrrq_vector[j]));
  4025. pci_disable_msix(pdev);
  4026. goto pmcraid_isr_legacy;
  4027. }
  4028. }
  4029. pinstance->num_hrrq = num_hrrq;
  4030. pinstance->interrupt_mode = 1;
  4031. iowrite32(DOORBELL_INTR_MODE_MSIX,
  4032. pinstance->int_regs.host_ioa_interrupt_reg);
  4033. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  4034. goto pmcraid_isr_out;
  4035. }
  4036. pmcraid_isr_legacy:
  4037. /* If MSI-X registration failed fallback to legacy mode, where
  4038. * only one hrrq entry will be used
  4039. */
  4040. pinstance->hrrq_vector[0].hrrq_id = 0;
  4041. pinstance->hrrq_vector[0].drv_inst = pinstance;
  4042. pinstance->hrrq_vector[0].vector = pdev->irq;
  4043. pinstance->num_hrrq = 1;
  4044. rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  4045. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  4046. pmcraid_isr_out:
  4047. return rc;
  4048. }
  4049. /**
  4050. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  4051. * @pinstance: per adapter instance structure pointer
  4052. * @max_index: number of buffer blocks to release
  4053. *
  4054. * Return Value
  4055. * None
  4056. */
  4057. static void
  4058. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  4059. {
  4060. int i;
  4061. for (i = 0; i < max_index; i++) {
  4062. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  4063. pinstance->cmd_list[i] = NULL;
  4064. }
  4065. kmem_cache_destroy(pinstance->cmd_cachep);
  4066. pinstance->cmd_cachep = NULL;
  4067. }
  4068. /**
  4069. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  4070. * @pinstance: pointer to per adapter instance structure
  4071. * @max_index: number of buffers (from 0 onwards) to release
  4072. *
  4073. * This function assumes that the command blocks for which control blocks are
  4074. * linked are not released.
  4075. *
  4076. * Return Value
  4077. * None
  4078. */
  4079. static void
  4080. pmcraid_release_control_blocks(
  4081. struct pmcraid_instance *pinstance,
  4082. int max_index
  4083. )
  4084. {
  4085. int i;
  4086. if (pinstance->control_pool == NULL)
  4087. return;
  4088. for (i = 0; i < max_index; i++) {
  4089. pci_pool_free(pinstance->control_pool,
  4090. pinstance->cmd_list[i]->ioa_cb,
  4091. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  4092. pinstance->cmd_list[i]->ioa_cb = NULL;
  4093. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  4094. }
  4095. pci_pool_destroy(pinstance->control_pool);
  4096. pinstance->control_pool = NULL;
  4097. }
  4098. /**
  4099. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  4100. * @pinstance - pointer to per adapter instance structure
  4101. *
  4102. * Allocates memory for command blocks using kernel slab allocator.
  4103. *
  4104. * Return Value
  4105. * 0 in case of success; -ENOMEM in case of failure
  4106. */
  4107. static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  4108. {
  4109. int i;
  4110. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  4111. pinstance->host->unique_id);
  4112. pinstance->cmd_cachep = kmem_cache_create(
  4113. pinstance->cmd_pool_name,
  4114. sizeof(struct pmcraid_cmd), 0,
  4115. SLAB_HWCACHE_ALIGN, NULL);
  4116. if (!pinstance->cmd_cachep)
  4117. return -ENOMEM;
  4118. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4119. pinstance->cmd_list[i] =
  4120. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  4121. if (!pinstance->cmd_list[i]) {
  4122. pmcraid_release_cmd_blocks(pinstance, i);
  4123. return -ENOMEM;
  4124. }
  4125. }
  4126. return 0;
  4127. }
  4128. /**
  4129. * pmcraid_allocate_control_blocks - allocates memory control blocks
  4130. * @pinstance : pointer to per adapter instance structure
  4131. *
  4132. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  4133. * and IOASAs. This is called after command blocks are already allocated.
  4134. *
  4135. * Return Value
  4136. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  4137. */
  4138. static int pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  4139. {
  4140. int i;
  4141. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  4142. pinstance->host->unique_id);
  4143. pinstance->control_pool =
  4144. pci_pool_create(pinstance->ctl_pool_name,
  4145. pinstance->pdev,
  4146. sizeof(struct pmcraid_control_block),
  4147. PMCRAID_IOARCB_ALIGNMENT, 0);
  4148. if (!pinstance->control_pool)
  4149. return -ENOMEM;
  4150. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4151. pinstance->cmd_list[i]->ioa_cb =
  4152. pci_pool_alloc(
  4153. pinstance->control_pool,
  4154. GFP_KERNEL,
  4155. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  4156. if (!pinstance->cmd_list[i]->ioa_cb) {
  4157. pmcraid_release_control_blocks(pinstance, i);
  4158. return -ENOMEM;
  4159. }
  4160. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  4161. sizeof(struct pmcraid_control_block));
  4162. }
  4163. return 0;
  4164. }
  4165. /**
  4166. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  4167. * @pinstance: pointer to per adapter instance structure
  4168. * @maxindex: size of hrrq buffer pointer array
  4169. *
  4170. * Return Value
  4171. * None
  4172. */
  4173. static void
  4174. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  4175. {
  4176. int i;
  4177. for (i = 0; i < maxindex; i++) {
  4178. pci_free_consistent(pinstance->pdev,
  4179. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  4180. pinstance->hrrq_start[i],
  4181. pinstance->hrrq_start_bus_addr[i]);
  4182. /* reset pointers and toggle bit to zeros */
  4183. pinstance->hrrq_start[i] = NULL;
  4184. pinstance->hrrq_start_bus_addr[i] = 0;
  4185. pinstance->host_toggle_bit[i] = 0;
  4186. }
  4187. }
  4188. /**
  4189. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  4190. * @pinstance: pointer to per adapter instance structure
  4191. *
  4192. * Return value
  4193. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  4194. */
  4195. static int pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  4196. {
  4197. int i, buffer_size;
  4198. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4199. for (i = 0; i < pinstance->num_hrrq; i++) {
  4200. pinstance->hrrq_start[i] =
  4201. pci_alloc_consistent(
  4202. pinstance->pdev,
  4203. buffer_size,
  4204. &(pinstance->hrrq_start_bus_addr[i]));
  4205. if (pinstance->hrrq_start[i] == 0) {
  4206. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  4207. i);
  4208. pmcraid_release_host_rrqs(pinstance, i);
  4209. return -ENOMEM;
  4210. }
  4211. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4212. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4213. pinstance->hrrq_end[i] =
  4214. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4215. pinstance->host_toggle_bit[i] = 1;
  4216. spin_lock_init(&pinstance->hrrq_lock[i]);
  4217. }
  4218. return 0;
  4219. }
  4220. /**
  4221. * pmcraid_release_hcams - release HCAM buffers
  4222. *
  4223. * @pinstance: pointer to per adapter instance structure
  4224. *
  4225. * Return value
  4226. * none
  4227. */
  4228. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  4229. {
  4230. if (pinstance->ccn.msg != NULL) {
  4231. pci_free_consistent(pinstance->pdev,
  4232. PMCRAID_AEN_HDR_SIZE +
  4233. sizeof(struct pmcraid_hcam_ccn_ext),
  4234. pinstance->ccn.msg,
  4235. pinstance->ccn.baddr);
  4236. pinstance->ccn.msg = NULL;
  4237. pinstance->ccn.hcam = NULL;
  4238. pinstance->ccn.baddr = 0;
  4239. }
  4240. if (pinstance->ldn.msg != NULL) {
  4241. pci_free_consistent(pinstance->pdev,
  4242. PMCRAID_AEN_HDR_SIZE +
  4243. sizeof(struct pmcraid_hcam_ldn),
  4244. pinstance->ldn.msg,
  4245. pinstance->ldn.baddr);
  4246. pinstance->ldn.msg = NULL;
  4247. pinstance->ldn.hcam = NULL;
  4248. pinstance->ldn.baddr = 0;
  4249. }
  4250. }
  4251. /**
  4252. * pmcraid_allocate_hcams - allocates HCAM buffers
  4253. * @pinstance : pointer to per adapter instance structure
  4254. *
  4255. * Return Value:
  4256. * 0 in case of successful allocation, non-zero otherwise
  4257. */
  4258. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  4259. {
  4260. pinstance->ccn.msg = pci_alloc_consistent(
  4261. pinstance->pdev,
  4262. PMCRAID_AEN_HDR_SIZE +
  4263. sizeof(struct pmcraid_hcam_ccn_ext),
  4264. &(pinstance->ccn.baddr));
  4265. pinstance->ldn.msg = pci_alloc_consistent(
  4266. pinstance->pdev,
  4267. PMCRAID_AEN_HDR_SIZE +
  4268. sizeof(struct pmcraid_hcam_ldn),
  4269. &(pinstance->ldn.baddr));
  4270. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  4271. pmcraid_release_hcams(pinstance);
  4272. } else {
  4273. pinstance->ccn.hcam =
  4274. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  4275. pinstance->ldn.hcam =
  4276. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  4277. atomic_set(&pinstance->ccn.ignore, 0);
  4278. atomic_set(&pinstance->ldn.ignore, 0);
  4279. }
  4280. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  4281. }
  4282. /**
  4283. * pmcraid_release_config_buffers - release config.table buffers
  4284. * @pinstance: pointer to per adapter instance structure
  4285. *
  4286. * Return Value
  4287. * none
  4288. */
  4289. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  4290. {
  4291. if (pinstance->cfg_table != NULL &&
  4292. pinstance->cfg_table_bus_addr != 0) {
  4293. pci_free_consistent(pinstance->pdev,
  4294. sizeof(struct pmcraid_config_table),
  4295. pinstance->cfg_table,
  4296. pinstance->cfg_table_bus_addr);
  4297. pinstance->cfg_table = NULL;
  4298. pinstance->cfg_table_bus_addr = 0;
  4299. }
  4300. if (pinstance->res_entries != NULL) {
  4301. int i;
  4302. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4303. list_del(&pinstance->res_entries[i].queue);
  4304. kfree(pinstance->res_entries);
  4305. pinstance->res_entries = NULL;
  4306. }
  4307. pmcraid_release_hcams(pinstance);
  4308. }
  4309. /**
  4310. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4311. * @pinstance : pointer to per adapter instance structure
  4312. *
  4313. * Return Value
  4314. * 0 for successful allocation, -ENOMEM for any failure
  4315. */
  4316. static int pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4317. {
  4318. int i;
  4319. pinstance->res_entries =
  4320. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4321. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4322. if (NULL == pinstance->res_entries) {
  4323. pmcraid_err("failed to allocate memory for resource table\n");
  4324. return -ENOMEM;
  4325. }
  4326. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4327. list_add_tail(&pinstance->res_entries[i].queue,
  4328. &pinstance->free_res_q);
  4329. pinstance->cfg_table =
  4330. pci_alloc_consistent(pinstance->pdev,
  4331. sizeof(struct pmcraid_config_table),
  4332. &pinstance->cfg_table_bus_addr);
  4333. if (NULL == pinstance->cfg_table) {
  4334. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4335. pmcraid_release_config_buffers(pinstance);
  4336. return -ENOMEM;
  4337. }
  4338. if (pmcraid_allocate_hcams(pinstance)) {
  4339. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4340. pmcraid_release_config_buffers(pinstance);
  4341. return -ENOMEM;
  4342. }
  4343. return 0;
  4344. }
  4345. /**
  4346. * pmcraid_init_tasklets - registers tasklets for response handling
  4347. *
  4348. * @pinstance: pointer adapter instance structure
  4349. *
  4350. * Return value
  4351. * none
  4352. */
  4353. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4354. {
  4355. int i;
  4356. for (i = 0; i < pinstance->num_hrrq; i++)
  4357. tasklet_init(&pinstance->isr_tasklet[i],
  4358. pmcraid_tasklet_function,
  4359. (unsigned long)&pinstance->hrrq_vector[i]);
  4360. }
  4361. /**
  4362. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4363. *
  4364. * @pinstance: pointer to adapter instance structure
  4365. *
  4366. * Return value
  4367. * none
  4368. */
  4369. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4370. {
  4371. int i;
  4372. for (i = 0; i < pinstance->num_hrrq; i++)
  4373. tasklet_kill(&pinstance->isr_tasklet[i]);
  4374. }
  4375. /**
  4376. * pmcraid_release_buffers - release per-adapter buffers allocated
  4377. *
  4378. * @pinstance: pointer to adapter soft state
  4379. *
  4380. * Return Value
  4381. * none
  4382. */
  4383. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4384. {
  4385. pmcraid_release_config_buffers(pinstance);
  4386. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4387. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4388. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4389. if (pinstance->inq_data != NULL) {
  4390. pci_free_consistent(pinstance->pdev,
  4391. sizeof(struct pmcraid_inquiry_data),
  4392. pinstance->inq_data,
  4393. pinstance->inq_data_baddr);
  4394. pinstance->inq_data = NULL;
  4395. pinstance->inq_data_baddr = 0;
  4396. }
  4397. if (pinstance->timestamp_data != NULL) {
  4398. pci_free_consistent(pinstance->pdev,
  4399. sizeof(struct pmcraid_timestamp_data),
  4400. pinstance->timestamp_data,
  4401. pinstance->timestamp_data_baddr);
  4402. pinstance->timestamp_data = NULL;
  4403. pinstance->timestamp_data_baddr = 0;
  4404. }
  4405. }
  4406. /**
  4407. * pmcraid_init_buffers - allocates memory and initializes various structures
  4408. * @pinstance: pointer to per adapter instance structure
  4409. *
  4410. * This routine pre-allocates memory based on the type of block as below:
  4411. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4412. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4413. * config-table entries : DMAable memory using pci_alloc_consistent
  4414. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4415. *
  4416. * Return Value
  4417. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4418. */
  4419. static int pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4420. {
  4421. int i;
  4422. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4423. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4424. pinstance->num_hrrq);
  4425. return -ENOMEM;
  4426. }
  4427. if (pmcraid_allocate_config_buffers(pinstance)) {
  4428. pmcraid_err("couldn't allocate memory for config buffers\n");
  4429. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4430. return -ENOMEM;
  4431. }
  4432. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4433. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  4434. pmcraid_release_config_buffers(pinstance);
  4435. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4436. return -ENOMEM;
  4437. }
  4438. if (pmcraid_allocate_control_blocks(pinstance)) {
  4439. pmcraid_err("couldn't allocate memory control blocks\n");
  4440. pmcraid_release_config_buffers(pinstance);
  4441. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4442. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4443. return -ENOMEM;
  4444. }
  4445. /* allocate DMAable memory for page D0 INQUIRY buffer */
  4446. pinstance->inq_data = pci_alloc_consistent(
  4447. pinstance->pdev,
  4448. sizeof(struct pmcraid_inquiry_data),
  4449. &pinstance->inq_data_baddr);
  4450. if (pinstance->inq_data == NULL) {
  4451. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  4452. pmcraid_release_buffers(pinstance);
  4453. return -ENOMEM;
  4454. }
  4455. /* allocate DMAable memory for set timestamp data buffer */
  4456. pinstance->timestamp_data = pci_alloc_consistent(
  4457. pinstance->pdev,
  4458. sizeof(struct pmcraid_timestamp_data),
  4459. &pinstance->timestamp_data_baddr);
  4460. if (pinstance->timestamp_data == NULL) {
  4461. pmcraid_err("couldn't allocate DMA memory for \
  4462. set time_stamp \n");
  4463. pmcraid_release_buffers(pinstance);
  4464. return -ENOMEM;
  4465. }
  4466. /* Initialize all the command blocks and add them to free pool. No
  4467. * need to lock (free_pool_lock) as this is done in initialization
  4468. * itself
  4469. */
  4470. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4471. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4472. pmcraid_init_cmdblk(cmdp, i);
  4473. cmdp->drv_inst = pinstance;
  4474. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4475. }
  4476. return 0;
  4477. }
  4478. /**
  4479. * pmcraid_reinit_buffers - resets various buffer pointers
  4480. * @pinstance: pointer to adapter instance
  4481. * Return value
  4482. * none
  4483. */
  4484. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4485. {
  4486. int i;
  4487. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4488. for (i = 0; i < pinstance->num_hrrq; i++) {
  4489. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4490. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4491. pinstance->hrrq_end[i] =
  4492. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4493. pinstance->host_toggle_bit[i] = 1;
  4494. }
  4495. }
  4496. /**
  4497. * pmcraid_init_instance - initialize per instance data structure
  4498. * @pdev: pointer to pci device structure
  4499. * @host: pointer to Scsi_Host structure
  4500. * @mapped_pci_addr: memory mapped IOA configuration registers
  4501. *
  4502. * Return Value
  4503. * 0 on success, non-zero in case of any failure
  4504. */
  4505. static int pmcraid_init_instance(struct pci_dev *pdev, struct Scsi_Host *host,
  4506. void __iomem *mapped_pci_addr)
  4507. {
  4508. struct pmcraid_instance *pinstance =
  4509. (struct pmcraid_instance *)host->hostdata;
  4510. pinstance->host = host;
  4511. pinstance->pdev = pdev;
  4512. /* Initialize register addresses */
  4513. pinstance->mapped_dma_addr = mapped_pci_addr;
  4514. /* Initialize chip-specific details */
  4515. {
  4516. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4517. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4518. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4519. pint_regs->ioa_host_interrupt_reg =
  4520. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4521. pint_regs->ioa_host_interrupt_clr_reg =
  4522. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4523. pint_regs->ioa_host_msix_interrupt_reg =
  4524. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  4525. pint_regs->host_ioa_interrupt_reg =
  4526. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4527. pint_regs->host_ioa_interrupt_clr_reg =
  4528. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4529. /* Current version of firmware exposes interrupt mask set
  4530. * and mask clr registers through memory mapped bar0.
  4531. */
  4532. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4533. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4534. pint_regs->ioa_host_interrupt_mask_reg =
  4535. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4536. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4537. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4538. pint_regs->global_interrupt_mask_reg =
  4539. mapped_pci_addr + chip_cfg->global_intr_mask;
  4540. };
  4541. pinstance->ioa_reset_attempts = 0;
  4542. init_waitqueue_head(&pinstance->reset_wait_q);
  4543. atomic_set(&pinstance->outstanding_cmds, 0);
  4544. atomic_set(&pinstance->last_message_id, 0);
  4545. atomic_set(&pinstance->expose_resources, 0);
  4546. INIT_LIST_HEAD(&pinstance->free_res_q);
  4547. INIT_LIST_HEAD(&pinstance->used_res_q);
  4548. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4549. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4550. spin_lock_init(&pinstance->free_pool_lock);
  4551. spin_lock_init(&pinstance->pending_pool_lock);
  4552. spin_lock_init(&pinstance->resource_lock);
  4553. mutex_init(&pinstance->aen_queue_lock);
  4554. /* Work-queue (Shared) for deferred processing error handling */
  4555. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4556. /* Initialize the default log_level */
  4557. pinstance->current_log_level = pmcraid_log_level;
  4558. /* Setup variables required for reset engine */
  4559. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4560. pinstance->reset_cmd = NULL;
  4561. return 0;
  4562. }
  4563. /**
  4564. * pmcraid_shutdown - shutdown adapter controller.
  4565. * @pdev: pci device struct
  4566. *
  4567. * Issues an adapter shutdown to the card waits for its completion
  4568. *
  4569. * Return value
  4570. * none
  4571. */
  4572. static void pmcraid_shutdown(struct pci_dev *pdev)
  4573. {
  4574. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4575. pmcraid_reset_bringdown(pinstance);
  4576. }
  4577. /**
  4578. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4579. */
  4580. static unsigned short pmcraid_get_minor(void)
  4581. {
  4582. int minor;
  4583. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4584. __set_bit(minor, pmcraid_minor);
  4585. return minor;
  4586. }
  4587. /**
  4588. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4589. */
  4590. static void pmcraid_release_minor(unsigned short minor)
  4591. {
  4592. __clear_bit(minor, pmcraid_minor);
  4593. }
  4594. /**
  4595. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4596. *
  4597. * @pinstance: pointer to adapter instance for which to register device
  4598. *
  4599. * Return value
  4600. * 0 in case of success, otherwise non-zero
  4601. */
  4602. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4603. {
  4604. int minor;
  4605. int error;
  4606. minor = pmcraid_get_minor();
  4607. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4608. pinstance->cdev.owner = THIS_MODULE;
  4609. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4610. if (error)
  4611. pmcraid_release_minor(minor);
  4612. else
  4613. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4614. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4615. return error;
  4616. }
  4617. /**
  4618. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4619. *
  4620. * @pinstance: pointer to adapter instance structure
  4621. *
  4622. * Return value
  4623. * none
  4624. */
  4625. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4626. {
  4627. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4628. device_destroy(pmcraid_class,
  4629. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4630. cdev_del(&pinstance->cdev);
  4631. }
  4632. /**
  4633. * pmcraid_remove - IOA hot plug remove entry point
  4634. * @pdev: pci device struct
  4635. *
  4636. * Return value
  4637. * none
  4638. */
  4639. static void pmcraid_remove(struct pci_dev *pdev)
  4640. {
  4641. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4642. /* remove the management interface (/dev file) for this device */
  4643. pmcraid_release_chrdev(pinstance);
  4644. /* remove host template from scsi midlayer */
  4645. scsi_remove_host(pinstance->host);
  4646. /* block requests from mid-layer */
  4647. scsi_block_requests(pinstance->host);
  4648. /* initiate shutdown adapter */
  4649. pmcraid_shutdown(pdev);
  4650. pmcraid_disable_interrupts(pinstance, ~0);
  4651. flush_work(&pinstance->worker_q);
  4652. pmcraid_kill_tasklets(pinstance);
  4653. pmcraid_unregister_interrupt_handler(pinstance);
  4654. pmcraid_release_buffers(pinstance);
  4655. iounmap(pinstance->mapped_dma_addr);
  4656. pci_release_regions(pdev);
  4657. scsi_host_put(pinstance->host);
  4658. pci_disable_device(pdev);
  4659. return;
  4660. }
  4661. #ifdef CONFIG_PM
  4662. /**
  4663. * pmcraid_suspend - driver suspend entry point for power management
  4664. * @pdev: PCI device structure
  4665. * @state: PCI power state to suspend routine
  4666. *
  4667. * Return Value - 0 always
  4668. */
  4669. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4670. {
  4671. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4672. pmcraid_shutdown(pdev);
  4673. pmcraid_disable_interrupts(pinstance, ~0);
  4674. pmcraid_kill_tasklets(pinstance);
  4675. pci_set_drvdata(pinstance->pdev, pinstance);
  4676. pmcraid_unregister_interrupt_handler(pinstance);
  4677. pci_save_state(pdev);
  4678. pci_disable_device(pdev);
  4679. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4680. return 0;
  4681. }
  4682. /**
  4683. * pmcraid_resume - driver resume entry point PCI power management
  4684. * @pdev: PCI device structure
  4685. *
  4686. * Return Value - 0 in case of success. Error code in case of any failure
  4687. */
  4688. static int pmcraid_resume(struct pci_dev *pdev)
  4689. {
  4690. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4691. struct Scsi_Host *host = pinstance->host;
  4692. int rc;
  4693. pci_set_power_state(pdev, PCI_D0);
  4694. pci_enable_wake(pdev, PCI_D0, 0);
  4695. pci_restore_state(pdev);
  4696. rc = pci_enable_device(pdev);
  4697. if (rc) {
  4698. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4699. return rc;
  4700. }
  4701. pci_set_master(pdev);
  4702. if ((sizeof(dma_addr_t) == 4) ||
  4703. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4704. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4705. if (rc == 0)
  4706. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4707. if (rc != 0) {
  4708. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4709. goto disable_device;
  4710. }
  4711. pmcraid_disable_interrupts(pinstance, ~0);
  4712. atomic_set(&pinstance->outstanding_cmds, 0);
  4713. rc = pmcraid_register_interrupt_handler(pinstance);
  4714. if (rc) {
  4715. dev_err(&pdev->dev,
  4716. "resume: couldn't register interrupt handlers\n");
  4717. rc = -ENODEV;
  4718. goto release_host;
  4719. }
  4720. pmcraid_init_tasklets(pinstance);
  4721. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4722. /* Start with hard reset sequence which brings up IOA to operational
  4723. * state as well as completes the reset sequence.
  4724. */
  4725. pinstance->ioa_hard_reset = 1;
  4726. /* Start IOA firmware initialization and bring card to Operational
  4727. * state.
  4728. */
  4729. if (pmcraid_reset_bringup(pinstance)) {
  4730. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4731. rc = -ENODEV;
  4732. goto release_tasklets;
  4733. }
  4734. return 0;
  4735. release_tasklets:
  4736. pmcraid_disable_interrupts(pinstance, ~0);
  4737. pmcraid_kill_tasklets(pinstance);
  4738. pmcraid_unregister_interrupt_handler(pinstance);
  4739. release_host:
  4740. scsi_host_put(host);
  4741. disable_device:
  4742. pci_disable_device(pdev);
  4743. return rc;
  4744. }
  4745. #else
  4746. #define pmcraid_suspend NULL
  4747. #define pmcraid_resume NULL
  4748. #endif /* CONFIG_PM */
  4749. /**
  4750. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4751. * completion of the ioa reset
  4752. * @cmd: pointer to reset command block
  4753. */
  4754. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4755. {
  4756. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4757. unsigned long flags;
  4758. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4759. pmcraid_ioa_reset(cmd);
  4760. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4761. scsi_unblock_requests(pinstance->host);
  4762. schedule_work(&pinstance->worker_q);
  4763. }
  4764. /**
  4765. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4766. *
  4767. * @cmd: pointer to pmcraid_cmd structure
  4768. *
  4769. * Return Value
  4770. * 0 for success or non-zero for failure cases
  4771. */
  4772. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4773. {
  4774. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4775. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4776. pmcraid_reinit_cmdblk(cmd);
  4777. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4778. ioarcb->request_type = REQ_TYPE_IOACMD;
  4779. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4780. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4781. /* If this was called as part of resource table reinitialization due to
  4782. * lost CCN, it is enough to return the command block back to free pool
  4783. * as part of set_supported_devs completion function.
  4784. */
  4785. if (cmd->drv_inst->reinit_cfg_table) {
  4786. cmd->drv_inst->reinit_cfg_table = 0;
  4787. cmd->release = 1;
  4788. cmd_done = pmcraid_reinit_cfgtable_done;
  4789. }
  4790. /* we will be done with the reset sequence after set supported devices,
  4791. * setup the done function to return the command block back to free
  4792. * pool
  4793. */
  4794. pmcraid_send_cmd(cmd,
  4795. cmd_done,
  4796. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4797. pmcraid_timeout_handler);
  4798. return;
  4799. }
  4800. /**
  4801. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4802. *
  4803. * @cmd: pointer to pmcraid_cmd structure
  4804. *
  4805. * Return Value
  4806. * 0 for success or non-zero for failure cases
  4807. */
  4808. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4809. {
  4810. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4811. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4812. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4813. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4814. __le64 timestamp;
  4815. timestamp = ktime_get_real_seconds() * 1000;
  4816. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4817. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4818. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4819. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4820. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4821. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4822. pmcraid_reinit_cmdblk(cmd);
  4823. ioarcb->request_type = REQ_TYPE_SCSI;
  4824. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4825. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4826. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4827. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4828. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4829. offsetof(struct pmcraid_ioarcb,
  4830. add_data.u.ioadl[0]));
  4831. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4832. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4833. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4834. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4835. ioarcb->data_transfer_length =
  4836. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4837. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4838. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4839. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4840. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4841. if (!pinstance->timestamp_error) {
  4842. pinstance->timestamp_error = 0;
  4843. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4844. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4845. } else {
  4846. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4847. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4848. return;
  4849. }
  4850. }
  4851. /**
  4852. * pmcraid_init_res_table - Initialize the resource table
  4853. * @cmd: pointer to pmcraid command struct
  4854. *
  4855. * This function looks through the existing resource table, comparing
  4856. * it with the config table. This function will take care of old/new
  4857. * devices and schedule adding/removing them from the mid-layer
  4858. * as appropriate.
  4859. *
  4860. * Return value
  4861. * None
  4862. */
  4863. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4864. {
  4865. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4866. struct pmcraid_resource_entry *res, *temp;
  4867. struct pmcraid_config_table_entry *cfgte;
  4868. unsigned long lock_flags;
  4869. int found, rc, i;
  4870. u16 fw_version;
  4871. LIST_HEAD(old_res);
  4872. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4873. pmcraid_err("IOA requires microcode download\n");
  4874. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4875. /* resource list is protected by pinstance->resource_lock.
  4876. * init_res_table can be called from probe (user-thread) or runtime
  4877. * reset (timer/tasklet)
  4878. */
  4879. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4880. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4881. list_move_tail(&res->queue, &old_res);
  4882. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4883. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4884. PMCRAID_FW_VERSION_1)
  4885. cfgte = &pinstance->cfg_table->entries[i];
  4886. else
  4887. cfgte = (struct pmcraid_config_table_entry *)
  4888. &pinstance->cfg_table->entries_ext[i];
  4889. if (!pmcraid_expose_resource(fw_version, cfgte))
  4890. continue;
  4891. found = 0;
  4892. /* If this entry was already detected and initialized */
  4893. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4894. rc = memcmp(&res->cfg_entry.resource_address,
  4895. &cfgte->resource_address,
  4896. sizeof(cfgte->resource_address));
  4897. if (!rc) {
  4898. list_move_tail(&res->queue,
  4899. &pinstance->used_res_q);
  4900. found = 1;
  4901. break;
  4902. }
  4903. }
  4904. /* If this is new entry, initialize it and add it the queue */
  4905. if (!found) {
  4906. if (list_empty(&pinstance->free_res_q)) {
  4907. pmcraid_err("Too many devices attached\n");
  4908. break;
  4909. }
  4910. found = 1;
  4911. res = list_entry(pinstance->free_res_q.next,
  4912. struct pmcraid_resource_entry, queue);
  4913. res->scsi_dev = NULL;
  4914. res->change_detected = RES_CHANGE_ADD;
  4915. res->reset_progress = 0;
  4916. list_move_tail(&res->queue, &pinstance->used_res_q);
  4917. }
  4918. /* copy new configuration table entry details into driver
  4919. * maintained resource entry
  4920. */
  4921. if (found) {
  4922. memcpy(&res->cfg_entry, cfgte,
  4923. pinstance->config_table_entry_size);
  4924. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  4925. res->cfg_entry.resource_type,
  4926. (fw_version <= PMCRAID_FW_VERSION_1 ?
  4927. res->cfg_entry.unique_flags1 :
  4928. res->cfg_entry.array_id & 0xFF),
  4929. le32_to_cpu(res->cfg_entry.resource_address));
  4930. }
  4931. }
  4932. /* Detect any deleted entries, mark them for deletion from mid-layer */
  4933. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4934. if (res->scsi_dev) {
  4935. res->change_detected = RES_CHANGE_DEL;
  4936. res->cfg_entry.resource_handle =
  4937. PMCRAID_INVALID_RES_HANDLE;
  4938. list_move_tail(&res->queue, &pinstance->used_res_q);
  4939. } else {
  4940. list_move_tail(&res->queue, &pinstance->free_res_q);
  4941. }
  4942. }
  4943. /* release the resource list lock */
  4944. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  4945. pmcraid_set_timestamp(cmd);
  4946. }
  4947. /**
  4948. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  4949. * @cmd: pointer pmcraid_cmd struct
  4950. *
  4951. * This function sends a Query IOA Configuration command to the adapter to
  4952. * retrieve the IOA configuration table.
  4953. *
  4954. * Return value:
  4955. * none
  4956. */
  4957. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  4958. {
  4959. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4960. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4961. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4962. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  4963. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4964. PMCRAID_FW_VERSION_1)
  4965. pinstance->config_table_entry_size =
  4966. sizeof(struct pmcraid_config_table_entry);
  4967. else
  4968. pinstance->config_table_entry_size =
  4969. sizeof(struct pmcraid_config_table_entry_ext);
  4970. ioarcb->request_type = REQ_TYPE_IOACMD;
  4971. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4972. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  4973. /* firmware requires 4-byte length field, specified in B.E format */
  4974. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  4975. /* Since entire config table can be described by single IOADL, it can
  4976. * be part of IOARCB itself
  4977. */
  4978. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4979. offsetof(struct pmcraid_ioarcb,
  4980. add_data.u.ioadl[0]));
  4981. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4982. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4983. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4984. ioarcb->data_transfer_length =
  4985. cpu_to_le32(sizeof(struct pmcraid_config_table));
  4986. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4987. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4988. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  4989. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  4990. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  4991. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4992. }
  4993. /**
  4994. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  4995. * @pdev: pointer to pci device structure
  4996. * @dev_id: pointer to device ids structure
  4997. *
  4998. * Return Value
  4999. * returns 0 if the device is claimed and successfully configured.
  5000. * returns non-zero error code in case of any failure
  5001. */
  5002. static int pmcraid_probe(struct pci_dev *pdev,
  5003. const struct pci_device_id *dev_id)
  5004. {
  5005. struct pmcraid_instance *pinstance;
  5006. struct Scsi_Host *host;
  5007. void __iomem *mapped_pci_addr;
  5008. int rc = PCIBIOS_SUCCESSFUL;
  5009. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  5010. pmcraid_err
  5011. ("maximum number(%d) of supported adapters reached\n",
  5012. atomic_read(&pmcraid_adapter_count));
  5013. return -ENOMEM;
  5014. }
  5015. atomic_inc(&pmcraid_adapter_count);
  5016. rc = pci_enable_device(pdev);
  5017. if (rc) {
  5018. dev_err(&pdev->dev, "Cannot enable adapter\n");
  5019. atomic_dec(&pmcraid_adapter_count);
  5020. return rc;
  5021. }
  5022. dev_info(&pdev->dev,
  5023. "Found new IOA(%x:%x), Total IOA count: %d\n",
  5024. pdev->vendor, pdev->device,
  5025. atomic_read(&pmcraid_adapter_count));
  5026. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  5027. if (rc < 0) {
  5028. dev_err(&pdev->dev,
  5029. "Couldn't register memory range of registers\n");
  5030. goto out_disable_device;
  5031. }
  5032. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  5033. if (!mapped_pci_addr) {
  5034. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  5035. rc = -ENOMEM;
  5036. goto out_release_regions;
  5037. }
  5038. pci_set_master(pdev);
  5039. /* Firmware requires the system bus address of IOARCB to be within
  5040. * 32-bit addressable range though it has 64-bit IOARRIN register.
  5041. * However, firmware supports 64-bit streaming DMA buffers, whereas
  5042. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  5043. * returns memory within 4GB (if not, change this logic), coherent
  5044. * buffers are within firmware acceptable address ranges.
  5045. */
  5046. if ((sizeof(dma_addr_t) == 4) ||
  5047. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  5048. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5049. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  5050. * bit mask for pci_alloc_consistent to return addresses within 4GB
  5051. */
  5052. if (rc == 0)
  5053. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5054. if (rc != 0) {
  5055. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  5056. goto cleanup_nomem;
  5057. }
  5058. host = scsi_host_alloc(&pmcraid_host_template,
  5059. sizeof(struct pmcraid_instance));
  5060. if (!host) {
  5061. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  5062. rc = -ENOMEM;
  5063. goto cleanup_nomem;
  5064. }
  5065. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  5066. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  5067. host->unique_id = host->host_no;
  5068. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  5069. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  5070. /* zero out entire instance structure */
  5071. pinstance = (struct pmcraid_instance *)host->hostdata;
  5072. memset(pinstance, 0, sizeof(*pinstance));
  5073. pinstance->chip_cfg =
  5074. (struct pmcraid_chip_details *)(dev_id->driver_data);
  5075. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  5076. if (rc < 0) {
  5077. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  5078. goto out_scsi_host_put;
  5079. }
  5080. pci_set_drvdata(pdev, pinstance);
  5081. /* Save PCI config-space for use following the reset */
  5082. rc = pci_save_state(pinstance->pdev);
  5083. if (rc != 0) {
  5084. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  5085. goto out_scsi_host_put;
  5086. }
  5087. pmcraid_disable_interrupts(pinstance, ~0);
  5088. rc = pmcraid_register_interrupt_handler(pinstance);
  5089. if (rc) {
  5090. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  5091. goto out_scsi_host_put;
  5092. }
  5093. pmcraid_init_tasklets(pinstance);
  5094. /* allocate verious buffers used by LLD.*/
  5095. rc = pmcraid_init_buffers(pinstance);
  5096. if (rc) {
  5097. pmcraid_err("couldn't allocate memory blocks\n");
  5098. goto out_unregister_isr;
  5099. }
  5100. /* check the reset type required */
  5101. pmcraid_reset_type(pinstance);
  5102. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  5103. /* Start IOA firmware initialization and bring card to Operational
  5104. * state.
  5105. */
  5106. pmcraid_info("starting IOA initialization sequence\n");
  5107. if (pmcraid_reset_bringup(pinstance)) {
  5108. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  5109. rc = 1;
  5110. goto out_release_bufs;
  5111. }
  5112. /* Add adapter instance into mid-layer list */
  5113. rc = scsi_add_host(pinstance->host, &pdev->dev);
  5114. if (rc != 0) {
  5115. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  5116. goto out_release_bufs;
  5117. }
  5118. scsi_scan_host(pinstance->host);
  5119. rc = pmcraid_setup_chrdev(pinstance);
  5120. if (rc != 0) {
  5121. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  5122. rc);
  5123. goto out_remove_host;
  5124. }
  5125. /* Schedule worker thread to handle CCN and take care of adding and
  5126. * removing devices to OS
  5127. */
  5128. atomic_set(&pinstance->expose_resources, 1);
  5129. schedule_work(&pinstance->worker_q);
  5130. return rc;
  5131. out_remove_host:
  5132. scsi_remove_host(host);
  5133. out_release_bufs:
  5134. pmcraid_release_buffers(pinstance);
  5135. out_unregister_isr:
  5136. pmcraid_kill_tasklets(pinstance);
  5137. pmcraid_unregister_interrupt_handler(pinstance);
  5138. out_scsi_host_put:
  5139. scsi_host_put(host);
  5140. cleanup_nomem:
  5141. iounmap(mapped_pci_addr);
  5142. out_release_regions:
  5143. pci_release_regions(pdev);
  5144. out_disable_device:
  5145. atomic_dec(&pmcraid_adapter_count);
  5146. pci_disable_device(pdev);
  5147. return -ENODEV;
  5148. }
  5149. /*
  5150. * PCI driver structure of pcmraid driver
  5151. */
  5152. static struct pci_driver pmcraid_driver = {
  5153. .name = PMCRAID_DRIVER_NAME,
  5154. .id_table = pmcraid_pci_table,
  5155. .probe = pmcraid_probe,
  5156. .remove = pmcraid_remove,
  5157. .suspend = pmcraid_suspend,
  5158. .resume = pmcraid_resume,
  5159. .shutdown = pmcraid_shutdown
  5160. };
  5161. /**
  5162. * pmcraid_init - module load entry point
  5163. */
  5164. static int __init pmcraid_init(void)
  5165. {
  5166. dev_t dev;
  5167. int error;
  5168. pmcraid_info("%s Device Driver version: %s\n",
  5169. PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
  5170. error = alloc_chrdev_region(&dev, 0,
  5171. PMCRAID_MAX_ADAPTERS,
  5172. PMCRAID_DEVFILE);
  5173. if (error) {
  5174. pmcraid_err("failed to get a major number for adapters\n");
  5175. goto out_init;
  5176. }
  5177. pmcraid_major = MAJOR(dev);
  5178. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  5179. if (IS_ERR(pmcraid_class)) {
  5180. error = PTR_ERR(pmcraid_class);
  5181. pmcraid_err("failed to register with sysfs, error = %x\n",
  5182. error);
  5183. goto out_unreg_chrdev;
  5184. }
  5185. error = pmcraid_netlink_init();
  5186. if (error) {
  5187. class_destroy(pmcraid_class);
  5188. goto out_unreg_chrdev;
  5189. }
  5190. error = pci_register_driver(&pmcraid_driver);
  5191. if (error == 0)
  5192. goto out_init;
  5193. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  5194. error);
  5195. class_destroy(pmcraid_class);
  5196. pmcraid_netlink_release();
  5197. out_unreg_chrdev:
  5198. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  5199. out_init:
  5200. return error;
  5201. }
  5202. /**
  5203. * pmcraid_exit - module unload entry point
  5204. */
  5205. static void __exit pmcraid_exit(void)
  5206. {
  5207. pmcraid_netlink_release();
  5208. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  5209. PMCRAID_MAX_ADAPTERS);
  5210. pci_unregister_driver(&pmcraid_driver);
  5211. class_destroy(pmcraid_class);
  5212. }
  5213. module_init(pmcraid_init);
  5214. module_exit(pmcraid_exit);