pmcraid.c 167 KB

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