pmcraid.c 167 KB

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