pmcraid.c 153 KB

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