pmcraid.c 167 KB

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